Sub topic 5
Continuous Batching and the Scheduler
This chapter assumes you know basic Python, GPU memory, prompts, and completions. It also assumes you understand that autoregressive generation creates one token at a time and that the KV cache stores attention data for tokens already processed.
Why fixed batches waste GPU time
A traditional batch is a fixed group of requests processed together. The system waits until every request in the group finishes before starting another group.
This creates idle time. One request may need 20 output tokens while another needs 200. After the short request finishes, its space in the batch remains unused until the long request finishes. The GPU still has work available, but the fixed batch cannot accept it.
vLLM uses continuous batching, which is a scheduling method that adds and removes requests while generation is running. When one request finishes, the scheduler can place a waiting request into the next decoding step.
| Method | When requests enter | When requests leave | Main limitation |
|---|---|---|---|
| Fixed batching | At batch start | With the whole batch | Finished requests leave unused slots |
| Continuous batching | As capacity becomes available | Individually | Scheduling must manage memory carefully |
Prefill and decode scheduling
A request has two important phases:
- Prefill processes the complete input prompt and creates the initial KV cache.
- Decode generates output tokens one at a time using that cache.
The scheduler decides how much GPU work to assign to each phase. A long prompt can require substantial prefill work, while many active requests can require repeated decode work. Giving all capacity to one phase can make the other phase wait.
The scheduler also checks whether the KV cache has enough space for requested sequences. A request may be waiting even when compute is available because its future tokens cannot fit in GPU memory.
A small offline example
With the offline API, pass several prompts to generate. vLLM can process them as a workload instead of requiring you to create fixed-size batches manually.
from vllm import LLM, SamplingParams
llm = LLM(
model="Qwen/Qwen2.5-1.5B-Instruct",
max_model_len=2048,
gpu_memory_utilization=0.85,
)
prompts = [
"Summarize continuous batching in one sentence.",
"Name one reason fixed batches waste GPU time.",
"Explain why the KV cache affects admission decisions.",
]
params = SamplingParams(temperature=0.2, max_tokens=40)
for result in llm.generate(prompts, params):
print(result.prompt)
print(result.outputs[0].text)The exact scheduling behavior depends on prompt lengths, output lengths, available KV-cache blocks, and server settings. More concurrent requests can increase throughput until memory or compute becomes the limiting resource. Beyond that point, requests wait longer and latency rises.
Common scheduling mistakes
- Treating
max_num_seqsas a guaranteed number of simultaneously fast requests. It is a concurrency limit, not a latency promise. - Ignoring output length. Long generations reserve KV-cache space for more decode steps.
- Comparing throughput without matching prompt and completion lengths.
- Increasing concurrency after GPU memory is already saturated. This usually increases queueing rather than useful work.
Exercise
Three requests are active. One finishes after its next token while two need many more tokens. What does continuous batching allow vLLM to do?
Show answer
It can release the completed request's resources and admit a waiting request without waiting for the other two requests to finish.