Exercises — Flash attention and efficient attention
Quick symbol refresher, so line one is readable:
- = number of tokens (rows of the sequence).
- = = dimension of each query/key vector.
- = query, key, value matrices, each .
- = the attention-scores matrix (row , column = "how much token looks at token ").
- = the maximum entry of a score row (used to make safe).
- = the softmax normaliser (the running "denominator").
- = the unnormalised block output (numerator before dividing by ).
Prerequisite ideas live in 4.1.1-Self-attention-mechanism and 4.1.3-Multi-head-attention; the memory trick is cousin to 5.27-Gradient-checkpointing.
Level 1 — Recognition
Exercise 1.1
For a sequence of tokens with , how many floating-point entries does the full scores matrix hold? Give the count and the memory in megabytes if each float is 4 bytes.
Recall Solution
is entries. Bytes bytes MB. What we did: counted rowscolumns (that is what "" means) and multiplied by 4 bytes. Why: this is the object Flash Attention refuses to store — the whole point.
Exercise 1.2
Which of these does Flash Attention never write to slow GPU memory (HBM)? (a) , (b) , (c) the full probability matrix , (d) the final output .
Recall Solution
Answer: (c). and the output still live in HBM — you must read inputs and write the answer. What Flash Attention avoids is materialising the full matrices and ; they only ever appear as small tiles inside fast on-chip SRAM.
Exercise 1.3
State the memory complexity of standard attention and of Flash Attention, in terms of .
Recall Solution
Standard: (it stores the full square scores matrix). Flash: (it keeps only running statistics per query row plus one block at a time).
Level 2 — Application
Exercise 2.1
A row of raw attention scores is . Compute the safe-softmax normaliser where .
Recall Solution
. Shifted exponents: Why subtract : is fine, but for larger scores overflows; shifting so the biggest exponent is keeps every term in and never changes the final softmax (top and bottom both get multiplied by , which cancels).
Exercise 2.2
Same row , values (scalars). Compute the softmax output where .
Recall Solution
Using from 2.1: Output What it means: the two large-score tokens (both ) split most of the weight, dragging the output toward their values and ; the weak tokens barely count.
Exercise 2.3
Split the same row into two blocks: block 1 , block 2 . Compute per-block , then merge to get global and confirm you recover .
Recall Solution
Block 1: , . Block 2: , . Merge: . Rescale factors and . ✓ Why the rescale: each block measured its exponents against its own local max; before adding sums we must re-express them against the common global max — the factor does exactly that shift.

Level 3 — Analysis
Exercise 3.1
Two blocks have local maxima , and local sums . Compute the merged and .
Recall Solution
. Rescale factors: for block 1, for block 2. What it looks like: block 2's whole sum () shrinks to because its scores were tiny compared with block 1's peak — exactly the desired behaviour: far-below-max tokens contribute almost nothing.
Exercise 3.2
Continue 3.1 with unnormalised block outputs (2-dim): , . Compute the final normalised output .
Recall Solution
Numerator Why: the numerator () and denominator () are rescaled by the same factors, so dividing at the very end gives the exact softmax-weighted average — proving you can accumulate one block at a time and never store the full matrix.
Exercise 3.3
For standard attention, total HBM traffic is . At , what fraction of the traffic is the quadratic term? (Ignore constants; use vs .)
Recall Solution
. . Fraction So about 98.5% of memory traffic is the quadratic term. Insight: since , killing the round-trips (what Flash Attention does) removes essentially all the memory cost — the linear traffic is a rounding error.
Level 4 — Synthesis
Exercise 4.1
You process 3 blocks for one query row. Local stats: , , . Merge them left to right (online, one block at a time) and give the running after each step.
Recall Solution
Start with block 1: . Fold in block 2 (): . Running: . Fold in block 3 (): . Final: . Why online works: each merge only ever needs the current running and the new block — never the raw scores. That is why memory stays : the running pair is a fixed-size summary of everything seen so far.
Exercise 4.2
Cross-check 4.1 by computing directly as if all blocks shared the global max : i.e. verify .
Recall Solution
✓ Identical to the online result — order of folding does not matter, because merging is associative. This is the correctness proof of Flash Attention in one line.
Level 5 — Mastery
Exercise 5.1
Full micro-attention. One query row, , values are 2-vectors. Scores split into two blocks: block 1 scores with values ; block 2 scores with values . Compute per-block , merge, and give the final output . Then verify against plain softmax over all four scores.
Recall Solution
Block 1: . Exponents . . Block 2: . Exponents . . Merge: . Factors: block 1 ; block 2 . Numerator
Direct check (plain softmax over ): , exps , sum . Weights . Matches the tiled result to rounding ✓ — Flash Attention returns exactly standard attention, just without ever storing the full row.
Exercise 5.2
Degenerate case: all four scores equal ( for any constant ) with values . What is the attention output, and does it depend on ? What is ?
Recall Solution
With equal scores every , so and each weight . Output — the plain average of the values, independent of . Why: softmax cares only about score differences; a common shift cancels. This is the same cancellation that makes the safe-softmax max-subtraction harmless, and it is the "no information" limit — uniform attention.
Exercise 5.3
Overflow stress test. Raw scores (values ). Show why naive fails and compute the correct output with the max-shift.
Recall Solution
Naive: and both overflow to in float; NaN — the network breaks. Safe: . , . . Weights . Output Why it works: subtracting the max guarantees the largest exponent is and all others are in — never overflow, identical result. Flash Attention must do this per block, which is exactly why it tracks at all.
See also 4.13-Scaling-laws-for-transformers for why longer is worth this trouble, and 4.2.3-GPT-architecture / 4.2.1-BERT-architecture for models that ship these long-context kernels.