Files
leleka/control-plane/tests/gguf_header_differential.rs
T

108 lines
4.0 KiB
Rust

//! GGUF header reader versus `engine/strata/tools/gguf_reader.py`.
//!
//! `tests/fixtures/offline/gguf_headers.json` is generated by `scripts/offline/gen_goldens.py` from the unmodified
//! Python reader: random GGUF v3 headers (all 13 metadata value types, arrays, unicode, alignment, split keys,
//! 1-4 dimensional tensors of every block geometry) with the metadata and tensor directory it parses from each, and
//! its `BLOCK_GEOMETRY` table. The live differential over thousands of files is
//! `scripts/offline/diff_gguf_header.py`.
use serde_json::Value;
use std::{fs, path::PathBuf};
use leleka_control_plane::{gguf, gguf_json};
fn golden() -> Value {
serde_json::from_str(include_str!("fixtures/offline/gguf_headers.json")).unwrap()
}
fn decode_hex(text: &str) -> Vec<u8> {
(0..text.len() / 2)
.map(|i| u8::from_str_radix(&text[i * 2..i * 2 + 2], 16).unwrap())
.collect()
}
fn scratch(name: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("gguf-diff-{}-{name}", std::process::id()));
fs::create_dir_all(&dir).unwrap();
dir
}
/// Writes the case's header plus zero payload as a GGUF file.
fn write_case(dir: &std::path::Path, number: usize, case: &Value) -> PathBuf {
let mut bytes = decode_hex(case["header_hex"].as_str().unwrap());
bytes.resize(
bytes.len() + case["payload_bytes"].as_u64().unwrap() as usize,
0,
);
let path = dir.join(format!("case{number}.gguf"));
fs::write(&path, bytes).unwrap();
path
}
fn assert_tensor_matches(got: &gguf::TensorInfo, want: &Value, data_start: u64, number: usize) {
assert_eq!(got.name, want["name"].as_str().unwrap());
assert_eq!(
serde_json::to_value(&got.dimensions).unwrap(),
want["dimensions"],
"case {number} {}",
got.name
);
assert_eq!(u64::from(got.kind), want["type"].as_u64().unwrap());
assert_eq!(data_start + got.offset, want["offset"].as_u64().unwrap());
assert_eq!(got.bytes, want["bytes"].as_u64().unwrap(), "{}", got.name);
}
#[test]
fn headers_parse_like_the_python_reader() {
let golden = golden();
let dir = scratch("headers");
let cases = golden["cases"].as_array().unwrap();
assert!(cases.len() >= 20);
for (number, case) in cases.iter().enumerate() {
let path = write_case(&dir, number, case);
let index = gguf::tensor_index(&path).unwrap_or_else(|e| panic!("case {number}: {e}"));
let data_start = index.summary.data_start;
assert_eq!(
data_start,
case["data_start"].as_u64().unwrap(),
"case {number}"
);
let wanted = case["tensors"].as_array().unwrap();
assert_eq!(index.tensors.len(), wanted.len(), "case {number}");
for (got, want) in index.tensors.iter().zip(wanted) {
assert_tensor_matches(got, want, data_start, number);
}
let metadata = gguf_json::metadata_json(gguf::metadata_entries(&path).unwrap());
assert_eq!(metadata, case["metadata"], "case {number}");
}
let _ = fs::remove_dir_all(dir);
}
/// The Rust byte geometry covers every kind `BLOCK_GEOMETRY` does (including Q8_K, IQ1_S, F64, MXFP4, NVFP4)
/// with identical (elements, bytes); the one extra kind is I8 (24), the engine's FP8 PLE table form that
/// `gguf_reader.hpp` has and the Python table lacks.
#[test]
fn block_geometry_matches_the_python_table() {
let golden = golden();
let table = golden["geometry"].as_object().unwrap();
for (id, row) in table {
let kind: u32 = id.parse().unwrap();
assert_eq!(
gguf::block_geometry(kind),
Some((
row["elements"].as_u64().unwrap(),
row["bytes"].as_u64().unwrap()
)),
"kind {kind} {}",
row["name"]
);
}
for kind in 0..64u32 {
if !table.contains_key(&kind.to_string()) {
assert_eq!(
gguf::block_geometry(kind).is_some(),
kind == 24,
"kind {kind} must be absent from the Rust table unless it is I8"
);
}
}
}