Files
leleka/docs/q8-iq4xs-reference-parity.md
T
Pavlo Ratushnyi 4ed6914d84
Linux / keyless-cuda-gate (pull_request) Skipped
Linux / verify (pull_request) Failing after 3m22s
Linux / keyless-cuda (pull_request) Skipped
Linux / static-cuda-package (pull_request) Skipped
Linux / hosted-cuda-package (pull_request) Skipped
Linux / linux-static-cli (pull_request) Skipped
Linux / linux-cli-container (pull_request) Skipped
Linux / release-cli (pull_request) Skipped
STRA-102 port Q8_0 and IQ4_XS preparation to Rust
2026-10-07 21:27:55 +02:00

6.2 KiB
Raw Blame History

Q8_0 and IQ4_XS CPU reference parity

This source port replaces GGUF kind 8 (Q8_0) and kind 23 (IQ4_XS) preparation arithmetic with independent Rust modules in leleka-quant-core. The crate has no production dependency beyond the standard library and no C/FFI, Strata, Python or CUDA build/runtime dependency. Remaining preparation formats still use the control plane's existing GGML C decoder; complete inference remains separate.

Pinned layouts and arithmetic

The numerical oracle is the unchanged vendored ggml-quants.c from llama.cpp 3cf03257f219afbe7334045ff7c6a06ac68c627d, also pinned by imported Strata d9ab8435f654c368c586340d490915f6addf56a3. Source hashes and MIT attribution remain in GGML provenance and the crate's retained notice. No native source or compiler arithmetic flag is changed.

  • Q8_0: 34 bytes / 32 values. The little-endian FP16 scale occupies bytes 0–1; remaining bytes are signed int8 values, including -128. Each output preserves the pinned C operand order: signed_quant * scale.
  • IQ4_XS: 136 bytes / 256 values. FP16 scale occupies bytes 0–1, little-endian packed high scale bits bytes 2–3, low scale nibbles bytes 4–7 and value nibbles bytes 8–135. Eight subblocks each produce 32 outputs. The six-bit subscale is biased by -32; the intermediate is scale * signed_subscale, followed by multiplication with the shared nonlinear IQ4 codebook. Low/high value nibbles retain the two 16-element output halves. The codebook shared with IQ4_NL is unchanged; moving it does not change its values or pin.

Both APIs accept every FP16 scale encoding, matching their previous C paths. Infinity times a zero Q8 quant or zero IQ4_XS subscale produces NaN. Consumers retain responsibility for value admission. IQ3_S's existing nonfinite rejection is unchanged. Shape errors reject before output writes, empty input/output is a valid zero-block operation, and arbitrary input alignment requires no heap allocation in the Rust decoders.

Independent oracle scope

All fixtures are generated synthetic bytes; no model weights or user data are read. Finite comparisons use actual pinned C decoder output and exact f32::to_bits() equality, including signed zero, with zero numerical tolerance.

  • Q8_0 exhausts all 63,488 finite FP16 encodings and all 256 signed byte codes at every scale: 16,252,928 element comparisons. Coverage assertions additionally require every byte code at every one of the 32 output positions.
  • IQ4_XS exhausts all 63,488 finite FP16 encodings, all 64 signed subscales at each scale and all 16 nonlinear codes at each subscale. Eight blocks per scale produce 130,023,424 element comparisons, including both output halves. Coverage assertions require every subscale at every one of eight subblock positions, all 256 packed byte values at each of 128 fields, and all 16 codepoints at each of 256 output positions.
  • Every one of 2,048 nonfinite FP16 encodings is admitted by both Rust bulk APIs and C. NaN classification is compared; non-NaN infinities must match exact bits/signs. Q8 infinity checks contain all signed codes including zero; IQ4_XS checks contain every subscale including zero. No portable NaN payload or sign equivalence is inferred.
  • A separate 14-block finite boundary batch for each format compares both bulk APIs using input intentionally one byte beyond a 16-byte boundary, exercising the C bridge's alignment-copy behavior and Rust's alignment-free decoder.
  • Standalone tests check independent expected values, signed zeros, lane layout, multiple blocks, nonfinite admission and malformed shapes preserving output. Root's separate bounded visitor tests cover integration, offsets, callback aborts, truncation and F32 byte receipts.

These corpora cover the stated scalar scale/codepoint domains and field positions. They do not exhaust the joint space of all packed bytes or all combinations of different subblock scales in one block, qualify an entire model, prove Linux/GCC parity, measure performance or exercise a GPU.

Commands

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).
  • Standalone quant crate: 12 passed, 0 failed (three new Q8_0 and four new IQ4_XS tests); all-target Clippy with warnings denied passed. The initial standalone tests passed, then Clippy found a test-only range predicate; an equivalent range expression was applied and the final full scoped suite and Clippy both passed. No decoder arithmetic or numerical gate was changed.
  • Pinned C bridge oracle: 14 passed, 0 failed, including five new tests and all nine earlier tests byte preserved. The finite corpora's 146,276,352 element comparisons, per-position coverage assertions, both unaligned batches and all nonfinite classification/infinity-sign/zero cases passed.
  • Bounded GGUF visitor: 9 passed, 0 failed; both new formats' geometry guards, two-chunk exact C bytes, source offsets, output byte receipts, malformed/truncated input, callback failure stopping subsequent reads and nonfinite behavior passed.
  • Control-plane library/test Clippy with warnings denied, formatting and diff checks passed. All ten vendored GGML manifest hashes passed; production bridge code, prior test functions and C compiler arithmetic flags are unchanged.
  • The normal quant-crate dependency tree contains only leleka-quant-core. Both owned temporary standalone build targets were removed; the separately coordinated existing shared target cache was retained.

No finite numeric mismatch occurred and no tolerance was relaxed. The NaN gate compares classification rather than payload/sign equivalence. These results apply to this CPU/compiler configuration. Linux/GCC, real-model quality, complete inference and GPU qualification remain separate gates.