Skip to content
nonconvexcard

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.

LLM OpsAdvanced⏱ 1 hour12 min readrooflinearithmetic intensityflopsmemory bandwidthprefill
5 problems →

Counting the work

Fix a decoder-only transformer with LL layers, model width dd, and NN non-embedding parameters. We count multiply–accumulate as two FLOPs, the standard convention.

PropositionForward-pass cost of a transformer

A forward pass over a sequence of SS tokens costs

Cfwd(S) ≈ 2NS⏟weights  +  4LdS2⏟attentionFLOPs.C_{\text{fwd}}(S) \ \approx\ \underbrace{2NS}_{\text{weights}} \;+\; \underbrace{4LdS^2}_{\text{attention}} \quad\text{FLOPs}.

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 O(LdS)O(LdS) 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 NN, per the usual convention.

When does attention actually dominate?

The quadratic term overtakes the linear one when 4LdS2>2NS4LdS^2 > 2NS, that is when

S  >  S⋆  :=  N2Ld.S \;>\; S_\star \;:=\; \frac{N}{2Ld}.

For a 7B-parameter model with L=32L = 32 and d=4096d = 4096:

S⋆=6.7×1092⋅32⋅4096≈2.6×104.S_\star = \frac{6.7\times 10^9}{2 \cdot 32 \cdot 4096} \approx 2.6 \times 10^4 .

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

PropositionThe roofline bound

Let a computation perform FF FLOPs while moving QQ bytes between memory and the processor, and define its arithmetic intensity I=F/QI = F/Q. On hardware with peak throughput π\pi FLOP/s and memory bandwidth β\beta byte/s, the attainable throughput satisfies

P ≤ min⁡(π, Iβ).P \ \le\ \min\big(\pi,\ I\beta\big).

The two regimes meet at the ridge point I⋆=π/βI^\star = \pi/\beta. A kernel with I<I⋆I < I^\star is memory-bound; one with I>I⋆I > I^\star is compute-bound.

The two phases have opposite intensity

Prefill

Processing a prompt of SS tokens does ≈2NS\approx 2NS FLOPs while loading the NN weights once, i.e. NbNb bytes for bb bytes per parameter. Its intensity is

Iprefill≈2NSNb=2Sb.I_{\text{prefill}} \approx \frac{2NS}{Nb} = \frac{2S}{b}.

At b=2b = 2 (BF16), Iprefill≈SI_{\text{prefill}} \approx S. Any prompt longer than a few hundred tokens clears the ridge point comfortably: prefill is compute-bound.

Decode

PropositionDecode arithmetic intensity is set by the batch size alone

Generating one token for each of BB sequences, in the regime where weight traffic dominates, has arithmetic intensity

Idecode≈2Bb,I_{\text{decode}} \approx \frac{2B}{b},

independent of NN, LL and dd.

What this does not say

This ignores KV-cache traffic, which also grows with BB 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 b=2b = 2, Idecode≈BI_{\text{decode}} \approx B. 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.

CorollaryA hard floor on single-stream decode latency

With NN parameters at bb bytes each and memory bandwidth β\beta, the time to emit one token satisfies

tstep ≥ Nbβ,t_{\text{step}} \ \ge\ \frac{Nb}{\beta},

whatever the implementation.

What this predicts, and where it fails

Empirical observation

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

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

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

Problems (5)

All problems