Inference Engineering

Lesson 3 of 14

Prefill and decode

A chat user waits twice, once for the first word and then between every word after it. The two waits come from different phases of the same request, limited by different parts of the GPU, so making one faster often does nothing for the other.

One request, two kinds of work

One shot, three stages, with real numbers for Llama 3.1 8B at 16 bits on one NVIDIA H100, slowed down so a few thousandths of a second can be seen. It plays by itself; press a stage to replay it, or drag to look around.

01 · Two waits

A pause, then a stream

Two waits in one reply

You

A 2,048-token report, then: What does it say about March?

Parrot

0 ms since you pressed Enter, slowed down

First wait: reading the question…

Then one token every…

Llama 3.1 8B on one NVIDIA H100, weights stored as 16-bit numbers. Each word or punctuation mark stands for one token.

Parrot’s reply comes in two acts. First a pause, while the model reads your whole question: that is prefill, and the pause is the time to first token. Then a stream, as it writes the answer one token at a time (a token is a piece of a word, about three-quarters of one on average): that is decode, and its pace is the time per output token.

Your two piles of tickets are these two waits. They run on the same GPU but are slow for different reasons, so take them one at a time, with lesson 1’s clerk, her rulebook and the belt that brings its pages up from the archive.

02 · The first wait

Reading the question: the clerk is the limit

A longer question, a longer first wait

Plays by itself; move the slider to take over.

  • Clerk: the arithmetic on every token
  • Belt: bringing the weights up once
  • Time to first token

Both axes are logarithmic. Floors for Llama 3.1 8B at 16 bits on one H100.

Your whole question lands on the desk at once. Each page rides up the belt once, and the clerk applies it to every question token before the next page. One trip serves them all, so the belt is quick next to her work: she sets the pace, and the wait grows with the question. A 2,048-token question takes at least 30.0 ms.

A very short question can’t keep her busy. Below a few hundred tokens the belt is slower than her arithmetic, and the first wait stops shrinking.

03 · The second wait

Writing the answer: the belt is the limit

Reading against writing, on one clock

0 ms of real time, slowed down

Reading a question0 tokens

Writing an answer0 tokens

  • One question token: arithmetic, about 15.7 µs each
  • One answer token: a full read of the weights, 4.57 ms each

In the time the GPU writes one token of answer, it reads about 290 tokens of question.

Llama 3.1 8B at 16 bits on one H100. A question token's cost is what one more token adds to the first wait of a 2,048-token question.

Each answer token depends on the one before, so they come one at a time, and every token needs every page again. Now the belt runs flat out and the clerk finishes each page in a blink, then waits: about 4.57 ms a token.

The two phases are wildly uneven. Writing one token of answer takes as long as reading about 290 tokens of question. A 300-word answer costs far more time than a 3,000-word question, so the slow-typing pile is often the bigger one.

04 · Two fixes

One fix will not cure both

Three GPUs, one request

A 8,192-token question and a 64-token answer. 0 ms of real time, slowed down

  1. A100

    first token … · then one every … · done …

  2. H100

    first token … · then one every … · done …

  3. H200

    first token … · then one every … · done …

  • Arithmetic rating (the clerk); reading the question
  • Memory bandwidth (the belt); one slice per answer token

Floors for Llama 3.1 8B at 16 bits. The ratings on the left are drawn relative to the best of the three GPUs.

So the single fix your boss wants doesn’t exist. More arithmetic shortens the first wait and barely touches the typing. A faster belt, or fewer bytes to fetch, speeds up the typing and leaves a long question’s wait alone. Storing the weights as 8-bit numbers always speeds up the typing; it shortens the first wait only if the GPU also does its arithmetic in 8 bits (lesson 10).

Decode has one cheap fix of its own, batching: while the clerk waits on the belt, each page can serve the next token of many chats at once. Prefill gains almost nothing from it, because its clerk was already busy.

05 · Playground

Time your own request

Try a long question with a short answer, then the reverse. Then swap the GPU and see which wait moves.

Your request

Llama 3.1 8B at 16 bits, real speed

Wait for the first token

Then one token every

Whole answer done after

first wait + one step per answer token

Every time is a floor for one user on an idle GPU. Real servers are slower, but the shape holds.

06 · Glossary

The vocabulary, in one place

Token
The unit a model reads and writes: a piece of a word, about three-quarters of one on average.
GPU
The chip that runs the model: arithmetic units plus memory of its own. Here an NVIDIA H100.
Weights
The numbers a model multiplies by: the clerk's rulebook, cut into one page per layer. Llama 3.1 8B reads 15.0 GB of them for every pass, at 16 bits each.
Arithmetic units
The clerk: the parts of the chip that multiply and add. An H100 is rated at 989 trillion operations a second (TFLOP/s) at 16 bits.
Memory bandwidth
The belt: how many bytes a second GPU memory, the archive, can deliver to the chip. 3.35 TB/s on an H100.
Prefill
Reading the question: every question token goes through the model in one pass. Limited by arithmetic, unless the question is very short.
Decode
Writing the answer: one pass of the whole model for each new token. Limited by memory bandwidth.
Time to first token
The first wait, set by prefill. At least 30.0 ms for a 2,048-token question on an H100.
Time per output token
The gap between answer tokens, set by decode. About 4.57 ms here, or 219 tokens a second.
Memory-bound, compute-bound
Limited by the belt (the bytes), or by the clerk (the arithmetic). Each needs a different fix.
Batching
Serving many chats in the same pass, so every page the belt brings up is used for all of them.
Floor
The fastest a GPU could possibly do a piece of work. Every time on this page is one; real software is somewhat slower.

You can now

  • Compute a request's time to first token, time per output token and end-to-end latency floors from the prompt length, answer length, model and GPU, and say when a short prompt makes prefill memory-bound instead.
  • Explain why prefill is limited by arithmetic and decode by memory, and why one output token costs as much GPU time as hundreds of prompt tokens.
  • Decide which change fixes which complaint: more arithmetic for slow starts on long prompts, faster memory or a smaller model for slow typing, smaller numbers for both, and batching for decode throughput.

Open the workbook: the formulas, the exact numbers, graded exercises and the sources behind this page.