FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness
Exact attention, several times faster, by respecting GPU memory
FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness
Attention is memory-bound, not compute-bound
Writes the full N×N score matrix to slow GPU memory, reads it back for softmax, then writes again. Most time goes to moving bytes, not math.
Computes attention tile by tile inside fast on-chip SRAM and never materializes the full matrix. Exact same output, far fewer memory reads and writes.
Counting FLOPs missed the real cost: memory traffic.
Tiles stay on chip; the big matrix never exists

SRAM runs near 19 TB/s but holds only 20 MB; HBM is 1.5 TB/s and 40 GB. Blocks of K and V stream through SRAM, making attention 7.6× faster on GPT-2.
Three tricks make exact attention IO-aware
- 1
Tile the inputs
Split Q, K and V into blocks sized to fit in SRAM, then loop over them.
- 2
Online softmax
Keep a running max and running sum per row, rescaling partial outputs so tiles combine into the exact softmax.
- 3
Recompute, don't store
Skip saving the attention matrix for the backward pass. Recompute it on chip; extra FLOPs cost less than memory reads.
Online softmax merges tiles without revisiting them
Each block tracks its own max and exponent sum. Rescaling by emi−m combines blocks exactly, so softmax never needs the full row at once.
- mi
- Row maximum within block i, for numerical stability
- ℓi
- Sum of exponentials within block i
- emi−m
- Correction aligning each block to the shared max
Faster training at identical quality
GPT-2 small on OpenWebText, 8×A100. Perplexity is unchanged because attention stays exact.
Speed comes with an engineering bill
Custom CUDA per variant
Every new attention variant needs a hand-written kernel, far lower-level work than writing PyTorch.
Tuned per GPU generation
Block sizes depend on SRAM size, so kernels may not transfer cleanly across GPU architectures.
Single-GPU analysis
The IO-aware analysis covers one GPU; multi-GPU attention was left for future work.
Why it matters
- Count memory reads and writes, not just FLOPs, when optimizing GPU kernels.
- Exact attention with memory linear in sequence length unlocked 16K and 64K contexts.
- It now ships inside PyTorch's scaled dot-product attention, the default in most training stacks.
17 upvotes on Hugging Face 8 slides, 2 min
27 May 2022