The inference cost model, on one page

Reference · roofline arithmetic for LLM serving · the back-of-envelope you do before writing a kernel

The one-line model

Prefill runs once over all P prompt tokens → compute-bound → sets time-to-first-token.  Decode runs once per output token, re-reading every weight from HBM → memory-bound → sets tokens/sec. Decode intensity is 2 × B ÷ bytes_per_weight; the model size cancels.

Glossary (adhere to this in every lesson)

TermMeaning
PrefillThe single forward pass over the whole prompt at the start of a request. Large matmuls; weights read once for all P tokens.
DecodeOne forward pass per generated token. Each pass re-reads the full weight set for one new token per sequence.
TTFTTime to first token — essentially the prefill time. The latency the user feels before anything appears.
Arithmetic intensityFLOPs performed ÷ bytes moved. A property of the work. Units: FLOP/byte.
Machine balancePeak FLOP/s ÷ memory bandwidth. A property of the hardware. A100: ≈153 FLOP/byte.
Memory-boundIntensity below machine balance. Time = bytes ÷ bandwidth. Math units idle. Fix by moving fewer bytes.
Compute-boundIntensity above machine balance. Time = FLOPs ÷ peak. Fix by doing less arithmetic (or using faster units).
RooflineThe model that says runtime = max(compute time, memory time). Deliberately optimistic; a floor on time, not a benchmark.
HBMHigh-bandwidth memory — the GPU's off-chip DRAM. Fast in absolute terms, still the decode bottleneck.
Weight-only quantizationStore weights narrow (INT8/INT4/NF4), dequantize to FP16 for the math. Cuts bytes, not FLOPs — which is why it helps decode and barely helps prefill.
KV cachePer-sequence cached attention keys/values. Grows with batch × context, and is what caps B in practice.

The formulas

The full roofline chain for an N-parameter model, batch B, prompt P, output O.

Listen first: six steps. One, weight bytes equals the parameter count times bytes per weight. Two, prefill floating-point operations equal two times parameters times prompt length times batch. Three, decode operations per step equal two times parameters times batch. Four, each phase's time is the larger of its operations divided by peak throughput, and its bytes divided by bandwidth — whichever resource is slower wins. Five, the whole batch takes prefill time plus output length times decode step time, and per-request cost is that divided by the batch size. Six, the crossover point is total training seconds divided by per-request seconds — how many requests it takes for serving to cost what training cost.

The numbers worth memorizing (A100 80GB SXM)

QuantityValueWhy it matters
Peak FP16 tensor-core throughput312 TFLOP/sNumerator of machine balance.
HBM2e bandwidth2039 GB/sDenominator. The decode bottleneck.
Machine balance≈153 FLOP/byteThe line every kernel is judged against.
Batch to make FP16 decode compute-bound≈1542B/2 > 153. Below it you are wasting silicon.
Batch to make INT4 decode compute-bound≈392B/0.5 > 153. Quantization gets you there 4× sooner.
Llama 2 7B decode, FP16, batch 16.9 ms/token · 0.65% math busyThe canonical "the GPU is idle and still slow" data point.
Llama 2 7B pretraining184,320 A100-hoursThe capex number serving is measured against.

NVIDIA A100 datasheet↗ Llama 2, Table 2↗

The decision table

SymptomWhat it meansLever
Low tokens/sec, math units near idleDecode is memory-bound (the normal case)Raise batch size; quantize weights; fuse kernels to cut HBM round-trips.
Slow time-to-first-tokenPrefill is compute-bound and the prompt is longChunked prefill, prompt caching, better matmul kernels.
Can't raise batch sizeKV cache has eaten the HBMPaged/quantized KV cache, shorter contexts, bigger GPU.
Quantized and prefill barely improvedExpected — quantization cuts bytes, not FLOPsNothing wrong. Measure decode, not the whole request.

Health warnings

💬 Ask your teacher to expand any row of these tables into its own lesson. The KV cache row is the one that is overdue.

Where this comes from

Reference · Zain's AI Inference Lab · sources: NVIDIA A100 datasheet, Llama 2 (arXiv:2307.09288)