FlashAttention
Exact attention without the matrix
Chapter 12 of 21 · III. Fast attention and inference · about 30 minutes. Builds on: 6. Attention, 10. Generating text: the KV cache and inference cost.
Attention is bottlenecked by memory, not math
Section titled “Attention is bottlenecked by memory, not math”A GPU has a small amount of very fast on-chip memory (SRAM) and a large amount of slower memory (HBM, the “GPU memory” in the spec sheet). Moving data between them costs far more time than the arithmetic done on it. The textbook implementation of attention does this:
- Compute () and write it to HBM.
- Read , compute softmax (), write to HBM.
- Read and , compute , write .
For tokens, has a billion entries per head. Writing and reading these matrices dominates the runtime, and storing them dominates memory. FlashAttention computes exactly the same result but never writes or to HBM.
Tiling, and the softmax obstacle
Section titled “Tiling, and the softmax obstacle”Split into row blocks and , into column blocks small enough to fit in SRAM. For each block, loop over the blocks, computing partial scores on chip and accumulating into the output. The obstacle: softmax normalizes over a whole row, but the loop only sees one block of the row at a time. The way around it is the online softmax .
Online softmax, derived
Section titled “Online softmax, derived”For one query row, scores and values , the exact output is . For numerical safety one subtracts the row maximum . Process the row in blocks and maintain three running quantities for the blocks seen so far: the max , the denominator , and the unnormalized output . When a new block arrives with maximum , set . The old terms were computed against the old max, so rescale them by :
Each update leaves (, ) equal to what you would have computed with the new max over everything seen, so after the last block the result is exactly softmax-weighted attention. See it:
What FlashAttention gains
Section titled “What FlashAttention gains”- Memory: instead of , because the score matrix is never stored. That is what made 100K-token contexts practical to train.
- Speed: far fewer HBM reads and writes. The arithmetic count is unchanged (still ), but time falls because memory traffic dominated.
- Backward pass: instead of saving the probabilities, store only each row’s log-sum-exp (a vector of numbers) and recompute the scores block by block during backpropagation. This trades extra arithmetic, which is cheap on GPUs, for memory traffic, which is not.
- Exactness: it is not an approximation. Outputs match standard attention up to floating-point rounding.
FlashAttention-2 reorganized the loops and work partitioning across thread blocks and warps and reduced non-matrix-multiply work, giving roughly a further 2× speed-up on the same hardware.
Why you should know this even if you never write a kernel
Section titled “Why you should know this even if you never write a kernel”Attention kernels are the reason sequence length is a software choice rather than a hard wall, they appear in every training and inference framework, and their numerical behavior (the order in which sums are accumulated, low-precision accumulation) is one source of tiny output differences between hardware and software versions.
Check yourself
Section titled “Check yourself”Why can FlashAttention be faster even though it does the same number of FLOPs?
Runtime is dominated by reading and writing the matrices to slow memory; keeping blocks in on-chip memory removes most of that traffic.
In the online update, what happens to m, l and o when a new block does not contain a larger score than the current max?
, so the rescale factor ; the new block’s terms are simply added to and .
Why store a log-sum-exp per row rather than the probability matrix for the backward pass?
With log-sum-exp the probabilities can be recomputed from the scores, which are themselves recomputed from and blocks; storing only numbers per head avoids memory.
Papers
Section titled “Papers”- The online softmax normalizer; short and readable.
- FlashAttention.
- FlashAttention-2.