Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 40 ยท jit and XLA

Inspecting jaxprs, StableHLO and Compiled Code

Look inside JAX: read jaxprs with make_jaxpr, view StableHLO and optimized HLO with lower and compile, check cost analysis, and understand what XLA did with your function.

jit compiles your function, but you do not have to treat the result as a black box. JAX lets you print each representation along the way: the jaxpr (JAX's own small functional language), StableHLO (the portable program handed to the compiler) and the optimized HLO (what XLA actually runs after fusion). Reading them answers questions like "did this branch compile away?", "is my loop unrolled?" and "why does this use so much memory?"

JAX cannot run in the in-browser runtime (see What Is JAX?), so JAX code and outputs below are read-only, shown in the shape JAX prints them. The editors build a tiny jaxpr-style tracer in plain Python so you can see the idea working.

Stage 1: jaxprs with make_jaxpr

jax.make_jaxpr(f)(args) traces f and prints the recorded program without running it:

Python
import jax
import jax.numpy as jnp

def f(x, w):
    return jnp.tanh(x @ w) + 1.0

print(jax.make_jaxpr(f)(jnp.ones((2, 3)), jnp.ones((3, 4))))
TEXT
{ lambda ; a:f32[2,3] b:f32[3,4]. let
    c:f32[2,4] = dot_general[dimension_numbers=(([1], [0]), ([], [])) ...] a b
    d:f32[2,4] = tanh c
    e:f32[2,4] = add d 1.0:f32[]
  in (e,) }

(Exact details vary a little between JAX versions.) Reading it:

  • lambda ; a b. lists the inputs with their shapes and dtypes: this is what the tracer knew.
  • Each let line is one primitive applied to variables: dot_general is the matrix product, tanh, add.
  • in (e,) is the output.

A jaxpr has no Python in it: no if, no loops, no names from your code. That is why jit is safe to cache and why transformations like grad and vmap can rewrite it: they transform a small, uniform program.

Python Stand-in: record primitives like make_jaxpr

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Seeing control flow and constants

make_jaxpr makes trace-time behaviour visible:

Python
def g(x, n):
    for _ in range(n):         # a Python loop on a Python int
        x = x * 2.0
    return x

print(jax.make_jaxpr(g, static_argnums=1)(1.0, 3))
# three `mul` lines: the loop was UNROLLED

Replace the loop with lax.fori_loop and you get a single while equation with the body as a nested jaxpr. Likewise jnp.where appears as a select_n, lax.cond as cond with two nested jaxprs, and lax.scan as a scan equation. Captured global arrays show up as constants in the jaxpr header ({ lambda a:f32[1000]; ...), a quick way to find accidentally embedded data (jit Pitfalls). Gradients are visible too: jax.make_jaxpr(jax.grad(f))(...) prints the forward and backward equations.

Stage 2: StableHLO via lower

jax.jit(f).lower(args) turns the jaxpr into a program in StableHLO, a compiler-friendly intermediate language, without compiling it:

Python
lowered = jax.jit(f).lower(jnp.ones((2, 3)), jnp.ones((3, 4)))
print(lowered.as_text())
TEXT
module @jit_f {
  func.func public @main(%arg0: tensor<2x3xf32>, %arg1: tensor<3x4xf32>) -> tensor<2x4xf32> {
    %0 = stablehlo.dot_general %arg0, %arg1, contracting_dims = [1] x [0] ...
    %1 = stablehlo.tanh %0 : tensor<2x4xf32>
    %cst = stablehlo.constant dense<1.0> : tensor<f32>
    ...
    return %3 : tensor<2x4xf32>
  }
}

StableHLO is device independent: the same text can be compiled for CPU, GPU or TPU. Many JAX tools (exporting a model with jax.export, serving, converting to other frameworks) work at this level.

Stage 3: optimized HLO via compile

lowered.compile() runs XLA for the current device. The optimized program shows what really executes:

Python
compiled = lowered.compile()
print(compiled.as_text())

Here you will see fusions: several operations merged into one kernel, for example fusion = fusion(...) kind=kLoop containing the tanh and the add. Layouts, buffer assignment and device-specific operations appear. If you expected two kernels and see one fusion, that is XLA doing its job (jit Basics).

Useful companions:

Python
compiled.cost_analysis()        # estimated FLOPs, bytes accessed
compiled.memory_analysis()      # argument, output and temporary sizes (when supported)

These are estimates, but they let you compare variants: does version B read fewer bytes than version A? Is the temporary memory what you expected before you launch a long run?

flowchart LR
    P["Python function"] -->|"make_jaxpr"| J["jaxpr"]
    J -->|"jit.lower"| S["StableHLO"]
    S -->|"lowered.compile"| H["optimized HLO"]
    H --> E["device executable"]

Each stage is a program you can print.

A practical use: counting recompilations and checking shapes

make_jaxpr is also a fast shape checker. Because it never runs the numbers, it reveals shape errors for a huge model instantly, on any machine, and costs almost nothing:

Python
jax.eval_shape(model_apply, params, x)      # returns output shapes/dtypes only

jax.eval_shape returns ShapeDtypeStruct objects, perfect for checking a network before allocating memory for it.

Reading a jaxpr: a short checklist

  1. Are the input shapes and dtypes what you expect? (f32 versus f64, weak types)
  2. Are there many repeated equations? You probably have an unrolled Python loop.
  3. Are there large constants? Pass them as arguments.
  4. Is there a convert_element_type in a hot loop? Mixed dtypes cause extra work.
  5. Does the structure of the backward pass look reasonable (jax.make_jaxpr(jax.grad(f)))?

The text formats are for humans and change between versions; do not parse them in production code. For stable interchange, use jax.export.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does jax.make_jaxpr(f)(x) do?

    It shows the primitives JAX recorded while tracing, with shapes and dtypes.
  2. 2. A jaxpr shows 100 identical mul equations. What is the likely cause?

    Use lax.fori_loop or lax.scan to compile the body once.
  3. 3. Which method gives the optimized program that XLA actually runs?

    lower() gives StableHLO; compile() runs XLA, and its text shows fusions and device-specific details.
  4. 4. Where do captured global arrays appear in a jaxpr?

    Constants baked into the program are listed, a clue that you should pass the data as an argument.
  5. 5. What is jax.eval_shape good for?

    It traces abstractly, so even enormous models can be shape-checked instantly.

Summary

  • make_jaxpr shows the traced program: primitives, shapes, dtypes and constants.
  • jit(f).lower(args).as_text() shows StableHLO; .compile().as_text() shows optimized HLO with fusions.
  • cost_analysis() and memory_analysis() give estimates for comparing variants.
  • eval_shape is a free shape checker.
  • Unrolled loops, big constants and dtype conversions are easy to spot in a jaxpr.

Next: vmap Basics: Write One Example, Run Many