aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-inference-abi / src / bin / q36_router_check.rs
| 1 | //! The Qwen3.6-35B-A3B router alone on the chip against the host reference (L306 gate 1 probe). |
| 2 | //! |
| 3 | //! q36_router_check <checkpoint-dir> <golden.json> <out.json> |
| 4 | //! |
| 5 | //! The host reference runs the golden sequence (prompt plus every generated token but the last) |
| 6 | //! and keeps each MoE layer's input residual; the chip's router (`omega_gpu_router`, the kernel the |
| 7 | //! decode graph records) then routes exactly those rows, layer by layer. Per row: the 8 routed ids |
| 8 | //! as sets, the routed weights and the shared expert gate (slot 8) against the reference. A pick |
| 9 | //! that differs is reported with the reference's gap between its 8th and 9th logit, so a near tie |
| 10 | //! (allowed) is told apart from a wrong pick (a bug). Control: the chip's ids for row r of layer l |
| 11 | //! must not match row r + 1's (the check can say no). |
| 12 | use std::path::Path; |
| 13 | |
| 14 | use aien_inference_abi::qwen38::{Logits, Qwen38Reference}; |
| 15 | use aien_inference_abi::qwen38_omega::router_probe; |
| 16 | use serde_json::{json, Value}; |
| 17 | |
| 18 | /// A weight is right when it is within this of the reference's (f32 softmax over 256 logits). |
| 19 | const WEIGHT_TOL: f32 = 1e-4; |
| 20 | /// A differing pick is a near tie when the reference's 8th/9th logit gap is under this. |
| 21 | const TIE_GAP: f32 = 1e-3; |
| 22 | |
| 23 | fn main() { |
| 24 | if let Err(e) = run() { |
| 25 | eprintln!("q36_router_check: {e}"); |
| 26 | std::process::exit(2); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | fn run() -> Result<(), String> { |
| 31 | let args: Vec<String> = std::env::args().collect(); |
| 32 | if args.len() != 4 { |
| 33 | return Err("usage: q36_router_check <checkpoint-dir> <golden.json> <out.json>".into()); |
| 34 | } |
| 35 | let dir = Path::new(&args[1]); |
| 36 | let g: Value = serde_json::from_str( |
| 37 | &std::fs::read_to_string(&args[2]).map_err(|e| format!("{}: {e}", args[2]))?, |
| 38 | ) |
| 39 | .map_err(|e| format!("{}: {e}", args[2]))?; |
| 40 | let ids_of = |k: &str| -> Vec<u32> { |
| 41 | g[k].as_array() |
| 42 | .map(|a| { |
| 43 | a.iter() |
| 44 | .filter_map(|v| v.as_u64()) |
| 45 | .map(|v| v as u32) |
| 46 | .collect() |
| 47 | }) |
| 48 | .unwrap_or_default() |
| 49 | }; |
| 50 | let mut toks = ids_of("prompt_ids"); |
| 51 | let gen = ids_of("generated_ids"); |
| 52 | toks.extend_from_slice(&gen[..gen.len().saturating_sub(1)]); |
| 53 | let t = toks.len(); |
| 54 | |
| 55 | let mut model = Qwen38Reference::open(dir)?; |
| 56 | model.moe_capture = true; |
| 57 | let mut state = model.new_state(); |
| 58 | model.forward(&mut state, &toks, Logits::Last)?; |
| 59 | let c = model.config.clone(); |
| 60 | let top_k = c.moe.as_ref().ok_or("not a MoE checkpoint")?.top_k; |
| 61 | let h = c.hidden; |
| 62 | eprintln!( |
| 63 | "reference: {t} positions, {} layers captured", |
| 64 | model.moe_x.len() |
| 65 | ); |
| 66 | |
| 67 | let (mut router, _) = router_probe(dir)?; |
| 68 | let slots = router.slots(); |
| 69 | let (mut same, mut tie, mut wrong, mut wmax, mut gmax, mut ctl_same) = |
| 70 | (0usize, 0usize, 0usize, 0f32, 0f32, 0usize); |
| 71 | let mut bad = Vec::new(); |
| 72 | let mut prev: Option<Vec<u32>> = None; |
| 73 | for l in 0..c.layers { |
| 74 | for r in 0..t { |
| 75 | let x = &model.moe_x[l][r * h..(r + 1) * h]; |
| 76 | let (mut ids, mut wts) = (vec![0u32; slots], vec![0f32; slots]); |
| 77 | router |
| 78 | .route(l, x, c.eps, true, 1.0, &mut ids, &mut wts) |
| 79 | .map_err(|e| format!("layer {l} row {r}: router: {e:?}"))?; |
| 80 | let want: Vec<u32> = model.routes[l][r * top_k..(r + 1) * top_k] |
| 81 | .iter() |
| 82 | .map(|&e| e as u32) |
| 83 | .collect(); |
| 84 | let want_w = &model.route_wts[l][r * top_k..(r + 1) * top_k]; |
| 85 | let gap = model.route_gaps[l][r]; |
| 86 | let mut a = ids[..top_k].to_vec(); |
| 87 | let mut b = want.clone(); |
| 88 | a.sort_unstable(); |
| 89 | b.sort_unstable(); |
| 90 | let sg = (wts[top_k] - model.shared_g[l][r]).abs(); |
| 91 | gmax = gmax.max(sg); |
| 92 | if let Some(p) = &prev { |
| 93 | if *p == a { |
| 94 | ctl_same += 1; |
| 95 | } |
| 96 | } |
| 97 | prev = Some(a.clone()); |
| 98 | if a == b { |
| 99 | same += 1; |
| 100 | for (k, &e) in want.iter().enumerate() { |
| 101 | let j = ids[..top_k].iter().position(|&v| v == e).unwrap_or(0); |
| 102 | wmax = wmax.max((wts[j] - want_w[k]).abs()); |
| 103 | } |
| 104 | } else if gap < TIE_GAP { |
| 105 | tie += 1; |
| 106 | } else { |
| 107 | wrong += 1; |
| 108 | if bad.len() < 40 { |
| 109 | bad.push( |
| 110 | json!({"layer": l, "row": r, "chip": ids[..top_k], "chip_w": wts[..top_k], |
| 111 | "ref": want, "ref_w": want_w, "gap": gap}), |
| 112 | ); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | let rows = c.layers * t; |
| 118 | let pass = wrong == 0 && wmax < WEIGHT_TOL && gmax < WEIGHT_TOL && ctl_same < rows / 10; |
| 119 | let line = format!( |
| 120 | "Q36_ROUTER layers {} rows {rows}: same picks {same}, near ties {tie} (gap < {TIE_GAP}), wrong {wrong}; \ |
| 121 | routed weight max diff {wmax:.2e}, shared gate max diff {gmax:.2e}; control (row vs next row same) {ctl_same} -> {}", |
| 122 | c.layers, |
| 123 | if pass { "PASS" } else { "FAIL" } |
| 124 | ); |
| 125 | println!("{line}"); |
| 126 | let out = json!({"golden": args[2], "positions": t, "layers": c.layers, "same": same, "near_ties": tie, |
| 127 | "wrong": wrong, "weight_max_diff": wmax, "shared_gate_max_diff": gmax, "control_same": ctl_same, |
| 128 | "pass": pass, "wrong_rows": bad, "summary": line}); |
| 129 | std::fs::write( |
| 130 | &args[3], |
| 131 | serde_json::to_string_pretty(&out).map_err(|e| e.to_string())?, |
| 132 | ) |
| 133 | .map_err(|e| format!("{}: {e}", args[3]))?; |
| 134 | if !pass { |
| 135 | std::process::exit(1); |
| 136 | } |
| 137 | Ok(()) |
| 138 | } |