17/26
Part 4 of 4 · Prefill, decode and memoryLearn and do
Prefill runs the prompt once, decode runs one token per step
Generation with a cache has two phases, and they cost different things.
| Prefill, once | Decode, once for each new token | |
|---|---|---|
| Tokens in | the whole prompt, the cat sat | 1 per sequence, like on |
T_new | the prompt's length | 1 |
| The cache | filled in every layer | 1 row added in every layer |
| The math | matrix times matrix | 1 row per sequence times a matrix |
Pope et al. (2022) call the two phases prefill and decode. Prefill runs the whole prompt in one pass on an empty cache: every prompt token is known up front, so it's big matrix-times-matrix work, which GPUs do well. That pass is most of the wait before the first token appears. Decode reads every weight matrix and the whole cache from GPU memory, to do one row of arithmetic per sequence.
Decode waits on memory, not on arithmetic
Multiply n rows by one C × C weight matrix, 2 bytes per number. A FLOP is one floating-point operation: one multiply or one add.
Decode multiplies one row per sequence by each C × C weight matrix, read once from memory. With a batch of 8 sequences, how many FLOPs do you get per byte read?
Pick an answer above to see the rest