L1 Beginner · Module 3: The performance mental model

3.4Prefill vs decode: TTFT and TPOT

A request runs as one big compute-bound prefill followed by many small memory-bound decode steps. The first sets time to first token, the second sets time per output token, and the two pull a serving engine in opposite directions.

✦ Interactive~20 min

By the end you will be able to

  • ✓Predict TTFT and TPOT for a model, GPU and request shape from roofline arithmetic
  • ✓Explain why prefill is compute-bound and decode is memory-bound
  • ✓Show how batching trades per-user TPOT for total throughput
  • ✓Explain why prefill and decode interfere when they share a GPU

You met the two phases in 0.2 and 1.4. With arithmetic intensity (3.2) you can now predict how long each one takes.

Two phases, two limits

Prefill runs all TT prompt tokens through the model at once. Every weight matmul has M=TM = T rows, so its intensity is in the hundreds or thousands: compute-bound. Its time is about

TTFT≈2N⋅T+attentionPpeak\text{TTFT} \approx \frac{2N \cdot T + \text{attention}}{P_{\text{peak}}}

Decode runs one new token per sequence. At batch bb, matmul intensity is about bb, and attention intensity is about 1–8 regardless of batch. Unless bb is in the hundreds, it is memory-bound:

TPOT≈weight bytes+b×KV bytes per sequenceBHBM\text{TPOT} \approx \frac{\text{weight bytes} + b \times \text{KV bytes per sequence}}{B_{\text{HBM}}}

For Llama-3-8B on an H100, a 2,048-token prompt and 256 output tokens, the roofline gives TTFT ≈ 30 ms and TPOT ≈ 4.6 ms. The whole answer takes about 1.2 s, and 97% of it is decode.

·
request arrives↑ first token at 30.3 mslast token at 1.19 s
TTFT
30.3 ms
TPOT
4.57 ms
end to end
1.19 s
tokens/s per user
219

Prefill is compute-bound (99% of its time in compute-limited ops). It processes 2,048 tokens in one pass.

Decode is memory-bound. Each step makes 1 token, so the GPU produces 219 tokens/s in total.

Roofline lower bounds in BF16, prefill of this one request alone. Real engines reach maybe 60–85% of these speeds.

fig. 3.4One request on one GPU, from roofline arithmetic. Change the prompt, the answer length, the number of users decoding together, the model and the GPU, and watch TTFT, TPOT and throughput move.

Batching: the central trade

Decode at batch 1 wastes almost all of the GPU’s arithmetic. Adding sequences to a step reuses the weights already being loaded, so throughput rises nearly in proportion while TPOT grows slowly, until the KV cache reads take over. That gives every serving engine its core dial:

  • Small batches: fast tokens for each user, expensive per token.
  • Large batches: cheap tokens, slower for each user, and more memory spent on KV cache.

The right setting depends on what users were promised: a service-level objective (SLO) such as “TTFT under 500 ms and TPOT under 50 ms at the 99th percentile”. Chapters 13.1 and 16.2 turn that into numbers you can measure.

Why the phases interfere

A serving engine runs both phases for different users on the same GPU. When a new request’s prefill enters a step, that step becomes compute-bound and much longer, and every user in the decode batch waits for it. A 30 ms prefill inserted into a stream of 5 ms decode steps is a visible stutter for everyone.

Sources

Check your understanding

0 / 4 answered
  1. 1.Which GPU spec most affects TPOT at small batch sizes?

  2. 2.Why is prefill compute-bound?

  3. 3.Going from batch 1 to batch 64 in decode usually…

  4. 4.Why can a new request's prefill hurt other users' TPOT?