Linux / keyless-cuda-gate (pull_request) Skipped
Linux / verify (pull_request) Successful in 4m50s
Linux / keyless-cuda (pull_request) Skipped
Linux / static-cuda-package (pull_request) Skipped
Linux / hosted-cuda-package (pull_request) Skipped
Linux / linux-cli-container (pull_request) Successful in 4m10s
Linux / linux-static-cli (pull_request) Successful in 8m9s
Linux / release-cli (pull_request) Skipped
Desktop foundation / core (ubuntu-latest) (push) Failing after 8s
Desktop foundation / core (macos-latest) (push) Canceled after 0s
Desktop foundation / core (windows-latest) (push) Canceled after 0s
183 lines
11 KiB
Markdown
183 lines
11 KiB
Markdown
# Leleka quantization foundation
|
||
|
||
`leleka-quant-core` provides IQ2_S, IQ3_S, IQ3_XXS, IQ4_NL, IQ4_XS, Q2_0, Q4_K,
|
||
Q5_K, Q6_K and Q8_0
|
||
CPU decoding and binary16-to-binary32
|
||
conversion in an independent Rust crate. Production code uses only the standard
|
||
library: no build script, C/FFI, GGML, Strata source/process, Python or CUDA dependency.
|
||
`sha2` and `hex` are test-only dependencies for the existing golden checks.
|
||
The control plane reexports `iq3_s` and `kar_codec::f16_bits_to_f32`, preserving
|
||
all existing preparation and KAR consumer paths. Its separate legacy GGML/build
|
||
dependencies remain; this extraction does not make the control plane autonomous.
|
||
|
||
## Contract and provenance
|
||
|
||
IQ3_S decoding accepts 110-byte blocks with 256 output elements. It retains the
|
||
existing layout, finite-scale rejection, checked output geometry, arithmetic,
|
||
pinned grid and golden hashes. `decode_into` processes blocks in order; if a later
|
||
block is invalid, earlier output blocks may already have been written. Callers
|
||
must handle the error and must not consume a partial result as successful output.
|
||
Binary16 conversion preserves the existing signed-zero, subnormal, infinity and
|
||
NaN payload bit behavior. Encoding and its rounding helpers remain in KAR.
|
||
|
||
The grid was imported from `ggml-common.h` at upstream Strata commit
|
||
`d9ab8435f654c368c586340d490915f6addf56a3`. Its ggml MIT notice is copied unchanged
|
||
in [LICENSE-GGML](LICENSE-GGML); the table's source provenance remains in its
|
||
header. The Rust extraction retains the original decoder and helper arithmetic.
|
||
No engine file is included or read to build or use this crate.
|
||
|
||
IQ4_NL decoding accepts 18-byte blocks with 32 output elements, using the pinned
|
||
16-value nonlinear codebook. Packed low nibbles produce elements 0–15 and high
|
||
nibbles produce elements 16–31. The arithmetic and table follow
|
||
`dequantize_row_iq4_nl` / `kvalues_iq4nl` at the same llama.cpp GGML commit
|
||
`3cf03257f219afbe7334045ff7c6a06ac68c627d`; the existing MIT notice in
|
||
[LICENSE-GGML](LICENSE-GGML) covers that table. `decode_into` validates complete
|
||
input blocks and exact output length before writing, accepts a zero-block
|
||
operation, and uses no heap buffer or input alignment requirement.
|
||
|
||
IQ4_NL deliberately propagates infinity and NaN scales to preserve its former C
|
||
preparation behavior. Existing consumers retain their own nonfinite-value policy;
|
||
the IQ3_S finite-scale rejection is unchanged. Finite-scale output is qualified
|
||
against the pinned C oracle with zero bit tolerance. For NaN scales, classification
|
||
and successful admission are checked, without claiming portable NaN payload/sign
|
||
bits; infinity output signs and bits are checked. See
|
||
[IQ4_NL reference parity](../../docs/iq4-reference-parity.md) for test scope and
|
||
actual CPU results. This port removes IQ4_NL's computational C dependency in the
|
||
preparation visitor; other legacy control-plane C paths and builds remain.
|
||
|
||
IQ4_XS accepts 136-byte blocks with 256 output elements. It shares IQ4_NL's
|
||
codebook through a crate-private constant; the public IQ4_NL API and table bytes
|
||
are unchanged. Each block stores a little-endian FP16 scale, little-endian 16-bit
|
||
packed scale highs, four packed scale-low bytes and 128 packed codepoint bytes.
|
||
Each of eight 32-element subblocks reconstructs a six-bit scale, subtracts 32,
|
||
then performs `base * multiplier` followed by `subblock_scale * codepoint`.
|
||
Low and high codepoint nibbles produce separate 16-element halves. This follows
|
||
the pinned `dequantize_row_iq4_xs` function and preserves its intermediate,
|
||
including signed zero and infinity times zero producing NaN.
|
||
|
||
Q8_0 accepts 34-byte blocks with 32 output elements: a little-endian FP16 scale
|
||
and 32 signed bytes, decoded by scale multiplication as in the pinned
|
||
`dequantize_row_q8_0` function. IQ4_XS and Q8_0 admit every FP16 scale encoding;
|
||
existing consumers retain their nonfinite-value policy. Their bulk APIs reject
|
||
malformed shapes before writes, accept empty input/output, require no source
|
||
alignment, and use no heap decoding buffer. The separate pinned C-oracle gates
|
||
record exact finite bits and explicit nonfinite classification/infinity signs
|
||
without implying portable NaN payload/sign bits, GPU behavior or complete model
|
||
support. See [combined reference parity](../../docs/q8-iq4xs-reference-parity.md)
|
||
for actual corpus, compiler and results.
|
||
|
||
Q4_K accepts 144-byte blocks; Q5_K accepts 176-byte blocks. Both produce 256
|
||
values from a little-endian FP16 scale and minimum, a shared 12-byte packed
|
||
six-bit scale/minimum header, and eight 32-element subblocks. The crate-private
|
||
header helper follows pinned `get_scale_min_k4`: the upper two bits for subblocks
|
||
4–7 share the first eight header bytes, and the remaining four bytes store their
|
||
low nibbles. Q4_K's quant bytes start at offset 16 and split each byte into two
|
||
32-element halves. Q5_K's 32 high-bit bytes start at offset 16 and low-nibble
|
||
bytes at offset 48; every high-bit lane is reused across four pairs, with masks
|
||
1/2, 4/8, 16/32 and 64/128 selecting the corresponding half.
|
||
|
||
The arithmetic follows pinned `dequantize_row_q4_K` / `dequantize_row_q5_K`:
|
||
first form `d * scale` and `dmin * minimum`, then compute
|
||
`subblock_scale * quant - subblock_minimum`. These steps are kept separate, with
|
||
no `mul_add` or reassociation. Both formats admit all FP16 encodings, preserving
|
||
signed zero, infinity times zero and infinity-minus-infinity NaN behavior;
|
||
consumer value policies remain unchanged. Like the other bulk APIs, invalid
|
||
shapes fail before output writes, empty input/output is valid, and decoding uses
|
||
no heap allocation or source alignment requirement. Synthetic standalone tests
|
||
check independently specified wire bytes/expected values, both scale-header
|
||
branches, all Q5 high masks, bulk/shape handling and nonfinite/signed-zero cases.
|
||
See [Q4_K/Q5_K reference parity](../../docs/q4-q5-k-reference-parity.md) for the
|
||
separate pinned C-oracle scope and actual results. These CPU preparation ports
|
||
do not remove all control-plane C builds or qualify serving/model/GPU behavior.
|
||
|
||
Q2_0 accepts 18-byte blocks producing 64 values. Its little-endian FP16 scale
|
||
occupies bytes 0–1; each following byte contains four consecutive two-bit
|
||
elements, read from least to most significant bits. Codes 0, 1, 2 and 3 map to
|
||
quant values −1, 0, 1 and 2, then multiply `quant * scale`, following pinned
|
||
`dequantize_row_q2_0`. This format is GGUF kind 42, distinct from Q2_K.
|
||
|
||
Q6_K accepts 210-byte blocks producing 256 values. Its low quant bytes occupy
|
||
0–127, high quant bytes 128–191, signed int8 subscales 192–207 and little-endian
|
||
FP16 base scale 208–209. Two 128-value halves each contain four 32-value
|
||
quadrants. High-bit shifts 0/2/4/6 combine with the low/high nibbles, giving a
|
||
six-bit code minus 32; the subscale index uses a 16-element lane group within
|
||
each quadrant. Arithmetic follows pinned `dequantize_row_q6_K`, preserving
|
||
`(base * signed_subscale) * signed_quant` rather than reassociating products.
|
||
The −128 subscale remains signed. No codebook or K scale/minimum helper is used.
|
||
|
||
Both formats propagate all FP16 encodings, including NaN and infinity. Infinity
|
||
times zero quant, and Q6_K infinity times zero subscale, produce NaN; existing
|
||
consumer value policies stay unchanged. Shape errors fail before output writes,
|
||
empty bulk input/output is valid, and decoding requires neither a heap buffer
|
||
nor source alignment. Standalone synthetic tests check independent expected
|
||
wire values, every Q6_K packed lane/quadrant mapping, bulk/bounds behavior,
|
||
signed zero and nonfinite zero-factor cases. Separate pinned C oracles qualify
|
||
finite output bits and explicit nonfinite classification/sign behavior; they
|
||
do not imply portable NaN payloads, model/GPU support or performance results.
|
||
See [Q6_K/Q2_0 reference parity](../../docs/q6-k-q2-0-reference-parity.md) for
|
||
the recorded corpus, compiler and actual test results.
|
||
|
||
IQ2_S accepts 82-byte blocks producing 256 values. After the little-endian FP16
|
||
scale, bytes 2–33 hold eight-bit grid index lows, bytes 34–65 hold raw eight-bit
|
||
sign masks, bytes 66–73 hold two-bit index highs and bytes 74–81 hold scale
|
||
nibbles. Each 32-value subblock contains four eight-value grids; the low scale
|
||
nibble applies to the first two, the high nibble to the last two. Its 1024-entry
|
||
grid uses explicit little-endian u64 bytes. Arithmetic preserves
|
||
`((base * (0.5 + nibble)) * 0.25) * magnitude * sign` in the pinned C order.
|
||
IQ2_S uses all 256 raw sign masks, with no seven-bit sign-code lookup.
|
||
|
||
IQ3_XXS accepts 98-byte blocks producing 256 values. Grid indices occupy bytes
|
||
2–65; bytes 66–97 contain eight little-endian u32 words, each storing four
|
||
seven-bit sign codes plus a top scale nibble. Each eight-value group uses two
|
||
four-value grids from the 256-entry u32 table, read as explicit little-endian
|
||
bytes, and the pinned 128-entry sign lookup. Arithmetic preserves
|
||
`((base * (0.5 + nibble)) * 0.5) * magnitude * sign`. Both ports use the same
|
||
F16 helper and admit every FP16 encoding; signed zero, infinity signs and NaN
|
||
classification retain the prior consumer policy. Grid magnitudes and valid
|
||
scale factors are nonzero; there is no packed zero factor for infinity in these
|
||
two formats. Bulk shapes fail before writes, empty input/output is valid, and
|
||
decoding has no heap allocation or input alignment requirement.
|
||
|
||
The exact lookup entries were independently imported from the pinned
|
||
`ggml-common.h` (SHA-256
|
||
`0061131b615c5721fc88a78feeb22c1f8c450f1c2646a317d80796a653bf595c`),
|
||
after checking its tracked source manifest. Import provenance and the ggml MIT
|
||
notice are retained in the table files and [LICENSE-GGML](LICENSE-GGML).
|
||
Canonical byte SHA-256 values checked by standalone tests are:
|
||
|
||
| Table | Canonical bytes | SHA-256 |
|
||
| --- | --- | --- |
|
||
| IQ2_S grid | 1024 little-endian u64 entries | `e1aa1473412b0552c2174c30ef22ab4073f6a181b85a17056e8249bd2932fd88` |
|
||
| IQ3_XXS grid | 256 little-endian u32 entries | `46e35f5a997efdee6c99ce57854c8a0d4f0ff8ca57e5e8a60c0793ea580acf5d` |
|
||
| IQ3_XXS sign lookup | 128 u8 entries | `a76742a603f8beca5212ecce0f1f02f11a4887fd2f7c8b15aca0ea3eb3380c31` |
|
||
|
||
Standalone tests additionally check independent expected wire values, index
|
||
highs, nibble halves, word/grid byte order, raw/codebook sign domains, signed
|
||
zero/nonfinite admission and bulk/shape boundaries. See
|
||
[IQ2_S/IQ3_XXS reference parity](../../docs/iq2-s-iq3-xxs-reference-parity.md)
|
||
for the separate pinned C-oracle corpus and actual results. No imported engine
|
||
source is changed. CPU tensor-format coverage does not qualify complete model
|
||
loading, readiness, GPU execution, quality or performance, and the existing
|
||
control-plane C build dependency remains until separately removed.
|
||
|
||
The existing tests preserve the SHA-256 of the little-endian grid bytes
|
||
(`bd1af4945a1717c65610b0284e4628b9a1ba3b306fae3a06f6e5f597356e349f`)
|
||
and the mixed-block decoded f32 bytes
|
||
(`cfee2753d1b389fe4ee30994ff3387d4400745b8432aeb32cb4d7fced39949ec`).
|
||
Independent IEEE 754 boundary encodings additionally check the shared conversion.
|
||
|
||
## Standalone checks and limits
|
||
|
||
```sh
|
||
cargo fmt --manifest-path crates/quant-core/Cargo.toml --check
|
||
cargo clippy --locked -p leleka-quant-core --all-targets -- -D warnings
|
||
cargo test --locked -p leleka-quant-core
|
||
cargo tree --locked -p leleka-quant-core --edges normal
|
||
```
|
||
|
||
These are bounded CPU preparation primitives and synthetic numerical checks.
|
||
They do not implement compute shaders, GPU dispatch, complete model loading,
|
||
inference, model quality or performance qualification. Independent C-oracle
|
||
checks in the control plane are separate offline tests; they do not add a native
|
||
dependency to this crate.
|