JAX

Composable function transformations (grad, jit, vmap) over NumPy-style code, popular for research and large-scale training on TPUs and GPUs.

Advanced · about 18 hours · difficulty 4/5

Learn first: Debugging and Running Training Experiments

JAX looks like NumPy but lets you transform functions: take gradients, compile them with XLA, vectorize them and parallelize them across devices. It favours a functional style where model parameters are explicit data rather than hidden object state.

Core transformations

  • jax.grad: gradient of a scalar function.
  • jax.jit: compile a function for big speedups.
  • jax.vmap: automatic batching of a per-example function.
  • jax.pmap / sharding APIs: multi-device parallelism.

Things that surprise newcomers

  • Arrays are immutable; use x.at[i].set(v) instead of in-place assignment.
  • Explicit random keys (jax.random.PRNGKey, split) instead of global seeds.
  • Python control flow inside jitted functions needs jax.lax.cond / scan when it depends on array values.

Ecosystem

Flax (neural network layers), Optax (optimizers) and Orbax (checkpointing). Choose JAX if you work on TPUs, in research groups that use it, or need its transformations; otherwise PyTorch is the more common path.

Good enough to move on when you can train an MLP with Flax and Optax and explain what jit, grad and vmap each do.

Key concepts

  • grad
  • jit and XLA
  • vmap
  • PRNG keys
  • Flax
  • Optax
  • Multi-device sharding

Other options for this step

  • PyTorch: Tensors, autograd, nn.Module, Dataset/DataLoader and the explicit training loop, the most widely used framework in research and much of industry.
  • TensorFlow and Keras: The Keras high-level API for fast model building with fit/evaluate, and the TensorFlow ecosystem for mobile and production deployment.

Mini project

Implement logistic regression in pure JAX using grad, jit and vmap for per-example gradients, then rebuild a Fashion-MNIST MLP with Flax and Optax.

Check yourself

Why does JAX require explicit random keys instead of a global random seed?

Free resources