The JAX/XLA Stack — No Black Boxes
Open the box between your Python and the MXU. How JAX traces a jitted function into StableHLO, how XLA fuses the graph into TPU machine code, JIT vs AOT, and how reading the compiled program lets you catch an OOM before training even starts.
200Single-slice execution & the "no black box" mandateLesson 1 of 2ShowHide
Chapter 2 of 2 in TPU Training Infrastructure
- The JAX/XLA Stack — No Black BoxesYou're here
Prerequisites: Why TPUs? Cloud TPUs & the TPU VM, The TPU Chip & the Systolic Array, Cloud TPU Architecture — how you consume TPUs
The one-line summary
L100 ended at "your code runs on the host and dispatches to the chip." That's the
black box this level pries open. Between your model.py and the
sits a compiler — and once you can read what it did, you
can explain every millisecond and catch an out-of-memory error before training
starts. The level's mandate: no black boxes.
The compile path
You write NumPy-like Python. The TPU runs machine code. Here's the bridge:
- Trace — on the host CPU. When you first call a
jax.jit-decorated function, JAX runs it once on the host VM — burning host CPU and RAM, with the TPU sitting idle — using abstract values to . This step exists because of a hardware mismatch: ordinary Python is dynamic and allocates memory on the fly, but a TPU is rigid — it needs strict, pre-allocated layouts before it will accept any work. Tracing freezes every tensor shape so XLA can compute exactly how many bytes of HBM to reserve. (Change an input shape and it re-traces and recompiles — the classic "why did my step suddenly get slow?") - Lower to StableHLO. Lowering is compiler-speak for translating a program down one
rung toward the hardware (think source → IR → machine code). The trace first becomes a
jaxpr — JAX's own internal op-list IR — which is then lowered to StableHLO, a
portable, hardware-neutral IR. (That's the
Python → jaxpr → StableHLOpipeline you'll step through in the simulation below.) - Compile with — also on the host. XLA optimizes the StableHLO — most importantly it ops — and emits an executable for your target (TPU, GPU, or CPU — the same program). This compile also runs on the host's CPU and RAM; the TPU chip stays completely idle until XLA finishes and ships the binary over PCIe. (That's exactly why a compilation cache is worth so much on multi-host jobs — you don't want every host paying the same compile.)
You never hand-write a kernel. You shape the Python; XLA writes the silicon's code.
Fusion: the optimization that matters most
A transformer layer is a matmul followed by a chain of cheap elementwise ops — bias, an like GELU, scale. Run them naively and every op re-reads its input from and writes its output back — a round-trip per op. Fusion merges the chain into one kernel: the matmul reads its inputs once, the elementwise work rides along in its epilogue (the tail end of the matmul kernel) on-chip, and only the final result is written back.
Same FLOPs, far fewer bytes — exactly the lever from the systolic-array lesson. (The ridge is the roofline crossover point: below it you're memory-bound — starved on HBM bandwidth; above it compute-bound — saturating the MXU.) Walk the compiler pipeline, then run the race: the fused kernel makes fewer HBM trips, so it spends less time stalled on memory — that shorter wall-clock per step is throughput you can measure. Watch the chain cross the v5p ridge from memory- to compute-bound:
Predict before you run the race: XLA fuses a chain of three elementwise ops that follow a matmul. What changes for the chain?
That's the whole game restated in compiler terms: keep the MXU fed by not wasting trips to HBM. Fusion is XLA doing it for you.
JIT vs AOT — and catching OOM before you spend a slice
Same compiler, two timings:
- JIT (
jax.jit) compiles on the first call and caches the result. Convenient, but you only find out what it compiled to (and whether it fits in HBM) when that first call runs. Both modes compile on the host. - AOT (
jax.jit(f).lower(args).compile()) compiles ahead of time, without running. The payoff: the compiled object carries a memory analysis you can read — so you can discover that a batch size or sharding choice will OOM a v5p slice at compile time, before a single step burns real (and scarce) capacity.
On multi-host jobs, a compilation cache lets every host reuse one compile instead of all of them paying the cost — a real startup-time win you'll meet again in the next lessons.
Where this sits in the stack
The compiler is the floor; everything above it is libraries. The vertical layering is your model code → JAX → StableHLO → XLA → TPU. Flax and Optax aren't rungs in that stack — they're sibling libraries on top of JAX (one builds the model, the other the optimizer):
- JAX — the numerics and transforms (
jit,grad,vmap) everything else builds on. - Flax NNX — the model/module system (which Flax API the model is built in is the modeling team's call, not the operator's). NNX is the recommended API for new code; Linen is the older one that still powers frameworks like MaxText, so you'll read both. (Either way it's XLA/JAX-first — not "TPUs are for TensorFlow.")
- Optax — composable optimizers.
- Orbax and Grain — checkpointing and data loading (the next lesson's stars).
Why this matters
Every later skill — SPMD sharding, fixing a starved input pipeline, debugging a megascale run — assumes you can look at what the compiler produced and reason about it. The engineer who treats XLA as magic guesses; the one who reads the HLO and the memory analysis knows. That's the difference this level is built to create.
Put it together
Watch fusion move the needle
In the explorer, walk the compile pipeline (step or auto), then run the race. Read off the HBM round-trips and the arithmetic intensity for each side, and say in one sentence why the bound flipped from memory to compute.
Place the AOT win
Explain when you'd reach for
.lower(args).compile()instead of a plainjax.jitcall — and the one thing it lets you check before you spend a slice.Name the stack
List the vertical layers from your model code to the silicon (your model → JAX → StableHLO → XLA → TPU), say which one fuses your ops, then name the two sibling libraries on top of JAX (Flax NNX, Optax) — they sit beside each other, not as rungs in that stack.
Review
What is the JAX → TPU compile path?
Check yourself
Why does op fusion raise arithmetic intensity?