chessbaseLinux/crates/maia/tests/reference.rs
vrubelroman 7fdc22345e
Some checks are pending
CI / check (push) Waiting to run
CI / windows-build (push) Waiting to run
CI / macos-build (push) Waiting to run
maia: network sizes read from the checkpoint (5M, 23M, 79M)
The larger Maia-3 models compress each square before the attention-bias
generator (sm1); the sizes now come from the tensors. Checked against the
original PyTorch model for 5M and 79M (reference test, MAIA_REFERENCE).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 19:52:09 +03:00

80 lines
2.9 KiB
Rust

//! Agreement with the original PyTorch model (tests/reference.json, written by
//! scripts/maia-reference.py): the same top moves with close probabilities. Needs the
//! weights: `MAIA_WEIGHTS=/path/maia3-5m.pt cargo test -p maia -- --ignored`. Another
//! model: its reference file in `MAIA_REFERENCE`.
use maia::Maia;
use shakmaty::fen::Fen;
use shakmaty::uci::UciMove;
use shakmaty::{CastlingMode, Chess};
#[test]
#[ignore = "needs the weights file (MAIA_WEIGHTS)"]
fn matches_the_original_model() {
let path = std::env::var("MAIA_WEIGHTS").expect("MAIA_WEIGHTS");
let maia = Maia::load(std::path::Path::new(&path)).expect("weights");
let reference = match std::env::var("MAIA_REFERENCE") {
Ok(file) => std::fs::read_to_string(file).expect("MAIA_REFERENCE"),
Err(_) => include_str!("reference.json").to_owned(),
};
let cases: serde_json::Value = serde_json::from_str(&reference).expect("json");
for case in cases.as_array().unwrap() {
let start = case["start"].as_str().unwrap();
let mut pos: Chess = if start == "startpos" {
Chess::default()
} else {
start[4..]
.parse::<Fen>()
.unwrap()
.into_position(CastlingMode::Standard)
.unwrap()
};
let mut history = vec![pos.clone()];
for m in case["moves"].as_array().unwrap() {
let m = m
.as_str()
.unwrap()
.parse::<UciMove>()
.unwrap()
.to_move(&pos)
.unwrap();
pos = maia::after(&pos, m);
history.push(pos.clone());
}
if !case["history"].as_bool().unwrap() {
history = vec![pos.clone()];
}
let t = std::time::Instant::now();
let p = maia.predict(
&history,
case["self"].as_u64().unwrap() as u32,
case["oppo"].as_u64().unwrap() as u32,
);
let ms = t.elapsed().as_millis();
let ours: Vec<(String, f32)> = p
.moves
.iter()
.take(5)
.map(|(m, pr)| (m.to_uci(CastlingMode::Standard).to_string(), *pr))
.collect();
let want: Vec<(String, f32)> = case["top"]
.as_array()
.unwrap()
.iter()
.map(|t| {
(
t[0].as_str().unwrap().to_owned(),
t[1].as_f64().unwrap() as f32,
)
})
.collect();
println!(
"{start} {:?} hist={} {ms} ms\n ours {ours:?}\n want {want:?}\n wdl {:?} want {:?}",
case["moves"], case["history"], p.wdl, case["value"]
);
for (k, (m, pr)) in want.iter().enumerate().take(3) {
assert_eq!(&ours[k].0, m, "move {k} of {start}");
assert!((ours[k].1 - pr).abs() < 0.02, "{m}: {} vs {pr}", ours[k].1);
}
}
}