FlashAttention and activation checkpointing: trading work for memory
Two exact tricks that make big networks fit. FlashAttention computes attention in tiles with an online softmax and never stores the score matrix; activation checkpointing keeps a few activations and recomputes the rest.
What runs in CodeSplash
- TRN032FlashAttention Explained here
- TRN033Activation checkpointing / gradient rematerialization Explained here
You will be able to
- Explain why the attention score matrix dominates memory for long sequences
- Compute a softmax normalizer block by block with the online rescaling
- Explain what activation checkpointing stores, what it recomputes and what it costs
- Say why both methods give the same results as the plain computation
Where the memory goes
ReadSelf-attention compares every token with every other: for a sequence of tokens, each head builds an score matrix. Double the length and that matrix grows fourfold. Plain implementations write it to the GPU's main memory, read it back for the softmax, write the probabilities, and read them again; moving those numbers takes longer than the arithmetic.
Training adds a second cost: every layer's activations are kept for the backward pass, so memory grows with batch × length × width × depth.
Online softmax
ReadA softmax needs the normalizer over all scores. The online softmaxOnline softmaxCompute a softmax normalizer block by block, rescaling the running sum whenever a new maximum appears. computes it block by block: keep a running maximum and a running sum ; when a new block brings a larger maximum , rescale the old sum by before adding the block's terms.
In the reference's example, two blocks of scores processed this way give a log-normalizer of 3.4608, exactly the value from all scores at once. Nothing is approximated.
FlashAttention
ReadFlashAttentionFlashAttentionExact attention computed tile by tile in fast on-chip memory, never storing the full score matrix. loads tiles of queries, keys and values into fast on-chip memory, computes their scores, updates the running maximum, sum and output with the online softmax, and moves on. The full matrix never exists in main memory. For the backward pass it keeps only the per-row maximum and sum and recomputes the tiles' scores.
The result equals ordinary attention up to floating-point rounding. It does the same arithmetic, or slightly more; it wins by moving far less data, so memory for attention grows with instead of .
Activation checkpointing
ReadActivation checkpointingGradient checkpointingSaving memory by recomputing some activations during the backward pass. It isn't a saved model checkpoint. keeps the activations only at chosen layers, the checkpoints, and drops the rest. In the backward pass, each segment is recomputed from its checkpoint before its gradients are taken. The reference's example recomputes a layer from its saved input and confirms the result matches the stored one exactly.
With layers and checkpoints every layers, stored activations fall from order to order , for about one extra forward pass of compute per step.
What runs here
ReadTheir effect shows up in the memory arithmetic CodeSplash does do: a network's Resources estimate separates weights, gradients, optimizer state and activations, and the activations are the part checkpointing would shrink.
Predict: is FlashAttention approximate?
PredictPredict first
A colleague worries FlashAttention changes a model's predictions because it "approximates attention". Are they right?
Reveal
No. The online softmax rescaling makes the tiled computation exactly equal to the full one, up to floating-point rounding. Approximate attention methods exist, but FlashAttention is not one of them.
Common mistakes
ReadCheck: memory tricks
CheckpointCheck yourself
What does FlashAttention mainly save?
Show the answer
Answer: a
It never writes the full score matrix to main memory; the arithmetic stays about the same.
Check yourself
In the online softmax, the running maximum rises from m to m′ when a new block arrives. What happens to the running sum ℓ of earlier exponentials?
Show the answer
Answer: a
Rescaling expresses the old terms relative to the new maximum, which keeps the exponentials from overflowing and the sum exact.
Check yourself
Sequence length doubles. Roughly how does the memory for attention scores grow with plain attention and with FlashAttention?
Show the answer
Answer: a
The plain score matrix is n × n; FlashAttention keeps per-row statistics and tiles, which grow with n.
Check yourself
What does activation checkpointing trade?
Show the answer
Answer: a
Recomputation gives the same activations, so the gradients are unchanged; only time is spent.
Check yourself
A network with dropout uses activation checkpointing. What must the recomputation get right for the gradients to be correct?
Show the answer
Answer: a
Recomputing with fresh randomness would give different activations than the loss was computed from. Frameworks save the random state for this.
Recap
Recap- Attention's n × n scores and training's stored activations dominate memory for big models.
- The online softmax computes the normalizer block by block, exactly.
- FlashAttention tiles attention through fast memory and never stores the score matrix: exact, faster, leaner.
- Activation checkpointing stores a few activations and recomputes the rest, for about one extra forward pass.
Concepts in this lesson