Skip to main content
issue 2026-07-18Inference55 minNeurIPS 2022interactive

FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness

This research paper explains how tiling Q, K, and V through on-chip SRAM with an online softmax yields exact attention that is faster and far more memory-efficient than materializing the full N×N score matrix in HBM.

FlashAttention tiles Q, K, V through SRAM instead of writing N×N to HBMLeft: standard attention materializes a huge score matrix in slow HBM. Right: FlashAttention streams blocks through fast SRAM with online softmax and writes only O(Nd) outputs.STANDARDHBM HOLDS N×NSCORES IN HBMMEMORY BOUNDFLASHATTENTIONTILE · SOFTMAX ONLINE · WRITE OQ TILEK TILEV TILESRAMWORKING SETEXACT SOFTMAX · FEWER HBM ROUND-TRIPSIO-AWARE TILING BEATS MATERIALIZED ATTENTIONFlashAttention: SRAM tiles, not HBM N×NMobile summary of HBM-resident scores versus SRAM tiling.HBM vs SRAMSTANDARD · WRITE N×Nscores live in slow HBMFLASH · TILE IN SRAMonline softmax · exact attentionrecompute in backward to save IOFASTER · LESS MEMORY · STILL EXACT

On GPUs, exact attention is often slow not because of arithmetic intensity but because of memory traffic. The textbook algorithm materializes the full N×NN \times N score matrix (and usually the attention probabilities) in HBM. For long sequences that matrix dominates both memory and wall-clock time — even when the FLOP count looks fine on paper.

FlashAttention’s punchline: treat attention as an IO-aware algorithm. Tile QQ, KK, and VV through fast on-chip SRAM, fuse the softmax with the matmuls, and never write the full attention matrix to HBM. The result is still exact attention (same math as softmax attention), just scheduled for the memory hierarchy.

What this paper explains

Standard attention for sequences of length NN and head dimension dd costs O(N2d)O(N^{2}d) FLOPs but also O(N2)O(N^{2}) HBM reads/writes if you store S=QKS = QK^{\top} and P=softmax(S)P = \mathrm{softmax}(S). HBM bandwidth is the scarce resource; SRAM is small but much faster.

Dao et al. show you can:

  1. Load blocks of KK and VV (and streaming blocks of QQ) into SRAM.
  2. Compute partial attention scores and maintain running softmax statistics (online softmax / “safe” streaming normalization).
  3. Accumulate the output block in SRAM and write only the final O(Nd)O(Nd) outputs back to HBM.
  4. In the backward pass, recompute attention instead of storing the huge intermediate — trading FLOPs for IO, which is a win when IO dominates.
Tiling Q K V through SRAMBlocks of Q, K, and V stream through SRAM; only the output returns to HBM.HBM · Q,K,V,O↔ tiles ↔SRAMscores + stats→ write O onlyO
Online softmax keeps row statistics so partial tiles still yield exact attention outputs.

Prior limits

  • Materialized attention — simple to implement in frameworks, disastrous O(N2)O(N^{2}) memory at long context.
  • Approximate attention (sparse patterns, low-rank, kernelized) — can cut memory/FLOPs but change the function; quality and kernel fusion become research projects of their own.
  • Kernel fusion without tiling — helps launch overhead, but still fails if the algorithm insists on writing N×NN \times N temps.

The mechanism

Think in blocks. Choose block sizes so a tile of QQ, KK, VV fits in SRAM. For each query block, iterate key/value blocks:

  • Update local scores Sij=QiKj/dS_{ij} = Q_i K_j^{\top} / \sqrt{d}.
  • Update running row-max and normalizing denominators so the final softmax matches the global one.
  • Accumulate OiO_i \leftarrow weighted VjV_j contributions.

Because the algebra of softmax can be rewritten with running statistics, you never need all columns of a row resident at once — only the statistics that make the eventual normalization exact.

Interactive

Sequence length vs attention memory

Illustrative footprint for one head (d=64, fp16): full scores N×N vs storing outputs O(Nd) if scores never hit HBM.

FlashAttention still uses SRAM tiles and stats; the point is avoiding an HBM-resident N×N matrix as N grows.

Algorithm / figure walkthrough

  1. Partition Q,K,VQ, K, V into SRAM-fitting blocks.
  2. For each QQ block, initialize output accumulators and softmax stats to neutral values.
  3. Stream K,VK, V blocks; update stats + output in SRAM.
  4. Write the finished OO block to HBM; discard score tiles.
  5. Backward: reload inputs, recompute block attention as needed, accumulate gradients.

The opening figure contrasts an HBM-resident N×NN\times N canvas with a small SRAM working set that slides over Q/K/VQ/K/V tiles.

What to notice when reading

  • The paper’s analysis is about HBM accesses, not just FLOPs — count bytes moved.
  • Exactness comes from online softmax, not from dropping terms.
  • Wall-clock gains depend on sequence length and hardware; the IO model predicts when tiling wins.

Results and evidence

The paper reports end-to-end speedups and large memory savings versus standard attention on long sequences (their benchmarks show up to roughly 2–4× faster attention and 10–20× lower memory for attention at long NN, depending on setting). Lower memory enables longer context and larger batches; they demonstrate improved training throughput on language and long-document tasks relative to baseline attention implementations of the era.

Treat those ranges as the authors’ reported operating regime on the GPUs they used — not a universal constant for every model size.

Limitations

  • Implementation is hardware-sensitive; block sizes must fit SRAM and warp constraints.
  • Exact FlashAttention still has O(N2d)O(N^{2}d) FLOPs — it does not asymptotically beat attention’s compute, only its IO/memory profile.
  • Very short sequences may not benefit; overheads can dominate when NN is small.
  • Later variants (FlashAttention-2, etc.) refine parallelism further; this paper is the IO-aware foundation.

How to read the paper

  1. Abstract + §1 — HBM vs SRAM motivation.
  2. §2 — background on attention and IO complexity.
  3. §3 — tiling algorithm and online softmax.
  4. §4–5 — runtime/memory experiments and ablations.
  5. Appendix — numerical details / backward recomputation.

Knowledge check

What does FlashAttention refuse to write to HBM in the forward pass?

Is FlashAttention an approximate attention method?

Why can recomputing attention in the backward pass still be faster?

Keep reading

  1. Original paper (arXiv:2205.14135)
  2. Related Fanout Daily: Attention Is All You Need, GQA, PagedAttention.

Sources

  • Dao et al., FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness, NeurIPS 2022 — arXiv:2205.14135