FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness
This research paper explains how tiling Q, K, and V through on-chip SRAM with an online softmax yields exact attention that is faster and far more memory-efficient than materializing the full N×N score matrix in HBM.
On GPUs, exact attention is often slow not because of arithmetic intensity but because of memory traffic. The textbook algorithm materializes the full score matrix (and usually the attention probabilities) in HBM. For long sequences that matrix dominates both memory and wall-clock time — even when the FLOP count looks fine on paper.
FlashAttention’s punchline: treat attention as an IO-aware algorithm. Tile , , and through fast on-chip SRAM, fuse the softmax with the matmuls, and never write the full attention matrix to HBM. The result is still exact attention (same math as softmax attention), just scheduled for the memory hierarchy.
What this paper explains
Standard attention for sequences of length and head dimension costs FLOPs but also HBM reads/writes if you store and . HBM bandwidth is the scarce resource; SRAM is small but much faster.
Dao et al. show you can:
- Load blocks of and (and streaming blocks of ) into SRAM.
- Compute partial attention scores and maintain running softmax statistics (online softmax / “safe” streaming normalization).
- Accumulate the output block in SRAM and write only the final outputs back to HBM.
- In the backward pass, recompute attention instead of storing the huge intermediate — trading FLOPs for IO, which is a win when IO dominates.
Prior limits
- Materialized attention — simple to implement in frameworks, disastrous memory at long context.
- Approximate attention (sparse patterns, low-rank, kernelized) — can cut memory/FLOPs but change the function; quality and kernel fusion become research projects of their own.
- Kernel fusion without tiling — helps launch overhead, but still fails if the algorithm insists on writing temps.
The mechanism
Think in blocks. Choose block sizes so a tile of , , fits in SRAM. For each query block, iterate key/value blocks:
- Update local scores .
- Update running row-max and normalizing denominators so the final softmax matches the global one.
- Accumulate weighted contributions.
Because the algebra of softmax can be rewritten with running statistics, you never need all columns of a row resident at once — only the statistics that make the eventual normalization exact.
Algorithm / figure walkthrough
- Partition into SRAM-fitting blocks.
- For each block, initialize output accumulators and softmax stats to neutral values.
- Stream blocks; update stats + output in SRAM.
- Write the finished block to HBM; discard score tiles.
- Backward: reload inputs, recompute block attention as needed, accumulate gradients.
The opening figure contrasts an HBM-resident canvas with a small SRAM working set that slides over tiles.
What to notice when reading
- The paper’s analysis is about HBM accesses, not just FLOPs — count bytes moved.
- Exactness comes from online softmax, not from dropping terms.
- Wall-clock gains depend on sequence length and hardware; the IO model predicts when tiling wins.
Results and evidence
The paper reports end-to-end speedups and large memory savings versus standard attention on long sequences (their benchmarks show up to roughly 2–4× faster attention and 10–20× lower memory for attention at long , depending on setting). Lower memory enables longer context and larger batches; they demonstrate improved training throughput on language and long-document tasks relative to baseline attention implementations of the era.
Treat those ranges as the authors’ reported operating regime on the GPUs they used — not a universal constant for every model size.
Limitations
- Implementation is hardware-sensitive; block sizes must fit SRAM and warp constraints.
- Exact FlashAttention still has FLOPs — it does not asymptotically beat attention’s compute, only its IO/memory profile.
- Very short sequences may not benefit; overheads can dominate when is small.
- Later variants (FlashAttention-2, etc.) refine parallelism further; this paper is the IO-aware foundation.
How to read the paper
- Abstract + §1 — HBM vs SRAM motivation.
- §2 — background on attention and IO complexity.
- §3 — tiling algorithm and online softmax.
- §4–5 — runtime/memory experiments and ablations.
- Appendix — numerical details / backward recomputation.
Knowledge check
What does FlashAttention refuse to write to HBM in the forward pass?
Is FlashAttention an approximate attention method?
Why can recomputing attention in the backward pass still be faster?
Keep reading
- Original paper (arXiv:2205.14135)
- Related Fanout Daily: Attention Is All You Need, GQA, PagedAttention.
Sources
- Dao et al., FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness, NeurIPS 2022 — arXiv:2205.14135