Repository navigation
✨ feat: default max_steps from StepTo, and a fixed_steps helper - #61
Merged
Merged
Conversation
DiffEqSolver's max_steps field now defaults to "auto". At solve time it becomes len(ts) - 1 for a diffrax.StepTo controller, and diffrax's own default otherwise. An integer or None, on the field or in the call, is used as is, and passing max_steps="auto" to a call asks for the same resolution. fixed_steps(t0, dt, n_steps) returns (t0, t1, StepTo, max_steps) for n_steps fixed steps of size dt. The grid is built behind jax.lax.optimization_barrier, so XLA can't round t1 and ts[-1] differently. t0 and dt can be traced, and a negative dt runs backward. Refs GalacticDynamics/galax#1090. BREAKING CHANGE: DiffEqSolver().max_steps is now the string "auto" instead of the int 4096. Code that reads the field and passes it straight to diffrax.diffeqsolve breaks. Solves with a StepTo controller now use len(ts) - 1 instead of 4096, so SaveAt(steps=True) output is that long. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With equinox 0.11.5 to 0.11.7 and jax 0.4.35, jax.grad through any diffrax.diffeqsolve fails with a custom JVP float0 error inside the checkpointed while loop. CI's lowest-resolution job never took a gradient, so it didn't catch this. equinox 0.11.8 works. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #61 +/- ##
=======================================
Coverage ? 98.51%
=======================================
Files ? 6
Lines ? 202
Branches ? 0
=======================================
Hits ? 199
Misses ? 3
Partials ? 0 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
These match diffrax 0.6.2's own requirements, which also bring jax>=0.4.38. That jax can vmap jax.lax.optimization_barrier, so fixed_steps drops its custom_vmap shim and keeps only the identity JVP (the JVP rule arrived in jax 0.5.3). jax 0.4.38 deprecates jax.core names that equinox 0.11.10 still uses, so pytest ignores those warnings. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds two small pieces for fixed-step solves. They're option 1 of GalacticDynamics/galax#1090, so galax's front ends can take their
max_stepsand fixed-grid behaviour from one place.max_steps="auto"DiffEqSolver'smax_stepsfield now defaults to"auto". At solve time it becomeslen(ts) - 1when the stepsize controller is adiffrax.StepTo, and diffrax's own default (4096) for any other controller. An integer orNone, on the field or in the call, is used as is. You can also passmax_steps="auto"to a call when the solver holds an integer.With
RecursiveCheckpointAdjoint,max_stepssets how many checkpoints the backward pass stores, so an exact bound saves memory. It's resolved at solve time, so swapping the grid witheqx.tree_atpicks up the new step count.fixed_stepsdiffraxtra.fixed_steps(t0, dt, n_steps)returns(t0, t1, StepTo, max_steps)forn_stepssteps of sizedt. It builds the gridt0 + dt * arange(n_steps + 1)behindjax.lax.optimization_barrier. The barrier stops XLA's simplifier from rewritingt1and the grid separately with different roundings, andStepTorejects at1that is one rounding offts[-1]. I couldn't reproduce that failure on CPU without the barrier, so it is a guard taken over from galax'sevolve, not a fix for a case shown here.t0anddtcan be traced, so it works underjit,vmapandgrad, and a negativedtruns backward. On jax older than 0.5.3 the barrier has no JVP rule, so the helper gives it an identity JVP by hand. There's a TODO to drop it once the jax floor reaches 0.5.3.Visible changes
DiffEqSolver().max_stepsnow reads"auto"instead of4096. Code that reads the field and passes it straight todiffrax.diffeqsolveneeds to resolve it first.StepTocontroller,SaveAt(steps=True)output now haslen(ts) - 1rows instead of 4096 padded rows. Dense-output buffers shrink the same way.diffrax>=0.6.2andequinox>=0.11.10, matching what diffrax 0.6.2 itself requires, and that brings jax 0.4.38 or later. jax 0.4.38 is the first version that canvmapthe barrier. With the old floors,jax.gradthrough anydiffrax.diffeqsolvealso failed (equinox 0.11.5 to 0.11.7 with jax 0.4.35), and the lowest-resolution CI job never took a gradient, so it didn't catch this.Results and gradients are unchanged. A
StepTogrid takes exactlylen(ts) - 1steps, so the solution is the same withmax_steps=4096and with the exact bound. One case changes for the better: aStepTogrid with more than 4096 steps used to stop with a max-steps error, and now it runs.Tests
The tests are doctests in
DiffEqSolver,fixed_steps, the README andskills/diffraxtra/SKILL.md. They cover:StepTogrid, and the fallback to 4096 for other controllers;max_stepson the field and in the call, a per-call"auto", and a per-callNonethat stays unbounded;jit,gradandvmapwith tracedt0anddt, checked against the exact gradients;n_stepsand scalar checks.The suite passes on the latest dependencies and with
--resolution lowest(jax 0.4.38, equinox 0.11.10, diffrax 0.6.2).Follow-up, not in this PR: cubic Hermite dense output for leapfrog-type solvers.
🤖 Generated with Claude Code