Chapter 10
The scheduler's loop
One Req through one step.
Inside process 2. Two lists of Req objects, waiting and running. The page pool and the radix tree from chapters 7 and 8. A model runner that owns the GPU. And one loop, top to bottom, about 208 times a second.
01 / 10
The loop, once, as code
while True:
reqs = recv_requests() # drain the zmq inbox, non-blocking
process_input_requests(reqs) # message → Req, prefix match, waiting_queue.append
batch = get_next_batch_to_run() # a prefill batch if anyone waits and pages allow, else running_batch
if batch is None:
continue
result = run_batch(batch) # ScheduleBatch → tensors → ModelRunner.forward → Sampler
process_batch_result(batch, result) # append tokens, finish Reqs, free/keep pages, send BatchTokenIDOutThat is Scheduler.event_loop_normal in managers/scheduler.py. event_loop_overlap is the same loop with the CPU work of the next step launched before the current step’s result is processed; sampled tokens are left on the GPU and read by the next forward pass directly, which is why the GPU lane stays solid.