4.1.14 · D5Transformer Architecture
Question bank — Flash attention and efficient attention
Before the traps, a tiny glossary so no symbol is used unexplained:
True or false — justify
Flash Attention returns a mathematically approximate version of standard attention.
False. It computes the exact same softmax output — the tiling and online rescaling are just a different order of arithmetic, not an approximation like Linformer/Performer.
Flash Attention reduces the number of floating-point operations (FLOPs) compared to standard attention.
False. It does slightly more FLOPs (≈1.2× in the backward pass, from recomputing ). The win is fewer HBM reads/writes, not fewer multiplications.
Flash Attention changes the asymptotic time complexity of attention from to .
False. Compute is still ; only the memory footprint drops from to . Wall-clock speedup comes from avoiding memory round-trips, not from a lower arithmetic order.
The scaling inside softmax is what Flash Attention removes to save memory.
False. The divisor is untouched — it stabilizes score variance and is part of the definition of attention. Flash Attention only changes how the softmax is computed, not what it computes.
Because Flash Attention never stores the full matrix, it cannot be used during training (only inference).
False. It works in training precisely because it recomputes and in the backward pass instead of storing them — that recomputation is the whole point.
If you double the sequence length , standard attention's HBM traffic for roughly quadruples.
True. has entries, so makes it — the quadratic term dominates for long sequences, which is exactly what Flash Attention removes.
For very large head dimension (say ), Flash Attention's relative speedup shrinks.
True. The memory bottleneck is worst when each moved byte does little arithmetic (small ). When is huge the kernel is already compute-bound, so removing memory round-trips helps less.
Flash Attention still needs to load , , and from HBM.
True. The inputs and the final output must live in HBM ( traffic). What it avoids is the extra traffic of writing and re-reading the intermediate and .
Spot the error
"Flash Attention keeps the whole scores matrix in fast SRAM instead of slow HBM, that's why it's faster."
Error: SRAM (~20 MB) is far too small to hold a large matrix. Flash Attention never materializes the full matrix anywhere — it holds only small tiles in SRAM at a time.
"To merge two blocks we just add their softmax denominators: ."
Error: The two blocks used different max-references (). You must rescale first: , else you're summing incompatible baselines.
"After the max-shift , the softmax denominator for a two-element row is always , i.e. ."
Error: Only the max entry becomes ; the other becomes . In the parent's example the denominator was , not .
"Since block outputs are exact once computed, we never touch again after moving to block 2."
Error: Earlier unnormalized outputs must be rescaled by whenever a later block raises the running max. That correction is essential for exactness.
"Flash Attention is an approximate/linear-attention method like Performer, just optimized."
Error: They're categorically different. Linear-attention methods change the math (approximate softmax) to get compute; Flash Attention keeps softmax exact and only reorganizes memory access. See 4.1.3-Multi-head-attention — Flash Attention slots in per head unchanged.
"Gradient checkpointing and Flash Attention are unrelated ideas."
Error: Flash Attention's backward pass is a form of 5.27-Gradient-checkpointing specialized to attention: it drops the stored activations (, ) and recomputes them on demand, trading compute for memory.
"The softmax denominator can be computed independently of the running max."
Error: The max and the sum are coupled — every exponential is , so changing rescales the whole sum. That coupling is exactly why merging blocks needs the factors.
Why questions
Why does memory bandwidth, not compute, dominate standard attention's runtime?
Moving the score/probability matrices between slow HBM (~1.5 TB/s) and the chip costs on the order of the compute time, and standard attention reads/writes them multiple times, so the kernel spends most wall-clock waiting on memory.
Why subtract the max before exponentiating?
Raw overflows for large scores. Subtracting the max makes the largest exponent exactly while leaving the ratios (and hence the softmax) unchanged — it's numerical safety, not a change of answer.
Why must we rescale earlier block contributions when a new block has a bigger max?
All exponentials share one reference . When grows, every previously accumulated term was computed against a smaller reference and is now too large by a factor , so we multiply them down to the common baseline.
Why does Flash Attention recompute and in the backward pass instead of caching them?
Caching them costs memory — the exact thing we're trying to avoid. Recomputing them from the stored costs only ~1.2× FLOPs but keeps memory at , and memory is the binding constraint.
Why is the memory reduction more valuable for long sequences?
Because grows explosively — doubling quadruples the attention-matrix memory. Removing that term is what lets models handle long contexts (relevant to 4.13-Scaling-laws-for-transformers and long-context 4.2.3-GPT-architecture variants).
Why can Flash Attention accumulate the output incrementally rather than waiting for all blocks?
Because the output is a weighted sum of value vectors, and sums can be built up block by block — as long as we carry the running normalizer and rescale partial outputs when the max updates, the running estimate converges to the exact result.
Why does the online-softmax trick generalize to any number of blocks, not just two?
Merging is associative: merging block 1 with block 2, then that result with block 3, gives the same as any other grouping — each merge just re-references everything to the new global max.
Edge cases
What if the entire sequence fits in a single block (one tile)?
Then there's no merge step — , all rescale factors are , and Flash Attention reduces exactly to ordinary safe-softmax attention.
What if two blocks happen to have the same max (as in the parent example)?
Both rescale factors are , so the merge simplifies to plain addition of and of . Rescaling costs nothing here but is still logically present.
What if a whole row of scores is (e.g. a fully-masked causal position with nothing to attend to)?
Every exponential is , so and the softmax is undefined (0/0). Implementations guard this — a query with no valid keys should never occur; causal masking (see 4.1.8-Positional-encoding neighbours) always leaves at least the token itself.
What happens with causal masking inside a tiled loop?
Blocks entirely in the "future" of a query are skipped (they'd be fully masked), so causal Flash Attention does less work, not more — a big reason it's efficient for 4.2.3-GPT-architecture decoders.
What if all attention scores in a row are equal?
The softmax is uniform ( each); the block sums still combine correctly because equal scores share the same max and every exponential is . The output is just the average of the value vectors.
Does Flash Attention change the numerical result versus standard attention, bit-for-bit?
Not exactly bit-for-bit — floating-point addition isn't perfectly associative, so a different summation order gives tiny (≈) differences. Mathematically it's exact; numerically it's within normal float tolerance.
What about the encoder-style bidirectional case (no mask), like 4.2.1-BERT-architecture?
Flash Attention applies unchanged — every query attends to every key, so no blocks are skipped, but the tiling and online softmax still avoid materializing the matrix.
Recall One-line summary to lock in
Flash Attention = exact softmax attention, reorganized so the score matrix never touches HBM; it trades ~1.2× compute for an memory drop and a 2–4× wall-clock speedup.