From Pieces to a Scheduler

Introduction

Project introduction

From Pieces to a Scheduler

You have written every piece a serving scheduler needs, namely attention with a KV cache, a plan for one batching step, the latency metrics, the size of the KV cache, and a block allocator.

In the notebook you first run a real PyTorch model to see that a KV cache gives exactly the same tokens with far less work, and that a step for many requests costs little more than a step for one. Then a simulated server with the same step cost serves hundreds of requests in under a second, so you can compare schedulers under heavy traffic.

What you will see

One request at a time leaves users waiting tens of seconds. Static batching helps, but tops out near 420 tokens per second. Continuous batching keeps climbing to about 1000 and, under light traffic, starts answers within tens of milliseconds. With limited KV memory, the server has to preempt requests and redo their work.

Then long documents arrive. A scheduler that admits every prompt at once has the best throughput, but each long prefill freezes every streaming user for hundreds of milliseconds. Chunked prefill fixes it, and you will measure exactly what it costs.

Who runs in each model step

3 batch slots
slot 1
A
slot 2
B
slot 3
C
waiting0
prefill + first token decode, one token empty slot0 empty slots so far
Requestsarrive at any time
Schedulerplans every step
Model stepprefill and decode
Tokensstream to users

How the notebook is organized

01

A real model

Prefill, decode, a KV cache, and the cost of one step, measured in PyTorch.

02

Three schedulers

One at a time, static batching, and continuous batching, measured under rising traffic.

03

Memory and the tail

Run out of KV memory, then stop long prompts from stalling everyone.

Serve, measure, and fix the tail

Run the whole investigation in the browser, about 50 minutes.

Open the notebook project