Skip to content

✨ feat: default max_steps from StepTo, and a fixed_steps helper - #61

Merged
nstarman merged 3 commits into
GalacticDynamics:mainfrom
nstarman:feat/stepto-max-steps
Nov 23, 2026
Merged

nstarman merged 3 commits into
GalacticDynamics:mainfrom
nstarman:feat/stepto-max-steps

Conversation

@nstarman

@nstarman nstarman commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

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_steps and fixed-grid behaviour from one place.

max_steps="auto"

DiffEqSolver's max_steps field now defaults to "auto". At solve time it becomes len(ts) - 1 when the stepsize controller is a diffrax.StepTo, and diffrax's own default (4096) for any other controller. An integer or None, on the field or in the call, is used as is. You can also pass max_steps="auto" to a call when the solver holds an integer.

With RecursiveCheckpointAdjoint, max_steps sets how many checkpoints the backward pass stores, so an exact bound saves memory. It's resolved at solve time, so swapping the grid with eqx.tree_at picks up the new step count.

fixed_steps

diffraxtra.fixed_steps(t0, dt, n_steps) returns (t0, t1, StepTo, max_steps) for n_steps steps of size dt. It builds the grid t0 + dt * arange(n_steps + 1) behind jax.lax.optimization_barrier. The barrier stops XLA's simplifier from rewriting t1 and the grid separately with different roundings, and StepTo rejects a t1 that is one rounding off ts[-1]. I couldn't reproduce that failure on CPU without the barrier, so it is a guard taken over from galax's evolve, not a fix for a case shown here. t0 and dt can be traced, so it works under jit, vmap and grad, and a negative dt runs 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.

t0, t1, stepto, _ = diffraxtra.fixed_steps(t0, dt, 1000)
solver = diffraxtra.DiffEqSolver(dfx.Tsit5(), stepsize_controller=stepto)
soln = solver(term, t0, t1, None, y0)  # max_steps is 1000

Visible changes

  • DiffEqSolver().max_steps now reads "auto" instead of 4096. Code that reads the field and passes it straight to diffrax.diffeqsolve needs to resolve it first.
  • Under a StepTo controller, SaveAt(steps=True) output now has len(ts) - 1 rows instead of 4096 padded rows. Dense-output buffers shrink the same way.
  • The dependency floors are now diffrax>=0.6.2 and equinox>=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 can vmap the barrier. With the old floors, jax.grad through any diffrax.diffeqsolve also 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 StepTo grid takes exactly len(ts) - 1 steps, so the solution is the same with max_steps=4096 and with the exact bound. One case changes for the better: a StepTo grid 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 and skills/diffraxtra/SKILL.md. They cover:

  • the default on a StepTo grid, and the fallback to 4096 for other controllers;
  • an explicit max_steps on the field and in the call, a per-call "auto", and a per-call None that stays unbounded;
  • a backward run;
  • jit, grad and vmap with traced t0 and dt, checked against the exact gradients;
  • the n_steps and 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

nstarman and others added 2 commits November 23, 2026 12:01
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

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@4bea833). Learn more about missing BASE report.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nstarman nstarman added this to the v1.7.0 milestone Nov 23, 2026
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>
@nstarman
nstarman merged commit c9baeac into GalacticDynamics:main Nov 23, 2026
22 checks passed
@nstarman
nstarman deleted the feat/stepto-max-steps branch November 23, 2026 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant