Skip to content

diffsol-jax

diffsol-jax exposes diffsol, a Rust library of ODE and DAE solvers, as JAX operations. You write the equations as ordinary jax.numpy functions; the solves run in diffsol.

Features include:

  • stiff solvers (BDF, ESDIRK, TR-BDF2) and an explicit solver (Tsitouras 5(4));
  • semi-explicit index-1 DAEs;
  • forward and adjoint derivatives via jax.grad, jax.jacrev, jax.jvp and jax.jacfwd;
  • batching via jax.vmap, with a vmapped axis becoming lanes of a single solve;
  • using a PyTree as the state and the parameters;
  • single and double precision;
  • support for neural differential equations.

Sharp bits

  • CPU only, for now.
  • No higher-order derivatives. grad(grad(...)) does not work.
  • Small non-stiff problems are slow. Each solver callback carries a fixed overhead of a few microseconds, which is the whole runtime for a two-state system: on those, an unbatched solve is an order of magnitude faster in diffrax.

Installation

pip install "diffsol-jax[cpu]"

Requires Python 3.12+ and JAX 0.12+. The cpu extra installs a CPU PJRT plugin, which the library needs to compile its callbacks. See Getting started for using a plugin of your own.

Quick Example

import jax
import jax.numpy as jnp
from diffsol_jax import ODEProblem, solve


def lotka_volterra(y, p, t):
    alpha, beta, delta, gamma = p
    return jnp.array(
        [alpha * y[0] - beta * y[0] * y[1], delta * y[0] * y[1] - gamma * y[1]]
    )


problem = ODEProblem(lotka_volterra, state=2, params=4)

y0 = jnp.array([1.0, 0.5])
params = jnp.array([1.5, 1.0, 0.75, 3.0])
t0, t1 = 0.0, 10.0
saveat = jnp.linspace(t0, t1, 200)

ys = solve(problem, y0, params, t0, t1, saveat=saveat)  # [200, 2]

Here, an ODEProblem holds the equations and the solver, and solve integrates it. The default solver is bdf, diffsol's variable-order BDF method. It can then be differentiated as native JAX operation:

def loss(p):
    return jnp.sum(solve(problem, y0, p, t0, t1, saveat=saveat) ** 2)


grads = jax.grad(loss)(params)

Next Steps

Have a look at the Getting started page, then the "Examples" on the sidebar for complete programs such as training a neural ODE.

For the solvers themselves, their methods, error control and the underlying adjoint, see the diffsol book.

See Also: Other Libraries in the JAX Ecosystem

Always useful
Equinox: neural networks and everything not already in core JAX!
jaxtyping: type annotations for shape/dtype of arrays.

Deep learning
Optax: first-order gradient (SGD, Adam,...) optimisers.
paramax: parameterizations and constraints for PyTrees.

Scientific computing
diffrax: numerical differential equation solvers in pure JAX.
Optimistix: root finding, minimisation, fixed points, and least squares.
Lineax: linear solvers.
BlackJAX: probabilistic+Bayesian sampling.
sympy2jax: SymPy<->JAX conversion; train symbolic expressions via gradient descent.
PySR: symbolic regression. (Non-JAX honourable mention!)

Awesome JAX
Awesome JAX: a longer list of other JAX projects.