APAaryan Patel

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
simulation of the two allocation policies · measured numbers below

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.

speed

static batch of 4 reserve max_model_len per slot

0%KV in use right now
0%average so far
0requests done
0forward passes

paged + continuous 16-token blocks on demand

0%KV in use right now
0%average so far
0requests done
0forward passes
a sequence's tokensreserved, holding nothingfree
how it works

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.

measured · 48 requests · M-series CPU · best of 3
Same weights, sampling and KV budget in every row
configtok/sTTFT p50passesKV use
sequential (batch 1)6492849 ms317968%
static batch 410301792 ms141835%
static batch 161811736 ms49721%
paged + continuous2028520 ms26094%
Speculative decoding, n-gram proposer, 96 tokens
kpassestok/sacceptance
0 (off)965301–
258711884.5%
458719076.3%
858708965.6%
being straight about what this is

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.
reproduce
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