JAX Reference
1. Core Philosophy
- Immutability: JAX arrays are immutable.
x[0] = 1raises error. Usex.at[0].set(1). - Functional: Functions should be Pure (Output depends only on input, no side effects).
- Accelerated: Runs on XLA (Accelerated Linear Algebra) for GPU/TPU.
2. The Four Transformations
jit: Compilesf(x)->xla_f(x). Speedup.grad: Differentiatesf(x)->f'(x). Autograd.vmap: Vectorizesf(x)->f(batch_x). Automatic Batching.pmap(Legacy) /sharding: Parallelizes across devices.
3. Control Flow
- Python Control Flow (
if x > 0): Logic is baked into the graph during tracing.xmust be static (value known at compile time). - JAX Control Flow (
lax.cond,lax.scan,lax.fori_loop): Logic remains in the graph.xcan be dynamic (tracer).
4. Pytrees
JAX generic concept for "container of arrays". JAX ops work over pytrees seamlessly.
- Supported:
list,tuple,dict,NamedTuple, custom classes registered as nodes. - Common util:
jax.tree.map(fn, tree)applies function to leaves.