Commit Graph
10 Commits
Author SHA1 Message Date
Pavlo Ratushnyi c1e7c02d1e STRA-338 size-rest: cpu-kernels, cpu-sys, gguf, gptoss and quant-core within 400 lines per file
Oversize files before/after: gptoss 2 -> 0 (lib.rs 1788 into 14 modules, synthetic_model.rs 922 into topic modules),
cpu-sys 2 -> 0 (lib.rs 1007, simd.rs 1503 with its inline x86 module split by kernel family), gguf 2 -> 0,
cpu-kernels 1 -> 0, quant-core 1 -> 0 (iq2s_grid.rs reflowed four entries per line, entries unchanged).
Modules are named after what they hold; public paths are re-exported unchanged; unsafe blocks and SAFETY comments moved
verbatim (cpu-sys 70 -> 70).

Census prod/test (fn>30 / cx>10 / nesting>4 / wide lines): cpu-sys 19/16, 3/0, 2/0, 5/2 -> 11/16, 3/0, 2/0, 3/2;
gptoss 13/16, 6/4, 1/0, 1/7 -> 12/14, 6/4, 1/0, 1/8; gguf 6/5, 6/0, 0/0, 3/1 -> 6/3, 6/0, 0/0, 3/1;
cpu-kernels and quant-core unchanged. Test ids and outcomes unchanged (recorded in docs/architecture/size-split-rest.md).
2026-11-06 16:28:24 +01:00
Pavlo RatushnyiandClaude Opus 5.5 87637c2b0b perf: multi-activation kernels make the CPU small-batch pass cheaper, bit-identical (STRA-220)
Profiling the speculative verification pass (batch_cost, position 900) showed its extra cost is compute in the
int8 weight products, not expert bytes: distinct experts per layer rise only from 4 to 9.5 at 9 tokens, while the
Q8_0 projections, whose weights every token shares, cost 3.6-4x a single step. Each (row, token) dot redid the
MXFP4 decode and scale work.

cpu-sys gains multi-activation kernels (portable, AVX2, AVX-VNNI) for MXFP4 and Q8_0 rows: the row is decoded once
per 64-block segment and up to 16 activations run over it with exactly the one-token kernel's accumulators and
final reduction, so every output is bit for bit the one-token kernel's (tests/multi.rs: every level, 1-16
activations, 13 row lengths). run_region uses them for jobs with two or more activations (speculative batches and
prefill chunks); one-token decode keeps its kernel. A new synthetic test checks a 20-token batch at every level
against feeding tokens one at a time. Model::set_multi_activation(false) restores the per-token path for paired
measurements; Profile counts the expert sets read.

batch_cost gains a per-size phase breakdown and an `ab` mode that runs every pass without and with the new kernels
back to back and checks the logits are identical. Paired, 9 rounds, real model: identical logits in all 90 paired
passes; time ratio with/without 1.01 (1 token), 0.98, 0.92, 0.90, 0.83, 0.81, 0.77, 0.79, 0.76 (2..9 tokens).
MEASURED_COST becomes 0.737 + 0.421 n (was 0.30 + 0.631 n). ab_speculation (3 pairs, outputs identical in all 24
runs) gave 1.34 / 1.04 / 0.97 / 0.94, within the pair-to-pair spread of the run before the change; replaying the
outputs through the drafter predicts 1.59 / 1.15 / 1.00 / 0.99, so the serving loop loses time the pass-cost model
does not see. Prefill also uses the kernels (bit-identical); its speed was not measured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 13:07:38 +02:00
Pavlo RatushnyiandClaude Sonnet 5.5 8d96e9c44c perf: overlap weight expansion with NPU execution, batch products, SIMD half conversion, graph cache (STRA-207)
- service: BATCH n lets it see each product's successor and expand the next weights on a thread
  while the device runs the current one; buckets up to 4096; OpenVINO CACHE_DIR compile cache
- client: batched expert products, layout sized by batch, default batch 2048
- cpu-sys/cpu-kernels: AVX2 F16C f16_to_f32 kernel, exact for every half bit pattern
- npu-worker: memory admission before an NPU prefill (falls back to the CPU when RAM is short)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-15 12:22:13 +02:00
Pavlo RatushnyiandClaude Sonnet 5.5 330b31b902 perf: run the router and the expert activation on AVX2 kernels (STRA-214)
The router's 32 single-precision dot products (2,880 elements each) and the expert activation (a scalar exp
per element) were the largest serial pieces left outside the matrix products. cpu-sys gains a vector
swiglu-oai kernel (portable and AVX2 + FMA, using the same vector exp as the softmax, relative error below
3e-6 in the tests, including gates far beyond the clamps), and the router reuses the strided dot kernel.

Measured on gpt-oss-20b, 10 threads, one run each on a noisy machine (not paired): router 3.0 -> 1.3 ms
and activation 3.0 -> 1.3 ms per decoded token; in prefill (chunk 64) router 2.5 -> 0.6 ms and activation
2.5 -> 1.6 ms per token. Decode 86 ms per token (11.6 tokens/s) over 19 tokens. Teacher-forced gate against
llama.cpp: best token 97.4%, mean KL 0.0088 (criterion <= 0.012; the previous build gave 0.0077, the
difference between the two builds is 0.0050, at the noise floor).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 22:46:09 +02:00
Pavlo RatushnyiandClaude Sonnet 5.5 4c00f0d209 feat: keep the gpt-oss KV cache in half precision (STRA-214)
Keys and values are stored as IEEE half floats (49 KB per token instead of 98 KB, llama.cpp's default
cache type). New cpu-sys kernels read them directly: attention scores and weighted value sums over half
floats (portable and AVX2 + FMA + F16C, blocked over positions like the f32 ones) and a vector
float-to-half conversion that is bit-identical to the portable one. The decoder writes rotated keys and
values through Dispatch::f32_to_f16; the synthetic f64 reference rounds K and V the same way.

Teacher-forced gate against llama.cpp (same 383 tokens): best token 97.4% (unchanged), mean KL 0.0077
(was 0.0082 with f32 cache), next-token NLL 1.2852 vs 1.2798. The conversions cost a little attention time
(2,000-token context, one noisy run: decode attention 9.9 ms against 7.7 ms with f32), which is the price
of halving the cache. The worker's default context rises to 32,768 tokens (1.6 GB).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 22:35:50 +02:00
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
Pavlo RatushnyiandClaude Sonnet 5.5 68fcd8de1c perf: use the pre-converted activation layout and 4-accumulator kernels for the MXFP4 experts (STRA-214)
The expert products (62 percent of a decoded token) now read the activation as f32
scales plus contiguous int8 quants, summed with four accumulators per iteration.
Adds the portable reference and the AVX-VNNI kernel for that layout; the AVX2 and
AVX-VNNI kernels are tested bit-identical to each other, and every level is within
the stated tolerance of the ggml-derived f64 reference. The decoder keeps both the
Q8_0 blocks (for the Q8_0 attention and output head) and the converted layout.

Measured on the real model with pre-touched pages, 10 threads, 40 tokens, alternating
old and new binaries (5 repeats; indicative, one burst-state outlier excluded): paired
old/new ratios 1.09, 1.14, 1.08, 1.06, i.e. about 8 percent less time per token (118 to
111 ms median), expert gate+up 41.6 to 37.6 ms. Greedy tokens are identical to
llama.cpp for the first 16 and to the previous build for the first 18; they diverge at
position 18 where the top-two logit gap is 0.009 (a numerical tie).

Also adds a per-step top-two logit margin print to the real-model runner.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 17:03:33 +02:00
Pavlo RatushnyiandClaude Sonnet 5.5 fa921a2ced feat: add Q8_0 activation quantizer and Q8_0 x Q8_0 dot kernels (STRA-211)
Completes the Q8_0 side of the CPU runtime's kernel set: a float-to-Q8_0
quantizer (scale = amax / 127 as a half, quants rounded to nearest even and
clamped to [-127, 127]) and the Q8_0 x Q8_0 dot, each at scalar, AVX2 and AVX-VNNI
(the quantizer shares the AVX2 code). Tests: the quantizer is bit-identical across
levels and equal to the vendored ggml reference except on exact .5 ties; per-block
integer sums are exact; float results are within the stated tolerance of an f64
reference built from ggml's dequantizer; the portable half conversion equals the
F16C instruction on over a million values. Windows MSVC and Linux GCC, debug and
release. Replaces _cvtss_sh (not available in MSVC) with the packed conversion.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 16:01:32 +02:00
Pavlo RatushnyiandClaude Sonnet 5.5 164bfa60b8 feat: add MXFP4 x Q8_0 dot kernels at scalar, AVX2 and AVX-VNNI (STRA-211)
The gpt-oss-20b experts are MXFP4, so the CPU runtime needs this dot product.
Blocks use ggml's byte layout, so GGUF tensors can be read in place. Per-block
integer sums are exact at every level; the float combination is checked against
an f64 reference built from the vendored ggml dequantizers (test-only
ggml-oracle feature) within a tolerance written before the run. Scale
conversion is compared exactly with ggml for every E8M0 code and every finite
half. AVX2 level now means AVX2 + FMA + F16C. ABI version 2.

Adds a kernel-level throughput example (bench_mxfp4); it is not an inference
benchmark and no end-to-end speed claim is made. Records the ggml upstream use
in docs/upstream-ggml.md.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 15:17:52 +02:00
Pavlo RatushnyiandClaude Sonnet 5.5 37620f1b60 feat: add CPU runtime kernel layer with AVX2 and AVX-VNNI dot products (STRA-202)
leleka-cpu-sys holds the C kernels (one translation unit per ISA level, built
by build.rs with cc) behind a checked FFI; it is the only unsafe code of the CPU
runtime and opts out of the workspace unsafe_code lint with a recorded exception.
leleka-cpu-kernels does safe run-time dispatch with a LELEKA_CPU_ISA override
that fails rather than silently changing level.

Ports the int8xint8 and int4xint8 dot products from Colibri c/idot.h at scalar,
AVX2 and AVX-VNNI. Tests compare every level with the C scalar kernel and an
independent i64 oracle (13 tests; Windows MSVC and Linux GCC 14.2). Activations
must be in [-127, 127]: -128 is outside the contract and a test pins that. No
performance claim: nothing was timed.

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