Skip to content

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.

Advanced ~13 min9 steps

What runs in CodeSplash

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

Read first

01

Where the memory goes

Read

Self-attention compares every token with every other: for a sequence of nn tokens, each head builds an n×nn \times n 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.

02

Online softmax

Read

A softmax needs the normalizer ∑jexj\sum_j e^{x_j} 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 mm and a running sum ℓ\ell; when a new block brings a larger maximum m′m', rescale the old sum by em−m′e^{m - m'} 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.

03

FlashAttention

Read

FlashAttentionFlashAttentionExact 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 n×nn \times n 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 nn instead of n2n^2.

04

Activation checkpointing

Read

Activation 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 LL layers and checkpoints every L\sqrt{L} layers, stored activations fall from order LL to order L\sqrt{L}, for about one extra forward pass of compute per step.

05

What runs here

Read

Their 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.

06

Predict: is FlashAttention approximate?

Predict

Predict first

A colleague worries FlashAttention changes a model's predictions because it "approximates attention". Are they right?

Yes, it approximates attention to save memoryNo, it computes exact attention in a different order
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.

07

Common mistakes

Read
08

Check: memory tricks

Checkpoint

Check 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.

09

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