Files
leleka/crates/experimental-ref/tests/remote_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

171 lines
7.6 KiB
Rust

//! The second-GPU expert tier against the Original's decision code (the golden host build extracts the
//! selection, slot sizing, claim bookkeeping, grouping, RemoteMeta packing and result scatter of
//! `remote_experts.cpp` verbatim and runs them on the host).
mod common;
use common::fixture;
use leleka_experimental_ref::remote_experts::{
Candidates, Dispatch, ExpertSizes, H, RemoteDevice, OpenShape, PrimaryView, RemoteError, RemoteMeta, RemotePlan,
RouteShape, dispatch, plan_open, scatter,
};
use serde_json::Value;
const LAYERS: usize = 6;
const EXPERTS: usize = 24;
fn ints(v: &Value) -> Vec<i64> {
v.as_array().unwrap().iter().map(|x| x.as_i64().unwrap()).collect()
}
fn as_i64s<T: Copy + Into<i64>>(xs: &[T]) -> Vec<i64> {
xs.iter().map(|&x| x.into()).collect()
}
fn sizes_of(c: &Value) -> ExpertSizes {
ExpertSizes {
native: c["native"].as_i64() == Some(1),
max_blob: c["max_blob"].as_u64().unwrap(),
blob_bytes: c["blobs"].as_array().unwrap().iter().map(|x| x.as_u64().unwrap()).collect(),
}
}
fn primary_of(c: &Value) -> Vec<u8> {
ints(&c["primary"]).iter().map(|&x| x as u8).collect()
}
fn pairs_of(v: &Value) -> Vec<(i32, i32)> {
let pair = |p: &Value| (p[0].as_i64().unwrap() as i32, p[1].as_i64().unwrap() as i32);
v.as_array().unwrap().iter().map(pair).collect()
}
/// `plan_open` of case `c` with `free` bytes of device memory, claiming into `claimed`.
fn open_with_free(c: &Value, free: u64, claimed: &mut [u8]) -> Result<RemotePlan, RemoteError> {
let primary = primary_of(c);
let ranked = pairs_of(&c["ranked"]);
let has = |l: i32, e: i32| primary[l as usize * EXPERTS + e as usize] != 0;
let slots = c["slots"].as_i64().unwrap() as i32;
let (layers, experts) = (LAYERS as i64, EXPERTS as i64);
let helper = RemoteDevice { device: 1, device_count: 2, free_bytes: free };
let candidates = Candidates { ranked: &ranked, primary_has: &has, claimed };
plan_open(helper, OpenShape { slots, layers, experts }, candidates, &sizes_of(c))
}
fn open_case(c: &Value) -> (RemotePlan, Vec<u8>) {
let mut claimed: Vec<u8> = ints(&c["claimed_in"]).iter().map(|&x| x as u8).collect();
let plan = open_with_free(c, u64::MAX / 2, &mut claimed).unwrap();
(plan, claimed)
}
#[test]
fn selection_sizing_and_claims_match_open() {
let g = fixture("remote_golden.json");
let cases = g["open"].as_array().unwrap();
assert_eq!(cases.len(), 40);
for (n, c) in cases.iter().enumerate() {
let (plan, claimed) = open_case(c);
assert_eq!(plan.selected, pairs_of(&c["selected"]), "case {n}: selected");
assert_eq!(plan.needed_bytes, c["needed"].as_u64().unwrap(), "case {n}: needed");
let layers = as_i64s(&plan.layers_present);
assert_eq!(layers, ints(&c["layers_present"]), "case {n}: layers");
assert_eq!(as_i64s(&claimed), ints(&c["claimed_out"]), "case {n}: claimed");
}
}
#[test]
fn memory_check_is_inclusive_and_refusal_keeps_claims() {
let g = fixture("remote_golden.json");
let c = &g["open"][3];
let (plan, _) = open_case(c);
let before: Vec<u8> = ints(&c["claimed_in"]).iter().map(|&x| x as u8).collect();
let mut claimed = before.clone();
let free = plan.needed_bytes + (512 << 20);
let err = open_with_free(c, free - 1, &mut claimed).unwrap_err();
assert_eq!(err, RemoteError::SlotsLeaveLessThanReserve { needed: plan.needed_bytes, free: free - 1 });
assert_eq!(claimed, before);
assert!(open_with_free(c, free, &mut claimed).is_ok());
}
/// The `dispatch` of one `begin` case against the plan of its tier.
fn dispatch_case(plan: &RemotePlan, primary: &[u8], c: &Value) -> Result<Dispatch, RemoteError> {
let pres: Vec<i32> = primary.iter().map(|&h| if h != 0 { 1 } else { -1 }).collect();
let ids: Vec<i32> = ints(&c["ids"]).iter().map(|&x| x as i32).collect();
let kind: Vec<i32> = ints(&c["kind"]).iter().map(|&x| x as i32).collect();
let (n_tok, k) = (c["n_tok"].as_i64().unwrap(), c["k"].as_i64().unwrap());
let primary = PrimaryView {
kind: (c["use_kind"].as_i64() == Some(1)).then_some(&kind[..]),
res: (c["use_primary"].as_i64() == Some(1)).then_some(&pres[..]),
};
dispatch(plan, c["layer"].as_i64().unwrap(), &ids, RouteShape { token_count: n_tok, experts_per_token: k }, primary)
}
/// The grouping arrays of a launched dispatch equal the original's.
fn check_grouping(n: usize, c: &Value, d: &Dispatch) {
let want = |key: &str| -> Vec<i64> { ints(&c[key]) };
assert_eq!(as_i64s(&d.owned), want("owned")[..d.owned.len()], "case {n}: owned");
let groups: Vec<i64> = d.groups.iter().map(|x| i64::from(x.expert)).collect();
assert_eq!(groups, want("group_id"), "case {n}: groups");
assert_eq!(as_i64s(&d.start), want("start"), "case {n}: start");
assert_eq!(as_i64s(&d.dst), want("dst"), "case {n}: dst");
assert_eq!(as_i64s(&d.tok), want("tok"), "case {n}: tok");
assert_eq!(as_i64s(&d.original_row), want("original_row"), "case {n}: rows");
assert_eq!(d.computed, c["computed"].as_u64().unwrap(), "case {n}: computed");
}
/// The packed `RemoteMeta` image and the result scatter of a launched dispatch.
fn check_remote_meta_image(n: usize, c: &Value, d: &Dispatch) {
// device pointers: the harness hands slot s the address 0x1_0000_0000 + s * 0x20_0000
let ptrs: Vec<u64> = c["ptr"].as_array().unwrap().iter().map(|x| x.as_u64().unwrap()).collect();
let img = RemoteMeta::to_bytes(d, &|slot| 0x1_0000_0000 + slot as u64 * 0x20_0000);
let want_ptrs: Vec<u64> = d.groups.iter().map(|x| 0x1_0000_0000 + x.slot as u64 * 0x20_0000).collect();
assert_eq!(ptrs, want_ptrs, "case {n}: ptr");
assert_eq!(as_i64s(&img), ints(&c["meta"]), "case {n}: meta image");
}
fn check_scatter_rows(n: usize, c: &Value, d: &Dispatch) {
let (n_tok, k) = (c["n_tok"].as_i64().unwrap(), c["k"].as_i64().unwrap());
// scatter with the real row width
let h = H;
let compact: Vec<f32> = (0..d.dst.len() * h)
.map(|i| ((i / h) as f32 + 1.0) + ((i % h) as f32 * 0.0001f32))
.collect();
let mut out = vec![-1.0f32; (n_tok * k) as usize * h];
scatter(&mut out, &compact, &d.original_row, h);
let rows = c["scatter_rows"].as_array().unwrap();
assert_eq!(rows.len(), (n_tok * k) as usize);
for (r, sig) in rows.iter().enumerate() {
for (j, at) in [0, h / 2, h - 1].into_iter().enumerate() {
let want = sig[j].as_f64().unwrap();
let got = f64::from(out[r * h + at]);
assert!((got - want).abs() <= want.abs() * 1e-6 + 1e-9, "case {n} row {r}[{at}]: {got} vs {want}");
}
}
}
#[test]
fn grouping_meta_and_scatter_match_begin_and_finish() {
let g = fixture("remote_golden.json");
let opens = g["open"].as_array().unwrap();
let plans: Vec<_> = opens.iter().map(|c| open_case(c).0).collect();
let (mut launched, mut refused, mut groups_seen) = (0, 0, 0);
for (n, c) in g["begin"].as_array().unwrap().iter().enumerate() {
let tier = c["tier"].as_u64().unwrap() as usize;
let got = dispatch_case(&plans[tier], &primary_of(&opens[tier]), c);
if c["ok"].as_i64() != Some(1) {
assert_eq!(got, Err(RemoteError::InvalidDispatch), "case {n}");
refused += 1;
continue;
}
let d = got.unwrap_or_else(|e| panic!("case {n}: {e}"));
check_grouping(n, c, &d);
if d.groups.is_empty() {
continue;
}
launched += 1;
groups_seen += d.groups.len();
check_remote_meta_image(n, c, &d);
check_scatter_rows(n, c, &d);
}
assert!(launched > 80 && refused >= 5 && groups_seen > 150, "fixture too thin: {launched} {refused} {groups_seen}");
}