Unit 1 of 10 · about 4 hours · explain, derive
One request through prefill and decode, and the shapes that set its cost
Every later count in this section hangs off one request through these two phases. If the lifecycle is vague, FLOPs, bytes and latency have nothing to attach to.
You'll be able to
- Trace one request from tokenized prompt through prefill to the last decode step, naming the tensors produced and reused at each stage.
- Write a shape manifest for a small model (layers, hidden size, heads, key-value heads, sequence lengths, batch, precision) and point at the entries that set its cost.
- Explain why prefill is compute-bound while decode is bandwidth-bound, using TTFT and per-token latency.
Covers
Read
In this order, only the parts listed
0 of 3 done
- One request: prefill, decode and what to optimizeHow to Scale Your Model - Inference
The Basics of Transformer Inference: What do we actually want to optimize? (TTFT and per-token latency) and A more granular view of the Transformer (linears, attention, everything else)
- Prefill and decode inside a serving engineHow to Scale Your Model - Inference
Designing an Effective Inference Engine: Continuous batching, with its orchestration of prefill and generate steps
Focus on
- Prefill processes the whole prompt at once and writes one key-value pair per prompt token per layer; decode reads the full cache back for each new token and appends one pair.
- TTFT covers scheduling delay plus prompt processing; per-token latency covers one decode step. Offline batch work cares about tokens per second, chat cares about both.
- The manifest entries that move every estimate are layer count, hidden size, head counts with key-value heads, prompt and output lengths, batch size and precision.
Common mistakes · 3
- Treating prefill and decode as one regime. Prefill is a batched parallel pass; decode is serial with cache reads, so one batch size or one roofline cannot cover both.
- Counting tokens without naming prefill versus output tokens. Cost, latency and cache all scale with the two lengths differently.
- Assuming batching speeds up every part. Batching reuses weights across requests, but each request adds its own key-value reads.
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.A 512-token prompt arrives. What does prefill produce, and what does each decode step then read and write?Answer
Prefill processes all 512 tokens in parallel, writes 512 key-value pairs per layer and returns first-token logits. Each decode step reads the full cache, samples one token, and appends its pair.
2.Which manifest entries must a shape manifest contain for this section's estimates?Answer
Layer count, hidden size, feed-forward size, query and key-value head counts with head dimension, prompt and output lengths, batch size, precision, and later the sharding.
3.Why does batching raise linear-layer efficiency but not remove the decode bottleneck?Answer
Batching reuses each weight across requests, but every request adds its own key-value bytes, so cache traffic grows with batch while weight traffic does not.
4.Which latency does a longer prompt directly worsen, TTFT or per-token latency?Answer
TTFT, through prompt processing time. Per-token latency is set by one decode step over weights plus cache.
Practice
- How to Scale Your Model applied inference Question 4: LLaMA 3 inference from downloaded weights
Worked Problems, Question 4, steps 1 to 3 (step 4 optional)
Steps 1 to 3 load the weights and count the parameters, run a plain forward pass with greedy sampling, then add a key-value cache, which splits the loop into one prefill pass and one decode step per token. Step 4 (separate prefill and decode servers) is optional, and step 5 (FlashAttention in Pallas) is TPU work that later sections replace. The question assumes JAX on Colab; any framework works. LLaMA 3 8B is about 16 GB in bf16 and fits a 24 GB GPU; on a 6 GB GPU use Llama 3.2 1B (about 2.5 GB in bf16), and count its tied embeddings once. Both downloads are gated: accept the license on Hugging Face and sign in first.
You're done when
You derive 18.4e9 parameters and 262,144 int8 key-value bytes per token from the manifest without notes, and your steps 1 to 3 implementation samples coherent tokens through one prefill pass and cached decode steps.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 7 more in the section
- How to Scale Your Model - InferenceJAX Scaling Book · primary
Work through the single-request, batching, key-value memory and parallelism arithmetic. Recompute every quantity for the small model and workload used by the track project, keeping symbols, units and assumptions visible.
- EtalonEtalon Authors · support
Read the latency metric definitions, their pitfalls and fluidity-index. Add TTFT and TPOT SLOs to a small load generator, and show why peak token throughput can rank two systems differently from an SLO-qualified rate.
Also in this section
- AI Performance Engineering - Start HereWafer · reference
A curated, dated index. Its Start Here list names the readings this section already assigns; use the later sections only to find a primary source for a later section, never as the reading itself.
- Transformer Inference ArithmeticKipply · required
Reproduce the FLOP, parameter-byte, key-value-byte and communication estimates for dense transformer inference. Compare the formulas with an instrumented framework trace and explain model residuals instead of fitting constants after measurement.
- Efficiently Scaling Transformer InferenceMLSys · required
Study the latency, memory and parallelism model. Use the paper to derive a batch and parallelism crossover for your workload, while reserving distributed execution for GPU-9.
- GPU Microbenchmark and Kernel SuiteRoadmap-Final · support
Adopt the project harness schema, architecture constraints and hardware tiers now. Implement only the environment manifest, independent references, raw-trial storage and the initial operation and byte models in this stage.
- How to Scale Your Model - TrainingJAX Scaling Book · required
Work Some Problems to Work Questions 1 to 3 for training-step parameters, Adam and activation bytes and step time; read Data Parallelism and Fully-Sharded Data Parallelism for the comms-bound batch rule.
- Amdahl's lawWikipedia · reference
Read Definition and Derivation for the overall-speedup formula with the worked share examples; apply it to kernel time shares at fixed workload.
- DistServe: Disaggregating Prefill and Decoding for Goodput-optimized LLM ServingZhong et al. (OSDI 2024) · support
Read Section 1 with Figure 1 for per-GPU goodput: the highest request rate served while a target share of requests meets the TTFT and TPOT SLOs.