Spaces:
Sleeping
Sleeping
Download rust/selfplay-core/src/selfplay_engine.rs from tchauffi/ChessTransformer: direct link, hf CLI and curl.
- Browser
- Download file 15 kB
-
https://huggingface.co/spaces/tchauffi/ChessTransformer/resolve/main/rust/selfplay-core/src/selfplay_engine.rs
- Command line
-
hf download hf://spaces/tchauffi/ChessTransformer/rust/selfplay-core/src/selfplay_engine.rs
-
curl -L -o selfplay_engine.rs https://huggingface.co/spaces/tchauffi/ChessTransformer/resolve/main/rust/selfplay-core/src/selfplay_engine.rs
15 kB
| //! Self-play on the deployed bot's search: many `chess_core::selfplay` games in | |
| //! parallel threads, their waves handed to Python as one batch. Same protocol | |
| //! and game records as `Engine`, which keeps its own older search. | |
| use chess_core::search::EvalOutput; | |
| use chess_core::selfplay::{SelfPlayGame, SelfPlayRules}; | |
| use numpy::{IntoPyArray, PyArray1, PyArray2, PyArrayMethods, PyReadonlyArray1, PyReadonlyArray2, | |
| PyReadonlyArray3}; | |
| use pyo3::prelude::*; | |
| use pyo3::types::PyDict; | |
| use rand::prelude::*; | |
| use rand::rngs::StdRng; | |
| use serde::Deserialize; | |
| use shakmaty::fen::Fen; | |
| use shakmaty::{CastlingMode, Chess}; | |
| const POLICY: usize = 64 * 73; | |
| /// Pad index of the legal-move array: one past the last logit, so a GPU gather | |
| /// over logits with one appended column reads a dummy there. | |
| const PAD: i16 = POLICY as i16; | |
| /// The legal-move index rows of a batch, padded to the longest: (flat, width, lengths). | |
| fn legal_rows<'a>(reqs: impl Iterator<Item = &'a chess_core::search::EvalRequest>) -> (Vec<i16>, usize, Vec<u16>) { | |
| let reqs: Vec<_> = reqs.collect(); | |
| let width = reqs.iter().map(|r| r.legal.len()).max().unwrap_or(0).max(1); | |
| let mut flat = vec![PAD; reqs.len() * width]; | |
| let mut lens = Vec::with_capacity(reqs.len()); | |
| for (i, r) in reqs.iter().enumerate() { | |
| for (j, &m) in r.legal.iter().enumerate() { | |
| flat[i * width + j] = m as i16; | |
| } | |
| lens.push(r.legal.len() as u16); | |
| } | |
| (flat, width, lens) | |
| } | |
| struct Config { | |
| num_sims: u32, | |
| num_parallel_games: usize, | |
| sim_batch: usize, | |
| c_puct: f64, | |
| fpu: f64, | |
| prior_temp: f32, | |
| move_temp: f64, | |
| temp_plies: u32, | |
| resign_threshold: f32, | |
| resign_plies: u32, | |
| no_resign_frac: f64, | |
| max_plies: u32, | |
| /// TT entries per game (it only caches evaluations). | |
| tt_cap: usize, | |
| fast_sims: u32, | |
| full_frac: f64, | |
| dirichlet_alpha: f64, | |
| dirichlet_eps: f64, | |
| threads: usize, | |
| /// Count the reused tree's warm visits toward the full / fast budget. | |
| cap_total_full: bool, | |
| cap_total_fast: bool, | |
| } | |
| fn d_games() -> usize { 128 } | |
| fn d_batch() -> usize { 16 } | |
| fn d_cpuct() -> f64 { 1.0 } | |
| fn d_fpu() -> f64 { 0.2 } | |
| fn d_one() -> f32 { 1.0 } | |
| fn d_one_f64() -> f64 { 1.0 } | |
| fn d_temp_plies() -> u32 { 30 } | |
| fn d_resign() -> f32 { 0.93 } | |
| fn d_resign_plies() -> u32 { 6 } | |
| fn d_noresign() -> f64 { 0.1 } | |
| fn d_max_plies() -> u32 { 300 } | |
| fn d_tt() -> usize { 100_000 } | |
| fn d_dir_eps() -> f64 { 0.25 } | |
| fn d_threads() -> usize { 6 } | |
| pub struct SelfPlayEngine { | |
| rules: SelfPlayRules, | |
| num_parallel_games: usize, | |
| threads: usize, | |
| openings: Vec<Chess>, | |
| opening_fens: Vec<String>, | |
| games: Vec<SelfPlayGame>, | |
| finished: Vec<SelfPlayGame>, | |
| rng: StdRng, | |
| started: usize, | |
| completed: usize, | |
| target: usize, | |
| positions_recorded: usize, | |
| /// (game slot, positions) of the last collect, in batch order. | |
| owners: Vec<(usize, usize)>, | |
| /// Legal-move count per position of the last collect, and the padded width. | |
| legal_lens: Vec<u16>, | |
| legal_width: usize, | |
| } | |
| impl SelfPlayEngine { | |
| fn fill(&mut self) { | |
| while self.games.len() < self.num_parallel_games && self.started < self.target { | |
| let oi = self.rng.gen_range(0..self.openings.len()); | |
| let seed = self.rng.gen(); | |
| let g = SelfPlayGame::new(self.started, oi, self.openings[oi].clone(), &self.rules, seed); | |
| self.started += 1; | |
| self.games.push(g); | |
| } | |
| } | |
| fn par_games<F>(&mut self, f: F) | |
| where | |
| F: Fn(usize, &mut SelfPlayGame) + Sync, | |
| { | |
| let chunk = self.games.len().div_ceil(self.threads.max(1)).max(1); | |
| std::thread::scope(|scope| { | |
| for (ci, games) in self.games.chunks_mut(chunk).enumerate() { | |
| let f = &f; | |
| scope.spawn(move || { | |
| for (j, g) in games.iter_mut().enumerate() { | |
| f(ci * chunk + j, g); | |
| } | |
| }); | |
| } | |
| }); | |
| } | |
| } | |
| impl SelfPlayEngine { | |
| fn new(config_json: &str, openings: Vec<String>, seed: u64, target: usize) -> PyResult<Self> { | |
| let c: Config = serde_json::from_str(config_json) | |
| .map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("config: {e}")))?; | |
| let mut parsed = Vec::new(); | |
| for fen in &openings { | |
| let f: Fen = fen.parse() | |
| .map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("fen {fen}: {e}")))?; | |
| let pos: Chess = f.into_position(CastlingMode::Standard) | |
| .map_err(|e| pyo3::exceptions::PyValueError::new_err(format!("fen {fen}: {e}")))?; | |
| parsed.push(pos); | |
| } | |
| let rules = SelfPlayRules { | |
| num_sims: c.num_sims, fast_sims: c.fast_sims, full_frac: c.full_frac, | |
| dirichlet_alpha: c.dirichlet_alpha, dirichlet_eps: c.dirichlet_eps, | |
| move_temp: c.move_temp, temp_plies: c.temp_plies, | |
| resign_threshold: c.resign_threshold, resign_plies: c.resign_plies, | |
| no_resign_frac: c.no_resign_frac, max_plies: c.max_plies, sim_batch: c.sim_batch, | |
| c_puct: c.c_puct, fpu: c.fpu, prior_temp: c.prior_temp, tt_cap: c.tt_cap, | |
| cap_total_full: c.cap_total_full, cap_total_fast: c.cap_total_fast, | |
| }; | |
| let mut e = SelfPlayEngine { | |
| rules, | |
| num_parallel_games: c.num_parallel_games.min(target.max(1)), | |
| threads: c.threads, | |
| openings: parsed, | |
| opening_fens: openings, | |
| games: Vec::new(), | |
| finished: Vec::new(), | |
| rng: StdRng::seed_from_u64(seed), | |
| started: 0, | |
| completed: 0, | |
| target, | |
| positions_recorded: 0, | |
| owners: Vec::new(), | |
| legal_lens: Vec::new(), | |
| legal_width: 1, | |
| }; | |
| e.fill(); | |
| Ok(e) | |
| } | |
| /// Advance every game to its next NN wave; returns (boards, player, | |
| /// castling, ep, legal) of all pending positions, `legal` being the legal | |
| /// moves' flat logit indices per position, padded with 4672. | |
| fn collect<'py>(&mut self, py: Python<'py>) -> PyResult<( | |
| Bound<'py, PyArray2<u8>>, Bound<'py, PyArray1<u8>>, Bound<'py, PyArray1<u8>>, | |
| Bound<'py, PyArray1<u8>>, Bound<'py, PyArray2<i16>>, | |
| )> { | |
| loop { | |
| let rules = self.rules; | |
| self.par_games(|_, g| g.advance(&rules)); | |
| if let Some(g) = self.games.iter().find(|g| g.stalled()) { | |
| return Err(pyo3::exceptions::PyRuntimeError::new_err(format!( | |
| "self-play engine stalled in game {}", g.id))); | |
| } | |
| let before = self.games.len(); | |
| let mut i = 0; | |
| while i < self.games.len() { | |
| if self.games[i].end().is_some() { | |
| let g = self.games.swap_remove(i); | |
| self.positions_recorded += g.records().len(); | |
| self.completed += 1; | |
| self.finished.push(g); | |
| } else { | |
| i += 1; | |
| } | |
| } | |
| let retired = before != self.games.len(); | |
| self.fill(); | |
| if !retired || self.games.is_empty() { | |
| break; | |
| } | |
| } | |
| self.owners.clear(); | |
| let (mut boards, mut player, mut castling, mut ep) = | |
| (Vec::new(), Vec::new(), Vec::new(), Vec::new()); | |
| for (gi, g) in self.games.iter().enumerate() { | |
| if let Some(reqs) = g.pending() { | |
| for r in reqs { | |
| boards.extend_from_slice(&r.tokens); | |
| player.push(r.player); | |
| castling.push(r.castling); | |
| ep.push(r.ep); | |
| } | |
| self.owners.push((gi, reqs.len())); | |
| } | |
| } | |
| let n = player.len(); | |
| let (legal, width, lens) = legal_rows( | |
| self.owners.iter().flat_map(|&(gi, _)| self.games[gi].pending().unwrap().iter())); | |
| self.legal_lens = lens; | |
| self.legal_width = width; | |
| Ok(( | |
| PyArray1::from_vec(py, boards).reshape([n, 64]).expect("reshape"), | |
| player.into_pyarray(py), | |
| castling.into_pyarray(py), | |
| ep.into_pyarray(py), | |
| PyArray1::from_vec(py, legal).reshape([n, width]).expect("reshape"), | |
| )) | |
| } | |
| /// Like `apply`, with the legal moves' logits already gathered: row i holds | |
| /// the logits at the indices of the last collect's `legal` row i. | |
| fn apply_legal(&mut self, logits: PyReadonlyArray2<f32>, values: PyReadonlyArray1<f32>) -> PyResult<()> { | |
| let logits = logits.as_array(); | |
| let values = values.as_slice()?; | |
| let total = self.legal_lens.len(); | |
| let width = self.legal_width; | |
| if values.len() != total || logits.shape() != [total, width] { | |
| return Err(pyo3::exceptions::PyValueError::new_err(format!( | |
| "apply_legal: got {} values / logits {:?} for {} pending positions of width {}", | |
| values.len(), logits.shape(), total, width))); | |
| } | |
| let flat = logits.as_slice().ok_or_else(|| { | |
| pyo3::exceptions::PyValueError::new_err("apply_legal: logits must be C-contiguous") | |
| })?; | |
| let lens = std::mem::take(&mut self.legal_lens); | |
| let lens = &lens; | |
| let mut offset = vec![None; self.games.len()]; | |
| let mut at = 0; | |
| for &(gi, k) in &self.owners { | |
| offset[gi] = Some((at, k)); | |
| at += k; | |
| } | |
| let offset = &offset; | |
| self.par_games(|gi, g| { | |
| let Some((at, k)) = offset[gi] else { return }; | |
| let outputs: Vec<EvalOutput> = (at..at + k) | |
| .map(|i| EvalOutput { logits: flat[i * width..i * width + lens[i] as usize].to_vec(), | |
| value: values[i] }) | |
| .collect(); | |
| g.resolve(outputs); | |
| }); | |
| self.owners.clear(); | |
| Ok(()) | |
| } | |
| /// Outputs for the batch of the last collect(), in its order. | |
| fn apply(&mut self, logits: PyReadonlyArray3<f32>, values: PyReadonlyArray1<f32>) -> PyResult<()> { | |
| let logits = logits.as_array(); | |
| let values = values.as_slice()?; | |
| let total: usize = self.owners.iter().map(|o| o.1).sum(); | |
| if values.len() != total || logits.shape() != [total, 64, 73] { | |
| return Err(pyo3::exceptions::PyValueError::new_err(format!( | |
| "apply: got {} values / logits {:?} for {} pending positions", | |
| values.len(), logits.shape(), total))); | |
| } | |
| let flat = logits.as_slice().ok_or_else(|| { | |
| pyo3::exceptions::PyValueError::new_err("apply: logits must be C-contiguous") | |
| })?; | |
| let mut offset = vec![None; self.games.len()]; | |
| let mut at = 0; | |
| for &(gi, k) in &self.owners { | |
| offset[gi] = Some((at, k)); | |
| at += k; | |
| } | |
| let offset = &offset; | |
| self.par_games(|gi, g| { | |
| let Some((at, k)) = offset[gi] else { return }; | |
| let outputs: Vec<EvalOutput> = (at..at + k) | |
| .map(|i| EvalOutput { logits: flat[i * POLICY..(i + 1) * POLICY].to_vec(), | |
| value: values[i] }) | |
| .collect(); | |
| g.resolve(outputs); | |
| }); | |
| self.owners.clear(); | |
| Ok(()) | |
| } | |
| /// Completed games since the last call, as dicts of numpy arrays (the | |
| /// `Engine.drain_finished` layout). | |
| fn drain_finished<'py>(&mut self, py: Python<'py>) -> PyResult<Vec<Bound<'py, PyDict>>> { | |
| let mut out = Vec::new(); | |
| for g in std::mem::take(&mut self.finished) { | |
| let (z_white, end) = g.end().expect("finished game has a result"); | |
| let recs = g.records(); | |
| let p = recs.len(); | |
| let mut boards = Vec::with_capacity(p * 64); | |
| let (mut player, mut castling, mut ep) = (Vec::new(), Vec::new(), Vec::new()); | |
| let (mut halfmove, mut root_v, mut z, mut full) = (Vec::new(), Vec::new(), Vec::new(), Vec::new()); | |
| let (mut visit_idx, mut visit_cnt) = (Vec::new(), Vec::new()); | |
| let mut visit_ptr = vec![0i64]; | |
| for r in recs { | |
| boards.extend_from_slice(&r.tokens); | |
| player.push(r.player); | |
| castling.push(r.castling); | |
| ep.push(r.ep); | |
| halfmove.push(r.halfmove); | |
| root_v.push(r.root_v); | |
| full.push(r.full); | |
| z.push(if r.player == 1 { z_white } else { -z_white }); | |
| visit_idx.extend_from_slice(&r.visit_idx); | |
| visit_cnt.extend_from_slice(&r.visit_cnt); | |
| visit_ptr.push(visit_idx.len() as i64); | |
| } | |
| let d = PyDict::new(py); | |
| d.set_item("boards", PyArray1::from_vec(py, boards).reshape([p, 64]).expect("reshape"))?; | |
| d.set_item("player", player.into_pyarray(py))?; | |
| d.set_item("castling", castling.into_pyarray(py))?; | |
| d.set_item("ep", ep.into_pyarray(py))?; | |
| d.set_item("halfmove", halfmove.into_pyarray(py))?; | |
| d.set_item("root_v", root_v.into_pyarray(py))?; | |
| d.set_item("full", full.into_pyarray(py))?; | |
| d.set_item("z", z.into_pyarray(py))?; | |
| d.set_item("visit_idx", visit_idx.into_pyarray(py))?; | |
| d.set_item("visit_cnt", visit_cnt.into_pyarray(py))?; | |
| d.set_item("visit_ptr", visit_ptr.into_pyarray(py))?; | |
| d.set_item("moves", g.moves_uci().to_vec())?; | |
| d.set_item("fen", &self.opening_fens[g.opening])?; | |
| d.set_item("z_white", z_white)?; | |
| d.set_item("end", end)?; | |
| d.set_item("resign_allowed", g.resign_allowed())?; | |
| d.set_item("id", g.id)?; | |
| out.push(d); | |
| } | |
| Ok(out) | |
| } | |
| fn done(&self) -> bool { | |
| self.completed >= self.target | |
| } | |
| /// (games started, games completed, positions recorded, TT entries) | |
| fn stats(&self) -> (usize, usize, usize, usize) { | |
| let tt = self.games.iter().map(|g| g.tt_len()).sum(); | |
| (self.started, self.completed, self.positions_recorded, tt) | |
| } | |
| } | |