diff --git a/control-plane/src/ggml_bridge.rs b/control-plane/src/ggml_bridge.rs index d18c2fa1..86a91766 100644 --- a/control-plane/src/ggml_bridge.rs +++ b/control-plane/src/ggml_bridge.rs @@ -220,6 +220,123 @@ mod tests { assert_eq!(rejected, 2_048); } + // Each half covers all 16 IQ4_NL codepoints for every scale. Over patterns + // 0..256 each field additionally covers every low/high nibble combination. + fn synthetic_iq4_block(scale_bits: u16, pattern: u32) -> [u8; 18] { + let mut block = [0u8; 18]; + block[..2].copy_from_slice(&scale_bits.to_le_bytes()); + for (field, byte) in block[2..].iter_mut().enumerate() { + let low = (field as u8).wrapping_add(pattern as u8) & 15; + let high = (15 - field as u8).wrapping_add((pattern >> 4) as u8) & 15; + *byte = low | (high << 4); + } + block + } + + #[test] + fn pinned_ggml_iq4_matches_rust_bits_for_every_finite_fp16_scale() { + use leleka_quant_core::iq4_nl; + let mut packed_fields = [[false; 256]; 16]; + let mut codepoints = [[false; 16]; 32]; + let mut compared = 0; + for scale_bits in 0..=u16::MAX { + if scale_bits & 0x7c00 == 0x7c00 { + continue; + } + let block = synthetic_iq4_block(scale_bits, u32::from(scale_bits)); + let mut reference = [0.0; iq4_nl::BLOCK_ELEMENTS]; + decode_into(20, &block, &mut reference).unwrap(); + let actual = iq4_nl::decode_block(&block).unwrap(); + let mut low_seen = [false; 16]; + let mut high_seen = [false; 16]; + for (field, byte) in block[2..].iter().enumerate() { + packed_fields[field][usize::from(*byte)] = true; + codepoints[field][usize::from(byte & 15)] = true; + codepoints[field + 16][usize::from(byte >> 4)] = true; + low_seen[usize::from(byte & 15)] = true; + high_seen[usize::from(byte >> 4)] = true; + } + assert!(low_seen.into_iter().all(|seen| seen)); + assert!(high_seen.into_iter().all(|seen| seen)); + for (element, (actual, reference)) in actual.iter().zip(&reference).enumerate() { + assert!(actual.is_finite() && reference.is_finite()); + assert_eq!( + actual.to_bits(), + reference.to_bits(), + "IQ4_NL scale={scale_bits:#06x} element={element} block={block:?}" + ); + } + compared += 1; + } + assert_eq!(compared, 63_488); + assert!(packed_fields.iter().flatten().all(|seen| *seen)); + assert!(codepoints.iter().flatten().all(|seen| *seen)); + } + + #[test] + fn pinned_ggml_iq4_batch_bits_match_rust_with_unaligned_source() { + use leleka_quant_core::iq4_nl; + let scales = [ + 0x0000, 0x8000, 0x0001, 0x8001, 0x03ff, 0x83ff, 0x0400, 0x8400, 0x3555, 0xb555, 0x3c00, + 0xbc00, 0x7bff, 0xfbff, + ]; + let length = scales.len() * iq4_nl::BLOCK_BYTES; + let mut storage = vec![0u8; length + 16]; + let start = storage.as_ptr().align_offset(16) + 1; + let source = &mut storage[start..start + length]; + assert_eq!(source.as_ptr().align_offset(16), 15); + for (number, scale) in scales.into_iter().enumerate() { + source[number * iq4_nl::BLOCK_BYTES..(number + 1) * iq4_nl::BLOCK_BYTES] + .copy_from_slice(&synthetic_iq4_block(scale, number as u32 + 70_000)); + } + let mut actual = vec![0.0; scales.len() * iq4_nl::BLOCK_ELEMENTS]; + let mut reference = vec![0.0; actual.len()]; + decode_into(20, source, &mut reference).unwrap(); + iq4_nl::decode_into(source, &mut actual).unwrap(); + for (element, (actual, reference)) in actual.iter().zip(&reference).enumerate() { + assert_eq!( + actual.to_bits(), + reference.to_bits(), + "IQ4_NL batch element={element}" + ); + } + } + + #[test] + fn pinned_ggml_iq4_nonfinite_classification_matches_rust_without_refusal() { + use leleka_quant_core::iq4_nl; + let mut compared = 0; + for scale_bits in 0..=u16::MAX { + if scale_bits & 0x7c00 != 0x7c00 { + continue; + } + let block = synthetic_iq4_block(scale_bits, u32::from(scale_bits)); + let mut reference = [0.0; iq4_nl::BLOCK_ELEMENTS]; + let mut actual = [0.0; iq4_nl::BLOCK_ELEMENTS]; + decode_into(20, &block, &mut reference).unwrap(); + iq4_nl::decode_into(&block, &mut actual).unwrap(); + let single = iq4_nl::decode_block(&block).unwrap(); + for (element, ((actual, single), reference)) in + actual.iter().zip(&single).zip(&reference).enumerate() + { + if scale_bits & 0x03ff == 0 { + assert!(actual.is_infinite() && reference.is_infinite()); + assert_eq!(actual.to_bits(), reference.to_bits()); + assert_eq!(single.to_bits(), reference.to_bits()); + } else { + // No cross-platform NaN payload/sign contract is inferred + // from classification. Neither decoder refuses these scales. + assert!( + actual.is_nan() && single.is_nan() && reference.is_nan(), + "IQ4_NL nonfinite scale={scale_bits:#06x} element={element}" + ); + } + } + compared += 1; + } + assert_eq!(compared, 2_048); + } + #[test] fn q8_0_and_q2_0_match_known_block_values() { let mut q8 = [0u8; 34]; diff --git a/control-plane/src/gguf_f32_stream.rs b/control-plane/src/gguf_f32_stream.rs index ad2a35f4..07cf7312 100644 --- a/control-plane/src/gguf_f32_stream.rs +++ b/control-plane/src/gguf_f32_stream.rs @@ -3,6 +3,8 @@ use std::io::{Read, Seek, SeekFrom}; +use leleka_quant_core::iq4_nl; + use crate::{ggml_bridge, gguf, iq3_s, kar_codec}; pub const SOURCE_CHUNK_BYTES: usize = 256 * iq3_s::BLOCK_BYTES; @@ -23,6 +25,12 @@ where { let (block_elements, block_bytes) = gguf::block_geometry(source_kind) .ok_or_else(|| format!("unsupported GGUF source kind {source_kind}"))?; + if source_kind == 20 + && (block_elements != iq4_nl::BLOCK_ELEMENTS as u64 + || block_bytes != iq4_nl::BLOCK_BYTES as u64) + { + return Err("IQ4_NL decoder geometry differs from GGUF source kind 20".into()); + } let block_bytes = usize::try_from(block_bytes).map_err(|_| "GGUF block is too large")?; if block_bytes > SOURCE_CHUNK_BYTES { return Err("GGUF block exceeds bounded source chunk".into()); @@ -60,6 +68,14 @@ where } callback(source, &derived)?; } + 20 => { + for block in source.as_chunks::<{ iq4_nl::BLOCK_BYTES }>().0 { + for value in iq4_nl::decode_block(block)? { + derived.extend_from_slice(&value.to_le_bytes()); + } + } + callback(source, &derived)?; + } 21 => { for block in source.as_chunks::<{ iq3_s::BLOCK_BYTES }>().0 { for value in iq3_s::decode_block(block)? { @@ -134,6 +150,178 @@ mod tests { ); } + #[test] + fn iq4_stream_spans_chunks_with_exact_c_parity_and_source_offsets() { + let blocks_per_chunk = SOURCE_CHUNK_BYTES / iq4_nl::BLOCK_BYTES; + let blocks = blocks_per_chunk + 257; + let prefix = 13; + let mut source = vec![0xa5; prefix]; + for block_index in 0..blocks { + let mut block = [0u8; iq4_nl::BLOCK_BYTES]; + let scale: u16 = [0x0001, 0x03ff, 0x0400, 0x3800, 0xbc00, 0x7bff][block_index % 6]; + block[..2].copy_from_slice(&scale.to_le_bytes()); + for (position, byte) in block[2..].iter_mut().enumerate() { + *byte = (block_index + position * 17) as u8; + } + source.extend_from_slice(&block); + } + let source_bytes = blocks * iq4_nl::BLOCK_BYTES; + source.extend_from_slice(&[0x5a; 31]); + let mut cursor = Cursor::new(&source); + let mut consumed = 0; + let mut chunks = Vec::new(); + let output_bytes = visit( + &mut cursor, + prefix as u64, + 20, + source_bytes as u64, + |raw, derived| { + assert!(raw.len() <= SOURCE_CHUNK_BYTES); + assert!(raw.len().is_multiple_of(iq4_nl::BLOCK_BYTES)); + assert!(derived.len() <= blocks_per_chunk * iq4_nl::BLOCK_ELEMENTS * 4); + assert_eq!( + raw, + &source[prefix + consumed..prefix + consumed + raw.len()] + ); + let mut reference = + vec![0.0; raw.len() / iq4_nl::BLOCK_BYTES * iq4_nl::BLOCK_ELEMENTS]; + ggml_bridge::decode_into(20, raw, &mut reference).unwrap(); + let reference_bytes: Vec = reference + .iter() + .flat_map(|value| value.to_le_bytes()) + .collect(); + assert_eq!(derived, reference_bytes); + consumed += raw.len(); + chunks.push((raw.len(), derived.len())); + Ok(()) + }, + ) + .unwrap(); + assert_eq!( + chunks, + [ + ( + blocks_per_chunk * iq4_nl::BLOCK_BYTES, + blocks_per_chunk * iq4_nl::BLOCK_ELEMENTS * 4 + ), + (257 * iq4_nl::BLOCK_BYTES, 257 * iq4_nl::BLOCK_ELEMENTS * 4), + ] + ); + assert_eq!(consumed, source_bytes); + assert_eq!(output_bytes, (blocks * iq4_nl::BLOCK_ELEMENTS * 4) as u64); + assert_eq!(cursor.position(), (prefix + source_bytes) as u64); + } + + #[test] + fn iq4_stream_refuses_invalid_shapes_before_seek_or_callback() { + for source_bytes in [0, 1, 17, 19, 37] { + let mut cursor = Cursor::new([0u8; 64]); + cursor.set_position(7); + let mut calls = 0; + let error = visit(&mut cursor, 13, 20, source_bytes, |_, _| { + calls += 1; + Ok(()) + }) + .unwrap_err(); + assert_eq!(error, "GGUF source tensor is empty or not block-aligned"); + assert_eq!(calls, 0); + assert_eq!(cursor.position(), 7); + } + } + + #[test] + fn iq4_stream_truncation_never_delivers_an_incomplete_chunk() { + let chunk_bytes = SOURCE_CHUNK_BYTES / iq4_nl::BLOCK_BYTES * iq4_nl::BLOCK_BYTES; + for available in [ + iq4_nl::BLOCK_BYTES - 1, + chunk_bytes + iq4_nl::BLOCK_BYTES - 1, + ] { + let requested = if available < chunk_bytes { + iq4_nl::BLOCK_BYTES + } else { + chunk_bytes + iq4_nl::BLOCK_BYTES + }; + let mut calls = 0; + let result = visit( + &mut Cursor::new(vec![0u8; available]), + 0, + 20, + requested as u64, + |raw, derived| { + calls += 1; + assert_eq!(raw.len(), chunk_bytes); + assert_eq!( + derived.len(), + chunk_bytes / iq4_nl::BLOCK_BYTES * iq4_nl::BLOCK_ELEMENTS * 4 + ); + Ok(()) + }, + ); + assert!(result.is_err()); + assert_eq!(calls, usize::from(available >= chunk_bytes)); + } + } + + #[test] + fn iq4_stream_callback_failure_stops_without_reading_the_next_chunk() { + let chunk_bytes = SOURCE_CHUNK_BYTES / iq4_nl::BLOCK_BYTES * iq4_nl::BLOCK_BYTES; + for fail_on in [1, 2] { + let mut cursor = Cursor::new(vec![0u8; chunk_bytes * 3]); + let mut calls = 0; + let error = visit(&mut cursor, 0, 20, (chunk_bytes * 3) as u64, |raw, _| { + calls += 1; + assert_eq!(raw.len(), chunk_bytes); + if calls == fail_on { + Err("owned callback failure".into()) + } else { + Ok(()) + } + }) + .unwrap_err(); + assert_eq!(error, "owned callback failure"); + assert_eq!(calls, fail_on); + assert_eq!(cursor.position(), (chunk_bytes * fail_on) as u64); + } + } + + #[test] + fn iq4_stream_preserves_existing_nonfinite_classification_and_infinity_signs() { + for scale in [0x7c00u16, 0xfc00, 0x7c01, 0x7fff, 0xfc01, 0xffff] { + let mut source = AlignedSource([0u8; SOURCE_CHUNK_BYTES]); + source.0[..2].copy_from_slice(&scale.to_le_bytes()); + for (position, byte) in source.0[2..iq4_nl::BLOCK_BYTES].iter_mut().enumerate() { + *byte = ((position as u8) << 4) | (15 - position as u8); + } + let raw = &source.0[..iq4_nl::BLOCK_BYTES]; + let mut reference = [0.0; iq4_nl::BLOCK_ELEMENTS]; + ggml_bridge::decode_into(20, raw, &mut reference).unwrap(); + let mut calls = 0; + let count = visit( + &mut Cursor::new(raw), + 0, + 20, + iq4_nl::BLOCK_BYTES as u64, + |_, derived| { + calls += 1; + for (word, reference) in derived.as_chunks::<4>().0.iter().zip(reference) { + let actual = f32::from_le_bytes(*word); + if reference.is_nan() { + assert!(actual.is_nan()); + } else { + assert!(reference.is_infinite()); + assert_eq!(actual.to_bits(), reference.to_bits()); + } + } + assert_eq!(derived.len(), iq4_nl::BLOCK_ELEMENTS * 4); + Ok(()) + }, + ) + .unwrap(); + assert_eq!(calls, 1); + assert_eq!(count, (iq4_nl::BLOCK_ELEMENTS * 4) as u64); + } + } + #[test] fn actual_target_quant_types_decode_one_block_each() { for kind in [8, 12, 13, 14, 18, 20, 21, 22, 23, 42] { diff --git a/crates/quant-core/README.md b/crates/quant-core/README.md index 0fdf8eac..8f57a72b 100644 --- a/crates/quant-core/README.md +++ b/crates/quant-core/README.md @@ -1,7 +1,7 @@ # Leleka quantization foundation -`leleka-quant-core` extracts the existing IQ3_S CPU decoder and binary16-to-binary32 -conversion into an independent Rust crate. Production code uses only the standard +`leleka-quant-core` provides IQ3_S and IQ4_NL 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 @@ -24,6 +24,25 @@ 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. + The existing tests preserve the SHA-256 of the little-endian grid bytes (`bd1af4945a1717c65610b0284e4628b9a1ba3b306fae3a06f6e5f597356e349f`) and the mixed-block decoded f32 bytes diff --git a/crates/quant-core/src/iq4_nl.rs b/crates/quant-core/src/iq4_nl.rs new file mode 100644 index 00000000..a8fd910e --- /dev/null +++ b/crates/quant-core/src/iq4_nl.rs @@ -0,0 +1,122 @@ +//! Bounded CPU decoding of GGML IQ4_NL blocks (32 values / 18 bytes). +//! Layout and codebook follow llama.cpp 3cf03257f219afbe7334045ff7c6a06ac68c627d +//! `ggml-quants.c::dequantize_row_iq4_nl` and `ggml-common.h::kvalues_iq4nl`. +//! The table is MIT licensed by the ggml authors; see ../LICENSE-GGML. +//! Nonfinite scales are propagated, matching this preparation format's C path. + +use crate::f16_bits_to_f32; + +pub const BLOCK_BYTES: usize = 18; +pub const BLOCK_ELEMENTS: usize = 32; + +const VALUES: [i8; 16] = [ + -127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113, +]; + +/// Decode one complete block. Every FP16 scale encoding is admitted, including +/// NaN and infinity; consumers retain responsibility for their value policy. +pub fn decode_block(block: &[u8; BLOCK_BYTES]) -> Result<[f32; BLOCK_ELEMENTS], String> { + let scale = f16_bits_to_f32(u16::from_le_bytes([block[0], block[1]])); + let mut output = [0.0; BLOCK_ELEMENTS]; + for (index, packed) in block[2..].iter().enumerate() { + output[index] = scale * f32::from(VALUES[usize::from(packed & 15)]); + output[index + BLOCK_ELEMENTS / 2] = scale * f32::from(VALUES[usize::from(packed >> 4)]); + } + Ok(output) +} + +/// Decode complete blocks into an exactly sized caller-owned output slice. +/// Shape errors are rejected before any output is written. Empty slices form a +/// valid zero-block operation. No alignment requirement or heap buffer is used. +pub fn decode_into(blocks: &[u8], output: &mut [f32]) -> Result<(), String> { + if !blocks.len().is_multiple_of(BLOCK_BYTES) + || (blocks.len() / BLOCK_BYTES) + .checked_mul(BLOCK_ELEMENTS) + .is_none_or(|expected| expected != output.len()) + { + return Err("IQ4_NL block and output lengths do not match".into()); + } + for (block, values) in blocks + .as_chunks::() + .0 + .iter() + .zip(output.as_chunks_mut::().0.iter_mut()) + { + values.copy_from_slice(&decode_block(block)?); + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn split_half_order_matches_independent_codebook_values() { + let mut block = [0u8; BLOCK_BYTES]; + block[..2].copy_from_slice(&0x3800u16.to_le_bytes()); + for (index, packed) in block[2..].iter_mut().enumerate() { + *packed = index as u8 | ((15 - index as u8) << 4); + } + let expected = [ + -63.5, -52.0, -41.5, -32.5, -24.5, -17.5, -11.0, -5.0, 0.5, 6.5, 12.5, 19.0, 26.5, + 34.5, 44.5, 56.5, 56.5, 44.5, 34.5, 26.5, 19.0, 12.5, 6.5, 0.5, -5.0, -11.0, -17.5, + -24.5, -32.5, -41.5, -52.0, -63.5, + ]; + let actual = decode_block(&block).unwrap(); + assert_eq!(actual, expected); + let blocks = block.repeat(2); + let mut output = [0.0; BLOCK_ELEMENTS * 2]; + decode_into(&blocks, &mut output).unwrap(); + assert_eq!(&output[..BLOCK_ELEMENTS], &expected); + assert_eq!(&output[BLOCK_ELEMENTS..], &expected); + } + + #[test] + fn malformed_shapes_leave_output_untouched() { + let blocks = [0u8; BLOCK_BYTES * 2]; + for source_len in [1, BLOCK_BYTES - 1, BLOCK_BYTES + 1, blocks.len() - 1] { + let mut output = [123.0; BLOCK_ELEMENTS * 2]; + assert!(decode_into(&blocks[..source_len], &mut output).is_err()); + assert_eq!(output, [123.0; BLOCK_ELEMENTS * 2]); + } + for output_len in [ + 0, + BLOCK_ELEMENTS - 1, + BLOCK_ELEMENTS + 1, + BLOCK_ELEMENTS * 2, + ] { + let mut output = [123.0; BLOCK_ELEMENTS * 2]; + assert!(decode_into(&blocks[..BLOCK_BYTES], &mut output[..output_len]).is_err()); + assert_eq!(output, [123.0; BLOCK_ELEMENTS * 2]); + } + let mut output = [123.0; BLOCK_ELEMENTS]; + assert!(decode_into(&[], &mut output).is_err()); + assert_eq!(output, [123.0; BLOCK_ELEMENTS]); + decode_into(&[], &mut []).unwrap(); + } + + #[test] + fn signed_zero_and_nonfinite_scales_are_admitted() { + let mut block = [0u8; BLOCK_BYTES]; + block[2..].fill(0x80); // Negative low codepoint; positive high codepoint. + for (scale, low, high) in [(0x0000u16, 0x8000_0000, 0), (0x8000, 0, 0x8000_0000)] { + block[..2].copy_from_slice(&scale.to_le_bytes()); + let output = decode_block(&block).unwrap(); + assert_eq!(output[0].to_bits(), low); + assert_eq!(output[16].to_bits(), high); + } + for scale in [0x7c00u16, 0xfc00, 0x7c01, 0xfc01, 0x7e00, 0xfe00] { + block[..2].copy_from_slice(&scale.to_le_bytes()); + let mut output = [0.0; BLOCK_ELEMENTS]; + decode_into(&block, &mut output).unwrap(); + if scale & 0x03ff == 0 { + assert!(output.iter().all(|value| value.is_infinite())); + assert_eq!(output[0].is_sign_negative(), scale & 0x8000 == 0); + assert_eq!(output[16].is_sign_negative(), scale & 0x8000 != 0); + } else { + assert!(output.iter().all(|value| value.is_nan())); + } + } + } +} diff --git a/crates/quant-core/src/lib.rs b/crates/quant-core/src/lib.rs index 7b616d30..0ffd0017 100644 --- a/crates/quant-core/src/lib.rs +++ b/crates/quant-core/src/lib.rs @@ -3,6 +3,7 @@ pub mod iq3_s; mod iq3s_grid; +pub mod iq4_nl; pub fn f16_bits_to_f32(bits: u16) -> f32 { let sign = (u32::from(bits & 0x8000)) << 16; diff --git a/docs/iq4-reference-parity.md b/docs/iq4-reference-parity.md new file mode 100644 index 00000000..db694f9e --- /dev/null +++ b/docs/iq4-reference-parity.md @@ -0,0 +1,93 @@ +# IQ4_NL CPU reference parity + +The pure Rust `leleka_quant_core::iq4_nl` decoder implements GGUF kind 20: +18 bytes produce 32 F32 values. Bytes 0–1 hold a little-endian FP16 scale and +bytes 2–17 hold codepoint pairs. The low nibble at byte `2+j` produces element +`j`; the high nibble produces element `j+16`. Multiplication uses the existing +binary16-to-binary32 helper and the signed nonlinear codebook: + +```text +-127 -104 -83 -65 -49 -35 -22 -10 1 13 25 38 53 69 89 113 +``` + +The arithmetic/table oracle is the unmodified vendored +`ggml-quants.c::dequantize_row_iq4_nl` and `ggml-common.h::kvalues_iq4nl` from +llama.cpp `3cf03257f219afbe7334045ff7c6a06ac68c627d`, also pinned by imported +Strata `d9ab8435f654c368c586340d490915f6addf56a3`. Source hashes and license +remain in [GGML provenance](../control-plane/third_party/ggml/UPSTREAM.md); +the quant crate's [MIT notice](../crates/quant-core/LICENSE-GGML) is retained. +No upstream source, table or pin is changed. The independent Rust crate has no +C/FFI, GGML, Strata, Python or CUDA build/runtime dependency. + +## Numerical and API gates + +The new control-plane oracle exhausts all **63,488 finite FP16 scale encodings**, +including both signed zeros, every signed subnormal and normal, and both signs +of the maximum finite value. For every scale its independently generated block +covers all 16 codepoints in each output half. Every decoded value is compared +with the actual pinned C decoder using `f32::to_bits()`: **zero tolerance**, +including signed zero. This is **2,031,616 element comparisons**. Coverage +assertions additionally require all 256 packed byte values at every one of the +16 byte positions and all 16 codepoints at every one of the 32 output positions. +This exhausts the scale/codepoint combinations but not the joint space of all +16 packed bytes. A separate 14-block batch compares exact output bits through +both bulk APIs with deliberately unaligned input, exercising the existing C +bridge's alignment-copy path. + +All **2,048 nonfinite FP16 scale encodings** are admitted by both Rust APIs and +the C oracle, preserving IQ4_NL's existing consumer behavior. The two infinity +encodings require exact output bits/signs. All 2,046 NaN encodings require NaN +classification; no portable NaN payload or sign equivalence is claimed. The +existing IQ3_S refusal of nonfinite scales is unchanged. Existing consumers +keep their own rejection or downstream value policies. + +Standalone tests check independent expected codebook values and split-half +order, signed zero, nonfinite admission, multiple blocks, empty input/output, +malformed input blocks and incorrect output lengths. Invalid shapes leave the +entire caller output unchanged. The decoder allocates no heap buffer and decodes +into caller-owned storage. The visitor remains bounded and retains errors and +consumer receipts; other format branches are unchanged. + +Only generated synthetic fixtures are used. Tracked Qwen evidence identifies +IQ4_NL expert/shared-expert/PLE tensors in +[historical validation](build-validation.md), but this gate reads no weights. +This qualifies CPU decoding and bounded preparation integration, not complete +model support, immutable weights, readiness, GPU behavior, model quality or +performance. Other target formats still require C; the control-plane build +still compiles its legacy GGML oracle. Removing that build dependency is a +separate task. + +## Targeted commands + +```sh +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 +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`). +- Quant crate: **5 passed, 0 failed**, including three new IQ4_NL tests; + standalone Clippy with warnings denied passed. +- Bridge oracle: **9 passed, 0 failed** (177 unrelated tests filtered), including + three new IQ4_NL tests. All 2,031,616 finite-scale element comparisons, + packed-field/codepoint coverage assertions, unaligned batch comparisons and + all nonfinite admission/classification/infinity-sign comparisons passed. +- QA's separate visitor gate: **7 passed, 0 failed**. Its new finite fixture + compared 1,821 blocks with C output across two callbacks: source chunks + 28,152 and 4,626 bytes (cap 28,160), derived chunks 200,192 and 32,896 bytes. + Shape rejection before seek/callback, early/late truncation, callback abort + at either chunk, and nonfinite classification/infinity signs passed. +- Vendored GGML manifest hashes, owned-file formatting and `git diff --check` + passed. The production C bridge body and all six earlier bridge test + functions remain byte unchanged. The private oracle build target was cleaned. + +No finite numeric mismatch was observed, and no numerical tolerance was relaxed. +NaN payload/sign differences were not measured; the nonfinite gate compares the +explicit classification/admission contract above. These results apply to this +CPU/compiler configuration; Linux/GCC, real-model and GPU qualification remain +separate.