WIP: STRA-102 Rust IQ2_S and IQ3_XXS model preparation #12
@@ -1064,4 +1064,291 @@ mod tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn pinned_c_table_bytes(name: &str, count: usize, width: usize) -> Vec<u8> {
|
||||
let source = include_str!("../third_party/ggml/ggml-common.h");
|
||||
let marker = format!(", {name}, {count})");
|
||||
let body = source
|
||||
.split_once(&marker)
|
||||
.unwrap()
|
||||
.1
|
||||
.split_once("GGML_TABLE_END()")
|
||||
.unwrap()
|
||||
.0;
|
||||
let values: Vec<u64> = body
|
||||
.split(',')
|
||||
.map(str::trim)
|
||||
.filter(|v| !v.is_empty())
|
||||
.map(|v| {
|
||||
if let Some(hex) = v.strip_prefix("0x") {
|
||||
u64::from_str_radix(hex, 16).unwrap()
|
||||
} else {
|
||||
v.parse().unwrap()
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(values.len(), count);
|
||||
values
|
||||
.iter()
|
||||
.flat_map(|value| value.to_le_bytes()[..width].to_vec())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pinned_c_iq2s_iq3xxs_complete_grid_sign_and_mask_bytes_match_port_provenance() {
|
||||
use sha2::{Digest, Sha256};
|
||||
for (name, count, width, digest) in [
|
||||
(
|
||||
"iq2s_grid",
|
||||
1024,
|
||||
8,
|
||||
"e1aa1473412b0552c2174c30ef22ab4073f6a181b85a17056e8249bd2932fd88",
|
||||
),
|
||||
(
|
||||
"iq3xxs_grid",
|
||||
256,
|
||||
4,
|
||||
"46e35f5a997efdee6c99ce57854c8a0d4f0ff8ca57e5e8a60c0793ea580acf5d",
|
||||
),
|
||||
(
|
||||
"ksigns_iq2xs",
|
||||
128,
|
||||
1,
|
||||
"a76742a603f8beca5212ecce0f1f02f11a4887fd2f7c8b15aca0ea3eb3380c31",
|
||||
),
|
||||
(
|
||||
"kmask_iq2xs",
|
||||
8,
|
||||
1,
|
||||
"5ac9831b2e30eb285ef34f8501620f878432d5c04331ad1ae47f977a83ba41a5",
|
||||
),
|
||||
] {
|
||||
let bytes = pinned_c_table_bytes(name, count, width);
|
||||
assert_eq!(hex::encode(Sha256::digest(&bytes)), digest);
|
||||
if name.ends_with("_grid") {
|
||||
assert!(bytes.iter().all(|value| *value != 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Independently pack wire fields; neither decoder nor its tables/helpers generate inputs.
|
||||
fn synthetic_iq2s_iq3xxs_block(kind: u32, bits: u16, pattern: usize) -> Vec<u8> {
|
||||
let mut block = vec![0u8; if kind == 22 { 82 } else { 98 }];
|
||||
block[..2].copy_from_slice(&bits.to_le_bytes());
|
||||
if kind == 22 {
|
||||
for group in 0..32 {
|
||||
let grid = (pattern + group * 17) & 1023;
|
||||
block[2 + group] = grid as u8;
|
||||
block[66 + group / 4] |= ((grid >> 8) as u8) << ((group % 4) * 2);
|
||||
block[34 + group] = (pattern + group * 29) as u8;
|
||||
}
|
||||
for subblock in 0..8 {
|
||||
block[74 + subblock] =
|
||||
(((pattern + subblock) & 15) | ((((pattern >> 4) + subblock) & 15) << 4)) as u8;
|
||||
}
|
||||
} else {
|
||||
assert_eq!(kind, 18);
|
||||
for field in 0..64 {
|
||||
block[2 + field] = (pattern + field * 17) as u8;
|
||||
}
|
||||
for subblock in 0..8 {
|
||||
let mut word = ((((pattern >> 7) + subblock) & 15) as u32) << 28;
|
||||
for group in 0..4 {
|
||||
word |= (((pattern + group * 17 + subblock * 29) & 127) as u32) << (group * 7);
|
||||
}
|
||||
block[66 + subblock * 4..70 + subblock * 4].copy_from_slice(&word.to_le_bytes());
|
||||
}
|
||||
}
|
||||
block
|
||||
}
|
||||
|
||||
fn rust_iq2s_iq3xxs_into(kind: u32, source: &[u8], output: &mut [f32]) -> Result<(), String> {
|
||||
if kind == 22 {
|
||||
leleka_quant_core::iq2_s::decode_into(source, output)
|
||||
} else {
|
||||
assert_eq!(kind, 18);
|
||||
leleka_quant_core::iq3_xxs::decode_into(source, output)
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_iq2s_iq3xxs_c_bits(kind: u32, source: &[u8], finite: bool) {
|
||||
let bytes = if kind == 22 { 82 } else { 98 };
|
||||
let mut expected = vec![0.0; source.len() / bytes * 256];
|
||||
let mut actual = vec![0.0; expected.len()];
|
||||
decode_into(kind, source, &mut expected).unwrap();
|
||||
rust_iq2s_iq3xxs_into(kind, source, &mut actual).unwrap();
|
||||
for (position, (actual, expected)) in actual.iter().zip(&expected).enumerate() {
|
||||
if finite {
|
||||
assert!(actual.is_finite() && expected.is_finite());
|
||||
}
|
||||
if expected.is_nan() {
|
||||
assert!(actual.is_nan(), "kind {kind} position {position}");
|
||||
} else {
|
||||
assert_eq!(
|
||||
actual.to_bits(),
|
||||
expected.to_bits(),
|
||||
"kind {kind} position {position}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pinned_ggml_iq2s_iq3xxs_every_finite_fp16_matches_c_bits() {
|
||||
let mut headers = 0;
|
||||
for bits in 0..=u16::MAX {
|
||||
if bits & 0x7c00 == 0x7c00 {
|
||||
continue;
|
||||
}
|
||||
for kind in [22, 18] {
|
||||
assert_iq2s_iq3xxs_c_bits(
|
||||
kind,
|
||||
&synthetic_iq2s_iq3xxs_block(kind, bits, usize::from(bits)),
|
||||
true,
|
||||
);
|
||||
}
|
||||
headers += 1;
|
||||
}
|
||||
assert_eq!(headers, 63_488);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pinned_ggml_iq2s_iq3xxs_all_grid_sign_subscale_positions_match_c_bits() {
|
||||
let c_signs = pinned_c_table_bytes("ksigns_iq2xs", 128, 1);
|
||||
for kind in [22, 18] {
|
||||
let grid_count = if kind == 22 { 1024 } else { 256 };
|
||||
let group_count = if kind == 22 { 32 } else { 64 };
|
||||
let sign_count = if kind == 22 { 256 } else { 128 };
|
||||
let mut grids = vec![vec![false; grid_count]; group_count];
|
||||
let mut signs = vec![vec![false; sign_count]; 32];
|
||||
let mut scale = vec![[false; 16]; if kind == 22 { 16 } else { 8 }];
|
||||
let mut output_signs = vec![[false; 2]; 256];
|
||||
for pattern in 0..if kind == 22 { 1024 } else { 2048 } {
|
||||
let block = synthetic_iq2s_iq3xxs_block(kind, 0xb555, pattern);
|
||||
assert_iq2s_iq3xxs_c_bits(kind, &block, true);
|
||||
for group in 0..32 {
|
||||
let sign = if kind == 22 {
|
||||
let grid = usize::from(block[2 + group])
|
||||
| (usize::from((block[66 + group / 4] >> ((group % 4) * 2)) & 3) << 8);
|
||||
grids[group][grid] = true;
|
||||
let sign = usize::from(block[34 + group]);
|
||||
signs[group][sign] = true;
|
||||
sign as u8
|
||||
} else {
|
||||
let subblock = group / 4;
|
||||
let word = u32::from_le_bytes(
|
||||
block[66 + subblock * 4..70 + subblock * 4]
|
||||
.try_into()
|
||||
.unwrap(),
|
||||
);
|
||||
let code = ((word >> ((group % 4) * 7)) & 127) as usize;
|
||||
signs[group][code] = true;
|
||||
grids[group * 2][usize::from(block[2 + group * 2])] = true;
|
||||
grids[group * 2 + 1][usize::from(block[3 + group * 2])] = true;
|
||||
c_signs[code]
|
||||
};
|
||||
for lane in 0..8 {
|
||||
output_signs[group * 8 + lane][usize::from((sign >> lane) & 1)] = true;
|
||||
}
|
||||
}
|
||||
for (position, seen) in scale.iter_mut().enumerate() {
|
||||
let code = if kind == 22 {
|
||||
(block[74 + position / 2] >> ((position % 2) * 4)) & 15
|
||||
} else {
|
||||
let word = u32::from_le_bytes(
|
||||
block[66 + position * 4..70 + position * 4]
|
||||
.try_into()
|
||||
.unwrap(),
|
||||
);
|
||||
(word >> 28) as u8
|
||||
};
|
||||
seen[usize::from(code)] = true;
|
||||
}
|
||||
}
|
||||
assert!(grids.iter().all(|seen| seen.iter().all(|v| *v)));
|
||||
assert!(signs.iter().all(|seen| seen.iter().all(|v| *v)));
|
||||
assert!(scale.iter().all(|seen| seen.iter().all(|v| *v)));
|
||||
assert!(output_signs.iter().all(|seen| seen.iter().all(|v| *v)));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pinned_ggml_iq2s_iq3xxs_every_raw_field_byte_matches_c_bits() {
|
||||
for kind in [22, 18] {
|
||||
let bytes = if kind == 22 { 82 } else { 98 };
|
||||
let mut seen = vec![[false; 256]; bytes - 2];
|
||||
for pattern in 0..=u8::MAX {
|
||||
let mut block = vec![0; bytes];
|
||||
block[..2].copy_from_slice(&0x3555u16.to_le_bytes());
|
||||
for (field, flags) in seen.iter_mut().enumerate() {
|
||||
let value = pattern
|
||||
.wrapping_mul((field * 2 + 1) as u8)
|
||||
.rotate_left((field % 8) as u32)
|
||||
.wrapping_add((field * 19) as u8);
|
||||
block[2 + field] = value;
|
||||
flags[usize::from(value)] = true;
|
||||
}
|
||||
assert_iq2s_iq3xxs_c_bits(kind, &block, true);
|
||||
}
|
||||
assert!(seen.iter().all(|seen| seen.iter().all(|v| *v)));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pinned_ggml_iq2s_iq3xxs_all_nonfinite_headers_and_zero_fields_preserve_c_values() {
|
||||
let mut headers = 0;
|
||||
for bits in 0..=u16::MAX {
|
||||
if bits & 0x7c00 != 0x7c00 {
|
||||
continue;
|
||||
}
|
||||
for kind in [22, 18] {
|
||||
let mut block = synthetic_iq2s_iq3xxs_block(kind, bits, usize::from(bits));
|
||||
assert_iq2s_iq3xxs_c_bits(kind, &block, false);
|
||||
block[2..].fill(0);
|
||||
assert_iq2s_iq3xxs_c_bits(kind, &block, false);
|
||||
}
|
||||
headers += 1;
|
||||
}
|
||||
assert_eq!(headers, 2048);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pinned_ggml_iq2s_iq3xxs_aligned_unaligned_bulk_and_shape_sentinels_match() {
|
||||
let edges = [
|
||||
0x0000u16, 0x8000, 0x0001, 0x8001, 0x03ff, 0x83ff, 0x0400, 0x8400, 0x3555, 0xb555,
|
||||
0x3800, 0xb800, 0x3c00, 0xbc00, 0x7bff, 0xfbff,
|
||||
];
|
||||
for kind in [22, 18] {
|
||||
let bytes = if kind == 22 { 82 } else { 98 };
|
||||
for unaligned in [false, true] {
|
||||
let mut storage = vec![0; edges.len() * bytes + 16];
|
||||
let start = storage.as_ptr().align_offset(16) + usize::from(unaligned);
|
||||
let source = &mut storage[start..start + edges.len() * bytes];
|
||||
assert_eq!(
|
||||
source.as_ptr().align_offset(16),
|
||||
if unaligned { 15 } else { 0 }
|
||||
);
|
||||
for (number, bits) in edges.into_iter().enumerate() {
|
||||
source[number * bytes..(number + 1) * bytes]
|
||||
.copy_from_slice(&synthetic_iq2s_iq3xxs_block(kind, bits, number * 257));
|
||||
}
|
||||
assert_iq2s_iq3xxs_c_bits(kind, source, true);
|
||||
let outputs = edges.len() * 256;
|
||||
for (input_len, output_len) in [
|
||||
(0, outputs),
|
||||
(source.len() - 1, outputs),
|
||||
(source.len(), outputs - 1),
|
||||
(source.len(), outputs + 1),
|
||||
] {
|
||||
let mut output = vec![123.0; output_len];
|
||||
assert!(
|
||||
rust_iq2s_iq3xxs_into(kind, &source[..input_len], &mut output).is_err()
|
||||
);
|
||||
assert!(output.iter().all(|value| *value == 123.0));
|
||||
assert!(decode_into(kind, &source[..input_len], &mut output).is_err());
|
||||
assert!(output.iter().all(|value| *value == 123.0));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
use std::io::{Read, Seek, SeekFrom};
|
||||
|
||||
use leleka_quant_core::{iq4_nl, iq4_xs, q2_0, q4_k, q5_k, q6_k, q8_0};
|
||||
use leleka_quant_core::{iq2_s, iq3_xxs, iq4_nl, iq4_xs, q2_0, q4_k, q5_k, q6_k, q8_0};
|
||||
|
||||
use crate::{ggml_bridge, gguf, iq3_s, kar_codec};
|
||||
|
||||
@@ -31,6 +31,8 @@ where
|
||||
13 => Some((q5_k::BLOCK_ELEMENTS, q5_k::BLOCK_BYTES, "Q5_K")),
|
||||
14 => Some((q6_k::BLOCK_ELEMENTS, q6_k::BLOCK_BYTES, "Q6_K")),
|
||||
42 => Some((q2_0::BLOCK_ELEMENTS, q2_0::BLOCK_BYTES, "Q2_0")),
|
||||
18 => Some((iq3_xxs::BLOCK_ELEMENTS, iq3_xxs::BLOCK_BYTES, "IQ3_XXS")),
|
||||
22 => Some((iq2_s::BLOCK_ELEMENTS, iq2_s::BLOCK_BYTES, "IQ2_S")),
|
||||
20 => Some((iq4_nl::BLOCK_ELEMENTS, iq4_nl::BLOCK_BYTES, "IQ4_NL")),
|
||||
23 => Some((iq4_xs::BLOCK_ELEMENTS, iq4_xs::BLOCK_BYTES, "IQ4_XS")),
|
||||
_ => None,
|
||||
@@ -119,6 +121,22 @@ where
|
||||
}
|
||||
callback(source, &derived)?;
|
||||
}
|
||||
18 => {
|
||||
for block in source.as_chunks::<{ iq3_xxs::BLOCK_BYTES }>().0 {
|
||||
for value in iq3_xxs::decode_block(block)? {
|
||||
derived.extend_from_slice(&value.to_le_bytes());
|
||||
}
|
||||
}
|
||||
callback(source, &derived)?;
|
||||
}
|
||||
22 => {
|
||||
for block in source.as_chunks::<{ iq2_s::BLOCK_BYTES }>().0 {
|
||||
for value in iq2_s::decode_block(block)? {
|
||||
derived.extend_from_slice(&value.to_le_bytes());
|
||||
}
|
||||
}
|
||||
callback(source, &derived)?;
|
||||
}
|
||||
20 => {
|
||||
for block in source.as_chunks::<{ iq4_nl::BLOCK_BYTES }>().0 {
|
||||
for value in iq4_nl::decode_block(block)? {
|
||||
@@ -908,4 +926,181 @@ mod tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn iq2s_iq3xxs_streams_preserve_bounded_c_bytes_offsets_receipts_and_failure_semantics() {
|
||||
let edges = [
|
||||
0x0000u16, 0x8000, 0x0001, 0x83ff, 0x0400, 0x3555, 0xbc00, 0x7bff,
|
||||
];
|
||||
for kind in [22, 18] {
|
||||
let (elements, bytes) = gguf::block_geometry(kind).unwrap();
|
||||
let bytes = bytes as usize;
|
||||
let per_chunk = SOURCE_CHUNK_BYTES / bytes;
|
||||
let blocks = per_chunk + 17;
|
||||
let prefix = 11;
|
||||
let mut source = vec![0xa5; prefix];
|
||||
for number in 0..blocks {
|
||||
let mut block = vec![0u8; bytes];
|
||||
for (position, byte) in block.iter_mut().enumerate() {
|
||||
*byte = (number * 17 + position * 29) as u8;
|
||||
}
|
||||
let header = 0;
|
||||
block[header..header + 2]
|
||||
.copy_from_slice(&edges[number % edges.len()].to_le_bytes());
|
||||
source.extend_from_slice(&block);
|
||||
}
|
||||
let requested = blocks * bytes;
|
||||
source.extend_from_slice(&[0x5a; 19]);
|
||||
let mut cursor = Cursor::new(&source);
|
||||
let mut consumed = 0;
|
||||
let mut chunks = Vec::new();
|
||||
let receipt = visit(
|
||||
&mut cursor,
|
||||
prefix as u64,
|
||||
kind,
|
||||
requested as u64,
|
||||
|raw, derived| {
|
||||
assert!(raw.len() <= SOURCE_CHUNK_BYTES);
|
||||
assert_eq!(
|
||||
raw,
|
||||
&source[prefix + consumed..prefix + consumed + raw.len()]
|
||||
);
|
||||
let mut oracle = vec![0.0; raw.len() / bytes * elements as usize];
|
||||
ggml_bridge::decode_into(kind, raw, &mut oracle).unwrap();
|
||||
let expected: Vec<u8> = oracle
|
||||
.iter()
|
||||
.flat_map(|value| value.to_le_bytes())
|
||||
.collect();
|
||||
assert_eq!(derived, expected, "kind {kind}");
|
||||
assert!(derived.len() <= per_chunk * elements as usize * 4);
|
||||
chunks.push((raw.len(), derived.len()));
|
||||
consumed += raw.len();
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
chunks,
|
||||
[
|
||||
(per_chunk * bytes, per_chunk * elements as usize * 4),
|
||||
(17 * bytes, 17 * elements as usize * 4)
|
||||
]
|
||||
);
|
||||
assert_eq!(consumed, requested);
|
||||
assert_eq!(receipt, blocks as u64 * elements * 4);
|
||||
assert_eq!(cursor.position(), (prefix + requested) as u64);
|
||||
for malformed in [0, 1, bytes - 1, bytes + 1] {
|
||||
let mut cursor = Cursor::new(&source);
|
||||
cursor.set_position(7);
|
||||
let mut calls = 0;
|
||||
assert!(
|
||||
visit(
|
||||
&mut cursor,
|
||||
prefix as u64,
|
||||
kind,
|
||||
malformed as u64,
|
||||
|_, _| {
|
||||
calls += 1;
|
||||
Ok(())
|
||||
}
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(calls, 0);
|
||||
assert_eq!(cursor.position(), 7);
|
||||
}
|
||||
for delivered in [0, 1] {
|
||||
let available = delivered * per_chunk * bytes + bytes - 1;
|
||||
let required = delivered * per_chunk * bytes + bytes;
|
||||
let mut calls = 0;
|
||||
assert!(
|
||||
visit(
|
||||
&mut Cursor::new(&source[..prefix + available]),
|
||||
prefix as u64,
|
||||
kind,
|
||||
required as u64,
|
||||
|raw, derived| {
|
||||
calls += 1;
|
||||
assert_eq!(raw.len(), per_chunk * bytes);
|
||||
assert_eq!(derived.len(), per_chunk * elements as usize * 4);
|
||||
Ok(())
|
||||
}
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(calls, delivered);
|
||||
}
|
||||
for fail_on in [1, 2] {
|
||||
let mut cursor = Cursor::new(&source);
|
||||
let mut calls = 0;
|
||||
let error = visit(
|
||||
&mut cursor,
|
||||
prefix as u64,
|
||||
kind,
|
||||
requested as u64,
|
||||
|_, _| {
|
||||
calls += 1;
|
||||
if calls == fail_on {
|
||||
Err("owned callback abort".into())
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
)
|
||||
.unwrap_err();
|
||||
assert_eq!(error, "owned callback abort");
|
||||
assert_eq!(calls, fail_on);
|
||||
assert_eq!(
|
||||
cursor.position(),
|
||||
(prefix
|
||||
+ if fail_on == 1 {
|
||||
per_chunk * bytes
|
||||
} else {
|
||||
requested
|
||||
}) as u64
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iq2s_iq3xxs_streams_preserve_nonfinite_signed_zero_and_zero_packed_fields() {
|
||||
for kind in [22, 18] {
|
||||
let (elements, bytes) = gguf::block_geometry(kind).unwrap();
|
||||
for bits in [
|
||||
0x0000u16, 0x8000, 0x7c00, 0xfc00, 0x7c01, 0xfc01, 0x7e00, 0xfe00,
|
||||
] {
|
||||
for zero_fields in [false, true] {
|
||||
let mut block = vec![0; bytes as usize];
|
||||
if !zero_fields {
|
||||
for (field, byte) in block[2..].iter_mut().enumerate() {
|
||||
*byte = (field * 29 + 17) as u8;
|
||||
}
|
||||
}
|
||||
block[..2].copy_from_slice(&bits.to_le_bytes());
|
||||
let mut oracle = vec![0.0; elements as usize];
|
||||
ggml_bridge::decode_into(kind, &block, &mut oracle).unwrap();
|
||||
let mut calls = 0;
|
||||
let receipt =
|
||||
visit(&mut Cursor::new(&block), 0, kind, bytes, |raw, derived| {
|
||||
assert_eq!(raw, block);
|
||||
assert_eq!(derived.len(), elements as usize * 4);
|
||||
for (actual, expected) in derived.as_chunks::<4>().0.iter().zip(&oracle)
|
||||
{
|
||||
let actual = f32::from_le_bytes(*actual);
|
||||
if expected.is_nan() {
|
||||
assert!(actual.is_nan());
|
||||
} else {
|
||||
assert_eq!(actual.to_bits(), expected.to_bits());
|
||||
}
|
||||
}
|
||||
calls += 1;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(calls, 1);
|
||||
assert_eq!(receipt, elements * 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# Leleka quantization foundation
|
||||
|
||||
`leleka-quant-core` provides IQ3_S, IQ4_NL, IQ4_XS, Q2_0, Q4_K, Q5_K, Q6_K and Q8_0
|
||||
`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.
|
||||
@@ -116,6 +117,49 @@ 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
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
//! Allocation-free IQ2_S CPU decoding (82 bytes / 256 values).
|
||||
//! Pinned to llama.cpp 3cf03257f219afbe7334045ff7c6a06ac68c627d
|
||||
//! `dequantize_row_iq2_s` and `block_iq2_s`; MIT, ggml authors (LICENSE-GGML).
|
||||
//! All FP16 encodings are admitted; consumers retain their value policy.
|
||||
|
||||
use crate::{f16_bits_to_f32, iq2s_grid::IQ2S_GRID};
|
||||
|
||||
pub const BLOCK_BYTES: usize = 82;
|
||||
pub const BLOCK_ELEMENTS: usize = 256;
|
||||
|
||||
pub fn decode_block(block: &[u8; BLOCK_BYTES]) -> Result<[f32; BLOCK_ELEMENTS], String> {
|
||||
let base = f16_bits_to_f32(u16::from_le_bytes([block[0], block[1]]));
|
||||
let mut output = [0.0; BLOCK_ELEMENTS];
|
||||
for subblock in 0..8 {
|
||||
let packed_scale = block[74 + subblock];
|
||||
let scales = [
|
||||
base * (0.5 + f32::from(packed_scale & 15)) * 0.25,
|
||||
base * (0.5 + f32::from(packed_scale >> 4)) * 0.25,
|
||||
];
|
||||
for group in 0..4 {
|
||||
let low = usize::from(block[2 + 4 * subblock + group]);
|
||||
let high = usize::from((block[66 + subblock] >> (2 * group)) & 3) << 8;
|
||||
let grid = IQ2S_GRID[low | high].to_le_bytes();
|
||||
let signs = block[34 + 4 * subblock + group];
|
||||
for (lane, magnitude) in grid.into_iter().enumerate() {
|
||||
let sign = if signs & (1 << lane) != 0 { -1.0 } else { 1.0 };
|
||||
output[32 * subblock + 8 * group + lane] =
|
||||
scales[group / 2] * f32::from(magnitude) * sign;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
/// Invalid shapes fail before writes. Empty input/output is a zero-block
|
||||
/// operation; no alignment requirement or heap decoding 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("IQ2_S block and output lengths do not match".into());
|
||||
}
|
||||
for (block, values) in blocks
|
||||
.as_chunks::<BLOCK_BYTES>()
|
||||
.0
|
||||
.iter()
|
||||
.zip(output.as_chunks_mut::<BLOCK_ELEMENTS>().0.iter_mut())
|
||||
{
|
||||
values.copy_from_slice(&decode_block(block)?);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
#[test]
|
||||
fn pinned_grid_little_endian_bytes_match_source_digest() {
|
||||
let mut digest = Sha256::new();
|
||||
for entry in IQ2S_GRID {
|
||||
digest.update(entry.to_le_bytes());
|
||||
}
|
||||
assert_eq!(
|
||||
hex::encode(digest.finalize()),
|
||||
"e1aa1473412b0552c2174c30ef22ab4073f6a181b85a17056e8249bd2932fd88"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn known_wire_values_cover_high_grid_bits_signs_and_scale_halves() {
|
||||
let mut block = [0u8; BLOCK_BYTES];
|
||||
block[..2].copy_from_slice(&0x3c00u16.to_le_bytes());
|
||||
block[34..38].copy_from_slice(&[1, 128, 0x55, 0xaa]);
|
||||
block[66] = 0xe4; // Grid indices 0,256,512,768.
|
||||
block[74] = 0x20; // Scale .125 for first16, .625 for second16.
|
||||
block[81] = 0x0f; // Last subblock first16 scale3.875.
|
||||
let output = decode_block(&block).unwrap();
|
||||
let expected = [
|
||||
-1.0f32, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 3.125, 3.125, 3.125, 3.125, 3.125, 1.0,
|
||||
3.125, -1.0, -15.625, 15.625, -26.875, 5.0, -15.625, 5.0, -5.0, 15.625, 5.0, -26.875,
|
||||
5.0, -15.625, 5.0, -5.0, 26.875, -15.625,
|
||||
];
|
||||
assert_eq!(&output[..32], &expected);
|
||||
assert_eq!(&output[32..224], &[1.0; 192]);
|
||||
assert_eq!(&output[224..240], &[31.0; 16]);
|
||||
assert_eq!(&output[240..], &[1.0; 16]);
|
||||
block[2..34].fill(255);
|
||||
block[34..66].fill(0);
|
||||
block[66..74].fill(255);
|
||||
block[74..].fill(0);
|
||||
assert_eq!(decode_block(&block).unwrap(), [5.375; BLOCK_ELEMENTS]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_eight_bit_signs_signed_zero_and_nonfinite_scales_are_admitted() {
|
||||
let mut block = [0u8; BLOCK_BYTES];
|
||||
block[..2].copy_from_slice(&0x3c00u16.to_le_bytes());
|
||||
for signs in 0..=u8::MAX {
|
||||
block[34..66].fill(signs);
|
||||
for (element, value) in decode_block(&block).unwrap().into_iter().enumerate() {
|
||||
assert_eq!(
|
||||
value,
|
||||
if signs & (1 << (element % 8)) != 0 {
|
||||
-1.0
|
||||
} else {
|
||||
1.0
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
block[34..66].fill(0x81);
|
||||
for scale in [
|
||||
0x0000u16, 0x8000, 0x7c00, 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();
|
||||
for (element, value) in output.into_iter().enumerate() {
|
||||
let negative = (scale & 0x8000 != 0) ^ matches!(element % 8, 0 | 7);
|
||||
if scale & 0x7c00 == 0x7c00 {
|
||||
if scale & 0x03ff != 0 {
|
||||
assert!(value.is_nan());
|
||||
} else {
|
||||
assert!(value.is_infinite());
|
||||
assert_eq!(value.is_sign_negative(), negative);
|
||||
}
|
||||
} else {
|
||||
assert_eq!(value.to_bits(), if negative { 0x8000_0000 } else { 0 });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unaligned_bulk_and_bad_shapes_preserve_output_contract() {
|
||||
let mut block = [0u8; BLOCK_BYTES];
|
||||
block[..2].copy_from_slice(&0x3c00u16.to_le_bytes());
|
||||
let mut storage = [0u8; BLOCK_BYTES * 2 + 16];
|
||||
let start = storage.as_ptr().align_offset(16) + 1;
|
||||
let source = &mut storage[start..start + BLOCK_BYTES * 2];
|
||||
assert_eq!(source.as_ptr().align_offset(16), 15);
|
||||
source[..BLOCK_BYTES].copy_from_slice(&block);
|
||||
source[BLOCK_BYTES..].copy_from_slice(&block);
|
||||
let mut output = [123.0; BLOCK_ELEMENTS * 2];
|
||||
decode_into(source, &mut output).unwrap();
|
||||
assert_eq!(output, [1.0; BLOCK_ELEMENTS * 2]);
|
||||
for len in [0, 1, BLOCK_BYTES - 1, BLOCK_BYTES + 1, source.len() - 1] {
|
||||
output.fill(123.0);
|
||||
assert!(decode_into(&source[..len], &mut output).is_err());
|
||||
assert_eq!(output, [123.0; BLOCK_ELEMENTS * 2]);
|
||||
}
|
||||
for len in [
|
||||
0,
|
||||
BLOCK_ELEMENTS - 1,
|
||||
BLOCK_ELEMENTS + 1,
|
||||
BLOCK_ELEMENTS * 2,
|
||||
] {
|
||||
output.fill(123.0);
|
||||
assert!(decode_into(&source[..BLOCK_BYTES], &mut output[..len]).is_err());
|
||||
assert_eq!(output, [123.0; BLOCK_ELEMENTS * 2]);
|
||||
}
|
||||
decode_into(&[], &mut []).unwrap();
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,189 @@
|
||||
//! Allocation-free IQ3_XXS CPU decoding (98 bytes / 256 values).
|
||||
//! Pinned to llama.cpp 3cf03257f219afbe7334045ff7c6a06ac68c627d
|
||||
//! `dequantize_row_iq3_xxs` and `block_iq3_xxs`; MIT, ggml authors (LICENSE-GGML).
|
||||
//! Explicit LE words/table bytes match the pinned C baseline on LE hosts.
|
||||
//! All FP16 encodings are admitted; consumers retain their value policy.
|
||||
|
||||
use crate::{
|
||||
f16_bits_to_f32,
|
||||
iq3xxs_grid::{IQ3XXS_GRID, KSIGNS_IQ2XS},
|
||||
};
|
||||
|
||||
pub const BLOCK_BYTES: usize = 98;
|
||||
pub const BLOCK_ELEMENTS: usize = 256;
|
||||
|
||||
pub fn decode_block(block: &[u8; BLOCK_BYTES]) -> Result<[f32; BLOCK_ELEMENTS], String> {
|
||||
let base = f16_bits_to_f32(u16::from_le_bytes([block[0], block[1]]));
|
||||
let mut output = [0.0; BLOCK_ELEMENTS];
|
||||
for subblock in 0..8 {
|
||||
let at = 66 + 4 * subblock;
|
||||
let packed = u32::from_le_bytes([block[at], block[at + 1], block[at + 2], block[at + 3]]);
|
||||
let scale = base * (0.5 + (packed >> 28) as f32) * 0.5;
|
||||
for group in 0..4 {
|
||||
let signs = KSIGNS_IQ2XS[((packed >> (7 * group)) & 127) as usize];
|
||||
for half in 0..2 {
|
||||
let index = usize::from(block[2 + 8 * subblock + 2 * group + half]);
|
||||
let grid = IQ3XXS_GRID[index].to_le_bytes();
|
||||
for (lane, magnitude) in grid.into_iter().enumerate() {
|
||||
let local = 4 * half + lane;
|
||||
let sign = if signs & (1 << local) != 0 { -1.0 } else { 1.0 };
|
||||
output[32 * subblock + 8 * group + local] = scale * f32::from(magnitude) * sign;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
/// Invalid shapes fail before writes. Empty input/output is a zero-block
|
||||
/// operation; no alignment requirement or heap decoding 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("IQ3_XXS block and output lengths do not match".into());
|
||||
}
|
||||
for (block, values) in blocks
|
||||
.as_chunks::<BLOCK_BYTES>()
|
||||
.0
|
||||
.iter()
|
||||
.zip(output.as_chunks_mut::<BLOCK_ELEMENTS>().0.iter_mut())
|
||||
{
|
||||
values.copy_from_slice(&decode_block(block)?);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
#[test]
|
||||
fn pinned_grid_and_sign_bytes_match_source_digests() {
|
||||
let mut digest = Sha256::new();
|
||||
for entry in IQ3XXS_GRID {
|
||||
digest.update(entry.to_le_bytes());
|
||||
}
|
||||
assert_eq!(
|
||||
hex::encode(digest.finalize()),
|
||||
"46e35f5a997efdee6c99ce57854c8a0d4f0ff8ca57e5e8a60c0793ea580acf5d"
|
||||
);
|
||||
assert_eq!(
|
||||
hex::encode(Sha256::digest(KSIGNS_IQ2XS)),
|
||||
"a76742a603f8beca5212ecce0f1f02f11a4887fd2f7c8b15aca0ea3eb3380c31"
|
||||
);
|
||||
for code in 0..128u8 {
|
||||
assert_eq!(
|
||||
KSIGNS_IQ2XS[usize::from(code)],
|
||||
code | (((code.count_ones() & 1) as u8) << 7)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn known_words_grid_byte_order_and_sign_fields_match_expected_values() {
|
||||
let mut block = [0u8; BLOCK_BYTES];
|
||||
block[..2].copy_from_slice(&0x3c00u16.to_le_bytes());
|
||||
for pair in block[2..66].as_chunks_mut::<2>().0 {
|
||||
pair[1] = 1;
|
||||
}
|
||||
block[66..70].copy_from_slice(&[1, 1, 1, 0x11]); // Codes1/2/4/8, scale nibble1.
|
||||
block[94..98].fill(255); // Four signcodes127, scale nibble15.
|
||||
block[64] = 255;
|
||||
block[65] = 0;
|
||||
let output = decode_block(&block).unwrap();
|
||||
let expected = [
|
||||
-3.0f32, 3.0, 3.0, 3.0, 15.0, 3.0, 3.0, -3.0, 3.0, -3.0, 3.0, 3.0, 15.0, 3.0, 3.0,
|
||||
-3.0, 3.0, 3.0, -3.0, 3.0, 15.0, 3.0, 3.0, -3.0, 3.0, 3.0, 3.0, -3.0, 15.0, 3.0, 3.0,
|
||||
-3.0,
|
||||
];
|
||||
assert_eq!(&output[..32], &expected);
|
||||
for group in output[32..224].as_chunks::<8>().0 {
|
||||
assert_eq!(group, &[1.0, 1.0, 1.0, 1.0, 5.0, 1.0, 1.0, 1.0]);
|
||||
}
|
||||
assert_eq!(
|
||||
&output[248..],
|
||||
&[-31.0, -217.0, -403.0, -480.5, -31.0, -31.0, -31.0, -31.0]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sign_codes_signed_zero_and_nonfinite_scales_are_admitted() {
|
||||
let mut block = [0u8; BLOCK_BYTES];
|
||||
for code in 0..128u32 {
|
||||
let word = code | (code << 7) | (code << 14) | (code << 21);
|
||||
for chunk in block[66..].as_chunks_mut::<4>().0 {
|
||||
chunk.copy_from_slice(&word.to_le_bytes());
|
||||
}
|
||||
block[..2].copy_from_slice(&0x3c00u16.to_le_bytes());
|
||||
let expected_signs = code as u8 | (((code.count_ones() & 1) as u8) << 7);
|
||||
for (element, value) in decode_block(&block).unwrap().into_iter().enumerate() {
|
||||
assert_eq!(
|
||||
value,
|
||||
if expected_signs & (1 << (element % 8)) != 0 {
|
||||
-1.0
|
||||
} else {
|
||||
1.0
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
for chunk in block[66..].as_chunks_mut::<4>().0 {
|
||||
chunk.copy_from_slice(&(1u32 | (1 << 7) | (1 << 14) | (1 << 21)).to_le_bytes());
|
||||
}
|
||||
for scale in [
|
||||
0x0000u16, 0x8000, 0x7c00, 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();
|
||||
for (element, value) in output.into_iter().enumerate() {
|
||||
let negative = (scale & 0x8000 != 0) ^ matches!(element % 8, 0 | 7);
|
||||
if scale & 0x7c00 == 0x7c00 {
|
||||
if scale & 0x03ff != 0 {
|
||||
assert!(value.is_nan());
|
||||
} else {
|
||||
assert!(value.is_infinite());
|
||||
assert_eq!(value.is_sign_negative(), negative);
|
||||
}
|
||||
} else {
|
||||
assert_eq!(value.to_bits(), if negative { 0x8000_0000 } else { 0 });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unaligned_bulk_and_bad_shapes_preserve_output_contract() {
|
||||
let mut block = [0u8; BLOCK_BYTES];
|
||||
block[..2].copy_from_slice(&0x3c00u16.to_le_bytes());
|
||||
let mut storage = vec![0u8; BLOCK_BYTES * 2 + 16];
|
||||
let start = storage.as_ptr().align_offset(16) + 1;
|
||||
let source = &mut storage[start..start + BLOCK_BYTES * 2];
|
||||
assert_eq!(source.as_ptr().align_offset(16), 15);
|
||||
source[..BLOCK_BYTES].copy_from_slice(&block);
|
||||
source[BLOCK_BYTES..].copy_from_slice(&block);
|
||||
let mut output = [123.0; BLOCK_ELEMENTS * 2];
|
||||
decode_into(source, &mut output).unwrap();
|
||||
assert_eq!(output, [1.0; BLOCK_ELEMENTS * 2]);
|
||||
for len in [0, 1, BLOCK_BYTES - 1, BLOCK_BYTES + 1, source.len() - 1] {
|
||||
output.fill(123.0);
|
||||
assert!(decode_into(&source[..len], &mut output).is_err());
|
||||
assert_eq!(output, [123.0; BLOCK_ELEMENTS * 2]);
|
||||
}
|
||||
for len in [
|
||||
0,
|
||||
BLOCK_ELEMENTS - 1,
|
||||
BLOCK_ELEMENTS + 1,
|
||||
BLOCK_ELEMENTS * 2,
|
||||
] {
|
||||
output.fill(123.0);
|
||||
assert!(decode_into(&source[..BLOCK_BYTES], &mut output[..len]).is_err());
|
||||
assert_eq!(output, [123.0; BLOCK_ELEMENTS * 2]);
|
||||
}
|
||||
decode_into(&[], &mut []).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// iq3xxs_grid from llama.cpp 3cf03257f219afbe7334045ff7c6a06ac68c627d ggml-common.h.
|
||||
// Copyright (c) 2023-2026 The ggml authors. MIT License.
|
||||
// See LICENSE-GGML in this crate for the full notice.
|
||||
// SHA256 of exact little-endian entry bytes: 46e35f5a997efdee6c99ce57854c8a0d4f0ff8ca57e5e8a60c0793ea580acf5d.
|
||||
// Pinned source import; do not regenerate or replace silently.
|
||||
|
||||
pub(crate) const IQ3XXS_GRID: [u32; 256] = [
|
||||
0x04040404, 0x04040414, 0x04040424, 0x04040c0c, 0x04040c1c, 0x04040c3e, 0x04041404, 0x04041414,
|
||||
0x04041c0c, 0x04042414, 0x04043e1c, 0x04043e2c, 0x040c040c, 0x040c041c, 0x040c0c04, 0x040c0c14,
|
||||
0x040c140c, 0x040c142c, 0x040c1c04, 0x040c1c14, 0x040c240c, 0x040c2c24, 0x040c3e04, 0x04140404,
|
||||
0x04140414, 0x04140424, 0x04140c0c, 0x04141404, 0x04141414, 0x04141c0c, 0x04141c1c, 0x04141c3e,
|
||||
0x04142c0c, 0x04142c3e, 0x04143e2c, 0x041c040c, 0x041c043e, 0x041c0c04, 0x041c0c14, 0x041c142c,
|
||||
0x041c3e04, 0x04240c1c, 0x04241c3e, 0x04242424, 0x04242c3e, 0x04243e1c, 0x04243e2c, 0x042c040c,
|
||||
0x042c043e, 0x042c1c14, 0x042c2c14, 0x04341c2c, 0x04343424, 0x043e0c04, 0x043e0c24, 0x043e0c34,
|
||||
0x043e241c, 0x043e340c, 0x0c04040c, 0x0c04041c, 0x0c040c04, 0x0c040c14, 0x0c04140c, 0x0c04141c,
|
||||
0x0c041c04, 0x0c041c14, 0x0c041c24, 0x0c04243e, 0x0c042c04, 0x0c0c0404, 0x0c0c0414, 0x0c0c0c0c,
|
||||
0x0c0c1404, 0x0c0c1414, 0x0c14040c, 0x0c14041c, 0x0c140c04, 0x0c140c14, 0x0c14140c, 0x0c141c04,
|
||||
0x0c143e14, 0x0c1c0404, 0x0c1c0414, 0x0c1c1404, 0x0c1c1c0c, 0x0c1c2434, 0x0c1c3434, 0x0c24040c,
|
||||
0x0c24042c, 0x0c242c04, 0x0c2c1404, 0x0c2c1424, 0x0c2c2434, 0x0c2c3e0c, 0x0c34042c, 0x0c3e1414,
|
||||
0x0c3e2404, 0x14040404, 0x14040414, 0x14040c0c, 0x14040c1c, 0x14041404, 0x14041414, 0x14041434,
|
||||
0x14041c0c, 0x14042414, 0x140c040c, 0x140c041c, 0x140c042c, 0x140c0c04, 0x140c0c14, 0x140c140c,
|
||||
0x140c1c04, 0x140c341c, 0x140c343e, 0x140c3e04, 0x14140404, 0x14140414, 0x14140c0c, 0x14140c3e,
|
||||
0x14141404, 0x14141414, 0x14141c3e, 0x14142404, 0x14142c2c, 0x141c040c, 0x141c0c04, 0x141c0c24,
|
||||
0x141c3e04, 0x141c3e24, 0x14241c2c, 0x14242c1c, 0x142c041c, 0x142c143e, 0x142c240c, 0x142c3e24,
|
||||
0x143e040c, 0x143e041c, 0x143e0c34, 0x143e242c, 0x1c04040c, 0x1c040c04, 0x1c040c14, 0x1c04140c,
|
||||
0x1c04141c, 0x1c042c04, 0x1c04342c, 0x1c043e14, 0x1c0c0404, 0x1c0c0414, 0x1c0c1404, 0x1c0c1c0c,
|
||||
0x1c0c2424, 0x1c0c2434, 0x1c14040c, 0x1c14041c, 0x1c140c04, 0x1c14142c, 0x1c142c14, 0x1c143e14,
|
||||
0x1c1c0c0c, 0x1c1c1c1c, 0x1c241c04, 0x1c24243e, 0x1c243e14, 0x1c2c0404, 0x1c2c0434, 0x1c2c1414,
|
||||
0x1c2c2c2c, 0x1c340c24, 0x1c341c34, 0x1c34341c, 0x1c3e1c1c, 0x1c3e3404, 0x24040424, 0x24040c3e,
|
||||
0x24041c2c, 0x24041c3e, 0x24042c1c, 0x24042c3e, 0x240c3e24, 0x24141404, 0x24141c3e, 0x24142404,
|
||||
0x24143404, 0x24143434, 0x241c043e, 0x241c242c, 0x24240424, 0x24242c0c, 0x24243424, 0x242c142c,
|
||||
0x242c241c, 0x242c3e04, 0x243e042c, 0x243e0c04, 0x243e0c14, 0x243e1c04, 0x2c040c14, 0x2c04240c,
|
||||
0x2c043e04, 0x2c0c0404, 0x2c0c0434, 0x2c0c1434, 0x2c0c2c2c, 0x2c140c24, 0x2c141c14, 0x2c143e14,
|
||||
0x2c1c0414, 0x2c1c2c1c, 0x2c240c04, 0x2c24141c, 0x2c24143e, 0x2c243e14, 0x2c2c0414, 0x2c2c1c0c,
|
||||
0x2c342c04, 0x2c3e1424, 0x2c3e2414, 0x34041424, 0x34042424, 0x34042434, 0x34043424, 0x340c140c,
|
||||
0x340c340c, 0x34140c3e, 0x34143424, 0x341c1c04, 0x341c1c34, 0x34242424, 0x342c042c, 0x342c2c14,
|
||||
0x34341c1c, 0x343e041c, 0x343e140c, 0x3e04041c, 0x3e04042c, 0x3e04043e, 0x3e040c04, 0x3e041c14,
|
||||
0x3e042c14, 0x3e0c1434, 0x3e0c2404, 0x3e140c14, 0x3e14242c, 0x3e142c14, 0x3e1c0404, 0x3e1c0c2c,
|
||||
0x3e1c1c1c, 0x3e1c3404, 0x3e24140c, 0x3e24240c, 0x3e2c0404, 0x3e2c0414, 0x3e2c1424, 0x3e341c04,
|
||||
];
|
||||
|
||||
// ksigns_iq2xs from llama.cpp 3cf03257f219afbe7334045ff7c6a06ac68c627d ggml-common.h.
|
||||
// Copyright (c) 2023-2026 The ggml authors. MIT License.
|
||||
// See LICENSE-GGML in this crate for the full notice.
|
||||
// SHA256 of exact entry bytes: a76742a603f8beca5212ecce0f1f02f11a4887fd2f7c8b15aca0ea3eb3380c31.
|
||||
// Pinned source import; do not regenerate or replace silently.
|
||||
|
||||
pub(crate) const KSIGNS_IQ2XS: [u8; 128] = [
|
||||
0x00, 0x81, 0x82, 0x03, 0x84, 0x05, 0x06, 0x87, 0x88, 0x09, 0x0a, 0x8b, 0x0c, 0x8d, 0x8e, 0x0f,
|
||||
0x90, 0x11, 0x12, 0x93, 0x14, 0x95, 0x96, 0x17, 0x18, 0x99, 0x9a, 0x1b, 0x9c, 0x1d, 0x1e, 0x9f,
|
||||
0xa0, 0x21, 0x22, 0xa3, 0x24, 0xa5, 0xa6, 0x27, 0x28, 0xa9, 0xaa, 0x2b, 0xac, 0x2d, 0x2e, 0xaf,
|
||||
0x30, 0xb1, 0xb2, 0x33, 0xb4, 0x35, 0x36, 0xb7, 0xb8, 0x39, 0x3a, 0xbb, 0x3c, 0xbd, 0xbe, 0x3f,
|
||||
0xc0, 0x41, 0x42, 0xc3, 0x44, 0xc5, 0xc6, 0x47, 0x48, 0xc9, 0xca, 0x4b, 0xcc, 0x4d, 0x4e, 0xcf,
|
||||
0x50, 0xd1, 0xd2, 0x53, 0xd4, 0x55, 0x56, 0xd7, 0xd8, 0x59, 0x5a, 0xdb, 0x5c, 0xdd, 0xde, 0x5f,
|
||||
0x60, 0xe1, 0xe2, 0x63, 0xe4, 0x65, 0x66, 0xe7, 0xe8, 0x69, 0x6a, 0xeb, 0x6c, 0xed, 0xee, 0x6f,
|
||||
0xf0, 0x71, 0x72, 0xf3, 0x74, 0xf5, 0xf6, 0x77, 0x78, 0xf9, 0xfa, 0x7b, 0xfc, 0x7d, 0x7e, 0xff,
|
||||
];
|
||||
@@ -1,8 +1,12 @@
|
||||
//! Pure Rust CPU quantization primitives for Leleka preparation tools.
|
||||
//! No inference, GPU operation or complete model support is implied.
|
||||
|
||||
pub mod iq2_s;
|
||||
mod iq2s_grid;
|
||||
pub mod iq3_s;
|
||||
pub mod iq3_xxs;
|
||||
mod iq3s_grid;
|
||||
mod iq3xxs_grid;
|
||||
pub mod iq4_nl;
|
||||
pub mod iq4_xs;
|
||||
mod k_scale_min;
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
# IQ2_S / IQ3_XXS CPU reference parity
|
||||
|
||||
GGUF kind 22 (IQ2_S) and kind 18 (IQ3_XXS) preparation decoding now have
|
||||
explicit Rust routes. This is a bounded quantization port, not a complete
|
||||
independent inference engine or a model/GPU qualification. No model, request,
|
||||
user-agent data or server is used by these synthetic CPU tests.
|
||||
|
||||
## Pinned layout and provenance
|
||||
|
||||
Actual oracle values come from unchanged vendored GGML C at llama.cpp
|
||||
`3cf03257f219afbe7334045ff7c6a06ac68c627d`, also pinned by imported Strata
|
||||
`d9ab8435f654c368c586340d490915f6addf56a3`. Original MIT notice and upstream
|
||||
records remain [in the crate](../crates/quant-core/LICENSE-GGML) and
|
||||
[in the GGML provenance](../control-plane/third_party/ggml/UPSTREAM.md).
|
||||
The production C bridge, compiler arithmetic flags, prior 24 oracle tests
|
||||
and 13 visitor test functions remain byte-preserved.
|
||||
|
||||
IQ2_S: 82 bytes / 256 outputs. FP16 LE header at 0–1, 32 grid low bytes at
|
||||
2–33, 32 raw sign masks at 34–65, eight packed two-bit grid-high bytes at
|
||||
66–73, eight two-nibble scales at 74–81. Each eight-output group addresses
|
||||
one 1024-entry eight-byte grid; two groups share each subscale.
|
||||
Arithmetic preserves `(d * (0.5 + nibble)) * 0.25`, then magnitude multiply,
|
||||
then sign multiply, without changing evaluation order.
|
||||
|
||||
IQ3_XXS: 98 bytes / 256 outputs. FP16 header at 0–1; 64 byte-sized four-value
|
||||
grid indexes at 2–65; eight little-endian scale/sign u32 words at 66–97.
|
||||
Each word has four seven-bit sign-table indexes and one high four-bit scale.
|
||||
Two grids supply the first/second four values of each eight-output group.
|
||||
Arithmetic preserves `(d * (0.5 + nibble)) * 0.5`, magnitude, then sign.
|
||||
|
||||
An independent test parses only each table declaration from pinned C, checks
|
||||
its count, serializes little-endian values and hashes the entire byte stream.
|
||||
These hashes match the independent Rust table provenance gates:
|
||||
|
||||
| Table | Bytes | SHA256 |
|
||||
|---|---:|---|
|
||||
| IQ2_S grid | 8192 | `e1aa1473412b0552c2174c30ef22ab4073f6a181b85a17056e8249bd2932fd88` |
|
||||
| IQ3_XXS grid | 1024 | `46e35f5a997efdee6c99ce57854c8a0d4f0ff8ca57e5e8a60c0793ea580acf5d` |
|
||||
| Sign table | 128 | `a76742a603f8beca5212ecce0f1f02f11a4887fd2f7c8b15aca0ea3eb3380c31` |
|
||||
| Bit masks | 8 | `5ac9831b2e30eb285ef34f8501620f878432d5c04331ad1ae47f977a83ba41a5` |
|
||||
|
||||
All FP16 encodings are admitted as before. NaN classification must match;
|
||||
every other output, including signed zero and infinity signs, matches exact
|
||||
bits. Neither grid contains zero magnitudes and `(0.5 + nibble)` is positive:
|
||||
zero packed fields are valid, but are not a zero multiplicative factor for an
|
||||
infinite header in these formats. Signed-zero headers are explicitly tested.
|
||||
NaN payload/sign equivalence is not claimed.
|
||||
|
||||
## Independent coverage and counts
|
||||
|
||||
Synthetic wire packing, parsed C sign coverage and raw-field bookkeeping do
|
||||
not call Rust decoder helpers. Every expected numerical value is actual C output.
|
||||
|
||||
- All **63,488 finite FP16 headers** for both formats: **32,505,856** comparisons.
|
||||
- IQ2_S **1024** independent patterns require every grid index at all 32
|
||||
eight-value positions, all 256 raw sign masks at each position, both output
|
||||
signs at every lane, and all 16 subscale nibbles at every one of 16 positions:
|
||||
**262,144** comparisons.
|
||||
- IQ3_XXS **2048** patterns require every grid index at all 64 four-value
|
||||
positions, all 128 sign codes at all 32 groups, both signs at every output
|
||||
lane and all 16 scale nibbles at all eight positions: **524,288** comparisons.
|
||||
- Raw-byte bijections require every byte value at all 80/96 packed-field
|
||||
positions respectively: **131,072** comparisons.
|
||||
- Sixteen finite boundary blocks per format through aligned and deliberately
|
||||
unaligned bulk inputs: **16,384** comparisons. Empty/truncated input and
|
||||
short/long output reject without changing Rust/C sentinel output.
|
||||
- All **2048 nonfinite FP16 headers** per format with generated and all-zero
|
||||
packed fields preserve NaN class and every non-NaN bit pattern.
|
||||
|
||||
Total finite comparisons: **33,439,744**, zero tolerance. This covers separate
|
||||
header/grid/sign/subscale domains and every field position; it does not exhaust
|
||||
the full joint header × grid × sign × subscale space or arbitrary packed data.
|
||||
|
||||
## Bounded visitor
|
||||
|
||||
Only kinds 18/22 gain Rust branches, with geometry agreement checked before
|
||||
seek. Source cap and other routes are unchanged. Synthetic tensors cross two
|
||||
callbacks, checking exact C bytes, offsets/slices, bounded buffers, receipts,
|
||||
no seek/callback for malformed shapes, no partial callback on early/late
|
||||
truncation and no later read after first/second callback failure. Nonfinite,
|
||||
signed-zero and zero-field fixtures preserve the pinned C value policy.
|
||||
|
||||
```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 -- --test-threads=2
|
||||
cargo test --locked -p strata-control --lib gguf_f32_stream::tests
|
||||
```
|
||||
|
||||
## Actual results
|
||||
|
||||
Local CPU gates, 2026-10-07, Darwin arm64; Rust 1.99.0
|
||||
(`b940084d7 2026-09-28`), Apple Clang 21.0.0 (`clang-2100.1.1.101`).
|
||||
C arithmetic flags and zero tolerance unchanged.
|
||||
|
||||
- Standalone quant-core: **36 passed, 0 failed** (eight new tests), all-target
|
||||
Clippy with warnings denied, owned formatting/whitespace and dependency tree
|
||||
passed. Every Rust grid/sign entry was independently compared with pinned C
|
||||
and whole-table canonical byte hashes matched. The one owned private target
|
||||
was cleaned (385 files, 19.3 MiB) and its directory removed.
|
||||
- Actual C reference suite: **30 passed, 0 failed**, including six new tests
|
||||
and all 24 prior functions byte-preserved. All **33,439,744** new finite
|
||||
exact-bit comparisons passed with zero mismatch; every grid, sign, scale and
|
||||
raw-field position coverage assertion passed, as did independent complete C
|
||||
table hashes, all nonfinite encodings, zero-field, bulk and shape gates.
|
||||
The full suite took 9.46 seconds; this is test runtime, not model throughput.
|
||||
- Visitor: **15 passed, 0 failed**, including two new tests and 13 prior
|
||||
functions byte-preserved. IQ2_S raw chunks were **28,126 / 1,394 bytes**, F32
|
||||
**351,232 / 17,408 bytes**. IQ3_XXS raw chunks were **28,126 / 1,666 bytes**,
|
||||
F32 **293,888 / 17,408 bytes**. Bounds, source offsets, receipts, no-seek
|
||||
malformed-shape behavior, early/late truncation, both callback-abort positions
|
||||
and nonfinite/signed-zero/zero-field gates passed.
|
||||
- Control-plane library/tests Clippy with warnings denied, full formatting,
|
||||
diff check and all ten vendored C manifest hashes passed. The normal quant
|
||||
dependency tree contains only the independent crate. The coordinated shared
|
||||
control-plane target cache was retained; no private control-plane target or
|
||||
experimental artifact was created.
|
||||
|
||||
No numeric mismatch, tolerance change or quality-gate weakening was observed.
|
||||
|
||||
Linux/compiler qualification, real model/GPU inference, model quality and paired
|
||||
performance are separate. No end-to-end speed or inference-readiness claim.
|
||||
Reference in New Issue
Block a user