use memra_engine::cache::Cache;
use memra_engine::hybrid::HybridModel;
use memra_engine::Engine;
use memra_gguf::GgufFile;
use memra_tokenizer::Tokenizer;
fn top2(l: &[f32]) -> (usize, f32, usize, f32) {
let (mut i1, mut v1, mut i2, mut v2) = (0usize, f32::NEG_INFINITY, 0usize, f32::NEG_INFINITY);
for (i, &v) in l.iter().enumerate() {
if v > v1 {
i2 = i1;
v2 = v1;
i1 = i;
v1 = v;
} else if v > v2 {
i2 = i;
v2 = v;
}
}
(i1, v1, i2, v2)
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<String> = std::env::args().skip(1).collect();
let model_path = args
.first()
.expect("usage: argmax-margin-probe <model.gguf> <prompt-file> [window]");
let prompt_file = args.get(1).expect("need <prompt-file>");
let window: usize = args.get(2).and_then(|s| s.parse().ok()).unwrap_or(24);
let e = Engine::new(0)?;
let g = GgufFile::open(model_path)?;
let model = HybridModel::load_without_mtp(&e, &g)?;
let tok = Tokenizer::from_gguf(&g).map_err(|err| format!("tokenizer: {err}"))?;
let text = std::fs::read_to_string(prompt_file)?;
let prompt = tok.encode(&text, true);
let t = prompt.len();
assert!(t > window + 2, "prompt shorter than the window");
println!(
"argmax-margin-probe: T={t} window={window} sm_count={} model={}",
e.sm_count(),
model_path
);
println!(
"env: MEMRA_FA_SPLIT={} MEMRA_Q80_G2={} MEMRA_FAST={} MEMRA_PRIME_CHUNK={}",
std::env::var("MEMRA_FA_SPLIT").unwrap_or_else(|_| "<unset>".into()),
std::env::var("MEMRA_Q80_G2").unwrap_or_else(|_| "<unset>".into()),
std::env::var("MEMRA_FAST").unwrap_or_else(|_| "<unset>".into()),
std::env::var("MEMRA_PRIME_CHUNK").unwrap_or_else(|_| "<unset>".into()),
);
let mut cache = Cache::new(&e, &model.cfg, t + 8)?;
let mut dec_at: Vec<Vec<f32>> = Vec::with_capacity(window);
for (i, &tk) in prompt.iter().enumerate() {
let l = model.decode_step(&e, tk, &mut cache)?;
if i + window >= t {
dec_at.push(l);
}
}
let mut pre_at: Vec<Vec<f32>> = Vec::with_capacity(window);
for i in (t - window)..t {
pre_at.push(model.forward_last(&e, &prompt[..=i])?);
}
println!("\npos prefill_top1 margin_p decode_top1 margin_d delta@ids agree");
let mut margins_d: Vec<f32> = Vec::with_capacity(window);
let mut margins_p: Vec<f32> = Vec::with_capacity(window);
let mut n_agree = 0usize;
let mut last_row: Option<(usize, usize, f32, f32, f32, f32)> = None;
for k in 0..window {
let pos = t - window + k;
let (p1, pv1, p2, pv2) = top2(&pre_at[k]);
let (d1, dv1, d2, dv2) = top2(&dec_at[k]);
let mp = pv1 - pv2;
let md = dv1 - dv2;
margins_p.push(mp);
margins_d.push(md);
let ids = [p1, p2, d1, d2];
let delta = ids
.iter()
.map(|&i| (pre_at[k][i] - dec_at[k][i]).abs())
.fold(0.0f32, f32::max);
let agree = p1 == d1;
if agree {
n_agree += 1;
}
println!(
"{pos:<8} {p1:<13} {mp:<12.4} {d1:<12} {md:<12.4} {delta:<14.4} {}",
if agree { "yes" } else { "NO <-- FLIP" }
);
if k == window - 1 {
last_row = Some((p1, d1, mp, md, delta, (pre_at[k][p1] - dec_at[k][p1]).abs()));
}
}
let mut sorted = margins_d.clone();
sorted.sort_by(|a, b| a.total_cmp(b));
let pct = |q: f64| sorted[((sorted.len() - 1) as f64 * q).round() as usize];
let (fp1, fd1, fmp, fmd, fdelta, _) = last_row.expect("window >= 1");
let below = margins_d.iter().filter(|&&m| m < fmd).count();
println!(
"\nmargin distribution (decode config, {} positions): min {:.4} p10 {:.4} p50 {:.4} p90 {:.4} max {:.4}",
sorted.len(), sorted[0], pct(0.10), pct(0.50), pct(0.90), sorted[sorted.len() - 1]
);
let mut sp = margins_p.clone();
sp.sort_by(|a, b| a.total_cmp(b));
println!(
"margin distribution (prefill config): min {:.4} p50 {:.4} max {:.4}",
sp[0],
sp[sp.len() / 2],
sp[sp.len() - 1]
);
println!(
"DECISION POSITION (the gate's): prefill_top1={fp1} decode_top1={fd1} margin_p={fmp:.4} \
margin_d={fmd:.4} config_delta_at_ids={fdelta:.4}"
);
println!(
" -> the decision margin is BELOW {}/{} sampled positions' margins (rank {} of {})",
below,
margins_d.len(),
below + 1,
margins_d.len()
);
println!(
" -> flip is ARITHMETICALLY POSSIBLE iff config_delta > margin: {:.4} > {:.4} = {}",
fdelta,
fmd.min(fmp),
fdelta > fmd.min(fmp)
);
println!(
"agreement across sampled positions: {}/{} ({} flip(s))",
n_agree,
window,
window - n_agree
);
let flips = window - n_agree;
let exposed = fdelta > fmd.min(fmp);
println!(
"VERDICT-INPUT: {}",
match (flips, exposed) {
(0, false) =>
"STABLE — no flip, and the config spread cannot reach across the decision \
margin (structurally safe at this position)",
(0, true) =>
"NEAR-TIE-EXPOSED — no flip fired, but the margin is inside the config spread: \
this position is a coin that happened to land the same way in both configs",
(1, true) =>
"NEAR-TIE class — the single flip sits at a margin the config spread covers \
(documented cross-config drift, not a numeric defect)",
(1, false) =>
"UNEXPLAINED — a flip at a margin the config spread does NOT cover; this is a \
real defect, investigate",
_ =>
"SYSTEMATIC — multiple positions disagree; NOT a near-tie coin, investigate",
}
);
Ok(())
}