Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Explainers/Research Papers

FlashAttention explained: making AI attention much faster

FlashAttention computes exact attention in small blocks in fast GPU memory, cutting slow memory traffic. Training gets faster and models can read longer texts.

Derpx0624 Aug · 6 min read
Contents
The idea in one sentenceHow it worksStep 1: Two kinds of GPU memoryStep 2: What normal attention does wrongStep 3: TilingStep 4: Softmax in pieces (online softmax)Step 5: The backward pass and recomputationWhat the paper foundWhat to be careful aboutSources
0% read · 6 min leftBack to top ↑
On this page0%
Contents
The idea in one sentenceHow it worksStep 1: Two kinds of GPU memoryStep 2: What normal attention does wrongStep 3: TilingStep 4: Softmax in pieces (online softmax)Step 5: The backward pass and recomputationWhat the paper foundWhat to be careful aboutSources

Attention is the step that lets every word in a text look at every other word. Doubling the text makes the work four times larger, so many papers tried to cut the arithmetic. Most of them did not make real programs faster. This paper found why: the slow part on a GPU (a chip made for many small calculations at once) is moving data around in memory, not the arithmetic. Fixing that made training faster and let models read longer texts.

Paper
TitleFlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness
AuthorsTri Dao, Daniel Y. Fu, Stefano Ermon, Atri Rudra, Christopher Ré
Year2022
VenuearXiv 2205.14135 (the paper is widely cited as a NeurIPS 2022 paper; the arXiv version does not state a venue)
Linkarxiv.org/abs/2205.14135

The idea in one sentence

Compute attention in small blocks that fit in the GPU's fast memory, so the huge table of scores is never written to slow memory.

The answer is exact: it is the same result as normal attention, not an approximation. Only the order of the steps and where the numbers are stored change.

How it works

Step 1: Two kinds of GPU memory

A GPU has a big, slower memory called HBM (high bandwidth memory) and a tiny, much faster memory on the chip called SRAM. The paper's figures for an A100 GPU:

MemorySizeSpeed
SRAM (on chip)about 20 MB in total (192 KB for each of 108 processors)about 19 TB/s
HBM40 GB1.5 TB/s

SRAM is about ten times faster and about two thousand times smaller. Every GPU operation loads data from HBM, calculates, and writes the result back to HBM. An operation is memory-bound when its time is set by these trips, not by the arithmetic. The paper says operations such as softmax and masking are memory-bound.

Step 2: What normal attention does wrong

With NNN words and head size ddd, attention computes three things:

S=QK⊤,P=softmax(S),O=PVS = QK^{\top},\qquad P=\mathrm{softmax}(S),\qquad O=PVS=QK⊤,P=softmax(S),O=PV

QQQ, KKK and VVV are the query, key and value tables. SSS is a table of N×NN\times NN×N scores, and PPP is the same table after softmax (a step that turns each row of scores into weights that add up to 1). Standard code writes SSS to HBM, reads it back, writes PPP to HBM, and reads it back again. For N=1024N=1024N=1024 that is over a million entries per head, per layer, going back and forth.

Step 3: Tiling

Tiling means cutting the work into blocks that fit in SRAM. FlashAttention splits QQQ, KKK and VVV into blocks. It loads a block of keys and values, then loops over blocks of queries. For each pair of blocks it computes a small piece of SSS in SRAM, uses it, and discards it. Only the final output goes back to HBM.

HBM: Q, K, V
SRAM: block of K and V
SRAM: block of Q
SRAM: small piece of S
HBM: output O

Copy one block of keys and values to SRAM

Step 1 of 4

The paper shows this needs Θ(N2d2/M)\Theta(N^2d^2/M)Θ(N2d2/M) HBM accesses, where MMM is the SRAM size, against Θ(Nd+N2)\Theta(Nd+N^2)Θ(Nd+N2) for standard attention.

Step 4: Softmax in pieces (online softmax)

Tiling has one difficulty. Softmax needs a whole row of scores at once, but a block holds only part of a row. The fix is to keep two numbers per row: the largest score so far mmm and the running sum ℓ\ellℓ of ex−me^{x-m}ex−m. (Subtracting the largest score keeps the numbers from overflowing.) When a new block arrives, rescale the old sum to the new largest score and add:

mnew=max⁡(m,m′),ℓnew=em−mnew ℓ+em′−mnew ℓ′m_{new}=\max(m,m'),\qquad \ell_{new}=e^{m-m_{new}}\,\ell+e^{m'-m_{new}}\,\ell'mnew​=max(m,m′),ℓnew​=em−mnew​ℓ+em′−mnew​ℓ′
Toy example (our own numbers, not from the paper)

One row of scores is x=(1,3,2,5)x=(1,3,2,5)x=(1,3,2,5), split into blocks (1,3)(1,3)(1,3) and (2,5)(2,5)(2,5).

Block 1: m=3m=3m=3, ℓ=e−2+e0=1.135\ell=e^{-2}+e^{0}=1.135ℓ=e−2+e0=1.135. Block 2: m′=5m'=5m′=5, ℓ′=e−3+e0=1.050\ell'=e^{-3}+e^{0}=1.050ℓ′=e−3+e0=1.050. Combine: mnew=5m_{new}=5mnew​=5, ℓnew=e3−5⋅1.135+e0⋅1.050=1.203\ell_{new}=e^{3-5}\cdot1.135+e^{0}\cdot1.050=1.203ℓnew​=e3−5⋅1.135+e0⋅1.050=1.203.

Weights ex−5/1.203e^{x-5}/1.203ex−5/1.203 are (0.015, 0.112, 0.041, 0.831)(0.015,\ 0.112,\ 0.041,\ 0.831)(0.015, 0.112, 0.041, 0.831). Computing softmax on the whole row at once gives exactly the same four numbers. The output OOO is updated the same way, with its old part rescaled each time a new block comes in.

This is why the method is exact: the rescaling is algebra, not an estimate.

Step 5: The backward pass and recomputation

Training also needs a backward pass, which uses SSS and PPP to work out how to adjust the model. Saving them would bring back the memory problem. So FlashAttention saves only the output OOO and the two statistics (m,ℓ)(m,\ell)(m,ℓ) per row, then recomputes SSS and PPP block by block inside SRAM. This costs extra arithmetic, but it saves slow HBM trips. The paper notes this is like gradient checkpointing, except that it also gets faster rather than only saving memory.

What the paper found

Measured for GPT-2 medium on an A100 (sequence length 1024, forward and backward, Figure 2 of the paper):

Standard attention40.3 GB of HBM traffic
FlashAttention4.4 GB of HBM traffic
HBM reads and writes, from the paper.
StandardFlashAttention
Arithmetic (GFLOPs)66.675.2
HBM reads and writes (GB)40.34.4
Runtime (ms)41.77.3

FlashAttention does more arithmetic and is still about 5.7 times faster, because HBM traffic was the bottleneck.

Training speed (baseline in brackets):

  • BERT-large, length 512: 15% faster than the MLPerf 1.1 speed record.
  • GPT-2 small, length 1K, 8 A100 GPUs: 2.7 days, against 9.5 days for HuggingFace and 4.7 days for Megatron-LM. Perplexity is the same, 18.2.
  • Long-range arena, length 1K to 4K: 2.4 times faster than standard attention.

Longer context, GPT-2 small:

ImplementationContextPerplexityTraining time
Megatron-LM1K18.24.7 days
FlashAttention1K18.22.7 days
FlashAttention4K17.53.6 days

Perplexity measures how surprised the model is by new text, so lower is better. The 4K model is 0.7 better and still faster than Megatron-LM at 1K. Longer documents also helped classification (up to 8.5 points on ECtHR and 4.3 on MIMIC-III compared with length 512).

On Path-X (sequence length 16K) FlashAttention reached 61.4% accuracy, and the block-sparse version reached 63.1% on Path-256 (length 64K). Earlier Transformers in that table did no better than chance there. Block-sparse FlashAttention is the paper's approximate variant, which skips some blocks.

These results show a real speed-up for exact attention on the A100 GPU in these setups. They do not show the same factors on other chips or models.

What to be careful about

  • Fewer calculations does not mean faster. The paper's point is that the number of operations can mislead, because memory trips can dominate.
  • Exact, but only for dense attention. The 63.1% Path-256 result uses the block-sparse approximation, not the exact version.
  • It needs hand-written GPU code. The authors list this as a limitation: each new attention variant needs a new CUDA kernel, and the code may not move easily to other GPUs.
  • Not every number is a big gain. For example, in the paper's long-range arena table, some approximate methods such as Linformer were slightly faster (2.5 times against 2.4 times) but less accurate.
Key takeaways
  • On a GPU, moving data between slow HBM and fast SRAM often costs more than the arithmetic.
  • FlashAttention works in blocks inside SRAM and never writes the full score table to HBM.
  • A running maximum and sum make blockwise softmax give the exact same answer.
  • It recomputes in the backward pass: more arithmetic, less memory traffic, and still faster.

Sources

  • Dao, Fu, Ermon, Rudra, Ré, FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness, 2022 (read in full from the arXiv PDF, version 2).
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
Research Papers · 6 minVL-JEPA explained: an AI that predicts meaning, not pixelsResearch Papers · 5 minMamba explained: an alternative to TransformersResearch Papers · 6 minLoRA explained: fine-tune a big AI model cheaplyResearch Papers · 7 minChinchilla explained: how big should an AI model be?
Start here
01Why do AI chatbots make things up?02How does Raft work? Getting computers to agree03Mamba explained: an alternative to Transformers