DEEP LEARNING Β· INTERVIEW QUESTION
What is gradient checkpointing? When and why would you use it?
π΄ advanced Β· Training & Optimization
Hint: Trade compute for memory by recomputing activations during backward pass.
Answer
Backward needs certain tensors from the forward computation; they must be saved or recomputed, rather than necessarily storing every intermediate.
For large models, this activation memory can exceed GPU capacity. Gradient checkpointing: only store activations at certain 'checkpoint' layers. During backward pass, recompute the intermediate activations from the nearest checkpoint (requires an extra forward pass for those segments).
Tradeoff: recomputation reduces saved activation memory at added compute cost. Under simplified equal-layer assumptions, a particular square-root checkpoint schedule reduces activation storage from to . General schedules have different tradeoffs; measure actual peak memory and runtime rather than assuming a fixed 33% overhead.
When to use:
-
Training models that don't fit in GPU memory.
-
When you want larger batch sizes.
-
Essential for training billion-parameter models.
Implementation: PyTorch's torch.utils.checkpoint.checkpoint(), DeepSpeed activation checkpointing. Selective checkpointing: only checkpoint specific layers (e.g., attention layers which have large activations). Combined with mixed precision and gradient accumulation for maximum memory efficiency.
Related questions
- Compare Adam, SGD with momentum, and AdaGrad. When would you use each?
- What is dropout and how does it prevent overfitting?
- What is transfer learning? When and why does it work?
- What is domain adaptation? Compare different approaches.
- What is mixed precision training? How does it speed up deep learning?