4.1.14 · D3Transformer Architecture

Worked examples — Flash attention and efficient attention

3,507 words16 min readBack to topic

This page is a drill deck. The parent note built the ideas (tiling, incremental softmax, recomputation). Here we hammer every shape the maths can take: normal blocks, blocks with different maxima, a degenerate all-equal row, a masked (causal) block, a single-block "edge" case, a memory word-problem, and an exam twist.

Throughout, HBM means High-Bandwidth Memory — the large but comparatively slow GPU RAM (tens of GB) where big matrices must otherwise be parked. The whole point of Flash Attention is to avoid writing the matrix out to HBM.

Before anything, one reminder of the two running numbers Flash Attention carries per query row. If you have not met them, read them here first:

Now the exact recipe every example below follows. Learn this once and every cell is just plugging numbers in.

Recall Why never store the full matrix?

What object does standard attention write to slow GPU memory (HBM)? ::: The full scores matrix and probabilities . What does Flash Attention keep instead, per query row? ::: Just three small things — a max , a sum , and an unnormalized output vector . Why divide by only at the very end and not per block? ::: Because is not complete until the last block; dividing early would use a partial denominator.


The scenario matrix

Every worked problem below is tagged with the cell it covers. Together they fill the table.

Cell Scenario class What can go wrong / why it matters
A Two blocks, equal maxima Rescale factors are all — the "easy" case, warm-up
B Two blocks, different maxima Rescale factors — this is the whole point of
C Degenerate: all scores equal softmax collapses to uniform ; check no divide-by-zero
D Masked / causal block (future keys ) Whole entries drop out;
D2 Fully masked row (every key ) → divide-by-zero danger; needs an explicit guard
E Single block (edge case) Flash must reduce exactly to plain softmax
F Limiting: one score softmax becomes a one-hot; output one value vector
G Word problem: memory in bytes The real-world payoff —
H Exam twist: does block order change the answer? Tests understanding of associativity of the merge

Cell A — two blocks, equal maxima


Cell B — two blocks, different maxima

This is the cell people get wrong, because they forget to rescale block 1 when block 2 reveals a bigger max. The figure below is a bar chart of the unnormalized exponential weight of each score.

Figure — Flash attention and efficient attention

How to read the figure. The horizontal axis lists the four scores in two groups; the vertical axis is the exponential weight (before normalizing). The blue bars are block 1 as it was first computed, using its own local max : the top score gives , the other . The pink bars are block 2, computed with its own max (again and ). The yellow arrow marks the moment we discover the true global max lives in block 2, so . The yellow bars show what block 1 becomes after we multiply it by : the tall blue shrinks to and the shrinks to . Those shrunken yellow bars are the ones we actually sum. So the picture literally says: block 1's bars must be pushed down to the same baseline as block 2 before adding.


Cell C — degenerate: all scores equal


Cell D — masked / causal block

In GPT-style decoders a query at position may not look at future keys . Flash Attention implements this by setting those scores to before the exponential.

Recall Masking mechanics

How does Flash Attention exclude a forbidden key? ::: Set its score to so ; it contributes nothing to or .


Cell D2 — the fully masked row (divide-by-zero danger)


Cell E — single block (edge case)


Cell F — limiting behaviour: one score dominates


Cell G — word problem: the memory payoff in bytes


Cell H — exam twist: does block order matter?


Connections

  • Built on ordinary self-attention and used per-head inside multi-head attention.
  • The recomputation trick is the same idea as gradient checkpointing — trade compute for memory.
  • Long-context feasibility feeds directly into scaling laws, and powers both BERT and GPT style models (the causal masks in Cells D and D2 are decoder features).
  • Masking interacts with positional encoding since positions decide which keys are in the future.
Recall Self-test

In Cell B, why is the block-1 rescale factor ? ::: Because ; block 1 was measured against too-small a max. Why does Cell C never divide by zero? ::: After the max-shift, at least one exponential is , so . What must a correct kernel do for a fully masked row (Cell D2)? ::: Detect and return the zero vector instead of computing . Does streaming blocks in reverse order change the result (Cell H)? ::: No — the merge is commutative/associative because all blocks are rescaled to the same global max.