Kjarni against PyTorch, ONNX Runtime and llama.cpp: the numbers and why
Same weights, every engine at its best settings. Where a 28 MB Rust inference engine is level, where it's ahead, where it's behind, and the measured reason for each.
Kjarni is an inference engine written in Rust. It runs embeddings, reranking, classification, Whisper, summarisation, translation, CLIP and chat models from Python, C#, Go, C or the command line, with no PyTorch, no ONNX Runtime and no CUDA underneath. Its outputs match PyTorch's: embeddings to within 1e-6.
For the 0.8.2 release I measured it against PyTorch, ONNX Runtime and llama.cpp. Below are the numbers, and for each difference, the reason as measured.
The short version: Kjarni's point is low overhead. It installs in 28 MB instead of 1.4 GB, starts in a tenth of a second instead of nearly three, and drops into a Docker image or a C#, Go or C program without bringing Python along. Speed is competitive: on the CPU it's level with PyTorch, ahead on some tasks and behind on others. On the GPU, through WebGPU, it's behind llama.cpp's CUDA backend on small models and within 5% of it from 3B parameters up.
How it was measured
- Same weights. PyTorch and ONNX Runtime run the same files Kjarni loads. Quantised models (Q4_K_M) are compared only with llama.cpp, on the same GGUF file.
- Every engine at its best settings. Each engine is tried at its default thread count and at 8, and PyTorch in both bf16 and f32. The faster run is reported.
- Same output. Every result is checked against PyTorch's before it counts.
- A quiet machine. Each run is a fresh process on an idle machine, best of several runs, all on one fresh boot.
Intel i7-13700 (8 performance cores, 8 efficiency cores, 24 threads), 64 GB RAM, NVIDIA RTX A2000 12 GB, Linux. Kjarni 0.8.2, PyTorch 2.13, ONNX Runtime 1.31, llama.cpp b11901.
This is one machine. Kjarni runs on Windows, macOS and ARM, and its tests pass there, but I haven't benchmarked those to any extent, and the CPU results in particular could look different on other hardware. The scripts and every raw number are in bench/.
The numbers
Encoders on the CPU, a batch of 64 texts, in ms (lower is better):
| Kjarni | PyTorch | ONNX Runtime | |
|---|---|---|---|
| MiniLM-L6 embeddings | 194 | 164 | 175 |
| mpnet-base embeddings | 1,231 | 1,317 | 1,189 |
| MiniLM rerank | 227 | 210 | 214 |
| DistilBERT classification | 144 | 143 | 147 |
| bge-m3 embeddings | 4,043 | 4,965 | 4,644 |
Other models on the CPU:
| Kjarni | Best other | |
|---|---|---|
| Whisper-small, 58 s of speech | 10.7x real time | openai-whisper 10.6x; faster-whisper 9.1x, 18.3x in int8 |
| Summarisation (distilbart) | 1.65 s | PyTorch 1.76 s |
| Translation (flan-t5) | 625 ms | PyTorch 585 ms |
| CLIP image embeddings | 65 images/s | open_clip 70 |
Text generation on the CPU, tokens per second (higher is better):
| Kjarni | Other engine | |
|---|---|---|
| Qwen2.5 0.5B, bf16, generation | 26.5 | PyTorch 24.6 |
| Qwen2.5 0.5B, bf16, 512-token prompt | 655 | PyTorch 969 |
| Llama 3.2 1B, bf16, generation | 10.9 | PyTorch 10.9 |
| Llama 3.2 1B, bf16, 512-token prompt | 264 | PyTorch 405 |
| Qwen2.5 0.5B, Q4_K_M, generation | 50.3 | llama.cpp 67.2 |
| Llama 3.2 1B, Q4_K_M, generation | 30.6 | llama.cpp 33.6 |
The bf16 rows generate after a 512-token prompt. The Q4_K_M rows, and the GPU table below, start from a nearly empty context, as llama.cpp's own benchmark does.
Text generation on the GPU, same Q4_K_M file, tokens per second:
| Kjarni (WebGPU) | llama.cpp Vulkan | llama.cpp CUDA | |
|---|---|---|---|
| Qwen2.5 0.5B | 222 | 236 | 332 |
| Llama 3.2 1B | 191 | 193 | 216 |
| Llama 3.2 3B | 86 | 79 | 91 |
| Qwen3.5 4B | 60 | 53 | 62 |
| 512-token prompt, 0.5B | 2,586 | 10,104 | 14,546 |
Size and start-up, MiniLM:
| Kjarni | PyTorch | |
|---|---|---|
pip install | 28 MB, 1 package | 1,358 MB, 45 packages (CPU-only) |
| New process to first embedding | 91 ms | 2,758 ms |
| Memory after a batch | 273 MB | 649 MB |
Why they differ
PyTorch's matrix multiply is faster per core. On one thread PyTorch encodes the bge-m3 batch in 24.4 s and Kjarni in 45.1 s. PyTorch calls Intel's MKL; Kjarni uses faer, a pure-Rust library. PyTorch's own profiler puts 86% of its time in the matrix multiply, so the gap is the kernel, not Python or per-op overhead.
Thread counts matter on this CPU. The i7-13700 has fast and slow cores. PyTorch was fastest at 8 threads, one per fast core, and slower at its default of 16 (bge-m3: 4.8 s against 6.0 s). Kjarni was fastest using all 24 threads. That's why the tables give each engine its best thread count.
Generation is limited by memory bandwidth. Each generated token reads every weight once. 0.99 GB of bf16 weights at 26.5 tokens a second is 26 GB/s, close to what this machine's memory delivers, and PyTorch reaches 24 GB/s. Neither engine can go much faster, so they're level. Prompts are different: a prompt is one large matrix multiply, where PyTorch's faster kernel wins by 1.5x.
Against llama.cpp on the CPU, Kjarni is behind on quantised models. At 75–91% of llama.cpp's generation speed on the same file, its quantised kernels have room to improve. I haven't profiled that gap in detail yet.
On the GPU, Kjarni loses about 1 ms per token outside the shaders. Generation time per token fits a straight line, a fixed cost plus model size divided by bandwidth. Kjarni's shaders read weights at 187 GB/s, the same as llama.cpp's CUDA kernels. The difference is a fixed cost of about 1.1 ms per token against 0.2 ms, spent preparing and submitting work through WebGPU and reading the result back. That's about a quarter of each token on a 0.5B model and 7% on a 4B, which is why the gap closes as models grow. llama.cpp's Vulkan backend has almost no fixed cost but reads weights more slowly (159 GB/s), so Kjarni passes it from 3B up.
On the GPU, Kjarni's prompts are 5–7x slower than llama.cpp CUDA's. Kjarni's GPU profiler shows 63% of a 0.5B prompt going to attention computed the simple way: the full matrix of scores is written to memory, normalised and read back. llama.cpp uses flash attention, which never writes that matrix. That's next on Kjarni's list.
Memory: PyTorch reads weights only when they're used. On MiniLM Kjarni uses less than half PyTorch's memory, but on bge-m3 it uses more: a peak of 3.4 GB against 2.4 GB. bge-m3 has a 250,000-word vocabulary, a 1 GB table of which a batch of English uses a few thousand rows. PyTorch maps the weight file and only the rows it reads are loaded; Kjarni copies the whole table into memory at load. Leaving that table mapped is a small change and is planned.
Size and start-up: there's no framework to load. PyTorch's 2.8 seconds to a first
embedding is mostly import torch (2.0 s). Kjarni is one native library and its
Python wrapper, so the same process takes 91 ms, and the install is one 28 MB
package.
When to use which
- PyTorch is the reference, and on a server with a GPU or many identical cores it'll usually be faster.
- ONNX Runtime is the fastest for embeddings on an NVIDIA GPU, by a wide margin.
- llama.cpp is faster for quantised chat models, especially on CUDA and for long prompts.
- Kjarni fits where size, start-up time and dependencies matter: a desktop app, a CLI, a C#, Go or C program, a small Docker image or a serverless function. You get PyTorch's output at competitive speed without shipping Python or 1.4 GB of framework, and one GPU backend that runs on NVIDIA, AMD, Intel and Apple hardware.
If a model you need isn't supported, open an issue. Most new models are a variation on an architecture Kjarni already runs, and adding one is usually a small change.
Try it
python -m venv .venv && . .venv/bin/activate
pip install kjarni
python - <<'EOF'
import kjarni
m = kjarni.Embedder(model="minilm-l6-v2", device="cpu", quiet=True)
v = m.encode("Same output as PyTorch, 28 MB instead of 1.4 GB")
print(len(v), v[:4])
EOF
384 [0.019405771046876907, -0.03303762897849083, -0.08054298907518387, 0.055704936385154724]
Getting it
- GitHub: kjarni-ai/kjarni
- Python: pypi.org/project/kjarni; C#: NuGet; Go: kjarni-go; C and the CLI: releases
- Every number here and the scripts behind it: bench/BENCHMARKS.md
If this was useful, ★ star Kjarni on GitHub. It is how other people find it.
Kjarni runs the same engine on every platform: