7.0 KiB
Q4_K / Q5_K CPU reference parity
This port implements GGUF kind 12 (Q4_K) and kind 13 (Q5_K) preparation decoding in the independent standard-library-only Rust quant crate. It does not replace the complete engine or prove model/GPU readiness. Other preparation formats still use the legacy C decoder. No model weights, request data or user experiments are read by these gates.
Pinned layout and arithmetic
The oracle is actual output from the unmodified vendored GGML C decoder at
llama.cpp 3cf03257f219afbe7334045ff7c6a06ac68c627d, also pinned by imported
Strata d9ab8435f654c368c586340d490915f6addf56a3. The source hashes and original
MIT license remain in GGML provenance
and the quant crate notice.
The production C bridge, prior fourteen oracle tests and compiler arithmetic
flags remain unchanged.
Both formats output 256 values and have two independent little-endian FP16
header values: d at bytes 0–1 and dmin at 2–3. Twelve packed scale/minimum
bytes occupy 4–15. Q4_K is 144 bytes with four 32-byte nibble groups at 16–143.
Q5_K is 176 bytes with a shared 32-byte high-bit bank at 16–47 and nibble groups
at 48–175. Each pair produces two consecutive 32-element halves. The Q5 bank
reuses each lane byte with masks 1/2, 4/8, 16/32 and 64/128 across the four pairs.
Six-bit scale/minimum unpacking preserves the different lower/upper field
locations for subblocks 0–3 and 4–7.
The arithmetic retains separate operations in C order: d * subscale,
dmin * subminimum, then scaled_d * quant - scaled_minimum. The Rust code does
not use mul_add; this gate does not change C contraction or fast-math flags.
All FP16 header encodings are admitted, preserving the old C value policy.
Infinity multiplied by zero and infinity subtraction can yield NaN. Shape
errors leave caller output untouched; downstream consumers keep their own
value policies. Signed-zero output bits are part of finite parity; NaN payload
or sign equivalence is not claimed.
Independent coverage
Expected numerical output always comes from the pinned C decoder, never from the new Rust implementation or its packed-scale helper. Generated bytes and coverage bookkeeping are independent of those production functions.
- For each format, every one of the 63,488 finite FP16 encodings is exercised
separately in the
drole withdmin=0.5and thedminrole withd=-1. These sweeps contribute 65,011,712 exact element comparisons across the two formats and roles. They do not exhaust both FP16 values jointly. - Each format separately compares 65,536 independently generated packed patterns with fixed finite scales. Assertions require every byte value at every packed scale/minimum field and quant field; every six-bit subscale and minimum at every one of eight positions; every quant code at every output position; and, for Q5_K, every byte value at each high-bank lane. Each block additionally covers all 16 Q4 codes or 32 Q5 codes in each output half. These cases contribute 33,554,432 comparisons.
- A 20×20 joint finite boundary matrix includes positive/negative zero, signed minimum/largest subnormals, normal boundaries, adjacent-to-one values and maximum finite values. Twenty further equal-scale fixtures per format explicitly require exact cancellation to positive zero. These contribute 215,040 comparisons.
- Sixteen-block boundary batches per format run with both 16-byte aligned and deliberately unaligned inputs through both bulk APIs, adding 16,384 comparisons. Invalid input/output lengths must reject before changing any sentinel output, for both the Rust and C APIs.
- Every 2,048 nonfinite FP16 encoding is exercised in each header role for each format with independently packed fields and explicit all-zero packed fields. A 10×10 joint matrix includes finite, zero, infinity and NaN headers. NaN classification must agree and every non-NaN output must match exact bits, including infinity signs and any finite zero.
All finite gates use exact f32::to_bits() equality and zero tolerance.
The finite corpus contributes 98,797,568 comparisons. It does not exhaust
all 63,488² finite header pairs, all combinations of packed fields, or every
scale/code combination at every header pair.
Bounded visitor
Only kinds 12 and 13 gain new Rust routes. Their decoder/metadata geometry must agree before seek; other format branches and the source cap remain unchanged. A synthetic tensor per format crosses two callbacks, verifying the actual C bytes, exact source offsets, total F32 byte receipt and bounded source/derived slices. Empty/misaligned shapes reject before seek or callback; first/late truncation never delivers a partial chunk; callback failure at either callback stops later reads. Nonfinite joint headers and zero packed fields retain C classification and all non-NaN output bits. Previous nine visitor tests remain.
cargo test --locked -p leleka-quant-core
cargo clippy --locked -p leleka-quant-core --all-targets -- -D warnings
cargo test --locked -p strata-control --lib ggml_bridge::tests -- --test-threads=2
cargo test --locked -p strata-control --lib gguf_f32_stream::tests
Actual local CPU result, 2026-10-07
- Rust:
rustc 1.99.0 (b940084d7 2026-09-28). - Host:
aarch64-apple-darwin(Darwin arm64). - C compiler: Apple Clang 21.0.0 (
clang-2100.1.1.101), with unchanged C build flags and no arithmetic tolerance or contraction-policy changes. - Standalone quant crate: 20 passed, 0 failed (eight new helper/Q4_K/Q5_K tests). All-target Clippy with warnings denied and owned-file formatting passed. The owned temporary target was cleaned and removed.
- Pinned C oracle: 19 passed, 0 failed, including five new tests and all fourteen prior tests byte preserved. All 98,797,568 new finite exact-bit comparisons, every packed/subscale/minimum/code position coverage assertion, both nonfinite header roles, explicit zero fields, joint edge matrices, cancellation, aligned/unaligned bulk and shape-sentinel gates passed.
- Visitor: 11 passed, 0 failed, including two new tests and all nine prior visitor test functions byte preserved. Q4_K used source chunks 28,080/2,448 bytes and F32 chunks 199,680/17,408 bytes; Q5_K used 28,160/2,992 and 163,840/17,408 bytes. Offsets, receipts, all shape/truncation/callback gates and nonfinite header/zero-field behavior passed.
- Control-plane library/test Clippy with warnings denied, formatting, diff and all ten vendored GGML manifest hash checks passed. The production bridge and C source remain unchanged; normal quant-crate dependency tree is crate-only. The existing coordinated shared target cache was retained.
No finite mismatch was observed and no quality gate or tolerance was weakened. NaN payload/sign equivalence is outside this classification contract. These results apply to this CPU/compiler configuration; Linux/GCC qualification, actual model/GPU inference, model quality and performance remain separate.