What is FlashAttention?
FlashAttention is an exact attention algorithm that computes softmax(QKᵀ/√d)V in small tiles kept in on-chip GPU memory, so the full N×N score matrix is never written to slow off-chip memory. It gives the same output as standard attention. The gain comes from moving fewer bytes, not from doing less arithmetic.
Attention is the part of a transformer whose cost grows with the square of the sequence length, and on GPUs its standard form is limited by memory traffic. FlashAttention speeds up GPT-2 training about 3× at 1K tokens and cuts attention's extra memory from quadratic to linear in sequence length. Its kernels now sit behind PyTorch's scaled_dot_product_attention, Hugging Face Transformers, vLLM, and most LLM training stacks.
Key ideas
- Standard attention is limited by memory traffic, because it writes and reads the N×N score matrix through HBM several times.
- FlashAttention splits Q, K, and V into tiles that fit in on-chip SRAM and fuses all of attention into one kernel.
- Online softmax keeps a running max and sum per row, so softmax can be computed one tile at a time and rescaled at the end.
- HBM accesses drop from Θ(Nd + N²) to Θ(N²d²/M), and extra memory drops from O(N²) to O(N).
- FlashAttention is exact: it returns the same output as standard attention, apart from floating-point rounding.
- FlashAttention-2 improves parallelism and work partitioning, and FlashAttention-3 targets Hopper GPUs with asynchrony and FP8.
Common questions
Do I need to retrain a model to use FlashAttention?
No. FlashAttention computes the same function as standard attention, so existing weights work unchanged. Swapping the attention kernel is a runtime choice, often a single flag. Outputs can differ in the last bits because the additions happen in a different order.
Why is FlashAttention faster if it does more FLOPs?
On modern GPUs, standard attention spends most of its time waiting on memory, not doing arithmetic. FlashAttention recomputes the score matrix in the backward pass instead of storing it, which adds FLOPs but removes far more bytes of HBM traffic. Since memory was the bottleneck, total time goes down.
Does FlashAttention help during LLM decoding?
Prefill, where the whole prompt is processed at once, benefits the most because it builds large score matrices. During decode each step has one query per sequence, so the score matrix is a single row and the main cost is reading the KV cache. Variants such as Flash-Decoding split that read across many thread blocks to use the whole GPU.
Which GPUs support FlashAttention?
FlashAttention-2 runs on NVIDIA Ampere, Ada, and Hopper GPUs such as the A100, RTX 4090, and H100, and the original version also covers older Turing cards. FlashAttention-3 is written for Hopper. AMD GPUs have ROCm ports, and PyTorch falls back to other kernels where no FlashAttention kernel applies.
Related explainers
- How Self-Attention Works — Query, key, value — the core of self-attention
- KV Cache — Why LLM generation caches keys and values, and what it costs
- Softmax, Logits & Temperature — Turning raw scores into probabilities
- Multi-Head Attention — Parallel attention heads learning different patterns
- The Transformer Architecture — A visual guide to 'Attention Is All You Need'
- Quantization — Shrinking model weights to 8 and 4 bits
References
- Dao et al. (2022), "FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness"
- Dao (2023), "FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning"
- Shah et al. (2024), "FlashAttention-3: Fast and Accurate Attention with Asynchrony and Low-precision"
- Milakov and Gimelshein (2018), "Online normalizer calculation for softmax"
Last updated