FlashAttention: IO-aware exact attention
A tiled, fused attention kernel that never writes the full score matrix to GPU memory, using an online softmax; versions 1 to 3 track successive GPU generations.
On this page16 sections
Long-context training and serving in PyTorch, vLLM, Megatron-LM and most open LLM stacks call FlashAttention kernels; it set the template for fusing operators by their memory traffic.
A way to compute attention exactly while avoiding the huge intermediate table that slows ordinary methods.
- 1Attention table grows with length squared
- 2Writing it to memory is the slow part
- 3Process in blocks with a running softmax
- 4Recompute pieces instead of storing them
- 5Same answer, far less memory traffic
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 tokens the model forms three matrices , , (queries, keys, values; is typically 64 to 128), computes the score matrix of size , turns each row into weights with softmax, (positive weights that add to 1, the largest score getting the largest weight), and multiplies the weights by .
What goes wrong. has entries: for 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 .
The trick. FlashAttention (Dao et al., 2022) never stores . It splits into blocks of rows and , into blocks, and for each block streams through the , 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 and a running sum . When a new block raises the maximum from to , multiply the old sum and the old partial output by and carry on. Because , 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 for this, FlashAttention keeps only and per row and recomputes score blocks on chip, trading extra arithmetic for much less memory traffic. Extra memory grows like , not .
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 ; only memory traffic falls. Each new GPU generation needs a new hand-tuned kernel, and reimplementations in easier languages are measured against these.
Go deeper
The full technical text.
What it computes
Standard attention forms the score matrix S = QKᵀ (size n×n for sequence length n), applies softmax row by row, then multiplies by V. Writing S and the softmax output to HBM makes memory traffic grow with n², and for typical head dimensions the operation is memory-bound rather than compute-bound. FlashAttention (Dao et al., 2022) computes the same exact result without ever materialising S.
The two ideas
- Tiling with an online softmax. Q is split into row blocks and K, V into column blocks. For each Q block the kernel streams over K and V blocks, keeping a running row maximum and running normaliser so the softmax can be corrected incrementally (the online normaliser of Milakov and Gimelshein, 2018). Partial outputs are rescaled as new blocks arrive, so only the final output is written.
- Recomputation in the backward pass. Instead of storing S or the softmax matrix, the backward kernel stores only the per-row statistics and recomputes score tiles on chip. This trades extra FLOPs for far fewer bytes, a good deal in a memory-bound regime. Extra memory is linear rather than quadratic in sequence length.
Versions
- FlashAttention-2 (2023) reworked work partitioning: fewer non-matmul FLOPs, parallelism over sequence length as well as batch and heads, and better splitting of work between warps. The authors report about 2× speedup over version 1.
- FlashAttention-3 (2024) targets Hopper: producer and consumer warp specialisation with TMA, interleaving of block matmuls and softmax to overlap tensor-core and special-function-unit work, and FP8 with block quantisation and incoherent processing. The abstract reports 1.5–2.0× speedup over version 2 on H100, up to 740 TFLOP/s in FP16 (75% utilisation), close to 1.2 PFLOP/s in FP8, and 2.6× lower numerical error than a baseline FP8 attention. It notes that version 2 reached 35% utilisation on H100.
Variants that matter in practice
Causal masking skips fully masked tiles; grouped-query and multi-query attention share K/V across heads; sliding windows restrict the K range; the decode step with a short query and long cached K/V is split across thread blocks along the sequence (split-KV) to use the whole GPU. Customisable engines such as FlashInfer generate such variants from templates.
Where it stops
The approach is exact, so it does not change the O(n²) compute of full attention, only its memory traffic. Each new GPU generation (Blackwell added new tensor-memory and instruction features) needs a new kernel, and open reimplementations in Triton or other compilers are measured against it.
Words on this page (12)
- attention
- The step in a transformer where each token looks at other tokens to decide what matters.
- softmax
- A step that turns a list of scores into percentages that add up to 100, used inside attention.
- HBM (high-bandwidth memory)
- The large, fast memory stacked next to the GPU chip. It holds the model and data but is far slower than on-chip storage.
- shared memory
- A small, very fast scratchpad inside each GPU processor block that the program controls directly.
- tiling
- Cutting a big job into small blocks that fit in fast memory and reusing each block many times.
- tensor core
- A special unit inside the GPU that multiplies small blocks of numbers very quickly.
- Hopper
- NVIDIA GPU generation (H100) introduced in 2022.
- transformer
- The neural-network design behind modern chatbots.
- gradient
- The vector of derivatives of an error with respect to each adjustable number; it says which way to change each one to reduce the error.
- backpropagation
- The method that computes all gradients of a network at once by applying the chain rule backwards, layer by layer.
- roofline model
- A chart that bounds a kernel’s speed by the smaller of peak compute and memory bandwidth times work per byte.
- FP8
- Floating-point numbers stored in 8 bits, with a scale factor, used to double matrix throughput on recent GPUs.
New to this? Read first (3)
- FlashAttention-2 (Stanford CRFM blog)Plain-language account of why attention is memory-bound and how tiling helps.
- Making Deep Learning Go Brrrr From First Principles (Horace He)A readable tour of compute-bound versus memory-bound work on GPUs.
- Roofline model (Wikipedia)Short overview of the chart and how to read it.
Understanding map
map of the whole wiki →What to read before FlashAttention: IO-aware exact attention (left), and what builds on it: entries that depend on it and its open problems (right). Every box links to its page.
- Start with
- Step 2
- Step 3
- FlashAttention: IO-aware exact attentionYou are here
- Then
- Paged attention and KV-cache kernelsAlgorithm
- FP8 attention that keeps FlashAttention-3 throughput at BF16-level accuracyOpen problem · D40
- An open attention backward kernel within 10% of FlashAttention-3 for head dimension 256Open problem · D33
- A Triton attention forward kernel within 5% of FlashAttention-3 on H100Open problem · D20
Open problems & questions
Progress timeline
- Oct 16, 2025Tawa reaches up to 96% of FlashAttention-3 (FP16)Related to: A Triton attention forward kernel within 5% of FlashAttention-3 on H100source
A Triton-based compiler with automatic warp specialisation reports near-parity with FlashAttention-3 for FP16 attention forward on H100, from a development branch.
- Jul 1, 2024FlashAttention-3 for Hoppersource
Asynchrony and FP8 support; up to 740 TFLOP/s FP16 on H100 per the abstract (arXiv, July 2024).
- Jul 1, 2023FlashAttention-2source
Better parallelism and work partitioning, about 2x faster than version 1 per the authors (arXiv, July 2023).
- May 1, 2022FlashAttentionsource
Introduces IO-aware tiled exact attention with recomputation in the backward pass (arXiv, May 2022).