Unit 4 of 12 · about 4 hours · derive, measure

Mixing prefill and decode, and chunked prefill to protect decode

One long prefill can freeze every streaming user for hundreds of milliseconds. Chunking bounds each iteration to a token budget, which protects decode without a separate prefill fleet.

You'll be able to

  • Derive per-iteration cost with and without chunking for a stated prompt size and decode batch.
  • Predict how a chunk budget moves TTFT, worst inter-token latency and total prefill time.
  • Measure the chunk-budget tradeoff on your GPU or simulator and name the budget region that holds.

Covers

Prefill and decode mixingChunked prefill and decode protection
1

Read

In this order, only the parts listed

0 of 4 done

  1. Chunked prefill against decode stallsHands-On LLM Serving and Optimization - Chapter 6, batching and scheduling

    Continuous Batching with Chunked Prefill (pp.195-198): Fig 6-6 happy path, Fig 6-7 prefill-priority stall, Fig 6-8 hybrid batching still stalls, Fig 6-9 chunked prefill; the ITL gain against TTFT and prefill-overhead cost; chunk size as part of max batched tokens

  2. Sarathi-Serve stall-free batching and token budgetSarathi-Serve: Tackling Throughput-Latency Tradeoffs in LLM Inference

    Sections 3.2 (arXiv:2403.02310v3) generation stalls with Fig 7; 4.1 chunked-prefills; 4.2 stall-free batching with Algorithm 3, which packs running decodes first, then a partial prefill, then new requests within the token budget; 4.3 token-budget choice from the TBT SLO with tile-quantization and profiling notes

    Skip the pipeline-parallel bubble analysis in Fig 8 and Section 5.3 until the distributed section. Treat Fig 10, Fig 11, Fig 14 and Table 4 numbers as the paper's results on its hardware, not targets.

  3. Stall-free scheduling cost analysisAI Systems Performance Engineering - Chapters 16 and 18, scheduling and admission

    Stall-Free Scheduling (Chunked Prefill) (p.998) with Table 16-5 chunk costs; the per-layer-per-head attention analysis (p.1001), which shows chunking smooths latency without reducing total work

  4. Interference numbers and budget tradeoffLLM Inference: From scratch, one token at a time - Module 05

    Concepts 03 and 04: the four scheduling policies with their costs; the token-budget fill order with decode tokens first; the budget tradeoff table for 256, 2048 and 8192 tokens

2

Focus on

  • Prefill-priority gives best TTFT and worst token-stream stability, decode-priority gives smooth streams and starving prefills, chunking bounds every iteration, and disaggregation removes the conflict with separate hardware.
  • Each iteration fills decode tokens first because they are cheap and latency-critical, then one prefill chunk with the remainder of the budget, so iteration time stays near constant.
  • Chunking changes when attention work runs, not how much: self-attention still performs about QK dot products per layer per head. The mixed iteration is nearly free because decode leaves arithmetic capacity unused.
  • Small budgets steady the stream but underfeed the GPU and add chunk overhead; large budgets approach unchunked stalls. Sarathi-Serve evaluates 512 to 2048; the vLLM 0.31.0 server default is 2048, or 8192 on GPUs with 70 GiB or more other than A100.
Common mistakes · 3
  • Saying chunked prefill reduces total attention work. It replays KV reads per chunk and adds launch overhead, so total work is unchanged or slightly higher; the win is bounded iteration time.
  • Calling chunking a pure TTFT-for-TPOT trade. The mixed iteration moves the same bytes as a decode-only one while doing far more arithmetic, so both phases beat running separately.
  • Setting the smallest budget that fits. Down at 256 tokens the GPU starves and overhead dominates, which the paper's Fig 14 shows as up to about 25% extra prefill cost at chunk 512 against negligible cost at 2048.
3

Try it yourself

Closed-book, before checking anything

Closed-book, price an 8000-token prompt arriving while 32 sequences decode, with the module's Llama-3-8B on H100 model at 70% of 989 TFLOP/s and its bandwidth model: unchunked prefill costs about 186 ms, which is the worst inter-token latency the decoders see. At a 2048-token budget the prefill takes 4 iterations (three 2016-token chunks and a 1952-token tail) of about 47 ms each, total about 188 ms, so worst inter-token latency falls to about 47 ms while TTFT barely moves.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Why does one 8000-token prefill stall 32 decoders for about 186 ms?Answer

    In the module's Llama-3-8B on H100 model, the prefill runs as one compute-bound unit costing FLOP, about 186 ms at 70% of 989 TFLOP/s. The decoders share the same hardware turn by turn, so they emit nothing until it finishes.

  2. 2.At a 2048-token budget with 32 decoders, how many iterations does that prefill take and what bounds each one?Answer

    4 iterations carrying at most 2016 prefill tokens each: three full chunks and a 1952-token tail. Each iteration is bounded by the budget, so the worst inter-token latency is one chunk cost, about 47 ms, instead of the full 186 ms.

  3. 3.Does chunking change the total QK work of the prefill?Answer

    No. Self-attention still performs about dot products per layer per head; chunking only spreads them across iterations. Repeated KV reads add a small overhead.

  4. 4.The budget drops from 2048 to 256. What improves and what degrades?Answer

    Worst inter-token latency improves toward pure decode steps. TTFT stretches over more iterations, throughput dips because 256 tokens underfeed the GPU, and per-chunk overhead grows.

5

Practice

  • LLM Inference Module 05 code lab: chunk-budget sweep

    Code lab: run the budget table for 256, 512, 1024, 2048 and 8192 and record throughput with ITL p50, p99 and max

    Expect ITL max to fall sharply as the budget shrinks with ITL p50 nearly flat and a slight throughput dip at 256. Absolute numbers depend on the sampling seed; keep the table for the ablation unit.

You're done when

You derive the module's 186 ms stall and the 4-iteration chunked schedule without notes and can defend a budget choice from a measured table.

Only when you can do this without notes.

Leads to

Notes

Resources · 4 for this unit, 14 more in the section

Also in this section

  • Orca: A Distributed Serving System for Transformer-Based Generative ModelsUSENIX OSDI · required

    Study iteration-level scheduling and selective batching, including the workload assumptions. Reproduce one scheduling timeline by hand and use it as the correctness baseline for the simulator.

  • Nano-vLLM - Scheduler, Block Manager and Engine LoopGeeeekExplorer · required

    Trace scheduler.py with llm_engine.py and block_manager.py. Add event logging in your own fork or project, explain every transition, and implement one bounded admission or chunking change with regression tests.

  • Mini-SGLang - Scheduler and Overlap SchedulingSGLang Project · required

    Read docs/structures.md, scheduler implementation and tests/core/test_scheduler.py, then compare overlap scheduling with the Nano-vLLM loop. Use the online benchmark only after fixing the same workload and hardware contract.

  • AI Systems Performance Engineering - Continuous Batching LabChris Fregly · support

    Run the Chapter 15 baseline and optimized continuous-batching pair under CPU-safe or available-GPU settings. Inspect the shared comparison contract and re-measure rather than quoting bundled deltas.

  • TensorTonic Language Modeling from Scratch - Simulate Continuous Decode BatchingTensorTonic · support

    Solve after the continuous-batching unit, then compare its admission rule with your own simulator.

  • LLM Inference Systems Laboratory - SchedulerRoadmap-Final · support

    Add token and memory admission, chunking, priority, cancellation, overload and fairness to the simulator, then carry one policy through the real engine path with a reference fallback.

  • vLLM v0.31.0 serve CLI and scheduler argumentsvLLM Project · reference

    Look up scheduler flags and defaults: --max-num-seqs, --max-num-batched-tokens, --max-num-active-seqs, --enable-chunked-prefill, --long-prefill-token-threshold and --scheduling-policy. Runtime defaults vary by GPU class, so check them against the installed v0.31.0 behavior.

  • vLLM v0.31.0 metrics designvLLM Project · reference

    Read the WAITING, RUNNING and PREEMPTED request states and the queue, inference, prefill and decode interval definitions. Swapped-preemption metrics are legacy in v1. The exported counters, including vllm:num_requests_waiting_by_reason and vllm:num_preemptions, are listed on the Production Metrics page, https://docs.vllm.ai/en/v0.31.0/usage/metrics/

  • vLLM v0.31.0 benchmark CLIvLLM Project · support

    Use vllm bench serve with --request-rate for offered-load sweeps, and vllm bench throughput and vllm bench latency for offline endpoint and phase baselines. Fix the dataset, lengths, request rate, warmup and sampling, and keep raw per-request results; do not copy example rates as targets.

  • vLLM v0.31.0 online servingvLLM Project · reference

    Read the /abort_requests endpoint and the Responses cancel API for request cancellation. Verify client-disconnect aborts on your engine; the page does not cover them.

  • vLLM v0.31.0 LoRA adaptersvLLM Project · reference

    Read per-request LoRARequest serving, server-wide --lora-modules with max_loras and max_lora_rank, and why adapter-slot pressure rewards grouping same-adapter requests.

  • vLLM v0.31.0 structured outputsvLLM Project · reference

    Read the xgrammar and guidance backends, the choice, regex, json, grammar and structural_tag options in StructuredOutputsParams, and the --structured-outputs-config.backend flag.

  • vLLM v0.31.0 multimodal inputsvLLM Project · reference

    Read limit_mm_per_prompt, content hashing with multi_modal_uuids in Cached Inputs, and the video pruning flags as examples of encoder-side scheduling cost.

  • TensorTonic Language Modeling from Scratch - Route Cache-Aware Prefill RequestsTensorTonic · support

    Solve after the affinity unit: assign each request to a feasible worker by longest cached prefix, lowest projected uncached load, greatest remaining capacity, then lowest worker id, with -1 when infeasible.