Lesson 12: Chunked prefill and disaggregation · Workbook
Chunked prefill and disaggregation: the workbook
The full working behind lesson 12. Two latencies a user feels, the stall in numbers, why a decode round has room to spare, the chunk-size trade-off, the cost of moving the KV cache between GPUs, and seven exercises to check yourself.
A serving engine with continuous batching runs in iterations. In each one, every running request gets one more token, and new requests can join. That design keeps the GPU busy, but it ties every user to the slowest piece of work in the iteration. When a long prompt arrives, its prefill lands in an iteration, and the next token for everyone else waits until that prefill finishes.
Users see this as a stutter: text streams smoothly, then freezes, then resumes. This lesson puts numbers on that stall and walks through the two fixes production systems use. One splits the prefill into chunks small enough to share iterations with decodes. The other moves prefill to different GPUs entirely. Both work, and both have a price.
This lesson builds on prefill and decode, the KV cache and continuous batching.
Two latencies a user feels
A streaming chat user feels two delays.
- Time to first token (TTFT): from sending the request to seeing the first token. It is mostly the prompt’s prefill.
- Inter-token latency (ITL): the gap between two consecutive tokens of the same response. Papers also call it time between tokens (TBT) or, averaged over a response, time per output token (TPOT).
A server that keeps the average ITL low can still have terrible worst-case gaps. The worst gap is what makes a response visibly freeze, so it is what we track here.
The stall, in numbers
Take Llama 3.1 8B in BF16 on one H100, the setup from lesson 1. 32 users are decoding, each with 1,024 tokens of context. One decode iteration reads all the weights once and every user’s KV cache, and its roofline floor is 5.77 ms. That is the normal gap between tokens.
Now an 8,192-token prompt arrives, say a user pasting a long document. A simple scheduler runs the whole prefill in the next iteration, alongside the 32 decodes. That iteration needs 134 ms, so all 32 users wait 134 ms for their next token, about 23 times the normal gap. The new user gets a first token after the same 134 ms.
Every number in this lesson is a roofline floor computed the same way as in lesson 1. Real kernels are slower, and the ratios matter more than the absolute values.
The Sarathi-Serve paper calls this a generation stall and reports that in vLLM, a stall can last over several seconds. Its measurements show that putting a full prefill into a batch of decodes raised the time between tokens by up to 28.3 times compared with a decode-only batch. DistServe measures the same effect and calls it prefill-decoding interference: adding one prefill to a decoding batch slows both, and the slowdown grows with prompt length.
Some schedulers pause decodes while they run prefills, and others let decodes ride along. Either way, running users wait for the whole prefill. The fix has to make prefills smaller, or move them somewhere else.
Why a decode iteration has room to spare
Lesson 1 showed that a decode iteration is memory-bound: it reads 15.0 GB of weights to do 32 tokens of arithmetic, while the H100’s ridge point is about 295 FLOPs per byte, which for weight matmuls means roughly 295 tokens. The tensor cores are mostly idle during decode, waiting on memory.
So you can add prefill tokens to a decode iteration almost for free, as long as the total stays near the ridge. Add 256 prompt tokens to the 32-user iteration and the floor rises by only 8.76 µs, from 5.77 ms, which is 0.2%, while the iteration does 9 times as many tokens of work. The weights were going to be read anyway. The 2023 SARATHI paper called this piggybacking decodes on a prefill chunk, and showed up to 10 times higher decode throughput for LLaMA-13B on an A6000 GPU.
Chunked prefill
Chunked prefill splits a long prompt into pieces of tokens and processes one piece per iteration. Each iteration carries all the running decodes plus one chunk. When the last chunk finishes, the request emits its first token and becomes an ordinary decoding request.
Sarathi-Serve frames this as a token budget: the most tokens an iteration may process while still meeting the inter-token latency target. Its scheduler, which it calls stall-free batching, fills each iteration in a fixed order:
- Every running decode, one token each.
- The next chunk of any prefill already in progress.
- Chunks of new requests, only if budget is left.
Because decodes always come first and the budget caps each iteration’s work, a long prompt can never stretch an iteration beyond the budget. The chunk size is whatever budget remains after the decodes. The simulator in this lesson holds the chunk size fixed instead, so each chunked iteration processes the running decodes plus prompt tokens. A token budget corresponds to .
There is one subtlety. Each chunk’s tokens attend to every earlier token of their prompt, so chunk must read the KV cache written by chunks to . Split a prompt into chunks and those reads add up to
that the unchunked prefill never had to re-read. The arithmetic is unchanged, but bytes go up. Sarathi-Serve finds that attention over chunks stays compute-bound even at small chunk sizes, so this is a modest cost in practice.
The price: time to first token
Chunking moves the prefill’s cost rather than removing it. Here is the 8,192-token prompt arriving at the same 32-user server, at four chunk sizes:
| Chunk size | Iterations for the prompt | Worst gap, running users | TTFT, new prompt |
|---|---|---|---|
| 256 | 32 | 6.13 ms | 191 ms |
| 512 | 16 | 9.88 ms | 148 ms |
| 1,024 | 8 | 19.1 ms | 139 ms |
| 2,048 | 4 | 37.2 ms | 136 ms |
| none | 1 | 134 ms | 134 ms |
Smaller chunks protect the running users: at 256 tokens the worst gap is 6.13 ms, close to the normal 5.77 ms. But the new user waits longer for a first token: 191 ms instead of 134 ms, 42% more.
The reason is the same weight read that made piggybacking cheap. Every iteration reads all 15.0 GB of weights, whether it carries 256 prompt tokens or 8,192. An unchunked prefill reads them once. A prefill in 32 chunks spans 32 iterations, and each one takes at least the memory floor of a decode iteration. Below the ridge, each iteration is paying for a full weight read but only using part of the compute it could have. Go down to 128-token chunks and the prompt needs 64 iterations and a TTFT of 382 ms, almost 3 times the unchunked figure, with no further gain for running users.
The roofline floor also leaves out costs that grow with the number of iterations: kernel launches, scheduling, and small matrices that run below peak. Sarathi-Serve measured the total overhead on real hardware, including all of these effects. For Yi-34B on two A100s, chunking added at most about 25% to prefill time at a chunk size of 512, and almost nothing at a token budget of 2,048. It also found that matrix shapes matter: a chunk of 257 tokens could take 32% longer than 256, because GPUs compute matmuls in fixed-size tiles and one extra token can mean a whole extra tile. Pick chunk sizes that are multiples of the tile size.
In its evaluation, Sarathi-Serve used a token budget of 512 under strict latency targets and 2,048 under relaxed ones. Against vLLM it reports 2.6 times higher serving capacity for Mistral-7B on one A100, and up to 3.7 times for Yi-34B on two A100s, while meeting tail-latency targets.
Try it
The figure simulates the scenario above, iteration by iteration. Under the colocated policies the top lane shows the GPU’s iterations; under disaggregation there is one lane per GPU plus a lane for the KV-cache copy. Blue-gray blocks are decode-only; pink blocks also carry prefill tokens. A teal bar is the KV-cache copy, since it is pure data movement. The lane labelled Running users marks each moment they get a token, with the worst gap highlighted. The New request lane runs from the prompt’s arrival through its first tokens.
Switch between policies and watch the two readouts that matter: the worst gap for running users and the new prompt’s TTFT. The time axis is the same for all three policies, so you can compare them directly.
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
Disaggregation: separate GPUs for each phase
Chunking makes prefills and decodes share iterations more fairly. Disaggregation stops them sharing at all. One pool of GPUs runs only prefills, and another runs only decodes. When a prefill finishes, the prompt’s KV cache is copied to a decode GPU, and that request continues decoding there.
Two papers published in 2024 developed this idea. DistServe (OSDI 2024) argues that colocating the phases causes interference and also couples their resource choices: one GPU configuration has to serve both a compute-bound phase that cares about TTFT and a memory-bound phase that cares about ITL. With separate pools, each phase gets its own parallelism and its own number of GPUs. DistServe reports serving 7.4 times more requests, or meeting a 12.6 times tighter latency target, compared with existing systems, with over 90% of requests within their latency limits. Splitwise (ISCA 2024) starts from the observation that token generation underuses compute even with good batching, so it does not need the newest GPUs. It explores clusters where the decode machines use older or power-capped GPUs, and reports either 1.4 times higher throughput at 20% lower cost, or 2.35 times more throughput at the same cost and power.
In the simulation, disaggregation gives the running users a worst gap of 6.12 ms, almost the normal 5.77 ms. The small increase is one more sequence joining the batch, with 8,192 tokens of context to read. The new prompt’s TTFT is 133 ms, the prefill running alone on its own GPU, which is a little faster than the unchunked case because no decodes ride along.
That comparison is not free. The disaggregated setup uses two GPUs where the others used one. The fair question is how many requests per GPU each design can serve within its latency targets, and that depends on the traffic. Serving metrics like this are the subject of lesson 14.
The cost of moving the cache
Disaggregation adds a step: the prompt’s KV cache has to cross from the prefill GPU to the decode GPU. The copy is pure data movement, so its floor is bytes over bandwidth:
where is the prompt length. For Llama 3.1 8B in BF16, the KV cache is 131,072 bytes per token (lesson 4 derives this), so the 8,192-token prompt’s cache is 1.07 GB.
Two links matter in practice:
- NVLink, within a server. NVIDIA lists 900 GB/s for the H100 SXM, but that counts both directions. Each H100 has 18 links at 25 GB/s per direction, so a one-way copy gets at most 450 GB/s. The copy takes 2.39 ms.
- InfiniBand, between servers. One InfiniBand network adapter (NVIDIA’s ConnectX-7, at the 400 Gb/s generation called NDR) carries 400 Gb/s, which is 50 GB/s. The copy takes 21.5 ms.
Over InfiniBand that is about 4 normal decode gaps. A bigger model makes it worse: Llama 3.1 70B stores 327,680 bytes per token, so the same prompt’s cache is 2.68 GB and takes 53.7 ms over one InfiniBand adapter.
The copy does not delay the first token, because the prefill GPU produces it. It delays the second. In the simulation, the new user’s gap between first and second token is 29.0 ms over InfiniBand and 11.6 ms over NVLink. That assumes the copy starts only after the prefill finishes.
The gap is more than the copy. The request can only join the decode batch at the next iteration boundary after its cache lands, and then it needs one decode iteration (6.12 ms) to produce the token. Over NVLink, most of the gap is that wait and that iteration, not the copy.
Real systems hide most of the copy. Splitwise sends each layer’s KV cache as soon as that layer’s prefill finishes, so the copy overlaps with the remaining layers’ compute. It switches to this layer-wise transfer for prompts of 512 tokens or more on its H100 setup, and keeps the simple copy for shorter ones. Splitwise measures the visible transfer time at under 7% of prompt computation time. In its end-to-end test, layer-wise transfer added 16.5% to the second token’s latency, against 64% for a serialized copy. DistServe gives a sizing example: one 512-token request on OPT-66B has about a 1.13 GB KV cache, so 10 requests per second need about 90 Gbps of bandwidth. DistServe places prefill and decode instances so the copy can use NVLink inside a node, and on its testbed the transfer was under 0.1% of total latency.
One more property helps disaggregation as prompts grow. Copy time grows in proportion to prompt length, but prefill attention grows with its square. In this model, the InfiniBand copy for the 8,192-token prompt is 16% of its prefill time, and that fraction falls as prompts get longer.
Choosing between them
The two fixes are not exclusive. DistServe notes that chunked prefill mitigates interference but cannot eliminate it, since every iteration that carries a chunk is slower than a pure decode iteration. Disaggregation removes the interference but adds a network copy, a second pool to size, and a scheduler that must balance two kinds of GPUs.
A reasonable mental model:
- Chunked prefill is a scheduling change on the GPUs you already have. It trades some TTFT for a bound on ITL, and the chunk size, or token budget, is the dial.
- Disaggregation is a deployment change. It gives each phase its own hardware and configuration, at the cost of moving KV caches and running two pools. It tends to pay off when latency targets are strict on both phases and traffic is large enough to keep both pools busy.
The next lesson, tensor and pipeline parallelism, covers the parallelism choices that disaggregation lets you make separately for each phase.
Check yourself
Every answer below uses the same roofline model as the figures: Llama 3.1 8B in BF16 on H100s, best-case floors.
Exercise 1. Llama 3.1 70B stores 327,680 bytes of KV cache per token in BF16. A 4,096-token prompt finishes prefill on one server and is copied to another over one 400 Gb/s InfiniBand adapter. How long does the copy take at best?
Show the worked answer
The cache is 4,096 × 327,680 bytes = 1.34 GB. At 50 GB/s that is 26.8 ms. Unless it overlaps with prefill compute, it lands between the request’s first and second tokens.
Exercise 2. The server has 64 users decoding at 1,024 tokens of context, and a 16,384-token prompt arrives. Without chunking, how long do the running users wait for their next token, compared with the normal gap? Enter the wait.
Show the worked answer
The iteration that carries the whole prompt takes 303 ms, about 43 times the normal gap of 7.05 ms. The prompt doubled and the stall grew 2.3 times: the matmul work doubles, and the attention part, which grows with the square of the prompt length, quadruples. That is why it is a bit more than double. The extra decodes add little.
Exercise 3. Back to 32 users and an 8,192-token prompt. You want the worst gap for running users to stay within twice the normal gap. Among chunk sizes that are powers of two, which is the largest that meets the target, and what TTFT does the new prompt get? Enter the chunk size.
Show the worked answer
A chunk of 512 tokens, which keeps the worst gap at 9.88 ms against a normal 5.77 ms. The new prompt’s TTFT is 148 ms, compared with 134 ms unchunked. Using the largest chunk that meets the ITL target keeps the TTFT cost as small as possible.
Exercise 4. Why does shrinking the chunk from 256 to 128 tokens hurt TTFT so much while doing nothing for running users?
Show the answer
At 256 prompt tokens plus 32 decodes, an iteration handles 288 tokens, just below the ridge of about 295, so it is memory-bound: its time is mostly the weight read. A smaller chunk cannot shrink the weight read, so running users gain nothing. The worst gap is 6.13 ms at 256 and 6.17 ms at 128, and at both sizes it comes from the iteration after the new user joins and its long cache must be read, not from a chunk. Meanwhile the number of iterations doubles, from 32 to 64, and each pays the full weight read. TTFT goes from 191 ms to 382 ms.
Exercise 5. Disaggregated, with Llama 3.1 8B and a serialized copy over one 400 Gb/s InfiniBand adapter. Above what prompt length does the copy take longer than one normal decode gap of the 32-user server?
Show the worked answer
The copy takes one normal gap (5.77 ms) when the prompt is 5.77 ms × 50 GB/s ÷ 131,072 bytes per token, about 2,199 tokens. Above that length, the copy alone costs more than a whole normal gap, on top of the decode iteration the second token needs anyway, so the new user’s first-to-second-token gap grows to more than twice the normal gap. That is the motivation for overlapping the copy with prefill. Splitwise does this layer by layer, and on its H100 setup it switches to it from 512 tokens.
Exercise 6. From lesson 1: Llama 3.1 8B in BF16 on one H100, with a single user decoding at 1,024 tokens of context. What is the roofline floor for one decode step? Enter it in milliseconds, then compare it with the 5.77 ms normal gap for 32 users.
Show the worked answer
4.52 ms. The step is memory-bound: it reads 15.0 GB of weights at 3.35 TB/s, plus one small KV cache. Serving 32 users costs only 5.77 ms, because the weights are read once for the whole batch; the extra time is mostly their 32 KV caches. Both steps are memory-bound, so the tensor cores sit mostly idle. That idle arithmetic in every decode iteration is what chunked prefill fills with prompt tokens.
Exercise 7. From lesson 4: Llama 3.1 8B stores 2 × 32 layers × 8 KV heads × 128 numbers × 2 bytes of KV cache per token in BF16. How large is the cache for the 8,192-token paste in this lesson, in MB? That is what disaggregation has to copy.
Show the worked answer
1,074 MB: 131,072 bytes per token × 8,192 tokens = 1,073,741,824 bytes. Over one 400 Gb/s InfiniBand adapter that takes 21.5 ms.
Sources
- Agrawal, Kedia, Panwar, Mohan, Kwatra, Gulavani, Tumanov, Ramjee (2024). Taming Throughput-Latency Tradeoff in LLM Inference with Sarathi-Serve. OSDI 2024. arXiv:2403.02310. Generation stalls, chunked prefills, stall-free batching, token budgets; USENIX page at usenix.org/conference/osdi24/presentation/agrawal
- Agrawal, Panwar, Mohan, Kwatra, Gulavani, Ramjee (2023). SARATHI: Efficient LLM Inference by Piggybacking Decodes with Chunked Prefills. arXiv:2308.16369. The earlier paper that introduced chunked prefills and decode-maximal batching
- Zhong, Liu, Chen, Hu, Zhu, Liu, Jin, Zhang (2024). DistServe: Disaggregating Prefill and Decoding for Goodput-optimized Large Language Model Serving. OSDI 2024. arXiv:2401.09670
- Patel, Choukse, Zhang, Shah, Goiri, Maleki, Bianchini (2024). Splitwise: Efficient Generative LLM Inference Using Phase Splitting. ISCA 2024. doi:10.1109/ISCA59077.2024.00019. arXiv version linked; layer-wise KV-cache transfer is in Section IV-C
- NVIDIA H100 product page, specifications table. H100 SXM: NVLink 900 GB/s, PCIe Gen5 128 GB/s
- NVIDIA (2022). NVIDIA Hopper Architecture In-Depth. 18 NVLink links per H100, 25 GB/s each way per link, 900 GB/s total
- NVIDIA ConnectX-7 adapter cards user manual, introduction. NDR InfiniBand at 400 Gb/s per port
- Llama 3.1 8B model card (Meta)