Unit 1 of 12 · about 4 hours · explain

Static, dynamic and continuous batching and iteration-level scheduling

Real request lengths vary by orders of magnitude, so a fixed batch spends most slots idle. Iteration-level refill is the mechanism every later policy in this section assumes.

You'll be able to

  • Contrast static, dynamic and continuous batching, and name the drain condition each one removes.
  • Explain iteration-level scheduling and selective batching at the operation level.
  • Explain why a throughput-maximizing batch can harm TTFT, inter-token latency and memory availability.

Covers

Static, dynamic and continuous batchingIteration-level token scheduling
1

Read

In this order, only the parts listed

0 of 3 done

  1. Request batching: static, dynamic and continuousHands-On LLM Serving and Optimization - Chapter 6, batching and scheduling

    Chapter 6, Request Batching and Scheduling-Level Optimizations: Why Do We Need Batching in Real-Time Serving? (p.190) with Fig 6-1 and Fig 6-2 at max batch size 3; Dynamic Batching in Online Inference (p.191) with max batch size, max delay time and the ferry-boat tuning example; Continuous Batching for LLM Online Inference (pp.192-194) with Fig 6-3 idle time and Fig 6-4 in-flight refill

  2. Batching: static cost, iteration loop and selective batchingLLM Inference: From scratch, one token at a time - Module 05

    Concepts 01 and 02 with the utilization arithmetic, the five-step iteration loop and selective batching; Math by hand Part 1 (a) to (d)

  3. Orca iteration-level scheduling and selective batchingOrca: A Distributed Serving System for Transformer-Based Generative Models

    Section 3 iteration-level scheduling with Fig 4; Section 3.2 selective batching with Fig 5; Section 4.2 Algorithm 1 iteration-level FCFS with max batch size and max_tokens reservation

    Skim the distributed execution in the rest of Section 4. Treat the 36.9x FasterTransformer throughput result as the paper's result on its hardware and workload, not a target.

2

Focus on

  • Slot utilization under static batching is mean length over max length, so one long request idles every shorter slot until it finishes.
  • Dynamic batching bounds assembly wait with a timeout but still drains the whole batch, so variable output lengths still idle slots.
  • Iteration-level scheduling admits and evicts at every decode step, which keeps the batch full regardless of length variance.
  • Selective batching flattens position-independent operations across sequences into one large matrix multiply and handles attention per sequence, which is what makes mixed-position batches implementable.
Common mistakes · 3
  • Calling continuous batching dynamic batching with a shorter timeout. Dynamic batching still runs each batch to completion; continuous batching changes the scheduling granularity to the iteration, which needs per-sequence KV management.
  • Assuming a larger static batch is always better. Utilization falls as batch size grows because the longest of more samples grows while the mean does not, and padding wastes both arithmetic and KV memory.
  • Quoting the 36.9x Orca result as expected gain. It compares against FasterTransformer on the paper's GPT-3 175B workload; your simulator should reproduce the direction and rough magnitude, not the number.
3

Try it yourself

Closed-book, before checking anything

Closed-book, compute slot utilization for output lengths [20, 35, 40, 60, 80, 120, 200, 900]: 900 iterations, 1455 useful sequence-steps, 20.2%. Then show batch 32 with mean 180 and longest 2000 gives 9%, and explain why growth moves it down: the max reaches further into the tail while the mean stays put.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.A static batch of 8 has output lengths [20, 35, 40, 60, 80, 120, 200, 900]. What is the slot utilization?Answer

    20.2%. Useful work is 1455 sequence-steps and capacity is sequence-steps.

  2. 2.The batch grows to 32 with mean length 180 and longest 2000. What happens to utilization and why?Answer

    It falls to 9%: useful over slots. More samples reach further into the length tail, so the max grows faster than the mean.

  3. 3.What scheduling change removes the wait for the longest sequence?Answer

    Decide every iteration instead of every request: evict each finished sequence and admit a waiting one on the next step, so slots refill without waiting for the batch maximum.

  4. 4.Why can the QKV projection batch across sequences at different positions while attention cannot?Answer

    The projection treats each token as an independent matrix row, so all tokens form one flat batch. Attention must stay within each request, since each sequence has its own KV length and positions.

5

Practice

  • TensorTonic: Simulate Continuous Decode Batching

    Implement continuous_batching_scheduler(requests, kv_capacity); public cases scheduler_public_1, scheduler_public_2 and scheduler_public_parallel_completion

    The public cases check arrival-order admission with skip-if-unfit, the two admission passes, active and finished ordering, and kv_tokens_in_use as actual use rather than reserved footprint. Compare its reserve-full-footprint rule with your simulator admission in the simulator unit.

You're done when

You reproduce both utilization numbers without notes and can trace which requests enter and leave each iteration on a small arrival timeline.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 15 more in the section

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).

  • 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.