inference-batching-lab
A decoder-only transformer with a hand-written KV cache, and three request-scheduling strategies benchmarked for real throughput and latency.
Why this project
Once you have a model, serving it well is a scheduling and memory-management problem. Writing the batching logic from scratch means the KV cache, the causal masking and the admission policy are all code I can explain line by line.
Byte-identical output
Batching only pays off if it does not change what gets generated. Every batched strategy matches one-at-a-time generation exactly, even though requests join and leave the batch at different times.
Measured
On CPU with a 241K-parameter model and 24 requests: sequential 1,917 tok/s, static batching 4,178 tok/s, continuous batching 4,198 tok/s.
Stack
Python · PyTorch · Hypothesis
Next
Paged KV-cache and speculative decoding, built in the follow-up project, paged-speculative-engine. This lab isolates scheduling and measures it with an untrained model on CPU.