interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aien-sovereign-core-mv / crates / aien-inference-runtime / tests / logit_drift.rs

116 lines · 4589 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//! Teacher-forced logit drift between the CPU reference and the Omega backend, printed
17//! per step (diagnostic), plus host tests proving the drift judge used by the strict gate
18//! rejects what it must.
19//! AIEN_E2E_CHECKPOINT=~/models/TinyLlama-1.1B-Chat-v1.0 \
20//! cargo test -p aien-inference-runtime --release --test logit_drift -- --ignored --nocapture
21mod drift;
22
23use aien_inference_runtime::model::EmbeddedModel;
24use drift::{compare, judge, logits_per_step, top2, StepCmp, MAX_ABS_DLOGIT};
25use std::path::{Path, PathBuf};
26
27fn step(ref_tok: usize, ref_margin: f32, cand_tok: usize, d: f32) -> StepCmp {
28 StepCmp {
29 step: 0,
30 ref_tok,
31 ref_margin,
32 cand_tok,
33 max_abs_dlogit: d,
34 }
35}
36
37#[test]
38fn judge_accepts_agreement_and_explained_near_tie() {
39 assert_eq!(judge(&[step(5, 0.3, 5, 0.04)]), Ok(0));
40 // the measured cut 3d case: margin 0.0005, difference 0.0314
41 assert_eq!(judge(&[step(9068, 0.0005, 3370, 0.0314)]), Ok(1));
42}
43
44#[test]
45fn judge_rejects_unexplained_flip_and_large_drift() {
46 // negative controls: a flip the measured difference cannot explain, and drift over the bound
47 assert!(judge(&[step(9068, 1.0, 3370, 0.03)]).is_err());
48 assert!(judge(&[step(5, 0.3, 5, MAX_ABS_DLOGIT * 2.0)]).is_err());
49 assert!(judge(&[step(5, 0.3, 5, f32::NAN)]).is_err());
50}
51
52#[test]
53fn compare_finds_flip_margin_and_difference() {
54 let r = vec![vec![0.0, 2.0, 1.9]];
55 let c = vec![vec![0.0, 1.9, 2.0]];
56 let s = &compare(&r, &c)[0];
57 assert_eq!((s.ref_tok, s.cand_tok), (1, 2));
58 assert!((s.ref_margin - 0.1).abs() < 1e-6 && (s.max_abs_dlogit - 0.1).abs() < 1e-6);
59}
60
61#[test]
62#[ignore = "needs the real checkpoint and the GB10 with a native Omega build; heavy queue only"]
63fn teacher_forced_logit_drift() {
64 let ckpt = PathBuf::from(std::env::var("AIEN_E2E_CHECKPOINT").expect("AIEN_E2E_CHECKPOINT"));
65 let dir = if ckpt.is_dir() {
66 ckpt.clone()
67 } else {
68 ckpt.parent().map(Path::to_path_buf).unwrap_or_default()
69 };
70 let (model_path, tokenizer_path) = (dir.join("model.safetensors"), dir.join("tokenizer.json"));
71 let prompt = "The DGX Spark is a small computer with a large";
72 let steps = 32usize;
73
74 let mut cpu = EmbeddedModel::load_checkpoint(&model_path, &tokenizer_path, false, true)
75 .expect("reference load");
76 let ptoks = cpu.tokenizer.encode(prompt).expect("encode");
77 let (ref_logits, ref_toks) = logits_per_step(&mut cpu.transformer, &ptoks, steps, &[]);
78 let tok = cpu.tokenizer;
79 drop(cpu.transformer);
80
81 let mut om = EmbeddedModel::load_checkpoint(&model_path, &tokenizer_path, true, true)
82 .expect("omega load");
83 println!(
84 "DRIFT omega backend: {}",
85 om.transformer.tensor_backend.name()
86 );
87 let (om_logits, _) = logits_per_step(&mut om.transformer, &ptoks, steps, &ref_toks);
88 println!("DRIFT {}", om.transformer.tensor_backend.op_report().line());
89
90 let cmp = compare(&ref_logits, &om_logits);
91 for (s, c) in cmp.iter().enumerate() {
92 let (_, _, r2, _) = top2(&ref_logits[s]);
93 println!(
94 "DRIFT step={s:2} ref_tok={:5} {:?} ref_margin={:.4} omega_tok={:5} max_abs_dlogit={:.4} ref2={r2} {:?}{}",
95 c.ref_tok,
96 tok.decode(&[c.ref_tok as u32]).unwrap_or_default(),
97 c.ref_margin,
98 c.cand_tok,
99 c.max_abs_dlogit,
100 tok.decode(&[r2 as u32]).unwrap_or_default(),
101 if c.ref_tok != c.cand_tok { " <-- FLIP" } else { "" }
102 );
103 }
104 let worst = cmp.iter().map(|c| c.max_abs_dlogit).fold(0.0f32, f32::max);
105 println!(
106 "DRIFT summary steps={steps} worst_max_abs_dlogit={worst:.4} judge={:?}",
107 judge(&cmp)
108 );
109}
110
111#[test]
112fn compare_keeps_nan_so_the_judge_fails_it() {
113 let s = compare(&[vec![1.0, 0.0]], &[vec![f32::NAN, 0.0]]);
114 assert!(s[0].max_abs_dlogit.is_nan());
115 assert!(judge(&s).is_err());
116}