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>
This commit is contained in:
rektdieandclaude committed 2026-11-15 21:27:08 +01:00
1 parent 9643c7b0a5
commit 1c9d2beec3
23 files changed
+2850

No files matched your search

+94
View File
@@ -76,3 +76,97 @@ Data lives in `train/data/` and is not committed.
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).