scripts/offline/diff_dequant.py (live, 32 rows x 3 seeds through export-tensor-f32) and a committed golden with an integration test over the production visitor. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
63 lines
2.4 KiB
Rust
63 lines
2.4 KiB
Rust
//! Dequantization versus the pinned gguf-py (`quants.dequantize`, llama.cpp 3cf03257) and `tools/iq_fixture.py`.
|
|
//!
|
|
//! `tests/fixtures/offline/dequant_oracle.json` holds one seeded 256-value row of each of 23 block types and the
|
|
//! oracle's float32 bits (`scripts/offline/gen_goldens.py dequant_oracle`; `scripts/offline/diff_dequant.py` runs
|
|
//! 32 rows x 3 seeds live). The production visitor (`gguf_f32_stream::visit`: independent Rust decoders where they
|
|
//! exist, the pinned ggml C bridge otherwise) must reproduce every float bit for bit. Q8_K has no gguf-py
|
|
//! dequantizer; its reference is `d * qs`.
|
|
use serde_json::Value;
|
|
use std::io::Cursor;
|
|
use strata_control::{gguf, gguf_f32_stream};
|
|
|
|
fn unhex(text: &str) -> Vec<u8> {
|
|
(0..text.len() / 2)
|
|
.map(|i| u8::from_str_radix(&text[i * 2..i * 2 + 2], 16).unwrap())
|
|
.collect()
|
|
}
|
|
|
|
#[test]
|
|
fn every_block_type_decodes_bit_exactly_like_gguf_py() {
|
|
let golden: Value =
|
|
serde_json::from_str(include_str!("fixtures/offline/dequant_oracle.json")).unwrap();
|
|
let types = golden["types"].as_array().unwrap();
|
|
assert_eq!(types.len(), 23);
|
|
for entry in types {
|
|
let name = entry["name"].as_str().unwrap();
|
|
let kind = entry["kind"].as_u64().unwrap() as u32;
|
|
let blocks = unhex(entry["blocks_hex"].as_str().unwrap());
|
|
let want = unhex(entry["f32_hex"].as_str().unwrap());
|
|
let (elements, bytes) =
|
|
gguf::block_geometry(kind).unwrap_or_else(|| panic!("{name}: geometry"));
|
|
assert_eq!(
|
|
blocks.len() as u64,
|
|
golden["elements"].as_u64().unwrap() / elements * bytes,
|
|
"{name}"
|
|
);
|
|
let mut got = Vec::new();
|
|
gguf_f32_stream::visit(
|
|
&mut Cursor::new(blocks.clone()),
|
|
0,
|
|
kind,
|
|
blocks.len() as u64,
|
|
|raw, f32| {
|
|
assert_eq!(raw.len() as u64 / bytes * elements, f32.len() as u64 / 4);
|
|
got.extend_from_slice(f32);
|
|
Ok(())
|
|
},
|
|
)
|
|
.unwrap_or_else(|e| panic!("{name}: {e}"));
|
|
let differing = got
|
|
.as_chunks::<4>()
|
|
.0
|
|
.iter()
|
|
.zip(want.as_chunks::<4>().0)
|
|
.filter(|(a, b)| a != b)
|
|
.count();
|
|
assert_eq!(
|
|
(got.len(), differing),
|
|
(want.len(), 0),
|
|
"{name} (kind {kind})"
|
|
);
|
|
}
|
|
}
|