Paper 09~14 min readNeurIPS 2022worked math + runnable code
Paper Breakdown

FlashAttention,
explained.

Everyone knew attention got painfully slow on long inputs, and everyone assumed the problem was too much math. This 2022 paper showed the arithmetic was never the bottleneck — the GPU was spending almost all its time shuffling data between fast and slow memory. Fix the shuffling, and attention gets several times faster while computing the exact same answer.

Video breakdown
The animated walkthrough is in production.
Read the full breakdown below in the meantime ↓
01

The real bottleneck

Self-attention has every token compare itself to every other token, producing an N×N scores matrix. For a sequence of length N, that matrix grows with the square of N — at 8,000 tokens it's 64 million numbers. The standard implementation writes that whole matrix out to memory, reads it back to apply softmax, then reads it a third time to multiply by the values.

The paper's diagnosis: the GPU's arithmetic units were mostly idle, waiting on those reads and writes. Attention was memory-bound, not compute-bound. The field had been optimizing the wrong thing.

02

Fast memory, slow memory

A GPU has two very different kinds of memory. HBM is the big main memory — tens of gigabytes, but relatively slow. SRAM is tiny on-chip memory — only kilobytes to a few megabytes, but roughly an order of magnitude faster.

The GPU memory hierarchy
SRAM~20 MB, ~19 TB/s — tiny but blazing fast. Work here.
HBM~40 GB, ~1.5 TB/s — huge but ~10x slower. Touch as little as possible.

Standard attention parks that giant scores matrix in slow HBM and keeps trudging back to it. FlashAttention's entire strategy is to never put it there in the first place.

03

Tiling and the online softmax

Instead of computing the full matrix at once, FlashAttention breaks the work into tiles — small blocks of queries and keys that fit inside fast SRAM. It loads a block, computes its piece of attention entirely in SRAM, and moves on, never writing the intermediate scores back to HBM.

The tricky part is softmax, which normally needs to see a whole row at once to normalize it. FlashAttention uses an online softmax: it processes blocks one at a time and keeps a running total and running maximum, rescaling previous partial results as new blocks arrive — so it gets the exact normalized answer without ever holding the full row. All the attention operations are fused into a single pass over fast memory.

One query block walks the key/value blocks — only the amber tile is in SRAM
Q block stays put K·V ✓ K·V ✓ K·V now in SRAM next… carry (m, ℓ, O) forward — rescale them whenever a new block raises the max
04

The math, worked with real numbers

The rescaling trick is the whole paper, so let's do it by hand. Softmax is computed "safely" by subtracting the row's max m before exponentiating (so nothing overflows), then dividing by the sum :

softmax(si) = esim /    where  m = maxi(si),   = Σi esim

Both m and ℓ look like they need the whole row. They don't. Take a row of scores s = [2, 1, 3, 0] and process it in two blocks — [2, 1] first, then [3, 0]:

StepRunning max mRunning sum ℓWhat happened
Block [2, 1]2e0 + e−1 = 1.3679Everything measured against max-so-far = 2
Block [3, 0] arrives3New max! The old sum used the wrong max
Rescale old ℓ31.3679 × e2−3 = 0.5033One multiply fixes every earlier term at once
Absorb new block30.5033 + e0 + e−3 = 1.5530Exactly what the full row gives: e−1+e−2+e0+e−3 = 1.5530 ✓

Why does one multiply fix everything? Every old term was es−2 but should have been es−3 — and es−3 = es−2·e2−3. The correction factor is the same for every term, so it can be applied to their sum. That's the update rule the kernel runs per block:

mnew = max(mold, mblock)
new = old · emoldmnew + Σi∈block esimnew
Onew = Oold · emoldmnew + Σi∈block esimnew · Vi

The third line is the punchline: the output accumulator O (the weighted sum of values built so far) is corrected by the same factor. Final answer = O / ℓ. Finishing the example: with m = 3 and ℓ = 1.5530, the weights come out [0.2369, 0.0871, 0.6439, 0.0321] — identical to computing softmax on the whole row at once. No approximation anywhere; the N×N matrix simply never existed.

05

Exact, not approximate

This is what set FlashAttention apart. Earlier attempts to speed up attention approximated it — sparse patterns, low-rank tricks — trading accuracy for speed. FlashAttention changes only the memory schedule. The result is bit-for-bit the same attention; you give up nothing.

Worth knowing

Because it never stores the N×N matrix, FlashAttention's memory grows only linearly with sequence length instead of quadratically. That's the door to long context windows — the reason models could suddenly handle tens of thousands of tokens.

RUN IT YOURSELF

Online softmax & tiled attention, for real

This is the paper's core algorithm in ~40 lines of plain Python, running in your browser through WebAssembly. naive_softmax needs the whole row; online_softmax streams it in blocks with the rescale trick; flash_attention extends the same trick to the full attention output. Run it and watch the tiled version match the standard one to 16 decimal places — then change the row, the block size, or Q/K/V and check it still does.

CPython · WebAssembly
06

What the paper actually measured

The headline numbers from the NeurIPS 2022 paper, not paraphrases:

BenchmarkAgainstResult
BERT-large training (seq 512)MLPerf 1.1 record15% faster end-to-end wall clock
GPT-2 training (seq 1K)HuggingFace / Megatron-LMup to 3× / ~1.7× faster end-to-end
Long Range Arenastandard attention2.4× speedup
Path-X (16,384 tokens)all prior Transformersfirst to beat chance (61.4%); block-sparse variant reached Path-256 at 64K tokens
Attention memoryquadratic baselinelinear in N — up to ~20× less at long lengths

And the structural comparison that explains why:

Writes full N×N matrix?Attention memoryAccuracy
Standard attentionYes — to slow HBMQuadratic in NExact
Sparse / low-rankNoLowerApproximate
FlashAttentionNoLinear in NExact
What it did not solve

FlashAttention makes attention IO-efficient, not sub-quadratic — the arithmetic is still O(N²); only the memory traffic became linear. The backward pass recomputes attention on the fly (trading FLOPs for memory), and each hardware generation needs its kernel rewritten — which is exactly what FlashAttention-2 and -3 turned out to be.

07

Why it still matters

FlashAttention became a default building block baked into every major training and inference stack — PyTorch's scaled_dot_product_attention, vLLM, TensorRT-LLM, xFormers. If you've used a model with a long context window, you've almost certainly used it. And it grew into a lineage:

VersionYearWhat changed
FlashAttention2022Tiling + online softmax + recomputation; IO-aware exact attention
FlashAttention-22023Better parallelism & work partitioning between GPU thread blocks and warps — roughly again, reaching 50–73% of A100 peak FLOPs
FlashAttention-32024Rebuilt for Hopper (H100): warp specialization, asynchronous TMA copies, FP8 support — another ~1.5–2×

Its deeper lesson is one every systems engineer learns eventually: on modern hardware, data movement is the cost, not arithmetic. The fastest code isn't the one that does the least math — it's the one that moves the least memory. FlashAttention is that principle, applied to the single most important operation in modern AI.

Frequently asked

Quick answers

What is FlashAttention?

An IO-aware algorithm that computes exact attention faster and with far less memory by minimizing data movement between a GPU's slow HBM and fast SRAM — processing attention in tiles with an online softmax.

Why was standard attention slow?

It writes the full N×N scores matrix to slow HBM and reads it back multiple times. The reads and writes — not the math — were the bottleneck; attention was memory-bound.

SRAM vs HBM?

HBM is large but slow main memory; SRAM is tiny but ~10x faster on-chip memory. FlashAttention does as much as possible in SRAM and touches HBM as little as possible.

Is it exact or approximate?

Exact — it computes the identical result as standard attention, just with a smarter memory schedule. No accuracy is lost.

How does it enable long context?

By never storing the full scores matrix, its memory grows linearly (not quadratically) with sequence length, making much longer context windows practical.

FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness · Dao, Fu, Ermon, Rudra, Ré · NeurIPS 2022 · read the original paper on arXiv → · Vibe Engines · 2026
Finished this one? 0 / 101 Paper Breakdowns done

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Paper Breakdowns

Cite this page

Reference it in your work, paper or an AI's context window.

APASingh, S. (2026). FlashAttention. Vibe Engines. https://vibeengines.com/paper/flash-attention
MLASingh, Saurabh. “FlashAttention.” Vibe Engines, 2026, vibeengines.com/paper/flash-attention.
BibTeX
@online{vibeengines-flash-attention,
  author       = {Singh, Saurabh},
  title        = {FlashAttention},
  year         = {2026},
  organization = {Vibe Engines},
  url          = {https://vibeengines.com/paper/flash-attention}
}