FlashAttention: tiling and online softmax
Compute exact attention block by block in fast on-chip memory, using a running max and running sum for the softmax. Explain why it is faster despite the same FLOPs: it never writes the T×T matrix to slow memory.
Builds on: Quadratic cost of attention in context length, Roofline model: compute- vs memory-bound
What the animation says
The T×T bottleneck
Standard attention scores every query against every key: a T by T grid. That grid is written to the GPU's big, slow memory, read back for softmax, written again, then read once more. At about eight thousand tokens, that's sixty-seven million numbers per head, every pass. Softmax does only a few operations per byte it moves, so the GPU mostly waits on memory. And softmax needs each row's max and sum before it can finish. So: can exact attention skip storing the grid?
Tiles and running stats
Yes, exactly. Compute one score tile at a time in fast on-chip memory, use it, then discard it. Each query row keeps a running max, a running sum of exponentials, and an unnormalized output. Block one: scores one and three, values ten and twenty. Max: three. Weights, e to score minus max, are point one three five and one. Sum: one point one three five. Output: twenty-one point three five. Block two brings scores two and five. The max jumps to five. What about the old sum?
Rescale, add, divide
Rescale. The old terms were measured against max three, but now the max is five. So multiply the old sum and the old output by e to the three minus five: point one three five. New sum: the shrunk old sum, plus e to the minus three, plus one. That's one point two oh three four. New output: the shrunk old output, plus the new weighted values. Forty-four point three eight. Divide at the end: thirty-six point eight eight. Full softmax over all four scores gives the same number.
Find the bug
Here's the loop in code: one query row, keys in blocks of two. This line finds the new max. This one computes the correction factor c. This line turns the block's scores into weights against the new max. Someone wrote this version, and its result doesn't match ordinary attention. Tap the two lines that break it.
Exact and fast
The fix: multiply the old sum and output by c. Now the check against ordinary attention prints True. The arithmetic is unchanged: the same matrix multiplies as standard attention. But score tiles live only in on-chip memory. Only Q, K, V, the output, and each row's max and sum reach slow memory. Each loaded key-value tile serves a whole block of queries, so work per byte grows with the query block size. With a causal mask, tiles fully above the diagonal are skipped. Extra memory grows with T, not T squared.
Tiles in fast memory, statistics on the fly
The online-softmax update
Two key blocks, one query row
Blockwise attention matches the full softmax
import numpy as np
T, dh, Bc = 8, 4, 2
rng = np.random.default_rng(0)
q, K, V = rng.normal(size=dh), rng.normal(size=(T, dh)), rng.normal(size=(T, dh))
m, l, o = -np.inf, 0.0, np.zeros(dh)
for j in range(0, T, Bc):
s = K[j:j+Bc] @ q / np.sqrt(dh)
m_new = max(m, s.max()); c = np.exp(m - m_new)
p = np.exp(s - m_new)
l, o, m = c*l + p.sum(), c*o + p @ V[j:j+Bc], m_new
w = np.exp(K @ q / np.sqrt(dh)); ref = w @ V / w.sum()
print(np.allclose(o / l, ref))
Same FLOPs, far less memory traffic
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses