Files
leleka/crates/context/tests/setup_policy_oracle.rs
T
Pavlo RatushnyiandClaude Sonnet 5.5 18d712eed5 feat(context): reuse the port-rope leleka-context crate and port setup.py's RAM/VRAM sizing rules
crates/context is taken verbatim from claude/port-rope (rope scaling incl. setup_resolve_rope/derived_factor, memory
plan, context policy). New setup_policy module: ram_ctx, ctx_ram_need, resident_budget_gib, low_ram_*; a golden grid
from 'import setup' (resolve_rope/derived_factor too, which the branch had only transcribed) checked bit for bit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 15:45:55 +02:00

173 lines
5.4 KiB
Rust

//! `setup.py` sizing rules versus `import setup` (the unmodified oracle), over the grid in
//! `tests/fixtures/setup_policy_oracle.json` (`scripts/offline/gen_goldens.py setup_policy`). 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);
}
#[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 kind = |name: &Value| 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}"),
};
let rows = g["resolve_rope"].as_array().unwrap();
assert!(rows.len() > 300);
for row in rows {
let got = setup_resolve_rope(
row["ctx"].as_u64().unwrap(),
kind(&row["scaling"]),
row["scale"].as_f64(),
262144,
);
if row.get("error").is_some() {
assert!(got.is_err(), "{row}");
continue;
}
match (&row["result"], got.unwrap_or_else(|e| panic!("{row}: {e}"))) {
(Value::Null, None) => {}
(want, Some((scaling, factor))) => {
assert_eq!(kind(&want[0]), Some(scaling), "{row}");
same(factor, &want[1], &row.to_string());
}
(want, got) => panic!("{row}: want {want}, got {got:?}"),
}
}
}
#[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}"),
}
}
}