All courses

Prefill vs decode: compute- vs memory-bound

Explain why processing the prompt (prefill) is compute-bound, while generating each new token reads every weight and the whole KV cache for little compute (memory-bound). Estimate time-to-first-token and tokens per second from the hardware specs.

Builds on: The KV cache, Roofline model: compute- vs memory-bound, FLOPs per token: 2N forward, 6N training

▶ Watch this lesson Start the whole course

What the animation says

Pause, then stream

Send a long prompt to a chatbot. There's a pause, then words stream out at a steady pace. The pause is the model reading your prompt. That phase is called prefill. The stream is decode: one new token at a time. Both phases cost about the same math per token: two operations per parameter. Yet reading two thousand prompt tokens takes about as long as writing ten. So what makes one output token so expensive?

Same weights, different reuse

The answer is memory. Watch one weight matrix travel from memory to the math units. In prefill, all two thousand prompt tokens are known, so they enter together as one tall matrix. Each weight is fetched from memory once, then reused for all those rows. Lots of math per byte. In decode, only one new token exists. The input is a single row. Every weight is still fetched, plus the stored keys and values of earlier tokens: the KV cache. But each weight now does just one multiply-add. The math units mostly sit idle.

Math per byte

Let's count math per byte moved. Prefill does two times N times T operations, but moves only the weights, two bytes each. That's about T operations per byte. A decode step does two N operations and moves at least those same bytes. At most one operation per byte. A GPU can do about three hundred operations in the time it reads one byte. Below three hundred per byte, it waits on memory. Above, it waits on math. Now a short prompt, one hundred tokens. Which limit hits its prefill?

A 7B model, by the numbers

Memory-bound: one hundred is below three hundred. Only longer prompts cross the line. Now a seven-billion-parameter model at two bytes each: fourteen gigabytes. The GPU does one petaflop per second and reads three terabytes per second. Prefill of two thousand tokens is twenty-eight trillion operations: about fifty-six milliseconds at half utilization. That's time to first token. Each decode step reads the weights plus a one-gigabyte KV cache: fifteen gigabytes. Fifteen gigabytes over three terabytes per second is five milliseconds. About two hundred tokens per second. The math alone? Fourteen thousandths of a millisecond.

Move fewer bytes

So prefill reuses each weight across the whole prompt. It's compute-bound: time to first token is its math divided by achieved compute. Single-sequence decode re-reads every weight and the cache for each token. It's memory-bound: tokens per second is bandwidth divided by bytes per token. Faster streaming means moving fewer bytes per token. You can buy one upgrade for this seven-billion model. Tap the one that speeds up streaming.

Why does a reply pause, then stream?

Send a long prompt to an LLM. There is a pause, then tokens stream out at a steady pace. Both phases run the same network at about $2N$ FLOPs per token. Yet reading a 2,000-token prompt can take about as long as writing roughly 10 tokens. Why is one output token so much costlier than one input token? And how can you predict the time-to-first-token and the streaming speed from a GPU spec sheet?

Same weights, very different reuse

**Prefill**: all $T$ prompt tokens are known, so each layer processes them together as a $T\times d$ matrix. Each weight matrix $W$ is fetched from GPU memory once and used for all $T$ rows of $XW$. That is a lot of math per byte. **Decode** (one request): only one new token exists, so $X$ is $1\times d$. Every weight is still fetched, plus the whole KV cache. But each weight does just one multiply-add. The GPU mostly waits on memory.

Intensity and the two time estimates

Let $b$ = bytes per weight (usually 2), $S_{KV}$ = KV-cache bytes, $P$ = peak FLOP/s, $BW$ = memory bandwidth (bytes/s), and $u$ = utilization (the fraction of $P$ achieved). Assume one sequence, no batching. - Prefill: $2NT$ FLOPs over $\approx bN$ bytes, so intensity $\approx 2T/b = T$ FLOPs/byte. - Decode step: $2N$ FLOPs over $bN+S_{KV}$ bytes, so intensity $\le 2/b = 1$. GPUs have $P/BW\approx 300$. Prefill is compute-bound once $T\gtrsim 300$. Single-sequence decode is memory-bound at any context length. For time-to-first-token (TTFT) and decode speed: $$\text{TTFT}\approx\frac{2NT}{uP},\qquad \text{tok/s}\approx\frac{BW}{bN+S_{KV}}$$

7B model, 2,000-token prompt

$N=7\times10^9$ at 2 bytes gives 14 GB of weights. GPU: $P=10^{15}$ FLOP/s, $BW=3\times10^{12}$ B/s. Shape: $L=32$, $h=h_{kv}=32$, $d_{head}=128$. **Prefill**: $2\cdot7\text{e}9\cdot2000=2.8\times10^{13}$ FLOPs. That is 28 ms at peak, or about 56 ms at $u=0.5$. **KV cache**: (K and V) × layers × heads × $d_{head}$ × tokens × bytes $=2\cdot32\cdot32\cdot128\cdot2000\cdot2\approx1.0$ GB. **Decode**: $15\text{ GB}/3\text{e}12=5$ ms per token, so about 200 tok/s. Its math, $1.4\text{e}10/10^{15}$, takes only 0.014 ms.

What to remember

Prefill reuses each fetched weight across all $T$ prompt tokens. It is compute-bound, with TTFT $\approx 2NT/(uP)$. Decode of one sequence fetches every weight plus the KV cache to produce one token. It is memory-bound, with tok/s $\approx BW/(\text{weight bytes}+\text{cache bytes})$. Faster generation means moving fewer bytes per token. More FLOP/s on the GPU barely helps.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.