• 4 min read

Ember

An LLM inference engine I wrote from scratch, with a paged KV cache, continuous batching and Triton kernels, running on a 4 GB laptop GPU.

PythonPyTorchTritonCUDALLM inferenceFastAPI

Ember results page

What it is

Ember serves Qwen3-0.6B and Qwen3.5-0.8B on one GTX 1650 Ti laptop GPU with 4 GB of memory and no tensor cores. I wrote the model forward pass, the paged KV cache, the continuous-batching scheduler, the OpenAI-compatible streaming server and the Triton kernels myself.

On the same GPU, prompts and load generator, it gets 3.4 times the throughput of llama.cpp. Its greedy output matches Hugging Face token for token. Every number on this page links back to raw data, the exact command and the commit in an evidence log in the repo.

How it works

Qwen3.5 is a hybrid model: most layers are Gated DeltaNet, a linear-attention layer with a fixed-size state, and the rest are normal attention. So the cache holds two kinds of memory, KV blocks for the attention layers and a pool of state slots for the DeltaNet layers.

Some of the things I learned on the way:

  • fp16 was 6 times slower than fp32 on this GPU. It has no tensor cores. I measured it, then stored the weights in fp16 and did the math in fp32, converting in registers inside a custom Triton GEMM. That left 5.8 times more memory for the KV cache.
  • A latency tail that was the compiler. One Triton kernel took a per-step value as a compile-time constant, so it recompiled itself (about 1 second) in the middle of serving. A per-step trace found it. The p99 time to first token at light load went from 1,435 ms to 775 ms.
  • Profiling killed the planned kernel. Flash attention for prefill was next on my list, but attention turned out to be 3 to 6% of prefill time. Re-tiling the prefill matmuls cut long-prompt time to first token by 10% instead.
  • My first DeltaNet prefill kernel was 4 times slower than PyTorch because of register spills. A sweep over compiled kernel shapes found one without spills. The second version is 2.6 to 6.3 times faster per layer.

Results

Qwen3-0.6B, same GPU, same 32 prompts per point, same client. llama.cpp ran in fp16 with its best setting on this GPU (8 parallel slots).

Emberllama.cpp
Chat, offline throughput466 tok/s135 tok/s
Long prompts (~520 tokens), offline throughput139 tok/s41 tok/s
Time per output token, chat at 0.25 req/s (p50)14.7 ms28.2 ms
Highest chat load meeting the SLO (p99 TTFT ≤ 2 s, p99 TPOT ≤ 100 ms)≥ 2 req/s1 req/s
First-token latency, long prompts at 0.5 req/s (p50)337 ms135 ms

llama.cpp still wins first-token latency at light load. Its fp16 prefill is faster than my fp32-compute design allows on this GPU.

How each milestone moved the same chat workload:

MilestoneWhat changedOffline throughputp99 TTFT at 0.25 req/s
M2Static batching, fp3247 tok/s25.1 s
M3Paged KV cache and continuous batching90 tok/s0.52 s
M4Paged-attention kernel, CUDA graphs, K-major matmuls, fused DeltaNet466 tok/s0.66 s

The M4 changes took the Qwen3.5 decode step from 69 to 96 ms down to 14 to 26 ms. CUDA graphs alone took a single-row step from 74 ms to 16 ms.

Correct before fast

  • The Qwen3 and Qwen3.5 forward passes match Hugging Face transformers bit for bit in fp32. 50 of 50 golden prompts give identical greedy tokens through the full serving engine, including when requests are preempted.
  • The fused DeltaNet prefill kernel adds in a different order, so its logits are within 1.4e-4 of Hugging Face. The greedy tokens are the same, and the PyTorch path stays bit-exact.
  • 395 tests in four tiers: CPU unit, integration, GPU kernels and real-weight golden tests. A gate script runs them before every push, and the scheduler has property tests with Hypothesis.
  • Benchmarks are interleaved A/B repeats with ranges. Baselines run from git worktrees at their own commits, and I report regressions next to wins.

Limits

  • One early Qwen3.5 serving run failed and I couldn’t explain why. It hasn’t happened again in dozens of runs since, but I don’t claim Qwen3.5 serving is stable yet.
  • Everything is measured on one GPU. Prefix caching, chunked prefill, speculative decoding and quantization are planned and not built.

Stack

Python 3.12, PyTorch, Triton, CUDA graphs, FastAPI, safetensors, Hypothesis, uv, WSL2.

Back to top