chessbaseLinux/crates/maia/tests/reference.rs
vrubelroman 9cd28b9039 Play against Maia: dialog, clocks, game screen, result strip
«⚔ Play» (Ctrl+G) opens «Play against the computer»: name and rating
(optional), strength 600–2600 (or weaker/as me/stronger), colour, Lichess time
controls, custom, no clock. The game screen in play mode: clock cards above
and below the notation (read-only), take back, resign (abort before the first
moves); no engine, analysis or editing. Maia answers in a background thread
with a human-like pause bounded so she never loses on time; stale answers are
dropped. Endings: mate, stalemate, no material, threefold, fifty moves, time
(draw when the other side cannot mate), resignation; then the result strip
(Analyse, Rematch, New game, Save to a database) and an ordinary game with the
tags of a played game and [%clk]. Leaving during a game asks (resign and add /
resign and leave / keep playing), the clocks wait meanwhile. maia reads the
published PyTorch checkpoint directly (zip + a small pickle reader that refuses
everything but tensors); maia_download fetches it on request (pinned revision,
SHA-256). Dev hooks CP_PLAY, CP_PLAY_AUTO, CP_MAIA. Russian texts; the other
languages follow.

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

76 lines
2.7 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`.
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 cases: serde_json::Value =
serde_json::from_str(include_str!("reference.json")).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);
}
}
}