Lesson 9 of 14
FlashAttention
To read a long document, a model scores every word against every other, and the obvious way stores that whole table in GPU memory, which grows four times when the document doubles. FlashAttention gets the same answer one small tile at a time on the chip, and never writes the table down.
A table too big for the desk
The 3D view could not load. The three stages are described below.
01 · The table
Every token against every other
When you paste a document into a chatbot, the model reads it as tokens (a token is a piece of a word, about three-quarters of one on average). To understand each token in context, a step called attention scores it against every token before it: how much should this word look at that one? The scores fill a table with one row and one column per token, and the model keeps one for each head, 32 side-by-side copies in each layer.
Double the document and the table gets four times bigger. For your 32,768-token documents, one layer’s tables come to 68.7 GB. Add the model and its KV cache and you need 89.1 GB of an 80.0 GB GPU. That is your out-of-memory error, long before the cache is full.
Double the prompt, four times the table
- Prompt
- One layer's table
- Model + KV cache + table
02 · The trick
A note that corrects itself
FlashAttention (Tri Dao and colleagues, 2022) never writes the table down. Lesson 1’s clerk scores one small tile of it on her desk, the chip’s fast memory, uses it, and throws it away.
The catch is the percentages. Each score is first boosted, so high scores count far more than low ones, and measured against the row’s highest score, which the clerk has not seen yet. So she keeps a sticky note per row: the highest score so far and a running total. When a higher score turns up, one correction factor shrinks everything already on the note.
One row, two scores at a time
Sticky note
- Highest score so far
- –
- Running total
- –
- Correction this tile
- –
03 · Tiles
Same answer, fewer trips
The note also holds the blend so far, corrected the same way. After the last tile, dividing the blend by the total gives the answer. It is not an estimate: it matches the obvious way’s apart from rounding.
The belt still brings K and V up again for every block of rows, but the table never rides it. For one head of your documents that is 4.31 GB instead of 8.62 GB, about 2.0 times faster, and nothing stored.
This helps when the model reads a long prompt. When it writes a reply, each step adds one token, so there is a single row of scores and no big table to avoid; that step’s time goes into reading the KV cache.
The table, one tile at a time
Numbers moved to and from GPU memory
Row 7: its sticky note
| highest score m | – |
|---|---|
| running total l | – |
| answer so far | – |
| exact answer | – |
04 · Playground
Fit a document yourself
Set the length to 32,768 tokens and watch the obvious way overflow. Then read several documents at once: push far enough and even FlashAttention runs out, because the KV cache itself fills the GPU.
Does the document fit?
Llama 3.1 8B on one NVIDIA H100, while it reads the documents
- The model
- KV cache
- One layer's score table
- Past the end of memory
05 · Glossary
The vocabulary, in one place
- Token
- The unit a model reads and writes: a piece of a word, about three-quarters of one on average.
- Attention
- The step where each token scores every token before it, and itself, for how much to look at it, then blends their information in those proportions.
- Score table
- One row and one column per token. The obvious way fills in the whole square and blanks out the half that looks ahead. 2.15 GB per head at 32,768 tokens.
- Head
- One of the side-by-side copies of attention in a layer, each with its own score table. Llama 3.1 8B has 32 per layer.
- Layer
- One stage of the model; every token passes through all of them. Llama 3.1 8B has 32.
- Q, K, V and O
- The prompt stored three ways: what each token is looking for (Q), what it offers to be matched on (K), and the information it passes on (V). O is the answer attention produces.
- Percentages
- Each row of scores, boosted and divided by the row total so it adds up to 100%. Also called the softmax.
- Desk (on-chip memory)
- The clerk's small, fast memory inside the chip: 228 KiB in each of the H100's processor blocks, as its spec states it.
- Archive (GPU memory)
- The chip's large main memory, 80.0 GB on an H100, where the model, the KV cache and any stored table live.
- Belt (memory bandwidth)
- How many bytes a second move between the archive and the desk: 3.35 TB/s on an H100.
- Tile
- A small block of the score table: some rows against some columns, small enough to fit on the desk.
- Sticky note
- Per row: the highest score so far, the running total and the blend so far. It lives on the desk and is thrown away with the row; it is not the KV cache.
- FlashAttention
- Attention done tile by tile on the desk, keeping only the sticky notes, so the score table is never stored (Tri Dao and colleagues, 2022).
You can now
- Explain why the obvious way to compute attention runs out of memory on long documents, and compute the score table's size (and the GPU's total need) for any length, head count and batch.
- Run online softmax by hand: keep a running max, sum and output per row, and apply the correction factor whenever the max changes, to get the same answer as the full softmax.
- Estimate memory traffic and arithmetic intensity for standard attention and FlashAttention on a given GPU, and decide when attention is memory-bound and when decode needs Flash-Decoding's key split.
Open the workbook: the formulas, the exact numbers, graded exercises and the sources behind this page.