aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / allen-reflex / examples / route_exam.rs
| 1 | //! Score the interactive router (aienos#399) on one route family file. |
| 2 | //! |
| 3 | //! cargo run --release -p allen-reflex --example route_exam -- <family.psv> [--sweep] |
| 4 | //! |
| 5 | //! Each core trains on the exam train split + extra cases only (as the Pi image), learning off. |
| 6 | //! Context per row: speaker and flags from the row (C consent, P pending); caps all present, |
| 7 | //! installed model "tinyllama" (the scoring context written in the family header). |
| 8 | //! Prints one `ROUTE <mode> <id> ok|MISS expected=<route>/<then> got=<route>/<then> why=<why>` |
| 9 | //! line per case and one `ROUTE_SUM` line per mode. `--sweep` (dev only) also prints the |
| 10 | //! accuracy for a grid of similarity thresholds, so the frozen calibration can be chosen. |
| 11 | #[path = "common/mod.rs"] |
| 12 | mod common; |
| 13 | use allen_reflex::route::{ |
| 14 | pct_decode, propose, Calibration, Caps, Route, RouteCtx, RouteRequest, Speaker, |
| 15 | }; |
| 16 | use allen_reflex::{exam_turns, Config, Mode}; |
| 17 | use common::*; |
| 18 | |
| 19 | struct Row { |
| 20 | id: String, |
| 21 | family: String, |
| 22 | speaker: Speaker, |
| 23 | consent: bool, |
| 24 | pending: bool, |
| 25 | route: Route, |
| 26 | then: String, |
| 27 | turns: String, |
| 28 | } |
| 29 | |
| 30 | fn load(path: &str) -> Vec<Row> { |
| 31 | let src = std::fs::read_to_string(path).expect("read family"); |
| 32 | let mut out = Vec::new(); |
| 33 | for l in src.lines() { |
| 34 | if l.starts_with('#') || l.is_empty() { |
| 35 | continue; |
| 36 | } |
| 37 | let c: Vec<&str> = l.splitn(7, '|').collect(); |
| 38 | assert_eq!(c.len(), 7, "bad row {l}"); |
| 39 | out.push(Row { |
| 40 | id: c[0].into(), |
| 41 | family: c[1].into(), |
| 42 | speaker: match c[2] { |
| 43 | "owner" => Speaker::Owner, |
| 44 | "guest" => Speaker::Guest, |
| 45 | s => panic!("bad speaker {s}"), |
| 46 | }, |
| 47 | consent: c[3].contains('C'), |
| 48 | pending: c[3].contains('P'), |
| 49 | route: Route::from_name(c[4]).unwrap_or_else(|| panic!("bad route {}", c[4])), |
| 50 | then: c[5].into(), |
| 51 | turns: pct_decode(c[6]).expect("bad escape"), |
| 52 | }); |
| 53 | } |
| 54 | out |
| 55 | } |
| 56 | |
| 57 | fn score( |
| 58 | rows: &[Row], |
| 59 | r: &allen_reflex::Reflex, |
| 60 | mode: Mode, |
| 61 | cal: Calibration, |
| 62 | print: bool, |
| 63 | ) -> (usize, usize) { |
| 64 | let mut ok = 0; |
| 65 | let (mut abst_true, mut abst_pred, mut abst_hit) = (0, 0, 0); |
| 66 | for (i, row) in rows.iter().enumerate() { |
| 67 | let t = exam_turns(&row.turns); |
| 68 | let ctx = RouteCtx { |
| 69 | speaker: row.speaker, |
| 70 | memory_consent: row.consent, |
| 71 | pending: row.pending, |
| 72 | caps: Caps::from_bits(15), |
| 73 | installed_model: Some("tinyllama"), |
| 74 | subject_id: [0; 32], |
| 75 | cal, |
| 76 | }; |
| 77 | let p = propose( |
| 78 | r, |
| 79 | &RouteRequest { |
| 80 | request_id: i as u64 + 1, |
| 81 | session_id: 1, |
| 82 | turns: &t, |
| 83 | }, |
| 84 | &ctx, |
| 85 | ); |
| 86 | let then = p.then.map(|t| t.name()).unwrap_or("-"); |
| 87 | let hit = p.route == row.route && then == row.then; |
| 88 | ok += hit as usize; |
| 89 | abst_true += (row.route == Route::Clarify) as usize; |
| 90 | abst_pred += (p.route == Route::Clarify) as usize; |
| 91 | abst_hit += (row.route == Route::Clarify && p.route == Route::Clarify) as usize; |
| 92 | if print { |
| 93 | println!( |
| 94 | "ROUTE {} {} {} family={} expected={}/{} got={}/{} why={} label={}", |
| 95 | mode.name(), |
| 96 | row.id, |
| 97 | if hit { "ok" } else { "MISS" }, |
| 98 | row.family, |
| 99 | row.route.name(), |
| 100 | row.then, |
| 101 | p.route.name(), |
| 102 | then, |
| 103 | p.why.name(), |
| 104 | p.label.name() |
| 105 | ); |
| 106 | } |
| 107 | } |
| 108 | if print { |
| 109 | let n = rows.len(); |
| 110 | let answered = n - abst_pred; |
| 111 | println!( |
| 112 | "ROUTE_SUM mode={} cases={} correct={} coverage={}/{} clarify_expected={} clarify_given={} clarify_both={} min_sim={} min_margin={}", |
| 113 | mode.name(), |
| 114 | n, |
| 115 | ok, |
| 116 | answered, |
| 117 | n, |
| 118 | abst_true, |
| 119 | abst_pred, |
| 120 | abst_hit, |
| 121 | cal.min_sim, |
| 122 | cal.min_margin |
| 123 | ); |
| 124 | } |
| 125 | (ok, rows.len()) |
| 126 | } |
| 127 | |
| 128 | fn main() { |
| 129 | let args: Vec<String> = std::env::args().collect(); |
| 130 | let rows = load(args.get(1).expect("family file")); |
| 131 | let sweep = args.iter().any(|a| a == "--sweep"); |
| 132 | let pool = pool(); |
| 133 | let all: Vec<&Case> = pool.iter().collect(); |
| 134 | for mode in [Mode::Reflex, Mode::Nearest, Mode::Perceptron] { |
| 135 | let mut r = build(Config::frozen(mode), &all); |
| 136 | r.set_learning(false); |
| 137 | score(&rows, &r, mode, Calibration::frozen(mode), true); |
| 138 | if sweep { |
| 139 | for k in 0..=12 { |
| 140 | let s = k as f32 * 0.05; |
| 141 | let (ok, n) = score( |
| 142 | &rows, |
| 143 | &r, |
| 144 | mode, |
| 145 | Calibration { |
| 146 | min_sim: s, |
| 147 | min_margin: f32::MIN, |
| 148 | }, |
| 149 | false, |
| 150 | ); |
| 151 | println!( |
| 152 | "ROUTE_SWEEP mode={} min_sim={:.2} correct={}/{}", |
| 153 | mode.name(), |
| 154 | s, |
| 155 | ok, |
| 156 | n |
| 157 | ); |
| 158 | } |
| 159 | for m in [0.0f32, 0.05, 0.1, 0.2, 0.3, 0.5, 1.0] { |
| 160 | let (ok, n) = score( |
| 161 | &rows, |
| 162 | &r, |
| 163 | mode, |
| 164 | Calibration { |
| 165 | min_sim: -1.0, |
| 166 | min_margin: m, |
| 167 | }, |
| 168 | false, |
| 169 | ); |
| 170 | println!( |
| 171 | "ROUTE_SWEEP mode={} min_margin={:.2} correct={}/{}", |
| 172 | mode.name(), |
| 173 | m, |
| 174 | ok, |
| 175 | n |
| 176 | ); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |