Files
Rigby/train/policy/src/data/loader.rs
T
rektdieandclaude 1c9d2beec3 feat: policy network trainer on Lc0 visit distributions
train/policy is a fork of montytrain's policy trainer (the one Monty,
Vine and Jackal use: bullet at a pinned revision plus two CUDA
operations, SelectAffine over the legal moves' output rows and a
softmax cross-entropy against the visit distribution). Rigby's
version drops Monty's SEE split of the outputs (3920 rows, as in
Vine), takes its arguments on the command line and checks a
checkpoint with --check.

The Stockfish binpacks used for the value net only keep the played
move, so the data comes from Lc0's raw V6 training tars instead.
lc02monty rebuilds each game from its first record and the played
moves and checks every record: pieces must match, and the moves Lc0
gave a probability must be exactly the legal moves. Lc0 writes
castling as king-takes-rook; Chess960 games keep their rook files.
On a test91 tar all 351,454 records convert. fetch.ps1 downloads a
run's hourly tars day by day, converts and deletes them, and
interleave shuffles the day files into one.

Defaults sized for a 20-minute run on the RTX 3070: 1024 hidden,
10 superbatches of 100M positions (~1M positions/s). The pinned
cudarc does not know CUDA 13.3, so env.ps1 sets
CUDARC_CUDA_VERSION=13000.

Checked: a smoke run on one tar lowers the loss from 2.89 to 2.38 and
gives plausible opening priors; a checkpoint reloads to identical
output; interleaved data trains.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 21:27:08 +01:00

127 lines
4.0 KiB
Rust

use acyclib::{
device::tensor::Shape,
trainer::{
dataloader::{DataLoader, HostDenseMatrix, HostMatrix, HostSparseMatrix, PreparedBatchHost},
DataLoadingError,
},
};
use montyformat::chess::Move;
use super::reader::{DataReader, DecompressedData};
use crate::inputs::{self, INPUT_SIZE, MAX_ACTIVE_BASE, MAX_MOVES, NUM_MOVES_INDICES};
#[derive(Clone)]
pub struct MontyDataLoader {
reader: DataReader,
threads: usize,
}
impl MontyDataLoader {
pub fn new(path: &str, buffer_size_mb: usize, reader_threads: usize, loader_threads: usize) -> Self {
Self { reader: DataReader::new(path, buffer_size_mb, reader_threads), threads: loader_threads }
}
}
impl DataLoader for MontyDataLoader {
type Error = DataLoadingError;
fn map_batches<F: FnMut(PreparedBatchHost) -> bool>(self, batch_size: usize, mut f: F) -> Result<(), Self::Error> {
self.reader.map_batches(batch_size, |batch| f(prepare(batch, self.threads)));
Ok(())
}
}
pub fn prepare(data: &[DecompressedData], threads: usize) -> PreparedBatchHost {
let batch_size = data.len();
let chunk_size = batch_size.div_ceil(threads);
let mut inputs = vec![0; MAX_ACTIVE_BASE * batch_size];
let mut moves = vec![0; MAX_MOVES * batch_size];
let mut dist = vec![0.0; MAX_MOVES * batch_size];
std::thread::scope(|s| {
for (((data_chunk, input_chunk), moves_chunk), dist_chunk) in data
.chunks(chunk_size)
.zip(inputs.chunks_mut(MAX_ACTIVE_BASE * chunk_size))
.zip(moves.chunks_mut(MAX_MOVES * chunk_size))
.zip(dist.chunks_mut(MAX_MOVES * chunk_size))
{
s.spawn(move || {
for (i, point) in data_chunk.iter().enumerate() {
let input_offset = MAX_ACTIVE_BASE * i;
let moves_offset = MAX_MOVES * i;
let mut j = 0;
inputs::map_base_inputs(&point.pos, |feat| {
assert!(feat < INPUT_SIZE);
input_chunk[input_offset + j] = feat as i32;
j += 1;
});
for k in j..MAX_ACTIVE_BASE {
input_chunk[input_offset + k] = -1;
}
assert!(j <= MAX_ACTIVE_BASE, "More inputs provided than the specified maximum!");
let mut total = 0;
let mut distinct = 0;
let pos = &point.pos;
for &(mov, visits) in &point.moves[..point.num] {
total += visits;
let mov = Move::from(mov);
moves_chunk[moves_offset + distinct] = inputs::map_move_to_index(pos, mov) as i32;
dist_chunk[moves_offset + distinct] = f32::from(visits);
distinct += 1;
}
for k in distinct..MAX_MOVES {
moves_chunk[moves_offset + k] = -1;
}
let total = f32::from(total);
for idx in 0..distinct {
dist_chunk[moves_offset + idx] /= total;
}
}
});
}
});
let mut prep = PreparedBatchHost { batch_size, inputs: Default::default() };
unsafe {
prep.inputs.insert(
"inputs".to_string(),
HostMatrix::Sparse(HostSparseMatrix::new(
inputs,
Some(batch_size),
Shape::new(INPUT_SIZE, 1),
MAX_ACTIVE_BASE,
)),
);
prep.inputs.insert(
"moves".to_string(),
HostMatrix::Sparse(HostSparseMatrix::new(
moves,
Some(batch_size),
Shape::new(NUM_MOVES_INDICES, 1),
MAX_MOVES,
)),
);
}
prep.inputs.insert(
"targets".to_string(),
HostMatrix::Dense(HostDenseMatrix::new(dist, Some(batch_size), Shape::new(MAX_MOVES, 1))),
);
prep
}