Getting started¶
Tip
If you're new to diffsol-jax, this page should tell you everything you need
to get started. After that:
- Have a look through the "Examples" on the sidebar for complete programs,
including training loops.
- The API reference documents every option of ODEProblem
and solve.
Installation¶
or with uv:
Requires Python 3.12+ and JAX 0.12+.
Note
The cpu extra installs a CPU PJRT plugin,
which the library uses to compile and run its solver callbacks. To use a
plugin of your own (one built from
openxla/xla for example) point the
environment variable at it instead:
Defining a Problem¶
There are two pieces: ODEProblem,
which holds the equations and the solver, and solve,
which integrates it.
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)
- The vector field is any traceable JAX function
rhs(y, p, t), returning something of the same structure asy. state=2andparams=4declare the shapes: one flat[2]state and one flat[4]parameter vector. (They may also be pytrees; see below.)- The default solver is
bdf. To solve the same equations with a different solver, make another problem:problem.replace(solver="tsit45").
Building a problem constructs the equations and the solver, so build it once and reuse it across solves.
Solving¶
y0 = jnp.array([1.0, 0.5])
params = jnp.array([1.5, 1.0, 0.75, 3.0])
t0, t1 = 0.0, 10.0
ys = solve(problem, y0, params, t0, t1)
By default only the solution at t1 is returned. Pass saveat to get the
solution at chosen times in [t0, t1]:
saveat = jnp.linspace(t0, t1, 200)
ys = solve(problem, y0, params, t0, t1, saveat=saveat) # [200, 2]
solve works under jax.jit and jax.vmap.
ys = jax.jit(solve)(problem, y0, params, t0, t1)
many = jax.vmap(solve, (None, None, 0, None, None))(problem, y0, param_batch, t0, t1)
ics = jax.vmap(solve, (None, 0, None, None, None))(problem, y0_batch, params, t0, t1)
Computing Gradients¶
solve is differentiable with respect to both y0 and params. jax.grad
and jax.jacrev use the diffsol adjoint; jax.jvp and jax.jacfwd use
forward sensitivities. See Computing gradients.
def loss(p):
return jnp.sum(solve(problem, y0, p, t0, t1) ** 2)
grads = jax.grad(loss)(params)
jac = jax.jacfwd(lambda p: solve(problem, y0, p, t0, t1))(params)
PyTrees as the State and the Parameters¶
state= and params= each take an int or a template pytree, whose float-array
leaves give the shapes and dtypes. The equations then act on pytrees of that
structure:
problem = ODEProblem(
lambda y, p, t: {"pos": y["vel"], "vel": -p.k * y["pos"]},
state={"pos": jnp.zeros(3), "vel": jnp.zeros(3)},
params=Spring(k=jnp.array(1.0)),
)
ys = solve(problem, {"pos": pos0, "vel": vel0}, params, t0, t1, saveat=saveat)
ys["pos"] # [nt, 3]
Only the shapes and dtypes of the template's float-array leaves are read; any
other leaf is treated as a constant. In particular the parameters can be a
neural network, and jax.grad returns a network of gradients that
Optax applies directly:
problem = ODEProblem(lambda y, net, t: net(y), state=4, params=net, solver="tsit45")
ys = solve(problem, y0, net, t0, t1, saveat=saveat)
grads = jax.grad(lambda net: loss(solve(problem, y0, net, t0, t1)))(net)
See the Neural ODE example for a full training loop.
Precision¶
A problem's dtype is float32 by default, or float64 once
jax_enable_x64 is on. The solver, its callbacks, and y0, params, ys,
the times and the gradients all work in that dtype. The default tolerances
follow it: 1e-4 for float32, 1e-6 for float64.