Unit 7 of 12 · about 4 hours · derive, implement
Scheduler state, resource, progress and bounded-wait invariants
Policies change, but the invariants do not. Every admission, preemption, cancel and finish path must preserve the same ownership and progress rules, or one path corrupts the rest.
You'll be able to
- Define state, resource, progress and bounded-wait invariants for a continuous-batching scheduler.
- Derive the no-overcommit admission rule from reserved blocks, running growth and free capacity.
- Implement per-iteration invariant checks that catch leaks, double frees and stalled waiters.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Nano-vLLM scheduler as a checkable state machineNano-vLLM - Scheduler, Block Manager and Engine Loop
Schedulerwaiting and running deques withSequenceStatusinnanovllm/engine/sequence.py; allocate, append, share, preempt and free transitions innanovllm/engine/block_manager.py;postprocessfinishing withrunning.removeanddeallocate - Mini-SGLang scheduler structure and lifecycleMini-SGLang - Scheduler and Overlap Scheduling
docs/structures.mdSystem Architecture with Key Components and the 8-step Data Flow request lifecycle;Scheduler._schedule_next_batchwithPrefillManagerandDecodeManagerinpython/minisgl/scheduler/Read the rank-0 loop only. Skip the multi-GPU broadcast, ZMQ and NCCL parts on one GPU.
- Orca reservation and progress propertiesOrca: A Distributed Serving System for Transformer-Based Generative Models
Section 4.2 Algorithm 1 reservation of
max_tokensslots per new request againstn_slots; the iteration-level FCFS property that an earlier arrival runs the same or more iterations than a later one
Focus on
- Ownership: every block is free or referenced by live requests with a matching count, and every RUNNING request holds a non-empty block table. Freeing checks the count; shared prefix blocks survive one holder's release.
- No overcommit: reserved plus running growth fits free capacity before any admit. Reservation is the static form; vLLM's runtime form is the
watermarkheadroom and the full-input-length admission check,scheduler_reserve_full_isl, in its scheduler configuration. - Progress: every iteration retires finished requests, frees their blocks and advances at least one waiter or token. Steady completions with a never-served waiter still violate progress.
- Bounded wait: aging or reserved capacity turns FCFS order into a wait bound. State the bound as a number of iterations or requests served, not as good intentions.
Common mistakes · 3
- Checking invariants only on admission. Preempt, finish and abort paths move the same state, so the check must run after every transition, including the error paths.
- Confusing a positive reference count with exclusive ownership. Shared prefix blocks outlive one holder, so freeing must decrement and test rather than release outright.
- Reading steady throughput as progress. A scheduler can complete easy requests forever while one waiter starves, which throughput hides and the bounded-wait check exposes.
Try it yourself
Closed-book, before checking anything
check_invariants for a Nano-vLLM-style scheduler and run it after every schedule, preempt and postprocess call: reserved plus running blocks fit total blocks, every RUNNING sequence has a non-empty block table, every FINISHED sequence left running with freed blocks, and the longest waiter advances within the stated bound. Show it catching a finish path that forgets deallocate.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.State the no-overcommit rule in one sentence with its three quantities.Answer
Admit only when reserved blocks plus running growth fits free blocks. The three quantities are current reservations, worst-case growth of running requests, and free capacity.
2.A finish path removes the sequence from running but skips
deallocate. Which check catches it?AnswerThe ownership check: the finished sequence still holds blocks that no live request references, so they never return to the free list. Held plus free still equals total, so a count check alone misses it.
3.Why must the invariant check run after preemption, not just admission?Answer
Preemption moves blocks back to free and the request back to waiting with reset counters. A wrong requeue or a missed counter reset corrupts the same state admission relies on.
4.Throughput is flat at a healthy value but the longest waiter never advances. Which invariant fails?Answer
Bounded wait. Progress of completions does not imply progress of every waiter; the wait bound needs its own check with the waiter's age.
You're done when
Your checks run green on a randomized history and catch an injected missing-free and an injected stuck waiter.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 15 more in the 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.
Also in this 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.
- 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).
- 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 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. - 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.