Files
leleka/docs/cpu-runtime.md
T
Pavlo RatushnyiandClaude Sonnet 5.5 eea344850d perf: run attention on AVX2 kernels, one pass over the keys and values per KV head (STRA-214)
Attention was scalar and latency bound. New kernels in cpu-sys (portable and AVX2 + FMA): scores for a
group of query heads against strided keys, weighted value sums for the group, and a vector exp for the
softmax (relative error below 3e-7). The 8 query heads that share a KV head are processed together over
blocks of 16 positions, so each key and value row is read into L1 once per group instead of once per head.
ops::attend_group_with_sink uses them through Dispatch; a group is bit-identical to its heads run one at a
time (test), and the kernels are checked against f64 references for odd widths, strides and block sizes.

Measured on gpt-oss-20b, 10 threads, 2,000 tokens of context (one run each, noisy machine): decode
attention 27.4 -> 14.5 -> 7.7 ms per token (per-head SIMD, then grouped), decode 116 -> 95 ms per token
(10.5 tokens/s, the same as at short context); prefill attention 8.9 -> 2.1 ms per token. Teacher-forced
quality gate against llama.cpp: best token 97.4% (was 97.1%), mean KL 0.0082 (was 0.0080).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 19:41:09 +02:00

4.1 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.

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.
  • KV cache is f32 (about 98 KB per token): the default context is 16,384 tokens (1.6 GB). Long contexts need f16/q8 KV and the RAM planning of STRA-204/205/210. Cold-expert streaming (KAR) is not implemented.
  • One request at a time (by decision); sampling is temperature and top-p over the 1,024 best logits.
  • The control plane still tokenizes with its Qwen assets and parses Qwen output; pointing it at this worker needs gpt-oss tokenizer and Harmony handling there. iGPU/NPU helpers for prefill (STRA-207) are a spike.