FlashAttention computes softmax attention one block of keys at a time, without storing the full score matrix. Derive the online softmax recurrence that keeps a running maximum and a running sum , and test it on the scores split into the blocks and with values ?
Sequence models and transformers, Efficient attention
Level L4 Advanced. Length Long. Nature Th+Mat Both.
What's being asked
Whether you can derive how a softmax is computed in a single streaming pass, which is the maths that makes tiled exact attention possible.
Key idea
Keep so far and . When a new block has maximum and sum , the merged values are and , which rescales the old sum to the new reference point.
Steps
- Stable softmax uses with , to avoid overflow. This normally needs two passes, one for and one for the sum.
- Suppose a block gave . Its sum relative to a larger reference is . Summing both blocks at gives the recurrence above.
- Block 1 : , . Block 2 : , .
- Merge: and . The one-pass result equals the direct .
- Output. Keep a normalised running output . Block 1 gives and block 2 gives . Merge .
- Check with the full softmax: weights on values give . The two agree, so the result is exact, not an approximation.
- Why it matters. Only blocks of and plus the running per query are held in fast on-chip memory, so the score matrix is never written to slow memory. FLOPs are unchanged, but memory traffic falls.
Common mistakes
- Forgetting to rescale the old sum by when the maximum changes. Adding directly is wrong whenever .
- Saying FlashAttention is an approximation. It is exact, with the same output as standard attention up to rounding.
- Saying it reduces the FLOPs from quadratic to linear. Compute stays . Memory for the scores becomes and slow-memory reads and writes drop.
How long your answer should be
One page; 6–8 minutes.
Last checked on 4 December 2026.
Read this
Previous in Sequence models and transformers: Assume the loss follows and training compute is . Derive the compute-optimal and and their scaling exponents. What do and imply?
Next in Sequence models and transformers: Prove that a transformer encoder layer without positional encodings is permutation-equivariant: permuting the input tokens permutes the outputs in the same way. How do you show that a decoder with a causal mask and no positional encodings is not, and that it can still recover position information?