diff --git a/.gitignore b/.gitignore index ccb954a..8991d99 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,9 @@ Rigby # Editors .vscode/ .idea/ + +# Training +train/data/ +train/**/target/ +train/**/checkpoints/ +*.zst diff --git a/train/README.md b/train/README.md new file mode 100644 index 0000000..fa339b6 --- /dev/null +++ b/train/README.md @@ -0,0 +1,61 @@ +# 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/-/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 should map it to Q with the same 400 scale: +`q = 2 * sigmoid(cp / 400) - 1` (engine inference is not written yet). + +### 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/`. diff --git a/train/value/Cargo.lock b/train/value/Cargo.lock new file mode 100644 index 0000000..84c7925 --- /dev/null +++ b/train/value/Cargo.lock @@ -0,0 +1,601 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "anyhow" +version = "1.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" + +[[package]] +name = "arrayvec" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fb67a6e08acf24fdeccbac2cb6ac4305825bd1f117462e0e6f2f193345ad56" + +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "bitflags" +version = "2.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" + +[[package]] +name = "bullet-compiler" +version = "0.1.0" +dependencies = [ + "half", +] + +[[package]] +name = "bullet-gpu" +version = "0.1.0" +dependencies = [ + "bullet-compiler", +] + +[[package]] +name = "bullet-trainer" +version = "0.1.0" +dependencies = [ + "bullet-compiler", + "bullet-gpu", + "rand_distr", + "rand_xoshiro", +] + +[[package]] +name = "bullet_lib" +version = "1.0.0" +dependencies = [ + "bullet-compiler", + "bullet-gpu", + "bullet-trainer", + "bulletformat", + "montyformat", + "oorandom", + "rand 0.9.5", + "sfbinpack", + "viriformat", +] + +[[package]] +name = "bulletformat" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d171953264e8dc3aa62757255e602e507fdd358a94e3cdacb9e481ff3a1c6b0" + +[[package]] +name = "bumpalo" +version = "3.20.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87a0cf66fd1eb2fb7b822cb78370d82303750809bc6ba855f3f58e517d3238db" + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "crunchy" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" + +[[package]] +name = "equivalent" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00d174d5400e5e8fd687ad1049e2f578285fa914201b1af7e8b112a4546bd826" + +[[package]] +name = "futures-core" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" + +[[package]] +name = "futures-task" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd" + +[[package]] +name = "futures-util" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "slab", +] + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "libc", + "r-efi 5.3.0", + "wasip2", +] + +[[package]] +name = "getrandom" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "libc", + "r-efi 6.0.0", +] + +[[package]] +name = "half" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b" +dependencies = [ + "cfg-if", + "crunchy", + "zerocopy", +] + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + +[[package]] +name = "indexmap" +version = "2.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "js-sys" +version = "0.3.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8dc09d2154098bbf4ef54eab6b117a3c24cbbffe94046b16ee6d6d4510584d3a" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + +[[package]] +name = "libc" +version = "0.2.191" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" + +[[package]] +name = "libm" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "montyformat" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4caf9c718f831a8568f030576e6a6e6aa487ed5f8732ddb0ba79e9a3ea0421ca" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", + "libm", +] + +[[package]] +name = "oorandom" +version = "11.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "rand" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41" +dependencies = [ + "rand_chacha", + "rand_core 0.9.5", +] + +[[package]] +name = "rand" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af" +dependencies = [ + "getrandom 0.4.3", + "rand_core 0.10.1", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core 0.9.5", +] + +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", +] + +[[package]] +name = "rand_core" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" + +[[package]] +name = "rand_distr" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d431c2703ccf129de4d45253c03f49ebb22b97d6ad79ee3ecfc7e3f4862c1d8" +dependencies = [ + "num-traits", + "rand 0.10.3", +] + +[[package]] +name = "rand_xoshiro" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "662effc7698e08ea324d3acccf8d9d7f7bf79b9785e270a174ea36e56900c91d" +dependencies = [ + "rand_core 0.10.1", +] + +[[package]] +name = "raw-cpuid" +version = "11.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "498cd0dc59d73224351ee52a95fee0f1a617a2eae0e7d9d720cc622c73a54186" +dependencies = [ + "bitflags", +] + +[[package]] +name = "rigby-value" +version = "0.1.0" +dependencies = [ + "bullet_lib", +] + +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "serde_spanned" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3" +dependencies = [ + "serde", +] + +[[package]] +name = "sfbinpack" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4db427bcbaeb4745893f6cd842c50f30f21ba28d18bb9bbee48061a2fcaa7e35" +dependencies = [ + "arrayvec", + "js-sys", + "raw-cpuid", + "thiserror", + "wasm-bindgen", +] + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "3.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09e52cb86a36cede5cb101bf8908837b3e4c6e5e59fe7fd85c23fb56200d189e" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe5197923287db20a58125f0bc85c062f7f2c892de97b18c356f9efb14b28524" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "toml" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "toml_write", + "winnow", +] + +[[package]] +name = "toml_write" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801" + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "viriformat" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e889c0fcc2bd229a3b72e11b6b826bef4b9d107ce05f4bc2172d1a87ac5027e" +dependencies = [ + "anyhow", + "arrayvec", + "bulletformat", + "rand 0.9.5", + "serde", + "toml", +] + +[[package]] +name = "wasip2" +version = "1.0.4+wasi-0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0181c14ddd6c7b8b0b0132a5f1ea2f32be0d893cd3ea97b6e3b28886b8a3d1c" +dependencies = [ + "cfg-if", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e931a02a39abdbf66966fc35902b5ed58344750a70885bed4ebb3d8eaf28ed9" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ca4b54280e455bd831e33a6b8e734baf432fe63fbc35c42ac14ce67a043a53a" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn 3.0.10", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b1f9c3798725016c6ee5f2f57869ec0831e8bbc0ab0310a86e787e97f41389e" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "winnow" +version = "0.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945" +dependencies = [ + "memchr", +] + +[[package]] +name = "wit-bindgen" +version = "0.57.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + +[[package]] +name = "zerocopy" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5fe1f8f1b06191a00962174c61aa5005e0bb391a6d80d07e24d115c01a92ed8" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "863ad3ac83293fb4d740aedbfdc9240dd8d1a50c1099acd76ce80ce7c7230c7f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] diff --git a/train/value/Cargo.toml b/train/value/Cargo.toml new file mode 100644 index 0000000..e9e65f5 --- /dev/null +++ b/train/value/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "rigby-value" +version = "0.1.0" +edition = "2024" +publish = false + +# Trains Rigby's value network with bullet on Lc0 data (Stockfish binpack format). +# bullet comes from the local clone next to this repository (C:\Users\rektd\repos\bullet). +[dependencies] +bullet_lib = { path = "../../../bullet/crates/bullet_lib", features = ["cuda"] } + +[profile.release] +opt-level = 3 +lto = true +codegen-units = 1 diff --git a/train/value/src/main.rs b/train/value/src/main.rs new file mode 100644 index 0000000..8fd84f3 --- /dev/null +++ b/train/value/src/main.rs @@ -0,0 +1,125 @@ +//! Rigby value network trainer. +//! +//! Architecture: (768 -> HIDDEN)x2 -> 1, SCReLU, dual perspective. +//! Data: Lc0 self-play positions converted to Stockfish binpacks (linrock/test80). +//! Target: WDL * game result + (1 - WDL) * sigmoid(score / SCALE). +//! +//! Usage: +//! cargo run --release -- [--data ]... [--superbatches N] [--id NAME] +//! +//! The network is written to checkpoints/-/quantised.bin every +//! SAVE_RATE superbatches and at the end. Inference in Rigby has to use the same +//! HIDDEN, SCALE, QA and QB. + +use bullet_lib::{ + game::inputs::Chess768, + nn::optimiser::AdamW, + trainer::{ + save::SavedFormat, + schedule::{TrainingSchedule, TrainingSteps, lr, wdl}, + settings::LocalSettings, + }, + value::{ + ValueTrainerBuilder, + loader::sfbinpack::{MoveType, PieceType, SfBinpackLoader, TrainingDataEntry}, + }, +}; + +const HIDDEN: usize = 128; +const SCALE: i32 = 400; +const QA: i16 = 255; +const QB: i16 = 64; + +const DEFAULT_DATA: &str = "../data/test80-2024-02-feb-2tb7p.min-v2.v6.binpack"; +const DEFAULT_SUPERBATCHES: usize = 40; +const SAVE_RATE: usize = 10; +const WDL: f32 = 0.4; +const LOADER_THREADS: usize = 6; + +/// Keep quiet, settled positions: past the opening, not in check, a quiet best +/// move and a sane score. +fn filter(entry: &TrainingDataEntry) -> bool { + entry.ply >= 16 + && !entry.pos.is_checked(entry.pos.side_to_move()) + && entry.score.unsigned_abs() <= 10000 + && entry.mv.mtype() == MoveType::Normal + && entry.pos.piece_at(entry.mv.to()).piece_type() == PieceType::None +} + +struct Args { + data: Vec, + superbatches: usize, + id: String, +} + +fn parse_args() -> Args { + let mut args = Args { data: Vec::new(), superbatches: DEFAULT_SUPERBATCHES, id: "rigby-value".to_string() }; + let mut it = std::env::args().skip(1); + while let Some(arg) = it.next() { + match arg.as_str() { + "--data" => args.data.push(it.next().expect("--data needs a path")), + "--superbatches" => { + args.superbatches = it.next().expect("--superbatches needs a number").parse().expect("not a number") + } + "--id" => args.id = it.next().expect("--id needs a name"), + other => panic!("unknown argument: {other}"), + } + } + if args.data.is_empty() { + args.data.push(DEFAULT_DATA.to_string()); + } + for path in &args.data { + assert!(std::path::Path::new(path).exists(), "data file not found: {path}"); + } + args +} + +fn main() { + let args = parse_args(); + + let mut trainer = ValueTrainerBuilder::default() + .dual_perspective() + .optimiser(AdamW) + .inputs(Chess768) + .save_format(&[ + SavedFormat::id("l0w").round().quantise::(QA), + SavedFormat::id("l0b").round().quantise::(QA), + SavedFormat::id("l1w").round().quantise::(QB), + SavedFormat::id("l1b").round().quantise::(QA * QB), + ]) + .loss_fn(|output, target| output.sigmoid().squared_error(target)) + .build(|builder, stm_inputs, ntm_inputs| { + let l0 = builder.new_affine("l0", 768, HIDDEN); + let l1 = builder.new_affine("l1", 2 * HIDDEN, 1); + + let stm_hidden = l0.forward(stm_inputs).screlu(); + let ntm_hidden = l0.forward(ntm_inputs).screlu(); + l1.forward(stm_hidden.concat(ntm_hidden)) + }); + + let schedule = TrainingSchedule { + net_id: args.id.clone(), + eval_scale: SCALE as f32, + steps: TrainingSteps { + batch_size: 16_384, + batches_per_superbatch: 6104, + start_superbatch: 1, + end_superbatch: args.superbatches, + }, + wdl_scheduler: wdl::ConstantWDL { value: WDL }, + lr_scheduler: lr::CosineDecayLR { + initial_lr: 0.001, + final_lr: 0.001 * 0.3f32.powi(5), + final_superbatch: args.superbatches, + }, + save_rate: SAVE_RATE, + }; + + let settings = + LocalSettings { threads: LOADER_THREADS, test_set: None, output_directory: "checkpoints", batch_queue_size: 64 }; + + let paths: Vec<&str> = args.data.iter().map(String::as_str).collect(); + let data_loader = SfBinpackLoader::new_concat_multiple(&paths, 1024, LOADER_THREADS, filter); + + trainer.run(&schedule, &settings, &data_loader); +} diff --git a/train/value/train.ps1 b/train/value/train.ps1 new file mode 100644 index 0000000..3da3dc7 --- /dev/null +++ b/train/value/train.ps1 @@ -0,0 +1,36 @@ +# Trains Rigby's value network. Run from anywhere: +# powershell -ExecutionPolicy Bypass -File C:\Users\rektd\repos\Rigby\train\value\train.ps1 +# Extra arguments go to the trainer, e.g.: +# ... train.ps1 --superbatches 60 --id rigby-value-v2 +# ... train.ps1 --data ..\data\a.binpack --data ..\data\b.binpack +param([Parameter(ValueFromRemainingArguments = $true)] [string[]] $TrainerArgs) + +$ErrorActionPreference = 'Stop' +Set-Location $PSScriptRoot + +# CUDA 13.3 (the toolkit bullet links against) and cargo +$env:CUDA_PATH = 'C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.3' +$env:PATH = "$env:USERPROFILE\.cargo\bin;$env:CUDA_PATH\bin\x64;$env:PATH" + +# The bullet clone the trainer builds against +if (-not (Test-Path '..\..\..\bullet\crates\bullet_lib')) { + throw 'bullet not found next to the Rigby repo: git clone https://github.com/jw1912/bullet C:\Users\rektd\repos\bullet' +} + +# Default data: unpack the .zst once if only the compressed file is there +$data = '..\data\test80-2024-02-feb-2tb7p.min-v2.v6.binpack' +if (-not ($TrainerArgs -contains '--data') -and -not (Test-Path $data)) { + if (-not (Test-Path "$data.zst")) { throw "No training data: $data(.zst) is missing" } + Write-Host "Unpacking $data.zst ..." + $src = (Resolve-Path "$data.zst").Path + python -P -c "import zstandard,sys,os; s=sys.argv[1]; d=s[:-4]; zstandard.ZstdDecompressor().copy_stream(open(s,'rb'), open(d+'.part','wb'), read_size=1<<24, write_size=1<<24); os.replace(d+'.part', d)" $src +} + +cargo build --release +if ($LASTEXITCODE -ne 0) { throw 'build failed' } + +& .\target\release\rigby-value.exe @TrainerArgs +if ($LASTEXITCODE -ne 0) { throw "training failed ($LASTEXITCODE)" } + +Write-Host '' +Write-Host 'Done. Networks are in' (Resolve-Path .\checkpoints).Path