↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

What continuous batching is

A language-model server answers many requests at once, each with its own prompt and its own answer length. Generating a token for one request means reading every weight of the model once, so a decode step is limited by memory traffic, not arithmetic: the same step can produce a token for several requests for little more than the cost of one. Serving them in a batch is how a server gets its throughput.

Static batching groups requests, runs them together and starts the next group only when every request of this one has finished, so short answers wait for the longest. Continuous batching (iteration-level scheduling, from Orca) rebuilds the batch at every step instead: a request that finishes leaves at once, and a waiting request joins in the very next step. vLLM pairs it with paged KV blocks, so requests of any length can come and go without moving each other's cache.

This page serves ten real requests to Qwen2.5-0.5B-Instruct both ways, with the tab's own scheduler and paged blocks running on this Mac's GPU in float32, and times every step.

The running batch

The The running batch panel draws both runs on one time axis, one row per request: prefill dark, decoding in the request's colour, waiting pale. All ten requests arrive at time 0 in the order listed, and at most 4 run at once (vLLM's max_num_seqs). Every step the scheduler first gives each running request its next token, taking a new KV block when its last one is full; then, while a seat, the token budget and enough blocks for the whole prompt are free, the oldest waiting request joins with its prompt, prefilled in the same forward as the others' decodes.

Continuous batching serves all ten in 162 steps, static batching in 248. Static batching runs each group of four as long as its longest answer: the second group keeps going for palindrome's 128 tokens after multiply (4 tokens) and french (9) have finished, with their seats empty, and reply and bakery wait the whole time. Static batching keeps 432 of its 4 × 248 seat-steps busy (44 %); continuous batching 432 of 4 × 162 (67 %), the empty seats coming only at the end, when fewer than four requests are left.

Static batching here is request-level scheduling with the same engine: it admits only into an empty batch, and a request that finishes early simply stops computing. Systems without a scheduler pad finished rows and keep computing them, which would make static batching look worse still.

One pool of KV blocks

Every request's keys and values live in one pool of 64 blocks of 16 positions, the KV-cache tab's paged layout shared by all requests. A request holds a block table, the physical block behind each of its logical blocks, takes a block from the free list only when its last one fills, and returns all of them when it finishes. The One pool of KV blocks panel colours each physical block by its owner at the chosen step; under it, the KV-cache tab's block table shows the chosen request's logical blocks and where they live.

Because blocks are small and taken one at a time, a request wastes at most 15 allocated slots, and a newcomer can use blocks scattered anywhere in the pool. In this run at most 30 of the 64 blocks were in use at once, so admission was limited by the four seats, not by memory. When the pool runs dry, vLLM preempts the most recently admitted request, frees its blocks and recomputes it later; the drawer's scheduler does the same, and its check exercises it with a pool too small for every request.

One forward for the whole step

Each step's tokens from every request are flattened into one list: a decoding request contributes its last token, a joining one its whole prompt. Every linear layer then runs once on all of them as one matrix, which is where batching pays; only attention is per request, each request's queries reading its own blocks (decodes gathered into one padded batch, as a paged-attention kernel reads them).

The What one step costs chart measures one decode step for 1 to 16 requests with 128 cached positions each: 63 ms for one request, 99 ms for four and 94 ms for sixteen, against 1.21 s for a single 2,048-token prefill. The model runs in float32 on a shared Mac (17–20 GB of swap in use while it was timed), so single steps vary by tens of milliseconds, but the shape holds: a step's cost grows far more slowly than the number of requests it serves.

Latency, throughput and the same answers

All ten requests produce 432 tokens. Continuous batching finishes them in 11.27 s of measured forwards, 38 tokens per second; static batching takes 15.80 s, 27 tokens per second. A request's first token comes after 1.61 s on average instead of 3.97 s, and its last after 4.73 s instead of 6.73 s.

The Each request bars show where the gain comes from: requests that would have waited for a slow group start as soon as anyone finishes. bakery, the last in the queue, is done after 5.23 s instead of 12.47 s, and french after 2.40 s instead of 4.36 s; the first four, which start together either way, finish a little later with continuous batching (sky after 4.46 s instead of 3.61 s), because the steps they share with newcomers' prefills take longer.

The times are sums of measured forwards: each step's forward run once untimed (the GPU builds kernels for every new shape) and then five times, keeping the fastest, since other jobs share this Mac; scheduling and sampling in Python are not counted. Batching changes the shapes of every matrix product, which can change the last bits of a float sum, so the page checks that each request's greedy tokens are the same batched as served alone: all ten are token for token the same in both runs as alone, and the same as transformers' generate gives on the CPU.

Where it came from

Orca (Yu et al., 2022) introduced iteration-level scheduling with selective batching, the attention computed per request and everything else batched. vLLM (Kwon et al., 2023) added PagedAttention, the block tables and free list above, and made the scheduler the default in open-source serving. Chunked prefill (Sarathi-Serve, the Chunked prefill tab) splits long prompts so that joining requests do not stall the others' decodes, and DistServe moves prefill and decode to separate GPUs. The KV cache tab shows the cache these blocks hold, HiCache extends it over host memory and disk, and DP attention spreads requests over data-parallel attention ranks.

Papers

  1. Orca: A Distributed Serving System for Transformer-Based Generative Models Gyeong-In Yu, Joo Seong Jeong, Geon-Woo Kim, Soojeong Kim, Byung-Gon Chun · OSDI 2022 Iteration-level scheduling: the batch is rebuilt every step, so requests join and leave at once.
  2. Efficient Memory Management for Large Language Model Serving with PagedAttention Woosuk Kwon et al. · SOSP 2023 vLLM: KV blocks, block tables and the scheduler loop this page's drawer follows, preemption included.
  3. Taming Throughput-Latency Tradeoff in LLM Inference with Sarathi-Serve Amey Agrawal et al. · OSDI 2024 Stall-free batching with chunked prefills, the next tab.
  4. DistServe: Disaggregating Prefill and Decoding for Goodput-optimized Large Language Model Serving Yinmin Zhong et al. · OSDI 2024 Runs prefill and decode on separate GPUs instead of mixing them in one batch.
  5. Qwen2.5 Technical Report Qwen Team · arXiv 2024 The model served here.

See also