Two memory + speed tools you'll always reach for
Already covered AMP basics in Track 2. This lesson gathers the modern combinations and adds gradient checkpointing — the trick that trades compute for memory.
AMP recap — the modern bf16 path
On Ampere+ GPUs (A100, RTX 30/40, H100) and Apple Silicon, prefer autocast(device_type='cuda', dtype=torch.bfloat16) with no GradScaler. Same speed as fp16, none of the underflow drama. fp16 + GradScaler is still around for older hardware (V100, T4) but for new training runs on modern GPUs, bf16 wins.
Gradient checkpointing
Normally, the forward pass stores every intermediate activation so backward can use it. For deep networks (a 24-layer Transformer with sequence length 4K) those activations dominate memory. Gradient checkpointing doesn't store some activations — it recomputes them on demand during backward. Memory drops by 50-70%, training time goes up by ~30%. Almost always worth it when you'd otherwise OOM.
Composing them
AMP + gradient checkpointing + torch.compile + FSDP all compose. The order to add them when scaling up:
- Start eager fp32 — get correctness.
- Add bf16 autocast — free 1.5–2x speed.
- Add torch.compile — another ~1.5–2x.
- If still OOM at desired batch size: gradient checkpointing.
- If model itself doesn't fit: FSDP.