The measured tables (strength → rating input, temperature) and the choice of Maia's move (rare moves dropped) move from the desktop's play.rs into the maia crate; the desktop plays by them. Android: «Learn the opening up to this move» in the move sheet opens the trainer over the game (tasks bar, wrong move lit for 400 ms, «Show the move» after three mistakes, the result card). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
90 lines
3.1 KiB
Rust
90 lines
3.1 KiB
Rust
//! A human-like move predictor: the Maia-3 network (CSSLab, University of Toronto,
|
|
//! "Chessformer", ICLR 2026) computed in plain Rust, without a machine-learning runtime.
|
|
//!
|
|
//! The network sees the position (and up to seven positions before it) from the side to
|
|
//! move and the ratings of both players, and gives the probability of each legal move
|
|
//! as a player of that rating would play it, plus the chances of win, draw and loss.
|
|
//!
|
|
//! The weights are the published checkpoint itself (`maia3-5m.pt`, PyTorch's zip of a
|
|
//! pickle and raw f32 storages), read without PyTorch: the network shares one bias
|
|
//! tensor between its blocks.
|
|
|
|
mod checkpoint;
|
|
mod net;
|
|
pub mod strength;
|
|
mod weights;
|
|
|
|
pub use net::Prediction;
|
|
pub use weights::WeightsError;
|
|
|
|
use shakmaty::{Chess, Move, Position};
|
|
|
|
/// The network, loaded.
|
|
pub struct Maia {
|
|
net: net::Net,
|
|
}
|
|
|
|
impl Maia {
|
|
/// Reads the checkpoint (`maia3-5m.pt`).
|
|
pub fn load(path: &std::path::Path) -> Result<Maia, WeightsError> {
|
|
// The file's bytes are let go once read into tensors, and the tensors move into
|
|
// the network: at most two copies of the weights at once (the large model: its
|
|
// 316 MB twice, not three times).
|
|
let w = {
|
|
let bytes = std::fs::read(path).map_err(WeightsError::Io)?;
|
|
weights::Weights::parse(&bytes)?
|
|
};
|
|
Maia::from_weights(w)
|
|
}
|
|
|
|
pub fn from_bytes(bytes: &[u8]) -> Result<Maia, WeightsError> {
|
|
Maia::from_weights(weights::Weights::parse(bytes)?)
|
|
}
|
|
|
|
fn from_weights(mut w: weights::Weights) -> Result<Maia, WeightsError> {
|
|
Ok(Maia {
|
|
net: net::Net::new(&mut w)?,
|
|
})
|
|
}
|
|
|
|
/// The moves a player rated `self_elo` would play in `history.last()` against one
|
|
/// rated `oppo_elo`, most likely first. `history`: the positions of the game up to
|
|
/// and including the current one (the last eight are used; fewer are padded with
|
|
/// the earliest given). Empty when there is no legal move.
|
|
pub fn predict(&self, history: &[Chess], self_elo: u32, oppo_elo: u32) -> Prediction {
|
|
self.net.predict(history, self_elo, oppo_elo)
|
|
}
|
|
}
|
|
|
|
/// A move to play from a prediction: the most likely one with `temperature` 0,
|
|
/// otherwise drawn at random with probabilities sharpened (< 1) or flattened (> 1).
|
|
/// `random` is a number in [0, 1).
|
|
pub fn pick(prediction: &Prediction, temperature: f32, random: f32) -> Option<Move> {
|
|
let moves = &prediction.moves;
|
|
if moves.is_empty() {
|
|
return None;
|
|
}
|
|
if temperature <= 0.0 {
|
|
return Some(moves[0].0);
|
|
}
|
|
let weights: Vec<f32> = moves
|
|
.iter()
|
|
.map(|(_, p)| p.max(1e-12).powf(1.0 / temperature))
|
|
.collect();
|
|
let total: f32 = weights.iter().sum();
|
|
let mut target = random.clamp(0.0, 0.999_999) * total;
|
|
for ((m, _), w) in moves.iter().zip(&weights) {
|
|
if target < *w {
|
|
return Some(*m);
|
|
}
|
|
target -= w;
|
|
}
|
|
moves.last().map(|(m, _)| *m)
|
|
}
|
|
|
|
/// The position after `m`, for building the history.
|
|
pub fn after(pos: &Chess, m: Move) -> Chess {
|
|
let mut p = pos.clone();
|
|
p.play_unchecked(m);
|
|
p
|
|
}
|