Unit 9 of 12 · about 3 hours · measure

Compilation, graph breaks and recompilation in a serving path

Graph capture from the previous unit replays only what compilation produced. A forward pass that recompiles per batch shape stalls at the worst moment, so breaks and guards belong in the serving path design.

You'll be able to

  • Explain graph breaks, guards and recompiles, and where each appears in a serving forward pass.
  • Stabilize a bounded compiled path across the workload shape distribution with dynamic shapes or bucketing, and count the remaining recompiles.
  • Place an intentional break around the dynamic attention operator instead of forcing full-graph compilation across it.

Covers

Compilation, graph breaks and recompilation
1

Read

In this order, only the parts listed

0 of 3 done

  1. Graph breaks, guards and recompilesPyTorch compiler troubleshooting guide (2.14)

    Graph break, Guards, Recompilation and Dynamic Shapes, with TORCH_LOGS set to graph_breaks and recompiles

  2. Intentional breaks at the attention boundaryUCSD CSE 291A (Spring 2026) - Guest lecture: Engineering vLLM

    May 7, 2026 scribe notes: Section 4.5, the attention-boundary split with an intentional graph break before and after attention

  3. Compile validation across bucketed shapesAI Systems Performance Engineering companion code - Chapter 18

    run_compile_validation: torch.compile with dynamic=True, mode="reduce-overhead" and a counting backend over padded bucket shapes

    Read the validation logic, not bundled timings as targets. It runs bucketed shapes only; add your own unbucketed series to compare compile counts. The companion decode_kernels.py in the same folder compiles with fullgraph=False; fullgraph=True raises at the first graph break instead.

2

Focus on

  • A graph break splits one function into several graphs. torch._dynamo.explain reports the graph count with a reason per break, and each graph carries its own shape guards.
  • Static size guards trigger a recompilation for each new size under dynamic=False, until the recompile limit sends the function to eager execution. The default (dynamic=None) recompiles with automatic dynamic shapes after a size mismatch; dynamic=True requests symbolic shapes upfront (sizes 0 and 1 still specialize). Neither guarantees one compilation: strides, dtypes, control flow and other guards can still specialize. Bucketing limits shape variation, not every source of recompilation.
  • Inductor may reassociate reductions, so compiled output can differ from eager by a few ULPs. Compare with a tolerance, never with exact equality.
  • Attention is the dynamic operator in a serving layer. vLLM splits the graph at attention by default (splitting_ops) so the static pieces stay capturable, the lecture's intentional break; the experimental use_inductor_graph_partition moves that split into Inductor at ops tagged cudagraph_unsafe.
Common mistakes · 3
  • Setting fullgraph=True to remove breaks. It raises on the first untraceable op; remove the op or scope compilation around it instead.
  • Asserting compiled output equals eager output exactly. Reduction reassociation changes rounding within tolerance.
  • Compiling once and assuming coverage. A failed guard may trigger recompilation or eager execution after the compilation limit; count both across the serving shape distribution.
3

Try it yourself

Closed-book, before checking anything

On CPU, compile a function that computes a = torch.relu(x), then calls print("mid", a.shape), then returns (a + 1).sum(-1), with fullgraph=False, and run torch._dynamo.explain on it: predict 2 graphs and 1 break naming builtin print before you run (a constant print before any traced op does not break). Then compile a reduction with dynamic=False, call it at two batch sizes above 1 under TORCH_LOGS=recompiles, and observe the size-mismatch guard recompile. Recompiling with dynamic=True runs both shapes with no recompile log and matches eager under torch.testing.assert_close, not torch.equal.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.What does explain report for the print function, and why two graphs?Answer

    Two graphs and one break naming builtin print. The traced ops before and after the untraceable call compile as separate graphs.

  2. 2.Why does the second batch size recompile under dynamic=False?Answer

    Guards specialized on the first size. The new size fails the size guard, so Dynamo recompiles for the new shape.

  3. 3.Where does an intentional break belong in a serving layer?Answer

    Around attention. Static MLP and norm regions stay compilable and capturable while the dynamic operator stays eager.

  4. 4.Compiled output differs from eager by a few ULPs. Is the compile wrong?Answer

    Not from that alone. Reassociated reductions round differently. Wrong means exceeding the declared tolerance or failing the oracle matrix.

You're done when

You predict the break count of a small function before running explain, remove a shape recompile with bucketing or dynamic shapes, and can point to the intentional-break boundary in the serving path.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 11 more in the section

Also in this section