Build your own continuous-batching inference server
CPU · your Mac~8 hPut the GPT-2 engine you wrote in Phase 1 behind an OpenAI-compatible API with a real scheduler: iteration-level batching, a paged KV cache with a block budget, admission control and preemption, all on CPU. It proves the phase because every component you simulated or read about in vLLM now exists as code you can step through, and the throughput gain over one-request-at-a-time serving is a number you measured.
Scope
- A FastAPI process exposing /v1/completions and /v1/chat/completions with SSE streaming and OpenAI-shaped errors.
- An engine process that runs one step loop; each step runs the batch of running sequences through your Phase-1 GPT-2 decoder with a padded or ragged attention path.
- A paged KV cache with 16-token blocks, block tables per sequence and a fixed block pool.
- A scheduler that admits waiting requests every iteration under the block budget, preempts the newest sequence when blocks run out, and resumes it by recompute.
- A /metrics endpoint with running, waiting, block usage and preemption counters.
- A bench script that drives the server at concurrency 1, 4 and 16 and reports TTFT, ITL and tokens per second.
Out of scope
- GPU execution or custom kernels.
- Prefix caching and chunked prefill (large capstone).
- Sampling beyond greedy, temperature and top-k.
What to measure
- Correctness versus your single-sequence engine
- How · greedy 64-token outputs for 8 prompts served concurrently compared with the same prompts served one at a time
- Target · 8 of 8 identical token sequences
- Throughput gain from continuous batching
- How · total tokens per second at concurrency 16 versus concurrency 1, 128 output tokens, median of 3 runs
- Target · at least 4x on an 8-core CPU
- Block budget invariant
- How · an assertion in the step loop plus the /metrics block-usage gauge sampled every 100 ms
- Target · never exceeds the pool size; zero assertion failures over a 200-request run
- Preemption behaviour
- How · run with a pool sized for 4 sequences and send 16 long requests
- Target · preemption counter above 0, all 16 requests complete, outputs identical to the unconstrained run
- Time to first token at concurrency 16
- How · p50 and p99 from the bench script
- Target · report both; p99 within 3x p50
Pay attention to
- Padded batches silently corrupt attention if the mask and position ids are wrong for sequences of different lengths; test with lengths 5 and 200 in one batch.
- A sequence that finishes mid-step must free its blocks before the next admission decision or the budget drifts upward.
- Preempting by recompute must reset the sequence's position counter and block table, otherwise it resumes with stale KV.
- Streaming must flush per token; buffering in the ASGI server makes TTFT look like e2e.
- Measure throughput by counted output tokens, not by number of SSE chunks.
Acceptance (all must pass)
- ☐pytest passes the 8-prompt correctness test and the block-budget invariant test.
- ☐The openai Python client completes streaming and non-streaming requests against the server without modification.
- ☐The preemption scenario completes with the counter above 0 and identical outputs.
- ☐README contains the concurrency 1/4/16 table and a paragraph explaining the throughput curve.