History Problem Core Idea Memory Hierarchy Tiling Results Impact Deep Dive Quiz
Interactive Paper Explainer

Attention That Respects the Hardware
FlashAttention

A visual, step-by-step guide to the algorithm that made long-context Transformers practical — exact attention, 2-4× faster, by refusing to waste trips between GPU memory levels.

Start Learning Read the Paper ↗
3×
GPT-2 Speedup (1K)
Exact
Not Approximate
15%
BERT-large vs MLPerf
2022
Year Published
History

From FLOPs to Bytes

Everyone was counting arithmetic operations. FlashAttention counted memory trips — and won.

2014–17
The quadratic era begins
Self-attention's O(N²) compute and memory become the known cost of Transformers.
2019–21
Approximate attention boom
Sparse, linear, low-rank, kernel methods — hundreds of papers reducing FLOPs, often with no wall-clock speedup and quality loss.
2022 · May
🚀 FlashAttention (Dao, Fu, Ermon, Rudra, Ré)
IO-aware exact attention: tile the computation, never materialize the N×N matrix, stream through SRAM — fewer HBM accesses by construction.
2023 →
The default kernel
FlashAttention-2, fused kernels everywhere, multi-GPU attention — long context stops being a luxury. Every serious stack inherits the idea.
The One-Sentence Insight

On a GPU, attention is memory-bound, not compute-bound: the time is dominated by moving data between the GPU's slow large memory (HBM) and its fast tiny memory (SRAM), not by arithmetic. Approximate attention attacked the wrong bottleneck. FlashAttention keeps the math exact and removes the memory traffic instead.

🧭 Study pairing
Start with Attention Is All You Need — this page is its systems sequel.
Chapter 01

The N×N Materialization

Standard attention computes an N×N matrix nobody needs to keep — and pays for it in the slowest memory on the chip.

💾
The Standard Recipe's IO Bill
  • Write S = QKᵀ (N×N) to HBM; read it back for softmax; write the normalized matrix; read again to multiply by V
  • Memory grows O(N²) — at 8K tokens the scores matrix alone is ~256× larger than the inputs
  • Every round trip crosses the slow HBM bottleneck; SRAM sits underused
  • Approximate attention cut FLOPs but often ran no faster — it was still HBM-bound
⚡
The FlashAttention Contract
  • Never materialize the N×N matrix anywhere
  • Load tiles of Q, K, V into SRAM; compute attention for a tile block completely; write only the output block
  • Softmax online: accumulate running statistics per row — exact, incremental, no second pass
  • Backward pass recomputes attention (cheap, it's in SRAM) instead of storing it
Analogy — The Kitchen Countertop

HBM is the walk-in pantry; SRAM is the countertop. Standard attention walks every ingredient to the countertop, then carries the half-finished dish back to the pantry, then returns to get it again — for every step of the recipe. FlashAttention rewrites the recipe so each countertop session completes a whole course: pantry trips: one in, one out.

Chapter 02

Three Ideas, One Kernel

Tiling, online softmax, and kernel fusion — each old individually, decisive together.

O = softmax(QKᵀ / √d) · V  ·  computed tile-by-tile with running (m, ℓ) statistics
Tiling
Block the loop
Q rows × K/V columns in blocks sized to SRAM; the N×N matrix never exists in full.
Online softmax
Exact, streaming
Track running max m and normalizer ℓ per row; rescale partial outputs as new tiles arrive — mathematically identical to the two-pass softmax.
Recomputation
Backward trick
Store only Q,K,V and output O (O(N)); recompute attention inside the backward kernel — memory drops from O(N²) to O(N).
IO-optimal
Proven
The paper analyzes IO complexity: fewer HBM accesses than standard attention, and optimal for a range of SRAM sizes.
Interactive Demo — Memory Trip Counter

Standard vs Flash attention: watch HBM round trips per forward pass. Press Run.

Chapter 03

The Two-Level World

The physics the algorithm respects: big-and-slow HBM, small-and-fast SRAM.

GPU Memory Ladder (A100-class)
LevelSizeBandwidthRole
HBM (main GPU memory)~40–80 GB~1.5–2 TB/sStores weights, activations, KV cache
SRAM (on-chip)~20 MB~19 TB/sTiling workspace — ~10× the bandwidth
CPU RAM100s of GB~100s GB/sOff-chip overflow (not in this story)

An algorithm that keeps its working set in SRAM can be arithmetic-bound and fast; one that spills to HBM is memory-bound regardless of its FLOP count.

Why Approximations Stalled

Sparse/linear attention reduced arithmetic — but their scattered memory patterns still strided through HBM. Many ran slower than the dense baseline they replaced while also losing quality. The paper's framing — "make attention IO-aware" — reframed the whole subfield: fix the data movement first, approximate only if you still need to.

Interactive Demo — Bandwidth Bottleneck Simulator
Chapter 04

Tiling in Motion

Watch one output tile assemble itself as K/V blocks stream through SRAM, with the running softmax rescaling everything.

Interactive Demo — Online Softmax Assembly

One row-block of Q, four blocks of K/V. Advance to see partial outputs merge into the exact final result.

The Rescaling Trick, Intuited

Softmax needs every score before it can normalize anything — normally forcing the full row into memory. The online version keeps a running max m and sum ℓ: when a new block arrives with a bigger max, earlier partial outputs are rescaled down (cheap, in SRAM). At the end: bit-for-bit the same math, zero global passes.

Block-Sparse Extension

Once attention is tiled, masking whole blocks is one line: skip loading them. Block-sparse FlashAttention combines exact tiling with approximate sparsity — the paper's long-context results (16K–64K) come from this extension, achieving quality above dense baselines at lower cost.

Chapter 05

Speed and Quality

The results table that ended the approximate-attention debate for training workloads.

Training Speedups (end-to-end wall clock)
WorkloadSpeedupReference
BERT-large (512 seq)15%vs MLPerf 1.1 training speed record
GPT-2 (1K seq)3×end-to-end vs HuggingFace baseline
Long-range arena (1K–4K)2.4×vs standard attention

Exact outputs — speedups come purely from IO behavior, with zero change to model math.

MEMORY
O(N)
activations for backward — the N×N matrix is never stored
LONGER CONTEXT
16K–64K
block-sparse variant reaches Path-X 61.4% / Path-256 63.1% — first better-than-chance Transformers
QUALITY
+0.7 ppl
GPT-2 perplexity improves with longer context enabled by block-sparse FlashAttention
SCALING
→ Flash-2/3
parallelism over sequence length and better occupancy drove further 2× in v2
Interactive Demo — Sequence Length Wall

Standard attention hits the memory wall as N grows; Flash stays in the race. Press play.

Legacy

Impact — The Substrate of Modern AI

FlashAttention stopped being a paper and became plumbing: the layer every long-context system stands on.

🧱 Default in every stack
PyTorch, JAX, HF Transformers, vLLM — fused attention kernels are assumed infrastructure now.
📏 The long-context era
32K→128K→1M windows became economically feasible — see PagedAttention for the serving side.
🔁 Flash-2 / Flash-3 lineage
v2 doubled speed with work partitioning; v3 targets Hopper features (TMA, WGMMA, incoherent masking).
🎓 The systems-ML field
Proved "a systems paper can change a modeling field" — IO-aware thinking spread to convolutions, norms, and optimizers.
🚫 Approximation reset
Long-sequence research pivoted: approximate methods now justify themselves against exact-but-fast baselines.
⚠️ What it did NOT solve
O(N²) compute remains (exactness kept); the KV cache at inference is a separate problem — PagedAttention's beat.
Deep Dive

Exactness as a Design Choice

The deepest lesson for system design: before approximating, check whether your bottleneck is real.

📉
The Approximate Mirage
  • Years of FLOP-counting papers that didn't run faster on real GPUs
  • Quality loss bought zero wall-clock gain in many cases
  • The community's mental model ("attention is quadratic") conflated arithmetic with latency
  • Entire subfields optimized a term that wasn't dominant
🔬
The Measurement Discipline
  • Profile first: HBM traffic dominated; SRAM sat idle
  • Reformulate the computation to fit the memory hierarchy — keep the math identical
  • Prove IO-optimality: the traffic bound isn't beatable for given SRAM sizes
  • Then — and only then — consider sparsity (block-sparse) as a multiplier
Interactive Demo — Decide the Bottleneck

Two attention workloads: FLOP-heavy but SRAM-resident vs FLOP-light but HBM-strided. Toggle and see which wins.

Verdict

FlashAttention's headline numbers are speedups; its real contribution is an epistemic correction. "Attention is quadratic" was a statement about arithmetic — the field acted on it as a statement about time. One careful IO analysis later, exact attention runs 2-4× faster and long context is plumbing. The lesson generalizes far past attention: measure the machine before modeling the math.

Test Yourself

Quick Quiz

Check your understanding of the key concepts from the FlashAttention paper.

Reference

Key Takeaways

Everything you need to remember about this paper.

✅ Attention on GPUs is memory (IO) bound — not compute bound. Count HBM trips, not FLOPs.
✅ Tiling + online softmax: compute attention block-by-block in SRAM; never materialize the N×N matrix.
✅ Exact outputs — same math as standard attention, provably fewer HBM accesses (IO-optimal for given SRAM sizes).
✅ Backward via recomputation: store O(N) activations, recompute attention in-kernel — memory drops from O(N²) to O(N).
✅ 15% BERT-large (vs MLPerf record), 3× GPT-2 (1K), 2.4× long-range arena; block-sparse enables 16K–64K contexts.
✅ Became the substrate of every modern stack — Flash-2/3, fused kernels, the entire long-context era.