Files
leleka/crates/context/tests/setup_policy_oracle.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

139 lines
4.9 KiB
Rust

//! `setup.py` sizing rules versus `import setup` (the unmodified oracle), over the grid in
//! `tests/fixtures/setup_policy_oracle.json`. Floats are compared
//! bit for bit.
use leleka_context::{
rope_scaling::{RopeScalingType, derived_factor, setup_resolve_rope},
setup_policy::{self as policy, Kv},
};
use serde_json::Value;
fn golden() -> Value {
serde_json::from_str(include_str!("fixtures/setup_policy_oracle.json")).unwrap()
}
fn model(name: &Value) -> &'static policy::Model {
policy::model(name.as_str().unwrap()).unwrap()
}
fn same(got: f64, want: &Value, what: &str) {
assert_eq!(got.to_bits(), want.as_f64().unwrap().to_bits(), "{what}: {got} vs {want}");
}
#[test]
fn model_table_and_contexts_are_setup_pys() {
let g = golden();
for (name, row) in g["models"].as_object().unwrap() {
let m = policy::model(name).unwrap_or_else(|| panic!("{name} missing"));
assert_eq!(u64::from(m.ram_gb), row["ram_gb"].as_u64().unwrap(), "{name}");
same(m.arena_gb, &row["arena_gb"], name);
}
assert_eq!(g["models"].as_object().unwrap().len(), policy::MODELS.len());
let contexts: Vec<u64> = g["contexts"].as_array().unwrap().iter().map(|c| c.as_u64().unwrap()).collect();
assert_eq!(contexts, policy::CONTEXTS);
}
fn rope_kind(name: &Value) -> Option<RopeScalingType> {
match name.as_str() {
None => None,
Some("none") => Some(RopeScalingType::None),
Some("linear") => Some(RopeScalingType::Linear),
Some("yarn") => Some(RopeScalingType::YaRN),
Some(other) => panic!("{other}"),
}
}
fn check_resolve_rope_row(row: &Value) {
let got =
setup_resolve_rope(row["ctx"].as_u64().unwrap(), rope_kind(&row["scaling"]), row["scale"].as_f64(), 262144);
if row.get("error").is_some() {
assert!(got.is_err(), "{row}");
return;
}
match (&row["result"], got.unwrap_or_else(|e| panic!("{row}: {e}"))) {
(Value::Null, None) => {}
(want, Some((scaling, factor))) => {
assert_eq!(rope_kind(&want[0]), Some(scaling), "{row}");
same(factor, &want[1], &row.to_string());
}
(want, got) => panic!("{row}: want {want}, got {got:?}"),
}
}
#[test]
fn derived_factor_and_resolve_rope_match() {
let g = golden();
for row in g["derived_factor"].as_array().unwrap() {
same(
derived_factor(row["ctx"].as_u64().unwrap(), row["trained"].as_u64().unwrap()),
&row["result"],
"derived_factor",
);
}
let rows = g["resolve_rope"].as_array().unwrap();
assert!(rows.len() > 300);
for row in rows {
check_resolve_rope_row(row);
}
}
#[test]
fn ram_rules_match() {
let g = golden();
for row in g["ram_ctx"].as_array().unwrap() {
assert_eq!(
policy::ram_ctx(model(&row["model"]), row["ram"].as_f64().unwrap(), row["low_ram"].as_bool().unwrap()),
row["result"].as_u64().unwrap(),
"{row}"
);
}
for row in g["ctx_ram_need"].as_array().unwrap() {
let got =
policy::ctx_ram_need(model(&row["model"]), row["ctx"].as_u64().unwrap(), row["low_ram"].as_bool().unwrap());
match (got, &row["result"]) {
(None, Value::Null) => {}
(Some(got), want) => same(got, want, &row.to_string()),
(got, want) => panic!("{row}: {got:?} vs {want}"),
}
}
for row in g["resident_budget"].as_array().unwrap() {
assert_eq!(
policy::resident_budget_gib(
model(&row["model"]),
row["ram"].as_f64().unwrap(),
row["kv"].as_f64().unwrap()
),
row["result"].as_i64().unwrap(),
"{row}"
);
}
}
#[test]
fn low_ram_rules_match() {
let g = golden();
let rows = g["low_ram"].as_array().unwrap();
assert!(rows.len() > 1000);
for row in rows {
let m = model(&row["model"]);
let f = |key: &str| row[key].as_f64().unwrap();
let kv = || Kv::parse(row["kv"].as_str().unwrap());
let ctx = || row["ctx"].as_u64().unwrap();
match row["fn"].as_str().unwrap() {
"needed" => assert_eq!(policy::low_ram_needed(m, f("ram")), row["result"].as_bool().unwrap(), "{row}"),
"fits" => {
assert_eq!(policy::low_ram_fits(m, f("ram"), f("vram")), row["result"].as_bool().unwrap(), "{row}")
}
"gpu_gb" => {
same(policy::low_ram_gpu_gb(m, f("vram"), ctx(), kv()), &row["result"], &row.to_string());
same(policy::low_ram_gpu_share(m, f("vram"), ctx(), kv()), &row["share"], &row.to_string());
}
"resident" => assert_eq!(
policy::low_ram_resident(m, f("ram"), f("vram"), ctx(), kv()),
row["result"].as_bool().unwrap(),
"{row}"
),
other => panic!("{other}"),
}
}
}