20.4.1 Step time and the roofline
This part of Serving, Capacity & Load Testing: Math to Code develops the mechanisms below. Work the numerical examples or trace the code before checking the answer.
Concepts from other sections (12)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- MBU / MFU: the fraction of peak memory bandwidth or peak FLOPs an engine actually achieves. Read more: 20.4.6 Load tests and the deployment runbook
- TPOT (time per output token): time between successive tokens of a running request, about one step. Read more: 23.4 Chunked prefill (mixed prefill + decode batches)
- TTFT (time to first token): time from a request’s arrival to its first generated token. Read more: 23.4 Chunked prefill (mixed prefill + decode batches)
- KV cache: the stored keys and values of all processed tokens, per layer, that let each new token attend without recomputation. Read more: Chapter 20: Inference Engines and GPU Deployment
- roofline: the bound on a loop’s speed from peak compute , memory bandwidth , and arithmetic intensity . Read more: 22.3 Cache-blocked, packed, batch-invariant matmul in C
- memory-bound: limited by the rate at which data can be read, not by arithmetic throughput. Read more: 22.7 Fused int4 and int8 dequantize-matmul in C
- BF16: a 16-bit float with float32’s sign and exponent and 7 mantissa bits; the top half of a rounded float32. Read more: 6.7 Safetensors for every dtype, atomic checkpoints, and the token stream
- ridge point: the intensity that separates memory-bound from compute-bound work. Read more: 20.4.6 Load tests and the deployment runbook
- Decode step: a forward pass over one new token against the cache. Read more: 21.2 The KV cache, incremental decoding, and generate
- Grouped-query attention (GQA): query heads share key/value heads, each serving a contiguous group of query heads. Read more: 13.5 Grouped-Query Attention with a Cache Hook, Windows, and Sinks
- Mixture of experts (MoE): a layer of many expert MLPs of which a router picks per token. Read more: 13.8 Mixture of Experts
- arithmetic intensity: FLOPs per byte moved; for a linear layer at BF16 it equals the number of tokens in the step. Read more: 20.4.6 Load tests and the deployment runbook
Learning objectives
- Identify whether compute, memory traffic, communication, or overhead limits a workload.
The step-time equation
Section titled “The step-time equation”A serving engine runs a loop: pick a batch of sequences, run one forward pass, append one token to each (or a chunk of prompt for sequences still prefilling), and repeat. The step time of that loop is
where MFU and MBU are the fractions of peak compute and peak memory bandwidth the engine actually achieves. Every metric a customer cares about is a function of that equation. TPOT is during decode; TTFT is queueing plus the prefill steps; throughput is ; and cost is the fleet price in USD/hour divided by throughput in the corresponding units. Every engine knob changes a term of it: batch size, bytes per weight, bytes per KV element, , or how prefill tokens are mixed in. The engineer’s job is to know which term dominates for this customer’s traffic and to move that one.
It also helps to know where this loop sits in a production stack. The gateway and router decide which replica runs a request; the engine’s scheduler and KV cache manager decide when, and with how much memory; and the autoscaler closes the loop on queue depth and KV utilization.
The first term of the step-time equation to understand is the max: whether a step is limited by compute or by memory. That is what the roofline tells you.
Prefill versus decode: the roofline
Section titled “Prefill versus decode: the roofline”The arithmetic intensity of a computation is . The roofline model says attainable throughput is , and the ridge point separates memory-bound work () from compute-bound work. For an H100 SXM, with 989 TFLOP/s of dense BF16, 1979 TFLOP/s of dense FP8, and 3.35 TB/s of HBM3, the ridge point is FLOP/B for BF16 and about 590 FLOP/B for FP8. The H200 keeps the same FLOPs but raises bandwidth to 4.8 TB/s, which is why it decodes faster in the same price class.
Linear layers dominate the FLOPs, so they decide which side of the ridge a step falls on. For a weight and tokens in the step, the FLOPs are and the weight bytes are , so . At BF16 (), the intensity equals the number of tokens in the step. That one fact explains the two phases.
In prefill, is the prompt length, say 2048, so : prefill is compute-bound. A lower bound on prefill time is for parameters and prompt tokens. For Llama-3.1-8B with a 2048-token prompt, that is ms at 100% MFU, and roughly 50 to 60 ms in practice.
In decode, is the batch size , since each sequence contributes one new token. At , , and the GPU’s compute is about 300 times underused. The step costs one full read of the weights plus the KV history. That gives a bound on single-sequence decode speed:
For Llama-3.1-8B in BF16 on one H100, the weights alone give tok/s, and about 202 tok/s with a 4k-token KV history. Real engines reach an MBU of 60 to 85%. Decode is a bandwidth problem and prefill is a compute problem: a decode step reads every weight once to produce one token per sequence.
Batching is what rescues decode, and it is the whole economic engine of an inference cloud. Increasing adds only KV bytes per step, because the weights are shared, so aggregate tokens per second rise almost linearly until one of three things happens: the KV reads dominate, KV memory runs out, or approaches and decode turns compute-bound. Note that attention itself never batches. Each sequence reads its own KV, so attention’s intensity stays around 1, raised to the group size by GQA.
Mixture-of-experts models complicate the picture. Their per-token FLOPs scale with active parameters, but at large batch nearly every expert is touched each step, so bytes per step trend toward total parameters. MoE is cheap at high batch and expensive at batch 1.
The decode section of code/capacity.py implements exactly this reasoning, computing both the memory time and the compute time of a step and taking the larger:
w_read = m.active * a.weight_bytes / n_gpus # weights touched per step, per GPUstep_bytes_b = w_read + batch * avg_ctx * kv_tok_per_gpu # plus every sequence's KV historyt_mem_b = step_bytes_b / (hbm_bw * mbu)t_cmp_b = 2 * m.active * batch / (peak * tp) # linear-layer FLOPsagg_tok_s = batch / max(t_mem_b, t_cmp_b)Summary
Section titled “Summary”Identify whether compute, memory traffic, communication, or overhead limits a workload. State the assumptions before applying the result to a different workload.
Review questions
Section titled “Review questions”-
Can increasing batch size move decode toward a compute bottleneck?
Answer
Yes. Reusing weights across more sequences raises arithmetic intensity; attention traffic, communication, and latency constraints still matter.