Files
leleka/crates/experimental-ref/tests/cvec_golden.rs
T
Pavlo Ratushnyi da8c974f68 Drop the stale engine/strata paths and retired generator names from comments, doc strings and generated fixtures (STRA-407)
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.
2026-11-23 22:37:17 +01:00

68 lines
2.6 KiB
Rust

//! Control-vector loading against `load_control_vectors` of `generate.cpp` (extracted verbatim, run on synthetic
//! control-vector GGUF files on the host).
mod common;
use common::{fixture, unhex};
use leleka_experimental_ref::cvec::{CvecMode, CvecOptions, load_control_vectors};
fn options_of(c: &serde_json::Value) -> CvecOptions {
let o = &c["options"];
CvecOptions {
first: o["first"].as_i64().unwrap(),
last: o["last"].as_i64().unwrap(),
mode: if o["mode"].as_i64() == Some(0) { CvecMode::Project } else { CvecMode::Add },
single: o["single"].as_i64().unwrap(),
}
}
fn files_of(c: &serde_json::Value) -> Vec<(String, Vec<u8>, f32)> {
c["files"]
.as_array()
.unwrap()
.iter()
.map(|f| {
(
f["label"].as_str().unwrap().to_string(),
unhex(f["bytes"].as_str().unwrap()),
f32::from_bits(f["scale"].as_u64().unwrap() as u32),
)
})
.collect()
}
/// A success case: the tables equal the original loader's, bit for bit.
fn check_ok_case(name: &str, c: &serde_json::Value, t: &leleka_experimental_ref::cvec::CvecTables) {
let bits = |k: &str| -> Vec<u32> {
c[k].as_array().unwrap().iter().map(|x| x.as_u64().unwrap() as u32).collect()
};
assert_eq!(t.dir.iter().map(|x| x.to_bits()).collect::<Vec<_>>(), bits("dir"), "{name}: dir");
assert_eq!(t.s.iter().map(|x| x.to_bits()).collect::<Vec<_>>(), bits("s"), "{name}: s");
assert_eq!(t.mode as i64, c["mode"].as_i64().unwrap(), "{name}");
let (first, last) = (c["first"].as_i64().unwrap(), c["last"].as_i64().unwrap());
assert_eq!((t.first, t.last), (first, last), "{name}");
assert_eq!(t.summary, c["summary"].as_str().unwrap(), "{name}");
}
#[test]
fn tables_and_errors_match_the_original_loader() {
let g = fixture("cvec_golden.json");
let (layers, n) = (g["n_layers"].as_i64().unwrap(), g["n_embd"].as_i64().unwrap());
let hc = g["hc"].as_i64().unwrap();
let mut ok_cases = 0;
let mut err_cases = 0;
for c in g["cases"].as_array().unwrap() {
let name = c["name"].as_str().unwrap();
let got = load_control_vectors(&files_of(c), layers, n, hc, &options_of(c));
if c["ok"].as_i64() == Some(1) {
check_ok_case(name, c, &got.unwrap_or_else(|e| panic!("{name}: {e}")));
ok_cases += 1;
} else {
let e = got.expect_err(name);
assert_eq!(e.to_string(), c["error"].as_str().unwrap(), "{name}");
err_cases += 1;
}
}
assert_eq!((ok_cases, err_cases), (7, 12));
}