FlashAttention

Definition

FlashAttention (Dao, Fu, Ermon, Rudra, Ré, 2022; arXiv:2205.14135) is an IO-aware, exact attention kernel that computes standard scaled-dot-product attention without ever materializing the score matrix in HBM. It fuses the whole matmul → softmax → matmul chain into one kernel by tiling into SRAM and using online softmax, collapsing HBM traffic from to . It is the canonical example of algorithm–hardware co-design.

How it works

Naive attention runs , , as separate kernels, round-tripping the matrix through HBM ~4× (write , read , write , read ) — traffic with tiny attached math, so arithmetic intensity collapses to , far below the ridge. The softmax is the smoking gun: an elementwise/reduction kernel that forces to be materialized. At one BF16 head’s scores are 128 MiB; at , 32 GiB — the capacity wall that capped context.

FlashAttention tiles: for each query block , stream over K/V blocks, forming each score tile only in SRAM, updating running softmax state via online softmax, and discarding . Only (in) and (out) cross HBM: , linear in . The paper’s bound is accesses ( = SRAM size) vs. — measured ~9× fewer HBM accesses, 2–4× wall-clock on GPT-2. The backward uses recomputation (store only per-row logsumexp , ; recompute on the fly), trading FLOPs for the memory.

The FA-1 → FA-4 lineage — same math, each generation retargeting the then-binding hardware constraint:

  • FA-1 (2205.14135): kill HBM traffic (IO-awareness). ~30–50% A100 peak.
  • FA-2 (2307.08691): kill idle warps/blocks — defer output rescaling, parallelize over seq-len, split Q (not K) across warps. ~2×, 50–73% A100.
  • FA-3 (2407.08608): Hopper async — warp-specialized TMA/wgmma producer-consumer overlap, ping-pong matmul/softmax, FP8. 1.5–2× over FA-2, ~740 TFLOP/s FP16, ~1.2 PFLOP/s FP8.
  • FA-4 (arXiv:2603.05451, 2026): Blackwell — software-emulated exp on FMA units (SFUs didn’t scale), conditional rescaling, TMEM for backward, 2-CTA MMA, CuTe-DSL. ~1.6 PFLOP/s BF16 (~71% util), 1.3× cuDNN.

Why it matters

Attention is where LLMs spend a growing fraction of time as context grows; FlashAttention removed the quadratic memory wall without approximation, making million-token context tractable and turning a memory-bound op near-compute-bound. It is exposed via F.scaled_dot_product_attention and backs PyTorch FlexAttention — the default attention kernel in modern training/inference.

Taught in

See also