benchmarks-mv
Measurement harnesses and evidence bundles for AIEN on the NVIDIA DGX Spark GB10. Speed is reported against the machine's physical limit and my own previous run. Attachment of AienOS.
Brought by drakestapleton
benchmarks-mv / src / bin / bench_orig3_judge_v2.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! ORIG-3 v2 judge: applies the frozen `orig3_selector_v2.json` decision rule |
| 17 | //! (`docs/orig3/PROTOCOL-v2.md`) to selmain mode E v2 output and the ORIG-4 overhead runs. |
| 18 | //! |
| 19 | //! Inputs: three per-record files written by aien-train `selmain` (mode E, config flags 14) |
| 20 | //! for the candidate, the shuffled-label control and the untrained control; the output |
| 21 | //! directory of omega-mv `tests/orig4/run.sh` with `ORIG4_OVERHEAD=1`; the candidate's |
| 22 | //! training wall time and the collection wall time in milliseconds. |
| 23 | //! Per-record columns (21): `seq fam core M N model rule ref static near oracle c_model |
| 24 | //! c_oracle c_rule c_ref c_static c_near press calls split cell`, costs in ps per call. |
| 25 | //! The three files must agree on every column except `model` and `c_model`. |
| 26 | //! Exit 0 after printing the verdict; exit 2 on unreadable, malformed or inconsistent input. |
| 27 | //! Reads files only: no network, no GPU, no live server. |
| 28 | |
| 29 | use clap::Parser; |
| 30 | use std::fs; |
| 31 | use std::path::{Path, PathBuf}; |
| 32 | |
| 33 | #[derive(Parser)] |
| 34 | #[command(name = "bench_orig3_judge_v2")] |
| 35 | #[command( |
| 36 | about = "Apply the ORIG-3 v2 frozen decision rule to selmain E v2 files and overhead runs" |
| 37 | )] |
| 38 | struct Args { |
| 39 | /// Candidate (mlp_scratch_v2) per-record file |
| 40 | #[arg(long)] |
| 41 | mlp: PathBuf, |
| 42 | /// shuffled-label control per-record file |
| 43 | #[arg(long)] |
| 44 | shuffled: PathBuf, |
| 45 | /// untrained control per-record file |
| 46 | #[arg(long)] |
| 47 | untrained: PathBuf, |
| 48 | /// OUT directory of tests/orig4/run.sh ORIG4_OVERHEAD=1 (summary.txt, ovh_*_1.stdout) |
| 49 | #[arg(long)] |
| 50 | overhead_dir: PathBuf, |
| 51 | /// Candidate training wall time, ms (run-select.sh v2_train_mlp wall_ms) |
| 52 | #[arg(long)] |
| 53 | train_wall_ms: u64, |
| 54 | /// Collection wall time, ms (harness start to end) |
| 55 | #[arg(long)] |
| 56 | collect_wall_ms: u64, |
| 57 | } |
| 58 | |
| 59 | const COLS: usize = 21; |
| 60 | const CORES: usize = 2; |
| 61 | /// Superiority margin: total <= 0.99 x min(retrieval, heuristic), in percent. |
| 62 | const SUP_PCT: u128 = 99; |
| 63 | /// Non-inferiority bound: total > 1.01 x min(retrieval, heuristic) is NO-GO, in percent. |
| 64 | const NONINF_PCT: u128 = 101; |
| 65 | /// One-sided exact sign test level per contrast (0.05 / 4: 2 core classes x 2 comparators). |
| 66 | const ALPHA: f64 = 0.0125; |
| 67 | |
| 68 | /// One decision of one per-record line. |
| 69 | #[derive(Clone, Copy, PartialEq, Eq)] |
| 70 | struct Rec { |
| 71 | core: usize, |
| 72 | press: u64, |
| 73 | split: u64, |
| 74 | calls: u128, |
| 75 | model: u64, |
| 76 | oracle: u64, |
| 77 | rule: u64, |
| 78 | reference: u64, |
| 79 | stat: u64, |
| 80 | near: u64, |
| 81 | /// Every column except model and c_model, for the cross-file consistency check. |
| 82 | key: [u64; COLS - 2], |
| 83 | } |
| 84 | |
| 85 | fn load(path: &Path) -> Result<Vec<Rec>, String> { |
| 86 | let text = fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?; |
| 87 | let mut out = Vec::new(); |
| 88 | for (ln, line) in text.lines().enumerate() { |
| 89 | let w: Vec<u64> = line |
| 90 | .split_whitespace() |
| 91 | .map(|f| f.parse::<u64>()) |
| 92 | .collect::<Result<_, _>>() |
| 93 | .map_err(|e| format!("{}:{}: {e}", path.display(), ln + 1))?; |
| 94 | if w.len() != COLS { |
| 95 | return Err(format!( |
| 96 | "{}:{}: {} columns, want {COLS}", |
| 97 | path.display(), |
| 98 | ln + 1, |
| 99 | w.len() |
| 100 | )); |
| 101 | } |
| 102 | let (core, press, split) = (w[2] as usize, w[17], w[19]); |
| 103 | if core > 2 || press > 1 || !(split == 1 || split == 2) { |
| 104 | return Err(format!( |
| 105 | "{}:{}: core {core} press {press} split {split} out of range", |
| 106 | path.display(), |
| 107 | ln + 1 |
| 108 | )); |
| 109 | } |
| 110 | let mut key = [0u64; COLS - 2]; |
| 111 | let mut k = 0; |
| 112 | for (i, v) in w.iter().enumerate() { |
| 113 | if i != 5 && i != 11 { |
| 114 | key[k] = *v; |
| 115 | k += 1; |
| 116 | } |
| 117 | } |
| 118 | out.push(Rec { |
| 119 | core, |
| 120 | press, |
| 121 | split, |
| 122 | calls: u128::from(w[18]), |
| 123 | model: w[11], |
| 124 | oracle: w[12], |
| 125 | rule: w[13], |
| 126 | reference: w[14], |
| 127 | stat: w[15], |
| 128 | near: w[16], |
| 129 | key, |
| 130 | }); |
| 131 | } |
| 132 | Ok(out) |
| 133 | } |
| 134 | |
| 135 | /// Lower median of a non-empty list. |
| 136 | fn median(mut v: Vec<u64>) -> u64 { |
| 137 | v.sort_unstable(); |
| 138 | v[(v.len() - 1) / 2] |
| 139 | } |
| 140 | |
| 141 | /// `scored=` of the COST line of one overhead run. |
| 142 | fn scored(dir: &Path, run: &str) -> Result<u64, String> { |
| 143 | let p = dir.join(format!("{run}.stdout")); |
| 144 | let text = fs::read_to_string(&p).map_err(|e| format!("{}: {e}", p.display()))?; |
| 145 | for line in text.lines() { |
| 146 | if let Some(rest) = line.strip_prefix("COST ") { |
| 147 | for f in rest.split_whitespace() { |
| 148 | if let Some(v) = f.strip_prefix("scored=") { |
| 149 | return v.parse().map_err(|e| format!("{}: {e}", p.display())); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | Err(format!("{}: no COST scored= line", p.display())) |
| 155 | } |
| 156 | |
| 157 | struct Overhead { |
| 158 | reps: usize, |
| 159 | med: [u64; 3], |
| 160 | scored_mlp: u64, |
| 161 | scored_near: u64, |
| 162 | mlp_ps: u128, |
| 163 | near_ps: u128, |
| 164 | } |
| 165 | |
| 166 | /// Per-decision overhead in ps: (median wall of the advisor run - median wall of the disabled |
| 167 | /// run) / scored decisions, from the interleaved OVERHEAD lines of summary.txt. |
| 168 | fn overhead(dir: &Path) -> Result<Overhead, String> { |
| 169 | let p = dir.join("summary.txt"); |
| 170 | let text = fs::read_to_string(&p).map_err(|e| format!("{}: {e}", p.display()))?; |
| 171 | let mut walls: [Vec<u64>; 3] = [Vec::new(), Vec::new(), Vec::new()]; |
| 172 | for line in text.lines() { |
| 173 | let Some(rest) = line.strip_prefix("OVERHEAD ") else { |
| 174 | continue; |
| 175 | }; |
| 176 | let mut run = None; |
| 177 | let mut us = None; |
| 178 | for f in rest.split_whitespace() { |
| 179 | if let Some(v) = f.strip_prefix("run=") { |
| 180 | run = Some(v); |
| 181 | } else if let Some(v) = f.strip_prefix("wall_us=") { |
| 182 | us = Some( |
| 183 | v.parse::<u64>() |
| 184 | .map_err(|e| format!("{}: {e}", p.display()))?, |
| 185 | ); |
| 186 | } |
| 187 | } |
| 188 | let i = match run { |
| 189 | Some("disabled") => 0, |
| 190 | Some("mlp") => 1, |
| 191 | Some("near") => 2, |
| 192 | _ => return Err(format!("{}: bad OVERHEAD line {line}", p.display())), |
| 193 | }; |
| 194 | walls[i].push(us.ok_or_else(|| format!("{}: no wall_us in {line}", p.display()))?); |
| 195 | } |
| 196 | let reps = walls[0].len(); |
| 197 | if reps == 0 || walls.iter().any(|w| w.len() != reps) { |
| 198 | return Err(format!("{}: unequal or missing OVERHEAD runs", p.display())); |
| 199 | } |
| 200 | let med = [ |
| 201 | median(walls[0].clone()), |
| 202 | median(walls[1].clone()), |
| 203 | median(walls[2].clone()), |
| 204 | ]; |
| 205 | let scored_mlp = scored(dir, "ovh_mlp_1")?; |
| 206 | let scored_near = scored(dir, "ovh_near_1")?; |
| 207 | if scored_mlp == 0 || scored_near == 0 { |
| 208 | return Err("overhead runs scored no decision".into()); |
| 209 | } |
| 210 | let per = |m: u64, s: u64| u128::from(m.saturating_sub(med[0])) * 1_000_000 / u128::from(s); |
| 211 | Ok(Overhead { |
| 212 | reps, |
| 213 | med, |
| 214 | scored_mlp, |
| 215 | scored_near, |
| 216 | mlp_ps: per(med[1], scored_mlp), |
| 217 | near_ps: per(med[2], scored_near), |
| 218 | }) |
| 219 | } |
| 220 | |
| 221 | /// Exact one-sided sign test: P(X >= k | X ~ Binomial(n, 1/2)). |
| 222 | fn sign_p(k: u64, n: u64) -> f64 { |
| 223 | if n == 0 { |
| 224 | return 1.0; |
| 225 | } |
| 226 | let ln2 = std::f64::consts::LN_2; |
| 227 | let mut lnc = 0.0f64; |
| 228 | let mut p = 0.0f64; |
| 229 | for i in 0..=n { |
| 230 | if i > 0 { |
| 231 | lnc += ((n - i + 1) as f64).ln() - (i as f64).ln(); |
| 232 | } |
| 233 | if i >= k { |
| 234 | p += (lnc - n as f64 * ln2).exp(); |
| 235 | } |
| 236 | } |
| 237 | p.min(1.0) |
| 238 | } |
| 239 | |
| 240 | const POLICIES: [&str; 10] = [ |
| 241 | "mlp_scratch_v2", |
| 242 | "retrieval", |
| 243 | "heuristic", |
| 244 | "best_static", |
| 245 | "fixed_rule", |
| 246 | "always_reference", |
| 247 | "untrained_control", |
| 248 | "shuffled_label_control", |
| 249 | "constant_control", |
| 250 | "oracle", |
| 251 | ]; |
| 252 | const HEUR: [&str; 3] = ["fixed_rule", "always_reference", "best_static"]; |
| 253 | |
| 254 | /// ps per call chosen by policy p for decision i (index into the three aligned files). |
| 255 | fn ps(p: usize, i: usize, m: &[Rec], s: &[Rec], u: &[Rec], heur: usize) -> u64 { |
| 256 | let r = &m[i]; |
| 257 | let h = [r.rule, r.reference, r.stat]; |
| 258 | match p { |
| 259 | 0 => r.model, |
| 260 | 1 => r.near, |
| 261 | 2 => h[heur], |
| 262 | 3 => r.stat, |
| 263 | 4 => r.rule, |
| 264 | 5 => r.reference, |
| 265 | 6 => u[i].model, |
| 266 | 7 => s[i].model, |
| 267 | // A constant score ranks every arm equal; the frozen tie rule picks arm 0. |
| 268 | 8 => r.reference, |
| 269 | _ => r.oracle, |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | fn fail(msg: String) -> ! { |
| 274 | eprintln!("bench_orig3_judge_v2: {msg}"); |
| 275 | std::process::exit(2); |
| 276 | } |
| 277 | |
| 278 | fn ratio(a: u128, b: u128) -> f64 { |
| 279 | a as f64 / b as f64 |
| 280 | } |
| 281 | |
| 282 | fn main() { |
| 283 | let args = Args::parse(); |
| 284 | let m = load(&args.mlp).unwrap_or_else(|e| fail(e)); |
| 285 | let s = load(&args.shuffled).unwrap_or_else(|e| fail(e)); |
| 286 | let u = load(&args.untrained).unwrap_or_else(|e| fail(e)); |
| 287 | if m.len() != s.len() || m.len() != u.len() { |
| 288 | fail("the three per-record files differ in length".into()); |
| 289 | } |
| 290 | for i in 0..m.len() { |
| 291 | if m[i].key != s[i].key || m[i].key != u[i].key { |
| 292 | fail(format!( |
| 293 | "line {} differs between files outside model columns", |
| 294 | i + 1 |
| 295 | )); |
| 296 | } |
| 297 | } |
| 298 | let o = overhead(&args.overhead_dir).unwrap_or_else(|e| fail(e)); |
| 299 | // Overhead per policy, ps per decision: the scorer path for the candidate and every |
| 300 | // control (they run the same advisor), retrieval its own, heuristics and oracle 0. |
| 301 | let ovh = |p: usize| -> u128 { |
| 302 | match p { |
| 303 | 0 | 6 | 7 | 8 => o.mlp_ps, |
| 304 | 1 => o.near_ps, |
| 305 | _ => 0, |
| 306 | } |
| 307 | }; |
| 308 | println!( |
| 309 | "orig3_v2 rule=manifests/prereg/orig3_selector_v2.json protocol=docs/orig3/PROTOCOL-v2.md" |
| 310 | ); |
| 311 | println!( |
| 312 | "overhead reps={} median_wall_us disabled={} mlp={} near={} scored mlp={} near={} per_decision_ps mlp={} retrieval={} heuristics=0", |
| 313 | o.reps, o.med[0], o.med[1], o.med[2], o.scored_mlp, o.scored_near, o.mlp_ps, o.near_ps |
| 314 | ); |
| 315 | let mut primary = [[false; 10]; CORES]; |
| 316 | let mut noninf_fail = [false; CORES]; |
| 317 | for c in 0..CORES { |
| 318 | let val: Vec<usize> = (0..m.len()) |
| 319 | .filter(|&i| m[i].core == c && m[i].split == 1 && m[i].press == 0) |
| 320 | .collect(); |
| 321 | let held: Vec<usize> = (0..m.len()) |
| 322 | .filter(|&i| m[i].core == c && m[i].split == 2 && m[i].press == 0) |
| 323 | .collect(); |
| 324 | if val.is_empty() || held.is_empty() { |
| 325 | fail(format!( |
| 326 | "core {c}: no VALIDATION or HELD-OUT pressure-0 decisions" |
| 327 | )); |
| 328 | } |
| 329 | let vt: Vec<u128> = (0..3) |
| 330 | .map(|h| { |
| 331 | val.iter() |
| 332 | .map(|&i| m[i].calls * u128::from([m[i].rule, m[i].reference, m[i].stat][h])) |
| 333 | .sum() |
| 334 | }) |
| 335 | .collect(); |
| 336 | let mut heur = 0; |
| 337 | for h in 1..3 { |
| 338 | if vt[h] < vt[heur] { |
| 339 | heur = h; |
| 340 | } |
| 341 | } |
| 342 | println!( |
| 343 | "core={c} heldout_n={} validation_n={} validation_total_ps fixed_rule={} always_reference={} best_static={} heuristic={}", |
| 344 | held.len(), |
| 345 | val.len(), |
| 346 | vt[0], |
| 347 | vt[1], |
| 348 | vt[2], |
| 349 | HEUR[heur] |
| 350 | ); |
| 351 | let cost = |
| 352 | |p: usize, i: usize| m[i].calls * u128::from(ps(p, i, &m, &s, &u, heur)) + ovh(p); |
| 353 | let choice = |p: usize| -> u128 { |
| 354 | held.iter() |
| 355 | .map(|&i| m[i].calls * u128::from(ps(p, i, &m, &s, &u, heur))) |
| 356 | .sum() |
| 357 | }; |
| 358 | let total: Vec<u128> = (0..10) |
| 359 | .map(|p| held.iter().map(|&i| cost(p, i)).sum()) |
| 360 | .collect(); |
| 361 | let oracle = choice(9); |
| 362 | let cmp = total[1].min(total[2]); |
| 363 | for p in 0..10 { |
| 364 | println!( |
| 365 | "core={c} policy={} choice_ps={} overhead_ps={} total_ps={} choice/oracle={:.4} total/oracle_choice={:.4} total/min(retrieval,heuristic)={:.4}", |
| 366 | POLICIES[p], |
| 367 | choice(p), |
| 368 | ovh(p) * held.len() as u128, |
| 369 | total[p], |
| 370 | ratio(choice(p), oracle), |
| 371 | ratio(total[p], oracle), |
| 372 | ratio(total[p], cmp) |
| 373 | ); |
| 374 | } |
| 375 | for p in [0usize, 6, 7, 8] { |
| 376 | let p1 = total[p] * 100 <= SUP_PCT * cmp; |
| 377 | let mut p2 = true; |
| 378 | let mut signs = String::new(); |
| 379 | for q in [1usize, 2] { |
| 380 | let (mut k, mut n) = (0u64, 0u64); |
| 381 | for &i in &held { |
| 382 | let (a, b) = (cost(p, i), cost(q, i)); |
| 383 | if a != b { |
| 384 | n += 1; |
| 385 | k += u64::from(a < b); |
| 386 | } |
| 387 | } |
| 388 | let pv = sign_p(k, n); |
| 389 | p2 &= pv <= ALPHA; |
| 390 | signs.push_str(&format!( |
| 391 | " vs_{} wins={k} discordant={n} p={pv:.3e}", |
| 392 | POLICIES[q] |
| 393 | )); |
| 394 | } |
| 395 | primary[c][p] = p1 && p2; |
| 396 | println!( |
| 397 | "core={c} primary policy={} P1(total<=0.99*min)={} P2(sign<=0.0125 vs both)={}{} PRIMARY={}", |
| 398 | POLICIES[p], |
| 399 | u8::from(p1), |
| 400 | u8::from(p2), |
| 401 | signs, |
| 402 | u8::from(primary[c][p]) |
| 403 | ); |
| 404 | } |
| 405 | noninf_fail[c] = total[0] * 100 > NONINF_PCT * cmp; |
| 406 | println!( |
| 407 | "core={c} noninferiority_fail(total>1.01*min)={} candidate/min={:.4}", |
| 408 | u8::from(noninf_fail[c]), |
| 409 | ratio(total[0], cmp) |
| 410 | ); |
| 411 | // Amortization against the cheaper comparator (overhead included). |
| 412 | let q = if total[1] <= total[2] { 1 } else { 2 }; |
| 413 | let n = held.len() as u128; |
| 414 | let saving = total[q] as i128 - total[0] as i128; |
| 415 | let per_dec = saving as f64 / n as f64; |
| 416 | let fixed_ps = u128::from(args.train_wall_ms + args.collect_wall_ms) * 1_000_000_000; |
| 417 | let calls: u128 = held.iter().map(|&i| m[i].calls).sum(); |
| 418 | let per_call = (choice(q) as f64 - choice(0) as f64) / calls as f64; |
| 419 | let be_dec = if per_dec > 0.0 { |
| 420 | format!("{:.0}", (fixed_ps as f64 / per_dec).ceil()) |
| 421 | } else { |
| 422 | "none".into() |
| 423 | }; |
| 424 | let be_calls = if per_call > 0.0 { |
| 425 | format!("{:.0}", (o.mlp_ps as f64 / per_call).ceil()) |
| 426 | } else { |
| 427 | "none".into() |
| 428 | }; |
| 429 | println!( |
| 430 | "core={c} amortization comparator={} saving_ps_per_decision={per_dec:.1} train_ms={} collect_ms={} break_even_decisions={be_dec} per_call_saving_ps={per_call:.3} mean_calls_per_decision={:.1} break_even_calls_per_decision={be_calls}", |
| 431 | POLICIES[q], |
| 432 | args.train_wall_ms, |
| 433 | args.collect_wall_ms, |
| 434 | calls as f64 / n as f64 |
| 435 | ); |
| 436 | // Reported only: held-out decisions at pressure 1 (no overhead, choice cost only). |
| 437 | let p1: Vec<usize> = (0..m.len()) |
| 438 | .filter(|&i| m[i].core == c && m[i].split == 2 && m[i].press == 1) |
| 439 | .collect(); |
| 440 | if !p1.is_empty() { |
| 441 | let ch = |p: usize| -> u128 { |
| 442 | p1.iter() |
| 443 | .map(|&i| m[i].calls * u128::from(ps(p, i, &m, &s, &u, heur))) |
| 444 | .sum() |
| 445 | }; |
| 446 | let or = ch(9); |
| 447 | println!( |
| 448 | "core={c} reported_pressure1 n={} choice/oracle mlp={:.4} retrieval={:.4} heuristic={:.4} best_static={:.4} fixed_rule={:.4}", |
| 449 | p1.len(), |
| 450 | ratio(ch(0), or), |
| 451 | ratio(ch(1), or), |
| 452 | ratio(ch(2), or), |
| 453 | ratio(ch(3), or), |
| 454 | ratio(ch(4), or) |
| 455 | ); |
| 456 | } |
| 457 | } |
| 458 | let invalid = (0..CORES).any(|c| primary[c][6] || primary[c][7] || primary[c][8]); |
| 459 | let go = (0..CORES).all(|c| primary[c][0]); |
| 460 | let nogo = noninf_fail.iter().any(|&f| f); |
| 461 | let verdict = if invalid { |
| 462 | "INVALID" |
| 463 | } else if go { |
| 464 | "GO" |
| 465 | } else if nogo { |
| 466 | "NO-GO" |
| 467 | } else { |
| 468 | "INCONCLUSIVE" |
| 469 | }; |
| 470 | println!("ORIG3_V2_VERDICT={verdict}"); |
| 471 | } |