Skip to content

JAX ODE solves under a vmapped sampler: chains that are done still solve at their last state #65

Open

Description

@nsiccha

A sampler that advances several chains as one jax.vmap batch (BlackJAX's NUTS, for example) runs each of its while loops until the last chain is done. JAX runs a batched loop's body for every lane and keeps the old carry where a lane's loop has ended, so a chain whose trajectory has ended is still evaluated on every later leapfrog step of the batch, one step past its last state. After a divergence that state is often far out.

With an rk_ode_rk45_tol solve in the density, the cost of an evaluation depends on the position. Past a divergence the system can be stiff, and on CUDA (the per-lane Pallas kernel, 0.1.353) one lane's long solve then holds every call of the batch. RK computes these discarded values correctly; the batched loop passes the body no mask, so nothing in the module can tell a discarded lane from a live one.

Measured (four-chain BlackJAX 1.7.1 NUTS warm-up, a density with a five-state solve in each of 24 plate cells, float64, tolerances 1e-8, at most 100,000 steps per output, RTX 5060 Ti):

  • One chain diverged after 5 of 127 leapfrog steps. Its remaining evaluations took 175,276 solver attempts against about 17,500 for each running chain, and the transition 25 s where the others took about 4 s.
  • At a point whose solves exhaust the step budget, one value and gradient takes 16.7 s on the card and 0.11 s on one CPU core.

Workaround, documented in "JAX runtime and derivatives", section "Chains under vmap": guard the sampler so that a chain that is done evaluates a state it has already evaluated. In BlackJAX 1.7.1 that is two changes in blackjax.mcmc.trajectory (dynamic_progressive_integration and dynamic_multiplicative_expansion). Only discarded results change: compiled to round alike, guarded and unguarded NUTS gave bitwise identical draws on Neal's funnel. With the guard, the same warm-up had no transition over 6.4 s.

This issue tracks the boundary until the samplers RK's JAX modules are used with skip finished lanes themselves (BlackJAX upstream main still integrates from trajectory.rightmost_state for every 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