FlashAttention and IO-Aware Attention
FlashAttention computes the exact same attention output as the standard formula, but restructures the computation so the GPU never has to write the full attention matrix to slow memory — making it dramatically faster without any change in the result.
Prerequisites: Scaled Dot-Product Attention and the √d Factor, The Quadratic Cost of Attention
Standard attention is usually described purely in terms of arithmetic: multiply queries by keys, take a softmax, multiply by values. But on real hardware, arithmetic is often not the bottleneck — moving data between memory tiers is. A GPU has a small amount of extremely fast on-chip memory (SRAM) and a much larger pool of slower memory (HBM), and standard attention implementations write the full, huge attention matrix out to slow memory and read it back repeatedly. FlashAttention is not a new algorithm in the mathematical sense — it computes the exact same numbers — it is a new way of scheduling the computation so slow-memory traffic collapses, turning attention from a memory-bandwidth-bound operation into something close to compute-bound.
The analogy: cooking with a tiny counter and a distant pantry
Imagine a kitchen where the counter (fast SRAM) fits only a few ingredients at a time, and the pantry (slow HBM) holds everything but is a long walk away. A naive cook doing attention makes a giant shopping list, walks it to the pantry to write it down, and later walks back to read it again — most of the time is spent walking, not cooking. FlashAttention reorganises the recipe: work on small batches that fit entirely on the counter, finish everything possible with what's there, and only walk to the pantry for the next batch — never writing the full list down at all. The final dish is identical; only the number of trips to the pantry changes.
What standard attention writes to slow memory
In words: standard attention computes a full similarity matrix of size , turns it into weights via softmax, then multiplies by values to get output . and are both — for tokens, that's over 67 million entries, materialised in slow memory and read back at least twice.
The trick — tiling and online softmax. Split , , into small blocks that fit in fast SRAM. Process block pairs one at a time, computing partial output and keeping a running (online) estimate of the softmax normaliser, updated incrementally as each new key/value block arrives — so the full matrix is never stored at once, only small tiles, transiently, in fast memory.
Worked example 1: counting memory traffic on a small case
Take tokens, block size . Standard attention: materialise the full score matrix (16 numbers) in HBM, read it back for softmax, read it again to multiply by — roughly "number movements" to/from slow memory for the matrix alone.
FlashAttention with tiling: process key/value block 1 (2 keys) against all 4 queries — a tile (8 numbers) computed and consumed entirely in SRAM, only a small running max/sum (4 numbers) written to HBM per query. Block 2, same again. Total slow-memory traffic for the intermediate matrix: close to zero — only the small running statistics ever touch HBM, versus for the naive approach. At the gap looks modest; at the naive approach moves gigabytes while FlashAttention moves megabytes.
Worked example 2: wall-clock speedup at scale
Reported benchmarks at sequence length 2,048 on a mid-sized transformer block show standard attention taking roughly 41 ms per forward pass, dominated by memory traffic, versus roughly 7 ms for FlashAttention doing the identical computation — about a 5–6× wall-clock speedup, purely from restructuring memory access, output matching to floating-point precision.
| Sequence length | Standard attention | FlashAttention |
|---|---|---|
| 1,024 | 12 ms | 3 ms |
| 2,048 | 41 ms | 7 ms |
| 4,096 | 156 ms | 15 ms |
The gap widens as sequence length grows: naive memory traffic outpaces FlashAttention's, which stays close to linear for a fixed block size.
The naive approach's slow-memory traffic grows like the curve above — quadratically — while FlashAttention's traffic grows close to linearly, which is why the gap widens so sharply at long context.
FlashAttention produces mathematically identical output to standard attention — it is an exact, not approximate, method — and its entire speedup comes from tiling the computation so the huge intermediate attention matrix never has to be written to and read from slow memory, using an online-softmax trick to update results incrementally.
What this means in practice
FlashAttention (and its successors, FlashAttention-2 and -3) is the default attention implementation in essentially every modern training and inference stack, since it makes long-context training and serving feasible on hardware that would otherwise be memory-bandwidth-starved. It composes cleanly with Multi-Query and Grouped-Query Attention — one shrinks the cache, the other shrinks memory traffic during the computation itself.
FlashAttention reduces memory traffic and usage, not the number of floating-point operations — the underlying computation is still in FLOPs, exactly as before. Calling it "linear attention" is wrong; that description belongs to genuinely sub-quadratic methods like Linear Attention and Kernel Feature Maps or Sparse Attention: Longformer and BigBird. FlashAttention makes the existing quadratic computation run faster and leaner, without changing its asymptotic complexity.
Practice
- Why does writing the full attention matrix to HBM become more costly, relative to the useful compute, as grows — even though both the compute and the matrix size grow quadratically?
- Explain in one sentence why FlashAttention and MQA/GQA solve two genuinely different bottlenecks rather than being alternative solutions to the same problem.
Related concepts
Practice in interviews
Further reading
- Dao, Fu, Ermon, Rudra & Ré, FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness (2022)