nibble
4-bit LLM inference on CPU, from scratch
2026 · in progress · C++20 · AVX2 · POSIX threads
What it is
A from-scratch C++20 inference engine that runs a 4-bit Llama model (TinyLlama-1.1B-Chat, Q4_0 GGUF) on a laptop CPU. No ML, BLAS, or tokenizer libraries: only the C++ standard library, POSIX, and threads.
Why
Local AI on any laptop, built under a hard constraint and held to real proof. Every claim is checked against two references: PyTorch for the exact math, llama.cpp for file-format behaviour and speed.
How it works
- GGUF parser with memory-mapped weights
- The model's own SentencePiece tokenizer
- A scalar reference kernel plus AVX2/FMA/F16C kernels, picked at runtime
- A fork-join thread pool
- A KV-cached Llama forward pass: 22 layers of RMSNorm, RoPE, grouped-query attention, and SwiGLU
- Greedy and top-p sampling behind a CLI
Every generated token streams all 555 MiB of weights once, so decode speed is set by bytes moved. That is why 4-bit weights matter.
Results
- Matches PyTorch on the same dequantized 4-bit weights to 2.4e-5 max logit error, float32 noise. llama.cpp on those weights differs by up to 0.66.
- AVX2 kernels match the scalar reference to 1.9e-5.
- The tokenizer matches llama.cpp on 13 edge cases and Hugging Face on a 56-token prompt.
- 44.5 tok/s decode on a Core Ultra 9 285H; llama.cpp reaches 85.9 under the same settings. nibble keeps exact f32 activations where llama.cpp uses int8.
Decisions
- One model family, one format: Llama and GGUF Q4_0.
- Correctness first: exact f32 activations, with the speed cost stated rather than hidden.
- Every unsupported input is an explicit error, never a silent fallback.