Gradient Checkpointing & Activation Recomputation
Trading compute for memory: the checkpointing algorithm, optimal checkpoint placement, selective recomputation, memory savings analysis, and integration with pipeline/tensor parallelism.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- The Activation Memory Problem
- Basic Checkpointing
- Optimal Checkpoint Placement
- Selective Recomputation
- Memory Savings Analysis
- Integration with Parallelism
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Compute activation memory for a transformer model.
- Derive the memory-compute tradeoff for checkpointing.
- Prove that checkpoints minimize memory for a chain of layers.
- Explain selective recomputation and which operations to recompute.
- Analyze checkpointing interaction with pipeline parallelism.
Notation
- — number of layers
- — activation memory per layer
- — number of checkpoints
- — forward time per layer
Core Intuition
Training requires storing intermediate activations from the forward pass to compute gradients in the backward pass. For a 70B model processing 4K tokens: activation memory can reach 100+ GB — often more than the model weights. Checkpointing drops some activations and recomputes them when needed during backward, trading ~33% more compute for dramatically less memory.
Gradient Checkpointing
The Activation Memory Problem
Standard training: Store all layers' activations:
For a transformer with :
- Per-layer activations: (attention + FFN intermediate) MB.
- Total: MB GB per sequence.
- With batch size 4: 32 GB just for activations.
Basic Checkpointing
Strategy: Only save activations at evenly-spaced checkpoints. During backward, recompute from the nearest checkpoint.
Memory: checkpointed activations + 1 segment of recomputed activations:
(Store checkpoints + at most layers between checkpoints during recomputation.)
Compute overhead: Re-run forward pass for each segment: ~33% more total compute (each layer's forward is computed twice: once in the original forward, once during backward recomputation — but the second time only for the segment being processed).
Optimal Checkpoint Placement
Minimize (memory expression without constant ).
Minimum memory: .
For : – checkpoints, memory (vs without checkpointing). 3x reduction.
Selective Recomputation
Not all operations are equal. Some activations are cheap to recompute and expensive to store:
Always recompute (cheap compute, large memory):
- Attention matrices ( memory but compute — actually, with FlashAttention, they're never stored anyway).
- Dropout masks (random, trivially regeneratable from seed).
Always store (expensive compute, small memory):
- LayerNorm statistics (mean, variance): tiny memory, moderate compute.
- Linear projection outputs: moderate memory, expensive to recompute.
Selective strategy: Checkpoint between transformer blocks (store input to each block); recompute attention + FFN within blocks. Memory: 1 activation per layer () instead of all intermediates.
Memory Savings Analysis
Without checkpointing:
- Activations per layer: (attention QKV, attention output, FFN up, FFN down, residuals).
- Total: .
With checkpointing per block:
- Store: (one hidden state per layer).
- Recompute: intermediates within each block during backward.
- Savings: reduction.
With FlashAttention + checkpointing:
- Don't store attention matrix (FlashAttention handles this).
- Don't store intermediate FFN activations (recomputed).
- Store only: layer inputs + LayerNorm stats.
- Near-optimal memory usage.
Integration with Parallelism
Pipeline parallelism: Each stage must store activations for micro-batches (1F1B schedule). Checkpointing reduces each micro-batch's activation footprint.
Tensor parallelism: Activations are split across TP ranks. Checkpointing saves the split activations and recomputes when needed (each rank recomputes independently).
Combined savings: For :
- Without checkpointing: (per micro-batch × stages in memory).
- With checkpointing: . 10x less.
Common Pitfalls
Pitfall 1. Checkpointing too frequently. Every checkpoint → no recomputation → wastes memory on the checkpoint storage itself.
Pitfall 2. Not using deterministic dropout. If dropout masks aren't reproducible during recomputation, gradients will be incorrect. Save the RNG state at each checkpoint.
Pitfall 3. Checkpointing with mixed precision incorrectly. FP16 activations saved at checkpoints must be used (not recomputed in FP32) to maintain gradient consistency.
Summary
- Activation memory grows linearly with layers and sequence length.
- Checkpointing trades ~33% extra compute for memory.
- Optimal: checkpoints for memory.
- Selective recomputation: Recompute cheap operations (attention, dropout); store expensive ones.
- Essential for training large models — used universally with Flash Attention.
Exercises
Exercise 1. For layers: compute optimal , memory with and without checkpointing.
Exercise 2. Derive the exact compute overhead of checkpointing (show why it's 33% for uniform segments).
Exercise 3. For a 70B model with : compute activation memory with and without checkpointing + FlashAttention.
Exercise 4. Design a selective recomputation strategy that minimizes memory while adding < 10% compute overhead.
Exercise 5. Explain why checkpointing is especially important for pipeline parallelism (hint: micro-batches in flight).