Chapter 04

Prefill vs decode

Two very different workloads.

tokensThe␣capital␣of␣France␣is␣Paris.stepsstep 1 · prefill5 tokens, one pass, fills the cachestep 2decodestep 3decode1 token each, reads the cache

The first step has a name: prefill. Every step after: decode. Same model, same weights, very different work.

← KV cache
01 / 10
The arithmetic, once

A step with nn tokens needs roughly 2Pn2Pn FLOPs for a model with PP parameters, and has to read the weights (2P2P bytes in bf16) plus the cache no matter what nn is:

tcompute=2PnFLOP/s,tmemory=2P+cachebytes/st_{\text{compute}} = \frac{2Pn}{\text{FLOP/s}}, \qquad t_{\text{memory}} = \frac{2P + \text{cache}}{\text{bytes/s}}

They cross at n=FLOP/sbytes/sn^* = \dfrac{\text{FLOP/s}}{\text{bytes/s}}, the accelerator’s ratio of compute to bandwidth. For an H100 that’s about 9891012/3.351012300989 \cdot 10^{12} / 3.35 \cdot 10^{12} \approx 300 tokens per step. Decode runs at n=1n = 1 per sequence.