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, adaptive step size, reuse of the Jacobian and its factorisation across steps, 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
chas moved by more thanmax_gamma_change(0.3), or afterlu_reuse_steps(20) accepted steps; - the Jacobian itself is rebuilt only after
jac_reuse_steps(51) accepted steps, so a refactorisation reassemblesI - c * Jfrom the storedJwithout 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.
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
initcorrects an inconsistent initial value by a Newton solve on the constraints. diffrax keeps its own copy ofy0, soSaveAt(t0=True)shows the value you passed, not the corrected one. suppress_algebraic_error=Truedrops the algebraic components from the error estimate, which suits problems where onlyzis of interest. On a rapid-equilibrium steady state it saved 29% of steps.- The steady-state event and
ImplicitAdjointwork 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
dtmaxwhen integrating tot1=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 fixedbdf_orderworks 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.
ODETermorSemiExplicitDAETerm.
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.
Metadata
Release files for diffrax-bdf 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| diffrax_bdf-0.1.0.tar.gz | 19.1 kB | With provenance | Details for diffrax_bdf-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| diffrax_bdf-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for diffrax_bdf-0.1.0-py3-none-any.whl |
Total release size: 40.7 kB
Release files / diffrax_bdf-0.1.0.tar.gz
| Download URL | diffrax_bdf-0.1.0.tar.gz |
|---|---|
| Size | 19.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a87bbaec99b0adcf4db84a5362edf89c876dd835a65c1d7ac676827686859e31
|
|
BLAKE2b-256 checksum How to use checksums |
5e31c6f45a4da150a98d1138969e6c5f1dedf27129b25b29658653fc59d5daa1
|
| 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 13, 2026.
Transparency logRelease files / diffrax_bdf-0.1.0-py3-none-any.whl
| Download URL | diffrax_bdf-0.1.0-py3-none-any.whl |
|---|---|
| Size | 21.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c6ed2be84c4817131c33c887160261ac6b34c55b7de8b1132615a144e54a7cfa
|
|
BLAKE2b-256 checksum How to use checksums |
bf20ad5de55c18c0cffbfecdbc7ba53b8a6413222435630c6f1bd6527ba3f8ac
|
| 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 13, 2026.
Transparency logSecurity for diffrax-bdf 0.1.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.
Attestation for diffrax_bdf-0.1.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 13, 2026.
Transparency logA 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.