Foundation reference

FlashAttention: IO-Aware Attention

FlashAttention computes exact attention faster and with memory that grows linearly in the sequence length; it trained GPT-2 with 1K-token sequences 3× faster and BERT-large 15% faster end to end than the baselines it compared against (Dao et al., 2022).

A newer notebook covers this concept. This older page keeps its summary, math and sources.

Concept 68 of 100EfficiencyStudy phase 6
Key equation
extra memory: O(N) instead of O(N2)\text{extra memory: } O(N) \text{ instead of } O(N^2)
Reading map and next steps

Study guide

Where to start

Concept 68 of 100FlashAttentionEfficiency · Study phase 6: Modern efficiency & inference
Question

Why does a GPU's memory hierarchy, a small fast on-chip SRAM next to a large but slower HBM, decide how fast attention runs?

How to study this pagePredict · Check · Generalize · Continue
PredictSay what you expect.

Before you use the demo in the newer notebook, say what you expect to change.

CheckCheck against the math and the papers.

Compare what the notebook demo shows with the key equation here and with the papers cited below.

GeneralizeName what stays the same.

Identify the part of the idea that survives a change of notation or model, so you can recognize it in another paper or system.

ContinueContinue in the newer notebook.

The newer notebook covers the same concept and has an interactive demo.

Why It Matters for Modern Models

  • FlashAttention computes exact attention faster and with memory that grows linearly in the sequence length; it trained GPT-2 with 1K-token sequences 3× faster and BERT-large 15% faster end to end than the baselines it compared against (Dao et al., 2022).
  • Because memory no longer grows as N², longer contexts became practical: FlashAttention gave the first Transformers to do better than chance on the Path-X benchmark (16K tokens), and its block-sparse version did the same on Path-256 (64K tokens) (Dao et al., 2022).
  • Published training recipes use memory-efficient exact attention of this kind; LLaMA, for example, used an implementation inspired by Rabe & Staats (2021) with the backward pass of Dao et al. (2022) (Touvron et al., 2023).

Questions to Think About

  • Why can an algorithm that performs more arithmetic, by recomputing attention in the backward pass, still be faster than one that stores and rereads the attention matrix?
  • How can softmax be computed in one pass over blocks by keeping a running maximum and a running sum, and why must earlier partial outputs be rescaled when the maximum changes?

Interactive Demo

The interactive demo for this concept is in the newer notebook.

Open the notebook demo

Core Math

If you prefer intuition first, try the demo in the newer notebook, then come back for the math.

Key Equation
extra memory: O(N) instead of O(N2)\text{extra memory: } O(N) \text{ instead of } O(N^2)

Standard attention over a sequence of NN tokens computes an N×NN \times N matrix of scores, writes it to the GPU's main memory (HBM), and reads it back for the softmax and the product with VV:

S=QK⊤d,P=softmax(S),O=PVS = \frac{QK^\top}{\sqrt{d}}, \qquad P = \text{softmax}(S), \qquad O = PV

Moving SS and PP in and out of HBM costs memory traffic that grows as N2N^2, and on modern GPUs most of the operations in a transformer are limited by such memory traffic rather than by arithmetic (Dao et al., 2022).

FlashAttention computes exactly the same output without ever storing SS or PP:

  • split QQ, KK and VV into blocks small enough to fit in the fast on-chip memory (SRAM);
  • for each block of queries, loop over the blocks of keys and values, computing scores and a partial output inside SRAM;
  • combine the partial results with the online softmax: keep a running maximum and a running sum of exponentials for each query, and rescale the partial output whenever the maximum increases (Milakov & Gimelshein, 2018).

In the backward pass it recomputes the blocks instead of reading a stored PP, so it performs more arithmetic than standard attention yet runs faster, because it moves far less data. The extra memory it needs grows as O(N)O(N) (the running statistics) instead of O(N2)O(N^2). Dao (2024) summarizes FlashAttention's speed-up as 2–4× over optimized baselines, and FlashAttention-2 is about twice as fast again.

Key Papers

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

Dao et al.2022NeurIPS
Read paper →

FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning

Dao2024ICLR
Read paper →

Online normalizer calculation for softmax

Milakov & Gimelshein2018arXiv
Read paper →

Self-attention Does Not Need O(n²) Memory

Rabe & Staats2021arXiv
Read paper →

Connections

Where to go next

A newer notebook covers FlashAttention, with an interactive demo.

Continue in the newer notebook