//! 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::() .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::() .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); } } }