Flash Attention
By Tri Dao et al.
FlashAttention is an IO-aware exact attention algorithm that reorders and fuses the computation of transformer self-attention to minimize reads and writes to GPU high-bandwidth memory, delivering significant speedups and memory savings…
Definition
FlashAttention is an IO-aware exact attention algorithm that reorders and fuses the computation of transformer self-attention to minimize reads and writes to GPU high-bandwidth memory, delivering significant speedups and memory savings without changing the mathematical result.
Overview
Introduced by Tri Dao and collaborators in 2022, FlashAttention tackles a subtle but critical inefficiency in standard transformer attention implementations: the bottleneck is often not raw floating-point compute but memory bandwidth, specifically the repeated movement of large intermediate attention matrices between a GPU's fast on-chip SRAM and its much slower high-bandwidth memory (HBM). Naive attention implementations materialize the full n×n attention score matrix in HBM, incurring expensive read/write traffic at every layer. FlashAttention avoids this by using tiling: it splits the query, key, and value matrices into blocks small enough to fit in SRAM, computes partial attention outputs for each block using an online softmax technique that avoids ever storing the full attention matrix, and accumulates the final result incrementally. Because it is IO-aware rather than algorithmically approximate, FlashAttention produces numerically identical (up to floating point rounding) results to standard attention — it is a systems-level optimization, not a sparsity or approximation technique like Sparse Transformers or Linformer. The original paper reported 2-4x training speedups and dramatically reduced memory usage (from quadratic to linear in sequence length) compared to standard PyTorch attention implementations, which in turn enabled training and inference with much longer context windows. FlashAttention-2 (2023) improved GPU occupancy and parallelism further, and FlashAttention-3 (2024) added support for Hopper-generation GPU features like FP8 and asynchronous execution. FlashAttention has since become a near-universal default: it ships as a fused kernel in PyTorch's scaled_dot_product_attention, is integrated into Hugging Face Transformers, vLLM, and virtually every major open-weight and proprietary LLM training and inference stack. It's a foundational reason modern LLMs can be trained and served efficiently at context lengths of tens or hundreds of thousands of tokens.
Key Concepts
- Computes exact (not approximate) attention with no change to model outputs
- Uses tiling and online softmax to avoid materializing the full n×n attention matrix
- Reduces memory usage from quadratic to linear in sequence length
- Delivers 2-4x+ training/inference speedups versus naive attention implementations
- FlashAttention-2 improved GPU parallelism and work partitioning
- FlashAttention-3 adds Hopper GPU support including FP8 and warp-specialization
- Integrated as a default kernel in PyTorch, Hugging Face Transformers, and vLLM
- Open-source implementation widely adopted across the LLM training ecosystem