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
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.
Same weights, very different reuse
Intensity and the two time estimates
7B model, 2,000-token prompt
What to remember
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.
Bytewise
All courses