Files
leleka/docs/cpu-runtime.md
T

9.4 KiB

Leleka CPU runtime (AVX2 / AVX-VNNI)

Status on 2026-10-14: runs gpt-oss-20b (MXFP4 GGUF, the lmstudio-community file with SHA-256 65d06d31a3977d553cb3af137b5c26b5f1e9297a6aaa29ae7caa98788cde53ab) end to end through the control plane's worker protocol. Not wired into the control plane's request path yet (see "Not done").

Crates (a separate runtime; shares only crates/backend-api with others)

Crate Role
leleka-cpu-sys The only unsafe code of this runtime: SIMD kernels (std::arch, AVX2 and AVX-VNNI plus a portable level), read-only file mapping, core topology. Every public function is safe and checks lengths and CPU support. Optional test features: c-reference (the first, C implementation, compared bit for bit) and ggml-oracle (llama.cpp's quantizers).
leleka-cpu-kernels Runtime dispatch by CPUID (LELEKA_CPU_ISA overrides and fails rather than silently switching).
leleka-gguf Bounded GGUF reader and per-token byte inventory.
leleka-gptoss The decoder: persistent thread pool, chunked prefill, prefix reuse, f32 KV cache, sampling.
leleka-tokenizer gguf_bpe: gpt-oss tokenizer from the GGUF vocabulary.
leleka-harmony Prompt rendering and output parsing of the Harmony chat format.
leleka-cpu-worker The executable: leleka-cpu-worker --serve --model <gguf> [--threads N] [--ctx N] [--no-warm] [--seed N].

Worker protocol

Same lines as Strata's --serve mode (control-plane/src/wire.rs). The worker prints READY <ctx> stop after loading (and, unless --no-warm, reading every page of the model once), then per request: PP <done> <total> after every prefill chunk, REUSED <n>, T <id> per generated token, and one DONE <generated> <prompt_tokens> <prompt_ms> <decode_ms> stop|length|cancel 0 0 <reused> 0 0 (or ERR). Input: GEN <max_new> [temperature=] [top_p=] [seed=] <ids>, STOP, QUIT. The end-of-generation marker (<|return|>, <|call|> or <|endoftext|>) is emitted as the last T so the caller can tell an answer from a tool call. A STOP while no request runs is ignored. Tokenization and Harmony rendering/parsing belong to the caller (the crates above are the pieces for it).

What was measured (Core Ultra 7 265U, 15 W, 32 GB class laptop; one noisy machine, no quiet gate for

every number; see the work results on STRA-214 and STRA-217 for the paired runs)

  • Decode, 10 threads, warm pages: 85-100 ms per token (10-11.7 tokens/s). Bionic's llama.cpp measured 7.6-8.7 tokens/s on the same prompt on this machine (STRA-209, partial baseline).
  • Decode at 2,000 tokens of context: 95 ms per token (10.5 tokens/s); attention costs 7.7 ms of it (27.4 ms with scalar attention loops). Attention reads each KV head's keys and values once for its 8 query heads (blocked in L1) with AVX2/FMA kernels and a vector exp.
  • Prefill, 10 threads, chunk 64: about 51 ms per token (19.6 tokens/s) on 256 random-id tokens. Bionic's iGPU engine measured about 90 tokens/s; its CPU engine about 14.8 tokens/s. Prefix reuse makes the second and later requests of a conversation cheap.
  • Correctness: first 16 greedy tokens equal llama.cpp's for "The capital of France is"; chunked prefill and prefix reuse are bit-identical to feeding tokens one at a time; kernels at every instruction-set level are tested against exact integer references and against llama.cpp's quantizers.

Serving through the control plane

The control plane (strata-next serve) serves /v1/responses for gpt-oss with this worker. Configuration (JSON file, or STRATA_GPTOSS_GGUF):

{
  "bind": "127.0.0.1:8081",
  "model": "gpt-oss-20b",
  "engine": {
    "executable": "target\\release\\leleka-cpu-worker.exe",
    "args": ["--model", "<gpt-oss-20b-MXFP4.gguf>", "--ctx", "8192", "--threads", "10"],
    "gptoss_gguf": "<gpt-oss-20b-MXFP4.gguf>",
    "startup_timeout_s": 300
  }
}

gptoss_gguf selects the gpt-oss family and takes the tokenizer from that file. The control plane renders the Harmony prompt from the Responses request (instructions, input items, tools, tool_choice, reasoning effort), sends the token ids to the worker, and turns the generated Harmony stream into reasoning, message and function_call items (buffered and streaming). The special markers of the template are written as private-use sentinel characters and only those become special tokens, so text from a client cannot forge a marker. Run benchmarks/tools/Smoke-GptOssStack.ps1 -Model <gguf> to check the whole stack: plain question, function call, tool-result round trip with prefix reuse (about 130 of 180 prompt tokens were cached on the second turn), and a streaming request. Not supported for this family: custom tools. Tool-call arguments arrive in one piece when the call ends (the stream decoder buffers them).

Known limits / not done

  • The first-step log-probabilities differ slightly from llama.cpp's (0.4 vs 0.45 for the top token); the teacher-forced logit comparison that would explain it is still to be done. Greedy tokens agreed on the prompts tried, but near-ties can flip.
  • The KV cache holds half-precision keys and values (about 49 KB per token, as llama.cpp's default): the worker's default context is 32,768 tokens (1.6 GB). A 131,072-token context would take 6.4 GB; RAM planning for it with the 12 GB model (STRA-204/205/210) and cold-expert streaming (KAR) are not done.
  • One request at a time (by decision); sampling is temperature and top-p over the 1,024 best logits.
  • iGPU/NPU helpers for prefill (STRA-207) are a spike: the NPU driver is present on the reference laptop, OpenVINO is not installed yet.

Decode speed limit on the reference laptop (2026-10-15)

The reference laptop has one 32 GB DDR5-5600 module (single channel, 44.8 GB/s peak). One decoded token of gpt-oss-20b reads about 2.56 GB (experts 1.27, attention Q8_0 0.68, output layer 0.62), so decode is capped near 14-15 tokens/s at the measured 36-40 GB/s, for this runtime and for llama.cpp alike. The CPU and the integrated GPU share that channel: streaming from both at once gave about 37 GB/s in total (GPU 28 GB/s alone, about 21 GB/s while the CPU decoded), so splitting decode across them does not add bandwidth. Verifying several tokens in one CPU pass is compute-bound at about 70 ms per token on a noisy machine, which limits CPU-side speculative decoding. Prompt-lookup drafting accepted only 1.03-1.2 tokens per step on the four set2 texts. A second memory module (dual channel) is the lossless way past the cap.

Experimental lossy mode: fewer experts per token

LELEKA_EXPERIMENTAL_EXPERTS_USED=N routes each token to N experts instead of 4 (off unless set; logs a warning; no quality gate allows it). Decode and teacher-forced quality against llama.cpp on table-igpu / docs-cpu (set2), one noisy run:

experts decode tok/s best-token agreement mean KL
4 (model) 8.7 93.4% / 92.7% 0.016 / 0.023
3 10.2 70.4% / 72.8% 0.161 / 0.185
2 12.8 51.0% / 52.4% 0.460 / 0.477

Removing expert bytes costs far more quality than it buys speed; not recommended.

Experimental quality probe: attention and output weights at fewer bits

LELEKA_EXPERIMENTAL_FAKE_BITS=N re-rounds the attention and output Q8_0 weights to an N-bit symmetric grid per block of 32 (absmax scale in half precision), stored in Q8_0 so there is no speed change; it only measures what an N-bit format would cost. Teacher-forced against llama.cpp on table-igpu (set2; baseline 8-bit: mean KL 0.016, 93.4% agreement):

bits per weight (scale not counted) best-token agreement mean KL
7 84.9% 0.053
6 81.4% 0.071
5 76.4% 0.112
4 63.5% 0.270

Plain block rounding of the attention and output layers is not usable below 8 bits on this model; a 2.5-bit format of that kind (a KAR-style group-32 scale-plus-planes layout is the same rounding) is far outside what the model tolerates. Only calibrated quantization could do better, and that was not tried. The first run of this probe had a half-precision conversion bug (small scales flushed to zero); the numbers above are from the fixed code.

Prompt-lookup speculation: measured acceptance on agent-style output (2026-10-15)

The model (greedy, run_real) generated 320 tokens for four agent-style prompts over about 800 tokens of context (a Rust file, or the first 2,200 characters of this document); the draft is the continuation of the latest earlier match of the last N tokens in prompt plus output, up to K tokens, and a step accepts the longest prefix equal to what the model produced.

task tokens per step (N=2..4, K=4 / K=8)
rewrite the code with renamed functions (copies the input) 3.3-3.7 / 5.4-5.8
list function names as JSON and repeat the first function 1.7-2.0 / 1.8-2.1
reproduce a section of the notes as bullets 1.4-1.6 / 1.4-1.7
explain what the code does (new text) 1.05-1.2 / 1.05-1.2

Four prompts, one run each: an indication of the spread, not a workload estimate. Verification cost decides whether this pays: on the CPU a chunk of 4-8 tokens costs 65-78 ms per token on the noisy machine (about 50 ms quiet), against about 110 ms for a decode step, so with 3.5-5.7 tokens per step the CPU gain is at most about 1.3x; the integrated GPU (compute about 3-4x the CPU, but expert reads grow with the number of distinct experts in the batch) could reach about 20 tokens/s on copy-heavy output by the estimate in STRA-220, and about nothing on free-form text.