Inference Arithmetic and the Roofline
Why prefill is compute-bound and decode is memory-bound, derived from the FLOP and byte counts of a transformer forward pass.
Counting the work
Fix a decoder-only transformer with layers, model width , and non-embedding parameters. We count multiply–accumulate as two FLOPs, the standard convention.
A forward pass over a sequence of tokens costs
Assumptions
- Every parameter participates in exactly one multiply–accumulate per token. This is exact for dense matrix multiplications and ignores layer norms, activations and residual adds, whose cost is and therefore negligible against both terms.
- Attention is dense (every query attends to every key). Sparse, sliding-window or linear attention variants change the second term and nothing else.
- Embedding and unembedding parameters are excluded from , per the usual convention.
When does attention actually dominate?
The quadratic term overtakes the linear one when , that is when
For a 7B-parameter model with and :
Key idea
Below roughly 25,000 tokens of context, attention is not where the FLOPs go — the feed-forward and projection weights are. This is why "attention is quadratic" is a misleading summary of short-context inference cost, and why it becomes the correct summary for long-context inference. The crossover is a property of the model's shape, not of attention as such.
The roofline
Let a computation perform FLOPs while moving bytes between memory and the processor, and define its arithmetic intensity . On hardware with peak throughput FLOP/s and memory bandwidth byte/s, the attainable throughput satisfies
The two regimes meet at the ridge point . A kernel with is memory-bound; one with is compute-bound.
The two phases have opposite intensity
Prefill
Processing a prompt of tokens does FLOPs while loading the weights once, i.e. bytes for bytes per parameter. Its intensity is
At (BF16), . Any prompt longer than a few hundred tokens clears the ridge point comfortably: prefill is compute-bound.
Decode
Generating one token for each of sequences, in the regime where weight traffic dominates, has arithmetic intensity
independent of , and .
What this does not say
This ignores KV-cache traffic, which also grows with and with context length and is the other half of decode's memory bill. Including it lowers the intensity further, so the conclusion below — that small-batch decode is memory-bound — only strengthens. The proposition is an upper bound on intensity, not an estimate of it.
At , . To reach an H100's ridge point you need a batch of roughly 300 concurrent sequences. Below that, the tensor cores are idle most of the time and the only thing that matters is how fast weights stream out of HBM.
With parameters at bytes each and memory bandwidth , the time to emit one token satisfies
whatever the implementation.
What this predicts, and where it fails
Empirical observation — a regularity seen in experiments. No theorem on this page establishes it in general.
Measured decode throughput on production stacks rises roughly linearly with batch size in the small-batch regime and then flattens, with the knee in the low hundreds of sequences — the qualitative shape this analysis predicts.
Open problem — no proof and no counterexample is known to the author of this page.
There is no accepted analytical model that predicts achieved throughput of a full serving stack from model shape and hardware specifications. Practitioners benchmark. The roofline gives a ceiling and the gap to it is measured, not derived — a situation that is normal in performance engineering and worth stating plainly rather than papering over.
References
- PaperS. Williams, A. Waterman, D. Patterson (2009). Roofline: An Insightful Visual Performance Model for Multicore ArchitecturesThe roofline model is stated here in its original hardware-agnostic form.
- PaperJ. Kaplan et al. (2020). Scaling Laws for Neural Language ModelsSource of the 2N-per-token forward cost convention used throughout.
These works are cited as sources to read, not reproduced. All exposition, examples and problems on this site are written independently; where an argument is standard, the citation points to a canonical treatment.
Results stated on this page
- PropositionForward-pass cost of a transformerproof includedA forward pass over S tokens costs about 2NS + 4LdS² FLOPs, with the quadratic term negligible until S exceeds N/(2Ld).
- PropositionThe roofline boundproof includedAttainable throughput is at most the minimum of peak compute and arithmetic intensity times memory bandwidth.
- PropositionDecode arithmetic intensity is set by the batch size aloneproof includedWith b bytes per parameter, weight-limited decoding has arithmetic intensity 2B/b, independent of model size.
- CorollaryA hard floor on single-stream decode latencyproof includedNo implementation can emit a token faster than the time to stream the weights once from memory.
Problems (5)
All problems- Derive the attention crossover lengthProofBeginner⏱ 15 min
- Single-stream decode latency floorsNumericalIntermediate⏱ 30 min
- Count the FLOPs of a forward passNumericalFoundation⏱ 15 min
- Build a serving capacity calculatorCodingAdvanced⏱ 3 hours
- Ridge point and the batch size needed to saturateNumericalBeginner⏱ 15 min