Unit 2 of 12 · about 4 hours · explain, derive
Waiting, running and preempted sets, token budgets and runner limits
Every iteration must fit both a request-count limit and a token limit. Misreading either knob admits batches that run out of memory or leaves paid capacity idle.
You'll be able to
- Name the WAITING, RUNNING, PREEMPTED and terminal request sets and the event that moves a request between them.
- Derive whether a waiting request fits from active KV tokens, its new prefill tokens, decode tokens, batch limits and memory reserve.
- Predict admit, defer or preempt outcomes for a small request mix under stated budgets.
Covers
Read
In this order, only the parts listed
0 of 4 done
- Two knobs: max batch size and max batched tokensHands-On LLM Serving and Optimization - Chapter 6, batching and scheduling
Continuous Batching for LLM Online Inference (pp.194-195): Fig 6-5 relating max batch size, max number of tokens and max model length, with prefill bound by token count and decode by batch size; Example 6-1
vllm serve Qwen/Qwen2.5-7B-Instruct --max-num-batched-tokens 4096 --max-num-seqs 128 - Nano-vLLM request sets and budget loopNano-vLLM - Scheduler, Block Manager and Engine Loop
Scheduler.schedule: the waiting and running deques withSequenceStatusinnanovllm/engine/sequence.py; the prefill loop overmax_num_seqsandmax_num_batched_tokenswith chunking for the first sequence only; the decode loop withcan_append,may_appendandpreempt - vLLM scheduler flags and defaultsvLLM v0.31.0 serve CLI and scheduler arguments
SchedulerConfig arguments:
--max-num-seqs,--max-num-batched-tokens,--max-num-active-seqs,--enable-chunked-prefill,--long-prefill-token-thresholdand--scheduling-policywithfcfsandpriority - Orca admission with reservationOrca: A Distributed Serving System for Transformer-Based Generative Models
Section 4.2 Algorithm 1:
Selectadmits at mostmax_bsrequests in arrival order and reservesmax_tokensslots per new request againstn_slots, which guarantees its future KV fits
Focus on
- WAITING holds admitted-but-unscheduled and preempted requests, RUNNING holds scheduled ones, and terminal states end ownership and free blocks. PREEMPTED returns to WAITING, never directly to RUNNING.
- The token budget counts every scheduled token: one per decode sequence plus each prefill chunk. The sequence count caps live requests. A batch must satisfy both at once.
- Nano-vLLM schedules prefill first and returns early when any prefill is scheduled, so decode-only iterations happen only with no schedulable prefill. Only the first waiting sequence may take a partial chunk.
- Conservative admission reserves the full footprint up front, as Orca does with
max_tokens. Optimistic admission packs tighter and relies on preemption, which the next unit covers.
Common mistakes · 3
- Treating the two knobs as phase-exclusive. Decode tokens also consume the token budget (one each), and long prefills also consume sequence slots; either limit can bind in any iteration.
- Reading
--max-num-seqsas the only running cap. When--max-num-active-seqsis set, vLLM admits at most that many into RUNNING and parks the rest in deferred waiting; unset, the cap ismax_num_seqs. - Copying Example 6-1 values as defaults. Installed v0.31.0 defaults vary by GPU class and context, so derive feasibility from the flags on your engine, not from the book example.
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.What moves a request from RUNNING to PREEMPTED, and where does it go next?Answer
Memory pressure during decode admission: the scheduler reclaims its blocks and requeues it at the front of WAITING. It must be rescheduled and recomputed, never resumed in place.
2.A batch holds 4 decode sequences under a 2048-token budget. How many tokens of prefill chunk fit?Answer
tokens. Each decode sequence contributes one token to the budget.
3.Why does Orca reserve
max_tokensslots for a new request instead of its prompt length?AnswerOutput length is unknown at admission, so only the request's maximum possible footprint guarantees its future KV fits. The cost is lower admission under uncertain lengths.
4.Nano-vLLM has a schedulable prefill and free decode slots. Does the decode loop run?Answer
No.
schedulereturns as soon as any prefill is scheduled, so decode-only iterations run only when no waiting prefill fits.
You're done when
You derive admit or defer for a new mix without notes and can name the set each request sits in after every scheduler step.
Only when you can do this without notes.
Leads to
Notes
Resources · 4 for this unit, 14 more in the section
- Hands-On LLM Serving and Optimization - Chapter 6, batching and schedulingChi Wang and Peiheng Hu (O'Reilly) · primary
Read "Request Batching and Scheduling-Level Optimizations" in Chapter 6: dynamic, continuous and chunked-prefill batching. Rebuild each policy in a small event simulator and state the user-visible metric it can improve or harm.
- 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.
- 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-thresholdand--scheduling-policy. Runtime defaults vary by GPU class, so check them against the installed v0.31.0 behavior.
Also in this section
- Sarathi-Serve: Tackling Throughput-Latency Tradeoffs in LLM InferenceMicrosoft Research · required
Focus on chunked prefills, stall-free batching and the throughput-latency trade-off. Implement a fixed-token chunk policy and preserve a workload where chunking loses to the simpler scheduler.
- AI Systems Performance Engineering - Chapters 16 and 18, scheduling and admissionChris Fregly (O'Reilly) · required
Read "Dynamic Batching, Scheduling, and Routing" in Chapter 16 and "SLO-Aware Request Management and Fault Tolerance" in Chapter 18 (early rejection, quality of service).
- 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: From scratch, one token at a time - Module 05Tushar Amrit · support
Work the Batching module's arithmetic and checkpoint before the static and continuous batching units.
- 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 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_reasonandvllm: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 servewith--request-ratefor offered-load sweeps, andvllm bench throughputandvllm bench latencyfor 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_requestsendpoint 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
LoRARequestserving, server-wide--lora-moduleswithmax_lorasandmax_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,grammarandstructural_tagoptions inStructuredOutputsParams, and the--structured-outputs-config.backendflag. - vLLM v0.31.0 multimodal inputsvLLM Project · reference
Read
limit_mm_per_prompt, content hashing withmulti_modal_uuidsin 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.