Robots Atlas>ROBOTS ATLAS
Inference

FlashAttention

2022ActivePublished: 29 May 2026Updated: 29 May 2026Published
Key innovation
IO-aware algorithm for computing exact self-attention that minimizes transfers between GPU HBM and SRAM via tiling and online softmax — speeds up attention 2-4× and reduces memory from O(n²) to O(n) without approximation.
Category
Inference
Abstraction level
Pattern
Operation level
Architecture blockInference
Use cases
Training Transformers with long context (32k+, 128k+)LLM inference (prefill speedup, lower memory)ViT and vision models (BERT, ViT, Stable Diffusion)Default attention implementation in PyTorch (scaled_dot_product_attention)Training models where full attention previously ran out of memory

How it works

The algorithm splits Q, K, V matrices into blocks of size B_r × d and B_c × d that fit in SRAM (typically 100-200 kB per SM). For each Q block it loads it once, then iterates over K and V blocks, computing partial attention results and accumulating them with numerically stable online softmax: maintaining current max m and sum l, at each new (K_j, V_j) pair updating O ← rescale(O_prev, m_old, m_new) + exp(S_new - m_new) · V_j. The n×n attention matrix is never materialized in HBM. The backward pass uses recomputation instead of saved attention matrix (gradient checkpointing).

Problem solved

The standard attention implementation materializes the n×n matrix in HBM and is memory-bound — the dominant cost is not softmax FLOPs but data transfer. This limits maximum context length and throughput.

Components

Q, K, V tiling

Splitting Q, K, V matrices into blocks fitting in GPU SRAM (typically B_r × d ~ 64-128 × 64-128).

Online softmax

Numerically stable recurrence maintaining running max and exponential sum — allows computing softmax block-wise without materializing the full matrix.

Backward-pass recomputation

Backward doesn't save the attention matrix, recomputing it from saved O, L (logsumexp) — FLOPs vs memory trade-off.

Implementation

Implementation pitfalls
Version vs hardware mismatchMedium

FlashAttention-3 requires Hopper (H100/H200) — doesn't work on Ampere (A100). v2 is standard on A100. Wrong version = loss of 2-4× speedup.

Fix:Runtime GPU architecture detection; use v2 for A100, v3 for H100. PyTorch sdpa selects automatically.
Limited support for custom attentionLow

FlashAttention assumes standard scaled-dot-product attention with optional causal mask. Non-standard masks (e.g., ALiBi, block-sparse) require special variants or prevent its use.

Fix:Check support for the bias/mask in use. FlexAttention (PyTorch) offers greater flexibility.

Evolution

Original paper · 2022 · NeurIPS 2022 · Tri Dao
FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness
Tri Dao, Daniel Y. Fu, Stefano Ermon, Atri Rudra, Christopher Ré
2022
FlashAttention v1 (Dao et al.)
Inflection point

First publication — tiling + online softmax, 2-4× speedup, O(n) memory for exact attention.

2023
FlashAttention-2 (Dao)
Inflection point

Better work partitioning across GPU warps, parallelism along sequence dimension — 2× faster than v1, ~50-70% peak FLOPs on A100.

2023
Integration in PyTorch 2.0 (scaled_dot_product_attention)

PyTorch adds FlashAttention as default backend for F.scaled_dot_product_attention — mass adoption across the ecosystem.

2024
FlashAttention-3 (Shah et al.)
Inflection point

Hopper (H100) support: asynchronous TMA, warp-specialization, FP8 — up to 75% peak FLOPs on H100, 2× faster than v2.

Computational complexity

Compute bottleneck

HBM-to-SRAM bandwidth

FlashAttention shifts attention from memory-bound toward compute-bound by maximizing data reuse in SRAM. The HBM bandwidth limit remains for loading Q/K/V blocks.

Depends on
SRAM size per SMHBM bandwidth

Execution paradigm

Primary mode
Dense

Exact attention — all token-token pairs are computed (modulo causal mask). A purely algorithmic optimization, no change to model mathematics.

Activation pattern
All paths active

Parallelism

Parallelism level
Fully parallel
Scope
TrainingInferenceAcross layers

Hardware requirements

Primary
Limited