Autodiff + XLA · NumPy-compatible · jit / grad / vmap · verified against JAX 0.7.x (2026)

JAX cheat sheet

JAX = NumPy + composable function transformations (jit, grad, vmap) + XLA compilation for CPU/GPU/TPU. The mental model is functional and pure: arrays are immutable, randomness is explicit, and side effects don't survive compilation. Get those three right and everything composes. This sheet targets JAX 0.7.x (typed jax.random.key, the jax.sharding API, Python 3.11+).

arrays & ops transformations randomness control flow / pytrees parallel / sharding gotcha most common

Verified 2026-08-24 against the official docs at docs.jax.dev (JAX 0.7.x changelog + guides) and the jax-ml/jax repo. Requires Python 3.11+. Neural-net layers (Flax) and optimizers (Optax) are separate libraries, cross-referenced here.

Outline

The four function transformations are the heart of JAX. If something behaves strangely, it's almost always one of the purity gotchas.

Arrays

  1. 1 · Install & jnp basics
  2. 2 · Immutable arrays & .at[]

Transforms

  1. 3 · jit — compile
  2. 4 · grad — autodiff
  3. 5 · vmap — auto-batch

Purity

  1. 6 · Randomness (keys)
  2. 7 · Control flow (lax)
  3. 8 · Pytrees

Scale & ship

  1. 9 · Devices & sharding
  2. 10 · Flax / Optax / debug
  3. 11 · Purity gotchas
  4. Worth memorizing

Arrays & Ops

NumPy you already know — but immutable.

1Install & jnp basics0.7.x
2Immutable arrays & .at[]functional updates

Transformations

Compose jit, grad, and vmap freely — that composability is the whole point.

3jit — compile with XLAspeed
4grad — automatic differentiationgradients
5vmap — auto-vectorizebatch for free

Purity: Randomness, Control Flow, Pytrees

The three places JAX diverges from imperative NumPy/PyTorch.

6Randomness (explicit keys)no global seed
7Control flow (lax)under jit
8Pytreesnested params

Scale & Ship

Multi-device execution, and the libraries you build real models with.

9Devices & shardingmulti-GPU/TPU
10Flax / Optax / debugbuild real models

Purity Gotchas

Nearly every JAX bug is one of these four.

!The big fourmemorize these

Worth memorizing

jnp is NumPy, but immutablex.at[i].set(v) instead of x[i] = v
jit / grad / vmap composejit(vmap(grad(f))) = compiled, batched, per-example gradients
grad needs a scalar outputvalue_and_grad(loss) is the training-step workhorse
RNG is explicitkey = jax.random.key(0); split before every draw; never reuse
branch with lax, not iflax.cond / jnp.where / lax.scan survive tracing
params are a pytreejax.tree.map applies updates leaf-wise across the whole model
jit recompiles per shapefix shapes; mark shape args static_argnums
side effects vanish under jituse jax.debug.print; return values, don't mutate
sharding via jax.shardingMesh + NamedSharding + plain jit auto-parallelizes (Shardy default)
layers=Flax, optimizers=OptaxJAX is the engine; these are the framework on top