WRITE · TRACE · UNDERSTAND

Project: simulate bounded continuous batching with deadlines

Requests have unique string id and integer arrival>=0, deadline>arrival, prompt>=0, output>=1.

Read lesson: Project: simulate bounded continuous batching with deadlines →

Your experiment

Requests have unique string id and integer arrival>=0, deadline>arrival, prompt>=0, output>=1. Capacities are positive integers. Reserve prompt+full output tokens while active; oversized requests are rejected. At each integer tick cancel pending/active requests with deadline<=tick, admit FIFO by (arrival,id) up to slots and reserved capacity without skipping a waiting head, then generate one token for every active request. Completion time is tick+1, so finishing exactly at deadline is allowed. Jump to the next arrival when idle. Return completed time mapping, sorted cancelled/rejected IDs and per-tick events (tick,admitted,completed,cancelled,active after generation,reserved before generation). This is a deterministic scheduling simulation, not model inference or a throughput benchmark.

Python runs in your browser. Your draft stays on this device.

What to look for

Review reservations, FIFO blocking, deadline boundaries and cancellation. Real serving requires measured prefill/decode costs, paged memory, admission telemetry and model-specific benchmarks. Replay follows the main orchestration function; helper computations execute and are checked separately.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Project: simulate bounded continuous batching with deadlines works

The idea

A static batch makes every request wait until the longest one in the batch has finished. Continuous batching works one token at a time instead. On each tick every active request generates one token, a request that finishes frees its slot and its memory at once, and a waiting request can join on the next tick. Short requests no longer queue behind long ones.

This project simulates that loop under two limits, max_active slots and capacity reserved tokens, and adds deadlines. The order of work inside a tick defines the behaviour: cancel what has expired, admit strictly first in first out, then generate. Admission never skips a waiting head that does not fit, which is fair to large requests but lets them block smaller ones behind them.

How it works

  1. Validate the inputs, put requests whose prompt + output exceeds capacity into rejected, and sort the rest by (arrival, id) into pending.
  2. When nothing is active and the next arrival lies ahead, jump tick forward to that arrival.
  3. Cancel every pending or active request whose deadline <= tick.
  4. Admit from the head of pending while it has arrived, a slot is free and its reservation fits, stopping at the first head that does not fit.
  5. Generate one token for every active request; those whose remaining reaches zero complete at time tick + 1 and leave.
  6. Append an event for the tick, add one to tick, and repeat until pending and active are both empty.

What it costs

Per tick
scan pending and active
Token rate
one per active per tick
Reserved
prompt + full output

This is a deterministic simulation: time moves in integer ticks and idle gaps are skipped, so its cost follows the number of ticks times the requests examined, not real inference speed.

Where it is used

  • vLLM's serving engine uses continuous batching, adding and removing requests from the running batch between generation steps.
  • Hugging Face Text Generation Inference lists continuous batching of incoming requests among its features.
  • Orca (Yu et al., OSDI 2022) introduced iteration-level scheduling, the idea that continuous batching is based on.

Watch out for

  • Admitting a smaller request from behind a waiting head that does not fit; the Lab's FIFO rule forbids skipping the head.
  • Getting the boundary wrong: cancellation uses deadline <= tick and completion is stamped tick + 1, so finishing exactly at the deadline is allowed.
  • Treating an oversized request as cancelled: one larger than capacity can never run and belongs in rejected before the loop starts.