■ Change #27 · wiki.edited
FlashAttention: IO-aware exact attention: Normalize Markdown to canonical form
explainer.student
**What attention is.** In a transformer, the neural-network design behind modern language models, each word-piece (token) of a text is represented by a vector. Attention lets each token collect information from the others. For $n$ tokens the model forms three $n \times d$ matrices $Q$, $K$, $V$ (queries, keys, values; $d$ is typically 64 to 128), computes the score matrix $S = QK^\top$ of size $n \times n$, turns each row into weights with softmax,− $$\mathrm{softmax}(s)_j = \frac{e^{s_j}}{\sum_k e^{s_k}},$$+ $\mathrm{softmax}(s)_j = \frac{e^{s_j}}{\sum_k e^{s_k}},$(positive weights that add to 1, the largest score getting the largest weight), and multiplies the weights by $V$.**What goes wrong.** $S$ has $n^2$ entries: for $n = 8192$ that is about 67 million numbers per attention head, written to GPU main memory and read back several times. The arithmetic is cheap next to this traffic, so the standard implementation is memory-bound (see the roofline entry), and its memory use grows like $n^2$.**The trick.** FlashAttention (Dao et al., 2022) never stores $S$. It splits $Q$ into blocks of rows and $K$, $V$ into blocks, and for each $Q$ block streams through the $K$, $V$ blocks inside fast on-chip memory. The obstacle is softmax: its denominator needs the whole row. The fix is an online softmax. Keep a running row maximum $m$ and a running sum $\ell$. When a new block raises the maximum from $m_{old}$ to $m_{new}$, multiply the old sum and the old partial output by $e^{m_{old}-m_{new}}$ and carry on. Because $e^{a-m_{new}} = e^{a-m_{old}}\,e^{m_{old}-m_{new}}$, the final answer is exactly what the standard method gives, not an approximation. Analogy: computing a class average from exam papers handed in one batch at a time, keeping only a running total and count instead of the whole pile. It breaks in one place: softmax also needs the rescaling step whenever a larger score turns up.**Backward pass.** Training needs gradients, the derivatives of the error with respect to every input, computed by running the network backwards (backpropagation). Instead of storing $S$ for this, FlashAttention keeps only $m$ and $\ell$ per row and recomputes score blocks on chip, trading extra arithmetic for much less memory traffic. Extra memory grows like $n$, not $n^2$.**Versions.** FlashAttention-2 (2023) reorganised the work across threads and reports about twice the speed of version 1. FlashAttention-3 (2024) targets the H100: data movement overlapped with arithmetic, and 8-bit floating point. Its abstract reports up to 740 TFLOP/s in FP16, 75% of the chip's peak, where version 2 reached 35%.**Limits.** The arithmetic still grows like $n^2$; only memory traffic falls. Each new GPU generation needs a new hand-tuned kernel, and reimplementations in easier languages are measured against these.
hash 9ef3056004c33c436da0122c241fe113e3e02e8e2bc5d74fc971f8a76dc08a81
previous 0404102bb18609acda116f862e866509707bac3755c6ef548615037110ec51a3