Unit 4 of 12 · about 4 hours · derive, measure
Batch size, sequence shape, chunked prefill and phase interference
Batching is the main lever on serving cost, and naive batching taxes decode latency. Chunked prefill exists because one long prompt once stalled every other request on the server.
You'll be able to
- Explain how batch size and sequence shape move work between bandwidth-bound and compute-bound execution.
- Explain chunked prefill and how unchunked prefills stall ongoing decodes.
- Predict the interference a long-prompt request inflicts on concurrent decode streams.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Chunked prefill and stall-free batchingTaming Throughput-Latency Tradeoff in LLM Inference with Sarathi-Serve
Sarathi-Serve arXiv:2403.02310v3, Section 4 Design and Implementation: 4.1 Chunked-prefills, 4.2 Stall-free batching and 4.3 Determining Token Budget; the prefill-prioritizing schedule that stalls ongoing decodes as the negative example
Skip the pipeline-parallel sections and the Evaluation capacity numbers. Treat the reported capacity gains as the paper's result on its hardware, not as your target.
- Batch size moves the bottleneckEfficiently Scaling Transformer Inference
Section 4.1 with Figures 6 and 7 (p. 8): decode stays on the stationary layout while prefill switches layout by tokens per batch
- Chunked prefill as the preemption fixTime to First Token - Measurement and Load-Testing Sessions
Week 5 decision point: when TTFT spikes and waiting requests climb before the target concurrency, stop and tune
--max-num-seqs,--gpu-memory-utilizationand chunked prefill first
Focus on
- A batch mixes prefill tokens and decode tokens in one iteration. Prefill tokens bring large parallel work; decode tokens bring one step each with their full cache reads.
- An unchunked long prefill occupies iterations that ongoing decodes also need, so their inter-token gaps spike. Chunked prefill splits the prompt into near-equal chunks that fit beside decodes.
- The token budget caps prefill tokens per iteration. A smaller budget protects decodes but stretches the new request's TTFT: that trade-off is the whole design.
- Splitting is not free. Each chunk's attention rereads the KV cache of all earlier chunks, and small chunks run at lower GPU utilization; Section 4.3 and the Overhead of chunked-prefills ablation (Section 5.4.1) show the cost.
Common mistakes · 3
- Assuming bigger batches always help both phases. Bigger batches raise throughput but an admitted long prefill still stalls the decodes it shares iterations with.
- Treating the chunk budget as a latency knob for the new request only. The budget sets every concurrent decode's worst gap too.
- Reading the paper's capacity multiples as expected gains. They hold for the paper's models, hardware and tail-latency constraints; your matrix decides yours.
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Why does one long prompt hurt ongoing decodes under prefill-prioritizing scheduling?Answer
Its full prefill occupies whole iterations, so decode steps wait. The waits appear as inter-token gaps on unrelated requests.
2.What does chunked prefill change about that iteration?Answer
Only one chunk of the prompt joins each iteration, bounded by the token budget, so decodes keep making progress every iteration.
3.What does a smaller token budget cost?Answer
The new request's prefill spreads over more iterations, so its TTFT grows. Decode protection is paid in prefill latency.
4.Decode batch grows from 1 to 32 at fixed lengths. What happens to per-step time and to tokens per second?Answer
Per-step time rises sublinearly while weight reuse climbs, so tokens per second rise until the work turns compute-bound or hits capacity.
Practice
- Hands-On LLM Serving companion: batching with vLLM
Run the cell that times the four listed prompts as one vLLM batch against the same prompts one by one, then add per-request timing yourself; the notebook records only the two totals
The notebook compares batched with one-by-one generation, not chunked prefill. Use it for batch-size effects and read chunking in Sarathi-Serve.
You're done when
You can predict, before running, whether a new arrival stalls concurrent decodes at a given chunk budget, and your batching measurements agree.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 9 more in the section
- Efficiently Scaling Transformer InferenceMLSys · primary
Read the prefill and decode cost, memory, batching and parallelism sections. Use its models to predict phase bottlenecks before measurement and record which assumptions fail on the selected model, engine and GPU.
- Time to First Token - Measurement and Load-Testing SessionsPatchy631 · support
Use only Week 3 and Week 5 as a practical checklist for Prometheus, Grafana and request-rate sweeps. Follow the competency gate rather than its calendar, and replace fixed concurrency goals with your hardware-safe saturation range.
- Taming Throughput-Latency Tradeoff in LLM Inference with Sarathi-ServeAgrawal et al. · support
Read arXiv v3, Chunked-prefills and Stall-free batching for the chunked-prefill vocabulary and the prefill-decode interference argument, and Overhead of chunked-prefills for the cost of splitting. Treat the reported capacity gains as the paper's result on its hardware.
Also in this section
- AI Systems Performance Engineering - Chapter 16, profiling and debugging inferenceChris Fregly (O'Reilly) · required
Read the first part of Chapter 16: monitoring system metrics, profiling with Nsight Systems and Nsight Compute, the troubleshooting recipes and debugging correctness. Treat every quoted speedup as a hypothesis for your own hardware.
- AI Systems Performance Engineering companion code - Chapter 16Chris Fregly · required
Begin with inference_profiling.py, inference_server_load_test.py, monitoring_bundle.py and runtime scheduler instrumentation. Run only hardware-compatible targets, retain raw artifacts and re-measure all bundled expectations on your own device.
- Hands-On LLM Serving and Optimization - Chapter 5 and the Chapter 4 metrics sectionChi Wang and Peiheng Hu (O'Reilly) · required
Read Chapter 5 on GPU specifications, model loading and arithmetic intensity in the prefill and decode phases, and "Measuring Performance in LLM Serving" in Chapter 4. Turn each claim into a prediction for one short-prompt and one long-prompt workload.
- vLLM Metrics Design and Production MetricsvLLM Project · required
Map scheduler and cache events to exported request, queue, TTFT, inter-token and generation metrics. The link is pinned to v0.31.0; check the definitions against the version you install.
- vLLM Benchmark CLI (v0.31.0)vLLM Project · required
Use
vllm bench latency,vllm bench throughputandvllm bench servefor phase and endpoint baselines. Fix the dataset, lengths, request rate, warmup and sampling, and keep the raw per-request results. - TensorRT-LLM Benchmarking GuideNVIDIA · support
Use Before Benchmarking and Online Serving Benchmarking as a reproducibility checklist. Borrow environment capture and startup metrics even if TensorRT-LLM is not the selected engine.
- LLM Inference Systems Laboratory - InstrumentationRoadmap-Final · support
Complete Milestone B: correlate request, iteration, KV, scheduler, kernel and resource events, quantify observer overhead, and retain raw arrival and completion records.
- py-spy - sampling profiler for PythonBen Frederick · support
Use py-spy record to take CPU flame graphs of tokenizer and detokenizer work, and py-spy top to watch the hot functions live. Launch the profiled process under py-spy.
- vLLM SamplingParams API (v0.31.0)vLLM Project · reference
Look up
temperature,seed,max_tokens,ignore_eos,stopandstop_token_idsdefaults when you fix the output contract. The link is pinned to v0.31.0; check the fields against the version you install.