File size: 15,001 Bytes
3656ccf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//! 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)
}

#[derive(Deserialize)]
struct Config {
    num_sims: u32,
    #[serde(default = "d_games")]
    num_parallel_games: usize,
    #[serde(default = "d_batch")]
    sim_batch: usize,
    #[serde(default = "d_cpuct")]
    c_puct: f64,
    #[serde(default = "d_fpu")]
    fpu: f64,
    #[serde(default = "d_one")]
    prior_temp: f32,
    #[serde(default = "d_one_f64")]
    move_temp: f64,
    #[serde(default = "d_temp_plies")]
    temp_plies: u32,
    #[serde(default = "d_resign")]
    resign_threshold: f32,
    #[serde(default = "d_resign_plies")]
    resign_plies: u32,
    #[serde(default = "d_noresign")]
    no_resign_frac: f64,
    #[serde(default = "d_max_plies")]
    max_plies: u32,
    /// TT entries per game (it only caches evaluations).
    #[serde(default = "d_tt")]
    tt_cap: usize,
    #[serde(default)]
    fast_sims: u32,
    #[serde(default = "d_one_f64")]
    full_frac: f64,
    #[serde(default)]
    dirichlet_alpha: f64,
    #[serde(default = "d_dir_eps")]
    dirichlet_eps: f64,
    #[serde(default = "d_threads")]
    threads: usize,
    /// Count the reused tree's warm visits toward the full / fast budget.
    #[serde(default)]
    cap_total_full: bool,
    #[serde(default)]
    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 }

#[pyclass]
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);
                    }
                });
            }
        });
    }
}

#[pymethods]
impl SelfPlayEngine {
    #[new]
    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.
    #[allow(clippy::type_complexity)]
    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)
    }
}