From 35af2be36cabba4e280f206a2bdf9f7cf7eb170f Mon Sep 17 00:00:00 2001 From: Kirill Bobyrev Date: Thu, 27 Jun 2024 21:55:04 -0700 Subject: [PATCH] chore: Store progress for further refactoring and MCTS preparation --- .vscode/settings.json | 1 + Cargo.lock | 1110 ++++++++++++++++++++- Cargo.toml | 5 + benches/perft.rs | 4 +- src/evaluation/brain.rs => books/.gitkeep | 0 justfile | 8 +- src/chess/bitboard.rs | 8 +- src/chess/core.rs | 8 +- src/chess/game.rs | 68 ++ src/chess/mod.rs | 2 +- src/chess/position.rs | 73 +- src/chess/state.rs | 82 -- src/engine/mod.rs | 2 +- src/environment.rs | 19 +- src/evaluation/mod.rs | 2 +- src/evaluation/network.rs | 1 + src/lib.rs | 8 +- src/mcts/tree.rs | 12 +- tables/.gitkeep | 0 tests/integration.rs | 4 +- 20 files changed, 1257 insertions(+), 160 deletions(-) rename src/evaluation/brain.rs => books/.gitkeep (100%) create mode 100644 src/chess/game.rs delete mode 100644 src/chess/state.rs create mode 100644 src/evaluation/network.rs create mode 100644 tables/.gitkeep diff --git a/.vscode/settings.json b/.vscode/settings.json index cc93b710f..aae2f95ed 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -57,6 +57,7 @@ "rustfmt", "rustyline", "setoption", + "setposition", "shakmaty", "startpos", "sysinfo", diff --git a/Cargo.lock b/Cargo.lock index 6401cbfb5..cd2cce5e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "aho-corasick" version = "1.1.3" @@ -11,6 +17,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + [[package]] name = "android_system_properties" version = "0.1.5" @@ -81,6 +93,15 @@ version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" +[[package]] +name = "arbitrary" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d5a26814d8dcb93b0e5a0ff3c6d80a8843bafb21b39e8e18a6f05471870e110" +dependencies = [ + "derive_arbitrary", +] + [[package]] name = "arrayvec" version = "0.7.4" @@ -108,12 +129,45 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" +[[package]] +name = "base16ct" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349a06037c7bf932dd7e7d1f653678b2038b9ad46a74102f1fc7bd7872678cce" + +[[package]] +name = "base64" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" + +[[package]] +name = "base64" +version = "0.21.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35636a1494ede3b646cc98f74f8e62c773a38a659ebc777a2cf26b9b74171df9" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "bitflags" version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "bstr" version = "1.9.1" @@ -140,6 +194,71 @@ version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +[[package]] +name = "bytemuck" +version = "1.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b236fc92302c97ed75b38da1f4917b5cdda4984745740f153a5d3059e48d725e" +dependencies = [ + "bytemuck_derive", +] + +[[package]] +name = "bytemuck_derive" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ee891b04274a59bd38b412188e24b849617b2e45a0fd8d057deb63e7403761b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.68", +] + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "candle-core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "311d8dbe293aa3b5c34f6a57727fafd67d17a74fa8b65276501237c233b34ffd" +dependencies = [ + "byteorder", + "gemm", + "half", + "intel-mkl-src", + "libc", + "memmap2", + "num-traits", + "num_cpus", + "rand", + "rand_distr", + "rayon", + "safetensors", + "thiserror", + "yoke", + "zip", +] + +[[package]] +name = "candle-nn" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1efb293365959fc5feacbd93fa918980df2d9f2fb89853af02e33a830c10b49b" +dependencies = [ + "candle-core", + "half", + "intel-mkl-src", + "num-traits", + "rayon", + "safetensors", + "serde", + "thiserror", +] + [[package]] name = "cast" version = "0.3.0" @@ -163,6 +282,20 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-targets 0.48.5", +] + [[package]] name = "ciborium" version = "0.2.2" @@ -209,7 +342,7 @@ dependencies = [ "anstream", "anstyle", "clap_lex", - "strsim", + "strsim 0.11.1", "terminal_size", ] @@ -219,10 +352,10 @@ version = "4.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c780290ccf4fb26629baa7a1081e68ced113f1d3ec302fa5948f1c381ebf06c6" dependencies = [ - "heck", + "heck 0.5.0", "proc-macro2", "quote", - "syn", + "syn 2.0.68", ] [[package]] @@ -269,6 +402,24 @@ version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" +[[package]] +name = "cpufeatures" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" +dependencies = [ + "libc", +] + +[[package]] +name = "crc32fast" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" +dependencies = [ + "cfg-if", +] + [[package]] name = "criterion" version = "0.5.1" @@ -337,6 +488,16 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + [[package]] name = "csv" version = "1.3.0" @@ -358,6 +519,41 @@ dependencies = [ "memchr", ] +[[package]] +name = "darling" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b750cb3417fd1b327431a470f388520309479ab0bf5e323505daf0290cd3850" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim 0.10.0", + "syn 1.0.109", +] + +[[package]] +name = "darling_macro" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e" +dependencies = [ + "darling_core", + "quote", + "syn 1.0.109", +] + [[package]] name = "deranged" version = "0.3.11" @@ -367,6 +563,48 @@ dependencies = [ "powerfmt", ] +[[package]] +name = "derive_arbitrary" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67e77553c4162a157adbf834ebae5b415acbecbeafc7a74b0e886657506a7611" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.68", +] + +[[package]] +name = "derive_builder" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d07adf7be193b71cc36b193d0f5fe60b918a3a9db4dad0449f57bcfd519704a3" +dependencies = [ + "derive_builder_macro", +] + +[[package]] +name = "derive_builder_core" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f91d4cfa921f1c05904dc3c57b4a32c38aed3340cce209f3a6fd1478babafc4" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "derive_builder_macro" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f0314b72bed045f3a68671b3c86328386762c93f82d98c65c3cb5e5f573dd68" +dependencies = [ + "derive_builder_core", + "syn 1.0.109", +] + [[package]] name = "diff" version = "0.1.13" @@ -379,18 +617,87 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "directories" +version = "4.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f51c5d4ddabd36886dd3e1438cb358cdcb0d7c499cb99cb4ac2e38e18b5cb210" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + +[[package]] +name = "displaydoc" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.68", +] + [[package]] name = "doc-comment" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" +[[package]] +name = "dyn-stack" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56e53799688f5632f364f8fb387488dd05db9fe45db7011be066fc20e7027f8b" +dependencies = [ + "bytemuck", + "reborrow", +] + [[package]] name = "either" version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" +[[package]] +name = "enum-as-inner" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ffccbb6966c05b32ef8fbac435df276c4ae4d3dc55a8cd0eb9745e6c12f546a" +dependencies = [ + "heck 0.4.1", + "proc-macro2", + "quote", + "syn 2.0.68", +] + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "errno" version = "0.3.9" @@ -401,6 +708,28 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "filetime" +version = "0.2.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ee447700ac8aa0b2f2bd7bc4462ad686ba06baa6727ac149a2d6277f0d240fd" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "windows-sys 0.52.0", +] + +[[package]] +name = "flate2" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f54427cfd1c7829e2a139fcefea601bf088ebca651d2bf53ebc600eac295dae" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + [[package]] name = "float-cmp" version = "0.9.0" @@ -410,6 +739,12 @@ dependencies = [ "num-traits", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "form_urlencoded" version = "1.2.1" @@ -419,6 +754,134 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "gemm" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ab24cc62135b40090e31a76a9b2766a501979f3070fa27f689c27ec04377d32" +dependencies = [ + "dyn-stack", + "gemm-c32", + "gemm-c64", + "gemm-common", + "gemm-f16", + "gemm-f32", + "gemm-f64", + "num-complex", + "num-traits", + "paste", + "raw-cpuid", + "seq-macro", +] + +[[package]] +name = "gemm-c32" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9c030d0b983d1e34a546b86e08f600c11696fde16199f971cd46c12e67512c0" +dependencies = [ + "dyn-stack", + "gemm-common", + "num-complex", + "num-traits", + "paste", + "raw-cpuid", + "seq-macro", +] + +[[package]] +name = "gemm-c64" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbb5f2e79fefb9693d18e1066a557b4546cd334b226beadc68b11a8f9431852a" +dependencies = [ + "dyn-stack", + "gemm-common", + "num-complex", + "num-traits", + "paste", + "raw-cpuid", + "seq-macro", +] + +[[package]] +name = "gemm-common" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2e7ea062c987abcd8db95db917b4ffb4ecdfd0668471d8dc54734fdff2354e8" +dependencies = [ + "bytemuck", + "dyn-stack", + "half", + "num-complex", + "num-traits", + "once_cell", + "paste", + "pulp", + "raw-cpuid", + "rayon", + "seq-macro", + "sysctl", +] + +[[package]] +name = "gemm-f16" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ca4c06b9b11952071d317604acb332e924e817bd891bec8dfb494168c7cedd4" +dependencies = [ + "dyn-stack", + "gemm-common", + "gemm-f32", + "half", + "num-complex", + "num-traits", + "paste", + "raw-cpuid", + "rayon", + "seq-macro", +] + +[[package]] +name = "gemm-f32" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9a69f51aaefbd9cf12d18faf273d3e982d9d711f60775645ed5c8047b4ae113" +dependencies = [ + "dyn-stack", + "gemm-common", + "num-complex", + "num-traits", + "paste", + "raw-cpuid", + "seq-macro", +] + +[[package]] +name = "gemm-f64" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa397a48544fadf0b81ec8741e5c0fba0043008113f71f2034def1935645d2b0" +dependencies = [ + "dyn-stack", + "gemm-common", + "num-complex", + "num-traits", + "paste", + "raw-cpuid", + "seq-macro", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "getrandom" version = "0.2.15" @@ -430,13 +893,25 @@ dependencies = [ "wasi", ] +[[package]] +name = "getset" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e45727250e75cc04ff2846a66397da8ef2b3db8e40e0cef4df67950a07621eb9" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "git2" version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b903b73e45dc0c6c596f2d37eccece7c1c8bb6e4407b001096387c63d0d93724" dependencies = [ - "bitflags", + "bitflags 2.6.0", "libc", "libgit2-sys", "log", @@ -449,10 +924,26 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6dd08c532ae367adf81c312a4580bc67f1d0fe8bc9c460520283f4c0ff277888" dependencies = [ + "bytemuck", "cfg-if", "crunchy", + "num-traits", + "rand", + "rand_distr", ] +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "heck" version = "0.5.0" @@ -488,6 +979,12 @@ dependencies = [ "cc", ] +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "idna" version = "0.5.0" @@ -498,6 +995,38 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "indexmap" +version = "2.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "intel-mkl-src" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ee70586cd5b3e772a8739a1bd43eaa90d4f4bf0fb2a4edc202e979937ee7f5e" +dependencies = [ + "anyhow", + "intel-mkl-tool", + "ocipkg", +] + +[[package]] +name = "intel-mkl-tool" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "887a16b4537d82227af54d3372971cfa5e0cde53322e60f57584056c16ada1b4" +dependencies = [ + "anyhow", + "log", + "walkdir", +] + [[package]] name = "is-terminal" version = "0.4.12" @@ -563,6 +1092,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.155" @@ -587,6 +1122,17 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" +[[package]] +name = "libredox" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85c833ca1e66078851dba29046874e38f08b2c883700aa29a03ddd3b23814ee8" +dependencies = [ + "bitflags 2.6.0", + "libc", + "redox_syscall", +] + [[package]] name = "libz-sys" version = "1.1.18" @@ -607,9 +1153,9 @@ checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" [[package]] name = "log" -version = "0.4.21" +version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" [[package]] name = "memchr" @@ -617,12 +1163,41 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "memmap2" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe751422e4a8caa417e13c3ea66452215d7d63e19e604f4980461212f3ae1322" +dependencies = [ + "libc", + "stable_deref_trait", +] + +[[package]] +name = "miniz_oxide" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" +dependencies = [ + "adler", +] + [[package]] name = "normalize-line-endings" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "bytemuck", + "num-traits", +] + [[package]] name = "num-conv" version = "0.1.0" @@ -639,6 +1214,37 @@ dependencies = [ "libm", ] +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "num_enum" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02339744ee7253741199f897151b38e72257d13802d4ee837285cc2990a90845" +dependencies = [ + "num_enum_derive", +] + +[[package]] +name = "num_enum_derive" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "681030a937600a36906c185595136d26abfebb4aa9c65701cefcaf8578bb982b" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.68", +] + [[package]] name = "num_threads" version = "0.1.7" @@ -648,6 +1254,46 @@ dependencies = [ "libc", ] +[[package]] +name = "oci-spec" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98135224dd4faeb24c05a2fac911ed53ea6b09ecb09d7cada1cb79963ab2ee34" +dependencies = [ + "derive_builder", + "getset", + "serde", + "serde_json", + "thiserror", +] + +[[package]] +name = "ocipkg" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60cf01280832705a4e4c4d046d9e67a54b900297c69191457a8fc6d198ddefa2" +dependencies = [ + "base16ct", + "base64 0.13.1", + "chrono", + "directories", + "flate2", + "lazy_static", + "log", + "oci-spec", + "regex", + "serde", + "serde_json", + "sha2", + "tar", + "thiserror", + "toml", + "ureq", + "url", + "uuid", + "walkdir", +] + [[package]] name = "once_cell" version = "1.19.0" @@ -667,7 +1313,9 @@ dependencies = [ "anyhow", "arrayvec", "assert_cmd", - "bitflags", + "bitflags 2.6.0", + "candle-core", + "candle-nn", "clap", "criterion", "itertools 0.13.0", @@ -679,6 +1327,12 @@ dependencies = [ "shakmaty", ] +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "percent-encoding" version = "2.3.1" @@ -771,6 +1425,39 @@ dependencies = [ "yansi", ] +[[package]] +name = "proc-macro-crate" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284" +dependencies = [ + "toml_edit", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn 1.0.109", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.86" @@ -780,6 +1467,18 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "pulp" +version = "0.18.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ec8d02258294f59e4e223b41ad7e81c874aa6b15bc4ced9ba3965826da0eed5" +dependencies = [ + "bytemuck", + "libm", + "num-complex", + "reborrow", +] + [[package]] name = "quote" version = "1.0.36" @@ -829,6 +1528,15 @@ dependencies = [ "rand", ] +[[package]] +name = "raw-cpuid" +version = "10.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c297679cb867470fa8c9f67dbba74a78d78e3e98d7cf2b08d6d71540f797332" +dependencies = [ + "bitflags 1.3.2", +] + [[package]] name = "rayon" version = "1.10.0" @@ -849,6 +1557,32 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "reborrow" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03251193000f4bd3b042892be858ee50e8b3719f2b08e5833ac4353724632430" + +[[package]] +name = "redox_syscall" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" +dependencies = [ + "bitflags 1.3.2", +] + +[[package]] +name = "redox_users" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a18479200779601e498ada4e8c1e1f50e3ee19deb0259c25825a98b5603b2cb4" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + [[package]] name = "regex" version = "1.10.5" @@ -878,25 +1612,80 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" +[[package]] +name = "ring" +version = "0.17.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "688c63d65483050968b2a8937f7995f443e27041a0f7700aa59b0822aedebb74" +dependencies = [ + "cc", + "getrandom", + "libc", + "spin", + "untrusted", + "windows-sys 0.48.0", +] + [[package]] name = "rustix" version = "0.38.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" dependencies = [ - "bitflags", + "bitflags 2.6.0", "errno", "libc", "linux-raw-sys", "windows-sys 0.52.0", ] +[[package]] +name = "rustls" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe6b63262c9fcac8659abfaa96cac103d28166d3ff3eaf8f412e19f3ae9e5a48" +dependencies = [ + "log", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-pki-types" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "976295e77ce332211c0d24d92c0e83e50f5c5f046d11082cea19f3df13a3562d" + +[[package]] +name = "rustls-webpki" +version = "0.102.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de2635c8bc2b88d367767c5de8ea1d8db9af3f6219eba28442242d9ab81d1b89" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + [[package]] name = "ryu" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +[[package]] +name = "safetensors" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ced76b22c7fba1162f11a5a75d9d8405264b467a07ae0c9c29be119b9297db9" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "same-file" version = "1.0.6" @@ -906,6 +1695,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "seq-macro" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3f0bf26fd526d2a95683cd0f87bf103b8539e2ca1ef48ce002d67aad59aa0b4" + [[package]] name = "serde" version = "1.0.203" @@ -923,7 +1718,7 @@ checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.68", ] [[package]] @@ -937,6 +1732,17 @@ dependencies = [ "serde", ] +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "shadow-rs" version = "0.29.0" @@ -957,16 +1763,51 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a2320d3932d5b410b2bdee568152b51f2373f018d3425dc7b424ab9b23a5d13" dependencies = [ "arrayvec", - "bitflags", + "bitflags 2.6.0", "btoi", ] +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" + +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "strsim" version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.68" @@ -978,6 +1819,42 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "synstructure" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.68", +] + +[[package]] +name = "sysctl" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec7dddc5f0fee506baf8b9fdb989e242f17e4b11c61dfbb0635b705217199eea" +dependencies = [ + "bitflags 2.6.0", + "byteorder", + "enum-as-inner", + "libc", + "thiserror", + "walkdir", +] + +[[package]] +name = "tar" +version = "0.4.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb797dad5fb5b76fcf519e702f4a589483b5ef06567f160c392832c1f5e44909" +dependencies = [ + "filetime", + "libc", + "xattr", +] + [[package]] name = "terminal_size" version = "0.3.0" @@ -994,6 +1871,26 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" +[[package]] +name = "thiserror" +version = "1.0.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.68", +] + [[package]] name = "time" version = "0.3.36" @@ -1052,6 +1949,38 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" +[[package]] +name = "toml" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_datetime" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4badfd56924ae69bcc9039335b2e017639ce3f9b001c393c1b2d1ef846ce2cbf" + +[[package]] +name = "toml_edit" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1" +dependencies = [ + "indexmap", + "toml_datetime", + "winnow", +] + +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + [[package]] name = "tz-rs" version = "0.6.14" @@ -1108,6 +2037,31 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "ureq" +version = "2.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11f214ce18d8b2cbe84ed3aa6486ed3f5b285cf8d8fbdbce9f3f767a724adc35" +dependencies = [ + "base64 0.21.5", + "flate2", + "log", + "once_cell", + "rustls", + "rustls-pki-types", + "rustls-webpki", + "serde", + "serde_json", + "url", + "webpki-roots", +] + [[package]] name = "url" version = "2.5.2" @@ -1125,12 +2079,27 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "uuid" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5de17fd2f7da591098415cff336e12965a28061ddace43b59cb3c430179c9439" +dependencies = [ + "getrandom", +] + [[package]] name = "vcpkg" version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "wait-timeout" version = "0.2.0" @@ -1177,7 +2146,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn", + "syn 2.0.68", "wasm-bindgen-shared", ] @@ -1199,7 +2168,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.68", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -1220,6 +2189,31 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "webpki-roots" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de2cfda980f21be5a7ed2eadb3e6fe074d56022bea2cdeb1a62eb220fc04188" +dependencies = [ + "rustls-pki-types", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + [[package]] name = "winapi-util" version = "0.1.8" @@ -1229,6 +2223,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-core" version = "0.52.0" @@ -1377,8 +2377,94 @@ version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" +[[package]] +name = "winnow" +version = "0.5.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876" +dependencies = [ + "memchr", +] + +[[package]] +name = "xattr" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8da84f1a25939b27f6820d92aed108f83ff920fdf11a7b19366c27c4cda81d4f" +dependencies = [ + "libc", + "linux-raw-sys", + "rustix", +] + [[package]] name = "yansi" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" + +[[package]] +name = "yoke" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c5b1314b079b0930c31e3af543d8ee1757b1951ae1e1565ec704403a7240ca5" +dependencies = [ + "serde", + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28cc31741b18cb6f1d5ff12f5b7523e3d6eb0852bbbad19d73905511d9849b95" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.68", + "synstructure", +] + +[[package]] +name = "zerofrom" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91ec111ce797d0e0784a1116d0ddcdbea84322cd79e5d5ad173daeba4f93ab55" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ea7b4a3637ea8669cedf0f1fd5c286a17f3de97b8dd5a70a6c167a1730e63a5" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.68", + "synstructure", +] + +[[package]] +name = "zeroize" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" + +[[package]] +name = "zip" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cc23c04387f4da0374be4533ad1208cbb091d5c11d070dfef13676ad6497164" +dependencies = [ + "arbitrary", + "crc32fast", + "crossbeam-utils", + "displaydoc", + "indexmap", + "num_enum", + "thiserror", +] diff --git a/Cargo.toml b/Cargo.toml index 781441234..b30083208 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,10 +59,15 @@ perf = "deny" # invalid_rust_codeblocks = "deny" # unescaped_backticks = "deny" +[features] +mkl = ["candle-core/mkl", "candle-nn/mkl"] + [dependencies] anyhow = "1.0.83" arrayvec = "0.7.2" bitflags = "2.2.1" +candle-core = "0.5.1" +candle-nn = "0.5.1" clap = { version = "4.5.7", features = ["derive", "wrap_help"] } itertools = "0.13.0" # Use SmallRng for performance. diff --git a/benches/perft.rs b/benches/perft.rs index b7b51faf3..048445bd5 100644 --- a/benches/perft.rs +++ b/benches/perft.rs @@ -8,7 +8,7 @@ use shakmaty::{CastlingMode, Chess, Position as ShakmatyPosition}; fn generate_moves(positions: &[Position]) { for position in positions { - criterion::black_box(position.generate_moves()); + std::hint::black_box(position.generate_moves()); } } @@ -68,7 +68,7 @@ fn movegen_bench(c: &mut Criterion) { |b, positions| { b.iter(|| { for position in positions { - criterion::black_box(position.legal_moves()); + std::hint::black_box(position.legal_moves()); } }); }, diff --git a/src/evaluation/brain.rs b/books/.gitkeep similarity index 100% rename from src/evaluation/brain.rs rename to books/.gitkeep diff --git a/justfile b/justfile index b758d655b..b9cfa78fa 100644 --- a/justfile +++ b/justfile @@ -8,18 +8,18 @@ build: run: cargo run --profile=release +# Format all code. fmt: cargo +nightly fmt --all # Checks the code for bad formatting, errors and warnings. lint: cargo +nightly fmt --all -- --check - cargo +nightly clippy --all-targets --all-features + cargo clippy --all-targets --all-features # Runs the linters and tries to apply automatic fixes. -fix: - cargo +nightly fmt --all - cargo +nightly clippy --all-targets --all-features --fix --allow-staged +fix: fmt + cargo clippy --all-targets --all-features --fix --allow-staged # Run most tests in debug mode to (potentially) catch more errors with # debug_assert. diff --git a/src/chess/bitboard.rs b/src/chess/bitboard.rs index ff1e9ea0d..39d322c74 100644 --- a/src/chess/bitboard.rs +++ b/src/chess/bitboard.rs @@ -37,9 +37,10 @@ use crate::chess::core::{Direction, PieceKind, Square, BOARD_SIZE, BOARD_WIDTH}; /// Bitboard is a thin wrapper around [u64]: /// /// ``` -/// use pabi::chess::bitboard::Bitboard; /// use std::mem::size_of; /// +/// use pabi::chess::bitboard::Bitboard; +/// /// assert_eq!(size_of::(), 8); /// ``` #[derive(Copy, Clone, PartialEq, Eq)] @@ -458,7 +459,10 @@ mod tests { assert_eq!(black.queens.bits, 1 << (3 + 8 * 7)); // Rank masks. - assert_eq!(Rank::Rank1.mask() << u32::from(BOARD_WIDTH), Rank::Rank2.mask()); + assert_eq!( + Rank::Rank1.mask() << u32::from(BOARD_WIDTH), + Rank::Rank2.mask() + ); assert_eq!( Rank::Rank5.mask() >> u32::from(BOARD_WIDTH), Rank::Rank4.mask() diff --git a/src/chess/core.rs b/src/chess/core.rs index a9217c035..975a1f01d 100644 --- a/src/chess/core.rs +++ b/src/chess/core.rs @@ -26,14 +26,12 @@ pub struct Move(u16); impl Move { // First 6 bits are reserved for the `from` square. const FROM_MASK: u16 = 0b0000_0000_0011_1111; - - // Next 6 bits are reserved for the `to` square. - const TO_OFFSET: u8 = 6; - const TO_MASK: u16 = 0b0000_1111_1100_0000; - // Next 3 bits are reserved for the promotion (if any). const PROMOTION_MASK: u16 = 0b0111_0000_0000_0000; const PROMOTION_OFFSET: u8 = 12; + const TO_MASK: u16 = 0b0000_1111_1100_0000; + // Next 6 bits are reserved for the `to` square. + const TO_OFFSET: u8 = 6; #[must_use] pub(super) fn new(from: Square, to: Square, promotion: Option) -> Self { diff --git a/src/chess/game.rs b/src/chess/game.rs new file mode 100644 index 000000000..5888da164 --- /dev/null +++ b/src/chess/game.rs @@ -0,0 +1,68 @@ +use super::core::{Color, Move, MoveList}; +use crate::chess::position::Position; +use crate::chess::zobrist::RepetitionTable; +use crate::environment::{Action, Environment, GameResult, Observation}; + +impl Observation for Position {} + +pub struct Game { + position: Position, + perspective: Color, + repetitions: RepetitionTable, + moves: MoveList, +} + +impl Game { + pub(super) fn new(root: Position) -> Self { + let mut repetitions = RepetitionTable::new(); + let _ = repetitions.record(root.hash()); + + let perspective = root.us(); + let moves = root.generate_moves(); + + Self { + position: root, + perspective, + repetitions, + moves, + } + } +} + +impl Environment for Game { + fn actions(&self) -> &[Move] { + &self.moves + } + + fn apply(&mut self, action: impl Action) -> Position { + todo!(); + } + + fn result(&self) -> Option { + // TODO: Check 50-move rule. + // TODO: Check threefold repetition. + // TODO: Check checkmate. + // TODO: Check Syzygy tablebases. + todo!(); + } +} + +impl Action for Move { + fn get_index(&self) -> u16 { + todo!(); + } +} + +#[cfg(test)] +mod tests { + + #[test] + fn detect_repetition() { + todo!(); + } + + #[test] + fn game_result() { + todo!(); + } +} diff --git a/src/chess/mod.rs b/src/chess/mod.rs index 211300788..b543173f3 100644 --- a/src/chess/mod.rs +++ b/src/chess/mod.rs @@ -4,8 +4,8 @@ pub mod attacks; pub mod bitboard; pub mod core; +pub mod game; pub mod position; -pub mod state; pub mod zobrist; mod generated; diff --git a/src/chess/position.rs b/src/chess/position.rs index 71e8e6464..a5556fe05 100644 --- a/src/chess/position.rs +++ b/src/chess/position.rs @@ -11,14 +11,22 @@ use std::fmt::{self, Write}; use anyhow::{bail, Context}; +use super::core::PieceKind; use crate::chess::bitboard::{Bitboard, Pieces}; use crate::chess::core::{ - CastleRights, Color, File, Move, MoveList, Piece, Promotion, Rank, Square, BOARD_WIDTH, + CastleRights, + Color, + File, + Move, + MoveList, + Piece, + Promotion, + Rank, + Square, + BOARD_WIDTH, }; use crate::chess::{attacks, generated, zobrist}; -use super::core::PieceKind; - /// Piece-centric implementation of the chess position, which includes all /// pieces and their placement, information about the castling rights, side to /// move, 50 move rule counters etc. @@ -438,41 +446,37 @@ impl Position { } fn update_castling_rights(&mut self, next_move: &Move) { - if self.castling.contains(CastleRights::WHITE_SHORT) { - if next_move.from() == Square::E1 + if self.castling.contains(CastleRights::WHITE_SHORT) + && (next_move.from() == Square::E1 || next_move.from() == Square::H1 - || next_move.to() == Square::H1 - { - self.castling.remove(CastleRights::WHITE_SHORT); - self.hash ^= generated::WHITE_CAN_CASTLE_SHORT; - } + || next_move.to() == Square::H1) + { + self.castling.remove(CastleRights::WHITE_SHORT); + self.hash ^= generated::WHITE_CAN_CASTLE_SHORT; } - if self.castling.contains(CastleRights::WHITE_LONG) { - if next_move.from() == Square::E1 + if self.castling.contains(CastleRights::WHITE_LONG) + && (next_move.from() == Square::E1 || next_move.from() == Square::A1 - || next_move.to() == Square::A1 - { - self.castling.remove(CastleRights::WHITE_LONG); - self.hash ^= generated::WHITE_CAN_CASTLE_LONG; - } + || next_move.to() == Square::A1) + { + self.castling.remove(CastleRights::WHITE_LONG); + self.hash ^= generated::WHITE_CAN_CASTLE_LONG; } - if self.castling.contains(CastleRights::BLACK_SHORT) { - if next_move.from() == Square::E8 + if self.castling.contains(CastleRights::BLACK_SHORT) + && (next_move.from() == Square::E8 || next_move.from() == Square::H8 - || next_move.to() == Square::H8 - { - self.castling.remove(CastleRights::BLACK_SHORT); - self.hash ^= generated::BLACK_CAN_CASTLE_SHORT; - } + || next_move.to() == Square::H8) + { + self.castling.remove(CastleRights::BLACK_SHORT); + self.hash ^= generated::BLACK_CAN_CASTLE_SHORT; } - if self.castling.contains(CastleRights::BLACK_LONG) { - if next_move.from() == Square::E8 + if self.castling.contains(CastleRights::BLACK_LONG) + && (next_move.from() == Square::E8 || next_move.from() == Square::A8 - || next_move.to() == Square::A8 - { - self.castling.remove(CastleRights::BLACK_LONG); - self.hash ^= generated::BLACK_CAN_CASTLE_LONG; - } + || next_move.to() == Square::A8) + { + self.castling.remove(CastleRights::BLACK_LONG); + self.hash ^= generated::BLACK_CAN_CASTLE_LONG; } } @@ -629,7 +633,7 @@ impl Position { /// Castle or regular king move. // TODO: Merge with the other castling rights handler. - fn make_king_move(self: &mut Self, next_move: &Move) -> bool { + fn make_king_move(&mut self, next_move: &Move) -> bool { let our_pieces = match self.side_to_move { Color::White => &mut self.white_pieces, Color::Black => &mut self.black_pieces, @@ -707,7 +711,7 @@ impl Position { true } - fn make_regular_move(self: &mut Self, next_move: &Move) { + fn make_regular_move(&mut self, next_move: &Move) { let our_pieces = match self.side_to_move { Color::White => &mut self.white_pieces, Color::Black => &mut self.black_pieces, @@ -916,6 +920,9 @@ pub fn perft(position: &Position, depth: u8) -> u64 { if depth == 0 { return 1; } + if depth == 1 { + return position.generate_moves().len() as u64; + } let mut nodes = 0; for next_move in position.generate_moves() { let mut next_position = position.clone(); diff --git a/src/chess/state.rs b/src/chess/state.rs deleted file mode 100644 index e9b004e9d..000000000 --- a/src/chess/state.rs +++ /dev/null @@ -1,82 +0,0 @@ -use arrayvec::ArrayVec; - -use crate::chess::position::Position; -use crate::chess::zobrist::RepetitionTable; - -pub(super) struct State { - position_history: ArrayVec, - repetitions: RepetitionTable, -} - -impl State { - pub(super) fn new(root: Position) -> Self { - let mut repetitions = RepetitionTable::new(); - let _ = repetitions.record(root.hash()); - - let mut position_history = ArrayVec::new(); - position_history.push(root); - - Self { - position_history, - repetitions, - } - } - - #[must_use] - pub(super) fn push(&mut self, position: Position) -> bool { - let draw = self.repetitions.record(position.hash()); - self.position_history.push(position); - draw - } - - #[must_use] - pub(super) fn last(&self) -> &Position { - debug_assert!(!self.position_history.is_empty()); - self.position_history.last().unwrap() - } - - /// Returns the number of full moves since the start of the search. - #[must_use] - pub(super) fn moves(&self) -> u8 { - assert!(!self.position_history.is_empty()); - let plies = self.position_history.len(); - if plies == 1 { - // Only the root is present: no moves have been made. - 0 - } else { - // Two plies per move, excluding the root. - plies as u8 / 2 - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::chess::core::Move; - use crate::chess::position::Position; - - #[test] - fn detect_repetition() { - let mut state = State::new(Position::starting()); - assert_eq!(state.moves(), 0); - - let mut position = Position::starting(); - position.make_move(&Move::from_uci("e2e4").unwrap()); - - assert!(!state.push(position.clone())); - assert_eq!(state.moves(), 1); - - assert!(!state.push(position.clone())); - assert_eq!(state.moves(), 1); - - // 3-fold "repetition" (the same position was pushed multiple times). - assert!(state.push(position.clone())); - assert_eq!(state.moves(), 2); - - position.make_move(&Move::from_uci("e7e5").unwrap()); - // Next move is not a repetition. - assert!(!state.push(position.clone())); - assert_eq!(state.moves(), 2); - } -} diff --git a/src/engine/mod.rs b/src/engine/mod.rs index 58c50182c..3ad988d17 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -175,7 +175,7 @@ impl<'a, R: BufRead, W: Write> Engine<'a, R, W> { Color::White => (wtime, winc), Color::Black => (btime, binc), }; - let next_move = todo!(); + todo!(); } /// Stops the search immediately. diff --git a/src/environment.rs b/src/environment.rs index 0f74a1114..94edc1584 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -1,18 +1,23 @@ -use crate::chess::core::Color; +//! Interface for Reinforcement Learning environment to abstract the chess +//! rules implementation. /// Result of the game from the perspective of the player to move at root. -enum GameResult { +pub enum GameResult { Win, Loss, Draw, } -trait Action: Sized { +// TODO: Require features tensor? +pub trait Observation {} + +pub trait Action: Sized { fn get_index(&self) -> u16; } -trait Environment { - fn get_actions(&self) -> Vec; - fn apply(&mut self, action: impl Action); - fn get_result(&self) -> Option; +/// Standard gym-like Reinforcement Learning environment interface. +pub trait Environment: Sized { + fn actions(&self) -> &[A]; + fn apply(&mut self, action: impl Action) -> O; + fn result(&self) -> Option; } diff --git a/src/evaluation/mod.rs b/src/evaluation/mod.rs index 0bd0bfe92..10146d2f9 100644 --- a/src/evaluation/mod.rs +++ b/src/evaluation/mod.rs @@ -5,7 +5,7 @@ //! //! [evaluation]: https://www.chessprogramming.org/Evaluation -pub(crate) mod brain; pub(crate) mod features; +pub(crate) mod network; pub type QValue = f32; diff --git a/src/evaluation/network.rs b/src/evaluation/network.rs new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/src/evaluation/network.rs @@ -0,0 +1 @@ + diff --git a/src/lib.rs b/src/lib.rs index 40456c8d7..a7e186202 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,16 +5,16 @@ // TODO: Re-export types for convenience. pub mod chess; pub mod engine; +pub mod environment; pub mod evaluation; pub mod mcts; pub use engine::Engine; -use shadow_rs; -use shadow_rs::shadow; -shadow!(build); +shadow_rs::shadow!(build); -/// Features the engine is built with (e.g. build type and target). Produced by `build.rs`. +/// Features the engine is built with (e.g. build type and target). Produced by +/// `build.rs`. const BUILD_FEATURES: &str = include_str!(concat!(env!("OUT_DIR"), "/features")); /// Returns the full engine version that can be used to identify how it was diff --git a/src/mcts/tree.rs b/src/mcts/tree.rs index b6e64bbf5..7798cfe4f 100644 --- a/src/mcts/tree.rs +++ b/src/mcts/tree.rs @@ -1,12 +1,16 @@ -use crate::chess::position::Position; - struct Tree { root: Node, } -struct Node { - position: Position, +type NodeIndex = usize; +// This is a special value that is used to indicate that the node has no parent. +const TOMBSTONE_PARENT: NodeIndex = usize::MAX; +// TODO: Measure the performance and see if switching to ArrayVec will make it +// faster. +struct Node { + parent: NodeIndex, + children: Vec, wins: u32, visits: u32, } diff --git a/tables/.gitkeep b/tables/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/tests/integration.rs b/tests/integration.rs index bafe3842d..de6b2e8a4 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -23,8 +23,8 @@ fn uci_setup() { // #[test] // #[ignore] // fn openbench_output() { -// let mut cmd = Command::cargo_bin(BINARY_NAME).expect("Binary should be built"); -// let _ = cmd.arg("bench"); +// let mut cmd = Command::cargo_bin(BINARY_NAME).expect("Binary should be +// built"); let _ = cmd.arg("bench"); // drop( // cmd.assert()