Topic 35 of 62 · Training at scale
Activation checkpointing
MediumHow does activation checkpointing (gradient checkpointing) reduce memory, and what is the trade-off?
1 · Start with the intuition
To compute ∂L/∂θ during the backward pass, each layer needs the intermediate activations it produced during the forward pass. Holding every intermediate tensor for all L layers alive at the same time creates a huge activation memory wave—especially at long sequence lengths. Activation checkpointing trades compute for memory: during the forward pass, it saves only the boundary inputs between selected blocks (e.g., each Transformer layer’s input) and discards the internal activations; when backward reaches that layer, it re-runs that layer’s forward pass on the fly to reconstruct the needed intermediates.
2 · How the mechanism works
Mechanism step 1 of 3 · no question to answer
Save block boundaries only
During forward, keep the input tensor at each checkpoint boundary and free the internal layer intermediates.
Next, we will look at: Recompute just in time. Move on when this operation makes sense.
Visualizing the behavior
Recomputation costs about one extra forward pass
Illustrative- Forward
- Recompute
- Backward (≈ 2× forward)
Peak stored activations for n = 16 equal layers
Derived3 · The governing equation
Standard forward: save all internal activations per layer (high memory, 1× forward FLOPs) Full checkpointing: save only 1 boundary tensor per layer (low memory, ~2× forward FLOPs → ~33% extra step FLOPs)
Notation: This is in-memory activation recomputation during a training step—completely distinct from saving model weight checkpoints to disk for fault tolerance.
Since the backward pass of a Transformer costs roughly 2× the FLOPs of the forward pass (one matmul for input grads, one for weight grads), a normal step costs ~3 forward-equivalents (1F + 2B). Re-running the full forward pass of every layer during backward turns that into 2F + 2B = 4 forward-equivalents—about 33% more FLOPs—while collapsing stored activation memory from ~34·s·b·h per layer to just 2·s·b·h per layer (the layer input). Note that activation checkpointing only shrinks activation memory; it does not change weight, gradient, or optimizer-state memory.
4 · Put concrete numbers on it
Illustrative scenario5 · Common misconception to avoid
Confusing activation checkpointing with saving model checkpoints to disk, or thinking activation checkpointing will fix an OOM caused by unsharded optimizer states at microbatch size 1 and short sequence length.
In practice · implementation & engineering rule of thumb
Wrap at the Transformer block level with use_reentrant=False, and disable the KV cache (model.config.use_cache = False) during training since cached K/V states are useless in a parallel training forward pass.
from torch.utils.checkpoint import checkpoint
# Inside DecoderLayer.forward during training:
if self.gradient_checkpointing and self.training:
hidden_states = checkpoint(self._block_forward, hidden_states, attention_mask, use_reentrant=False)
else:
hidden_states = self._block_forward(hidden_states, attention_mask)Go one level deeper
Follow-ups that often come after a correct first answer.
- Selective recomputation
- Not all activations cost the same to rebuild. Megatron’s selective recomputation keeps expensive matmul outputs and recomputes large but cheap tensors, such as attention softmax and dropout outputs, recovering most of the memory for a few percent of compute.
- Checkpointing vs other memory levers
- Checkpointing only reduces activation memory. It does nothing for weights, gradients or optimizer states; those need sharding (ZeRO/FSDP), lower precision or offload. Profile which of the four dominates before choosing.
6 · Self-check & interview synthesis
Explain it in your own words first
Before revealing the reference answer, try answering the opening question aloud in four beats: what it is → how the math works → what trade-off it makes → a concrete number or failure mode.
Follow-up question interviewers often ask next
MediumWhy must a checkpointed block restore the random number generator (RNG) state during its backward recomputation?
Show follow-up reasoning
If the block contains a stochastic operation like dropout, recomputing the forward pass with a different random seed would zero out a different subset of activations than the original forward pass used—producing activations and gradients that do not match the output produced in forward.