Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/crates/allen-reflex/examples/tune.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / crates / allen-reflex / examples / tune.rs` 195 lines ยท 7274 bytes Raw Blame History 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 //! Tuning on dev and on family-grouped cross-validation of the training pool. 17 //! usage: cargo run --release -p allen-reflex --example tune 18 //! Never reads held-out. Every knob is moved one at a time from Config::frozen, and each 19 //! setting is scored on (a) dev, trained on the whole pool, and (b) 10-fold cross-validation 20 //! of the pool where a train case and all its paraphrases (one family) sit in the same fold. 21 //! Then a risk-coverage table for the abstain thresholds (margin, neighbour agreement). 22 #[path = "common/mod.rs"] 23 mod common; 24 use allen_reflex::{exam_turns, Action, Config, Mode}; 25 use common::*; 26 27 /// Fold of a case: train case Dxxx shares the fold of its paraphrase family F-Dxxx. 28 fn fold(c: &Case) -> usize { 29 let key = if c.family.starts_with('D') { 30 format!("F-{}", c.family) 31 } else { 32 c.family.clone() 33 }; 34 let mut h: u64 = 0xcbf2_9ce4_8422_2325; 35 for b in key.bytes() { 36 h ^= b as u64; 37 h = h.wrapping_mul(0x100_0000_01b3); 38 } 39 (h % 10) as usize 40 } 41 42 /// (action ok, exact ok, n) on `test` for a core trained on `train`. 43 fn run(cfg: Config, train: &[&Case], test: &[&Case]) -> (usize, usize, usize) { 44 let mut core = build(cfg, train); 45 core.set_learning(false); 46 let (mut a, mut e) = (0, 0); 47 for c in test { 48 let (d, _) = core.decide(&exam_turns(&c.turns)); 49 let (x, y) = judge(c, &d); 50 a += x as usize; 51 e += y as usize; 52 } 53 (a, e, test.len()) 54 } 55 56 fn cv(cfg: Config, pool: &[Case]) -> (usize, usize, usize) { 57 let mut t = (0, 0, 0); 58 for k in 0..10 { 59 let tr: Vec<&Case> = pool.iter().filter(|c| fold(c) != k).collect(); 60 let te: Vec<&Case> = pool.iter().filter(|c| fold(c) == k).collect(); 61 let r = run(cfg, &tr, &te); 62 t = (t.0 + r.0, t.1 + r.1, t.2 + r.2); 63 } 64 t 65 } 66 67 fn line(name: &str, cfg: Config, pool: &[Case], dev: &[Case]) { 68 let all: Vec<&Case> = pool.iter().collect(); 69 let dv: Vec<&Case> = dev.iter().collect(); 70 let d = run(cfg, &all, &dv); 71 let c = cv(cfg, pool); 72 println!( 73 "{:<10} {:<28} dev action {:>2}/{} exact {:>2}/{} cv action {:>3}/{} exact {:>3}/{}", 74 cfg.mode.name(), 75 name, 76 d.0, 77 d.2, 78 d.1, 79 d.2, 80 c.0, 81 c.2, 82 c.1, 83 c.2 84 ); 85 } 86 87 fn main() { 88 let pool = pool(); 89 let dev = load("dev"); 90 let only = std::env::args().nth(1).unwrap_or_default(); 91 if only.is_empty() || only == "grid" { 92 for mode in [Mode::Reflex, Mode::Nearest, Mode::Perceptron] { 93 let f = Config::frozen(mode); 94 line("frozen", f, &pool, &dev); 95 if mode == Mode::Perceptron { 96 for ep in [5, 10, 40] { 97 let mut c = f; 98 c.perceptron_epochs = ep; 99 line(&format!("epochs={ep}"), c, &pool, &dev); 100 } 101 } 102 for k in [1, 5] { 103 if mode == Mode::Perceptron { 104 break; 105 } 106 let mut c = f; 107 c.hv.k = k; 108 line(&format!("k={k}"), c, &pool, &dev); 109 } 110 for ch in 0..8 { 111 for w in [0u16, 6] { 112 let mut c = f; 113 c.weights[ch] = w; 114 line(&format!("weight[{ch}]={w}"), c, &pool, &dev); 115 } 116 } 117 if mode != Mode::Reflex { 118 continue; 119 } 120 for words in [64, 256] { 121 let mut c = f; 122 c.hv.words = words; 123 line(&format!("D={}", words * 64), c, &pool, &dev); 124 } 125 for alpha in [0.0, 0.25, 0.75, 1.0] { 126 let mut c = f; 127 c.hv.alpha = alpha; 128 line(&format!("alpha={alpha}"), c, &pool, &dev); 129 } 130 for zb in [0.0, 0.8] { 131 let mut c = f; 132 c.hv.zero_band = zb; 133 line(&format!("zero_band={zb}"), c, &pool, &dev); 134 } 135 for (ep, eta) in [(0, 0.5), (10, 0.5), (40, 0.05), (40, 1.0)] { 136 let mut c = f; 137 c.hv.epochs = ep; 138 c.hv.eta = eta; 139 line(&format!("epochs={ep} eta={eta}"), c, &pool, &dev); 140 } 141 } 142 } 143 if only.is_empty() || only == "risk" { 144 // risk-coverage: pooled over cross-validation folds and dev 145 for mode in [Mode::Reflex, Mode::Nearest, Mode::Perceptron] { 146 let f = Config::frozen(mode); 147 let mut rows: Vec<(f32, f32, bool, bool, bool)> = Vec::new(); // margin, agree, correct, exp clarify, src dev 148 let mut collect = |train: Vec<&Case>, test: Vec<&Case>, is_dev: bool| { 149 let mut core = build(f, &train); 150 core.set_learning(false); 151 for c in test { 152 let (d, e) = core.decide(&exam_turns(&c.turns)); 153 if !matches!(e.why, allen_reflex::Why::Scored) { 154 continue; 155 } 156 let ok = judge(c, &d).1; 157 rows.push((e.margin, e.agree, ok, c.action == Action::Clarify, is_dev)); 158 } 159 }; 160 for k in 0..10 { 161 collect( 162 pool.iter().filter(|c| fold(c) != k).collect(), 163 pool.iter().filter(|c| fold(c) == k).collect(), 164 false, 165 ); 166 } 167 collect(pool.iter().collect(), dev.iter().collect(), true); 168 println!( 169 "{} risk-coverage over {} scored decisions (cv + dev)", 170 f.mode.name(), 171 rows.len() 172 ); 173 println!(" t_margin t_agree answered wrong_answered exact_if_abstain_is_clarify"); 174 for tm in [0.0f32, 0.005, 0.01, 0.02, 0.05, 0.1] { 175 for ta in [0.0f32, 0.5, 0.7] { 176 let (mut ans, mut wrong, mut exact) = (0, 0, 0); 177 for r in &rows { 178 let abstain = r.0 < tm || r.1 < ta; 179 if abstain { 180 exact += r.3 as usize; 181 } else { 182 ans += 1; 183 wrong += !r.2 as usize; 184 exact += r.2 as usize; 185 } 186 } 187 println!( 188 " {tm:<8} {ta:<7} {ans:>8} {wrong:>14} {exact:>5}/{}", 189 rows.len() 190 ); 191 } 192 } 193 } 194 } 195 }