Core optimizations
Why attention is HBM-bound
Standard attention writes an n by n score matrix to HBM. Bytes grow with n squared, so long sequences leave tensor cores idle.
Textbook attention is easy to write and expensive to run. For each head you build an n × n score matrix, softmax it, and multiply by V. The math is correct. For long sequences the memory traffic is the problem: those scores live in GPU HBM, and moving them costs more than the FLOPs.
Concept
HBM is the GPU's main memory. On an H100 it moves on the order of 3 TB/s. On-chip SRAM (shared memory / L1) is smaller, much higher bandwidth, and much lower latency. IO-awareness means designing the algorithm around that gap, instead of treating every byte as equal.
The big idea
Imagine printing a seating chart for every pair of people at a stadium, carrying it to a warehouse, then bringing it back to seat them. The warehouse is HBM. The seats are SRAM. Classic scientific computing called this "blocking" or "tiling." FlashAttention is that idea applied to attention.
The analogy breaks on FLOPs. Full attention still does O(n² d) math. IO-aware kernels do not make attention linear. They stop warehousing the chart.
Walking through it
For one head, sequence length n, head dimension d:
S = Q K^T / sqrt(d) # n × n scores
P = softmax(S) # n × n probabilities
O = P V # n × d outputBytes scale as O(n²) for S and P, plus O(nd) for Q, K, V and O. FLOPs are O(n² d), so arithmetic intensity for the score stage is roughly O(d). That is often not enough to saturate tensor cores once S round-trips through HBM.
Sequence length n = 4096, head dim d = 128, FP16:
Full score matrix per head: 4096² × 2 bytes ≈ 32 MBAt n = 8192 the same matrix is 8192² × 2 ≈ 128 MB per head. A block with 32 heads that wrote full maps would move multiple GB of attention matrices through HBM on one layer, before MLP traffic.
Training also stores activations for the backward pass. Materialized n × n maps blow up memory at long context and force smaller batches.
Common mistakes
- Blaming FLOPs when the kernel is waiting on HBM. Intensity near
O(d)is the giveaway. - Thinking a faster GPU fixes
n × nwrites. Bandwidth helps, but the quadratic still wins asngrows. - Confusing this with approximate attention. The problem here is layout and traffic, not the formula.
Check yourself
- Why is the score stage often memory-bound even though it does
O(n² d)FLOPs? Answer: It also movesO(n²)bytes, so intensity is only aboutO(d), which is below the ridge on modern GPUs. - How big is one FP16 score matrix at
n = 4096? Answer: 4096² × 2 ≈ 32 MB per head.
Key takeaways
- Standard attention materializes
n × nscores in HBM. - Bytes grow with
n². FLOPs grow withn² d. Intensity stays aboutO(d). - SRAM is the fast path. HBM is the warehouse you should not use for the score matrix.
- This is why FlashAttention exists. It does not change the FLOP count of full attention.
Further learning
Next, online softmax is the identity that lets you stream the row in tiles.
Related
FlashAttention
Why standard attention is HBM-bound, how tiling plus online softmax computes exact attention in on-chip SRAM, and what FlashAttention-2/3 and decode variants change.
The roofline model and arithmetic intensity
Predicting performance from FLOPs and bytes: compute-bound vs memory-bound. Start here for the map, then work the examples.
Self-attention
The operation at the core of every modern language model. Each position builds a weighted average of the others, with weights set by query-key matches.