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, onceDecode, once for each new token
Tokens inthe whole prompt, the cat sat1 per sequence, like on
T_newthe prompt's length1
The cachefilled in every layer1 row added in every layer
The mathmatrix times matrix1 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.

Predict

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