Unit 2 of 12 · about 4 hours · explain, derive
Prefill parallelism, the decode dependency and decode shapes
Every phase-separated measurement in this section assumes this split. If you cannot say which work is parallel and which is forced sequential, no TTFT or TPOT number means anything.
You'll be able to
- Explain why prefill exposes sequence parallelism while decode is sequential across generated tokens.
- Derive the per-step shapes of a decode step as matrix-vector-like work against a growing KV cache.
- Predict how prompt length and output length move prefill work versus decode steps.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Prefill parallelism and the decode dependencyEfficiently Scaling Transformer Inference
Section 2.2, Inference Setup (p. 3): prefill as one forward pass parallel over batch and prompt tokens, decode as sequential forward passes with one new token each
- Why decode cannot parallelize across timeEfficiently Scaling Transformer Inference
Section 2.1 (p. 3): decode cannot batch across the sequence, and KV cache load per generated token grows with context and batch
- Decode shapes: one row at a timeHands-On LLM Serving and Optimization - Chapter 5 and the Chapter 4 metrics section
Applying Arithmetic Intensity Analysis to the LLM Prefill and Decode Phases (pp. 183 to 185): Figure 5-16 with for prefill and for decode
Use the shape argument and the regime verdicts only. Recompute the arithmetic-intensity numbers yourself: the printed formula drops parentheses and understates short-sequence values by about half.
Focus on
- Treat the Two regimes: prefill and decode unit as read. This unit adds the per-step shapes and the serving measurement consequences.
- Prefill processes all prompt tokens together, so its work grows with prompt length but parallelizes across the sequence. Decode produces one token per step because each token needs the previous one.
- A decode step loads the full weight set plus the whole KV cache to produce one token and append one entry: matrix-vector-like shapes with almost no reuse.
- The KV cache turns quadratic recomputation into linear per-step reads. Without it, each decode step would rescan the full prefix.
Common mistakes · 3
- Calling decode compute-bound because the model is large. A decode step loads weights and cache for one token, so size raises the bytes moved, not the reuse.
- Assuming prefill cost repeats per token. Prefill runs once; decode steps each read the cache but never redo the full prompt pass.
- Treating batching as parallelism across time. Batching parallelizes across requests; the output steps of one request stay sequential.
Try it yourself
Closed-book, before checking anything
[[[4]]].Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Why can prefill use large matrix multiplies while decode cannot?Answer
Prefill holds all prompt tokens at once, so the multiply has rows. Decode holds one new token because each token depends on the previous one, so .
2.What does one decode step read?Answer
The full weight set once plus the KV cache for all cached positions, to produce one token and append one entry.
3.In the TensorTonic public case with query 0 and keys 1 and 2, why is the output 4?Answer
Both scores are 0, so the weights are 0.5 and 0.5. Then .
4.A batch holds 8 requests that each generate 50 tokens. How many sequential steps does each request need?Answer
50. Batching does not shorten any single request's dependency chain.
Practice
- TensorTonic: Decode Attention with a KV Cache
Implement
kv_cache_decode_step; the public cases check cache extension, uniform two-position attention and two query heads sharing one KV headSolve the public cases by hand first, then implement. The grouped-query head mapping is the bridge to the attention-variant units.
You're done when
You derive the decode-step shapes without notes and your kv_cache_decode_step passes all public cases.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 10 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.
- 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.
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.
- 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.
- 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.
- 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.
- 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.
- 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.