4/30
Part 1 of 7 · Two clocksLearn

On a laptop CPU, reading the prompt was 45 times cheaper per token than writing the answer

The two phases cost very different amounts per token. A small model, timed on a laptop, shows how different.

# 512 prompt tokens
prompt = torch.randint(0, 1000, (1, 512))
# prefill: one forward pass over all of them
logits, caches = model(prompt)
token = torch.randint(0, 1000, (1, 1))
# decode: one token, against the cache
logits, caches = model(token, caches)

These two calls were timed on a GPT built from the kv-cache lesson's cached attention, 8 layers of width 512, about 26.8 million parameters, on one laptop CPU with 4 threads. The medians of three runs:

Milliseconds per token on one laptop CPU
prefill
0.18 ms
decode
8.1 ms

Prefill took 92 ms for all 512 prompt tokens. Each decode step took 8.1 ms for one token.

Prefill pushes all 512 tokens through each weight matrix in one pass, as one big matrix product. Decode pushes one token through the same matrices, so it does a sliver of the arithmetic for the same trip through the weights. The kv-cache lesson showed why: decode waits on memory, not on arithmetic.

That's why the first clock is short for a long prompt and the second clock adds up. On a GPU the gap is wider still.