Files
Rigby/train/README.md
T
rektdieandclaude c4f65b2632 feat: policy network v1 replaces the move heuristics
networks/rigby-policy-v1.bin is now built in by default (POLICYFILE;
POLICYFILE= still builds the heuristics). It is a 3072 -> 1024 -> 3920
network trained by train/policy on 171M positions of Lc0 test91
self-play (2026-10-25 .. 11-15) against the search's visit
distributions: 10 superbatches of 100M, 10 minutes on the RTX 3070,
final loss 2.296 (2.39 after the first superbatch). The butterfly
history bias and PolicyTemp apply on top of its logits as they did on
the heuristics' scores.

Sensible priors: 1.e4 e5 75%, Kiwipete Bxa6 38%, black bxc3 43%,
c8=Q in the K+P ending. The engine's quantised output matches the
trainer's float output to tenths of a percent, a few percent on very
sharp distributions. Costs about 10% nps on the bench.

Checked: bench deterministic; RIGBY_VERIFY_TREE build driven over UCI
(Hash 1 MB, repeated positions, fen + moves, wtime/winc, go infinite +
stop) without an abort; a build with POLICYFILE= gives the old bench
529903.

Bench: 630039

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 22:16:58 +01:00

182 lines
7.6 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. The server limits each connection to ~2.3 MB/s (what a browser
gets) but not their number, so fetch.ps1 downloads 32 tars at a time across days
(`-Parallel`): 72 MB/s measured, about 10 minutes for three weeks.
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, a few
percent on very sharp distributions (rigby-policy-v1, 1.e4 e5: 80.2% vs 75.3%).
### Networks
| File | Data | Hidden | Superbatches | Final loss |
| --- | --- | --- | --- | --- |
| `networks/rigby-policy-v1.bin` | test91 2026-10-25 .. 11-15 (171M positions) | 1024 | 10 (10 min) | 2.296 |
### 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).