Files
Rigby/train/README.md
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

173 lines
7.2 KiB
Markdown

# Training
## Value network (`value/`)
A small `(768 -> 128)x2 -> 1` SCReLU network trained with
[bullet](https://github.com/jw1912/bullet) on Lc0 self-play data.
### Run
```
powershell -ExecutionPolicy Bypass -File C:\Users\rektd\repos\Rigby\train\value\train.ps1
```
Default: 40 superbatches of 100M positions, about 12 minutes on the RTX 3070
(5.8M positions/s). Arguments are passed to the trainer:
| Argument | Default | Meaning |
| --- | --- | --- |
| `--superbatches N` | 40 | Training length; the learning rate decays to the end |
| `--id NAME` | `rigby-value` | Checkpoint name |
| `--data FILE` | the test80 Feb 2024 binpack | Repeatable; several files are read one after the other |
Output: `value/checkpoints/<id>-<superbatch>/quantised.bin` every 10 superbatches
and at the end. That file is what the engine loads.
### Network format
`quantised.bin` (197,440 bytes for 128 hidden), little-endian `i16`:
| Part | Shape | Quantisation |
| --- | --- | --- |
| `l0w` | 768 x 128, feature-major (`[feature][hidden]`) | QA = 255 |
| `l0b` | 128 | QA |
| `l1w` | 2 x 128 (side to move first, then the other side) | QB = 64 |
| `l1b` | 1 | QA * QB |
Inputs (bullet's `Chess768`): `64 * piece + square`, with pieces relative to the
accumulator's side (own pawn = 0 ... own king = 5, their pawn = 6 ... their king = 11)
and squares flipped vertically for black. The side to move's accumulator comes first.
Output: `((sum screlu(acc) * w) / QA + b) * 400 / (QA * QB)` in centipawns, side to
move. The engine maps it to Q with the same 400 scale:
`q = 2 * sigmoid(cp / 400) - 1 = tanh(cp / 800)` (`src/nnue.cpp`, `src/eval.cpp`).
### Checking the engine's inference
```
cargo run --release -- --check checkpoints/<id>-<superbatch>
```
prints bullet's own (float) evaluation of fixed positions; the engine's `evalfens`
command prints the same positions with the quantised network. Expect agreement
within a few centipawns (quantisation); a wrong feature mapping shows up as
differences of hundreds.
### Networks
| File | Data | Superbatches | Final loss |
| --- | --- | --- | --- |
| `networks/rigby-value-v1.bin` | test80 2024-02 | 40 (10 min) | 0.0220 |
### Data
Lc0 self-play data converted to Stockfish binpacks by linrock
([huggingface.co/datasets/linrock](https://huggingface.co/datasets/linrock)), here
`test80-2024-02-feb-2tb7p.min-v2.v6.binpack` (6.9 GB zstd, 8.3 GB unpacked).
bullet reads binpacks directly (`SfBinpackLoader`); no conversion is needed.
The filter keeps positions from ply 16 on that are not in check, with a quiet best
move and |score| <= 10000. Training target: `0.4 * result + 0.6 * sigmoid(score / 400)`.
Data lives in `train/data/` and is not committed.
### Setup on a new machine
1. Rust via rustup (MSVC toolchain), Visual Studio C++ build tools.
2. CUDA Toolkit 13.3 with `cudart`, `nvrtc`, `cublas` (bullet compiles its kernels at
runtime with NVRTC, no nvcc needed).
3. `git clone https://github.com/jw1912/bullet` next to this repo.
4. `pip install zstandard` and a binpack from linrock's datasets in `train/data/`.
## Policy network (`policy/`)
`3072 -> HIDDEN -> CReLU -> pairwise product (HIDDEN / 2) -> one row per move`,
softmax over the legal moves, trained on Lc0's visit distributions. The trainer is
montytrain's policy trainer (the one Monty, Vine and Jackal use; it adds two CUDA
operations to a pinned bullet revision), with Rigby's inputs and outputs and an Lc0
converter.
### Data
```
powershell -ExecutionPolicy Bypass -File C:\Users\rektd\repos\Rigby\train\policy\fetch.ps1 -From 20261025 -To 20261115
```
Downloads the hourly tars of an Lc0 run from
[storage.lczero.org/files/training_data](https://storage.lczero.org/files/training_data/)
(default `test91`, the active run in November 2026; test80 stopped in August 2025),
one day at a time: each day is converted to `data/lc0/monty/<run>-<day>.binpack` and
its tars are deleted. Days already converted are skipped, so an interrupted run can
be restarted. Finally every day file is shuffled into `data/lc0/policy.binpack`.
One hourly tar (~73 MB) holds ~350k positions, which take ~12 MB converted; about
three weeks of test91 (~40 GB of tars, ~190M positions) is plenty for a
10-superbatch run. With 8 connections the download runs at ~12 MB/s.
Tars downloaded by hand can be converted directly:
```
target\release\lc02monty.exe ..\data\lc0\monty\mine.binpack <tar or folder of tars>... [--delete]
target\release\interleave.exe ..\data\lc0\policy.binpack ..\data\lc0\monty
```
`lc02monty` (`src/lc0.rs`) reads V6 records (classical input format), rebuilds each
game from its first position and the played moves, and checks every record: the
pieces must match and the moves Lc0 gave a probability must be exactly the legal
moves. A game is cut at the first record that fails (`LC0_DEBUG=1` prints why); on
test91 data nothing fails. Lc0 writes castling as the king taking its rook; Chess960
games (about 4%) are kept, the castling rooks taken as the outermost on each side.
Stored per position: the played move, best Q and the visit fractions of every legal
move (montyformat scales them to 0..255).
### Run
```
powershell -ExecutionPolicy Bypass -File C:\Users\rektd\repos\Rigby\train\policy\train.ps1
```
Default: 1024 hidden, 10 superbatches of 100M positions, about 17 minutes on the
RTX 3070 (~1M positions/s; 2048 hidden runs at ~400k/s). AdamW, weights clipped to
±0.99, LR 1e-3 decaying exponentially to 1e-5. Arguments:
| Argument | Default | Meaning |
| --- | --- | --- |
| `--superbatches N` | 10 | Training length |
| `--hidden N` | 1024 | Hidden layer width (even) |
| `--id NAME` | `rigby-policy` | Checkpoint name |
| `--data FILE` | `../data/lc0/policy.binpack` | montyformat file |
| `--buffer-mb N` | 4096 | Shuffle buffer |
| `--batches N` | 6104 | Batches of 16384 per superbatch (smaller for test runs) |
Output: `policy/checkpoints/<id>-<superbatch>/quantised.bin` every 5 superbatches.
Try it in the engine with `setoption name PolicyFile value <path>`, or build it in
with `make POLICYFILE=<path>`.
### Network format
`quantised.bin`, `i8`, every weight times 128, no header (the engine works out
HIDDEN from the size: `3072 * H + H + (H / 2) * 3920 + 3920` bytes):
| Part | Shape |
| --- | --- |
| `l0w` | 3072 x H, feature-major |
| `l0b` | H |
| `l1w` | 3920 x H/2, move-major |
| `l1b` | 3920 |
Inputs, relative to the side to move (`src/inputs.rs`, `src/policy.cpp`): squares
flipped vertically for black and horizontally when its king is on files e-h;
`384 * theirs + 64 * piece + square`, plus 768 if the opponent attacks the square
and 1536 if the side to move defends it (attacks of sliders see through the other
side's king). Outputs: one row per (piece, from, to) reachable on an empty board,
then 4 x 22 promotions, 2 castling rows and 8 double pushes, in the same flipped
frame. Hidden: `clamp(acc[i], 0, 1) * clamp(acc[i + H/2], 0, 1)`.
### Checking the engine's inference
`train.ps1 --check checkpoints\<id>-<superbatch> --hidden <H>` prints the trainer's
(float) policy of fixed positions; the engine's `policyfens` command prints the
same with the quantised network. They agree to a few tenths of a percent.
### Setup
As for the value trainer, plus: the pinned bullet revision's `cudarc` does not know
CUDA 13.3, so `env.ps1` sets `CUDARC_CUDA_VERSION=13000` (13.3 is compatible).