Unit 8 of 9 · about 4 hours · measure

`torch.compile`: capture, guards, graph breaks and dynamic shapes

Compile silently accepts partial graphs: a break just runs that part eagerly. Without break counts and equivalence checks, a speedup number can hide a semantics change.

You'll be able to

  • Compile a stable fragment and prove eager equivalence on values and gradients.
  • Trigger a graph break and a recompilation on purpose, then read each from the logs.
  • Choose fullgraph, dynamic and backend settings from the failure you want to forbid or allow.

Covers

torch.compile capture, guards and graph breaksDynamic shapes, recompilation and semantic comparison
1

Read

In this order, only the parts listed

0 of 2 done

  1. torch.compile parametersPyTorch - compiler troubleshooting

    fullgraph, dynamic, backend and mode; guard failures with TORCH_LOGS=guards; recompile_limit of 8 before falling back to eager

  2. Breaks, guards, recompiles and logsPyTorch - compiler troubleshooting

    Terminology with the graph break, guards, recompilation and dynamic shapes definitions; Logging Tools with TORCH_LOGS graph_breaks, guards and recompiles; Simple Workarounds including data-dependent fixes; Reporting Issues, Ablation with eager and inductor backends

    Read Terminology, Logging Tools and Simple Workarounds fully. Skim TorchDynamo internals and Reporting Issues.

2

Focus on

  • Default compile is partial: each frame compiles what it can and runs the rest eagerly. fullgraph=True turns an unnoticed break into a loud error.
  • Guards are runtime checks on shapes, types and values. A failed guard recompiles, and eight recompiles exhaust the budget and fall back to eager.
  • Data-dependent control flow is the classic break: .item, tensor conditions and prints split the graph. Hoist, use torch.cond, or capture scalar outputs.
  • Equivalence comes before timing. Compare eager and compiled values and gradients with a tolerance, across shapes, before believing any speedup.
Common mistakes · 3
  • Reporting a speedup without a break count. A fully broken compile is just eager with overhead, and the timer will not tell you.
  • Feeding changing Python floats, such as a scheduled learning rate, into a compiled function. Under dynamic=False each new value recompiles until the limit of 8; the default recompiles once and then treats the float as dynamic. A tensor avoids both.
  • Reusing a function across compile experiments. Caches are per code object, so a fullgraph=True compile of a function already compiled without it can reuse the old entries and not raise. Call torch._dynamo.reset() between experiments.
3

Try it yourself

Closed-book, before checking anything

Compile a pure function and assert eager and compiled outputs match. Then compile a branch on flag.item() and read the TORCH_LOGS=graph_breaks line naming Tensor.item() as the break. Call torch._dynamo.reset(), compile the same function with fullgraph=True and show it raises. Finally count unique graphs with a counting backend: dynamic=False gives one per shape.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.Compiled code matches eager but the log names Tensor.item() as a graph break. What happened to the function?Answer

    Dynamo split it at the .item() call and runs that part eagerly. Fix by hoisting the value out, using torch.cond, or enabling scalar capture.

  2. 2.Under dynamic=False, one shape reuses its graph while a new shape adds a second. What does dynamic=True change?Answer

    dynamic=True traces shape-generic graphs from the start, where the operations allow it. The default, None, compiles static first and recompiles once as dynamic after a shape change: two graphs for sizes 3, 3, 4 and 5, against three under dynamic=False.

  3. 3.Timing collapses to eager speed after several shape changes. Which limit fired, and how do you see why?Answer

    The recompile limit, default 8, fell back to eager. Inspect the failing guards with TORCH_LOGS=recompiles.

  4. 4.Why compare values and gradients with tolerance before trusting a compile speedup?Answer

    Partial capture or specialization can change numerics. Equivalence first proves the speedup measures the same computation.

You're done when

You can trigger, name and fix one break and one recompile from the logs, with eager equivalence holding across your test shapes.

Only when you can do this without notes.

Leads to

Notes

Resources · 1 for this unit, 8 more in the section
  • PyTorch - compiler troubleshootingPyTorch · required

    Read graph breaks, guards, recompilations and logging. Compile one stable fragment, intentionally trigger a graph break or recompilation, inspect it, and compare eager and compiled outputs and gradients.

Also in this section

  • PyTorch - reliability and performance recipesPyTorch · primary

    Use the Profiler, Automatic Mixed Precision, model loading and Benchmark recipes, then the Performance Tuning Guide. Apply each to one compact model from an earlier section and keep a CPU fallback when CUDA is unavailable.

  • PyTorch - checkpointing, numerical accuracy and CUDA semanticsPyTorch · required

    Use the official user-guide entries for torch.utils.checkpoint, numerical accuracy, CUDA semantics, reproducibility and serialization. Predict memory categories before measuring and define resume equivalence before running.

  • Deep Learning Training DebuggerRoadmap-Final · reference

    Use the failure-injection, measurement and evidence rules as the deeper standard. The full Tier 2 project is a later route and is not added to this stage milestone.

  • TensorTonic - mixed-precision memory and transformer FLOPsTensorTonic · support

    Solve only Mixed-Precision Training Memory Accountant and Transformer Training FLOP Estimator. Reconcile both with the compact model's profiler and document every included and excluded term.

  • Learn PyTorch - profiler walkthroughDaniel Bourke · optional

    Use notebook 10 only if the official profiler route remains unclear. Transfer the relevant annotations into the compact model instead of keeping the tutorial code.

  • TrenTorch - precision, checkpointing and resumeTrenTorch · support

    Solve Loss Scaling, FP16/BF16 Representable Range, Gradient Checkpointing and Resume-from-checkpoint in NumPy, then reproduce each behavior with the PyTorch API in the compact model.

  • Python os - `os.replace`Python Software Foundation · reference

    Read os.replace for the atomic-rename step of checkpoint writes: save to a temp file in the same directory, flush and fsync, then replace the final name.

  • Hands-On Machine Learning with Scikit-Learn and PyTorchAurelien Geron (O'Reilly, 2025) · support

    Read Appendix B, Reduced Precision Models and Mixed-Precision Training, for why float16 training underflows and what fp32 primary weights and loss scaling fix.