Skip to content

jax_source ODE solves: batched (vmap) gradients on CUDA are lane-independent only up to rounding #62

Open

Description

@nsiccha

jax_source translates rk_ode_rk45_tol / rk_ode_bdf_tol into one diffrax solve whose gradient uses RecursiveCheckpointAdjoint(checkpoints=64). Under jax.vmap (a sampler's chains, a plate's cells) a lane's value never depends on the other lanes. On CUDA a lane's gradient can change in its trailing digits when another lane of the batch, valid or failed, needs more solver steps than it does.

Mechanism. equinox's checkpointed while loop keeps a batch's checkpoints in lockstep (equinox/internal/_loop/common.py: "all checkpoints need to be in lockstep across the batch"), so the lane needing the most steps decides which steps the backward pass recomputes. XLA's GPU autotuner, which chooses kernels anew in each process, can compile the recomputed steps to round differently from the forward steps. When no step is recomputed, or the recomputed steps round like the forward ones, the lanes stay bitwise independent.

Measured (jax 0.12.0.1, diffrax 0.7.2, equinox 0.13.8, one RTX 5060 Ti, float64, tolerances 1e-8; a density with one five-state solve in each of 24 plate cells, batches of three points, one of them made to need more steps):

  • default autotuning: 9 of 12 processes of a byte-identical lowered program showed the coupling, at most 2.1e-4 on gradient entries up to 2e3 (the JAX and native gradients differ by about 1.2e-3); the same programs' gradients also differed between processes, and between a point alone and in a batch, by about as much;
  • 512 checkpoints (no recomputation for these solves) removed the coupling in processes that showed it with 64;
  • XLA_FLAGS=--xla_gpu_autotune_level=0: the same gradient in 5 of 5 processes, lane-independent, no measurable cost per call;
  • CPU: no coupling. Two plain-diffrax toys (3 lanes of a 2-state solve; 3 points of 24 five-state cells) did not show the coupling on CUDA either, though the second's gradients differed between processes in the last digits.

Documented in docs/src/jax-runtime.md, section "ODE solves": compare batched gradients with a tolerance, not bitwise.

What would remove it: recomputed steps that round like the forward steps (an XLA property), or a per-lane checkpoint schedule (an equinox design change). Neither is in RK's emission; more checkpoints only move the threshold, at memory cost per lane.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions