Skip to content

Regression test and warning for the Diffrax fixed-step checkpoint-adjoint defect - #68

Open
rogeriojorge wants to merge 1 commit into
agent/autodiff-planfrom
fix/fixed-step-adjoint-guard
Open

rogeriojorge wants to merge 1 commit into
agent/autodiff-planfrom
fix/fixed-step-adjoint-guard

Conversation

@rogeriojorge

@rogeriojorge rogeriojorge commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Stacked on #44 (base agent/autodiff-plan); no CI runs on a stacked base, tests were run locally.

Problem. With adaptive_time_step=False, Diffrax 0.7.2 RecursiveCheckpointAdjoint returns wrong gradients when many SaveAt(ts) times fall exactly on step ends, which linspace(0, t_max, timesteps) usually does. Forward values are right. On the Orszag-Tang test case (201 saves, dt = 0.01, max_steps = 220) the default adjoint is 2.5% off ForwardMode. The existing tests use at most 5 saves and miss it. Upstream report: patrick-kidger/diffrax#788

Change.

  • test_fixed_step_many_saves_default_adjoint_matches_forward_mode: strict xfail, so it fails loudly once Diffrax is fixed.
  • test_fixed_step_many_saves_one_checkpoint_per_step_is_exact: the workaround RecursiveCheckpointAdjoint(checkpoints=max_steps) matches ForwardMode to 1e-9.
  • Docstring warning on adjoint with the workarounds.

Measured cost (same case, laptop CPU):

adjoint rel. error grad time compiled temp memory
default RCA 2.5e-2 1.49 s 40 MB
RCA(checkpoints=220) 1.2e-15 1.18 s 73 MB
DirectAdjoint 1.5e-15 1.50 s 87 MB

No automatic default. dt and t_max are traced inside the jitted simulation, so the step count is not known at trace time. checkpoints=max_steps with the default max_steps=1e6 would store a million states, and DirectAdjoint memory also grows with max_steps. So the PR documents the workaround and does not change the default.

Tests: pytest -n 2 tests gives 19 passed, 1 xfailed.

With adaptive_time_step=False and many saves on step ends, Diffrax 0.7.2
RecursiveCheckpointAdjoint gives gradients a few percent off (2.5% for 201
saves at dt = 0.01). Add a strict-xfail regression test against ForwardMode,
a passing test of the exact workaround (checkpoints = max_steps) and a
docstring warning.
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