Skip to content
DAH

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
Why other work relies on it

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.

  1. 1Attention table grows with length squared
  2. 2Writing it to memory is the slow part
  3. 3Process in blocks with a running softmax
  4. 4Recompute pieces instead of storing them
  5. 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 nn tokens the model forms three n×dn \times d matrices QQ, KK, VV (queries, keys, values; dd is typically 64 to 128), computes the score matrix S=QK⊤S = QK^\top of size n×nn \times n, turns each row into weights with softmax, softmax(s)j=esj∑kesk,\mathrm{softmax}(s)_j = \frac{e^{s_j}}{\sum_k e^{s_k}}, (positive weights that add to 1, the largest score getting the largest weight), and multiplies the weights by VV.

What goes wrong. SS has n2n^2 entries: for n=8192n = 8192 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 n2n^2.

The trick. FlashAttention (Dao et al., 2022) never stores SS. It splits QQ into blocks of rows and KK, VV into blocks, and for each QQ block streams through the KK, VV 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 mm and a running sum ℓ\ell. When a new block raises the maximum from moldm_{old} to mnewm_{new}, multiply the old sum and the old partial output by emold−mnewe^{m_{old}-m_{new}} and carry on. Because ea−mnew=ea−mold emold−mnewe^{a-m_{new}} = e^{a-m_{old}}\,e^{m_{old}-m_{new}}, 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 SS for this, FlashAttention keeps only mm and ℓ\ell per row and recomputes score blocks on chip, trading extra arithmetic for much less memory traffic. Extra memory grows like nn, not n2n^2.

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 n2n^2; 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

  1. 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.
  2. 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)
Words on this page
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)
New to this? Read first

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.

  1. Start with
  2. Step 2
  3. Step 3
  4. Then

Open problems & questions

Progress timeline

  1. Oct 16, 2025
    Tawa reaches up to 96% of FlashAttention-3 (FP16)
    Related to: A Triton attention forward kernel within 5% of FlashAttention-3 on H100

    A Triton-based compiler with automatic warp specialisation reports near-parity with FlashAttention-3 for FP16 attention forward on H100, from a development branch.

    source
  2. Jul 1, 2024
    FlashAttention-3 for Hopper

    Asynchrony and FP8 support; up to 740 TFLOP/s FP16 on H100 per the abstract (arXiv, July 2024).

    source
  3. Jul 1, 2023
    FlashAttention-2

    Better parallelism and work partitioning, about 2x faster than version 1 per the authors (arXiv, July 2023).

    source
  4. May 1, 2022
    FlashAttention

    Introduces IO-aware tiled exact attention with recomputation in the backward pass (arXiv, May 2022).

    source