JAX 301: advanced autodiff and extending JAX#
The 101 docs introduced jax.grad(); the pages here go as deep as you
like: the machinery underneath (JVPs, VJPs, Jacobians, Hessians), how
autodiff interacts with sharding, defining your own derivative rules and
even your own types, autodiff with mutable state, and controlling the
memory/compute tradeoff of differentiation.
The Autodiff Cookbook with JVP and VJP β autodiffβs basic ingredients, the
jvpandvjpmachinery: recipes for Hessian-vector products and full Jacobians, plusvmapand complex numbers.First-class VJPs β the VJP object as a pytree, splitting the forward and backward passes into separately compiled functions run on your own schedule, and excluding argument values (like weights) from the saved state with
saveable_args.Autodiff and sharding β how autodiff interacts with sharding: cotangent shardings as a function of primal shardings, and controlling backward-pass communication with
unreducedandreduced, in both explicit and manual (shard_map) modes.Custom derivative rules with hijax primitives β defining custom derivative rules with hijax primitives, the recommended approach: one primitive can carry rules for both modes, plus linearization, batching, producing structured residuals, logging data out of the backward pass, and more.
Custom JVPs or VJPs with custom_jvp and custom_vjp β the classic decorators, customizing one differentiation mode at a time: still fully supported, and often the most convenient tool for simple cases.
Autodiff with refs β autodiff with mutable arrays: plumbing values out of backward passes, in-place gradient accumulation with
with_refs, and differentiating with respect to refs.Gradient checkpointing with jax.checkpoint (jax.remat) β gradient checkpointing with
jax.checkpoint: what autodiff saves versus recomputes, name-based policies, offloading, and per-function control withcustom_remat.Defining new JAX types with hijax β defining entirely new JAX types with hijax, with their own derivatives, batching, and sharding behavior.