Unit 3 of 12 · about 4 hours · explain, derive
Memory admission, KV reserve and preemption by recompute or swap
Token budgets mean nothing if the KV blocks do not exist. Admission bets memory on unknown output lengths, and preemption is how the scheduler collects when the bet loses.
You'll be able to
- Explain optimistic versus reserved memory admission and the deadlock each one prevents or risks.
- Contrast recompute and swap preemption and pick the cheaper one from sequence size and interconnect numbers.
- Derive the reserve needed to admit one more request without evicting a running one.
Covers
Read
In this order, only the parts listed
0 of 4 done
- Admission bets, swap versus recompute, and the crossoverLLM Inference: From scratch, one token at a time - Module 05
Concept 05: admission control as a bet on unknown lengths; swap cost of moving 2.5 GiB at about 50 GB/s each way against recompute cost of one prefill; short sequences favour recomputation and long ones favour swapping
The module's 2.5 GiB at about 50 GB/s is about 54 ms each way once GiB is converted to GB; keep the two units apart.
- Nano-vLLM recompute preemption walkthroughNano-vLLM - Scheduler, Block Manager and Engine Loop
Scheduler.preemptwithBlockManager.deallocateinnanovllm/engine/block_manager.py, requeue at the waiting front withis_prefillreset; the decode loop evicting from the running tail viacan_appendbeforemay_append - Preemption states and counters in vLLMvLLM v0.31.0 metrics design
PREEMPTED state with the queue-time and inference-time interval split across preemptions, and the note that swapped-preemption metrics are legacy because v1 preempts by recomputation. The
vllm:num_preemptionscounter andvllm:request_num_preemptionshistogram are listed on the Production Metrics page, https://docs.vllm.ai/en/v0.31.0/usage/metrics/ - Orca reservation against scheduler deadlockOrca: A Distributed Serving System for Transformer-Based Generative Models
Section 4.2 around Algorithm 1: reserving
max_tokensslots per new request so the K/V manager never deadlocks with no space for the next token
Focus on
- Admission commits blocks for tokens that do not exist yet. Reserving the maximum bounds the risk and lowers fill; admitting on current length fills tighter and needs a working preemption path.
- Recompute discards KV and re-runs prefill on resume. Swap copies KV to host memory and back. The crossover sits where one prefill costs more than the round trip.
- Nano-vLLM only recomputes:
preemptfrees every block and the request restarts prefill from its tokens. vLLM v1 also preempts by recomputation; swap is gone, so size the reserve for recompute cost, not transfer cost. - Repeated preemption of the same request signals an undersized reserve or an overpacked batch, not bad luck. Count preemptions per request and treat growth as the alarm.
Common mistakes · 3
- Assuming swap exists in vLLM v1. The engine removed swap space and preempts by recomputation, so a plan that prices preemption as a PCIe transfer misprices every eviction.
- Pricing preemption as free because tokens are reused. Recompute re-runs the full prefill and re-consumes its token budget, which delays every other waiter.
- Reserving exactly the prompt length. Growth during decode is the risk, so the reserve must cover future tokens or the scheduler must be ready to preempt on the first append that does not fit.
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 deadlock does Orca reservation prevent?Answer
A pool full of running requests with no free slot for any next token, where no iteration can issue and no request can finish to free space. Reserving the maximum footprint admits only requests whose completion fits.
2.A 2.5 GB sequence faces preemption on a 50 GB/s link. What does swap cost, and when is recompute cheaper?Answer
About 50 ms each way, about 100 ms total. Recompute is cheaper when one prefill of that sequence costs less than the round trip.
3.Nano-vLLM preempts a decode sequence. What exactly is freed and what restarts?Answer
deallocatefrees all its blocks and zeroes its cached tokens, and the sequence requeues at the waiting front withis_prefillset, so it restarts with prefill from its stored tokens.4.One request preempts five times while others preempt never. What should you change?Answer
The admission policy, not the request. Repeated preemption of the same victim means the reserve is too small or the batch is overpacked; raise the reserve or tighten admission.
You're done when
You price swap against recompute for a sized sequence without notes and can trace blocks from admission through preemption to resume.
Only when you can do this without notes.
Leads to
Notes
Resources · 4 for this unit, 14 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.
- 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.
- 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/
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).
- 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-thresholdand--scheduling-policy. Runtime defaults vary by GPU class, so check them against the installed v0.31.0 behavior. - 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.