use super::*;
use metrale_bench::gate::Bound;
fn timing() -> TimingLimits {
TimingLimits {
serve_allowance_s: 600,
boot_timeout_s: 900,
shard_overhead_s: 420,
shard_floor_s: 300,
build_allowance_s: 1800,
}
}
fn statuses(pass: &[&str]) -> BTreeMap<String, GateStatus> {
gate::REQUIRED_GATES
.iter()
.map(|id| {
let s = if pass.contains(id) {
GateStatus::Pass
} else {
GateStatus::Missing("no gate records committed".into())
};
(id.to_string(), s)
})
.collect()
}
#[test]
fn a_passing_gate_is_not_remaining() {
let left = remaining(&statuses(&["decode-floor", "vision-fidelity"]), &[]).unwrap();
assert_eq!(left.len(), gate::REQUIRED_GATES.len() - 2);
assert!(!left.contains(&"decode-floor"));
assert!(!left.contains(&"vision-fidelity"));
}
#[test]
fn a_failed_gate_is_remaining() {
let mut s = statuses(&[]);
s.insert(
"decode-floor".into(),
GateStatus::Fail(vec!["too slow".into()]),
);
assert!(remaining(&s, &[]).unwrap().contains(&"decode-floor"));
}
#[test]
fn naming_an_already_passing_gate_is_refused() {
let err = remaining(&statuses(&["decode-floor"]), &["decode-floor".into()]).unwrap_err();
assert!(err.to_string().contains("already passes"), "{err}");
}
#[test]
fn a_group_expands_to_the_shards_of_a_fresh_partition_in_order() {
let four = fresh_partition(4);
assert_eq!(
expand("bfcl-subset", &four),
[
("bfcl-subset", Some((0, 4))),
("bfcl-subset", Some((1, 4))),
("bfcl-subset", Some((2, 4))),
("bfcl-subset", Some((3, 4))),
]
);
assert_eq!(expand("decode-floor", &four), [("decode-floor", None)]);
assert_eq!(
expand("bfcl-subset", &fresh_partition(1)),
[("bfcl-subset", Some((0, 1)))]
);
assert_eq!(expand("bfcl-subset", &fresh_partition(7)).len(), 7);
assert_eq!(expand("decode-floor", &fresh_partition(7)).len(), 1);
}
#[test]
fn every_required_gate_yields_units_with_a_positive_estimate() {
let all = units(
&gate::REQUIRED_GATES,
&|_| None,
&fresh_partition(6),
&timing(),
)
.unwrap();
assert_eq!(all.len(), gate::REQUIRED_GATES.len() - 2 + 12);
for u in &all {
assert!(u.secs() > 0, "{}", u.label());
assert!(matches!(u.estimate, Estimate::Declared(_)), "{}", u.label());
}
let shards = all.iter().filter(|u| u.group.is_some()).count();
assert_eq!(shards, 12);
assert!(all.iter().all(|u| u.group.is_some() == u.shard.is_some()));
}
#[test]
fn a_group_expands_only_to_the_shards_it_still_owes() {
let owed = |g: &'static gate::group::BenchmarkGroup| -> Vec<(usize, usize)> {
if g.id == "bfcl-subset-echolp" {
vec![(2, 4), (3, 4)]
} else {
fresh_partition(6)(g)
}
};
let all = units(
&["bfcl-subset-echolp", "bfcl-subset", "decode-floor"],
&|_| None,
&owed,
&timing(),
)
.unwrap();
let labels: Vec<String> = all.iter().map(Unit::label).collect();
assert_eq!(
labels,
[
"bfcl-subset-echolp[2/4]",
"bfcl-subset-echolp[3/4]",
"bfcl-subset[0/6]",
"bfcl-subset[1/6]",
"bfcl-subset[2/6]",
"bfcl-subset[3/6]",
"bfcl-subset[4/6]",
"bfcl-subset[5/6]",
"decode-floor",
]
);
let none = |_: &'static gate::group::BenchmarkGroup| -> Vec<(usize, usize)> { vec![] };
assert!(
units(&["bfcl-subset"], &|_| None, &none, &timing())
.unwrap()
.is_empty()
);
}
#[test]
fn a_shards_estimate_is_the_groups_share_floored_and_its_param_names_the_slice() {
let whole = units(
&["bfcl-subset"],
&|_| Some((6000, 1)),
&fresh_partition(1),
&timing(),
)
.unwrap();
assert_eq!(whole[0].secs(), 6000 + timing().shard_overhead_s);
assert_eq!(whole[0].shard_param().as_deref(), Some("shard=0/1"));
assert_eq!(whole[0].file_stem(), "bfcl-subset-s0of1");
let four = units(
&["bfcl-subset"],
&|_| Some((6000, 1)),
&fresh_partition(4),
&timing(),
)
.unwrap();
assert_eq!(four[3].secs(), 1500 + timing().shard_overhead_s);
assert_eq!(four[3].shard_param().as_deref(), Some("shard=3/4"));
assert_eq!(four[3].label(), "bfcl-subset[3/4]");
let eight = units(&["bfcl-subset"], &|_| None, &fresh_partition(8), &timing()).unwrap();
let declared = metrale_bench::registry::find("bfcl-subset")
.unwrap()
.expected_secs;
assert_eq!(eight[0].secs(), declared / 8 + timing().shard_overhead_s);
let thin = units(
&["bfcl-subset"],
&|_| Some((6000, 1)),
&fresh_partition(100),
&timing(),
)
.unwrap();
assert_eq!(
thin[0].secs(),
(60 + timing().shard_overhead_s).max(timing().shard_floor_s)
);
assert!(
(1650..=1873).contains(&shard_secs(7560, 6, &timing())),
"{}",
shard_secs(7560, 6, &timing())
);
let plain = units(&["decode-floor"], &|_| None, &fresh_partition(4), &timing()).unwrap();
assert_eq!(plain[0].shard_param(), None);
assert_eq!(plain[0].file_stem(), "decode-floor");
}
#[test]
fn the_default_shard_count_is_two_per_box_and_one_for_a_lone_box() {
assert_eq!(shard_count(0), 1);
assert_eq!(shard_count(1), 1);
assert_eq!(shard_count(2), 4);
assert_eq!(shard_count(3), 6);
}
#[test]
fn the_deadline_is_the_serve_allowance_plus_the_scaled_estimate() {
let all = units(
&["video-fidelity"],
&|_| Some((17, 1)),
&fresh_partition(4),
&timing(),
)
.unwrap();
assert_eq!(all[0].secs(), 17);
assert_eq!(
all[0].deadline(3.0),
std::time::Duration::from_secs(600) + std::time::Duration::from_secs(51)
);
let load_plus_bench = std::time::Duration::from_secs(40 + 17);
assert!(all[0].deadline(1.0) > load_plus_bench);
assert!(std::time::Duration::from_secs(17 * 3) < load_plus_bench);
}
#[test]
fn a_measured_duration_beats_the_declared_one() {
let all = units(
&["decode-floor"],
&|id| (id == "decode-floor").then_some((155, 1_789_000_000)),
&fresh_partition(4),
&timing(),
)
.unwrap();
assert_eq!(
all[0].estimate,
Estimate::Measured {
secs: 155,
recorded_at: 1_789_000_000
}
);
let all = units(
&["decode-floor"],
&|_| Some((0, 1)),
&fresh_partition(4),
&timing(),
)
.unwrap();
assert!(matches!(all[0].estimate, Estimate::Declared(_)));
}
#[test]
fn local_order_puts_groups_first_then_speed_shortest_first() {
let all = order_local(
units(
&gate::REQUIRED_GATES,
&|_| None,
&fresh_partition(4),
&timing(),
)
.unwrap(),
);
let ids: Vec<String> = all.iter().map(Unit::label).collect();
assert!(ids[0].starts_with("bfcl-subset-echolp["), "{ids:?}");
assert!(ids[4].starts_with("bfcl-subset["), "{ids:?}");
let first_plain = all.iter().position(|u| u.group.is_none()).unwrap();
assert_eq!(first_plain, 8);
let speed: Vec<u64> = all
.iter()
.filter(|u| u.group.is_none() && u.class == Sensitivity::Speed)
.map(Unit::secs)
.collect();
assert!(speed.windows(2).all(|w| w[0] <= w[1]), "{speed:?}");
let again = order_local(
units(
&gate::REQUIRED_GATES,
&|_| None,
&fresh_partition(4),
&timing(),
)
.unwrap(),
);
assert_eq!(ids, again.iter().map(Unit::label).collect::<Vec<_>>());
}
#[test]
fn a_measured_shard_run_is_scaled_to_the_whole_draw() {
use super::super::whole_draw_secs;
let mut p = std::collections::BTreeMap::new();
assert_eq!(whole_draw_secs(300, &p), 300);
p.insert("shard".to_string(), "4/6".to_string());
assert_eq!(whole_draw_secs(300, &p), 1800);
p.insert("shard".to_string(), "0/1".to_string());
assert_eq!(whole_draw_secs(300, &p), 300);
p.insert("shard".to_string(), "inherit".to_string());
assert_eq!(whole_draw_secs(300, &p), 300);
}
fn baseline(
default_metrics: &[(&str, Bound)],
other_metrics: &[(&str, Bound)],
) -> gate::GateBaseline {
let model = |m: &[(&str, Bound)]| gate::ModelBaseline {
metrics: m.iter().map(|(k, b)| (k.to_string(), b.clone())).collect(),
..gate::ModelBaseline::default()
};
gate::GateBaseline {
schema: 2,
hardware: BTreeMap::from([(
"gb10".to_string(),
gate::HardwareBaseline {
default: "default-ckpt".into(),
models: BTreeMap::from([
("default-ckpt".to_string(), model(default_metrics)),
("other-ckpt".to_string(), model(other_metrics)),
]),
},
)]),
}
}
fn ceiling(max: f64) -> Bound {
Bound {
max: Some(max),
..Bound::default()
}
}
#[test]
fn a_gate_is_energy_bounded_when_its_default_entry_bounds_joules_per_token() {
let floor = (
"c1_tokens_per_sec",
Bound {
min: Some(20.0),
..Bound::default()
},
);
let rung = baseline(
&[
floor.clone(),
("c1_gpu_rail_joules_per_token", ceiling(1.7)),
],
&[],
);
assert!(energy_bounded(&rung, "gb10").unwrap());
let bare = baseline(&[("gpu_rail_joules_per_token", ceiling(1.7))], &[]);
assert!(
energy_bounded(&bare, "gb10").unwrap(),
"decode-floor's key has no rung prefix"
);
let speed_only = baseline(std::slice::from_ref(&floor), &[]);
assert!(!energy_bounded(&speed_only, "gb10").unwrap());
let unbounded = baseline(
&[
floor.clone(),
("c1_gpu_rail_joules_per_token", Bound::default()),
],
&[],
);
assert!(!energy_bounded(&unbounded, "gb10").unwrap());
let other = baseline(&[floor], &[("c1_gpu_rail_joules_per_token", ceiling(1.7))]);
assert!(!energy_bounded(&other, "gb10").unwrap());
assert!(!energy_bounded(&rung, "h100-sxm").unwrap());
}
#[test]
fn a_class_whose_default_is_missing_is_an_error_not_a_no() {
let mut b = baseline(&[("c1_gpu_rail_joules_per_token", ceiling(1.7))], &[]);
b.hardware.get_mut("gb10").unwrap().default = "gone".into();
assert!(energy_bounded(&b, "gb10").is_err());
}
#[test]
fn the_committed_dense_sweep_is_energy_bounded_on_gb10() {
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.and_then(std::path::Path::parent)
.expect("the workspace root");
let bounded =
|id: &str| energy_bounded(&gate::read_baseline(root, id).unwrap(), "gb10").unwrap();
assert!(bounded("concurrency-sweep"));
for id in ["ttft-warm-gate", "bfcl-subset", "kat-equality-gate"] {
assert!(!bounded(id), "{id}");
}
}