FlashAttention
FlashAttention is an I/O-aware algorithm that computes exact attention without materializing the full attention matrix, reducing memory usage from O(n^2) to O(n) and improving speed 2-10x on GPUs.
FlashAttention is a groundbreaking algorithmic reformulation of the standard attention computation, designed to exploit the memory hierarchy of modern GPUs. Standard attention materializes the full N x N attention matrix in HBM (high-bandwidth memory), which is both memory-prohibitive for long sequences and bandwidth-bound. FlashAttention works by tiling the Q, K, and V matrices across GPU on-chip SRAM (shared memory), computing attention in small blocks, and recombining results without ever writing the full softmax matrix to HBM. It uses two key tricks: (1) recomputation of attention during backward pass to avoid storing intermediate values (a form of gradient checkpointing), and (2) on-the-fly softmax normalization using a running rescaling technique. The result is a 2-10x wall-clock speedup for attention, and memory savings that make it practical to handle context windows of 128K+ tokens on a single A100. Two variants exist: FlashAttention-1 (forward only) and FlashAttention-2 (improved backward pass, better threadblock scheduling). FlashAttention-3 adds FP8 support and asynchronous processing on Hopper GPUs. For AI builders, integrating FlashAttention into training or inference pipelines is nearly drop-in via libraries like xformers, PyTorch's scaled_dot_product_attention, or Hugging Face's Transformers. It is especially beneficial for long-document RAG, code generation with large contexts, and multi-turn chat with extensive history. Note that FlashAttention requires Ampere or newer GPUs (compute capability 8.0+), and may show small numerical differences due to block-wise accumulation order.
FlashAttention does not approximate or sparsify attention-it computes the exact same mathematical result as standard attention, just more efficiently.
Related terms
Concepts you'll usually encounter alongside this one.
Attention is a mechanism that computes weighted sums of input tokens, allowing the model to focus on the most relevant parts of the sequence when generating each output.
The context window defines the maximum number of input tokens a transformer model can process at once, determining the length of conversation history or document it can reason over.
The KV cache stores intermediate key and value tensors from previous tokens during autoregressive generation, enabling subquadratic computation by avoiding redundant re-encoding of the full context.
Used in benchmarks
Workloads on MyAI Bench that involve FlashAttention.
Mentioned in tutorials
Hands-on guides that put this concept to work.
Stand up a fully functional local LLM stack on Ubuntu 22.04, NVIDIA drivers, CUDA, Ollama, and your first model, in under 30 minutes.
Run local LLMs on Windows 11 the right way, WSL2, the WSL CUDA toolkit, and Ollama with full GPU acceleration. Native installer covered too.
Apple Silicon is shockingly good at LLM inference. Get Ollama running with unified memory acceleration on any M-series Mac in 15 minutes.
DeepSeek's reasoning model rivals GPT-4 on math and code. Run the distilled 7B/14B/32B variants on consumer hardware in 30 minutes.
See how this term affects your memory budget.
Real numbers, real chips, every workload.
Picks for builders, by use case.
Stay Ahead of the AI Curve
Get weekly AI hardware news, benchmark updates, and deals in your inbox. Founding-subscriber list, be one of the first.