How my from-scratch LLM engine caught up with llama.cpp on the CPU
November 2026 · Fatin Mojumder
The short version
- In October my engine generated 12.6 tokens per second and llama.cpp was almost 9x faster. I wrote up why: my engine expanded the model to a format six times larger, so every token dragged far more data through memory.
- I fixed that. It now runs on the compressed weights directly, and on the same MacBook it generates 74.6 tokens per second on 4 threads, against 71.9 for llama.cpp's CPU backend. Memory dropped from 5,756 MB to 1,079 MB.
- It is not faster than llama.cpp overall. Reading the prompt is still about 2x slower, and llama.cpp on the GPU generates 111 tokens per second.
Where I left off
LLM Engine (the project I now show as TokenForge) is a C++ program I wrote to run Meta's Llama 3.2 on a laptop CPU: it reads the model file, splits text into tokens, runs the transformer and picks the next word, with no machine learning libraries.
The October benchmark showed that almost all of the time per token went into matrix vector products, and that those were slow because of how many bytes they read. The model file stores each weight in about 5 bits. My engine converted every weight to a 32 bit float at load, so each token read 4.9 GB of weights instead of 0.8 GB. The writeup ended with a list of six changes. This post covers the five I made and what they did.
What I changed
1. Stop expanding the weights. The engine now memory maps the model file and points straight at the packed weight blocks inside it. Before each matrix vector product, the input vector is quantized once to 8 bit blocks, and every output row becomes an integer dot product on the packed Q4_K, Q6_K or Q8_0 blocks, with the block scales applied once per sub-block. That is the approach llama.cpp uses, and the block layouts are llama.cpp's so the file decodes identically; the code is my own. Weight data read per token went from 4.94 GB to 763 MB, about 6.5x less.
2. Write the inner loop for the hardware. The dot products have NEON versions for Apple Silicon (using the sdot instruction, with a fallback for chips without it), an AVX2 version for x86, and a plain scalar version that the others are tested against.
3. Split the rows across a worker pool. In October the thread pool only split up attention heads, which are tiny. Now a persistent fork-join pool splits the rows of every matrix vector product, handing out chunks dynamically so the M4's performance and efficiency cores both stay busy.
4. Stop copying what can be shared. The output layer now points at the embedding matrix instead of copying it, which saved about 1 GB. The Q, K and V projections share one quantized copy of their input, and so do the gate and up projections.
5. Precompute RoPE. The rotary position frequencies are now computed once instead of for every token. While doing this I found the engine was ignoring the rope_freqs scaling factors that Llama 3 model files carry, so that was a correctness fix as well as a speedup.
The sixth item, processing the whole prompt as one batch, is still open.
Results
Same machine and model as before: MacBook Air, Apple M4 (10 cores, 24 GB), Llama 3.2 1B Instruct Q4_K_M, a 42 token prompt, 128 generated tokens, greedy decoding, median of 3 runs. llama.cpp is the Homebrew build of llama-bench on the same file.
| engine | generation tok/s | prompt tok/s | peak memory |
|---|---|---|---|
| LLM Engine, October (32 bit weights) | 12.6 | 8.7 | 5,756 MB |
| LLM Engine, 1 thread | 30.5 | 31.0 | 1,079 MB |
| LLM Engine, 4 threads | 74.6 | 76.4 | 1,078 MB |
| LLM Engine, 10 threads | 72.1 | 75.2 | 1,083 MB |
| llama.cpp, CPU, 1 thread | 27.0 | 50.0 | |
| llama.cpp, CPU, 4 threads | 71.9 | 163.6 | |
| llama.cpp, GPU (Metal) | 111.0 | 863.7 |
Generation is 5.9x faster than in October and level with llama.cpp's CPU backend at the same thread count: 30.5 against 27.0 on one thread, 74.6 against 71.9 on four. Memory is 5.3x lower and now sits close to the file size plus the KV cache, because the weights are read straight from the mapped file. Loading takes 0.09 seconds instead of 4.4.
What the numbers say
One thread is no longer limited by memory. In October a single thread pulled about 62 GB/s of weights, roughly half the M4's bandwidth, and still only managed 12.6 tokens per second because each token needed 4.9 GB. Now a single thread moves about 23 GB/s. Each byte needs more work (unpacking blocks and applying scales), so one core runs out of arithmetic before memory runs out of bytes. That is why threads help now, when they couldn't have helped much before.
Four threads is the sweet spot, and I don't fully know why. Four threads move about 55 GB/s and ten threads are slightly slower. That is less than the single thread pulled in October, so it isn't simply the memory limit. My guess is the cost of synchronizing the pool, which hands out and collects work for each of the roughly 113 matrix vector products per token (seven in each of 16 layers, plus the output layer), together with the slower efficiency cores. I haven't measured that yet.
The prompt oddity went away. In October the prompt ran slower than generation even though, one token at a time, they do the same work. Now they run at the same speed (31.0 and 30.5 on one thread), which is what you would expect. Batching the prompt is the next fix; llama.cpp turns it into matrix-matrix products, which is why it reads prompts about 2x faster on the CPU.
Checking it still gives the right answers
Faster is only useful if the output is the same. I kept the old 32 bit path behind a flag, LLM_REFERENCE_FP32=1, and wrote llm_compare to run both side by side on the real model. Over 64 greedy steps, the quantized kernels picked the same next token as the 32 bit reference 63 times, and the logits stayed very close (cosine similarity at least 0.997). The one disagreement came at a step where the top two tokens were nearly tied, which rounding the activations to 8 bits can flip.
The kernels also have unit tests against plain floating point math, and the NEON versions are tested with and without the sdot instruction. One limit: all of this compares my engine with itself, not with llama.cpp's output.
What's still missing
- Prompt batching. Prompts are still processed one token at a time.
- Tokenizer parity. On 8 test strings, my tokenizer matches llama.cpp on 3. Plain sentences and numbers match; contractions, extra spaces, tabs and newlines, camelCase and non-ASCII text don't, because I haven't implemented the Llama 3 pre-tokenizer rules yet. Prompts like that reach the model as different tokens.
- The GPU. The engine is CPU only. llama.cpp on the M4's GPU still generates about 1.5x faster than my best CPU number.
What I learned
The October measurements told me exactly where to dig, and the fix they pointed at, reading fewer bytes per token, delivered most of the gain. The other lesson is that fixing one bottleneck exposes the next. Once the bytes dropped 6.5x, a single core became limited by arithmetic and four cores by something I still have to measure. I wouldn't have seen either without running the benchmark again after the change.
The code and benchmark tools are in the repo. Raw results for this post are in bench/results/20261115-012213 (speed and memory) and bench/results/verify-20261115-012039 (correctness and tokenizer checks).