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-runtime / tests / drift / mod.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 | //! Teacher-forced logit comparison shared by the strict gate and the drift diagnostic. |
| 17 | //! |
| 18 | //! Both backends are fed the reference's own greedy tokens, so every step compares |
| 19 | //! logits for the same context. A greedy text comparison cannot tell a wrong backend |
| 20 | //! from a near-tie: TinyLlama on the gate prompt has a step where "processing" leads |
| 21 | //! "memory" by 0.0005 logits while the f32 backends differ by ~0.03 everywhere |
| 22 | //! (evidence-out/FB1-CUT3D-02dfa2e-drift, 2026-10-04). |
| 23 | #![allow(dead_code)] |
| 24 | use aien_inference_abi::transformer_backend::NativeTransformerBackend; |
| 25 | use aien_inference_abi::SequenceState; |
| 26 | |
| 27 | /// Largest allowed |logit difference| at any step. The cut 3d run measured 0.059 worst |
| 28 | /// over 32 steps (32000 logits each); the bound leaves 2.5x headroom. |
| 29 | pub const MAX_ABS_DLOGIT: f32 = 0.15; |
| 30 | |
| 31 | pub fn top2(l: &[f32]) -> (usize, f32, usize, f32) { |
| 32 | let (mut i1, mut v1, mut i2, mut v2) = (0usize, f32::NEG_INFINITY, 0usize, f32::NEG_INFINITY); |
| 33 | for (i, &v) in l.iter().enumerate() { |
| 34 | if v > v1 { |
| 35 | (i2, v2) = (i1, v1); |
| 36 | (i1, v1) = (i, v); |
| 37 | } else if v > v2 { |
| 38 | (i2, v2) = (i, v); |
| 39 | } |
| 40 | } |
| 41 | (i1, v1, i2, v2) |
| 42 | } |
| 43 | |
| 44 | /// Logits for every step: step 0 from prefill, step s from decoding `forced[s-1]`. |
| 45 | /// With `forced` empty the model's own argmax is fed back; the fed tokens are returned. |
| 46 | pub fn logits_per_step( |
| 47 | t: &mut NativeTransformerBackend, |
| 48 | prompt: &[u32], |
| 49 | steps: usize, |
| 50 | forced: &[u32], |
| 51 | ) -> (Vec<Vec<f32>>, Vec<u32>) { |
| 52 | let seq_id = 0x10_9175_u64; |
| 53 | let mut out = vec![t.prefill_sequence(seq_id, prompt).expect("prefill")]; |
| 54 | let mut toks = Vec::new(); |
| 55 | for s in 0..steps { |
| 56 | let tok = if forced.is_empty() { |
| 57 | top2(&out[s]).0 as u32 |
| 58 | } else { |
| 59 | forced[s] |
| 60 | }; |
| 61 | toks.push(tok); |
| 62 | if s + 1 == steps { |
| 63 | break; |
| 64 | } |
| 65 | let seq: &mut SequenceState = t.sequences.get_mut(&seq_id).expect("sequence"); |
| 66 | seq.tokens.push(tok); |
| 67 | let pos = seq.tokens.len() - 1; |
| 68 | let hidden = NativeTransformerBackend::try_forward_token_impl_paged( |
| 69 | &t.weights, |
| 70 | &*t.tensor_backend, |
| 71 | tok, |
| 72 | pos, |
| 73 | seq, |
| 74 | seq_id, |
| 75 | t.kv_manager.as_ref(), |
| 76 | ) |
| 77 | .expect("decode"); |
| 78 | out.push(NativeTransformerBackend::compute_logits_impl( |
| 79 | &t.weights, |
| 80 | &*t.tensor_backend, |
| 81 | &hidden, |
| 82 | )); |
| 83 | } |
| 84 | t.release_sequence(seq_id); |
| 85 | (out, toks) |
| 86 | } |
| 87 | |
| 88 | pub struct StepCmp { |
| 89 | pub step: usize, |
| 90 | pub ref_tok: usize, |
| 91 | pub ref_margin: f32, |
| 92 | pub cand_tok: usize, |
| 93 | pub max_abs_dlogit: f32, |
| 94 | } |
| 95 | |
| 96 | pub fn compare(reference: &[Vec<f32>], candidate: &[Vec<f32>]) -> Vec<StepCmp> { |
| 97 | reference |
| 98 | .iter() |
| 99 | .zip(candidate) |
| 100 | .enumerate() |
| 101 | .map(|(step, (r, c))| { |
| 102 | let (r1, rv1, _, rv2) = top2(r); |
| 103 | let d = r |
| 104 | .iter() |
| 105 | .zip(c) |
| 106 | .map(|(a, b)| (a - b).abs()) |
| 107 | .fold(0.0f32, |m, x| { |
| 108 | if m.is_nan() || x.is_nan() { |
| 109 | f32::NAN |
| 110 | } else { |
| 111 | m.max(x) |
| 112 | } |
| 113 | }); // a NaN logit must fail the judge, f32::max would drop it |
| 114 | StepCmp { |
| 115 | step, |
| 116 | ref_tok: r1, |
| 117 | ref_margin: rv1 - rv2, |
| 118 | cand_tok: top2(c).0, |
| 119 | max_abs_dlogit: d, |
| 120 | } |
| 121 | }) |
| 122 | .collect() |
| 123 | } |
| 124 | |
| 125 | /// Pass when every step stays within `MAX_ABS_DLOGIT` and every argmax disagreement is a |
| 126 | /// near-tie the measured difference can explain: a flip needs the top-two gap to move by |
| 127 | /// at least the reference margin, and that gap moves by at most 2 * max_abs_dlogit. |
| 128 | pub fn judge(steps: &[StepCmp]) -> Result<usize, String> { |
| 129 | let mut flips = 0; |
| 130 | for s in steps { |
| 131 | if s.max_abs_dlogit.is_nan() || s.max_abs_dlogit > MAX_ABS_DLOGIT { |
| 132 | return Err(format!( |
| 133 | "step {}: max |dlogit| {} exceeds {MAX_ABS_DLOGIT}", |
| 134 | s.step, s.max_abs_dlogit |
| 135 | )); |
| 136 | } |
| 137 | if s.cand_tok != s.ref_tok { |
| 138 | if s.ref_margin > 2.0 * s.max_abs_dlogit { |
| 139 | return Err(format!( |
| 140 | "step {}: argmax {} vs reference {} with reference margin {} > 2 * max |dlogit| {}", |
| 141 | s.step, s.cand_tok, s.ref_tok, s.ref_margin, s.max_abs_dlogit |
| 142 | )); |
| 143 | } |
| 144 | flips += 1; |
| 145 | } |
| 146 | } |
| 147 | Ok(flips) |
| 148 | } |