Files
Rigby/train/value/src/zstd_reader.rs
T
rektdieandclaude 79c866c77c feat: value network v2, 1024 hidden trained on linrock S2
Replaces the (768 -> 128)x2 value net with (768 -> 1024)x2 -> 1 SCReLU, same
inputs, quantisation and output scale, trained on much more data: the 12 S2
files of huggingface.co/datasets/linrock/bullet-training-data (6.6B positions,
already filtered and shuffled; v1 had one month of test80).

Training: 170 superbatches of 100M positions (17B positions, about 2.6
passes over S2), AdamW, LR 1e-3 cosine to 2.4e-6, target 0.4 * result +
0.6 * sigmoid(score / 400) as in v1. 1h34m on the RTX 3070 at ~3M
positions/s (GPU-bound); final running loss 0.01278 (not comparable to v1's
0.0220, other data). The width was picked for the 2 hour budget: 1024 trains
at 3-4M/s, and in the engine a random 1024 net cost only 16% nps against 128,
thanks to the SIMD kernels.

Trainer (train/value):
- --hidden N (default now 1024) and --batches N for speed tests
- reads bulletformat straight from .bin.zst (src/zstd_reader.rs), so the
  108 GB of S2 never has to be unpacked to 210 GB; training needs ~40 MB/s
  of compressed input, which the HDD holding it gives easily
- also reads raw .bin files (bullet's DirectSequentialDataLoader)

Engine:
- nnue::HIDDEN = 1024, Makefile EVALFILE = networks/rigby-value-v2.bin
- the output layer now sums in blocks of 128 that fit 32 bits and adds
  them up in 64, so it stays exact for any width (the single 32-bit sum was
  only provably safe up to 128)

Checked: evalfens against the trainer's --check on the final checkpoint:
same signs and sizes, within 6 cp on quiet positions and up to 7% on large
evaluations (quantisation; e.g. 553 vs 594 cp). Bench deterministic. UCI:
uci, isready, ucinewgame, go nodes / movetime / infinite + stop.
nps on bench: 4.6M -> 3.5M.

Bench: 663779

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 18:14:52 +01:00

76 lines
2.7 KiB
Rust

//! Reads bulletformat positions straight from zstd-compressed files (linrock's
//! `*.bullet.bin.zst`), so the data never has to be unpacked to disk: training
//! needs ~30 MB/s of compressed input, which even an HDD gives easily.
//!
//! Files are read one after the other and the list repeats forever, like bullet's
//! `DirectSequentialDataLoader`; each file must already be shuffled.
use std::{
fs::File,
io::{BufReader, Read},
};
use bullet_lib::game::formats::bulletformat::ChessBoard;
use bullet_trainer::reader::DataReader;
#[derive(Clone)]
pub struct ZstdBulletReader {
paths: Vec<String>,
}
impl ZstdBulletReader {
pub fn new(paths: &[&str]) -> Self {
for path in paths {
assert!(std::path::Path::new(path).exists(), "data file not found: {path}");
}
Self { paths: paths.iter().map(|p| p.to_string()).collect() }
}
}
/// Reads until `buf` is full or the stream ends; returns the bytes read.
fn read_full(reader: &mut impl Read, buf: &mut [u8]) -> usize {
let mut filled = 0;
while filled < buf.len() {
match reader.read(&mut buf[filled..]) {
Ok(0) => break,
Ok(n) => filled += n,
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => {}
Err(e) => panic!("reading training data: {e}"),
}
}
filled
}
impl DataReader<ChessBoard> for ZstdBulletReader {
fn read_chunks<F: FnMut(&[ChessBoard]) -> bool>(&self, mut skip: usize, mut f: F) {
const SIZE: usize = size_of::<ChessBoard>();
const CAP: usize = (64 << 20) / SIZE;
let mut buf = vec![ChessBoard::default(); CAP];
loop {
for path in &self.paths {
let file = BufReader::with_capacity(4 << 20, File::open(path).unwrap());
let mut decoder = zstd::stream::read::Decoder::with_buffer(file).unwrap();
loop {
// ChessBoard is plain bytes (bulletformat's 32-byte record).
let bytes = unsafe { std::slice::from_raw_parts_mut(buf.as_mut_ptr().cast::<u8>(), CAP * SIZE) };
let n = read_full(&mut decoder, bytes);
if n == 0 {
break;
}
assert_eq!(n % SIZE, 0, "{path} is not a whole number of positions");
let mut chunk = &buf[..n / SIZE];
if skip > 0 {
let s = skip.min(chunk.len());
skip -= s;
chunk = &chunk[s..];
}
if !chunk.is_empty() && f(chunk) {
return;
}
}
}
}
}
}