torch.compile Primer for Perf Engineers
torch.compile Primer for ML Performance Engineers
Section titled “torch.compile Primer for ML Performance Engineers”▶ Open the full primer — self-contained long-form document: full-stack pipeline diagram (GPU and TPU paths side by side), graph-break and recompilation-storm diagrams, the compiler-first decision tree, an 8-entry failure-mode bestiary keyed to diagnosis commands, and a 10-statement interview deployment sheet.
The spine, for quick review:
- The stack: Dynamo (bytecode → FX graph + guards) → AOTAutograd (joint fw/bw, functionalized ATen) → Inductor→Triton (GPU) or openxla→StableHLO→XLA (TPU). Every production pathology attributes to exactly one stage.
- Graph breaks are structural: fusion and cudagraphs can’t cross them — serving runs
fullgraph=Trueso breaks fail the build instead of silently costing latency. - Dynamic shapes: symbolic after first change, but production serving pre-compiles a bucket lattice anyway — and on TPU bucketing is the programming model, not an optimization (whole-program static-shape XLA compiles).
- Fusion = roofline arithmetic: unfused elementwise chains run at AI≈0.25; fusing N ops divides HBM bytes by N. Check
output_code.pykernel counts before believing any speedup claim. - Production pattern: piecewise compilation with attention outside as a custom op, cudagraphs per bucket, warm-up over the lattice, cache artifacts shipped in the image (
save_cache_artifacts). - Compiler-first, custom-kernel-last: profile → roofline-place → inspect codegen → fix capture → only then hand-write, because hand kernels are re-owned at every compiler/hardware rev.
Version-anchored to the PyTorch 2.10–2.12 era (Jan 2026 release, two-month cadence).