66 lines
2.1 KiB
Rust
66 lines
2.1 KiB
Rust
//! Decode the first real block of each quantized GGUF kind in one source shard.
|
|
//! This is an offline diagnostic; input weights remain outside Git.
|
|
|
|
use std::{collections::BTreeMap, env, fs::File, path::Path};
|
|
|
|
use sha2::{Digest, Sha256};
|
|
use leleka_control_plane::{gguf, gguf_f32_stream};
|
|
|
|
type Failure = Box<dyn std::error::Error>;
|
|
|
|
fn all_finite(values: &[u8]) -> bool {
|
|
values
|
|
.as_chunks::<4>()
|
|
.0
|
|
.iter()
|
|
.all(|word| f32::from_le_bytes(*word).is_finite())
|
|
}
|
|
|
|
/// Decodes the first block of `tensor` and prints its element count and the SHA-256 of the decoded values.
|
|
fn scan_kind(
|
|
source: &mut File,
|
|
data_start: u64,
|
|
(kind, tensor): (u32, &gguf::TensorInfo),
|
|
) -> Result<(), Failure> {
|
|
let (expected_elements, bytes) =
|
|
gguf::block_geometry(kind).ok_or_else(|| format!("unsupported source kind {kind}"))?;
|
|
let absolute = data_start
|
|
.checked_add(tensor.offset)
|
|
.ok_or("GGUF tensor offset overflow")?;
|
|
let mut hasher = Sha256::new();
|
|
let decoded_bytes = gguf_f32_stream::visit(source, absolute, kind, bytes, |_, values| {
|
|
if !all_finite(values) {
|
|
return Err(format!("{}: non-finite decoded value", tensor.name));
|
|
}
|
|
hasher.update(values);
|
|
Ok(())
|
|
})?;
|
|
if decoded_bytes != expected_elements * 4 {
|
|
return Err(format!("{}: decoded block length mismatch", tensor.name).into());
|
|
}
|
|
println!(
|
|
"kind {kind}: {} values from {} SHA-256 {}",
|
|
expected_elements,
|
|
tensor.name,
|
|
hex::encode(hasher.finalize())
|
|
);
|
|
Ok(())
|
|
}
|
|
|
|
fn main() -> Result<(), Failure> {
|
|
let path = env::args()
|
|
.nth(1)
|
|
.ok_or("usage: scan-quant-blocks FILE.gguf")?;
|
|
let path = Path::new(&path);
|
|
let index = gguf::tensor_index(path)?;
|
|
let mut first_by_kind = BTreeMap::new();
|
|
for tensor in &index.tensors {
|
|
first_by_kind.entry(tensor.kind).or_insert(tensor);
|
|
}
|
|
let mut source = File::open(path)?;
|
|
for (kind, tensor) in first_by_kind {
|
|
scan_kind(&mut source, index.summary.data_start, (kind, tensor))?;
|
|
}
|
|
Ok(())
|
|
}
|