What is FlashAttention?
FlashAttention is an attention algorithm that splits the computation into small blocks done in the GPU's fast on-chip memory, cutting reads and writes to the GPU's slower main memory (HBM) while computing the exact result rather than an approximation.
Its relation to the attention mechanism is that of function to implementation: the mechanism defines what the model computes, while FlashAttention performs the same computation with less memory traffic. PagedAttention solves a different problem, splitting the KV cache into pages to reduce memory fragmentation.
- Topic
- Inference and performance
- Also known as
- Flash attention
- Origin
- 2022, Dao et al.
How FlashAttention works
- Query, key and value matrices are split into blocks moved into the GPU's fast on-chip memory (SRAM).
- Attention scores and softmax are computed per block, with softmax totals updated across blocks.
- The full attention matrix is never built in main memory; only the result and small intermediate statistics are written.
- In training the attention matrix is not stored for backpropagation; blocks are recomputed when needed.
In more detail
Online softmax and recomputation in the backward pass
FlashAttention relies on online softmax rescaling: as each key-value block is loaded into SRAM, running maximums and normalisation denominators are updated incrementally without materialising the full N×N score matrix in HBM. In training, only the final output and log-sum-exp statistics are saved and attention blocks are recomputed during the backward pass.
FlashAttention-2, FlashAttention-3 and the Blackwell generation
FlashAttention-3 (2024) overlaps tensor-core matrix multiplies with asynchronous memory transfers (TMA/WGMMA) on Hopper GPUs and introduces block-wise FP8 support with incoherent processing to keep quantisation error low. FlashAttention-4 extends these IO-aware kernels to Blackwell architectures.
Linear memory versus quadratic compute
While FlashAttention shrinks attention activation memory from O(N^2) to O(N), total compute FLOPs remain O(N^2) (slightly higher during training due to backward recomputation). Therefore, at very long context lengths (e.g. 128K, 1M tokens), prefill latency still grows quadratically unless paired with ring/sequence parallelism or sparse/sliding-window patterns.
Commonly confused terms
| Term | How it differs |
|---|---|
| FlashDecoding | FlashAttention computes exact attention by tiling over Q, K and V (effective for training and long-prompt prefill), whereas FlashDecoding splits along the KV sequence length to parallelise single-token autoregressive decoding when batch size is small. |
| Linear or approximate attention | Linear attention / state-space mechanisms modify the mathematical formulation to reduce compute FLOPs from O(N^2) to O(N), whereas FlashAttention keeps the exact mathematical attention formula with O(N^2) FLOPs and only cuts HBM memory footprint from O(N^2) to O(N) while reducing HBM IO through tiling. |
Why FlashAttention matters for on-premises AI
In standard attention the intermediate matrix needs memory growing with the square of sequence length; FlashAttention never stores it, so extra memory stays linear. The same GPU can then handle longer contexts and larger batches, getting more from hardware in in-house systems working with long documents. Results match standard attention mathematically, with only tiny floating-point differences from operation order. Supported GPU generations and version compatibility are checked before installation.
A software company runs its in-house code assistant on an inference engine with FlashAttention; processing long source files in one prompt leaves room for more concurrent requests on the same GPU.
Common questions
Does FlashAttention change the result?
No, it gives the same result as standard attention mathematically. Only tiny floating-point differences from operation order may appear; it is not an approximation.
How does FlashAttention differ from PagedAttention?
FlashAttention cuts memory traffic in attention computation. PagedAttention splits the KV cache into pages to reduce memory waste; the two can be used together.
Related terms
- Attention mechanism
- Self-attention
- PagedAttention
- Key-value cache
- GPU memory
- FlashDecoding
- Long-context model
Sources
- Dao et al., FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness, 2022
- Dao, FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning, 2023
In the glossary: Glossary, D to L