204 lines
9.1 KiB
Markdown
204 lines
9.1 KiB
Markdown
# Training
|
|
|
|
## Value network (`value/`)
|
|
|
|
A `(768 -> 1024)x2 -> 1` SCReLU network trained with
|
|
[bullet](https://github.com/jw1912/bullet) on Lc0-derived data from linrock.
|
|
|
|
### Run
|
|
|
|
How `rigby-value-v2` was trained (the 12 S2 files, see Data):
|
|
|
|
```
|
|
powershell -ExecutionPolicy Bypass -File C:\Users\rektd\repos\Rigby\train\value\train.ps1 --hidden 1024 --superbatches 170 --id rigby-value-v2 --data <iter-1 file> ... --data <iter-12 file>
|
|
```
|
|
|
|
On the RTX 3070, 1024 hidden trains at 3-4M positions/s (bound by the GPU): about
|
|
33 s per superbatch of 100M positions. 512 hidden runs at ~6M/s, 1536 at ~2.6M/s.
|
|
Arguments are passed to the trainer:
|
|
|
|
| Argument | Default | Meaning |
|
|
| --- | --- | --- |
|
|
| `--superbatches N` | 40 | Training length; the learning rate decays to the end |
|
|
| `--hidden N` | 1024 | Hidden width; the engine's `nnue::HIDDEN` must match |
|
|
| `--id NAME` | `rigby-value` | Checkpoint name |
|
|
| `--data FILE` | the test80 Feb 2024 binpack | Repeatable; files are read one after the other, round and round. All `.bin.zst`, all `.bin` or all `.binpack` |
|
|
| `--batches N` | 6104 | Batches of 16384 per superbatch (smaller for speed tests) |
|
|
|
|
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` (1,579,072 bytes for 1024 hidden), little-endian `i16`:
|
|
|
|
| Part | Shape | Quantisation |
|
|
| --- | --- | --- |
|
|
| `l0w` | 768 x H, feature-major (`[feature][hidden]`) | QA = 255 |
|
|
| `l0b` | H | QA |
|
|
| `l1w` | 2 x H (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`).
|
|
The engine computes `screlu(v) * w` as `v * (v * w)` in 16 bits, so `|l1w|` must be
|
|
at most 128 (bullet's AdamW clips weights to ±1.98, i.e. 127); it refuses a net
|
|
that breaks this.
|
|
|
|
### Checking the engine's inference
|
|
|
|
```
|
|
cargo run --release -- --check checkpoints/<id>-<superbatch> --hidden <H>
|
|
```
|
|
|
|
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; v2 at superbatch 20: at most 13 cp); a
|
|
wrong feature mapping shows up as differences of hundreds.
|
|
|
|
### Networks
|
|
|
|
| File | Hidden | Data | Superbatches | Final loss |
|
|
| --- | --- | --- | --- | --- |
|
|
| `networks/rigby-value-v1.bin` | 128 | test80 2024-02 | 40 (10 min) | 0.0220 |
|
|
| `networks/rigby-value-v2.bin` | 1024 | linrock S2 (6.6B positions) | 170 (1h34m) | 0.01278 |
|
|
|
|
Losses are not comparable across data sets.
|
|
|
|
### Data
|
|
|
|
v2: the `S2` folder of
|
|
[linrock/bullet-training-data](https://huggingface.co/datasets/linrock/bullet-training-data),
|
|
12 files `test77nov-unfilt-test79-maraprmay-v6-dd.skip-see-ge0.wdl-pdist.iter-N.bullet.bin.zst`
|
|
(~9 GB each, 108 GB in all; ~547M positions each, 6.6B in all, different positions
|
|
in every file, each already filtered and shuffled). The trainer reads the `.zst`
|
|
files directly (`value/src/zstd_reader.rs`): unpacked they would take 210 GB, and
|
|
training only needs ~40 MB/s of compressed input, which an HDD gives easily. They
|
|
live in `D:\rigby-data\s2\s2-1.parts\` on this machine. Hugging Face caps one connection at ~20 MB/s, so
|
|
download several files at once.
|
|
|
|
v1: Lc0 self-play data converted to Stockfish binpacks by linrock
|
|
([huggingface.co/datasets/linrock](https://huggingface.co/datasets/linrock)),
|
|
`test80-2024-02-feb-2tb7p.min-v2.v6.binpack` (6.9 GB zstd, 8.3 GB unpacked), read
|
|
directly (`SfBinpackLoader`). The filter keeps positions from ply 16 on that are not
|
|
in check, with a quiet best move and |score| <= 10000.
|
|
|
|
Training target for both: `0.4 * result + 0.6 * sigmoid(score / 400)`.
|
|
|
|
Data other than S2 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. linrock's data (see Data); `pip install zstandard` to unpack a `.binpack.zst`.
|
|
|
|
## 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).
|