Computing Gradients¶
jax.grad, jax.jacrev, jax.jvp and jax.jacfwd all work on solve, with
respect to both y0 and params. For the numerical details of the diffsol
implementations, see the
diffsol book.
Forward mode (jax.jvp, jax.jacfwd) always uses diffsol
forward sensitivities: an augmented system solved alongside the state,
efficient when the number of parameters is small. Reverse mode (jax.grad,
jax.jacrev) depends on the ad= argument of the solve:
ad="adjoint"(the default) solves the diffsol adjoint equations backwards in time. One backward solve produces the gradient with respect to all parameters at once.ad="forward"transposes the forward sensitivities. This avoids the backward solve, and can be cheaper when there are few parameters.
The Problem¶
A solve integrates an initial value problem:
where \(u(t) \in \mathbb{R}^{n}\) is the state, \(p \in \mathbb{R}^{m}\) the parameters, and \(M\) an optional mass matrix. Internally the initial state is appended to the parameter vector.
Forward Sensitivities¶
Define \(S(t) = \partial u(t) / \partial p\). Differentiating the ODE with respect to \(p\) gives the augmented system
which diffsol solves together with the state, using the same solver and
tolerances. The Jacobians are never formed: each column of the right-hand side
is a Jacobian-vector product of \(f\), derived from your rhs with jax.jvp.
Given a tangent \(\dot p\), forward mode returns \(\dot u = S \dot p\).
The adjoint¶
For jax.grad of a scalar loss built from the solution, the adjoint method
solves
backwards from \(t_N\) to \(t_0\), with \(\lambda\) receiving a jump at each output
time (the cotangent that JAX propagates in). The parameter gradient accumulates
\(\partial f/\partial p^{\top} \lambda\) along the way. jax.jacrev batches its
cotangent directions through a single backward solve.
Checkpointing¶
The backward solve needs the forward trajectory, so the forward pass keeps a
checkpoint. A problem keeps at most max_checkpoints of them alive.
Limitations¶
- No higher-order derivatives.
grad(grad(...))is not supported: the sensitivity and adjoint solves are not themselves differentiated. - No gradient with respect to
t0,t1orsaveat. The times are constants; their cotangent is a symbolic zero. - The mass matrix must not depend on the parameters. See DAEs.
- The adjoint does not support events. A problem with a
rootandresetdefaults toad="forward"; see Discrete events.