Files
leleka/control-plane/examples/compare-kar-range.rs
T
Pavlo Ratushnyi 4013e0c9bd
Desktop foundation / core (ubuntu-latest) (push) Failing after 28s
Desktop foundation / core (macos-latest) (push) Canceled after 0s
Desktop foundation / core (windows-latest) (push) Canceled after 0s
Linux / verify (push) Canceled after 0s
Linux / keyless-cuda (push) Canceled after 0s
Linux / static-cuda-package (push) Canceled after 0s
Linux / hosted-cuda-package (push) Canceled after 0s
Linux / linux-static-cli (push) Canceled after 0s
Linux / linux-cli-container (push) Canceled after 0s
Linux / release-cli (push) Canceled after 0s
Import tracked Strata Next source snapshot
2026-10-05 18:15:08 +02:00

90 lines
3.5 KiB
Rust

//! Independently decode a bounded KAR range and compare it with GGUF values.
//! The one-tensor GGUF and KAR inputs stay outside Git. This is a CPU diagnostic.
use std::{
env,
fs::File,
io::{Read, Seek, SeekFrom},
path::Path,
};
use strata_control::{ggml_bridge, gguf, kar};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut args = env::args().skip(1);
let usage = "usage: compare-kar-range FILE.gguf FILE.kar LEVEL START COUNT";
let source_path = args.next().ok_or(usage)?;
let archive_path = args.next().ok_or(usage)?;
let level: u8 = args.next().ok_or(usage)?.parse()?;
let start: usize = args.next().ok_or(usage)?.parse()?;
let count: usize = args.next().ok_or(usage)?.parse()?;
if args.next().is_some() || count == 0 || count > 1_000_000 {
return Err(usage.into());
}
let source_path = Path::new(&source_path);
let archive_path = Path::new(&archive_path);
let index = gguf::tensor_index(source_path)?;
if index.tensors.len() != 1 {
return Err("diagnostic source must contain exactly one tensor".into());
}
let tensor = &index.tensors[0];
let (block_elements, block_bytes) = match tensor.kind {
20 => (32usize, 18usize),
21 => (256, 110),
_ => return Err("diagnostic supports IQ4_NL and IQ3_S sources".into()),
};
let end = start.checked_add(count).ok_or("GGUF range overflow")?;
let elements = tensor
.dimensions
.iter()
.try_fold(1usize, |n, d| n.checked_mul(usize::try_from(*d).ok()?))
.ok_or("GGUF element count overflow")?;
if end > elements {
return Err("GGUF range exceeds tensor".into());
}
let reconstructed = kar::decode_tensor_range(archive_path, &tensor.name, level, start, count)?;
let first_block = start / block_elements;
let final_block = end.div_ceil(block_elements);
let first_block_bytes = first_block
.checked_mul(block_bytes)
.and_then(|n| u64::try_from(n).ok())
.ok_or("GGUF offset overflow")?;
let absolute = index
.summary
.data_start
.checked_add(tensor.offset)
.and_then(|n| n.checked_add(first_block_bytes))
.ok_or("GGUF offset overflow")?;
let mut source = File::open(source_path)?;
source.seek(SeekFrom::Start(absolute))?;
let mut block = vec![0u8; block_bytes];
let mut decoded = vec![0.0f32; block_elements];
let mut error_sum = 0.0f64;
let mut reference_sum = 0.0f64;
let mut maximum_absolute_error = 0.0f64;
for number in first_block..final_block {
source.read_exact(&mut block)?;
ggml_bridge::decode_into(tensor.kind, &block, &mut decoded)?;
let begin = start.max(number * block_elements);
let finish = end.min((number + 1) * block_elements);
for position in begin..finish {
let reference = f64::from(decoded[position - number * block_elements]);
let value = f64::from(reconstructed[position - start]);
let error = (reference - value).abs();
error_sum += error * error;
reference_sum += reference * reference;
maximum_absolute_error = maximum_absolute_error.max(error);
}
}
let relative_l2 = if reference_sum == 0.0 {
error_sum.sqrt()
} else {
(error_sum / reference_sum).sqrt()
};
println!(
"{}: {count} archived F32 values decoded at level {level}, start {start}; relative L2 {relative_l2:.8}, maximum absolute error {maximum_absolute_error:.8}",
tensor.name
);
Ok(())
}