Rust comments that named scripts/offline/*, gen_goldens.py, oracle/gen_*.{sh,cpp,py}, build_oracle.sh and similar retired generators now say where the data
came from (the Original's code, Strata 41cf05a0) or drop the sentence. The path/generator text of seven generated fixtures (sampler_oracle.json, sampling_keys.json,
memory_plan_golden.json, rope_golden.json, cvec_oracle.json, stra161 fixtures.json, commentary-tool-prompt.json) is reworded; no test reads those fields and no pin
covers their bytes. cmd_time/doc.rs: the two path texts in the format string; its lines are re-broken so they stay within 120 columns (the string is unchanged
otherwise). Comment-only except those data/string spots; no behaviour change.
145 lines
5.3 KiB
Rust
145 lines
5.3 KiB
Rust
//! Golden tests: the Rust references against fixtures produced by the Original's own host code
|
|
//! (the reference functions are extracted verbatim from Strata 41cf05a0).
|
|
|
|
use leleka_experimental_ref::{
|
|
fwht, kv_q8,
|
|
layout::{self, KvFormat, QsaShapes, StreamFormat},
|
|
q4_0::{self, BlockQ4_0, Contraction},
|
|
};
|
|
use serde_json::Value;
|
|
|
|
fn load(name: &str) -> Value {
|
|
let path = format!("{}/tests/fixtures/{name}", env!("CARGO_MANIFEST_DIR"));
|
|
serde_json::from_str(&std::fs::read_to_string(path).expect("fixture")).expect("json")
|
|
}
|
|
|
|
fn floats<const N: usize>(v: &Value) -> [f32; N] {
|
|
let a = v.as_array().expect("array");
|
|
assert_eq!(a.len(), N);
|
|
let mut out = [0.0; N];
|
|
for (o, x) in out.iter_mut().zip(a) {
|
|
*o = f32::from_bits(x.as_u64().expect("bits") as u32);
|
|
}
|
|
out
|
|
}
|
|
|
|
fn hex(s: &str) -> Vec<u8> {
|
|
(0..s.len()).step_by(2).map(|i| u8::from_str_radix(&s[i..i + 2], 16).unwrap()).collect()
|
|
}
|
|
|
|
/// Two elements of the largest magnitude with opposite sign: the Original's host reference and its CUDA
|
|
/// kernel resolve the tie differently (documented in `q4_0`).
|
|
fn has_opposite_sign_tie(x: &[f32; 32]) -> bool {
|
|
let amax = x.iter().fold(0.0f32, |m, v| m.max(v.abs()));
|
|
x.contains(&amax) && x.iter().any(|v| *v == -amax)
|
|
}
|
|
|
|
#[test]
|
|
fn fwht_rows_are_bit_identical_to_the_host_reference() {
|
|
let g = load("kv_golden_separate.json");
|
|
let rows = g["fwht"].as_array().unwrap();
|
|
assert_eq!(rows.len(), 24);
|
|
for (i, row) in rows.iter().enumerate() {
|
|
let input: [f32; 256] = floats(&row["in"]);
|
|
let want: [f32; 256] = floats(&row["out"]);
|
|
let mut host = input;
|
|
fwht::fwht256_host_reference(&mut host);
|
|
assert_eq!(host.map(f32::to_bits), want.map(f32::to_bits), "host order row {i}");
|
|
let mut kernel = [0.0; 256];
|
|
fwht::fwht256(&input, &mut kernel);
|
|
assert_eq!(kernel.map(f32::to_bits), want.map(f32::to_bits), "kernel order row {i}");
|
|
assert_eq!(
|
|
fwht::fwht256_warp_model(&input).map(f32::to_bits),
|
|
want.map(f32::to_bits),
|
|
"warp model row {i}"
|
|
);
|
|
}
|
|
}
|
|
|
|
fn check_q4(fixture: &str, c: Contraction) -> (usize, usize) {
|
|
let g = load(fixture);
|
|
let groups = g["q4"].as_array().unwrap();
|
|
assert_eq!(groups.len(), 576);
|
|
let (mut kernel_compared, mut ties) = (0, 0);
|
|
for (i, e) in groups.iter().enumerate() {
|
|
let x: [f32; 32] = floats(&e["x"]);
|
|
let want = BlockQ4_0::from_bytes(&hex(e["block"].as_str().unwrap()));
|
|
assert_eq!(q4_0::quantize_group_host_reference_with(&x, c), want, "host reference, group {i}");
|
|
if has_opposite_sign_tie(&x) {
|
|
ties += 1;
|
|
continue;
|
|
}
|
|
assert_eq!(q4_0::quantize_group(&x, c), want, "kernel semantics, group {i}");
|
|
kernel_compared += 1;
|
|
}
|
|
(kernel_compared, ties)
|
|
}
|
|
|
|
#[test]
|
|
fn q4_0_blocks_match_the_host_reference_separate_operations() {
|
|
let (compared, ties) = check_q4("kv_golden_separate.json", Contraction::Separate);
|
|
assert!(compared > 350 && ties > 0, "{compared} {ties}");
|
|
}
|
|
|
|
#[test]
|
|
fn q4_0_blocks_match_a_fused_multiply_add_build() {
|
|
let (compared, _) = check_q4("kv_golden_fused.json", Contraction::Fused);
|
|
assert!(compared > 350);
|
|
// the fused build really differs from the separate one on the boundary groups
|
|
let s = load("kv_golden_separate.json");
|
|
let f = load("kv_golden_fused.json");
|
|
let differ = s["q4"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.zip(f["q4"].as_array().unwrap())
|
|
.filter(|(a, b)| a["block"] != b["block"])
|
|
.count();
|
|
assert!(differ > 40, "{differ}");
|
|
}
|
|
|
|
#[test]
|
|
fn int8_k_groups_match_the_hybrid_parity_host_reference() {
|
|
let g = load("kv_golden_separate.json");
|
|
let groups = g["q8"].as_array().unwrap();
|
|
assert_eq!(groups.len(), 96);
|
|
for (i, e) in groups.iter().enumerate() {
|
|
let x: [f32; 64] = floats(&e["x"]);
|
|
let (codes, scale) = kv_q8::quantize_group(&x);
|
|
assert_eq!(u64::from(scale), e["scale"].as_u64().unwrap(), "scale {i}");
|
|
let want: Vec<i8> = e["codes"].as_array().unwrap().iter().map(|c| c.as_i64().unwrap() as i8).collect();
|
|
assert_eq!(codes.to_vec(), want, "codes {i}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn layout_sizes_match_the_original_functions() {
|
|
let g = load("kv_golden_separate.json");
|
|
let s = QsaShapes::real();
|
|
assert_eq!(g["per_cell"]["q8"].as_u64(), Some(kv_q8::bytes_per_cell(2, 256)));
|
|
assert_eq!(g["per_cell"]["q4"].as_u64(), Some(q4_0::bytes_per_cell(2, 256)));
|
|
for (key, fmt) in [
|
|
("f16", StreamFormat::F16),
|
|
("int8", StreamFormat::Int8),
|
|
("q4", StreamFormat::Q4),
|
|
("value_int8", StreamFormat::ValueInt8),
|
|
] {
|
|
assert_eq!(g["block_bytes"][key].as_u64(), Some(layout::kv_block_bytes(&s, fmt)), "{key}");
|
|
}
|
|
let mut checked = 0;
|
|
for e in g["pool_bytes"].as_array().unwrap() {
|
|
let fmt = match e["format"].as_str().unwrap() {
|
|
"fp16" => KvFormat::Fp16,
|
|
"int8" => KvFormat::Int8,
|
|
"q4_0" => KvFormat::Q4_0,
|
|
"k8v4" => KvFormat::K8V4,
|
|
"int8-keyless" => KvFormat::Int8Keyless,
|
|
other => panic!("{other}"),
|
|
};
|
|
let pages = e["pages"].as_u64().unwrap();
|
|
assert_eq!(layout::kv_pool_bytes(fmt, &s, pages), e["bytes"].as_u64().unwrap(), "{fmt:?} {pages}");
|
|
checked += 1;
|
|
}
|
|
assert_eq!(checked, 25);
|
|
}
|