PyTorch torch.compile, FSDP2 & Training Optimization
torch.compile internals (TorchDynamo, TorchInductor), FSDP2 sharding strategies, activation checkpointing, mixed-precision training, and the PyTorch profiler workflow for training throughput optimization.
Overview
PyTorch is the dominant framework for large-model training in 2025-2026. High throughput depends on three layers: the compiler stack (torch.compile), memory management (FSDP2, activation checkpointing) and numerical precision (mixed precision, loss scaling). Together they decide whether a job reaches 60% or 40% of theoretical hardware peak.
The layers stack in order. Model code goes through torch.compile, which turns it into fused kernels. FSDP2 and activation checkpointing then decide what sits in GPU memory. Mixed precision in BF16 or FP8 sets the number format of the math. Finally the profiler finds the idle gaps that are left, and closing them is what raises the job's share of hardware peak. This chapter follows that order.
Compiling with torch.compile
torch.compile (PyTorch 2.0+) is a JIT compiler that turns Python-level PyTorch code into optimized native code through three stages.
Each stage has one job.
- TorchDynamo captures the graph of tensor ops at the Python bytecode level. Untraceable Python, such as data-dependent if statements, print calls and external libraries, causes a graph break and a fallback to Python, so minimizing graph breaks is critical.
- AOT Autograd turns the forward graph into a joint forward-backward graph. That lets the backend fuse across the forward and backward passes, which is impossible in eager mode.
- TorchInductor is the default backend code generator. It applies loop tiling, pointwise fusion and persistent reductions, and it autotunes kernel parameters with the Triton autotuner.
> [!NOTE] On LLM training, torch.compile gives a 1.2-1.5x speedup over eager mode by fusing RMSNorm, RoPE and residual additions, cutting memory bandwidth and removing Python interpreter overhead.
Interview questions
- How would you use torch.compile to speed up LLM training and what are the failure modes to watch for?
- What is the complete optimization checklist for maximum GPU MFU on LLaMA-3 70B training?
Sign up free to read the full topic
More AI Infrastructure topics
- Multi-tenant GPU Clusters & Confidential Computing
- Numerical Correctness Gates: Golden Outputs, Tolerances & Determinism
- Performance Baselines, Regression Detection & Auto-bisect for Accelerators
- Post-silicon Validation & Bring-up Labs
- Power & Cooling for GPU Racks
- Pre-silicon Software Shift-left: Virtual Platforms to First-silicon Readiness
- Quantization & Model Compression
- Rack-scale NVLink: Operating GB200/GB300 NVL72
- Release Engineering for Semiconductor Equipment Software
- ROCm & the AMD GPU Stack
- Running CI on Scarce Accelerators: Scheduling, Artifacts & Cost
- Serving LLMs On-prem: Triton, NIM, vLLM & the Model Store
Start free on Cariara · Coding problems · System design studio