FDEInterviews logo
NeurIPS 2022

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

The 2022 Stanford kernel that made attention fast by counting memory trips, not math: exact attention computed in on-chip tiles, several times faster, with memory linear in sequence length.

Breakdown by FDE Interviews · 11 min read · Updated

About the original · 2022
AUTHORS
Dao, Fu, Ermon, Rudra, Ré
FROM
Stanford University, University at Buffalo
PUBLISHED
May 2022 · NeurIPS 2022
CITED BY
5,606Semantic Scholar · Oct 2026
IN THIS BREAKDOWN · 10 SECTIONS

You can read 24% of this breakdown.

01

The paper in one minute

Attention compares every token with every other token, so a sequence of N tokens produces an N × N matrix of scores. Standard implementations compute that matrix, write it to the GPU's main memory (HBM), read it back to apply softmax, write the result, and read it again to multiply by the values. Many papers tried to make attention cheaper by approximating it with fewer operations, but they rarely ran faster in wall-clock time.

Tri Dao, Daniel Fu, Stefano Ermon, Atri Rudra and Christopher Ré at Stanford and Buffalo identified why: on modern GPUs, attention is limited by memory traffic, not arithmetic. An A100 has about 20 MB of on-chip SRAM running at roughly 19 TB/s, and 40 to 80 GB of HBM at 1.5 to 2 TB/s. Writing and reading an N × N matrix through HBM is what takes the time.

FlashAttention computes exact attention without ever storing that matrix. It splits queries, keys and values into blocks that fit in SRAM, and for each block computes scores, applies softmax and multiplies by values on-chip, writing only the output. The trick that makes this possible is an online softmax: keep a running maximum and running sum per row, and rescale earlier partial results when a later block changes them, so the final answer is exactly the same. For training, the backward pass recomputes the scores from the stored inputs and these small statistics rather than reading a stored N × N matrix. Everything is fused into one CUDA kernel.

It does slightly more arithmetic and much less memory traffic. On GPT-2 medium attention, traffic fell from 40.3 GB to 4.4 GB and runtime from 41.7 ms to 7.3 ms. End to end, GPT-2 trained 3× faster than the Hugging Face implementation, BERT-large 15% faster than the MLPerf 1.1 record, and attention memory grew linearly with sequence length. A block-sparse version went further, to 64K tokens.

THE IDEA, VISUALLY
ATTENTION IS LIMITED BY MEMORY TRAFFIC, NOT MATH SRAM (on-chip) ~20 MB · ~19 TB/s HBM (GPU memory) 40–80 GB · 1.5–2 TB/s CPU DRAM >1 TB · 12.8 GB/s A100 memory hierarchy (Figure 1) standard: write S = QKᵀ (N × N) to HBM N × N scores flash: one Q-tile × K/V-tile at a time Softmax is computed block by block with a running max and sum (online softmax), so the result is exact. The backward pass recomputes scores instead of storing them. MORE MATH, FAR LESS TRAFFIC GPT-2 medium attention, forward + backward, A100 (Fig. 2) GFLOPs standard 66.6 flash 75.2 memory read/write (GB) standard 40.3 flash 4.4 runtime (ms) standard 41.7 flash 7.3 WHAT IT BOUGHT · BERT-large: 15% faster than the MLPerf 1.1 record · GPT-2: up to 3× faster than Hugging Face · memory linear in sequence length, up to 20× less · first Transformers above chance on Path-X (16K)
FlashAttention does a little more arithmetic and far less memory traffic. Keeping the attention tiles in fast on-chip memory, instead of writing an N × N matrix to GPU memory, is where the speed comes from.
Research notes: the problem, approach and results
40.3 → 4.4 GB

memory traffic for GPT-2 medium attention, standard vs FlashAttention

3×

faster GPT-2 training than the Hugging Face implementation, same model and perplexity

20×

less memory than standard exact attention, growing linearly with sequence length

THE PROBLEM

Attention's cost grows with the square of sequence length, and approximate attention methods that cut the math rarely ran faster in practice, because the real bottleneck was moving data in and out of GPU memory.

THE IDEA

Compute exact attention block by block in fast on-chip SRAM with an online softmax, fuse it into one kernel, never store the N × N matrix, and recompute it in the backward pass instead of reading it back.

THE RESULT

Up to 7.6× faster attention on GPT-2, 3× faster GPT-2 training than Hugging Face, 15% faster BERT than the MLPerf record, memory linear in length, and the first Transformers above chance on Path-X.

02

Why it matters to an FDE

FlashAttention and its successors are now inside nearly every serving and training stack, from PyTorch's built-in attention to vLLM and SGLang. You rarely call it directly, but it shapes what is possible and what to check.

It is why long context became affordable. Attention memory linear in sequence length is what allows contexts of tens or hundreds of thousands of tokens on one GPU. When a customer asks why a model supports long inputs, the answer includes this kernel.

Check it is actually on. Older model code, custom attention variants, unusual head sizes or older GPUs can silently fall back to standard attention. If long-context latency or memory looks wrong, confirm the fast kernel is being used.

Think in memory traffic, not FLOPs. The paper's lesson generalizes: on GPUs, many operations are bound by moving data. When profiling a slow model, measure memory bandwidth use, not just FLOPs; fusion and keeping data on-chip often matter more than reducing math.

Exact, not approximate. FlashAttention changes nothing about model outputs (up to floating-point rounding), which makes it a safe optimization to enable, unlike approximate attention methods that change model quality.

Hardware specifics matter. The paper notes kernels may not transfer across GPU architectures; later versions were tuned for newer GPUs. Performance claims depend on the GPU generation.

THE FULL BREAKDOWN

Understand the mechanism. Try it on a real problem.

Unlock this breakdown and 49 more papers.

Premium opens every paper in full, plus every answer, concept, course and guide on the site. One payment for six months of access.

6 months · One payment · No auto-renewal

Get Premium · ₹2,000 / $25
  • ◇1 architecture diagram
  • </>1 runnable code experiment
  • ▦2 results tables
  • ?5 interview questions with answers
  1. 03Key takeaways

    The IO-aware idea, the algorithm's pieces, the results, and the misreadings to avoid

    Free account
  2. 04How it works

    The tiled algorithm, online softmax in detail, recomputation and the IO analysis

    Premium
  3. 05The numbers that matter

    Training speedups, long-range results, memory scaling, and a discrepancy in the paper

    Premium
  4. 06Try it yourself

    Implement tiled online-softmax attention in NumPy, prove it exact, and count memory traffic

    Premium
  5. 07Where it breaks

    The limits the authors list, and what changed in later versions

    Premium
  6. 08Using it on a deployment

    Making sure fast attention is working for a customer's training or serving stack

    Premium
  7. 09Interview angles

    Five questions this paper prepares you for, with answer hints

    Premium
Back to the paper library →