Why does FlashAttention speed up serving even though it doesn't change the math?
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
FlashAttention computes exactly the same attention output as a naive implementation: same logits, same softmax, same V-weighted sum. So where does the 2-4× speedup come from on serving workloads? Where is the win biggest and where is it smallest?
FlashAttention is IO-aware: tiling plus online softmax keep attention in SRAM, so HBM traffic drops from O(n^2) to O(n*d). Same math, fewer memory round-trips, biggest win in prefill.
Imagine doing a huge multiplication by hand. The naive way is to write every intermediate number onto a giant sheet of paper on the far side of the room, walk over to read it back, then walk over again to write the next step. The paper is slow to reach, so most of your time goes to walking, not multiplying. FlashAttention keeps a small notepad in your pocket. It works on one small chunk at a time on the notepad, keeps a running total, and never writes the giant sheet at all. The final answer is identical, because the arithmetic is the same. You just stopped wasting time walking back and forth to the far paper, so the whole job finishes much faster.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
4 min: exact math unchanged + SRAM versus HBM + naive kernel materializes n-by-n in HBM + tiling and fusion + online softmax + traffic drops to O(n*d) + prefill wins, batch-1 decode does not.
| Aspect | Naive attention | FlashAttention |
|---|---|---|
| Output | Exact | Exact, identical |
| Score matrix in HBM | Materialized, n by n | Never written |
| HBM traffic | Order n-squared | Order n times d |
| Peak memory | Order n-squared | Order n |
| Biggest win | n/a | Long-context prefill |
Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Claiming FlashAttention changes the math or approximates softmax. It is exact; the only thing that changes is the memory access pattern, not a single output number.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.