Lesson 12 of 14
Chunked prefill and disaggregation
When someone pastes a long document into a busy chatbot, the GPU stops to read it, and every other user's reply freezes mid-word. This lesson measures that freeze and the two fixes in use today, reading the paste in small chunks between words or reading it on a second GPU, along with what each one costs.
One big order at a busy counter
The 3D view could not load. The three stages are described below.
01 · The freeze
One order, one very long round
A GPU, the chip that runs the model, writes replies for many people at once, in rounds. Think of lesson 6’s sushi counter: each round, the chef makes one trip to the fridge, then one piece for every seated diner. A piece is a token (a piece of a word, about three-quarters of one on average). With 32 diners, Llama 3.1 8B and an H100, a round takes 5.77 ms.
A newcomer pastes a document. Before the first word of their reply, the GPU must read every token of it once and take notes on each (lesson 4’s KV cache). That reading is prefill. A few hundred tokens of reading hide inside the fridge trip. 8,192 tokens do not: the chef’s hands become the slow part, and that one round takes 134 ms. Nobody seated gets a piece until it ends.
How long one round takes
32 pieces, plus some of a newcomer’s order
- Round time, at best
- A normal round: pieces only
- Order tokens the fridge trip hides
A 100-page document is about 65,536 tokens. Its round takes 2.06 s at best, because every token is compared with every token before it. That is how long every other reply froze.
02 · The fix
Cut the order into chunks
Chunked prefill reads the order a slice at a time. Each round, the chef serves everyone and reads one chunk of 512 tokens. The worst round grows only to 9.88 ms, so no reply freezes.
The newcomer pays. Their order now spans 16 rounds, each with its own fridge trip and everyone else’s pieces, so their time to first token is 148 ms instead of 134 ms. Smaller chunks help the seated diners only until a chunk fits inside the fridge trip. Below that, they only add rounds.
How big a chunk?
32 seated diners, one newcomer with 8,192 tokens
Worst gap between pieces, seated diners
Newcomer’s wait for the first piece
03 · Another way
A second counter
Disaggregation gives the reading its own GPU. One GPU only reads new prompts; another only writes replies, so it never stops for a paste. The reader makes the newcomer’s first token.
The price is the second GPU, and a delivery: the prompt’s notes, 1.07 GB, must be copied to the serving GPU before the newcomer’s second token. Over one 400 Gb/s network link that takes 21.5 ms. The two fixes can also be combined.
Three ways to take the order
- Round of pieces only
- Round that reads the order
- Carrying the order’s notes across
- A piece for every seated diner
- A piece for the newcomer
04 · Playground
Schedule it yourself
Change the number of running replies, the length of the paste and the chunk size, then compare the worst gap with the time to first token. Try a 32K-token paste without chunking.
Schedule the order yourself
Pick a way and move the sliders
decode-only iterationiteration carrying prefill tokensKV-cache copy (bytes over a link)
- Normal gap between tokens
- Worst gap for running users
- Time to first token, new prompt
- New request: first to second token
05 · Glossary
The vocabulary, in one place
- GPU
- The chip that runs the model. Here one NVIDIA H100: the chef at the counter.
- Token
- The unit a model reads and writes: a piece of a word, about three-quarters of one on average. One piece of sushi.
- Round
- One pass of the GPU that gives every running reply one more token; also called an iteration. 5.77 ms at best for 32 replies here.
- Fridge trip
- Reading the model’s weights from GPU memory, which every round has to do once, however many pieces it makes (lesson 1’s belt).
- Prefill
- Reading a prompt: running every one of its tokens through the model once and keeping notes on each, before the first token of the reply.
- KV cache
- The notes a model keeps on every token it has read (lesson 4). 1.07 GB for a 8,192-token prompt to Llama 3.1 8B.
- Gap between tokens
- How long a reader waits between two words of a streaming reply; also called inter-token latency. The worst gap is when a reply visibly freezes.
- Time to first token
- How long after sending a prompt the first token of the reply arrives. Mostly the prefill.
- Chunked prefill
- Reading a long prompt a slice at a time, one slice per round, alongside every running reply’s token.
- Disaggregation
- Reading prompts on one GPU and writing replies on another, then copying each prompt’s KV cache across.
You can now
- Explain why one long pasted prompt freezes every other user's reply on a server that batches by iteration, and compute how long the freeze lasts from a roofline floor.
- Compute how a chunk size trades the running users' worst gap between tokens against the new prompt's time to first token, explain why chunks above the ridge point lengthen every round while chunks below it only add rounds, and choose the largest chunk that meets a gap target.
- Decide when disaggregating prefill and decode onto separate GPUs pays off, and compute the KV-cache copy time it adds before the second token.
Try it in the Serving Lab: Stop long prompts from stalling everyone else.
Open the workbook: the formulas, the exact numbers, graded exercises and the sources behind this page.