Skip to main content

diffrax-bdf

A backward differentiation formula solver for diffrax, aimed at stiff steady-state problems — in particular the kinetic reaction networks in enzax.

diffrax has no BDF (#8). This package supplies one as an ordinary AbstractSolver subclass, with no patching of diffrax itself.

import diffrax
import jax.numpy as jnp
from diffrax_bdf import BDF, BDFController

sol = diffrax.diffeqsolve(
    diffrax.ODETerm(vector_field),
    BDF(),                       # variable order 1-5, as scipy and CVODE do
    t0=0.0,
    t1=jnp.inf,
    dt0=1e-6,
    y0=guess,
    args=parameters,
    stepsize_controller=BDFController(rtol=1e-8, atol=1e-10, dtmax=1e6),
    event=diffrax.Event(diffrax.steady_state_event()),
    adjoint=diffrax.ImplicitAdjoint(),
    saveat=diffrax.SaveAt(t1=True),
)

Pin the order with BDF(bdf_order=3) to use any adaptive step size controller, including diffrax's own PIDController.

Status

Variable order 1-5 with stability limit detection, adaptive step size, reuse of the Jacobian and its factorisation across steps (also under vmap), and semi-explicit index-1 DAEs.

Method

The formulation follows scipy.integrate.BDF and MATLAB's ode15s: the history is a backward-difference array rescaled on each change of step size, with the Shampine & Reichelt NDF correction available via use_ndf (False gives the classical BDF coefficients, the family SUNDIALS CVODE implements). The order is chosen each step by scipy's heuristic, comparing the error estimate at the current order against what it would have been one order lower and one order higher.

The linear-algebra reuse policy is CVODE's, because that is where CVODE's advantage over scipy actually lives. Kvaerno5 already computes one Jacobian per step and reuses it across its five stages, so "one Newton solve per step instead of five stage solves" buys only a few linear solves. The real saving is keeping J and the factorisation of I - c * J across steps, gated on two separate counters:

  • the factorisation is rebuilt when c has moved by more than max_gamma_change (0.3), or after lu_reuse_steps (20) accepted steps;
  • the Jacobian itself is rebuilt only after jac_reuse_steps (51) accepted steps, so a refactorisation reassembles I - c * J from the stored J without touching the vector field.

BDFController adds CVODE's step-size deadband on top of diffrax.PIDController: an accepted step keeps its step size unless the controller wants to grow it by at least 1.5x, and never shrinks it. Holding h still holds c still, which is what lets the factorisation survive.

It also carries the order. diffrax asks a solver for its error order once, before the solve (solver.error_order(terms) sees neither state nor time), so a variable-order solver cannot report a per-step order through that route. The only other channel to the controller is y_error, which the integration loop treats opaquely, so the order travels inside it.

Everything is written for static shapes -- the order is a traced integer used to mask fixed-size buffers -- because diffrax carries the solver state through a lax.while_loop.

Order selection on a steady-state solve

Integrating the n=3 problem below to steady state, the order climbs through the transient and then falls back as the residual collapses and the step grows:

order:  1 2 2 3 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 2 1 1 1 1 1 1 1
step:   1.2e-07  ------------------------------------------------>  7.9e+05

The tail is the useful part: BDF1 with an enormous step is Newton's method on f(y) = 0, so a correct variable-order BDF turns into a steady-state solver by itself as it converges.

Stability limit detection

BDF orders 3 to 5 are only A(α)-stable, so an eigenvalue close to the imaginary axis makes them unstable at moderate step sizes. A weakly damped oscillation then holds the step size at the edge of stability. scipy's heuristic keeps the order high because the solution looks smooth, and the solve stalls short of its steady state. On a variant of enzax's glycolysis model, a mode 87 degrees from the negative real axis made BDF hit 10,000 steps where Kvaerno5 needed about 200.

BDF therefore runs SUNDIALS CVODE's stability limit detection (STALD). From the last five steps it estimates whether the error is growing at the rate a stability limit produces, and if so lowers the highest order the solve may use. Unlike CVODE, which lowers only the next order, the cap holds for the rest of the solve, because scipy's heuristic otherwise raises the order straight back. Turn it off with stability_limit_detection=False. It costs nothing measurable on a single solve, and about 13% under vmap on a 19-state model.

Batching with vmap

Under vmap, a lax.cond whose predicate differs between members becomes a select, which runs both branches. Done naively, that would rebuild the Jacobian and its factorisation at every step for every member. Instead, each reuse decision runs its branch only when some member needs it, and each member keeps only its own refresh. On enzax's three-experiment glycolysis case study, this took a batched solve from 922 ms to 267 ms, against 374 ms for Kvaerno5. All members still step together until the last one finishes.

Differential algebraic equations

BDF also solves semi-explicit index-1 DAEs, where some components of y obey differential equations and the rest obey algebraic constraints:

term = SemiExplicitDAETerm(vector_field, algebraic_mask=(False, True))

Here vector_field returns (f, g) for y = (z, u), meaning dz/dt = f and 0 = g. The corrector solves the constraints in the same chord iteration as the differential equations, with iteration matrix M − cJ where M is 1 on differential components and 0 on algebraic ones, and reuses its factorisation across steps as for an ODE.

  • By default init corrects an inconsistent initial value by a Newton solve on the constraints. diffrax keeps its own copy of y0, so SaveAt(t0=True) shows the value you passed, not the corrected one.
  • suppress_algebraic_error=True drops the algebraic components from the error estimate, which suits problems where only z is of interest. On a rapid-equilibrium steady state it saved 29% of steps.
  • The steady-state event and ImplicitAdjoint work unchanged: the steady state is the root of (f, g).

Caveats

  • Enable jax_enable_x64. High-order backward differences suffer heavy cancellation; above order 2 they are noise in float32.
  • Set dtmax when integrating to t1=inf. As the residual collapses the controller's growth factor saturates and the step grows without bound until it overflows the time variable.
  • Variable order requires BDFController; a fixed bdf_order works with any adaptive controller.
  • The Jacobian is formed densely, so this suits small to medium systems.
  • Requires a term whose control is a scalar, i.e. ODETerm or SemiExplicitDAETerm.
  • Stability limit detection lowers the order for the rest of the solve, so a solve that meets an oscillatory transient may finish at order 2 with small steps. Kvaerno5 can be faster on such problems.

Performance

Steady-state solves to rtol=1e-8, CPU, float64, medians of interleaved rounds. Compare within a block, not across them -- absolute timings drift between runs.

Whether BDF beats Kvaerno5 depends entirely on the problem. Where Kvaerno5's step control struggles, BDF wins by a wide margin; where it does not, Kvaerno5 wins on step count.

problem solver steps rejected median
fast equilibrium, n=3 Kvaerno5 7784 4563 192.4 ms
fast equilibrium, n=3 BDF(bdf_order=5) 276 14 3.5 ms
fast equilibrium, n=3 BDF() variable 231 6 3.3 ms
stiff random network, n=18 Kvaerno5 212 14 23.6 ms
stiff random network, n=18 BDF(bdf_order=5) 593 11 28.8 ms
stiff random network, n=18 BDF() variable 589 12 31.7 ms
stiff random network, n=60 Kvaerno5 193 7 73.6 ms
stiff random network, n=60 BDF(bdf_order=5) 586 6 145.2 ms
stiff random network, n=60 BDF() variable 580 7 153.7 ms

The first problem is the interesting one: Kvaerno5 rejects 59% of its steps, where BDF rejects 2-5%.

Variable order helps where the order genuinely needs to move -- 231 steps against 276 for the best fixed order on the steady-state problem. On the random networks, where order 5 is right throughout, it matches fixed order 5 on step count and pays about 8% per step for the heuristic. Reuse is worth a further 16-18%.

On enzax's models

The motivating case. Measured against enzax's own steady-state configuration (t1=inf, steady_state_event, ImplicitAdjoint, rtol=atol=1e-9), each solver with the step size controller that suits it. Medians of interleaved rounds.

model n regime Kvaerno5 (enzax today) BDF() speedup
methionine 5 bad guess 555 ms (649 steps, 145 rejected) 158 ms (423, 52) 3.5x
methionine 5 warm guess, 2% parameter jitter 30.7 ms (38 steps) 23.3 ms (88) 1.3x
methionine 5 jacrev through the solve 27.9 ms 22.4 ms 1.25x
conserved moiety 5 bad guess 191 ms (225, 30) 53.7 ms (293, 4) 3.6x
conserved moiety 5 warm guess, 2% jitter 58.3 ms (68) 31.4 ms (165) 1.9x
conserved moiety 5 jacrev 53.0 ms 27.6 ms 1.9x
glycolysis 18 bad guess 2302 ms (1007, 247) 271 ms (821, 70) 8.5x
glycolysis 18 guess perturbed 20% 491 ms (212, 38) 108 ms (277, 18) 4.6x
glycolysis 18 jacrev 427 ms 104 ms 4.1x

"bad guess" is enzax's own BAD_GUESS = full(shape, 0.01). Steady states agree with Kvaerno5 to 2e-7 relative or better and gradients to 1e-5 or better.

Note the step counts: BDF often takes more steps and is still several times faster, because a step is one Newton solve against a reused factorisation rather than five stage solves. It also rejects far fewer steps -- 70 against 247 on glycolysis from a bad guess. Compile time is 30-45% lower throughout, which matters when every leapfrog step of a NUTS chain pays it.

The gap widens with model size, which is the encouraging part: 1.3x on the 5-state methionine model against 4-8x on the 18-state glycolysis one.

Tuning

enzax's PIDController(pcoeff=0.1, icoeff=0.3) is tuned for Kvaerno5, which rejects heavily; it damps step size growth to 0.4 / error_order where the default pure-I controller uses 1 / error_order. BDF rejects very few steps and does not need the damping, so it should be left on BDFController's defaults. Using enzax's tuning costs BDF roughly a factor of two -- 174 steps and 45.6 ms against 88 steps and 26.9 ms on methionine.

Testing

uv run pytest. scipy's BDF is pure Python and is used as a white-box oracle: the coefficient tables, the difference-array rescaling, and a single pinned step are all asserted against it directly. The reuse policy, the deadband and the order-selection heuristic are tested directly as well, since without them the solver has no advantage over Kvaerno5 at all. Stability limit detection is checked against a line-by-line NumPy port of CVODE's cvSLdet (tests/stald_reference.py), on random histories and on histories recorded from a stalling solve.

Licence

MIT, except src/diffrax_bdf/_stald.py and tests/stald_reference.py, which are ported from SUNDIALS and keep its BSD-3-Clause licence; see LICENSES/.

Metadata

Release files for diffrax-bdf 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for diffrax-bdf 0.2.0
File Size Uploaded Provenance Actions
diffrax_bdf-0.2.0.tar.gz 26.5 kB With provenance Details for diffrax_bdf-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for diffrax-bdf 0.2.0
File Interpreter ABI Platform Actions
diffrax_bdf-0.2.0-py3-none-any.whl Python 3 none any Details for diffrax_bdf-0.2.0-py3-none-any.whl

Total release size: 56.3 kB

Release files / diffrax_bdf-0.2.0.tar.gz

Download URL diffrax_bdf-0.2.0.tar.gz
Size 26.5 kB
Tags Source
SHA-256 checksum
How to use checksums
2150b668c8d96eda67a1887ef445ee5a29dd51f63edb8ec08ae612b13b4146bf
BLAKE2b-256 checksum
How to use checksums
519f07fc125bfa8554f7183e9be8be02ce366695a128b2f99e77ef2300a03b86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

Release files / diffrax_bdf-0.2.0-py3-none-any.whl

Download URL diffrax_bdf-0.2.0-py3-none-any.whl
Size 29.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0f1ed33d0050b645ca0714d78e2ef35eae74961eb74b18be4b343400fd5d1d7b
BLAKE2b-256 checksum
How to use checksums
fd530b2ad4d0b7a9e08ec36352ecfc9d6650e65f08f74256f47c47e1eb6e7996
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

Security for diffrax-bdf 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for diffrax_bdf-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page