I built an LLM inference engine from scratch and measured it against my laptop GPU's limit
I built an LLM inference engine from scratch in plain PyTorch. On my laptop GPU it decodes a single stream at 71% of the fastest speed the GPU’s memory allows. This post covers how I got there, where it still loses to vLLM, and how it serves a public demo behind a router I wrote.
That engine is one part of tiny-llm, a project I built on one RTX 3070 Ti Laptop with 8 GB of memory:
- a 30M-parameter Llama-style model, trained from scratch
- an inference engine in plain PyTorch, with no inference libraries
- an HTTP server with an OpenAI-compatible
/v1/completionsroute and streaming - my own router in front of two replicas on Hugging Face Spaces
You can try the demo. It is a base model trained only on children’s stories, so it continues stories and does not answer questions. If nobody has used it for two days, it will be asleep. Your first request wakes it up: the page asks you to try again, and about 20 seconds later it answers.
One thing to be clear about: the 71% is measured on TinyLlama 1.1B, which I did not train. My own model is only 60 MB, so memory speed is not what limits it: even with CUDA graphs it reaches only 15% of its own ceiling. To test an engine against the memory ceiling, I needed a model big enough to reach it.
Why build it
I wanted to understand what really happens when you send a request to an LLM API.
Inference libraries hide where the time goes. That is what they should do if you only want to serve a model, but I needed to see what happens during each millisecond of a decode step.
So I built every layer by hand and measured each one. I also computed the hardware’s limit first, so every result has an answer to the question “how much speed is left?”
Training, briefly
The model has 29.9M parameters, 8 layers, a width of 512, 8 attention heads, and my own 8,192-token BPE tokenizer. It trained on TinyStories for 996M tokens, about 2.1 epochs, in 2 h 27 min on the laptop.
Throughput was 115K tokens per second at 56% of the GPU’s theoretical peak (MFU). torch.compile gave 57% more speed than eager mode. Final validation loss was 1.2825, with training loss at 1.280, so the two stayed close. After exporting to the Hugging Face format, logits matched to 1.4e-5.
Prompted with “Once upon a time”, it writes things like: “…a little girl named Lily. She was very curious… she found a shiny rock that looked like it was magic… she saw a butterfly that was also very shiny…”
The full run is public on W&B, with loss, learning rate, MFU, GPU charts and sample stories every 200 steps.
Training was the short part of the project. The rest of this post is about serving.
Three ideas you need for the rest of this post
What a KV cache is. To choose the next word, a transformer lets the new token look back at every earlier token. Each token gets a “key” and a “value” that the later tokens read. Without a cache, the model recomputes all of those from the start for every new token. With a KV cache, it stores them and computes only the new token’s pair. The cost is memory, and managing that memory becomes the main problem once many users share a GPU.
Why decode is limited by memory speed. With a KV cache, each step pushes a single token through the model. Every layer multiplies a large weight matrix by one small vector. Nearly every weight is read once and used once, then the next token reads them all again. The arithmetic is tiny compared with the data movement, so the GPU’s compute units mostly wait for memory. Batching helps because one read of a weight can serve many sequences: with the CUDA graphs described below, the engine reaches 886 tok/s in total at batch 8 and 2,138 at batch 32. For a single stream, though, memory speed sets the limit.
How the ceiling is computed. I measured the GPU’s real read speed at 423 GB/s, which is 94% of the rated 448. TinyLlama’s weights in bf16 are 2,098 MiB, or about 2.2 GB. Dividing gives:
423 GB/s ÷ 2.2 GB per token ≈ 192 tok/s per sequence
It is an upper limit, because it counts only the weights. A real step also reads the KV cache and moves other data, so 100% is out of reach.
Getting to 71%
For TinyLlama in bf16 that ceiling is 192 tok/s. My engine reaches 137 tok/s, which is 71% of it and 3.1× Hugging Face generate on the same model and prompt. All of these use TinyLlama 1.1B at batch 1 with a 128-token prompt and 256 new tokens, reporting the median of 5 runs.
| Engine | tok/s | % of ceiling | ms per step |
|---|---|---|---|
Hugging Face generate |
44 | 23% | 22.5 |
| Mine, naive (full forward per token) | 35 | 18% | 28.8 |
| Mine, KV cache | 54 | 28% | 18.6 |
| Mine, KV cache + CUDA graphs | 137 | 71% | 7.3 |
The KV cache alone got me to 28%. Then I profiled a decode step, and the result explained the rest. In eager mode the GPU sat idle for 80% of the step, waiting for the CPU to launch hundreds of small kernels. The problem was not memory bandwidth at all. It was the host.
A CUDA graph fixes this by recording the whole decode step once and replaying it, so the CPU launches one graph and not hundreds of kernels. In the profile, host time per step fell from 19.0 ms to 0.67 ms, and GPU idle time fell to 3%. Decode went from 54 to 137 tok/s. The graph step actually runs a few more kernels than eager (827 against 731), for the attention mask and the cache updates, but once they are inside one graph they cost almost nothing to launch.
With the host out of the way, the GPU’s own work started to matter. I wrote a Triton kernel that fuses the residual add with RMSNorm. It cut a decode step from 1,164 GPU operations to 827 and made graph decode 1.06× faster (128 to 135 tok/s in a side-by-side test). It is the default, so the 54 and the 137 above both include it.
Speed means little if the answers change, so I checked two things. First, my eager path against Hugging Face: over 415 positions in bf16, logits stay within 0.34 of HF’s, and the top token matches at 100.0% in prefill and 99.2% in cached decode. The whole difference comes from the Triton norm, which sums in a different order. With PyTorch’s norm, the output is bit-identical to HF’s.
Second, the graph against eager. Replaying the graph gives exactly the same output as the same step run eagerly with the same shapes. Against the plain eager path it is only bf16-close, because the graph attends over a fixed-size masked window and PyTorch then picks a different attention kernel. In a teacher-forced test on TinyLlama the top token agreed at 98.97%, and every flip was a near-tie, where the top two tokens were within 0.125 of each other.
I have not measured how the remaining 29% splits up, so I won’t guess.
Many users at once
One fast stream is not a service. Three things changed once several requests shared the GPU.
Continuous batching. The engine decodes a fixed number of requests together, each in its own slot. A static batch waits for its longest request to finish before anything new can start. Continuous batching lets a new request take a slot as soon as one frees up. On a test of 64 mixed-length requests with TinyLlama, 8 slots went from 218 to 299 tok/s, a gain of 1.37×. At 32 slots the gain was only 1.05×: 64 requests fill 32 slots only twice, so there is little waiting to remove. It is not a fixed multiplier. Adding CUDA graphs to the continuous engine gave 637 tok/s, which is 2.13× eager continuous at 8 slots.
Paged KV. Simple slots reserve memory for the longest possible sequence, so on my 30M model only 52% of the reserved KV memory held real tokens. A paged cache hands out fixed-size blocks through a per-sequence table, which is the idea vLLM uses. In the same 64 MiB, it reached 97% use and fit 25 sequences at once where contiguous slots fit 8, for 1,049 tok/s against 638, or 1.64×. When memory runs out, the newest sequence is preempted and recomputed later.
The catch. That 1.64× is from an offline test. I have not load-tested --kv paged through the HTTP server. The paged engine also has no CUDA graphs yet, so contiguous slots with graphs are 2.9× faster than paged on the same model and memory. Paged KV is the better memory design, and today it is the slower engine. Every HTTP load test below uses contiguous slots, with graphs on the GPU.
Against vLLM, honestly
The fair question is how this compares with real engines. I ran vLLM 0.33 and llama.cpp with the same client (scripts/load_test.py) and the same TinyLlama bf16 model: 14-token prompts, 128 new tokens, Poisson arrivals, prefix caching off. For vLLM, slots are its --max-num-seqs setting, and for llama.cpp its -np. “Rated load” is the highest rate that keeps time to first token (TTFT) p95 under 1 s.
| Setup | Slots | One stream | Rated load | Peak | Past saturation |
|---|---|---|---|---|---|
| Mine | 8 | 120 tok/s | 4 req/s | ~5.9 req/s | at 16 req/s: 60% refused, TTFT p95 5.6 s |
| Mine | 32 | same engine | 8 req/s | ≥ 13 req/s | at 16 req/s: 15% refused, TTFT p95 2.4 s |
| vLLM | 8 | 162 tok/s | 6–8 req/s | ~8.7 req/s | at 16 req/s: none refused, TTFT p95 46 s |
| vLLM (default) | 256 | 161 tok/s | 32 req/s | ~34 req/s | at 48 req/s: TTFT p95 20 s |
| llama.cpp | 8 | 153 tok/s | 4 req/s | 5.5 req/s, then falls to 3.7 | at 16 req/s: none refused, TTFT p95 188 s |
Some caveats. All of this came from one session on 2026-11-07, and the laptop GPU thermally throttled in every run, at 83–86 °C. I treat any difference under about 15% as a tie.
vLLM is the faster engine. It is 1.35× faster on one stream, and at its default 256 slots it carries far more load than mine. My one-stream number also drops from 137 offline to 120 through the HTTP server. vLLM has FlashAttention and fused kernels, while I use PyTorch’s SDPA over padded slots and capture only the decode step as a graph. That is my first suspect for the gap, but I have not profiled it yet.
What my server does differently is refuse work. When its 32-place queue is full it returns a 429 at once, which caps TTFT p95 at about 5.6 s at 8 slots. vLLM’s queue has no limit, so nobody is refused, and at 16 req/s the p95 wait is 46 s.
This is a trade and not a win. At that load, 60% of my requests got told to try again, and a limit like this could be added in front of vLLM too. It is a policy choice. I think a fast refusal is often better than a 46-second wait, but it depends on the product.
Two CPU Spaces behind my own router
The demo runs the 30M model on two private Hugging Face Spaces on the free CPU tier, with 2 vCPUs and 8 slots each. (Docker Spaces need a $9/month PRO account, so the hardware is free but the account is not.) In front of them sits a public router Space I wrote. It sends each request to the replica with the shortest queue, retries once on the other replica, applies a rate limit, and serves the demo and status pages. Request logs contain no prompts or IP addresses.
The router matters more than the traffic does. A small story demo does not need two replicas. I built it to see whether routing holds up on real, remote, slow machines.
I load-tested it three times: two replicas, then one, then two again, with 128 new tokens per request. The targets were TTFT p95 under 2 s, 99% availability and at most 1% refused.
| Run | Replicas | Held within targets | Peak | tok/s at peak |
|---|---|---|---|---|
| A | 2 | 4 req/s | 4.61 req/s | 590 |
| B | 1 | 1 req/s | 2.31 req/s | 295 |
| C | 2 | 3 req/s | 4.27 req/s | 546 |
Two replicas gave 1.85–2.00× the peak of one, and none of the 2,940 requests failed: 2,346 were served, and the router refused the other 594 with a 429 when the replicas were full. Two replicas reliably hold 3 req/s against one replica’s 1; 4 req/s is borderline, met in run A and missed in run C.
Three things limit how far this goes. I measured on a temporary router with the per-visitor limit turned off, because one laptop would otherwise hit it at once. Each rate is one 60-second sample on shared hardware. And TTFT was measured from my laptop, so it includes the internet path, Hugging Face’s proxy and the router hop: even at 1 req/s the median TTFT was already 1.18–1.24 s, more than half of the 2 s target.
What didn’t work, and what I learned
Quantization first made things slower. My first INT8 path cast the weights back to bf16 on every call, the plain PyTorch way. With graphs it ran at 0.37× bf16 at batch 1 and 0.50× at batch 32, because it moved more bytes and not fewer. Smaller weights only help if the kernel reads them directly. So I wrote a Triton kernel that dequantizes inside the matmul, which reached 1.47× at batch 1 (200 tok/s, past the bf16 ceiling, because the INT8 weights are about half the bytes) and 1.05× at batch 32 (D-016). Quality held up, with validation loss up by only 0.0010 and 98.3% agreement on the top token.
Batching changes some outputs. Only 30 of 64 requests matched solo generate() exactly in the 8-slot continuous test. In bf16, near-ties flip, so I report this number and do not assert on it.
A container hid the CPU count. PyTorch sizes its thread pool from the host’s cores and ignores the container’s CPU limit. Under docker run --cpus 2 on my 16-core laptop it started 8 threads, and decode ran about 5× slower: 105 ms between tokens against 21 ms. The server now reads the container’s CPU limit and sets the thread count to match.
The laptop is a noisy lab. Throttling is in every table. The honest response was to log it beside the results and stop reading small differences as real.
Three lessons I will carry forward:
- Compute the limit before optimizing. Without the 192 tok/s ceiling, 54 tok/s would have looked fine, since it already beat Hugging Face.
- Measure what the GPU is not doing. Idle time pointed straight at the host, where I would have tuned kernels. The decode-overhead step (D-024) and graphs in the continuous engine (D-027) came from that.
- Compare with the real tools and print the result, even when you lose. Using vLLM and llama.cpp as baselines from the start (D-012) kept the claims honest.
Every major choice is written down in DECISIONS.md, including bf16 inference weights (D-023), serving as a production service (D-028), replicas behind my own router (D-031) and the Triton RMSNorm default (D-032). Results tables are generated from the run files, so a number can be traced back to the run that produced it.
What surprised me most was how little one fast kernel can change on its own. I wrote a Triton kernel that does the residual add and RMSNorm in one step instead of two. Tested alone, that step ran 5 to 11 times faster, but the whole model got only about 6% faster (128 to 135 tok/s), because the norm is a small part of each step and the big matrix multiplications take most of the time.
What’s next
- Profile why vLLM is 1.35× faster on one stream
- CUDA graphs for the paged engine, then an HTTP load test of
--kv paged - Release strategies for rolling out a new model version, built on the router
Links:
If you find a mistake in a number, please open an issue. I would rather fix it than defend it.