Manuscript Offprint

arXiv:2609.02876

GRADSOLVE: fast exact gradients for ODE ensembles on GPUs

Alessio Spurio Mancini

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Ordinary differential equations (ODEs) underlie models in science and engineering, and many applications need derivatives of their solutions with respect to parameters. Ensembles of independent trajectories suit graphics processing units (GPUs), but current GPU software forces a trade-off: the fastest ensemble solvers cannot be differentiated in reverse mode at the speed they solve, and the solvers built for differentiation solve more slowly. No single tool has yet offered a reverse-mode gradient at the speed of a fused-kernel solve.

We present GRADSOLVE, an open-source JAX library for solving and reverse-mode differentiating low-dimensional ODE ensembles on NVIDIA GPUs. It records the steps an adaptive solver accepts and differentiates a fixed-step replay of them; the returned gradient is the exact discrete adjoint of those steps, the same derivative Diffrax returns by default, obtained more cheaply from a fixed-length chain than from an adaptive loop. It targets ensembles differentiated many times against one recorded mesh, keeps Diffrax as a fallback, and supports explicit and Rosenbrock integrators.

Used as a solver, GRADSOLVE's forward-only kernel ran 2.8× faster than DiffEqGPU.jl; used for gradients, once a record exists, it computed them 5.6–14.1× faster than Diffrax's checkpointed adjoint at matched forward-state accuracy across three GPU generations, the advantage narrowing on large ensembles and, on stiff systems, down to parity at tight accuracy. GRADSOLVE is released at https://github.com/ECLIPSE-AI4Science/gradsolve.

Keywords: reverse-mode differentiation; ODE ensembles; GPU computing; sensitivity analysis; differentiable programming; scientific machine learning

1 Introduction

Ordinary differential equations (ODEs) underpin quantitative modeling throughout science and engineering, and the equations of practical interest only rarely admit closed-form solutions [16, 17]. Their numerical integration is therefore a routine component of scientific computing, supported by mature solver suites in every major ecosystem [18, 33, 43, 53], and the solver's efficiency frequently governs the overall cost of the computation it is embedded in, particularly when the same equation must be solved many times. Alongside raw solver efficiency, a second requirement has become equally central: the ability to differentiate the solution of an ODE with respect to its parameters. Such gradients are required in several settings. In the training of neural differential equations, models in which a neural network forms part of the right-hand side of the equation, a loss must be differentiated through the numerical integrator at each optimization step [8, 20, 34]. In the calibration of mechanistic models, rate constants, initial conditions, or forcing terms are adjusted iteratively until the simulated trajectory reproduces observed data [37, 48]. Sensitivity analysis quantifies the response of the solution to perturbations of the inputs [1, 6, 42], and optimal control seeks the control trajectory minimizing a prescribed cost functional [3, 12]. Each of these tasks requires the same quantity, the gradient of an ODE solution with respect to its parameters, and whenever the parameters outnumber the outputs, as is typical, reverse-mode differentiation, which obtains the whole gradient from one backward sweep through the computation, needs the fewest passes.

In practice, such gradients are seldom required for a single trajectory in isolation. Calibration procedures, for example, evaluate many parameter settings simultaneously; each training step differentiates a loss through a batch of trajectories; and uncertainty quantification propagates thousands of parameter draws through the same equation [44]. The resulting workload, an ensemble of independent ODE solves, is naturally parallel and therefore well suited to graphics processing units (GPUs). On the GPU, however, computational speed and differentiability have so far been provided by disjoint families of tools. The fastest ensemble solvers compile the entire adaptive integration procedure, including step-size control and error estimation, into a single GPU kernel, the program the GPU runs. Each of the GPU's many parallel threads, its smallest units of execution, integrates one trajectory; such a kernel is called fused below. DiffEqGPU.jl [33, 50] represents the published state of the art, alongside the CUDA solver suite MPGOS [28]. The compiled control flow that makes these kernels fast, however, cannot be traced by the reverse-mode automatic differentiation of the language they are written in. Reverse-mode gradients are offered only through a slower path that advances the whole ensemble together as one array, and, to our knowledge, no reverse-mode ensemble gradient has been reported that keeps the cost advantage of these kernels over the array-based solvers. Conversely, the solvers designed for differentiation, Diffrax [19, 20] in JAX [5] and torchode [24] in PyTorch [32], differentiate the adaptive loop itself. Even where the accepted step sizes are held fixed, the program being differentiated is still the loop that chose them, with its error tests and branches and, in a compiled framework such as JAX, a fixed upper bound on its number of iterations. Its backward sweep is correspondingly more expensive than one over a fixed-length chain. torchdiffeq [7], also in PyTorch, offers both routes; the interface we benchmark, odeint_adjoint, avoids differentiating the loop by solving a second equation backward in time for the sensitivities (a continuous adjoint), at the price of a gradient that is not the exact derivative of the solution the forward solver returned. Two further tools are not timed here. The solver in JAX's own experimental module [5] returns a continuous adjoint; the Julia sensitivity suite [27] returns a discrete adjoint, the derivative of the steps actually taken, but only on its array-based paths. Neither provides an adaptive per-trajectory GPU ensemble solve with a discrete adjoint. Fast solving and differentiability have moreover sat in different tools and largely in different language ecosystems: the fused kernels are written in Julia and CUDA C++, whereas Python, where most differentiable modeling is done, has had only the differentiable solvers. A practitioner working in Python has therefore had differentiable solvers but no fused ensemble solver, and in no ecosystem has the fastest ensemble solver also been the one that returns a reverse-mode gradient.

GRADSOLVE is an open-source GPU library, written in Python and built on JAX, designed to eliminate this trade-off and released at https://github.com/ECLIPSE-AI4Science/gradsolve. To differentiate an ensemble, it first solves each trajectory once with a standard adaptive integrator and records the sequence of steps the integrator accepted. It then computes the gradient by differentiating a fixed-step replay of the recorded steps, compiled as one regular program over the whole ensemble. The right-hand side is an ordinary JAX function written by the user. Its recording runs as a batched JAX loop on the GPU, or, on request, the library translates the function into a single fused GPU kernel, the form in which the built-in systems run (Section 3.4). Because the steps of the replay are predetermined, the differentiated computation is a fixed-length chain of identical steps with no branch in it. Its backward sweep is one linear pass back through the chain, whereas an adaptive loop differentiated in place must be given a fixed upper bound on its length, must decide which intermediate states to store and which to recompute (checkpointing), and is carried through every trial of every trajectory. This difference in the form of the backward sweep, not in the kind of derivative computed, is where Section 5 traces the speedups reported below: holding the method and the mesh fixed, the fixed-step scan is several times cheaper than Diffrax's loop on the GPU. The returned gradient is exact in a precisely defined sense: it is the exact derivative of the replay, with the recorded steps held fixed as data, rather than a derivative of the adaptive solve that generated them. When no gradient is required, the same interface sends those systems to the forward-only kernel, so that no cost is incurred for differentiability when it is not used. The derivative GRADSOLVE returns is classical: differentiating a previously accepted step sequence is the discrete-adjoint construction developed for sensitivity analysis and PDE-constrained optimization [10, 11, 15, 20, 31, 40, 55, 56]; Section 3 places it among its neighbors. To our knowledge, GRADSOLVE is the first implementation of this construction whose record is produced by a fused GPU ensemble kernel and whose backward sweep is a single fixed-length sweep over the whole ensemble. The workloads GRADSOLVE is built for include ensemble calibrations and sensitivity studies of low-dimensional mechanistic models and the training of small neural right-hand sides: cases in which the same ensemble is differentiated many times against one recorded set of steps.

Among the differentiable solvers, Diffrax is the fastest differentiable baseline we measured. Its default adjoint, called the checkpointed adjoint below, differentiates the steps the solver actually took, exactly, and saves memory by storing only some intermediate states and recomputing the rest; the ensemble runs as a single compiled array program. GRADSOLVE returns the same discrete adjoint as Diffrax, specialized to the regime of low-dimensional GPU ensembles differentiated many times against one record, and keeps Diffrax itself as a routing fallback. Of the PyTorch pair, torchode assigns each trajectory an independent step-size controller, and torchdiffeq returns the continuous-adjoint gradient described above. We benchmark against all three, and against DiffEqGPU.jl on the forward side. At matched achieved accuracy, that is, with each baseline's tolerance adjusted until its error equals ours, GRADSOLVE computes gradients 7.8−9.3×7.8-9.3\times faster than Diffrax's checkpointed adjoint on a Lorenz ensemble. Across A100, H100, and RTX 4090 GPUs the margin is 5.6−14.1×5.6-14.1\times, declining with ensemble size on every card. These ranges differ because the first is a tolerance sweep at one ensemble size and the second spans ensemble size and card; the figure to carry away is that the gradient costs several times less than Diffrax's, the saving largest on small non-stiff ensembles. Against Diffrax's forward mode, the cheaper setting when each trajectory carries one to three inputs, the margin is 3.2−3.5×3.2-3.5\times for the replay in reverse mode and 7.3−7.9×7.3-7.9\times for the replay in forward mode. It is faster on every system we measured except on the stiff Robertson system at the tighter accuracies, where Diffrax's higher-order stiff method reaches a given accuracy in fewer steps than GRADSOLVE's default second-order one. The advantage is therefore largest on non-stiff ensembles and on stiff ones short of the very tightest accuracy. Against the PyTorch solvers the gap widens to between one and two orders of magnitude, consistent with the cost, measured below in their forward passes alone, of launching each solver operation as its own GPU kernel from the CPU that drives the GPU. On the forward side, GRADSOLVE's forward-only kernel runs 2.8×2.8\times faster than DiffEqGPU.jl's fused kernel on the same GPU in double precision, and 1.95×1.95\times faster in single precision (Section 4.1).

The remainder of the paper is organized as follows. Section 2 formulates the problem of differentiating an adaptive ensemble solve. Section 3 presents the record-and-replay method and the four integrators it covers. Section 4 reports the forward and gradient benchmarks against DiffEqGPU.jl, Diffrax, torchode, and torchdiffeq, together with three parameter-estimation examples in which the lower gradient cost yields a faster fit at matched accuracy; Appendix A defines the benchmark problems. Section 5 identifies the origin of the measured advantage through controlled experiments and states when to expect it. Section 6 summarizes the results and their range of validity, and concludes; Appendix B gives the benchmark protocol and the gradient checks.

References (53)