Skip to content
cipheraxatPublic

About

A small vLLM-style LLM inference engine in PyTorch: paged KV cache, continuous batching, chunked prefill, prefix caching, Triton paged-attention kernel, OpenAI-compatible server

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

nanoserve

A small LLM inference engine in the style of vLLM, written from scratch in PyTorch (about 1,600 lines). It serves Qwen3 models through an OpenAI-compatible API and implements the core ideas of a modern serving engine:

  • Paged KV cache. K/V live in fixed-size blocks. Each request holds a block table, so memory is allocated per block as a sequence grows instead of reserving max_model_len up front.
  • Continuous batching. The scheduler rebuilds the batch every step: finished requests leave and new ones join without waiting for the rest of the batch.
  • Chunked prefill. Each step has a token budget. Long prompts are split across steps and share them with running decodes, so a 2k-token prompt doesn't stall everyone else's next token.
  • Prefix caching. Full blocks are content-addressed by a chained hash (parent hash + tokens). Requests that share a prefix (system prompt, few-shot examples, multi-turn chat) reuse the cached blocks and skip that prefill. Freed blocks stay cached in an LRU pool until the memory is needed.
  • Preemption by recompute. When the cache is full, the newest running request gives up its blocks and is requeued at the front. Its prefix is often still cached when it comes back.
  • Triton paged-attention decode kernel. On CUDA, every decode reads K/V straight from the block table with an online softmax instead of gathering them into a padded buffer, including in steps that also carry prefill chunks. 14 to 19x faster than the gather path at batch 16 to 64 on an L4.
  • Packed batches. All new tokens in a step are packed into one [T] dimension; only attention needs per-sequence structure. QKV and gate/up projections are fused into one matmul each.
  • Sampling. Temperature, top-k, top-p with one shared sort, and per-request seeds that give the same tokens no matter what else is in the batch.
  • OpenAI-compatible server. /v1/completions and /v1/chat/completions with SSE streaming, prompt-cache usage in the response (prompt_tokens_details.cached_tokens), client-disconnect aborts, and Prometheus /metrics.

Correctness is tested against Hugging Face transformers: greedy decoding produces exactly the same tokens as HF for every request, under continuous batching, chunked prefill, prefix-cache hits and preemption.

Architecture

nanoserve has three layers. The FastAPI server runs on the asyncio event loop and handles HTTP, tokenization and streaming. A single background engine thread owns all engine state and runs the step loop. The model and the paged KV cache live on the device (CUDA, MPS or CPU). The only link between the event loop and the engine thread is AsyncEngine: a thread-safe inbox carries requests in, and one asyncio queue per request carries tokens out.

flowchart TB
    HTTP["HTTP clients<br/>OpenAI SDK, curl, bench_serving.py"]
    PY["Offline Python<br/>LLMEngine.generate()"]

    subgraph loop["asyncio event loop"]
        SRV["server.py: FastAPI<br/>/v1/completions, /v1/chat/completions<br/>chat template, tokenize, validate,<br/>incremental detokenize, SSE, /metrics"]
    end

    AE[["async_engine.py: AsyncEngine<br/>inbox queue.Queue in (add, abort)<br/>one asyncio.Queue per request out"]]

    subgraph thread["engine thread"]
        STEP["engine.py: LLMEngine.step()"]
        SCH["scheduler.py: Scheduler<br/>waiting and running queues,<br/>token budget, preemption"]
        BM["block_manager.py: BlockManager<br/>block tables, ref counts,<br/>prefix hashes, LRU pool"]
        MR["model_runner.py: ModelRunner<br/>pack the batch, slot mapping,<br/>attention metadata"]
        SAMP["sampler.py: Sampler<br/>temperature, top-k, top-p, seeds"]
    end

    subgraph device["device: CUDA, MPS or CPU"]
        MODEL["models/qwen3.py: Qwen3ForCausalLM<br/>fused QKV and gate/up"]
        TRI["triton_attention.py<br/>paged decode kernel"]
        GATH["attention.py<br/>gather + SDPA"]
        KV[("Paged KV cache<br/>layers × 2 × blocks × block_size × H_kv × D")]
    end

    HTTP <-->|"request / SSE chunks or JSON"| SRV
    SRV <-->|"token ids + SamplingParams / RequestOutput"| AE
    AE <--> STEP
    PY <--> STEP
    STEP --> SCH
    SCH <-->|"allocate, append, free"| BM
    STEP -->|"scheduled sequences"| MR
    STEP -->|"logits / next token ids"| SAMP
    MR <-->|"packed inputs / logits"| MODEL
    MODEL -->|"decodes on CUDA"| TRI
    MODEL -->|"prefill chunks,<br/>or all without Triton"| GATH
    TRI <--> KV
    GATH <--> KV
    BM -. "block tables index into" .-> KV
Loading

LLMEngine owns the scheduler, block manager, model runner and sampler, and nothing else changes them. The scheduler decides what runs in a step; the block manager decides where its K/V lives. The scheduler never edits a block table itself.

Request data flow

The path of one streaming chat request, from the HTTP call to the last SSE chunk:

sequenceDiagram
    autonumber
    participant C as Client
    participant S as FastAPI handler<br/>(event loop)
    participant A as AsyncEngine
    participant E as Engine thread<br/>(LLMEngine)
    participant D as Model + KV cache<br/>(device)

    C->>S: POST /v1/chat/completions, stream=true
    S->>S: apply chat template, tokenize, validate_request
    S->>A: generate(token_ids, params)
    A->>A: register an asyncio.Queue for the request id
    A->>E: inbox.put(add, request id, token ids, params)
    E->>E: add_request, then append to Scheduler.waiting
    loop one engine step, while any request is unfinished
        E->>E: drain the inbox (adds and aborts)
        E->>E: schedule: pick sequences and token counts, match cached prefix on admission
        E->>D: one forward pass over the packed batch
        D-->>E: logits, only for sequences that reached the end of their tokens
        E->>E: sample, append token, publish full blocks, check stop conditions
        E-->>A: RequestOutput via loop.call_soon_threadsafe
        A-->>S: yield the output
        S->>S: incremental detokenize
        S-->>C: SSE chunk with the text delta
    end
    E->>E: EOS, stop token or max_tokens: finish and free its blocks
    S-->>C: final chunk with usage and cached_tokens, then data: [DONE]
    Note over C,E: Steps that only process a prefill chunk send nothing to this request.<br/>If the client disconnects, the generator closes, AsyncEngine queues an abort<br/>and the engine frees the request's blocks on its next loop.
Loading

One engine step

  1. Schedule. Running requests first (1 token per decode, the next chunk for a request still in prefill), then waiting requests while the token budget and free blocks allow. On admission, the block manager matches the longest cached prefix and marks those tokens as already computed.
  2. Prepare. Flatten every request's new tokens into input_ids[T] and positions[T], compute each token's cache slot from its block table, and build the attention metadata once for all layers.
  3. Forward. Each layer writes this step's K/V into the paged cache, then attends over the full context through the block table. Logits are computed only for requests whose prompt is fully processed.
  4. Sample and update. Append tokens, publish newly full blocks to the prefix cache, finish requests on EOS or max_tokens, and free their blocks.

The same step as tensors. T is the number of new tokens in the step across all requests, and S is the number of requests that sample a token in it:

flowchart TB
    Q["Scheduler queues<br/>each Sequence: token_ids, num_computed_tokens, block_table"]
    Q --> SCHED["schedule()<br/>running first, then waiting FCFS,<br/>within max_num_batched_tokens"]
    SCHED --> SO["SchedulerOutput: list of (sequence, num_tokens)<br/>1 for a decode, up to the budget for a prefill chunk"]
    SO --> PREP["ModelRunner.prepare()"]
    PREP --> IDS["input_ids [T], positions [T]"]
    PREP --> SLOT["slot_mapping [T]<br/>block_table[p / block_size] × block_size + p mod block_size"]
    PREP --> META["attention metadata<br/>decode group: block_tables, context_lens<br/>gather group: key_slots, causal mask"]
    PREP --> LI["logits_index [S]<br/>last packed row of each sequence<br/>whose tokens are now all computed"]
    IDS --> EMB["embed_tokens: hidden [T, hidden_size]"]
    subgraph layer["each decoder layer, on the packed [T] batch"]
        QKV["RMSNorm, fused qkv_proj,<br/>q/k RMSNorm, RoPE"]
        WKV["write_kv: index_copy_ this step's K and V<br/>into the paged cache at slot_mapping"]
        PA["paged_attention<br/>decodes: Triton kernel<br/>prefill chunks: gather + SDPA<br/>outputs scattered back to [T]"]
        MLP["o_proj + residual,<br/>RMSNorm, fused gate_up, SiLU, down_proj + residual"]
        QKV --> WKV --> PA --> MLP
    end
    EMB --> QKV
    SLOT --> WKV
    META --> PA
    MLP --> SEL["keep only the logits_index rows,<br/>final RMSNorm, lm_head"]
    LI --> SEL
    SEL --> LOG["logits [S, vocab]"]
    LOG --> SMP["Sampler<br/>greedy argmax, or argmax of p / Exp(1)"]
    SMP --> UPD["update<br/>num_computed_tokens += num_tokens<br/>register_full_blocks into the prefix cache<br/>append token, stop on EOS or max_tokens<br/>free finished sequences' blocks"]
    UPD --> Q
Loading

Request lifecycle

stateDiagram-v2
    [*] --> WAITING: add_request
    WAITING --> RUNNING: admitted, cached prefix attached, blocks allocated
    RUNNING --> WAITING: preempted, blocks freed, requeued at the front
    RUNNING --> FINISHED: EOS or stop token, max_tokens or max_model_len
    RUNNING --> FINISHED: abort on client disconnect
    WAITING --> FINISHED: abort
    FINISHED --> [*]: blocks released
Loading

While RUNNING, a request gets its next prefill chunk or one decode token in every step. A preempted request keeps the tokens it has generated, and its num_computed_tokens goes back to 0. When it is admitted again, the prefix cache usually still holds most of its blocks, so recomputing it is cheap.

Paged KV cache and prefix sharing

Each request holds a block table: logical block i of the request maps to a physical block in the cache. A full block is published under a chained hash of its parent block's hash and its own tokens, so a later request with the same prefix maps the same physical blocks and skips that prefill. Below, with 16-token blocks, B shares A's first two blocks (a 32-token system prompt) and gets its own block for the rest:

flowchart LR
    subgraph ta["Request A block table"]
        direction TB
        A0["0"]
        A1["1"]
        A2["2"]
    end
    subgraph cache["Physical KV blocks"]
        direction TB
        K7["block 7, ref count 2<br/>h1 = hash(None, tokens 0-15)"]
        K3["block 3, ref count 2<br/>h2 = hash(h1, tokens 16-31)"]
        K12["block 12, ref count 1<br/>A's tail, not full yet"]
        K5["block 5, ref count 1<br/>B's tail, not full yet"]
    end
    subgraph tb["Request B block table"]
        direction TB
        B0["0"]
        B1["1"]
        B2["2"]
    end
    A0 --- K7
    A1 --- K3
    A2 --- K12
    K7 --- B0
    K3 --- B1
    K5 --- B2
Loading

When a block's ref count drops to 0, a block that was never hashed returns to the plain free list. A hashed block goes to an LRU pool, where a later request can still match it. New blocks come from the plain free list first, and only then is the least recently used cached block evicted. A match always leaves at least the last prompt token to compute, because the model has to run on it to produce the first output token.

Source map

File What it does
block_manager.py Block allocation, ref counts, chained-hash prefix cache, LRU eviction
scheduler.py Continuous batching with a token budget, chunked prefill, preemption
model_runner.py Weight loading, KV cache sizing, packing a batch into one forward pass
attention.py Paged attention (torch gather + SDPA path for any mix of prefill and decode)
triton_attention.py Triton paged decode kernel with online softmax and GQA
models/qwen3.py Qwen3 decoder for packed, paged inference (fused QKV and gate/up)
sampler.py Batched temperature, top-k, top-p and seeded sampling
engine.py, async_engine.py Step loop, stop conditions, background engine thread
server.py OpenAI-compatible API, streaming, Prometheus metrics
sequence.py, sampling.py, config.py Per-request state, sampling parameters, engine settings

Quick start

git clone https://github.com/cipheraxat/nanoserve && cd nanoserve
uv sync
uv run nanoserve --model Qwen/Qwen3-0.6B --port 8000
curl -s localhost:8000/v1/chat/completions -H 'content-type: application/json' \
  -d '{"messages":[{"role":"user","content":"What is 17*23?"}],"max_tokens":32,"temperature":0}'

Offline use:

from nanoserve import EngineConfig, LLMEngine, SamplingParams

engine = LLMEngine(EngineConfig(model="Qwen/Qwen3-0.6B", max_num_batched_tokens=512))
outputs = engine.generate(["The capital of France is"], SamplingParams(max_tokens=16, temperature=0))
print(engine.tokenizer.decode(outputs[0]))

Useful flags: --max-num-seqs, --max-num-batched-tokens (the chunked-prefill budget), --kv-cache-memory-gb, --block-size, --no-prefix-caching, --attention-backend {auto,torch,triton}.

Benchmarks

All runs use Qwen3-0.6B in bf16 with greedy decoding and ignore_eos, so every request generates exactly the tokens it asked for.

NVIDIA L4 (Colab, torch 2.11 + CUDA 13, Triton 3.6)

Continuous batching vs static batching. 128 requests with 32 to 512 prompt tokens and 16 to 256 output tokens, at most 64 in flight:

system time output tok/s
nanoserve (continuous batching, paged KV, Triton decode) 20.5 s 915
Hugging Face generate, static batches of 64 80.7 s 233

3.9x the throughput. Static batching pads every batch to its longest prompt and keeps decoding until its longest request is done; continuous batching refills the slot as soon as a request finishes.

Triton paged decode kernel vs torch gather + SDPA, one attention layer at Qwen3-0.6B's shape (16 query heads, 8 KV heads, head dim 128), bf16:

batch context torch gather Triton paged speedup
1 2,048 0.46 ms 0.18 ms 2.5x
16 2,048 10.86 ms 0.62 ms 17.4x
64 512 10.65 ms 0.58 ms 18.5x
64 2,048 43.76 ms 2.66 ms 16.4x
64 8,192 171.71 ms 12.50 ms 13.7x

Max absolute difference from the reference is within bf16 rounding (at most 5e-4).

The kernel only paid off once it handled every decode. Under continuous batching most steps carry a prefill chunk next to the running decodes, and at first those mixed steps sent every sequence down the gather path. With that design nanoserve reached only 168 tok/s on the benchmark above, slower than Hugging Face. The engine now splits each step: decodes go through the kernel, prefill chunks through the gather path, and the results are scattered back into the packed batch. That took throughput from 168 to 915 tok/s.

Prefix caching. 32 requests share a 1,024-token system prompt, each with a different 32-token suffix and 32 output tokens:

prefix caching total time prompt tokens computed cache hit rate TTFT p50 TTFT p99
off 11.1 s 34,784 0% 5.22 s 10.00 s
on 4.9 s 3,040 94% 2.03 s 3.82 s

11x less prefill work, 2.3x faster end to end, 2.6x lower median TTFT.

Chunked prefill. 16 requests are decoding while six 2,048-token prompts arrive. Inter-token latency (ITL) seen by the decoding requests:

token budget per step ITL p50 ITL p99 ITL max
unlimited (whole prompt in one step) 35 ms 473 ms 474 ms
512 35 ms 130 ms 130 ms
256 35 ms 65 ms 65 ms

A 256-token budget cuts the worst decode stall 7.3x with no change to median ITL.

Apple M4, 16 GB (MPS)

These were taken before the decode/prefill split, which only matters on CUDA.

Prefix caching. 32 requests share a 1,024-token system prompt, each with a different 32-token suffix and 32 output tokens:

prefix caching total time prompt tokens computed cache hit rate TTFT p50 TTFT p99
off 100.4 s 34,784 0% 47.1 s 96.9 s
on 15.1 s 3,040 94% 7.1 s 12.2 s

6.6x faster end to end, with 11x less prefill work.

Chunked prefill. 16 requests are decoding while six 2,048-token prompts arrive. Inter-token latency (ITL) seen by the decoding requests:

token budget per step ITL p50 ITL p99 ITL max
unlimited (whole prompt in one step) 81 ms 21.5 s 23.5 s
512 71 ms 4.5 s 4.6 s
256 74 ms 2.2 s 2.3 s

A 256-token budget cuts the worst decode stall about 10x, at no cost to median ITL.

Continuous batching vs static batching. 64 requests with 32 to 512 prompt tokens and 16 to 256 output tokens, at most 32 in flight: nanoserve 60.0 output tok/s vs Hugging Face generate with static batches of 32 at 50.3 tok/s (1.19x).

On this machine MPS is limited by kernel launch overhead and memory pressure: a single 2,048-token prefill takes about 3 s in both nanoserve and Hugging Face. The absolute numbers are low; the comparisons are what these runs show.

Reproduce with the scripts in benchmarks/ (bench_offline.py, bench_prefix.py, bench_chunked.py, bench_attention.py, and bench_serving.py for an online Poisson load test against a running server). Raw JSON is in benchmarks/results/. The L4 numbers come from one Colab run that cloned this repo at the split-attention commit, ran the test suite on the GPU, then ran the four benchmarks; scripts/colab_gpu_bench.py does the same through the colab CLI.

Tests

uv run pytest -q

The tests build a tiny random Qwen3 with Hugging Face transformers, load the same weights into nanoserve, and require identical greedy tokens across batching, chunking, prefix caching and preemption. They also cover the block manager, scheduler, sampler (top-k/top-p against a brute-force reference, distribution, seeds), the HTTP server (streaming, validation, concurrency, metrics), the decode/prefill split (on CPU, with a torch stand-in for the kernel), and on CUDA the Triton kernel against the torch path plus an end-to-end engine run. Everything except the CUDA tests runs on CPU without downloading anything; all 40 pass on an L4.

Notes and limits

  • Only the Qwen3 dense architecture is implemented. Adding another decoder means a model file like models/qwen3.py.
  • Single device; no tensor parallelism, CUDA graphs or quantization.
  • On Apple Silicon (MPS), two slow paths showed up while profiling: advanced indexing for the K/V gather is about 6x slower than index_select, and a [>8, 1024] x [1024, 151936] LM-head matmul falls off the fast path (about 8x slower at 32 rows). The engine uses index_select everywhere and slices the vocabulary projection on MPS.

License

MIT

About

A small vLLM-style LLM inference engine in PyTorch: paged KV cache, continuous batching, chunked prefill, prefix caching, Triton paged-attention kernel, OpenAI-compatible server

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages