← All work

paged-speculative-engine

A from-scratch serving engine that combines continuous batching, a paged KV-cache and speculative decoding, and solves the problem that only appears when you combine them.

p = 0.64
chi-squared, 4,000 trials
0.62x
ceiling vs plain batching on CPU

Why this project

Each of the three techniques is well covered alone. Almost nothing covers the fact that they do not compose for free: continuous batching assumes every request advances one token per step, and speculative decoding breaks that assumption the moment one request accepts all its draft tokens while another rejects them all.

The composition problem, precisely

  • The forward pass is always uniform-width across the batch. Drafting is k sequential width-1 calls and verification is one width-k call.
  • The heterogeneity is between calls, not within one. After verification each request truncates its own KV-cache back to its accepted length.
  • That is where paging earns its cost. Truncation is just freeing a request's trailing blocks to the pool, touching nothing that belongs to any other request.

Correctness

  • Output matches plain greedy decoding exactly at temperature 0, for single requests and for requests forced through small batches.
  • Across 300 randomly generated scheduler configurations (prompts, batch sizes, draft lengths, block sizes), every request matches running alone.
  • The block free-list is never corrupted across 150 random allocate, free and append sequences.
  • At temperature above zero, the sampled distribution matches the target model's exact distribution, with a chi-squared p-value of 0.64 over 4,000 trials.

Benchmark

On CPU with a tiny model, the best case (a draft model identical to the target) reaches 0.62x of plain batching, and an untrained draft 0.14x. Speculative decoding pays off on GPUs, where decode is memory-bandwidth bound, a regime a CPU run does not reproduce, so the GPU kernel work is the next step.

Stack

Python · PyTorch · Hypothesis · SciPy

Next

A fused Triton paged-attention kernel, a lightly distilled draft model, tree-based speculation and prefix caching.