The memory hierarchy, and where attention actually runs
The roofline's "bandwidth" is HBM bandwidth. On an H100 there are several tiers above it:
| tier | capacity | bandwidth | latency |
|---|---|---|---|
| registers | 256 KB per SM | ~100 TB/s | ~1 cycle |
| shared memory / L1 | 228 KB per SM | ~20 TB/s | ~30 cycles |
| L2 | 50 MB | ~10 TB/s | ~200 cycles |
| HBM3 | 80 GB | 3.35 TB/s | ~500 cycles |
Shared memory is about 6× the bandwidth of HBM and 16× lower latency, but there is only 228 KB of it per SM. That constraint — fast but tiny — is what shapes every high-performance attention kernel.
Now count what naive attention costs in HBM traffic. For one head, sequence length N, head dimension d, in fp16:
1. read Q, K 2 * N * d * 2 bytes
2. write S = QK^T N^2 * 2 bytes
3. read S N^2 * 2 bytes
4. write P = softmax(S) N^2 * 2 bytes
5. read P, read V N^2 * 2 + N * d * 2 bytes
6. write O N * d * 2 bytes
---------------------
~4 N^2 * 2 bytes of traffic for the score matrix
At N = 8192, d = 128: the N × d terms are about 2 MB each, while each N² term is 134 MB. The score matrix dominates by roughly 60×, and you move it four times.
For a full model — 32 heads, 32 layers — that is 4 × 134 MB × 32 × 32 = 549 GB of HBM traffic for one 8k-token prefill, just shuttling intermediate scores. At 3.35 TB/s that is 164 ms of pure memory movement, and none of it is arithmetic.
The FLOPs, meanwhile, are 4 N² d per head — real work that must happen. The problem is not the arithmetic. It is that the intermediate never needed to exist in HBM.
This is a general lesson worth extracting: the roofline says "minimize bytes from DRAM", and one of the most powerful ways to do that is to restructure an algorithm so intermediates stay in a faster tier. That is what kernel fusion is, and attention is its most valuable application.
NAIVE FLASH
HBM HBM
| Q,K -> | Q,K,V (tiles) ->
| [S = QK^T] |
| <- S (134 MB) | SRAM: [tile QK^T]
| S -> | [online softmax]
| [softmax] | [accumulate O]
| <- P (134 MB) | (never leaves SRAM)
| P,V -> |
| [PV] | <- O only
| <- O |
|
4 x N^2 traffic O(N) traffic for the scores
REMEMBERShared memory has roughly 6x the bandwidth of HBM and 16x lower latency, so keeping a computation resident there is worth restructuring the algorithm for.