Mixed Precision Training: FP16 vs. BF16, Dynamic Loss Scaling, Tensor Cores & PyTorch AMP
How to get a free 2×–3× GPU speedup and ~45% VRAM reduction with 3 lines of PyTorch: FP32 vs. FP16 vs. BF16 bit layouts, why BF16 never overflows, FP16 Dynamic Loss Scaling (GradScaler), and FP32 Master Weights!
Training in pure FP32 leaves 60%–70% of your GPU's Tensor Core throughput idle and burns 2× the activation VRAM!
Wrapping your forward pass in torch.autocast('cuda', dtype=torch.bfloat16) is the single highest-ROI 1-line speedup in PyTorch.
Knowing why FP16 overflows at 65,504 saves hours of debugging mysterious NaN gradients on older T4/V100 GPUs!
Top ML Systems interview question: 'Explain the bit-level difference between FP16 and BF16, and why FP16 needs Dynamic Loss Scaling while BF16 does not!'
What gives BF16 the exact same massive numeric range (±3.4e38) as FP32 despite using only 16 bits?
- Compare FP32, FP16 (5-bit exponent, max
65,504), and BF16 (8-bit exponent, FP32 range3.4e38) bit layouts - Explain why BF16 eliminates overflow/underflow and needs NO
GradScaler, while FP16 requires Dynamic Loss Scaling - Understand how PyTorch AMP (
torch.autocast) keeps FP32 Master Weights and sensitive ops (Softmax, LayerNorm) in FP32 while running Tensor Core GEMMs in 16-bit
65,504; BF16 max = 3.4 × 10^38 (identical to FP32!).Mixed Precision (AMP) is like building a house using a fast framing nailer (BF16/FP16 Tensor Cores) for 95% of the heavy lifting, while keeping a precision laser level (FP32 Master Weights + Softmax/LayerNorm) for the delicate measurements!
1. FP32 vs. FP16 vs. BF16 Bit Layouts
Modern NVIDIA GPUs (Ampere A100+, Hopper H100, RTX 30/40/50) execute BF16/FP16 matrix multiplies at 2×–4× the FLOPs of FP32 and halve activation memory bandwidth! Prefer BF16 on any Ampere+ GPU; use FP16 + GradScaler on older Volta/Turing (V100/T4) GPUs.
FP32 vs. FP16 vs. BF16 Dynamic Range & Speed
2. FP16 Dynamic Loss Scaling (`GradScaler`)
.backward() so tiny gradients survive FP16 underflow, then unscales by 1/S before optimizer.step()!Small gradients like 1e-6 round straight to 0.0 in FP16 (underflow!). GradScaler multiplies the loss by a large factor (e.g., 65,536) before .backward(), shifting gradients into FP16's representable zone, then divides by before optimizer.step() — automatically halving if any inf/NaN appears!
3. FP32 Master Weights & PyTorch `torch.autocast`
Never cast the whole model blindly with model.half()! Small updates like 1e-4 disappear when added to 1.0 in 16-bit. PyTorch AMP keeps weights in FP32, casts GEMMs to BF16/FP16 automatically, and keeps reductions (Softmax, LayerNorm, CrossEntropy) safely in FP32!
Why should you use torch.autocast('cuda', dtype=torch.bfloat16) instead of blindly calling model.half()?
The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.
No! Because BF16 has the exact same 8-bit exponent range as FP32, gradients don't underflow or overflow like FP16 — GradScaler is only needed for torch.float16!
Mixed Precision Training: BF16/FP16 Tensor Cores + FP32 Master Weights
See how PyTorch autocast speeds up GEMMs 2–3× while protecting numerical stability.
torch.autocast (or FP16 + GradScaler on older GPUs) to unlock 2×–3× Tensor Core speed and ~45% lower VRAM while keeping Master Weights and Softmax/Norm safely in FP32.Every Modern GPU Training & Fine-Tuning Script
BF16 AMP is enabled by default in PyTorch Lightning, Hugging Face Accelerate, and all frontier LLM/ViT training runs.
# Modern BF16 AMP on Ampere/Hopper+ GPUs (zero GradScaler needed!)
for x, y in train_loader:
optimizer.zero_grad(set_to_none=True)
with torch.autocast(device_type="cuda", dtype=torch.bfloat16):
logits = model(x)
loss = criterion(logits, y)
loss.backward()
optimizer.step()
loss.backward() INSIDE the with torch.autocast(): block.autocast BEFORE calling loss.backward() — backward ops automatically use the matching forward precision!