Step a scheduler through static and continuous batches
Replay four requests under both policies and see where the queue time goes when a finished request frees a slot.
Static batching waits for a full batch, runs every request to its longest output length, and only then admits the next batch. Continuous batching admits a new request as soon as a slot frees, at the next model iteration. The difference is not a faster matrix multiplication; it is less idle capacity while a short request waits behind a long one.
This example replays four requests through both policies in integer time steps. Static batching produces a makespan of 39 steps and a mean wait of 7.5; continuous batching produces 12 steps and 1.25. Those are simulator outputs, not a serving benchmark. The value is the schedule: you can point to the step where a slot was idle and say why. The simulator’s cost model is deliberately simple and is part of what the page must disclose.
Run it
From the canonical repository at commit
f958967d70992d7b09d9c75000bd8c543f1644d4:
python examples/ch15/scheduler.py --jsonPython 3.11 or later, standard library only. No model, network, GPU or credentials. It finishes immediately.
Implementation
Static batching groups requests by arrival, starts a batch at the latest arrival
in the group, and charges max(prompt_len) + max(output_len) steps. Continuous
batching admits while a slot is free, processes one token per active request per
step, and adds prompt tokens when a request completes.
def continuous_batching(requests: List[Request], max_batch: int) -> SimResult:
reqs = sorted(requests, key=lambda r: r.arrival)
pending = list(reqs)
active: List[dict] = []
current_time = 0
completed = 0
total_tokens = 0
waits = []
# Event loop by integer time steps.
while pending or active:
# Admit new requests.
while pending and pending[0].arrival <= current_time and len(active) < max_batch:
r = pending.pop(0)
active.append({"req": r, "remaining": r.output_len, "admitted_at": current_time})
waits.append(current_time - r.arrival)
if not active:
# Advance time to next arrival.
if pending:
current_time = pending[0].arrival
continue
break
# Process one token for each active request.
finished = []
for item in active:
item["remaining"] -= 1
total_tokens += 1
if item["remaining"] <= 0:
finished.append(item)
for item in finished:
active.remove(item)
completed += 1
total_tokens += item["req"].prompt_len # approximate prompt tokens
current_time += 1
makespan = current_time
throughput = total_tokens / makespan if makespan > 0 else 0.0
mean_wait = sum(waits) / len(waits) if waits else 0.0
return SimResult(
policy="continuous",
completed=completed,
total_tokens=total_tokens,
makespan=makespan,
throughput_tokens_per_step=throughput,
mean_wait=mean_wait,
metadata={"max_batch": max_batch},
)Expected outcome
Observed with the command above (exit 0):
{
"static": {
"policy": "static",
"completed": 4,
"total_tokens": 56,
"makespan": 39,
"throughput_tokens_per_step": 1.435897435897436,
"mean_wait": 7.5,
"metadata": {
"batch_size": 2
}
},
"continuous": {
"policy": "continuous",
"completed": 4,
"total_tokens": 56,
"makespan": 12,
"throughput_tokens_per_step": 4.666666666666667,
"mean_wait": 1.25,
"metadata": {
"max_batch": 2
}
}
}Both policies completed four requests and counted 56 tokens. The continuous schedule finished in 12 steps instead of 39, and its mean admission wait fell from 7.5 to 1.25. Those values follow from the simulator’s rules; they are not a prediction of a GPU serving system.
Mechanism and limitations
Documented: the static policy batches by arrival and charges the maximum
prompt and output lengths in each batch. The continuous policy admits at most
max_batch active requests and processes one token for each per step; it adds
prompt tokens when a request finishes. The tests check both policies complete the
same requests, count the same tokens, and improve makespan and mean wait in this
fixture.
Observed: the command exited 0 with the schedule metrics above. The targeted
tests.test_ch15_scheduler is part of the canonical suite; no independent test
count is claimed here.
Not shown: real model execution, prefill/decode cost differences, chunked prefill, fairness between short and long requests, tail latency, GPU memory pressure, or a production scheduler. The static model is especially rough: it charges the longest prompt as prefill for the whole batch. Continuous batching also has a cost in cache management and per-iteration scheduling that this integer model does not include.
Challenge this result
Run the same fixture with batch_size=1 for static and max_batch=1 for
continuous. Predict which policy still wins and why. Then change request 2’s
output length to 1 and request 3’s to 20; recompute the schedules by hand for the
first few steps. Explain how a policy can improve average wait while making one
request’s tail latency worse, and what metric a fairness-aware scheduler should
add.
Source and verification
Canonical source:
examples/ch15/scheduler.py
at commit f958967d70992d7b09d9c75000bd8c543f1644d4. Tests:
tests/test_ch15_scheduler.py
.
Chapter: Continuous Batching
.
The run above is a fresh local execution on Windows 11 with Python 3.13.15; it
is a deterministic simulation, not a measurement of a serving engine.
Understand this example
Copy this prompt into your AI tool. No code runs here.
Apply this example
Copy this prompt into your AI tool. No code runs here.