case study · ml infrastructure · Python · numpy
vllm-lite
The interesting part of LLM serving isn't the model. It's sharing one copy of the weights and one pool of KV memory across many requests without wasting either.
- of reserved KV cache holding real tokens, paged + continuous batching
- 94%
- the same number for a 16-wide static batch, from the same memory
- 21%
- better median time-to-first-token than sequential serving
- 5.5×
- tests, most of them asserting that output never changes
- 90
Same memory. Same requests. Two schedulers.
48 requests with prompt and output lengths drawn from the repo's lognormal ranges, against a 2,048-token KV budget. Each cell is a 16-token block. Static batching reserves a 512-token slot per sequence and waits for its longest member. Paging allocates blocks on demand and refills a slot the step it frees.
static batch of 4 reserve max_model_len per slot
paged + continuous 16-token blocks on demand
The virtual-memory trick, applied to attention.
The cache is one flat tensor per layer, chopped into 16-token blocks. A sequence doesn't own contiguous memory. It owns a block table, and attention gathers through it. That one indirection lets a sequence grow a block at a time instead of reserving its worst case up front.
Continuous batching
The batch is rebuilt on every forward pass. A finished sequence frees its slot immediately and a queued request takes it on the very next pass. Prefill and decode share one token budget, so a long prompt is sliced across passes instead of stalling everyone else.
Prefix caching
Every full block gets a hash of all tokens up to it, chained through the previous block's hash, and is published the moment it fills. With a 256-token system prompt shared by 48 requests, that bought +49% throughput and 1.8× better TTFT.
Invisible from outside
Greedy decoding has exactly one right answer, so the tests assert identical output across block sizes, prefix caching on and off, chunked prefill budgets, batch composition, forced preemption, and speculative decoding at every k.
| config | tok/s | TTFT p50 | passes | KV use |
|---|---|---|---|---|
| sequential (batch 1) | 649 | 2849 ms | 3179 | 68% |
| static batch 4 | 1030 | 1792 ms | 1418 | 35% |
| static batch 16 | 1811 | 736 ms | 497 | 21% |
| paged + continuous | 2028 | 520 ms | 260 | 94% |
| k | passes | tok/s | acceptance |
|---|---|---|---|
| 0 (off) | 96 | 5301 | – |
| 2 | 58 | 7118 | 84.5% |
| 4 | 58 | 7190 | 76.3% |
| 8 | 58 | 7089 | 65.6% |
A serving layer, measured honestly.
- The weights are randomThe text is nonsense on purpose. Random weights do the same arithmetic and move the same bytes, which is what the scheduler and cache are measured on.
- numpy on CPU, not CUDARatios between configurations hold because every row runs the same code path. Absolute tok/s means nothing outside this machine.
- Attention loops in PythonProjections, MLP and LM head are batched across the step. The per-sequence attention loop is the part a fused GPU kernel would flatten.
- Not implementedTensor parallelism, CUDA graphs, quantization, LoRA, swap-to-CPU preemption, beam search.
git clone https://github.com/asp53826/vllm-lite cd vllm-lite && pip install -r requirements.txt python bench/bench.py --requests 48 pytest # 90 tests