├── .circleci ├── Dockerfile └── config.yml ├── .clang-format ├── .github └── ISSUE_TEMPLATE │ └── bug_report.md ├── .gitignore ├── .gitmodules ├── AUTHORS ├── CITATION.cff ├── CONTRIBUTING.md ├── COPYING ├── FLAGS.md ├── OpenBench └── Makefile ├── README.md ├── appveyor.yml ├── build-sycl.cmd ├── build.cmd ├── build.sh ├── build_rescorer.cmd ├── changelog.txt ├── cross-files ├── aarch64-darwin ├── aarch64-linux-android ├── armv7a-linux-android └── x86_64-darwin ├── dist ├── README-cuda.txt ├── README-onnx-dml.txt └── install-dml.cmd ├── install_openSUSE_lc0.sh ├── meson.build ├── meson_options.txt ├── openSUSE_install.md ├── pyproject.toml ├── scripts ├── appveyor_android_build.cmd ├── appveyor_android_package.cmd ├── appveyor_win_build.cmd ├── appveyor_win_package.cmd ├── bumpversion.py ├── check_dx.bat ├── check_opencl.bat ├── checkdir.py ├── compile_proto.py ├── gen_py_bindings.py ├── pybind │ ├── __init__.py │ ├── core.py │ ├── exceptions.py │ ├── functions.py │ ├── parameters.py │ ├── retval.py │ └── writer.py └── sycl_build_hack.py ├── src ├── chess │ ├── bitboard.h │ ├── board.cc │ ├── board.h │ ├── board_test.cc │ ├── callbacks.h │ ├── gamestate.cc │ ├── gamestate.h │ ├── pgn.h │ ├── position.cc │ ├── position.h │ ├── position_test.cc │ ├── types.h │ ├── uciloop.cc │ └── uciloop.h ├── engine.cc ├── engine.h ├── engine_loop.cc ├── engine_loop.h ├── engine_test.cc ├── main.cc ├── neural │ ├── backend.cc │ ├── backend.h │ ├── backends │ │ ├── blas │ │ │ ├── README.md │ │ │ ├── blas.h │ │ │ ├── convolution1.cc │ │ │ ├── convolution1.h │ │ │ ├── encoder.h │ │ │ ├── fully_connected_layer.cc │ │ │ ├── fully_connected_layer.h │ │ │ ├── layer_norm.ispc │ │ │ ├── network_blas.cc │ │ │ ├── se_unit.cc │ │ │ ├── se_unit.h │ │ │ ├── winograd_convolution3.cc │ │ │ ├── winograd_convolution3.h │ │ │ └── winograd_transform.ispc │ │ ├── cuda │ │ │ ├── common_kernels.cu │ │ │ ├── cuda_common.h │ │ │ ├── fp16_kernels.cu │ │ │ ├── inputs_outputs.h │ │ │ ├── kernels.h │ │ │ ├── layers.cc │ │ │ ├── layers.h │ │ │ ├── network_cuda.cc │ │ │ ├── network_cudnn.cc │ │ │ ├── readme.txt │ │ │ └── winograd_helper.inc │ │ ├── dx │ │ │ ├── MetaCommand.h │ │ │ ├── dx_common.h │ │ │ ├── layers_dx.cc │ │ │ ├── layers_dx.h │ │ │ ├── network_dx.cc │ │ │ ├── network_dx.h │ │ │ ├── shader_wrapper.cc │ │ │ ├── shader_wrapper.h │ │ │ └── shaders │ │ │ │ ├── AddVectors.hlsl │ │ │ │ ├── Conv1x1.hlsl │ │ │ │ ├── ExpandPlanes.hlsl │ │ │ │ ├── Gemm.hlsl │ │ │ │ ├── PolicyMap.hlsl │ │ │ │ ├── SE.hlsl │ │ │ │ ├── WinogradCommon.h │ │ │ │ ├── WinogradTransform.hlsl │ │ │ │ ├── WinogradTransformSE.hlsl │ │ │ │ ├── dxc_helper.py │ │ │ │ ├── meson.build │ │ │ │ ├── shader_shared.h │ │ │ │ └── shaders.h │ │ ├── metal │ │ │ ├── metal_common.h │ │ │ ├── mps │ │ │ │ ├── MetalNetworkBuilder.h │ │ │ │ ├── MetalNetworkBuilder.mm │ │ │ │ ├── NetworkGraph.h │ │ │ │ └── NetworkGraph.mm │ │ │ ├── network_metal.cc │ │ │ └── network_metal.h │ │ ├── network_check.cc │ │ ├── network_demux.cc │ │ ├── network_mux.cc │ │ ├── network_onnx.cc │ │ ├── network_random.cc │ │ ├── network_record.cc │ │ ├── network_rr.cc │ │ ├── network_tf_cc.cc │ │ ├── network_trivial.cc │ │ ├── onednn │ │ │ ├── layers.cc │ │ │ ├── layers.h │ │ │ └── network_onednn.cc │ │ ├── opencl │ │ │ ├── OpenCL.cc │ │ │ ├── OpenCL.h │ │ │ ├── OpenCLBuffers.cc │ │ │ ├── OpenCLBuffers.h │ │ │ ├── OpenCLParams.h │ │ │ ├── OpenCLTuner.cc │ │ │ ├── OpenCLTuner.h │ │ │ ├── README.md │ │ │ ├── clblast_level3 │ │ │ │ ├── common.opencl │ │ │ │ ├── xgemm_batched.opencl │ │ │ │ ├── xgemm_part1.opencl │ │ │ │ ├── xgemm_part2.opencl │ │ │ │ ├── xgemm_part3.opencl │ │ │ │ └── xgemv.opencl │ │ │ ├── clsource │ │ │ │ ├── config.opencl │ │ │ │ ├── convolve1.opencl │ │ │ │ ├── convolve3.opencl │ │ │ │ ├── policymap.opencl │ │ │ │ └── se.opencl │ │ │ └── network_opencl.cc │ │ ├── shared │ │ │ ├── activation.cc │ │ │ ├── activation.h │ │ │ ├── activation.ispc │ │ │ ├── winograd_filter.cc │ │ │ └── winograd_filter.h │ │ ├── sycl │ │ │ ├── common_kernels.dp.cpp │ │ │ ├── cuBlasContext.h │ │ │ ├── fp16_kernels.dp.cpp │ │ │ ├── inputs_outputs.h │ │ │ ├── kernels.h │ │ │ ├── layers.cc.dp.cpp │ │ │ ├── layers.h │ │ │ ├── network_sycl.cc.dp.cpp │ │ │ ├── sycl_common.h │ │ │ └── winograd_helper.h │ │ └── xla │ │ │ ├── network_xla.cc │ │ │ ├── pjrt.cc │ │ │ ├── pjrt.h │ │ │ ├── xla_runner.cc │ │ │ └── xla_runner.h │ ├── batchsplit.cc │ ├── batchsplit.h │ ├── decoder.cc │ ├── decoder.h │ ├── encoder.cc │ ├── encoder.h │ ├── encoder_test.cc │ ├── factory.cc │ ├── factory.h │ ├── loader.cc │ ├── loader.h │ ├── memcache.cc │ ├── memcache.h │ ├── mock_backend.h │ ├── network.h │ ├── network_legacy.cc │ ├── network_legacy.h │ ├── onnx │ │ ├── adapters.cc │ │ ├── adapters.h │ │ ├── builder.cc │ │ ├── builder.h │ │ ├── converter.cc │ │ ├── converter.h │ │ └── onnx.proto │ ├── register.cc │ ├── register.h │ ├── shared_params.cc │ ├── shared_params.h │ ├── tables │ │ ├── activation_function.h │ │ ├── attention_policy_map.h │ │ └── policy_map.h │ ├── wrapper.cc │ ├── wrapper.h │ └── xla │ │ ├── hlo.proto │ │ ├── hlo_builder.cc │ │ ├── hlo_builder.h │ │ ├── onnx2hlo.cc │ │ ├── onnx2hlo.h │ │ ├── print_hlo.cc │ │ ├── print_hlo.h │ │ ├── xla_tensor.cc │ │ └── xla_tensor.h ├── python │ └── weights.h ├── rescorer_main.cc ├── search │ ├── artifacts.h │ ├── classic │ │ ├── node.cc │ │ ├── node.h │ │ ├── params.cc │ │ ├── params.h │ │ ├── search.cc │ │ ├── search.h │ │ ├── stoppers │ │ │ ├── alphazero.cc │ │ │ ├── alphazero.h │ │ │ ├── common.cc │ │ │ ├── common.h │ │ │ ├── factory.cc │ │ │ ├── factory.h │ │ │ ├── legacy.cc │ │ │ ├── legacy.h │ │ │ ├── simple.cc │ │ │ ├── simple.h │ │ │ ├── smooth.cc │ │ │ ├── smooth.h │ │ │ ├── stoppers.cc │ │ │ ├── stoppers.h │ │ │ ├── timemgr.cc │ │ │ └── timemgr.h │ │ └── wrapper.cc │ ├── dag_classic │ │ ├── node.cc │ │ ├── node.h │ │ ├── params.h │ │ ├── search.cc │ │ ├── search.h │ │ ├── stoppers │ │ │ ├── alphazero.cc │ │ │ ├── alphazero.h │ │ │ ├── common.cc │ │ │ ├── common.h │ │ │ ├── factory.cc │ │ │ ├── factory.h │ │ │ ├── legacy.cc │ │ │ ├── legacy.h │ │ │ ├── simple.cc │ │ │ ├── simple.h │ │ │ ├── smooth.cc │ │ │ ├── smooth.h │ │ │ ├── stoppers.cc │ │ │ ├── stoppers.h │ │ │ ├── timemgr.cc │ │ │ └── timemgr.h │ │ └── wrapper.cc │ ├── instamove │ │ └── instamove.cc │ ├── mock_search.h │ ├── register.cc │ ├── register.h │ └── search.h ├── selfplay │ ├── game.cc │ ├── game.h │ ├── loop.cc │ ├── loop.h │ ├── multigame.cc │ ├── multigame.h │ ├── tournament.cc │ └── tournament.h ├── syzygy │ ├── syzygy.cc │ ├── syzygy.h │ └── syzygy_test.cc ├── tools │ ├── backendbench.cc │ ├── backendbench.h │ ├── benchmark.cc │ ├── benchmark.h │ ├── describenet.cc │ ├── describenet.h │ ├── leela2onnx.cc │ ├── leela2onnx.h │ ├── onnx2leela.cc │ └── onnx2leela.h ├── trainingdata │ ├── reader.cc │ ├── reader.h │ ├── rescorer.cc │ ├── rescorer.h │ ├── trainingdata.cc │ ├── trainingdata.h │ ├── writer.cc │ └── writer.h ├── utils │ ├── atomic_vector.h │ ├── bf16_utils.h │ ├── bititer.h │ ├── cache-old.h │ ├── cache.h │ ├── commandline.cc │ ├── commandline.h │ ├── configfile.cc │ ├── configfile.h │ ├── cppattributes.h │ ├── esc_codes.cc │ ├── esc_codes.h │ ├── exception.h │ ├── fastmath.h │ ├── files.cc │ ├── files.h │ ├── filesystem.h │ ├── filesystem.posix.cc │ ├── filesystem.win32.cc │ ├── fp16_utils.h │ ├── fp8_utils.h │ ├── hashcat.h │ ├── hashcat_test.cc │ ├── histogram.cc │ ├── histogram.h │ ├── logging.cc │ ├── logging.h │ ├── mutex.h │ ├── numa.cc │ ├── numa.h │ ├── optionsdict.cc │ ├── optionsdict.h │ ├── optionsparser.cc │ ├── optionsparser.h │ ├── optionsparser_test.cc │ ├── protomessage.cc │ ├── protomessage.h │ ├── random.cc │ ├── random.h │ ├── smallarray.h │ ├── spinhelper.h │ ├── string.cc │ ├── string.h │ ├── transpose.h │ ├── weights_adapter.cc │ └── weights_adapter.h ├── version.cc ├── version.h └── version.inc ├── subprojects ├── abseil-cpp.wrap ├── eigen.wrap ├── gaviotatb.wrap ├── gtest.wrap ├── packagefiles │ └── gaviotatb │ │ └── meson.build ├── protobuf-3.6.0.wrap ├── protobuf.wrap └── zlib.wrap ├── tensorflow.md ├── third_party ├── d3dx12.h ├── opencl.hpp └── pjrt_c_api.h └── windows_build.md /.circleci/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM floopcz/tensorflow_cc:ubuntu-shared-cuda 2 | ARG DEBIAN_FRONTEND=noninteractive 3 | 4 | RUN wget https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB && apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB && sh -c 'echo deb https://apt.repos.intel.com/mkl all main > /etc/apt/sources.list.d/intel-mkl.list' && apt-get update && apt-get install -y intel-mkl-64bit-2018.2-046 5 | RUN apt-get install -y clang ninja-build python3-pip nvidia-opencl-dev libopenblas-dev libboost-dev libgtest-dev git ssh tar gzip ca-certificates libeigen3-dev sudo 6 | RUN apt-get install -y g++-8 7 | RUN apt-get install -y cuda 8 | RUN pip3 install meson 9 | RUN ln -s /usr/include/ /usr/include/openblas 10 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | jobs: 3 | build: 4 | docker: 5 | - image: nvidia/cuda:11.6.1-cudnn8-devel-ubuntu20.04 6 | steps: 7 | - checkout 8 | - run: 9 | name: Install build tools 10 | command: | 11 | apt-get update 12 | apt-get -y install git python3-pip gcc-10 g++-10 clang-12 zlib1g zlib1g-dev 13 | pip3 install meson==0.63 14 | pip3 install ninja 15 | - run: 16 | name: "Pull Submodules" 17 | command: git submodule update --init 18 | - run: 19 | name: Meson GCC 20 | environment: 21 | CC: gcc-10 22 | CXX: g++-10 23 | command: meson build-gcc -Dgtest=false 24 | - run: 25 | name: Meson Clang 26 | environment: 27 | CC: clang-12 28 | CXX: clang++-12 29 | command: meson build-clang -Dgtest=false -Db_lto=false 30 | - run: 31 | name: Build GCC 32 | command: | 33 | cd build-gcc 34 | ninja -j 4 35 | - run: 36 | name: Build Clang 37 | command: | 38 | cd build-clang 39 | ninja -j 4 40 | "mac": 41 | macos: 42 | xcode: 14.1.0 43 | resource_class: macos.m1.medium.gen1 44 | steps: 45 | - checkout 46 | - run: 47 | name: "Pull Submodules" 48 | command: git submodule update --init 49 | - run: 50 | name: Install build tools 51 | command: | 52 | pip3 install meson==1.3.0 53 | pip3 install ninja 54 | curl -LJOs https://github.com/ispc/ispc/releases/download/v1.21.0/ispc-v1.21.0-macOS.universal.tar.gz 55 | tar xf ispc-v1.21.0-macOS.universal.tar.gz 56 | cp ispc-v1.21.0-macOS.universal/bin/ispc /usr/local/bin 57 | - run: 58 | name: Build lc0 59 | command: | 60 | meson build --buildtype=release -Dgtest=false -Dopencl=false --cross-file cross-files/x86_64-darwin 61 | cd build 62 | ninja 63 | - run: 64 | name: Build lc0 arm 65 | command: | 66 | meson build-arm --buildtype=release -Dgtest=false -Dopencl=false 67 | cd build-arm 68 | ninja 69 | - run: 70 | name: Make universal binary 71 | command: lipo -create -o /tmp/lc0 build/lc0 build-arm/lc0 72 | - store_artifacts: 73 | path: /tmp/lc0 74 | destination: lc0-macos_12.6.1 75 | workflows: 76 | version: 2 77 | builds: 78 | jobs: 79 | - build 80 | - "mac" 81 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | Language: Cpp 3 | BasedOnStyle: Google 4 | DerivePointerAlignment: false 5 | --- 6 | Language: Proto 7 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **WARNING: Please only use GitHub issues for BUG REPORTS and FEATURE REQUESTS** 11 | Lengthy algorithm discussions etc are fine too, but also consider creating a wiki page or a page on website. 12 | 13 | Here is what to do with other types of questions: 14 | 1. Build problems: ask in #help channel in our discord: https://discord.gg/pKujYxD 15 | 2. Configuration questions: ask in #help channel in our discord: https://discord.gg/pKujYxD 16 | 3. Running problems: ask in #help channel in our discord: https://discord.gg/pKujYxD 17 | 18 | 19 | If you are filing a bug report, please fill the fields below. 20 | Otherwise, feel free to remove this text and type a free-form issue as usual. 21 | 22 | BUG REPORT 23 | 24 | **Describe the bug** 25 | A clear and concise description of what the bug is. 26 | 27 | **Steps to Reproduce** 28 | 1. 29 | 2. 30 | 3. 31 | 4. 32 | Expected behavior: 33 | Observed behavior: 34 | 35 | **Lc0 version** 36 | Include Lc0 version/operating system/backend type. 37 | 38 | **Lc0 parameters** 39 | Command line, if not default. 40 | Include screenshot of configuration window, if using through GUI. 41 | 42 | **Hardware** 43 | * Number and model of GPUs that you use. 44 | * Amount of RAM in the system 45 | * Other specs (CPU etc) if it may be relevant 46 | 47 | **Lc0 logs** 48 | Please attach Lc0 logs. Here is how to produce them (e.g. for D:\logfile.txt): 49 | 50 | Set the following UCI option: 51 | **Logfile:** D:\\logfile.txt 52 | OR 53 | pass this as a command line argument: 54 | `--logfile=D:\logfile.txt` 55 | OR 56 | Create **lc0.config** file in the same directory as your **lc0.exe** is located, with the following contents: 57 | ``` 58 | logfile=D:\logfile.txt 59 | ``` 60 | 61 | After running Lc0, **D:\logfile.txt** should appear. 62 | 63 | 64 | **Chess GUI logs** 65 | If there is a problem with particular GUI (cutechess/arena/etc), also attach logs of that program. 66 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | !subprojects/*.wrap 2 | *.swp 3 | .clang_complete 4 | .DS_Store 5 | .cache/ 6 | .clangd/ 7 | build/ 8 | builddir/ 9 | __pycache__/ 10 | compile_commands.json 11 | CUDA_NN/ 12 | lc0.xcodeproj/ 13 | LC0VSProj/ 14 | src/.vs/ 15 | subprojects/* 16 | testdata/ 17 | xcuserdata 18 | .clang-tidy 19 | compile_flags.txt 20 | .vscode 21 | .mesonpy* -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "libs/lczero-common"] 2 | path = libs/lczero-common 3 | url = https://github.com/LeelaChessZero/lczero-common.git 4 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | A-childs-encyclopedia 2 | Adam Treat 3 | Alex Greason 4 | Alexander Lyashuk 5 | Alexis Olson 6 | alice 7 | almaudoh 8 | Aloril 9 | Andrew Grant 10 | Andy Olsen 11 | Ankan 12 | Ankan Banerjee 13 | Anson Hu 14 | Asger Alstrup Palm 15 | baum 16 | benini 17 | borg323 18 | Boštjan Mejak 19 | Brandon Lin 20 | Brett Holman 21 | Carlo Wood 22 | cn4750 23 | Cong 24 | Contrad Namiseb (Bonan) 25 | cwbriscoe 26 | danegraphics 27 | Daniel Monroe 28 | Daniel Uranga 29 | Dieter Dobbelaere 30 | dje-dev 31 | Dominik Schlösser 32 | DU-jdto 33 | dubslow 34 | Dubslow 35 | Ed Lee 36 | Epanek 37 | Error323 38 | evalon32 39 | exa 40 | exoticorn 41 | F. Huizinga 42 | fischerandom 43 | fohristiwhirl 44 | Francis 45 | Francis Li 46 | Francois 47 | Francois Pays 48 | François Pays 49 | Ganesh Krishnan 50 | GBeauregard 51 | Gian-Carlo Pascutto 52 | gmorenz 53 | Google LLC 54 | gsobala 55 | Hace 56 | Hans Ekbrand 57 | Henrik Forstén 58 | Ikko Eltociear Ashimine 59 | Jack Thomson 60 | James Horsfall Thomas 61 | jjoshua2 62 | John Newlin 63 | Karl Kfoury 64 | kiilas 65 | Kip Hamiltons 66 | Kovax 67 | Leandro Álvarez González 68 | Linmiao Xu 69 | Lisa Butterfly 70 | Luka Rahne 71 | Marcin Stefaniuk 72 | Martin 73 | Martin Senft 74 | masterkni6 75 | masterkni666 76 | Mike Roberts 77 | Naphthalin 78 | nathan-lc0 79 | Neelesh Jain 80 | nguyenpham 81 | noobpwnftw 82 | Ofek Shochat 83 | oscardssmith 84 | Pan 85 | patrik-ha 86 | PaulJeFi 87 | Pratik Dixit 88 | QxC4eva 89 | Raj 90 | Reece H. Dunn 91 | Ron Wolf 92 | Sami Kiminki 93 | Shreyas Kapur 94 | shtayerc 95 | Simon 96 | slash 97 | students 98 | SunnyWar 99 | TesseractA 100 | Tilps 101 | Timofey Kondrashov 102 | Ting-Hsuan Huang 103 | Tony Su 104 | trre123 105 | Usman Haider 106 | uyhcire 107 | Valentin 108 | Valeriy Huz 109 | Victor Popovici 110 | Videodr0me 111 | Viren6 112 | Yan Zhang 113 | zz4032 -------------------------------------------------------------------------------- /CITATION.cff: -------------------------------------------------------------------------------- 1 | cff-version: 1.2.0 2 | title: LeelaChessZero 3 | type: software 4 | authors: 5 | - name: The LCZero Authors 6 | repository-code: 'https://github.com/LeelaChessZero/lc0' 7 | url: 'https://lczero.org/' 8 | repository-artifact: 'https://github.com/LeelaChessZero/lc0/releases/' 9 | abstract: >- 10 | Lc0 is a UCI-compliant chess engine designed to play chess 11 | via neural network, specifically those of the 12 | LeelaChessZero project. 13 | keywords: 14 | - chess 15 | - neural networks (NN) 16 | - artificial intelligence (AI) 17 | license: GPL-3.0 18 | -------------------------------------------------------------------------------- /OpenBench/Makefile: -------------------------------------------------------------------------------- 1 | ifndef EXE 2 | EXE = $(CURDIR)/lc0 3 | endif 4 | 5 | BUILD_FLAGS = 6 | ifdef EVALFILE 7 | BUILD_FLAGS += -Dembed=true 8 | endif 9 | ifdef SEARCH 10 | BUILD_FLAGS += -Ddefault_search=$(SEARCH) 11 | endif 12 | 13 | POST_BUILD_COMMANDS = 14 | ifdef EVALFILE 15 | POST_BUILD_COMMANDS = \ 16 | cat $(EVALFILE) >> $(EXE); \ 17 | perl -e "printf '%sLc0!', pack('V', -s '$(EVALFILE)')" >> $(EXE) 18 | endif 19 | 20 | .PHONY: all 21 | all: 22 | chmod +x ../build.sh 23 | ../build.sh $(BUILD_FLAGS) && mv ../build/release/lc0 $(EXE) 24 | $(POST_BUILD_COMMANDS) -------------------------------------------------------------------------------- /build-sycl.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal 3 | 4 | rem 1. Set the following for the options you want to build. 5 | rem SYCL can be off, l0, amd or nvidia. 6 | set SYCL=l0 7 | set CUDNN=true 8 | set CUDA=true 9 | set DX12=false 10 | set OPENCL=false 11 | set MKL=false 12 | set DNNL=true 13 | set OPENBLAS=false 14 | set EIGEN=false 15 | set TEST=false 16 | 17 | rem 2. Edit the paths for the build dependencies. 18 | set CUDA_PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0 19 | set CUDNN_PATH=%CUDA_PATH% 20 | set OPENBLAS_PATH=C:\OpenBLAS 21 | set MKL_PATH=C:\Program Files (x86)\Intel\oneAPI\mkl\latest\ 22 | set DNNL_PATH=C:\Program Files (x86)\Intel\oneAPI\dnnl\latest\cpu_iomp 23 | set OPENCL_LIB_PATH=%CUDA_PATH%\lib\x64 24 | set OPENCL_INCLUDE_PATH=%CUDA_PATH%\include 25 | 26 | rem 3. In most cases you won't need to change anything further down. 27 | echo Deleting build directory: 28 | rd /s build 29 | 30 | rem Use cl for C files to get a resource compiler as needed for zlib. 31 | set CC=cl 32 | set CXX=icx 33 | 34 | set BLAS=true 35 | if %MKL%==false if %DNNL%==false if %OPENBLAS%==false if %EIGEN%==false set BLAS=false 36 | 37 | if "%CUDA_PATH%"=="%CUDNN_PATH%" ( 38 | set CUDNN_LIB_PATH=%CUDNN_PATH%\lib\x64 39 | set CUDNN_INCLUDE_PATH=%CUDNN_PATH%\include 40 | ) else ( 41 | set CUDNN_LIB_PATH=%CUDA_PATH%\lib\x64,%CUDNN_PATH%\lib\x64 42 | set CUDNN_INCLUDE_PATH=%CUDA_PATH%\include,%CUDNN_PATH%\include 43 | ) 44 | 45 | if %CUDNN%==true set PATH=%CUDA_PATH%\bin;%PATH% 46 | 47 | meson setup build --buildtype release -Ddx=%DX12% -Dcudnn=%CUDNN% -Dplain_cuda=%CUDA% ^ 48 | -Dopencl=%OPENCL% -Dblas=%BLAS% -Dmkl=%MKL% -Dopenblas=%OPENBLAS% -Ddnnl=%DNNL% -Dgtest=%TEST% ^ 49 | -Dcudnn_include="%CUDNN_INCLUDE_PATH%" -Dcudnn_libdirs="%CUDNN_LIB_PATH%" ^ 50 | -Dmkl_include="%MKL_PATH%\include" -Dmkl_libdirs="%MKL_PATH%\lib\intel64" -Ddnnl_dir="%DNNL_PATH%" ^ 51 | -Dopencl_libdirs="%OPENCL_LIB_PATH%" -Dopencl_include="%OPENCL_INCLUDE_PATH%" ^ 52 | -Dopenblas_include="%OPENBLAS_PATH%\include" -Dopenblas_libdirs="%OPENBLAS_PATH%\lib" ^ 53 | -Ddefault_library=static -Dsycl=%SYCL% -Db_vscrt=md 54 | 55 | if errorlevel 1 exit /b 56 | 57 | pause 58 | 59 | cd build 60 | 61 | ninja -------------------------------------------------------------------------------- /build.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal 3 | 4 | rem 1. Set the following for the options you want to build. 5 | set CUDNN=true 6 | set CUDA=true 7 | set DX12=false 8 | set OPENCL=false 9 | set MKL=false 10 | set DNNL=false 11 | set OPENBLAS=false 12 | set EIGEN=false 13 | set TEST=false 14 | 15 | rem 2. Edit the paths for the build dependencies. 16 | set CUDA_PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0 17 | set CUDNN_PATH=%CUDA_PATH% 18 | set OPENBLAS_PATH=C:\OpenBLAS 19 | set MKL_PATH=C:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows\mkl 20 | set DNNL_PATH=C:\dnnl_win_1.1.1_cpu_vcomp 21 | set OPENCL_LIB_PATH=%CUDA_PATH%\lib\x64 22 | set OPENCL_INCLUDE_PATH=%CUDA_PATH%\include 23 | 24 | rem 3. In most cases you won't need to change anything further down. 25 | echo Deleting build directory: 26 | rd /s build 27 | 28 | set CC=cl 29 | set CXX=cl 30 | set CC_LD=link 31 | set CXX_LD=link 32 | 33 | if exist "C:\Program Files\Microsoft Visual Studio\2022" ( 34 | where /q cl 35 | if errorlevel 1 call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64 36 | set backend=vs2022 37 | ) else if exist "C:\Program Files (x86)\Microsoft Visual Studio\2019" ( 38 | where /q cl 39 | if errorlevel 1 call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64 40 | set backend=vs2019 41 | ) else ( 42 | where /q cl 43 | if errorlevel 1 call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64 44 | set backend=vs2017 45 | ) 46 | 47 | set BLAS=true 48 | if %MKL%==false if %DNNL%==false if %OPENBLAS%==false if %EIGEN%==false set BLAS=false 49 | 50 | if "%CUDA_PATH%"=="%CUDNN_PATH%" ( 51 | set CUDNN_LIB_PATH=%CUDNN_PATH%\lib\x64 52 | set CUDNN_INCLUDE_PATH=%CUDNN_PATH%\include 53 | ) else ( 54 | set CUDNN_LIB_PATH=%CUDA_PATH%\lib\x64,%CUDNN_PATH%\lib\x64 55 | set CUDNN_INCLUDE_PATH=%CUDA_PATH%\include,%CUDNN_PATH%\include 56 | ) 57 | 58 | if %CUDNN%==true set PATH=%CUDA_PATH%\bin;%PATH% 59 | 60 | meson setup build --backend %backend% --buildtype release -Ddx=%DX12% -Dcudnn=%CUDNN% -Dplain_cuda=%CUDA% ^ 61 | -Dopencl=%OPENCL% -Dblas=%BLAS% -Dmkl=%MKL% -Dopenblas=%OPENBLAS% -Ddnnl=%DNNL% -Dgtest=%TEST% ^ 62 | -Dcudnn_include="%CUDNN_INCLUDE_PATH%" -Dcudnn_libdirs="%CUDNN_LIB_PATH%" ^ 63 | -Dmkl_include="%MKL_PATH%\include" -Dmkl_libdirs="%MKL_PATH%\lib\intel64" -Ddnnl_dir="%DNNL_PATH%" ^ 64 | -Dopencl_libdirs="%OPENCL_LIB_PATH%" -Dopencl_include="%OPENCL_INCLUDE_PATH%" ^ 65 | -Dopenblas_include="%OPENBLAS_PATH%\include" -Dopenblas_libdirs="%OPENBLAS_PATH%\lib" ^ 66 | -Ddefault_library=static 67 | 68 | if errorlevel 1 exit /b 69 | 70 | pause 71 | 72 | cd build 73 | 74 | msbuild /m /p:Configuration=Release /p:Platform=x64 /p:WholeProgramOptimization=true ^ 75 | /p:PreferredToolArchitecture=x64 lc0.sln /filelogger 76 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | 5 | # Move to this script's directory. 6 | CDPATH= cd -- "$(dirname -- "$0")" 7 | 8 | case $1 in 9 | plain|debug|debugoptimized|release|minsize) 10 | BUILDTYPE=$1 11 | shift 12 | ;; 13 | *) 14 | BUILDTYPE=release 15 | ;; 16 | esac 17 | 18 | BUILDDIR=build/${BUILDTYPE} 19 | 20 | MESON=$(PATH="${PATH}:${HOME}/.local/bin" command -v meson || :) 21 | MESON=${MESON:?"Could not find meson. Is it installed and in PATH?"} 22 | 23 | if [ -f "${BUILDDIR}/build.ninja" ] 24 | then 25 | "${MESON}" configure "${BUILDDIR}" -Dbuildtype="${BUILDTYPE}" -Dprefix="${INSTALL_PREFIX:-/usr/local}" "$@" 26 | else 27 | "${MESON}" "${BUILDDIR}" --buildtype "${BUILDTYPE}" --prefix "${INSTALL_PREFIX:-/usr/local}" "$@" 28 | fi 29 | 30 | "${MESON}" compile -C "${BUILDDIR}" 31 | 32 | if [ -n "${INSTALL_PREFIX}" ] 33 | then 34 | "${MESON}" install -C "${BUILDDIR}" 35 | fi 36 | -------------------------------------------------------------------------------- /build_rescorer.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal 3 | 4 | echo Deleting build directory: 5 | rd /s build 6 | 7 | set CC=cl 8 | set CXX=cl 9 | set CC_LD=link 10 | set CXX_LD=link 11 | 12 | if exist "C:\Program Files\Microsoft Visual Studio\2022" ( 13 | where /q cl 14 | if errorlevel 1 call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64 15 | set backend=vs2022 16 | ) else if exist "C:\Program Files (x86)\Microsoft Visual Studio\2019" ( 17 | where /q cl 18 | if errorlevel 1 call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64 19 | set backend=vs2019 20 | ) else ( 21 | where /q cl 22 | if errorlevel 1 call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64 23 | set backend=vs2017 24 | ) 25 | 26 | meson setup build --backend %backend% --buildtype release -Drescorer=true -Dlc0=false -Dgtest=false -Ddefault_library=static 27 | 28 | if errorlevel 1 exit /b 29 | 30 | pause 31 | 32 | cd build 33 | 34 | msbuild /m /p:Configuration=Release /p:Platform=x64 /p:WholeProgramOptimization=true ^ 35 | /p:PreferredToolArchitecture=x64 lc0.sln /filelogger 36 | -------------------------------------------------------------------------------- /cross-files/aarch64-darwin: -------------------------------------------------------------------------------- 1 | 2 | [host_machine] 3 | system = 'darwin' 4 | cpu_family = 'aarch64' 5 | cpu = 'aarch64' 6 | endian = 'little' 7 | 8 | [properties] 9 | 10 | 11 | [binaries] 12 | c = 'clang' 13 | cpp = 'clang++' 14 | objc = 'clang' 15 | objcpp = 'clang++' 16 | ar = 'ar' 17 | ld = 'ld' 18 | 19 | [built-in options] 20 | c_args = ['-arch', 'arm64'] 21 | cpp_args = ['-arch', 'arm64'] 22 | objc_args = ['-arch', 'arm64'] 23 | objcpp_args = ['-arch', 'arm64'] 24 | c_link_args = ['-arch', 'arm64'] 25 | cpp_link_args = ['-arch', 'arm64'] 26 | objc_link_args = ['-arch', 'arm64'] 27 | objcpp_link_args = ['-arch', 'arm64'] 28 | -------------------------------------------------------------------------------- /cross-files/aarch64-linux-android: -------------------------------------------------------------------------------- 1 | 2 | # Tested with Android NDK r27c, default toolchain 3 | # Targeting API level 21 4 | 5 | # Set the toolchain path on your environment 6 | # export PATH="$HOME/.local/share/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH" 7 | 8 | [host_machine] 9 | system = 'android' 10 | cpu_family = 'aarch64' 11 | cpu = 'aarch64' 12 | endian = 'little' 13 | 14 | [properties] 15 | cpp_link_args = ['-llog', '-static-libstdc++'] 16 | 17 | [binaries] 18 | c = 'aarch64-linux-android21-clang' 19 | cpp = 'aarch64-linux-android21-clang++' 20 | ar = 'llvm-ar' 21 | strip = 'llvm-strip' 22 | ld = 'ld' 23 | ranlib = 'llvm-ranlib' 24 | as = 'aarch64-linux-android21-clang' 25 | -------------------------------------------------------------------------------- /cross-files/armv7a-linux-android: -------------------------------------------------------------------------------- 1 | 2 | # Tested with Android NDK r27c, default toolchain 3 | # Targeting API level 21 4 | 5 | # When targeting API levels < 24 the build fails unless _FILE_OFFSET_BITS is unset. 6 | # Meson passes _FILE_OFFSET_BITS=64 but recent NDK toolchains have issues building 7 | # for 32-bit ABIs when such macro it set. Relevant links: 8 | # https://android.googlesource.com/platform/bionic/+/master/docs/32-bit-abi.md 9 | # https://github.com/mesonbuild/meson/pull/2996#issuecomment-384045808 10 | 11 | # Set the toolchain path on your environment 12 | # export PATH="$HOME/.local/share/android-sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH" 13 | 14 | [host_machine] 15 | system = 'android' 16 | cpu_family = 'arm' 17 | cpu = 'armv7a' 18 | endian = 'little' 19 | 20 | [properties] 21 | cpp_args = ['-U_FILE_OFFSET_BITS'] 22 | cpp_link_args = ['-llog', '-static-libstdc++'] 23 | 24 | [binaries] 25 | c = 'armv7a-linux-androideabi21-clang' 26 | cpp = 'armv7a-linux-androideabi21-clang++' 27 | ar = 'llvm-ar' 28 | strip = 'llvm-strip' 29 | ld = 'ld' 30 | ranlib = 'llvm-ranlib' 31 | as = 'armv7a-linux-androideabi21-clang' 32 | -------------------------------------------------------------------------------- /cross-files/x86_64-darwin: -------------------------------------------------------------------------------- 1 | 2 | [host_machine] 3 | system = 'darwin' 4 | cpu_family = 'x86_64' 5 | cpu = 'x86_64' 6 | endian = 'little' 7 | 8 | [properties] 9 | 10 | 11 | [binaries] 12 | c = 'clang' 13 | cpp = 'clang++' 14 | objc = 'clang' 15 | objcpp = 'clang++' 16 | ar = 'ar' 17 | ld = 'ld' 18 | 19 | [built-in options] 20 | c_args = ['-arch', 'x86_64'] 21 | cpp_args = ['-arch', 'x86_64'] 22 | objc_args = ['-arch', 'x86_64'] 23 | objcpp_args = ['-arch', 'x86_64'] 24 | c_link_args = ['-arch', 'x86_64'] 25 | cpp_link_args = ['-arch', 'x86_64'] 26 | objc_link_args = ['-arch', 'x86_64'] 27 | objcpp_link_args = ['-arch', 'x86_64'] 28 | -------------------------------------------------------------------------------- /dist/README-cuda.txt: -------------------------------------------------------------------------------- 1 | Lc0 2 | 3 | Lc0 is a UCI-compliant chess engine designed to play chess via 4 | neural network, specifically those of the LeelaChessZero project 5 | (https://lczero.org). 6 | 7 | This binary uses CUDA and cuDNN dynamic link libraries copyrighted 8 | by Nvidia corporation (http://www.nvidia.com), and redistributed as 9 | permitted by the respective license file (see CUDA.txt section 2.2 10 | and CUDNN.txt section "CUDNN DISTRIBUTION" for details). You are 11 | authorized to redistribute these libraries together with this 12 | package as a whole but not individually. 13 | 14 | 15 | License 16 | 17 | Leela Chess is free software: you can redistribute it and/or modify 18 | it under the terms of the GNU General Public License as published by 19 | the Free Software Foundation, either version 3 of the License, or 20 | (at your option) any later version. 21 | 22 | Leela Chess is distributed in the hope that it will be useful, 23 | but WITHOUT ANY WARRANTY; without even the implied warranty of 24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25 | GNU General Public License for more details. 26 | 27 | You should have received a copy of the GNU General Public License 28 | along with Leela Chess. If not, see . 29 | 30 | Additional permission under GNU GPL version 3 section 7 31 | 32 | If you modify this Program, or any covered work, by linking or 33 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 34 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 35 | modified version of those libraries), containing parts covered by the 36 | terms of the respective license agreement, the licensors of this 37 | Program grant you additional permission to convey the resulting work. 38 | 39 | -------------------------------------------------------------------------------- /dist/README-onnx-dml.txt: -------------------------------------------------------------------------------- 1 | Lc0 2 | 3 | Lc0 is a UCI-compliant chess engine designed to play chess via 4 | neural network, specifically those of the LeelaChessZero project 5 | (https://lczero.org). 6 | 7 | To run this version you will most likely need a very recent DirectML dll, 8 | which you can get by running the included `install.cmd` script. Alternatively, 9 | you can download the currently latest nuget installer package from 10 | . 11 | If you don't know how to use nuget installer packages, you can change the 12 | extension to .zip and open it as a normal zip file, the dll you need is 13 | `/bin/x64-win/DirectML.dll`. 14 | 15 | License 16 | 17 | Leela Chess is free software: you can redistribute it and/or modify 18 | it under the terms of the GNU General Public License as published by 19 | the Free Software Foundation, either version 3 of the License, or 20 | (at your option) any later version. 21 | 22 | Leela Chess is distributed in the hope that it will be useful, 23 | but WITHOUT ANY WARRANTY; without even the implied warranty of 24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25 | GNU General Public License for more details. 26 | 27 | You should have received a copy of the GNU General Public License 28 | along with Leela Chess. If not, see . 29 | 30 | Additional permission under GNU GPL version 3 section 7 31 | 32 | If you modify this Program, or any covered work, by linking or 33 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 34 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 35 | modified version of those libraries), containing parts covered by the 36 | terms of the respective license agreement, the licensors of this 37 | Program grant you additional permission to convey the resulting work. 38 | 39 | -------------------------------------------------------------------------------- /dist/install-dml.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | where /q tar 3 | if errorlevel 1 goto error 4 | 5 | cd /d %~dp0 6 | 7 | cls 8 | echo Installing the DirectML.dll version required by the Lc0 onnx-dml backend. 9 | curl -# --ssl-no-revoke -o tmp_directml.zip https://globalcdn.nuget.org/packages/microsoft.ai.directml.1.10.0.nupkg" 10 | if errorlevel 1 goto error 11 | 12 | tar -xzOf tmp_directml.zip bin/x64-win/DirectML.dll >DirectML.dll 13 | if errorlevel 1 goto error 14 | 15 | del /q tmp_directml.zip 16 | echo Installation successful. 17 | pause 18 | exit /b 19 | 20 | :error 21 | cls 22 | echo Installation failed - see the README for an alternative approach. 23 | pause 24 | 25 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["meson-python"] 3 | build-backend = "mesonpy" 4 | 5 | [project] 6 | name = "lczero_bindings" 7 | version = "0.1.0" 8 | description = "Leela Chess Zero Python bindings" 9 | authors = [{ name = "The LCZero Authors" }] 10 | license = {file = "COPYING"} 11 | readme = "README.md" 12 | requires-python = ">=3.7" 13 | classifiers = [ 14 | "Programming Language :: Python :: 3", 15 | "Topic :: Games/Entertainment :: Board Games", 16 | "Topic :: Scientific/Engineering :: Artificial Intelligence", 17 | "Environment :: GPU" 18 | ] 19 | 20 | [project.urls] 21 | homepage = "https://github.com/LeelaChessZero/lc0" 22 | 23 | [tool.meson-python.args] 24 | dist = [] 25 | setup = ["-Dpython_bindings=true"] 26 | compile = [] 27 | install = [] -------------------------------------------------------------------------------- /scripts/appveyor_android_build.cmd: -------------------------------------------------------------------------------- 1 | cd arm64-v8a 2 | ninja 3 | llvm-strip lc0 4 | cd C:\projects\lc0 5 | cd armeabi-v7a 6 | ninja 7 | llvm-strip lc0 8 | -------------------------------------------------------------------------------- /scripts/appveyor_android_package.cmd: -------------------------------------------------------------------------------- 1 | git clone https://github.com/LeelaChessZero/chessenginesupport-androidlib.git --branch lc0 --single-branch oex 2 | cd oex 3 | git checkout 0c02a8893b9c57ec57b40569ba60625912c6d32f 4 | cd .. 5 | perl -e "printf '%%sLc0!', pack('V', -s 'c:/cache/%NET%.pb.gz')" >tail.bin 6 | copy /y /b arm64-v8a\lc0+c:\cache\%NET%.pb.gz+tail.bin oex\LeelaChessEngine\leelaChessEngine\src\main\jniLibs\arm64-v8a\liblc0.so 7 | copy /y /b armeabi-v7a\lc0+c:\cache\%NET%.pb.gz+tail.bin oex\LeelaChessEngine\leelaChessEngine\src\main\jniLibs\armeabi-v7a\liblc0.so 8 | set ANDROID_HOME=C:\android-sdk-windows 9 | appveyor DownloadFile https://dl.google.com/android/repository/sdk-tools-windows-3859397.zip 10 | 7z x sdk-tools-windows-3859397.zip -oC:\android-sdk-windows > nul 11 | yes | C:\android-sdk-windows\tools\bin\sdkmanager.bat --licenses 12 | cd oex\LeelaChessEngine 13 | sed -i "s/591226/%NET%/" leelaChessEngine/src/main/res/values/strings.xml 14 | sed -i "/versionCode/ s/1/%APPVEYOR_BUILD_NUMBER%/" leelaChessEngine/src/main/AndroidManifest.xml 15 | sed -i "s/0.25dev/%APPVEYOR_REPO_TAG_NAME%/" leelaChessEngine/src/main/AndroidManifest.xml 16 | call gradlew.bat assemble 17 | copy leelaChessEngine\build\outputs\apk\debug\leelaChessEngine-debug.apk ..\..\lc0-%APPVEYOR_REPO_TAG_NAME%-android.apk 18 | -------------------------------------------------------------------------------- /scripts/appveyor_win_build.cmd: -------------------------------------------------------------------------------- 1 | SET PGO=false 2 | IF %APPVEYOR_REPO_TAG%==true IF %DX%==false IF %ONNX_DML%==false SET PGO=true 3 | IF %PGO%==false msbuild "C:\projects\lc0\build\lc0.sln" /m /p:WholeProgramOptimization=true /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" 4 | IF EXIST build\lc0.pdb del build\lc0.pdb 5 | IF %PGO%==true msbuild "C:\projects\lc0\build\lc0.sln" /m /p:WholeProgramOptimization=PGInstrument /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" 6 | IF ERRORLEVEL 1 EXIT 7 | cd build 8 | IF %NAME%==cpu-openblas copy C:\cache\OpenBLAS\dist64\bin\libopenblas.dll 9 | IF %NAME%==cpu-dnnl copy C:\cache\%DNNL_NAME%\bin\dnnl.dll 10 | IF %NAME%==onednn copy C:\cache\%DNNL_NAME%\bin\dnnl.dll 11 | IF %NAME%==onednn copy dnnl.dll .. 12 | copy "%MIMALLOC_PATH%"\out\msvc-x64\Release\mimalloc-override.dll 13 | copy "%MIMALLOC_PATH%"\out\msvc-x64\Release\mimalloc-redirect.dll 14 | IF %PGO%==true ( 15 | IF %OPENCL%==true copy C:\cache\opencl-nug.0.777.77\build\native\bin\OpenCL.dll 16 | IF %CUDA%==true copy "%CUDA_PATH%"\bin\*.dll 17 | IF %CUDNN%==true copy "%CUDA_PATH%"\cuda\bin\cudnn64_7.dll 18 | lc0 benchmark --num-positions=1 --backend=trivial --movetime=10000 19 | ) 20 | cd .. 21 | IF %PGO%==true msbuild "C:\projects\lc0\build\lc0.sln" /m /p:WholeProgramOptimization=PGOptimize /p:DebugInformationFormat=ProgramDatabase /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" 22 | -------------------------------------------------------------------------------- /scripts/bumpversion.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import argparse 4 | import os 5 | 6 | 7 | VERSION_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../src/version.inc") 8 | VERSION_CONTENT = """ 9 | #define LC0_VERSION_MAJOR {} 10 | #define LC0_VERSION_MINOR {} 11 | #define LC0_VERSION_PATCH {} 12 | #define LC0_VERSION_POSTFIX "{}" 13 | """ 14 | VERSION_CONTENT = VERSION_CONTENT.strip() 15 | 16 | 17 | def get_version(): 18 | with open(VERSION_FILE, 'r') as f: 19 | major = int(f.readline().split()[2]) 20 | minor = int(f.readline().split()[2]) 21 | patch = int(f.readline().split()[2]) 22 | postfix = f.readline().split()[2] 23 | 24 | postfix = postfix.replace('"', '') 25 | return major, minor, patch, postfix 26 | 27 | 28 | def set_version(major, minor, patch, postfix=""): 29 | version_inc = VERSION_CONTENT.format(major, minor, patch, postfix) 30 | 31 | with open(VERSION_FILE, 'w') as f: 32 | f.write(version_inc) 33 | 34 | 35 | def update(major, minor, patch, postfix=""): 36 | set_version(major, minor, patch, postfix) 37 | 38 | 39 | def main(argv): 40 | major, minor, patch, postfix = get_version() 41 | 42 | if argv.major: 43 | major += 1 44 | minor = 0 45 | patch = 0 46 | postfix = "" 47 | update(major, minor, patch) 48 | if argv.minor: 49 | minor += 1 50 | patch = 0 51 | postfix = "" 52 | update(major, minor, patch) 53 | if argv.patch: 54 | patch += 1 55 | postfix = "" 56 | update(major, minor, patch) 57 | if argv.postfix and len(argv.postfix) > 0: 58 | postfix = argv.postfix 59 | update(major, minor, patch, postfix) 60 | 61 | if len(postfix) == 0: 62 | print('v{}.{}.{}'.format(major, minor, patch)) 63 | else: 64 | print('v{}.{}.{}-{}'.format(major, minor, patch, postfix)) 65 | 66 | 67 | if __name__ == "__main__": 68 | argparser = argparse.ArgumentParser(description=\ 69 | 'Set or read current version.') 70 | argparser.add_argument('--major', action='store_true', 71 | help='bumps major version') 72 | argparser.add_argument('--minor', action='store_true', 73 | help='bumps minor version') 74 | argparser.add_argument('--patch', action='store_true', 75 | help='bumps patch') 76 | argparser.add_argument('--postfix', type=str, 77 | help='set postfix') 78 | argv = argparser.parse_args() 79 | main(argv) 80 | 81 | -------------------------------------------------------------------------------- /scripts/check_dx.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | ECHO Sanity checking the dx12 driver. 3 | lc0 benchmark --num-positions=1 --backend=check --backend-opts=mode=check,freq=1.0,atol=5e-1,dx12 %* 4 | PAUSE 5 | 6 | -------------------------------------------------------------------------------- /scripts/check_opencl.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | ECHO Sanity checking the opencl driver. 3 | lc0 benchmark --num-positions=1 --backend=check --backend-opts=mode=check,freq=1.0,opencl %* 4 | PAUSE 5 | 6 | -------------------------------------------------------------------------------- /scripts/checkdir.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import sys 4 | import os 5 | if len(sys.argv) > 1 and os.path.isdir(sys.argv[1]): 6 | sys.exit(0) 7 | sys.exit(1) 8 | -------------------------------------------------------------------------------- /scripts/pybind/core.py: -------------------------------------------------------------------------------- 1 | # This file is part of Leela Chess Zero. 2 | # Copyright (C) 2020 The LCZero Authors 3 | # 4 | # Leela Chess is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # Leela Chess is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with Leela Chess. If not, see . 16 | # 17 | # Additional permission under GNU GPL version 3 section 7 18 | # 19 | # If you modify this Program, or any covered work, by linking or 20 | # combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 21 | # Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 22 | # modified version of those libraries), containing parts covered by the 23 | # terms of the respective license agreement, the licensors of this 24 | # Program grant you additional permission to convey the resulting work. 25 | 26 | 27 | class PyObject: 28 | def __init__(self, name): 29 | self.name = name 30 | 31 | def BuildDocString(self): 32 | return "nullptr" 33 | -------------------------------------------------------------------------------- /scripts/pybind/exceptions.py: -------------------------------------------------------------------------------- 1 | # This file is part of Leela Chess Zero. 2 | # Copyright (C) 2020 The LCZero Authors 3 | # 4 | # Leela Chess is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # Leela Chess is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with Leela Chess. If not, see . 16 | # 17 | # Additional permission under GNU GPL version 3 section 7 18 | # 19 | # If you modify this Program, or any covered work, by linking or 20 | # combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 21 | # Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 22 | # modified version of those libraries), containing parts covered by the 23 | # terms of the respective license agreement, the licensors of this 24 | # Program grant you additional permission to convey the resulting work. 25 | 26 | from .core import PyObject 27 | 28 | 29 | class CppException(PyObject): 30 | def __init__(self, name, cpp_name, *args, **kwargs): 31 | self.cpp_name = cpp_name 32 | super().__init__(name, *args, **kwargs) 33 | 34 | def type_struct_name(self): 35 | return f'T{self.name}ExceptionType' 36 | 37 | def Generate(self, w): 38 | w.Write(f'PyObject *{self.type_struct_name()};') 39 | 40 | def GenerateRegister(self, w, module): 41 | w.Write(f'{self.type_struct_name()} = PyErr_NewException(' 42 | f'"{module.name}.{self.name}", nullptr, nullptr);') 43 | w.Write(f'if ({self.type_struct_name()} == nullptr) return nullptr;') 44 | w.Write(f'Py_INCREF({self.type_struct_name()});') 45 | w.Write(f'PyModule_AddObject(module, "{self.name}", ' 46 | f'{self.type_struct_name()});') 47 | 48 | def GenerateHandle(self, w, func): 49 | w.Unindent() 50 | w.Open(f'}} catch (const {self.cpp_name} &ex) {{') 51 | w.Write(f'PyErr_SetString({self.type_struct_name()}, ex.what());') 52 | w.Write(f'return {func._failure()};') 53 | -------------------------------------------------------------------------------- /scripts/pybind/writer.py: -------------------------------------------------------------------------------- 1 | # This file is part of Leela Chess Zero. 2 | # Copyright (C) 2020 The LCZero Authors 3 | # 4 | # Leela Chess is free software: you can redistribute it and/or modify 5 | # it under the terms of the GNU General Public License as published by 6 | # the Free Software Foundation, either version 3 of the License, or 7 | # (at your option) any later version. 8 | # 9 | # Leela Chess is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with Leela Chess. If not, see . 16 | # 17 | # Additional permission under GNU GPL version 3 section 7 18 | # 19 | # If you modify this Program, or any covered work, by linking or 20 | # combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 21 | # Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 22 | # modified version of those libraries), containing parts covered by the 23 | # terms of the respective license agreement, the licensors of this 24 | # Program grant you additional permission to convey the resulting work. 25 | 26 | 27 | class Writer: 28 | '''A helper class for writing file line by line with indent.''' 29 | def __init__(self, fo): 30 | self.fo = fo 31 | self.indent = 0 32 | 33 | def Indent(self): 34 | self.indent += 2 35 | 36 | def Unindent(self): 37 | self.indent -= 2 38 | 39 | def Write(self, text): 40 | for line in text.split('\n'): 41 | if line: 42 | self.fo.write(' ' * self.indent + line + '\n') 43 | else: 44 | self.fo.write('\n') 45 | 46 | def Open(self, text='{'): 47 | self.Write(text) 48 | self.Indent() 49 | 50 | def Close(self, text='}'): 51 | self.Unindent() 52 | self.Write(text) 53 | -------------------------------------------------------------------------------- /scripts/sycl_build_hack.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import os 4 | 5 | dir = os.getenv('MESON_BUILD_ROOT') 6 | 7 | with open(dir + '/build.ninja', 'r') as file: 8 | lines = file.readlines() 9 | 10 | updated = [] 11 | dep_flag = False 12 | link_flag = False 13 | 14 | for line in lines: 15 | # Replace xilink with icx -fsycl as the linker. 16 | if not link_flag: 17 | link_flag = 'xilink.exe' in line 18 | if link_flag: 19 | line = line.replace('xilink.exe', 'icx') 20 | line = line.replace('/MACHINE:x64', '-fsycl') 21 | line = line.replace('/OUT:', '-o ') 22 | line = line.replace('/SUBSYSTEM:CONSOLE', '') 23 | line = line.replace('/OPT:REF', '') 24 | line = line.replace('/PDB:', '/Fd') 25 | # Replace msvc compatible dependencies with gcc ones as icx output with /showincludes includes 26 | # temporary header files causing full project rebuilds. 27 | if line.startswith('rule') or line.startswith('build'): 28 | dep_flag = 'cpp_COMPILER' in line 29 | if dep_flag: 30 | line = line.replace('deps = msvc', 'deps = gcc\n depfile = $out.d') 31 | line = line.replace('/showIncludes', '/QMD') 32 | if 'icx' in line: 33 | line = line.replace('/Fo$out', '/Fo$out /QMF$out.d') 34 | updated.append(line) 35 | 36 | with open(dir + '/build.ninja', 'w') as file: 37 | file.writelines(updated) 38 | -------------------------------------------------------------------------------- /src/chess/gamestate.cc: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2024 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #include "chess/gamestate.h" 29 | 30 | #include 31 | #include 32 | 33 | namespace lczero { 34 | 35 | Position GameState::CurrentPosition() const { 36 | return std::accumulate( 37 | moves.begin(), moves.end(), startpos, 38 | [](const Position& pos, Move m) { return Position(pos, m); }); 39 | } 40 | 41 | std::vector GameState::GetPositions() const { 42 | std::vector positions; 43 | positions.reserve(moves.size() + 1); 44 | positions.push_back(startpos); 45 | std::transform(moves.begin(), moves.end(), std::back_inserter(positions), 46 | [&](Move m) { 47 | return Position(positions.back(), m); 48 | }); 49 | return positions; 50 | } 51 | 52 | } // namespace lczero 53 | -------------------------------------------------------------------------------- /src/chess/gamestate.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2024 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | 32 | #include "chess/position.h" 33 | 34 | namespace lczero { 35 | 36 | // A structure that is passed to Search/SearchEnvironment to provide the game 37 | // state. Somewhat mirrors usi `position moves ...` command. 38 | struct GameState { 39 | Position startpos; 40 | std::vector moves; 41 | 42 | // Returns the position of the last move in the list. 43 | Position CurrentPosition() const; 44 | // Returns positions after all moves, including the starting and the last one. 45 | std::vector GetPositions() const; 46 | }; 47 | 48 | } // namespace lczero -------------------------------------------------------------------------------- /src/engine_loop.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018-2025 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | #include 32 | 33 | #include "chess/uciloop.h" 34 | #include "search/search.h" 35 | #include "utils/optionsparser.h" 36 | 37 | namespace lczero { 38 | 39 | // Runs the stdin/stdout UCI loop for the engine. 40 | void RunEngine(SearchFactory* factory); 41 | 42 | } // namespace lczero -------------------------------------------------------------------------------- /src/neural/backend.cc: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2024 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #include "neural/backend.h" 29 | 30 | namespace lczero { 31 | 32 | std::vector Backend::EvaluateBatch( 33 | std::span positions) { 34 | std::vector results; 35 | results.reserve(positions.size()); 36 | std::unique_ptr computation = CreateComputation(); 37 | for (const EvalPosition& pos : positions) { 38 | results.emplace_back(); 39 | EvalResult& result = results.back(); 40 | result.p.resize(pos.legal_moves.size()); 41 | computation->AddInput( 42 | pos, EvalResultPtr{&result.q, &result.d, &result.m, 43 | std::span(result.p.data(), result.p.size())}); 44 | } 45 | computation->ComputeBlocking(); 46 | return results; 47 | } 48 | 49 | } // namespace lczero -------------------------------------------------------------------------------- /src/neural/backends/blas/README.md: -------------------------------------------------------------------------------- 1 | The files in this directory comprise the BLAS backend of Lc0. 2 | 3 | ## License 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | **The source files of this directory are not covered by any additional 19 | permission.** 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/neural/backends/blas/convolution1.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | */ 18 | 19 | #pragma once 20 | 21 | #include 22 | #include 23 | 24 | namespace lczero { 25 | 26 | // Convolution 1x1 27 | template 28 | class Convolution1 { 29 | public: 30 | Convolution1() = delete; 31 | 32 | // Batched forward inference. 33 | static void Forward(const size_t batch_size, const size_t input_channels, 34 | const size_t output_channels, const float* input, 35 | const float* weights, float* output); 36 | 37 | private: 38 | static constexpr auto kWidth = 8; 39 | static constexpr auto kHeight = 8; 40 | static constexpr auto kSquares = kWidth * kHeight; 41 | }; 42 | } // namespace lczero 43 | -------------------------------------------------------------------------------- /src/neural/backends/blas/encoder.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2022-2023 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | */ 18 | 19 | #pragma once 20 | 21 | #include 22 | 23 | #ifdef USE_ISPC 24 | #include "layer_norm_ispc.h" 25 | #endif 26 | 27 | namespace lczero { 28 | 29 | void LayerNorm2DWithSkipConnection(const size_t batch_size, 30 | const size_t channels, float* data, 31 | const float alpha, const float* skip, 32 | const float* gammas, const float* betas, 33 | float epsilon) { 34 | for (size_t i = 0; i < batch_size; i++) { 35 | #ifndef USE_ISPC 36 | // Mean taken in dimension C. 37 | float mean = 0; 38 | if (skip != nullptr) { 39 | for (size_t c = 0; c < channels; ++c) { 40 | data[i * channels + c] = 41 | data[i * channels + c] * alpha + skip[i * channels + c]; 42 | mean += data[i * channels + c]; 43 | } 44 | } else { 45 | for (size_t c = 0; c < channels; ++c) { 46 | data[i * channels + c] *= alpha; 47 | mean += data[i * channels + c]; 48 | } 49 | } 50 | mean /= channels; 51 | 52 | // Variance. 53 | float var = 0; 54 | for (size_t c = 0; c < channels; ++c) { 55 | auto diff = data[i * channels + c] - mean; 56 | var += diff * diff; 57 | } 58 | var /= channels; 59 | 60 | // Norm. 61 | float den = 1.0f / std::sqrt(var + epsilon); 62 | for (size_t c = 0; c < channels; ++c) { 63 | data[i * channels + c] = 64 | betas[c] + gammas[c] * (data[i * channels + c] - mean) * den; 65 | } 66 | #else 67 | if (skip != nullptr) { 68 | ispc::LayerNorm2DWithSkipConnection(channels, data + i * channels, alpha, 69 | skip + i * channels, gammas, betas, 70 | epsilon); 71 | } else { 72 | ispc::LayerNorm2DWithSkipConnection(channels, data + i * channels, alpha, 73 | nullptr, gammas, betas, epsilon); 74 | } 75 | 76 | #endif 77 | } 78 | } 79 | 80 | } // namespace lczero 81 | -------------------------------------------------------------------------------- /src/neural/backends/blas/fully_connected_layer.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | */ 18 | 19 | #pragma once 20 | 21 | #include "neural/backends/shared/activation.h" 22 | 23 | #include 24 | #include 25 | 26 | namespace lczero { 27 | 28 | template 29 | class FullyConnectedLayer { 30 | public: 31 | FullyConnectedLayer() = delete; 32 | 33 | // Forward inference, batched, from input_size to output_size 34 | static void Forward1D(const size_t batch_size, const size_t input_size, 35 | const size_t output_size, const float* input, 36 | const float* weights, const float* biases, 37 | const ActivationFunction activation, float* output); 38 | 39 | // Forward inference, no batched, from input_size to scalar 40 | static float Forward0D(const size_t input_size, const float* input, 41 | const float* weights); 42 | 43 | }; 44 | 45 | } // namespace lczero 46 | -------------------------------------------------------------------------------- /src/neural/backends/blas/layer_norm.ispc: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2023 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | */ 18 | 19 | export void LayerNorm2DWithSkipConnection(uniform const size_t channels, 20 | uniform float data[], 21 | const uniform float alpha, 22 | const uniform float skip[], 23 | const uniform float gammas[], 24 | const uniform float betas[], 25 | uniform const float epsilon) { 26 | #if 0 27 | // Faster but potentially less stable version for future testing. 28 | // One pass mean and variance taken in dimension C. Uses shifted variance calculation. 29 | float imean = 0; 30 | float ivar = 0; 31 | float k = data[0] * alpha; 32 | if (skip != NULL) { 33 | k += skip[0]; 34 | foreach (c = 0 ... channels) { 35 | float t = data[c] * alpha + skip[c]; 36 | data[c] = t; 37 | t -= k; 38 | imean += t; 39 | ivar += t * t; 40 | } 41 | } else { 42 | foreach (c = 0 ... channels) { 43 | float t = data[c] * alpha; 44 | data[c] = t; 45 | t -= k; 46 | imean += t; 47 | ivar += t * t; 48 | } 49 | } 50 | float mean = reduce_add(imean) / channels; 51 | float var = (reduce_add(ivar) - channels * mean * mean) / channels; 52 | mean += k; 53 | #else 54 | // Mean taken in dimension C. 55 | float imean = 0; 56 | if (skip != NULL) { 57 | foreach (c = 0 ... channels) { 58 | data[c] = data[c] * alpha + skip[c]; 59 | imean += data[c]; 60 | } 61 | } else { 62 | foreach (c = 0 ... channels) { 63 | data[c] *= alpha; 64 | imean += data[c]; 65 | } 66 | } 67 | float mean = reduce_add(imean) / channels; 68 | 69 | // Variance. 70 | float ivar = 0; 71 | foreach (c = 0 ... channels) { 72 | float diff = data[c] - mean; 73 | ivar += diff * diff; 74 | } 75 | float var = reduce_add(ivar) / channels; 76 | #endif 77 | 78 | float den = rsqrt(var + epsilon); 79 | foreach (c = 0 ... channels) { 80 | data[c] = betas[c] + gammas[c] * (data[c] - mean) * den; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/neural/backends/blas/se_unit.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | */ 18 | 19 | #pragma once 20 | 21 | #include "neural/backends/shared/activation.h" 22 | 23 | #include 24 | 25 | namespace lczero { 26 | 27 | template 28 | void ApplySEUnit(const size_t batch_size, const size_t channels, 29 | const size_t se_fc_outputs, const float* input, 30 | const float* bias, const float* residual, 31 | const float* weights_w1, const float* weights_b1, 32 | const float* weights_w2, const float* weights_b2, 33 | float* output, const ActivationFunction activation); 34 | 35 | } // namespace lczero 36 | -------------------------------------------------------------------------------- /src/neural/backends/blas/winograd_convolution3.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | */ 18 | 19 | #pragma once 20 | 21 | #include 22 | #include 23 | 24 | namespace lczero { 25 | 26 | // Convolution 3x3 on a 8x8 board using the Winograd algorithm. 27 | // 28 | // Ref: 29 | // 30 | // Fast Algorithms for Convolutional Neural Networks 31 | // https://arxiv.org/abs/1509.09308 32 | // 33 | // https://ai.intel.com/winograd/ 34 | // https://ai.intel.com/winograd-2/ 35 | 36 | // Convolution 3x3 using the Winograd algorithm 37 | template 38 | class WinogradConvolution3 { 39 | public: 40 | // The instance will allocate memory resources for the 41 | // largest batch size, and the largest input and output 42 | // layers. 43 | WinogradConvolution3(const size_t max_batch_size, 44 | const size_t max_input_layers, 45 | const size_t max_output_layers); 46 | 47 | // Forward inference, batched. 48 | void Forward(const size_t batch_size, const size_t input_channels, 49 | const size_t output_channels, const float* input, 50 | const float* weights, float* output); 51 | 52 | private: 53 | void TransformIn(const size_t batch_size, const float* input, 54 | const size_t channels); 55 | 56 | void Sgemm(const size_t batch_size, const float* weights, 57 | const size_t input_channels, const size_t output_channels); 58 | 59 | void TransformOut(const size_t batch_size, float* output, 60 | const size_t channels); 61 | 62 | static constexpr auto kWidth = 8; 63 | static constexpr auto kHeight = 8; 64 | static constexpr auto kSquares = kWidth * kHeight; 65 | 66 | static constexpr auto kWtiles = (kWidth + 1) / 2; // 4 67 | static constexpr auto kTiles = kWtiles * kWtiles; // 16 68 | 69 | static constexpr auto kWinogradAlpha = 4; 70 | static constexpr auto kWinogradTile = kWinogradAlpha * kWinogradAlpha; 71 | 72 | std::vector V_; 73 | std::vector M_; 74 | }; 75 | } // namespace lczero 76 | -------------------------------------------------------------------------------- /src/neural/backends/cuda/readme.txt: -------------------------------------------------------------------------------- 1 | cuda/cudnn backend for lc0. Here is a brief description of various files: 2 | 3 | 1. network_cudnn.cc -> cpp file containing network, computation, etc stuff related to lc0 4 | 2. layers.cc -> cpp files containing layer classes 5 | 3. layers.h -> header file for layer classes. 6 | 4. kernels.h -> header file for cuda kernels 7 | 5. common_kernels.cu -> common kernels (fp32, and fp16 that can work with old GPUs) 8 | 6. fp16_kernels.cu -> fp16 specific kernels (not used on other GPUs) 9 | 7. cuda_common.h -> header for common cuda stuff like ReportCUDAErrors, etc. 10 | 8. readme.txt -> this file 11 | 12 | High level overview: network is built of layer objects, layers are either implemented using cudnn/cublas libraries, or custom cuda kernels. 13 | 14 | lc0 search -> network_cudnn -> layers -> kernels -------------------------------------------------------------------------------- /src/neural/backends/dx/dx_common.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | #pragma once 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #include 34 | 35 | #include "d3dx12.h" 36 | #include "utils/fp16_utils.h" 37 | 38 | #define DEFAULT_FP16 true 39 | 40 | // To debug in case some GPUs can't read from sysmem directly in shader. 41 | //#define COPY_BEFORE_SHADER_READ 42 | // Dump per-layer debug data to find where things go wrong. 43 | //#define DEBUG_DUMP_PER_LAYER_DATA 44 | 45 | namespace lczero { 46 | 47 | namespace dx_backend { 48 | 49 | void DxError(HRESULT status, const char* file, const int& line); 50 | #define ReportDxErrors(status) DxError(status, __FILE__, __LINE__) 51 | 52 | struct DXAlloc { 53 | ID3D12Resource* resource; 54 | uint32_t offset; 55 | // Various ways of binding an allocation to shader: 56 | // 1. RAW/Structured buffer bound as root UAV, use gpu_va directly. 57 | // 2. Typed buffer UAV bound as 4-component typed format (e.g: 58 | // R16G16B16A16_FLOAT) 59 | // 3. Typed buffer UAV bound as single component scalar typed format (e.g: 60 | // R16_FLOAT) 61 | 62 | uint64_t gpu_va; 63 | 64 | // Handle of UAV created as 4-component vector type. 65 | D3D12_GPU_DESCRIPTOR_HANDLE desc_handle_vector; 66 | 67 | // Handle of UAV created as scalar type. 68 | D3D12_GPU_DESCRIPTOR_HANDLE desc_handle_scalar; 69 | }; 70 | 71 | typedef uint16_t dx_half; 72 | 73 | inline int DivUp(int a, int b) { return (a + b - 1) / b; } 74 | 75 | } // namespace dx_backend 76 | } // namespace lczero 77 | -------------------------------------------------------------------------------- /src/neural/backends/dx/shaders/PolicyMap.hlsl: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #include "shader_shared.h" 29 | 30 | // ------------------- Policy Map Shader -----------------------------// 31 | RWBuffer input : register(u8); 32 | 33 | // Output is always fp32. 34 | RWStructuredBuffer output : register(u1); 35 | 36 | // Weights are always int32. 37 | RWStructuredBuffer indices : register(u2); 38 | 39 | cbuffer PolicyMapConsts : register(b0) { 40 | uint N; 41 | uint inputSize; 42 | uint usedSize; 43 | uint outputSize; 44 | }; 45 | 46 | [numthreads(kPolicyMapBlockSize, 1, 1)] 47 | void PolicyMapShader 48 | ( 49 | uint3 globalThreadIdx : SV_DispatchThreadID 50 | ) 51 | { 52 | int tid = globalThreadIdx.x; 53 | int n = tid / usedSize; 54 | int i = tid % usedSize; 55 | 56 | if (n >= N) return; 57 | 58 | int j = indices[i]; 59 | 60 | if (j >= 0) { 61 | output[n * outputSize + j] = input[n * inputSize + i]; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/neural/backends/dx/shaders/dxc_helper.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Call dxc with absolute paths where '/' are converted to '\' (in windows). 3 | import sys 4 | import os 5 | import re 6 | 7 | x = sys.argv 8 | x.pop(0) 9 | for i in range(len(x)): 10 | if re.match(r"[a-zA-Z]:/", x[i]): 11 | x[i] = os.path.normpath(x[i]) 12 | # We asssume dxc is already on the path. 13 | os.system('dxc ' + '"{}"'.format('" "'.join(x))) 14 | 15 | -------------------------------------------------------------------------------- /src/neural/backends/dx/shaders/shader_shared.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #define kExpandPlanesElementsPerBlock 256 31 | #define kExpandPlanesFp32BlockSize kExpandPlanesElementsPerBlock 32 | #define kExpandPlanesFp16BlockSize (kExpandPlanesElementsPerBlock / 2) 33 | 34 | // for both input transform and output transform shaders 35 | #define kWinogradTransformShaderBlockSize 64 36 | 37 | #define kConv1x1BlockSize 64 38 | 39 | #define kAddVectorsBlockSize 512 40 | 41 | #define kPolicyMapBlockSize 256 42 | 43 | 44 | // Constants for GEMM shader. 45 | #define kGemmBlockWidth 16 46 | #define kGemmBlockHeight 16 47 | 48 | #define kGemmElPerThreadX 8 49 | #define kGemmElPerThreadY 8 50 | 51 | #define kGemmElPerBlockX (kGemmElPerThreadX * kGemmBlockWidth) 52 | #define kGemmElPerBlockY (kGemmElPerThreadY * kGemmBlockHeight) 53 | 54 | #define kGemmShMemKChunk 16 -------------------------------------------------------------------------------- /src/neural/backends/dx/shaders/shaders.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "ExpandPlanes_shader_fp32.h" 31 | #include "ExpandPlanes_shader_fp16.h" 32 | #include "input_transform_shader_fp32.h" 33 | #include "output_transform_shader_fp32.h" 34 | #include "conv_1x1_shader_fp32.h" 35 | #include "add_vectors_shader.h" 36 | #include "policy_map_shader_fp32.h" 37 | 38 | #include "output_transform_shader_fp32_se_128.h" 39 | #include "output_transform_shader_fp32_se_256.h" 40 | #include "output_transform_shader_fp32_se_320.h" 41 | #include "output_transform_shader_fp32_se_384.h" 42 | #include "output_transform_shader_fp32_se_512.h" 43 | #include "output_transform_shader_fp32_se_640.h" 44 | #include "output_transform_shader_fp32_se_768.h" 45 | #include "output_transform_shader_fp32_se_1024.h" 46 | 47 | #include "se_128.h" 48 | #include "se_256.h" 49 | #include "se_320.h" 50 | #include "se_384.h" 51 | #include "se_512.h" 52 | #include "se_640.h" 53 | #include "se_768.h" 54 | #include "se_1024.h" 55 | 56 | #include "MatrixMul_Fp32.h" 57 | #include "MatrixMul_Fp16.h" 58 | -------------------------------------------------------------------------------- /src/neural/backends/metal/mps/MetalNetworkBuilder.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2021 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | #pragma once 28 | 29 | #include 30 | #include 31 | 32 | namespace lczero { 33 | namespace metal_backend { 34 | 35 | struct Activations { 36 | std::string default_activation = "relu"; 37 | std::string smolgen_activation = "swish"; 38 | std::string ffn_activation = "relu_2"; 39 | }; 40 | 41 | class MetalNetworkBuilder { 42 | public: 43 | MetalNetworkBuilder(void); 44 | ~MetalNetworkBuilder(void); 45 | 46 | std::string init(int gpu_id); 47 | 48 | void build(int kInputPlanes, MultiHeadWeights& weights, 49 | InputEmbedding embedding, bool attn_body, bool attn_policy, 50 | bool conv_policy, bool wdl, bool moves_left, 51 | Activations& activations, std::string& policy_head, 52 | std::string& value_head); 53 | 54 | void forwardEval(float* inputs, int batchSize, 55 | std::vector output_mems); 56 | 57 | private: 58 | int gpu_id; 59 | }; 60 | 61 | } // namespace metal_backend 62 | } // namespace lczero 63 | -------------------------------------------------------------------------------- /src/neural/backends/opencl/OpenCLParams.h: -------------------------------------------------------------------------------- 1 | /* 2 | Originally from the Leela Zero project. 3 | Copyright (C) 2017 Gian-Carlo Pascutto 4 | 5 | This file is part of Leela Chess Zero. 6 | Copyright (C) 2018 The LCZero Authors 7 | 8 | Leela Chess is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | Leela Chess is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with Leela Chess. If not, see . 20 | */ 21 | 22 | #pragma once 23 | 24 | struct OpenCLParams { 25 | int gpuId = -1; 26 | 27 | bool tune_only = false; 28 | bool force_tune = false; 29 | bool tune_exhaustive = false; 30 | int tune_batch_size = 1; 31 | std::string tuner_file; 32 | }; 33 | -------------------------------------------------------------------------------- /src/neural/backends/opencl/OpenCLTuner.h: -------------------------------------------------------------------------------- 1 | /* 2 | Originally from the Leela Zero project. 3 | Copyright (C) 2017 Gian-Carlo Pascutto 4 | 5 | This file is part of Leela Chess Zero. 6 | Copyright (C) 2018 The LCZero Authors 7 | 8 | Leela Chess is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | Leela Chess is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with Leela Chess. If not, see . 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | #include "OpenCLParams.h" 29 | #include "neural/backends/opencl/OpenCL.h" 30 | #include "neural/backends/opencl/OpenCLParams.h" 31 | 32 | using Configurations = std::pair>; 33 | using TuneParameters = std::map; 34 | 35 | class OpenCL; 36 | 37 | class Tuner { 38 | OpenCL& m_opencl; 39 | const OpenCLParams& m_params; 40 | cl::Context m_context; 41 | cl::Device m_device; 42 | 43 | public: 44 | std::string tune_sgemm(const int m, const int n, const int k, 45 | const int batch_size, const int runs = 4); 46 | std::string load_sgemm_tuners(const int m, const int n, const int k, 47 | const int batch_size); 48 | 49 | static constexpr auto TUNER_VERSION = 0; 50 | Tuner(OpenCL& opencl, const OpenCLParams& params, cl::Context context, 51 | cl::Device device) 52 | : m_opencl(opencl), 53 | m_params(params), 54 | m_context(context), 55 | m_device(device) {} 56 | 57 | private: 58 | void store_sgemm_tuners(const int m, const int n, const int k, 59 | const int batch_size, std::string tuners); 60 | bool valid_config_sgemm(TuneParameters p, bool exhaustive); 61 | std::string parameters_to_defines(const TuneParameters& p); 62 | std::string parameters_to_string(const TuneParameters& p); 63 | TuneParameters get_parameters_by_int(const std::vector& opts, 64 | const int n); 65 | std::string sgemm_tuners_from_line(std::string line, const int m, const int n, 66 | const int k, const int batch_size); 67 | }; 68 | -------------------------------------------------------------------------------- /src/neural/backends/opencl/README.md: -------------------------------------------------------------------------------- 1 | The files in this directory comprise the OpenCL backend of Lc0. 2 | 3 | ## License 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | **The source files of this directory are not covered by any additional 19 | permission.** 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/neural/backends/opencl/clblast_level3/xgemm_batched.opencl: -------------------------------------------------------------------------------- 1 | 2 | // ================================================================================================= 3 | // This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. This 4 | // project loosely follows the Google C++ styleguide and uses a tab-size of two spaces and a max- 5 | // width of 100 characters per line. 6 | // 7 | // Author(s): 8 | // Cedric Nugteren 9 | // 10 | // This file contains the batched version of the non-direct GEMM kernel. See part 1 for information 11 | // about the non-batched version of the kernel. 12 | // 13 | // ================================================================================================= 14 | 15 | // Enables loading of this file using the C++ pre-processor's #include (C++11 standard raw string 16 | // literal). Comment-out this line for syntax-highlighting when developing. 17 | R"( 18 | 19 | // ================================================================================================= 20 | 21 | // Main entry point of the kernel. This is the regular full version. 22 | __kernel __attribute__((reqd_work_group_size(MDIMC, NDIMC, 1))) 23 | void XgemmBatched(const int kSizeM, const int kSizeN, const int kSizeK, 24 | const __global realM* restrict agm, 25 | const __global realN* restrict bgm, 26 | __global realM* restrict cgm) { 27 | const int batch = get_group_id(2); 28 | 29 | // Sets the offsets 30 | const int a_offset = kSizeM*kSizeK*batch; 31 | const int b_offset = kSizeK*kSizeN*batch; 32 | const int c_offset = kSizeM*kSizeN*batch; 33 | const __global realM* restrict agm_ = &agm[a_offset / VWM]; 34 | const __global realN* restrict bgm_ = &bgm[b_offset / VWN]; 35 | __global realM* restrict cgm_ = &cgm[c_offset / VWM]; 36 | 37 | // Allocates workgroup-private memory (local memory) 38 | #if SA == 1 39 | __local realM alm[KWG * MWG/VWM]; 40 | #endif 41 | #if SB == 1 42 | __local realN blm[KWG * NWG/VWN]; 43 | #endif 44 | 45 | // Computes the matrix-multiplication and stores the result in global memory 46 | #if SA == 1 && SB == 1 47 | XgemmBody(kSizeM, kSizeN, kSizeK, agm_, bgm_, cgm_, alm, blm); 48 | #elif SA == 1 49 | XgemmBody(kSizeM, kSizeN, kSizeK, agm_, bgm_, cgm_, alm); 50 | #elif SB == 1 51 | XgemmBody(kSizeM, kSizeN, kSizeK, agm_, bgm_, cgm_, blm); 52 | #else 53 | XgemmBody(kSizeM, kSizeN, kSizeK, agm_, bgm_, cgm_); 54 | #endif 55 | } 56 | 57 | // ================================================================================================= 58 | 59 | // End of the C++11 raw string literal 60 | )" 61 | 62 | // ================================================================================================= 63 | -------------------------------------------------------------------------------- /src/neural/backends/opencl/clsource/config.opencl: -------------------------------------------------------------------------------- 1 | // Enables loading of this file using the C++ pre-processor's #include (C++11 standard raw string 2 | // literal). Comment-out this line for syntax-highlighting when developing. 3 | R"( 4 | 5 | typedef float net_t; 6 | #define vload_net_t(offset,p) ((p)[(offset)]) 7 | #define vstore_net_t(data,offset,p) (((p)[(offset)])=(data)) 8 | 9 | #define BOARD_SIZE 8 10 | #define BOARD_SQUARES (BOARD_SIZE*BOARD_SIZE) 11 | 12 | // End of the C++11 raw string literal 13 | )" 14 | -------------------------------------------------------------------------------- /src/neural/backends/opencl/clsource/policymap.opencl: -------------------------------------------------------------------------------- 1 | // Enables loading of this file using the C++ pre-processor's #include (C++11 standard raw string 2 | // literal). Comment-out this line for syntax-highlighting when developing. 3 | 4 | R"( 5 | __kernel void policymap( 6 | __global const net_t * restrict input, 7 | __global net_t * restrict output, 8 | __global short* restrict indices, 9 | const int N, 10 | const int inputSize, 11 | const int usedSize, 12 | const int outputSize) { 13 | 14 | int tid = get_global_id(0); 15 | 16 | int n = tid / usedSize; 17 | int i = tid % usedSize; 18 | 19 | if (n >= N) return; 20 | 21 | int j = indices[i]; 22 | 23 | if (j >= 0) { 24 | output[n * outputSize + j] = input[n * inputSize + i]; 25 | } 26 | } 27 | // End of the C++11 raw string literal 28 | )" 29 | -------------------------------------------------------------------------------- /src/neural/backends/opencl/clsource/se.opencl: -------------------------------------------------------------------------------- 1 | // Enables loading of this file using the C++ pre-processor's #include (C++11 standard raw string 2 | // literal). Comment-out this line for syntax-highlighting when developing. 3 | 4 | R"( 5 | __kernel void global_avg_pooling( 6 | const int channels, 7 | __global const net_t * restrict in, 8 | __global net_t * restrict out) { 9 | 10 | const int col = get_global_id(0); // column 11 | const int c = get_global_id(1); // channel 12 | 13 | const int lid = get_local_id(0); 14 | 15 | __local net_t row_acc[BOARD_SIZE]; 16 | 17 | if (c < channels && col < BOARD_SIZE) { 18 | 19 | net_t acc = 0.0f; 20 | 21 | for ( int i = 0; i < BOARD_SIZE; i++) { 22 | acc += vload_net_t(c * BOARD_SQUARES + i * BOARD_SIZE + col, in); 23 | } 24 | row_acc[lid] = acc; 25 | } 26 | 27 | barrier(CLK_LOCAL_MEM_FENCE); 28 | 29 | if (lid == 0) { 30 | net_t acc = 0.0f; 31 | for ( int i = 0; i < BOARD_SIZE; i++) { 32 | acc += row_acc[i]; 33 | } 34 | acc = acc/BOARD_SQUARES; 35 | vstore_net_t(acc, c, out); 36 | } 37 | } 38 | 39 | __kernel void apply_se( 40 | const int channels, 41 | const int batch_size, 42 | __global const net_t * restrict input, 43 | __global net_t * restrict residual, 44 | __global const net_t * restrict fc_out) { 45 | 46 | const int col = get_global_id(0); // column 47 | const int c = get_global_id(1); // channel 48 | 49 | const int batch = c / channels; 50 | 51 | if (c < batch_size * channels && col < BOARD_SIZE) { 52 | net_t gamma = vload_net_t(c + batch * channels, fc_out); 53 | gamma = 1.0f/(1.0f + exp(-gamma)); // Sigmoid 54 | net_t beta = vload_net_t(c + batch * channels + channels, fc_out); 55 | 56 | for ( int i = 0; i < BOARD_SIZE; i++) { 57 | const int idx = c * BOARD_SQUARES + i * BOARD_SIZE + col; 58 | const net_t in = vload_net_t(idx, input); 59 | const net_t res = vload_net_t(idx, residual); 60 | 61 | net_t val = gamma * in + res + beta; 62 | 63 | val = val > 0.0f ? val : 0.0f; 64 | 65 | vstore_net_t(val, idx, residual); 66 | } 67 | } 68 | } 69 | // End of the C++11 raw string literal 70 | )" 71 | -------------------------------------------------------------------------------- /src/neural/backends/shared/activation.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018-2022 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | */ 18 | 19 | #pragma once 20 | 21 | #include 22 | #include 23 | 24 | #include "neural/tables/activation_function.h" 25 | 26 | namespace lczero { 27 | 28 | // Softmax activation 29 | void SoftmaxActivation(const size_t size, const float* input, float* output); 30 | 31 | void BiasResidual(const size_t batch_size, const size_t channels, float* data, 32 | const float* biases, const float* eltwise, 33 | const ActivationFunction activation); 34 | 35 | void BiasActivate(const size_t batch_size, const size_t channels, float* data, 36 | const float* biases, 37 | const ActivationFunction activation); 38 | 39 | float Activate(const float val, const ActivationFunction activation); 40 | 41 | void Activate(const size_t len, const float* data, const float* bias, 42 | float* output, const ActivationFunction activation); 43 | 44 | void Activate(const size_t len, float gamma, const float* data, 45 | const float* bias, float beta, float* out, 46 | const ActivationFunction activation); 47 | 48 | } // namespace lczero 49 | -------------------------------------------------------------------------------- /src/neural/backends/shared/winograd_filter.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | */ 18 | 19 | #pragma once 20 | 21 | #include 22 | #include 23 | 24 | namespace lczero { 25 | 26 | // Here are BLAS-free methods to setup the filter 27 | // for the 3x3 winograd convolution algorithm. 28 | // 29 | // Ref: 30 | // 31 | // Fast Algorithms for Convolutional Neural Networks 32 | // https://arxiv.org/abs/1509.09308 33 | // 34 | // https://ai.intel.com/winograd/ 35 | // https://ai.intel.com/winograd-2/ 36 | 37 | // Convolution filter for 3x3 Winograd algorithm 38 | 39 | // Create the zero-padded U matrix. 40 | std::vector WinogradFilterZeropadU(const std::vector& U, 41 | const size_t outputs, 42 | const size_t channels, 43 | const size_t outputs_pad, 44 | const size_t channels_pad); 45 | 46 | // Create the filter transform matrix. 47 | std::vector WinogradFilterTransformF(const std::vector& f, 48 | const size_t outputs, 49 | const size_t channels); 50 | 51 | } // namespace lczero 52 | -------------------------------------------------------------------------------- /src/neural/backends/sycl/sycl_common.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018-2024 The LCZero Authors 4 | Copyright (C) 2023 Intel Corporation 5 | 6 | This program is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | This program is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License 17 | along with this program. If not, see . 18 | 19 | SPDX-License-Identifier:GNU General Public License v3.0 or later 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include "dpct/dpct.hpp" 26 | #include "dpct/blas_utils.hpp" 27 | 28 | #include "utils/exception.h" 29 | 30 | namespace lczero { 31 | namespace sycldnn_backend { 32 | 33 | static constexpr int kNumOutputPolicy = 1858; 34 | 35 | // max supported filter count for fast path 36 | // TODO: extend it to cover bigger networks! 37 | // (We are limited by no of registers per thread) 38 | static constexpr int kMaxResBlockFusingChannels = 384; // limit on num_filters 39 | static constexpr int kMaxResBlockFusingSeKFp16Ampere = 40 | 512; // (use a different kernel with reduced register pressure) 41 | static constexpr int kMaxResBlockFusingSeK = 42 | 128; // limit on (num_filters / se_ratio) 43 | static constexpr int kMaxResBlockFusingSeFp16AmpereSmem = 44 | 72 * kMaxResBlockFusingSeKFp16Ampere * 45 | sizeof(sycl::half); // shared memory used by the special 46 | // kernel 47 | 48 | #ifdef USE_CUBLAS 49 | void CublasError(int status, const char* file, const int& line); 50 | 51 | #define ReportCUBLASErrors(status) CublasError(status, __FILE__, __LINE__) 52 | #endif 53 | 54 | inline int DivUp(int a, int b) { return (a + b - 1) / b; } 55 | 56 | } // namespace sycldnn_backend 57 | } // namespace lczero 58 | -------------------------------------------------------------------------------- /src/neural/batchsplit.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2025 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "neural/backend.h" 31 | 32 | namespace lczero { 33 | 34 | // Creates a backend wrapper that ensures that the maximum batch size of the 35 | // wrapped backend is respected. 36 | std::unique_ptr CreateBatchSplitingBackend(Backend* parent); 37 | 38 | } // namespace lczero -------------------------------------------------------------------------------- /src/neural/decoder.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2021 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "chess/position.h" 31 | #include "neural/network.h" 32 | #include "proto/net.pb.h" 33 | 34 | namespace lczero { 35 | 36 | // Decodes the move that led to current position using the current and previous 37 | // input planes. Move is from the perspective of the current position to move 38 | // player, so will need flipping if it is to be applied to the prior position. 39 | // 40 | // NOTE: Assumes InputPlanes are not transformed. Any canonical transforms must 41 | // have already been reverted. 42 | Move DecodeMoveFromInput(const InputPlanes& planes, const InputPlanes& prev); 43 | 44 | // Decodes the current position into a board, rule50 and gameply. 45 | // 46 | // NOTE: Assumes InputPlanes are not transformed, regardless of input_format. 47 | void PopulateBoard(pblczero::NetworkFormat::InputFormat input_format, 48 | InputPlanes planes, ChessBoard* board, int* rule50, 49 | int* gameply); 50 | 51 | } // namespace lczero -------------------------------------------------------------------------------- /src/neural/encoder.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | 32 | #include "chess/position.h" 33 | #include "neural/network.h" 34 | #include "proto/net.pb.h" 35 | 36 | namespace lczero { 37 | 38 | constexpr int kMoveHistory = 8; 39 | constexpr int kPlanesPerBoard = 13; 40 | constexpr int kAuxPlaneBase = kPlanesPerBoard * kMoveHistory; 41 | 42 | enum class FillEmptyHistory { NO, FEN_ONLY, ALWAYS }; 43 | 44 | // Returns the transform that would be used in EncodePositionForNN. 45 | int TransformForPosition(pblczero::NetworkFormat::InputFormat input_format, 46 | const PositionHistory& history); 47 | 48 | // Encodes the last position in history for the neural network request. 49 | InputPlanes EncodePositionForNN( 50 | pblczero::NetworkFormat::InputFormat input_format, 51 | const PositionHistory& history, int history_planes, 52 | FillEmptyHistory fill_empty_history, int* transform_out); 53 | 54 | InputPlanes EncodePositionForNN( 55 | pblczero::NetworkFormat::InputFormat input_format, 56 | std::span positions, int history_planes, 57 | FillEmptyHistory fill_empty_history, int* transform_out); 58 | 59 | bool IsCanonicalFormat(pblczero::NetworkFormat::InputFormat input_format); 60 | bool IsCanonicalArmageddonFormat( 61 | pblczero::NetworkFormat::InputFormat input_format); 62 | bool IsHectopliesFormat(pblczero::NetworkFormat::InputFormat input_format); 63 | bool Is960CastlingFormat(pblczero::NetworkFormat::InputFormat input_format); 64 | 65 | uint16_t MoveToNNIndex(Move move, int transform); 66 | Move MoveFromNNIndex(int idx, int transform); 67 | 68 | } // namespace lczero 69 | -------------------------------------------------------------------------------- /src/neural/loader.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #include "neural/network.h" 36 | #include "proto/net.pb.h" 37 | 38 | namespace lczero { 39 | 40 | class OptionsDict; 41 | using FloatVector = std::vector; 42 | using FloatVectors = std::vector; 43 | 44 | using WeightsFile = pblczero::Net; 45 | 46 | // Read weights file and fill the weights structure. 47 | WeightsFile LoadWeightsFromFile(const std::string& filename); 48 | 49 | // Read weights from the "locations", which is one of: 50 | // * "" -- tries to find a file which looks like a weights file. 51 | // * "" -- weights are embedded in the binary. 52 | // * filename -- reads weights from the file. 53 | // Returns std::nullopt if no weights file was found in mode. 54 | std::optional LoadWeights(std::string_view location); 55 | 56 | // Tries to find a file which looks like a weights file, and located in 57 | // directory of binary_name or one of subdirectories. If there are several such 58 | // files, returns one which has the latest modification date. 59 | std::string DiscoverWeightsFile(); 60 | 61 | } // namespace lczero 62 | -------------------------------------------------------------------------------- /src/neural/memcache.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2025 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "neural/backend.h" 31 | 32 | namespace lczero { 33 | 34 | class CachingBackend : public Backend { 35 | public: 36 | // Clears the cache. 37 | virtual void ClearCache() = 0; 38 | virtual void SetCacheSize(size_t size) = 0; 39 | }; 40 | 41 | // Creates a caching backend wrapper, which returns values immediately if they 42 | // are found, and forwards the request to the wrapped backend otherwise (and 43 | // caches the result). 44 | std::unique_ptr CreateMemCache(std::unique_ptr parent, 45 | size_t cache_size); 46 | 47 | } // namespace lczero -------------------------------------------------------------------------------- /src/neural/mock_backend.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2024 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "gmock/gmock.h" 31 | 32 | #include "neural/backend.h" 33 | 34 | namespace lczero { 35 | 36 | class MockBackendComputation : public BackendComputation { 37 | public: 38 | MOCK_METHOD(size_t, UsedBatchSize, (), (const, override)); 39 | MOCK_METHOD(AddInputResult, AddInput, 40 | (const EvalPosition& pos, EvalResultPtr result), (override)); 41 | MOCK_METHOD(void, ComputeBlocking, (), (override)); 42 | }; 43 | 44 | class MockBackend : public Backend { 45 | public: 46 | MOCK_METHOD(BackendAttributes, GetAttributes, (), (const, override)); 47 | MOCK_METHOD(std::unique_ptr, CreateComputation, (), 48 | (override)); 49 | MOCK_METHOD(std::vector, EvaluateBatch, 50 | (std::span positions), (override)); 51 | MOCK_METHOD(std::optional, GetCachedEvaluation, 52 | (const EvalPosition&), (override)); 53 | MOCK_METHOD(UpdateConfigurationResult, UpdateConfiguration, 54 | (const OptionsDict&), (override)); 55 | }; 56 | 57 | class MockBackendFactory : public BackendFactory { 58 | public: 59 | MOCK_METHOD(int, GetPriority, (), (const, override)); 60 | MOCK_METHOD(std::string_view, GetName, (), (const, override)); 61 | MOCK_METHOD(std::unique_ptr, Create, (const OptionsDict&), 62 | (override)); 63 | }; 64 | 65 | } // namespace lczero 66 | -------------------------------------------------------------------------------- /src/neural/onnx/converter.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2021 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | 32 | #include "neural/onnx/onnx.pb.h" 33 | #include "proto/net.pb.h" 34 | 35 | namespace lczero { 36 | 37 | // Options to use when converting "old" weights to ONNX weights format. 38 | struct WeightsToOnnxConverterOptions { 39 | enum class DataType { kFloat32, kFloat16, kBFloat16 }; 40 | DataType data_type = DataType::kFloat32; 41 | std::string input_planes_name = "/input/planes"; 42 | std::string output_policy_head = "/output/policy"; 43 | std::string output_wdl = "/output/wdl"; 44 | std::string output_value = "/output/value"; 45 | std::string output_mlh = "/output/mlh"; 46 | int batch_size = -1; 47 | int opset = 17; 48 | bool alt_mish = false; // Use "Mish" approximation (fp32 only). 49 | bool alt_layernorm = false; // Discrete "LayerNormalization" implementation. 50 | bool no_shape = false; // Avoid use of "Shape" operator. 51 | std::string policy_head = "vanilla"; 52 | std::string value_head = "winner"; 53 | 54 | static DataType StringToDataType(const std::string&); 55 | }; 56 | 57 | // Converts "classical" weights file to weights file with embedded ONNX model. 58 | pblczero::Net ConvertWeightsToOnnx(const pblczero::Net&, 59 | const WeightsToOnnxConverterOptions&); 60 | 61 | } // namespace lczero 62 | -------------------------------------------------------------------------------- /src/neural/shared_params.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2024 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "utils/optionsdict.h" 31 | #include "utils/optionsparser.h" 32 | 33 | namespace lczero { 34 | 35 | // Backend parameters that appear in UCI interface and are in use by most 36 | // backends. 37 | struct SharedBackendParams { 38 | static const constexpr char* kEmbed = ""; 39 | static const constexpr char* kAutoDiscover = ""; 40 | 41 | static const OptionId kPolicySoftmaxTemp; 42 | static const OptionId kHistoryFill; 43 | static const OptionId kWeightsId; 44 | static const OptionId kBackendId; 45 | static const OptionId kBackendOptionsId; 46 | static const OptionId kNNCacheSizeId; 47 | 48 | static void Populate(OptionsParser*); 49 | 50 | private: 51 | SharedBackendParams() = delete; 52 | }; 53 | 54 | } // namespace lczero -------------------------------------------------------------------------------- /src/neural/tables/activation_function.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018-2022 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | */ 18 | 19 | #pragma once 20 | 21 | namespace lczero { 22 | // The following list matches the one in net.proto. Ideally this would be done 23 | // by including proto/net.pb.h, but this is incompatible with nvcc. 24 | enum ActivationFunction { 25 | ACTIVATION_DEFAULT = 0, 26 | ACTIVATION_MISH = 1, 27 | ACTIVATION_RELU = 2, 28 | ACTIVATION_NONE = 3, 29 | ACTIVATION_TANH = 4, 30 | ACTIVATION_SIGMOID = 5, 31 | ACTIVATION_SELU = 6, 32 | ACTIVATION_SWISH = 7, 33 | ACTIVATION_RELU_2 = 8, 34 | ACTIVATION_SOFTMAX = 9, 35 | }; 36 | 37 | struct Activations { 38 | ActivationFunction default_activation = ACTIVATION_RELU; 39 | ActivationFunction smolgen_activation = ACTIVATION_SWISH; 40 | ActivationFunction ffn_activation = ACTIVATION_RELU_2; 41 | }; 42 | 43 | } // namespace lczero 44 | -------------------------------------------------------------------------------- /src/neural/wrapper.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2024 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | #include 32 | 33 | #include "neural/network.h" 34 | #include "neural/register.h" 35 | 36 | namespace lczero { 37 | 38 | class NetworkAsBackendFactory : public BackendFactory { 39 | public: 40 | using FactoryFunc = std::function( 41 | const std::optional&, const OptionsDict&)>; 42 | 43 | NetworkAsBackendFactory(const std::string& name, FactoryFunc factory, 44 | int priority = 0); 45 | 46 | int GetPriority() const override { return priority_; } 47 | std::string_view GetName() const override { return name_; } 48 | std::unique_ptr Create(const OptionsDict&) override; 49 | 50 | private: 51 | std::string name_; 52 | FactoryFunc factory_; 53 | int priority_; 54 | }; 55 | 56 | } // namespace lczero -------------------------------------------------------------------------------- /src/neural/xla/print_hlo.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2024 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | 32 | #include "neural/xla/hlo.pb.h" 33 | 34 | namespace lczero { 35 | 36 | struct PrettyPrintHloOptions { 37 | // Print layout information (which is always major-to-minor now, e.g. 38 | // {3,2,1,0}. Switched off by default as it's just noise. 39 | bool print_layout = false; 40 | }; 41 | 42 | // Pretty-prints the given HLO module to the given stream. 43 | void PrettyPrintHlo(const pblczero::HloModuleProto& module, 44 | PrettyPrintHloOptions options, std::ostream& stream); 45 | 46 | } // namespace lczero -------------------------------------------------------------------------------- /src/rescorer_main.cc: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018-2021 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #include 29 | 30 | #include "chess/board.h" 31 | #include "trainingdata/rescorer.h" 32 | #include "utils/commandline.h" 33 | #include "utils/esc_codes.h" 34 | #include "utils/exception.h" 35 | #include "utils/logging.h" 36 | #include "version.h" 37 | 38 | int main(int argc, const char** argv) { 39 | using namespace lczero; 40 | EscCodes::Init(); 41 | LOGFILE << "Lc0 started."; 42 | CERR << EscCodes::Bold() << EscCodes::Red() << " _ _ _ _ _"; 43 | CERR << " _ |_ |_ | | | _ |_ _"; 44 | CERR << "| |_ _||_ |_|| |_ | " << EscCodes::Reset() << " v" 45 | << GetVersionStr() << " built " << __DATE__; 46 | 47 | try { 48 | InitializeMagicBitboards(); 49 | 50 | CommandLine::Init(argc, argv); 51 | CommandLine::RegisterMode( 52 | "rescore", "(default) Update data scores with tablebase support"); 53 | 54 | // Consuming optional "rescore" mode. 55 | CommandLine::ConsumeCommand("rescore"); 56 | RunRescorer(); 57 | } catch (std::exception& e) { 58 | std::cerr << "Unhandled exception: " << e.what() << std::endl; 59 | abort(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/search/artifacts.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2024 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | namespace lczero { 31 | 32 | // Contains the search artifacts that are needed e.g. to build the training 33 | // data. The selfplay loop would fetch this from search to build training data 34 | // frames. 35 | struct SearchArtifacts {}; 36 | 37 | } // namespace lczero -------------------------------------------------------------------------------- /src/search/classic/stoppers/alphazero.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "utils/optionsdict.h" 31 | 32 | namespace lczero { 33 | namespace classic { 34 | 35 | std::unique_ptr MakeAlphazeroTimeManager( 36 | int64_t move_overhead, const OptionsDict& params); 37 | 38 | } // namespace classic 39 | } // namespace lczero -------------------------------------------------------------------------------- /src/search/classic/stoppers/common.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "search/classic/stoppers/stoppers.h" 31 | #include "utils/optionsdict.h" 32 | #include "utils/optionsparser.h" 33 | 34 | namespace lczero { 35 | namespace classic { 36 | 37 | enum class RunType { kUci, kSelfplay }; 38 | void PopulateCommonStopperOptions(RunType for_what, OptionsParser* options); 39 | 40 | // Populates KLDGain and SmartPruning stoppers. 41 | void PopulateIntrinsicStoppers(ChainedSearchStopper* stopper, 42 | const OptionsDict& options); 43 | 44 | std::unique_ptr MakeCommonTimeManager( 45 | std::unique_ptr child_manager, const OptionsDict& options, 46 | int64_t move_overhead); 47 | 48 | } // namespace classic 49 | } // namespace lczero 50 | -------------------------------------------------------------------------------- /src/search/classic/stoppers/factory.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2019-2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "search/classic/stoppers/common.h" 31 | #include "search/classic/stoppers/timemgr.h" 32 | #include "utils/optionsdict.h" 33 | #include "utils/optionsparser.h" 34 | 35 | namespace lczero { 36 | namespace classic { 37 | 38 | // Populates UCI/command line flags with time management options. 39 | void PopulateTimeManagementOptions(RunType for_what, OptionsParser* options); 40 | 41 | // Creates a new time manager for a new search. 42 | std::unique_ptr MakeTimeManager(const OptionsDict& dict); 43 | 44 | } // namespace classic 45 | } // namespace lczero 46 | -------------------------------------------------------------------------------- /src/search/classic/stoppers/legacy.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "search/classic/stoppers/timemgr.h" 31 | #include "utils/optionsdict.h" 32 | 33 | namespace lczero { 34 | namespace classic { 35 | 36 | float ComputeEstimatedMovesToGo(int ply, float midpoint, float steepness); 37 | 38 | std::unique_ptr MakeLegacyTimeManager(int64_t move_overhead, 39 | const OptionsDict& params); 40 | 41 | } // namespace classic 42 | } // namespace lczero 43 | -------------------------------------------------------------------------------- /src/search/classic/stoppers/simple.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "utils/optionsdict.h" 31 | 32 | namespace lczero { 33 | namespace classic { 34 | 35 | std::unique_ptr MakeSimpleTimeManager( 36 | int64_t move_overhead, const OptionsDict& params); 37 | 38 | } // namespace classic 39 | } // namespace lczero 40 | -------------------------------------------------------------------------------- /src/search/classic/stoppers/smooth.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "search/classic/stoppers/timemgr.h" 31 | #include "utils/optionsdict.h" 32 | 33 | namespace lczero { 34 | namespace classic { 35 | 36 | std::unique_ptr MakeSmoothTimeManager(int64_t move_overhead, 37 | const OptionsDict& params); 38 | 39 | } // namespace classic 40 | } // namespace lczero -------------------------------------------------------------------------------- /src/search/classic/stoppers/timemgr.cc: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2019 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #include "search/classic/stoppers/timemgr.h" 29 | 30 | #include "search/classic/stoppers/stoppers.h" 31 | 32 | namespace lczero { 33 | namespace classic { 34 | 35 | StoppersHints::StoppersHints() { Reset(); } 36 | 37 | void StoppersHints::UpdateEstimatedRemainingTimeMs(int64_t v) { 38 | if (v < remaining_time_ms_) remaining_time_ms_ = v; 39 | } 40 | int64_t StoppersHints::GetEstimatedRemainingTimeMs() const { 41 | return remaining_time_ms_; 42 | } 43 | 44 | void StoppersHints::UpdateEstimatedRemainingPlayouts(int64_t v) { 45 | if (v < remaining_playouts_) remaining_playouts_ = v; 46 | } 47 | int64_t StoppersHints::GetEstimatedRemainingPlayouts() const { 48 | // Even if we exceeded limits, don't go crazy by not allowing any playouts. 49 | return std::max(decltype(remaining_playouts_){1}, remaining_playouts_); 50 | } 51 | 52 | void StoppersHints::UpdateEstimatedNps(float v) { estimated_nps_ = v; } 53 | 54 | std::optional StoppersHints::GetEstimatedNps() const { 55 | return estimated_nps_; 56 | } 57 | 58 | void StoppersHints::Reset() { 59 | // Slightly more than 3 years. 60 | remaining_time_ms_ = 100000000000; 61 | // Type for N in nodes is currently uint32_t, so set limit in order not to 62 | // overflow it. 63 | remaining_playouts_ = 4000000000; 64 | // NPS is not known. 65 | estimated_nps_.reset(); 66 | } 67 | 68 | } // namespace classic 69 | } // namespace lczero -------------------------------------------------------------------------------- /src/search/dag_classic/params.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018-2025 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "search/classic/params.h" 31 | 32 | namespace lczero { 33 | namespace dag_classic { 34 | 35 | using ContemptMode = classic::ContemptMode; 36 | using SearchParams = classic::BaseSearchParams; 37 | 38 | } // namespace dag_classic 39 | } // namespace lczero 40 | -------------------------------------------------------------------------------- /src/search/dag_classic/stoppers/alphazero.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "utils/optionsdict.h" 31 | 32 | namespace lczero { 33 | namespace dag_classic { 34 | 35 | std::unique_ptr MakeAlphazeroTimeManager( 36 | int64_t move_overhead, const OptionsDict& params); 37 | 38 | } // namespace dag_classic 39 | } // namespace lczero -------------------------------------------------------------------------------- /src/search/dag_classic/stoppers/common.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "search/dag_classic/stoppers/stoppers.h" 31 | #include "utils/optionsdict.h" 32 | #include "utils/optionsparser.h" 33 | 34 | namespace lczero { 35 | namespace dag_classic { 36 | 37 | enum class RunType { kUci, kSimpleUci, kSelfplay }; 38 | void PopulateCommonStopperOptions(RunType for_what, OptionsParser* options); 39 | 40 | // Populates KLDGain and SmartPruning stoppers. 41 | void PopulateIntrinsicStoppers(ChainedSearchStopper* stopper, 42 | const OptionsDict& options); 43 | 44 | std::unique_ptr MakeCommonTimeManager( 45 | std::unique_ptr child_manager, const OptionsDict& options, 46 | int64_t move_overhead); 47 | 48 | } // namespace dag_classic 49 | } // namespace lczero 50 | -------------------------------------------------------------------------------- /src/search/dag_classic/stoppers/factory.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2019-2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "search/dag_classic/stoppers/common.h" 31 | #include "search/dag_classic/stoppers/timemgr.h" 32 | #include "utils/optionsdict.h" 33 | #include "utils/optionsparser.h" 34 | 35 | namespace lczero { 36 | namespace dag_classic { 37 | 38 | // Populates UCI/command line flags with time management options. 39 | void PopulateTimeManagementOptions(RunType for_what, OptionsParser* options); 40 | 41 | // Creates a new time manager for a new search. 42 | std::unique_ptr MakeTimeManager(const OptionsDict& dict); 43 | 44 | } // namespace dag_classic 45 | } // namespace lczero 46 | -------------------------------------------------------------------------------- /src/search/dag_classic/stoppers/legacy.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "search/dag_classic/stoppers/timemgr.h" 31 | #include "utils/optionsdict.h" 32 | 33 | namespace lczero { 34 | namespace dag_classic { 35 | 36 | float ComputeEstimatedMovesToGo(int ply, float midpoint, float steepness); 37 | 38 | std::unique_ptr MakeLegacyTimeManager(int64_t move_overhead, 39 | const OptionsDict& params); 40 | 41 | } // namespace dag_classic 42 | } // namespace lczero 43 | -------------------------------------------------------------------------------- /src/search/dag_classic/stoppers/simple.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "utils/optionsdict.h" 31 | 32 | namespace lczero { 33 | namespace dag_classic { 34 | 35 | std::unique_ptr MakeSimpleTimeManager( 36 | int64_t move_overhead, const OptionsDict& params); 37 | 38 | } // namespace dag_classic 39 | } // namespace lczero 40 | -------------------------------------------------------------------------------- /src/search/dag_classic/stoppers/smooth.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "search/dag_classic/stoppers/timemgr.h" 31 | #include "utils/optionsdict.h" 32 | 33 | namespace lczero { 34 | namespace dag_classic { 35 | 36 | std::unique_ptr MakeSmoothTimeManager(int64_t move_overhead, 37 | const OptionsDict& params); 38 | 39 | } // namespace dag_classic 40 | } // namespace lczero -------------------------------------------------------------------------------- /src/search/dag_classic/stoppers/timemgr.cc: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2019 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #include "search/dag_classic/stoppers/timemgr.h" 29 | 30 | #include "search/dag_classic/stoppers/stoppers.h" 31 | 32 | namespace lczero { 33 | namespace dag_classic { 34 | 35 | StoppersHints::StoppersHints() { Reset(); } 36 | 37 | void StoppersHints::UpdateEstimatedRemainingTimeMs(int64_t v) { 38 | if (v < remaining_time_ms_) remaining_time_ms_ = v; 39 | } 40 | int64_t StoppersHints::GetEstimatedRemainingTimeMs() const { 41 | return remaining_time_ms_; 42 | } 43 | 44 | void StoppersHints::UpdateEstimatedRemainingPlayouts(int64_t v) { 45 | if (v < remaining_playouts_) remaining_playouts_ = v; 46 | } 47 | int64_t StoppersHints::GetEstimatedRemainingPlayouts() const { 48 | // Even if we exceeded limits, don't go crazy by not allowing any playouts. 49 | return std::max(decltype(remaining_playouts_){1}, remaining_playouts_); 50 | } 51 | 52 | void StoppersHints::UpdateEstimatedNps(float v) { estimated_nps_ = v; } 53 | 54 | std::optional StoppersHints::GetEstimatedNps() const { 55 | return estimated_nps_; 56 | } 57 | 58 | void StoppersHints::Reset() { 59 | // Slightly more than 3 years. 60 | remaining_time_ms_ = 100000000000; 61 | // Type for N in nodes is currently uint32_t, so set limit in order not to 62 | // overflow it. 63 | remaining_playouts_ = 4000000000; 64 | // NPS is not known. 65 | estimated_nps_.reset(); 66 | } 67 | 68 | } // namespace dag_classic 69 | } // namespace lczero -------------------------------------------------------------------------------- /src/search/mock_search.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2025 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | 32 | #include "search/search.h" 33 | 34 | namespace lczero { 35 | 36 | class MockSearch : public SearchBase { 37 | public: 38 | using SearchBase::SearchBase; 39 | UciResponder* GetUciResponder() const { return uci_responder_; } 40 | MOCK_METHOD(void, SetBackend, (Backend * backend), (override)); 41 | MOCK_METHOD(void, SetSyzygyTablebase, (SyzygyTablebase * tb), (override)); 42 | MOCK_METHOD(void, NewGame, (), (override)); 43 | MOCK_METHOD(void, SetPosition, (const GameState&), (override)); 44 | MOCK_METHOD(void, StartSearch, (const GoParams&), (override)); 45 | MOCK_METHOD(void, StartClock, (), (override)); 46 | MOCK_METHOD(void, WaitSearch, (), (override)); 47 | MOCK_METHOD(void, StopSearch, (), (override)); 48 | MOCK_METHOD(void, AbortSearch, (), (override)); 49 | MOCK_METHOD(SearchArtifacts, GetArtifacts, (), (const, override)); 50 | }; 51 | 52 | class MockSearchFactory : public SearchFactory { 53 | public: 54 | MOCK_METHOD(std::string_view, GetName, (), (const, override)); 55 | MOCK_METHOD(void, PopulateParams, (OptionsParser*), (const, override)); 56 | MOCK_METHOD(std::unique_ptr, CreateSearch, 57 | (UciResponder*, const OptionsDict*), (const, override)); 58 | }; 59 | 60 | } // namespace lczero 61 | -------------------------------------------------------------------------------- /src/search/register.cc: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018-2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #include "search/register.h" 29 | 30 | #include 31 | 32 | namespace lczero { 33 | 34 | SearchManager* SearchManager::Get() { 35 | static SearchManager factory; 36 | return &factory; 37 | } 38 | 39 | void SearchManager::AddSearchFactory(std::unique_ptr algorithm) { 40 | algorithms_.push_back(std::move(algorithm)); 41 | } 42 | 43 | std::vector SearchManager::GetSearchNames() const { 44 | std::vector res; 45 | res.reserve(algorithms_.size()); 46 | std::transform(algorithms_.begin(), algorithms_.end(), 47 | std::back_inserter(res), 48 | [](const auto& alg) { return alg->GetName(); }); 49 | return res; 50 | } 51 | 52 | SearchFactory* SearchManager::GetFactoryByName(std::string_view name) const { 53 | auto it = 54 | std::find_if(algorithms_.begin(), algorithms_.end(), 55 | [name](const auto& alg) { return alg->GetName() == name; }); 56 | return it == algorithms_.end() ? nullptr : it->get(); 57 | } 58 | 59 | } // namespace lczero -------------------------------------------------------------------------------- /src/search/register.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2024 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | #include 32 | 33 | #include "search/search.h" 34 | 35 | namespace lczero { 36 | 37 | class SearchFactory; 38 | 39 | // A singleton class that keeps one instance of each search algorithm's factory. 40 | class SearchManager { 41 | public: 42 | static SearchManager* Get(); 43 | void AddSearchFactory(std::unique_ptr); 44 | 45 | std::vector GetSearchNames() const; 46 | 47 | // Returns the factory for the given algorithm name. Returns nullptr if not 48 | // found. 49 | SearchFactory* GetFactoryByName(std::string_view name) const; 50 | 51 | struct Register { 52 | Register(std::unique_ptr factory) { 53 | SearchManager::Get()->AddSearchFactory(std::move(factory)); 54 | } 55 | }; 56 | 57 | private: 58 | SearchManager() = default; 59 | 60 | std::vector> algorithms_; 61 | }; 62 | 63 | #define REGISTER_SEARCH(alg) \ 64 | namespace { \ 65 | static SearchManager::Register reg3b50Y_##alg(std::make_unique()); \ 66 | } 67 | 68 | } // namespace lczero -------------------------------------------------------------------------------- /src/selfplay/loop.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | 32 | #include "chess/uciloop.h" 33 | #include "selfplay/tournament.h" 34 | #include "utils/optionsparser.h" 35 | 36 | namespace lczero { 37 | 38 | class SelfPlayLoop { 39 | public: 40 | SelfPlayLoop(StringUciResponder* uci_responder) 41 | : uci_responder_(uci_responder) { 42 | uci_responder_->PopulateParams(&options_); 43 | } 44 | ~SelfPlayLoop(); 45 | 46 | void Run(); 47 | 48 | private: 49 | void SendGameInfo(const GameInfo& move); 50 | void SendTournament(const TournamentInfo& info); 51 | 52 | void EnsureOptionsSent(); 53 | 54 | StringUciResponder* uci_responder_ = nullptr; // absl_notnull 55 | OptionsParser options_; 56 | std::unique_ptr tournament_; 57 | std::unique_ptr thread_; 58 | }; 59 | 60 | } // namespace lczero 61 | -------------------------------------------------------------------------------- /src/tools/backendbench.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | namespace lczero { 31 | 32 | class BackendBenchmark { 33 | public: 34 | BackendBenchmark() = default; 35 | 36 | void Run(); 37 | }; 38 | 39 | } // namespace lczero 40 | -------------------------------------------------------------------------------- /src/tools/describenet.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2021 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "proto/net.pb.h" 31 | 32 | namespace lczero { 33 | 34 | void DescribeNetworkCmd(); 35 | void ShowNetworkGenericInfo(const pblczero::Net& weights); 36 | void ShowNetworkFormatInfo(const pblczero::Net& weights); 37 | void ShowNetworkTrainingInfo(const pblczero::Net& weights); 38 | void ShowNetworkWeightsInfo(const pblczero::Net& weights); 39 | void ShowNetworkOnnxInfo(const pblczero::Net& weights, 40 | bool show_onnx_internals); 41 | void ShowAllNetworkInfo(const pblczero::Net& weights); 42 | 43 | } // namespace lczero -------------------------------------------------------------------------------- /src/tools/leela2onnx.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2021 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | namespace lczero { 31 | 32 | void ConvertLeelaToOnnx(); 33 | 34 | } // namespace lczero -------------------------------------------------------------------------------- /src/tools/onnx2leela.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2021 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | namespace lczero { 31 | 32 | void ConvertOnnxToLeela(); 33 | 34 | } // namespace lczero -------------------------------------------------------------------------------- /src/trainingdata/reader.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2021 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "trainingdata/trainingdata.h" 31 | 32 | namespace lczero { 33 | 34 | // Constructs InputPlanes from training data. 35 | // 36 | // NOTE: If the training data is a cannonical type, the canonicalization 37 | // transforms are reverted before returning, since it is assumed that the data 38 | // will be used with DecodeMoveFromInput or PopulateBoard which assume the 39 | // InputPlanes are not transformed. 40 | InputPlanes PlanesFromTrainingData(const V6TrainingData& data); 41 | 42 | class TrainingDataReader { 43 | public: 44 | // Opens the given file to read chunk data from. 45 | TrainingDataReader(std::string filename); 46 | 47 | ~TrainingDataReader(); 48 | 49 | // Reads a chunk. Returns true if a chunk was read. 50 | bool ReadChunk(V6TrainingData* data); 51 | 52 | // Gets full filename of the file being read. 53 | std::string GetFileName() const { return filename_; } 54 | 55 | private: 56 | std::string filename_; 57 | gzFile fin_; 58 | bool format_v6 = false; 59 | }; 60 | 61 | } // namespace lczero 62 | -------------------------------------------------------------------------------- /src/trainingdata/rescorer.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018-2024 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | 32 | #include "chess/uciloop.h" 33 | #include "utils/optionsparser.h" 34 | 35 | namespace lczero { 36 | 37 | void RunRescorer(); 38 | 39 | } // namespace lczero 40 | -------------------------------------------------------------------------------- /src/trainingdata/writer.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018-2021 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | #include 32 | 33 | namespace lczero { 34 | 35 | struct V6TrainingData; 36 | 37 | class TrainingDataWriter { 38 | public: 39 | // Creates a new file to write in data directory. It will has @game_id 40 | // somewhere in the filename. 41 | TrainingDataWriter(int game_id); 42 | TrainingDataWriter(std::string filename); 43 | 44 | ~TrainingDataWriter() { 45 | if (fout_) Finalize(); 46 | } 47 | 48 | // Writes a chunk. 49 | void WriteChunk(const V6TrainingData& data); 50 | 51 | // Flushes file and closes it. 52 | void Finalize(); 53 | 54 | // Gets full filename of the file written. 55 | std::string GetFileName() const { return filename_; } 56 | 57 | private: 58 | std::string filename_; 59 | gzFile fout_; 60 | }; 61 | 62 | } // namespace lczero 63 | -------------------------------------------------------------------------------- /src/utils/bf16_utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2024 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | #pragma once 28 | namespace lczero { 29 | 30 | static inline uint16_t FP32toBF16(float f32) { 31 | uint32_t x; 32 | memcpy(&x, &f32, sizeof(float)); 33 | #ifndef BF16_TRUNC 34 | // Make sure NaN stays a NaN (the same way intel does fp16 conversion). 35 | if ((x & 0x7f800000) == 0x7f800000 && (x & 0x7fffff)) return (x >> 16) | 0x40; 36 | // Round to nearest even number. 37 | if (x & 0x17fff) x += 0x8000; 38 | #endif 39 | return x >> 16; 40 | } 41 | 42 | static inline float BF16toFP32(uint16_t bf16) { 43 | uint32_t x = static_cast(bf16) << 16; 44 | float f32; 45 | memcpy(&f32, &x, sizeof(float)); 46 | return f32; 47 | } 48 | 49 | } // namespace lczero 50 | -------------------------------------------------------------------------------- /src/utils/commandline.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | #include 32 | 33 | namespace lczero { 34 | 35 | class CommandLine { 36 | public: 37 | CommandLine() = delete; 38 | 39 | // This function must be called before any other. 40 | static void Init(int argc, const char** argv); 41 | 42 | // Name of the executable filename that was run. 43 | static const std::string& BinaryName() { return binary_; } 44 | 45 | // Directory where the binary is run. Without trailing slash. 46 | static std::string BinaryDirectory(); 47 | 48 | // If the first command line parameter is @command, remove it and return 49 | // true. Otherwise return false. 50 | static bool ConsumeCommand(std::string_view command); 51 | 52 | // Command line arguments. 53 | static const std::vector& Arguments() { return arguments_; } 54 | 55 | static void RegisterMode(const std::string& mode, 56 | const std::string& description); 57 | 58 | static const std::vector>& GetModes() { 59 | return modes_; 60 | } 61 | 62 | private: 63 | static std::string binary_; 64 | static std::vector arguments_; 65 | static std::vector> modes_; 66 | }; 67 | 68 | } // namespace lczero 69 | -------------------------------------------------------------------------------- /src/utils/configfile.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | #include 32 | 33 | namespace lczero { 34 | 35 | class OptionsParser; 36 | 37 | class ConfigFile { 38 | public: 39 | ConfigFile() = delete; 40 | 41 | // This function must be called after PopulateOptions. 42 | static bool Init(); 43 | 44 | // Returns the command line arguments from the config file. 45 | static const std::vector& Arguments() { return arguments_; } 46 | 47 | // Add the config file parameter to the options dictionary. 48 | static void PopulateOptions(OptionsParser* options); 49 | 50 | private: 51 | // Parses the config file into the arguments_ vector. 52 | static bool ParseFile(std::string& filename); 53 | 54 | // Returns the absolute path to the config file argument given. 55 | static std::string ProcessConfigFlag(const std::vector& args); 56 | 57 | static std::vector arguments_; 58 | }; 59 | 60 | } // namespace lczero 61 | -------------------------------------------------------------------------------- /src/utils/esc_codes.cc: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2019 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #include "utils/esc_codes.h" 29 | 30 | #ifdef _WIN32 31 | #include 32 | #endif 33 | 34 | namespace lczero { 35 | 36 | bool EscCodes::enabled_; 37 | 38 | void EscCodes::Init() { 39 | #ifdef _WIN32 40 | HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); 41 | DWORD mode; 42 | GetConsoleMode(h, &mode); 43 | enabled_ = SetConsoleMode(h, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING); 44 | #else 45 | enabled_ = true; 46 | #endif 47 | } 48 | 49 | } // namespace lczero 50 | -------------------------------------------------------------------------------- /src/utils/exception.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | #include "utils/logging.h" 32 | 33 | namespace lczero { 34 | 35 | // Exception to throw around. 36 | class Exception : public std::runtime_error { 37 | public: 38 | Exception(const std::string& what) : std::runtime_error(what) { 39 | LOGFILE << "Exception: " << what; 40 | } 41 | }; 42 | 43 | } // namespace lczero 44 | -------------------------------------------------------------------------------- /src/utils/files.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2021 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #include 29 | 30 | namespace lczero { 31 | 32 | // Reads (possibly gz-compressed) file to string. Throws on error. 33 | std::string ReadFileToString(const std::string& filename); 34 | 35 | // Writes string to file, without compression. Throws on error. 36 | void WriteStringToFile(const std::string& filename, 37 | std::string_view content); 38 | 39 | // Writes string to gz-compressed file. Throws on error. 40 | void WriteStringToGzFile(const std::string& filename, 41 | std::string_view content); 42 | } // namespace lczero 43 | -------------------------------------------------------------------------------- /src/utils/filesystem.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018-2019 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | namespace lczero { 36 | 37 | // Creates directory at a given path. Throws exception if cannot. 38 | // Returns silently if already exists. 39 | void CreateDirectory(const std::string& path); 40 | 41 | // Returns list of full paths of regular files in this directory. 42 | // Silently returns empty vector on error. 43 | std::vector GetFileList(const std::string& directory); 44 | 45 | // Returns size of a file, 0 if file doesn't exist or can't be read. 46 | uint64_t GetFileSize(const std::string& filename); 47 | 48 | // Returns modification time of a file, 0 if file doesn't exist or can't be read. 49 | time_t GetFileTime(const std::string& filename); 50 | 51 | // Returns the base directory relative to which user specific non-essential data 52 | // files are stored or an empty string if unspecified. 53 | std::string GetUserCacheDirectory(); 54 | 55 | // Returns the base directory relative to which user specific configuration 56 | // files are stored or an empty string if unspecified. 57 | std::string GetUserConfigDirectory(); 58 | 59 | // Returns the base directory relative to which user specific data files are 60 | // stored or an empty string if unspecified. 61 | std::string GetUserDataDirectory(); 62 | 63 | // Returns a vector of base directories to search for configuration files. 64 | std::vector GetSystemConfigDirectoryList(); 65 | 66 | // Returns a vector of base directories to search for data files. 67 | std::vector GetSystemDataDirectoryList(); 68 | 69 | } // namespace lczero 70 | -------------------------------------------------------------------------------- /src/utils/hashcat.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #include 29 | #include 30 | 31 | #pragma once 32 | namespace lczero { 33 | 34 | // Tries to scramble @val. 35 | inline uint64_t Hash(uint64_t val) { 36 | return 0xfad0d7f2fbb059f1ULL * (val + 0xbaad41cdcb839961ULL) + 37 | 0x7acec0050bf82f43ULL * ((val >> 31) + 0xd571b3a92b1b2755ULL); 38 | } 39 | 40 | // Appends value to a hash. 41 | inline uint64_t HashCat(uint64_t hash, uint64_t x) { 42 | hash ^= 0x299799adf0d95defULL + Hash(x) + (hash << 6) + (hash >> 2); 43 | return hash; 44 | } 45 | 46 | // Combines 64-bit values into concatenated hash. 47 | inline uint64_t HashCat(std::initializer_list args) { 48 | uint64_t hash = 0; 49 | for (uint64_t x : args) hash = HashCat(hash, x); 50 | return hash; 51 | } 52 | 53 | } // namespace lczero 54 | -------------------------------------------------------------------------------- /src/utils/hashcat_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #include "utils/hashcat.h" 29 | #include 30 | 31 | namespace lczero { 32 | 33 | TEST(HashCat, TestCollision) { 34 | uint64_t hash1 = HashCat({0x8000000010500000, 0x4000080000002000, 35 | 0x8000000000002000, 0x4000000000000000}); 36 | uint64_t hash2 = HashCat({0x4000000010500000, 0x1000080000002000, 37 | 0x4000000000002000, 0x1000000000000000}); 38 | EXPECT_NE(hash1, hash2); 39 | } 40 | 41 | } // namespace lczero 42 | 43 | int main(int argc, char** argv) { 44 | ::testing::InitGoogleTest(&argc, argv); 45 | return RUN_ALL_TESTS(); 46 | } 47 | -------------------------------------------------------------------------------- /src/utils/histogram.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | namespace lczero { 35 | 36 | // Histogram with a logarithmic x-axis. 37 | // 38 | // 0.50 + 39 | // | 40 | // | 41 | // | 42 | // 0.40 | 43 | // 44 | // .... 45 | // 46 | // | 47 | // 0.10 + 48 | // | 49 | // |# 50 | // |# ## #| 51 | // |# # # #### # # # #| 52 | // 0.00 +----+----+----+----+---- ... +----+ 53 | // 54 | // -inf -15 -14 -13 -12 5 inf 55 | 56 | class Histogram { 57 | public: 58 | // Creates a histogram with default scales. 59 | Histogram(); 60 | 61 | // Creates a histogram from 10^min_exp to 10^max_exp 62 | // with minor_scales spacing. 63 | Histogram(int min_exp, int max_exp, int minor_scales); 64 | 65 | void Clear(); 66 | 67 | // Adds a sample. 68 | void Add(double value); 69 | 70 | // Dumps the histogram to stderr. 71 | void Dump() const; 72 | 73 | private: 74 | int GetIndex(double val) const; 75 | 76 | static constexpr int kDefaultMinExp = -15; 77 | static constexpr int kDefaultMaxExp = 5; 78 | static constexpr int kDefaultMinorScales = 5; 79 | 80 | const int min_exp_; 81 | const int max_exp_; 82 | const int minor_scales_; 83 | const int major_scales_; 84 | const int total_scales_; 85 | std::vector buckets_; 86 | double total_; 87 | double max_; 88 | }; 89 | 90 | } // namespace lczero 91 | -------------------------------------------------------------------------------- /src/utils/numa.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | namespace lczero { 31 | 32 | class Numa { 33 | public: 34 | Numa() = delete; 35 | 36 | // Initialize and display statistics about processor configuration. 37 | static void Init(); 38 | 39 | // Bind thread to processor group. 40 | static void BindThread(int id); 41 | 42 | private: 43 | static int threads_per_core_; 44 | }; 45 | 46 | } // namespace lczero 47 | -------------------------------------------------------------------------------- /src/utils/random.cc: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #include "random.h" 29 | #include 30 | 31 | namespace lczero { 32 | 33 | Random::Random() : gen_(std::random_device()()) {} 34 | 35 | Random& Random::Get() { 36 | static Random rand; 37 | return rand; 38 | } 39 | 40 | int Random::GetInt(int min, int max) { 41 | Mutex::Lock lock(mutex_); 42 | std::uniform_int_distribution<> dist(min, max); 43 | return dist(gen_); 44 | } 45 | 46 | bool Random::GetBool() { return GetInt(0, 1) != 0; } 47 | 48 | double Random::GetDouble(double maxval) { 49 | Mutex::Lock lock(mutex_); 50 | std::uniform_real_distribution<> dist(0.0, maxval); 51 | return dist(gen_); 52 | } 53 | 54 | float Random::GetFloat(float maxval) { 55 | Mutex::Lock lock(mutex_); 56 | std::uniform_real_distribution<> dist(0.0, maxval); 57 | return dist(gen_); 58 | } 59 | 60 | std::string Random::GetString(int length) { 61 | std::string result; 62 | for (int i = 0; i < length; ++i) { 63 | result += 'a' + GetInt(0, 25); 64 | } 65 | return result; 66 | } 67 | 68 | double Random::GetGamma(double alpha, double beta) { 69 | Mutex::Lock lock(mutex_); 70 | std::gamma_distribution dist(alpha, beta); 71 | return dist(gen_); 72 | } 73 | 74 | } // namespace lczero 75 | -------------------------------------------------------------------------------- /src/utils/random.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | #include 32 | #include 33 | #include "utils/mutex.h" 34 | 35 | namespace lczero { 36 | 37 | class Random { 38 | public: 39 | static Random& Get(); 40 | double GetDouble(double max_val); 41 | float GetFloat(float max_val); 42 | double GetGamma(double alpha, double beta); 43 | // Both sides are included. 44 | int GetInt(int min, int max); 45 | std::string GetString(int length); 46 | bool GetBool(); 47 | template 48 | void Shuffle(RandomAccessIterator s, RandomAccessIterator e); 49 | 50 | private: 51 | Random(); 52 | 53 | Mutex mutex_; 54 | std::mt19937 gen_ GUARDED_BY(mutex_); 55 | }; 56 | 57 | template 58 | void Random::Shuffle(RandomAccessIterator s, RandomAccessIterator e) { 59 | Mutex::Lock lock(mutex_); 60 | std::shuffle(s, e, gen_); 61 | } 62 | 63 | } // namespace lczero 64 | -------------------------------------------------------------------------------- /src/utils/smallarray.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | 32 | namespace lczero { 33 | 34 | // Non resizeable array which can contain up to 255 elements. 35 | template 36 | class SmallArray { 37 | public: 38 | SmallArray() = delete; 39 | SmallArray(size_t size) : size_(size), data_(std::make_unique(size)) {} 40 | SmallArray(SmallArray&&); // TODO implement when needed 41 | T& operator[](int idx) { return data_[idx]; } 42 | const T& operator[](int idx) const { return data_[idx]; } 43 | int size() const { return size_; } 44 | 45 | private: 46 | unsigned char size_; 47 | std::unique_ptr data_; 48 | }; 49 | 50 | } // namespace lczero 51 | -------------------------------------------------------------------------------- /src/utils/spinhelper.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2023 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | #include 32 | 33 | #include "utils/mutex.h" 34 | 35 | namespace lczero { 36 | 37 | class SpinHelper { 38 | public: 39 | virtual ~SpinHelper() = default; 40 | virtual void Backoff() {} 41 | virtual void Wait() {} 42 | }; 43 | 44 | class ExponentialBackoffSpinHelper : public SpinHelper { 45 | public: 46 | ExponentialBackoffSpinHelper() 47 | : backoff_iters_(kMinBackoffIters), spin_to_sleep_iters_(0) {} 48 | 49 | virtual void Backoff() { 50 | thread_local std::uniform_int_distribution distribution; 51 | thread_local std::minstd_rand generator(std::random_device{}()); 52 | const size_t spin_count = distribution( 53 | generator, decltype(distribution)::param_type{0, backoff_iters_}); 54 | 55 | for (size_t i = 0; i < spin_count; i++) SpinloopPause(); 56 | 57 | backoff_iters_ = std::min(2 * backoff_iters_, kMaxBackoffIters); 58 | spin_to_sleep_iters_ = 0; 59 | } 60 | 61 | // Spin to sleep 62 | virtual void Wait() { 63 | if (spin_to_sleep_iters_ < kMaxSpinToSleepIters) { 64 | spin_to_sleep_iters_++; 65 | SpinloopPause(); 66 | } else { 67 | spin_to_sleep_iters_ = 0; 68 | std::this_thread::sleep_for(kSleepDuration); 69 | } 70 | } 71 | 72 | private: 73 | static constexpr size_t kMaxSpinToSleepIters = 0x10000; 74 | static constexpr size_t kMinBackoffIters = 0x20; 75 | static constexpr size_t kMaxBackoffIters = 0x400; 76 | static constexpr std::chrono::microseconds kSleepDuration{1000}; 77 | 78 | size_t backoff_iters_; 79 | size_t spin_to_sleep_iters_; 80 | }; 81 | 82 | } // namespace lczero 83 | -------------------------------------------------------------------------------- /src/utils/string.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | #include 32 | 33 | namespace lczero { 34 | 35 | // Joins strings using @delim as delimiter. 36 | std::string StrJoin(const std::vector& strings, 37 | const std::string& delim = " "); 38 | 39 | // Splits strings at whitespace. 40 | std::vector StrSplitAtWhitespace(const std::string& str); 41 | 42 | // Split string by delimiter. 43 | std::vector StrSplit(const std::string& str, 44 | const std::string& delim); 45 | 46 | // Parses comma-separated list of integers. 47 | std::vector ParseIntList(const std::string& str); 48 | 49 | // Trims a string of whitespace from the start. 50 | std::string LeftTrim(std::string str); 51 | 52 | // Trims a string of whitespace from the end. 53 | std::string RightTrim(std::string str); 54 | 55 | // Trims a string of whitespace from both ends. 56 | std::string Trim(std::string str); 57 | 58 | // Returns whether strings are equal, ignoring case. 59 | bool StringsEqualIgnoreCase(const std::string& a, const std::string& b); 60 | 61 | // Flow text into lines of width up to @width. 62 | std::vector FlowText(const std::string& src, size_t width); 63 | 64 | } // namespace lczero 65 | -------------------------------------------------------------------------------- /src/utils/transpose.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018-2020 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #pragma once 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | namespace lczero { 35 | 36 | // Transposes flattened tensor from @from into @to. @to must have space for 37 | // from.size() elements. 38 | // @dims -- Dimensions of @from tensor. For example, {120, 60, 3, 3} 39 | // @order -- New-to-old dimension index mapping. For example {3, 2, 0, 1} 40 | template 41 | void TransposeTensor(const std::vector& dims, std::vector order, 42 | const std::vector from, T* to) { 43 | assert(from.size() == std::accumulate(dims.begin(), dims.end(), 1u, 44 | std::multiplies())); 45 | if (order.empty()) { 46 | for (size_t i = 0; i < dims.size(); ++i) 47 | order.push_back(dims.size() - i - 1); 48 | } 49 | std::vector cur_idx(dims.size()); 50 | for (size_t _ = 0; _ < from.size(); ++_) { 51 | size_t from_idx = 0; 52 | for (int i : order) { 53 | from_idx *= dims[i]; 54 | from_idx += cur_idx[i]; 55 | } 56 | *to++ = from[from_idx]; 57 | for (int i = static_cast(dims.size()) - 1; i >= 0; --i) { 58 | if (++cur_idx[i] == dims[i]) { 59 | cur_idx[i] = 0; 60 | } else { 61 | break; 62 | } 63 | } 64 | } 65 | } 66 | 67 | } // namespace lczero 68 | -------------------------------------------------------------------------------- /src/utils/weights_adapter.cc: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | 28 | #include "src/utils/weights_adapter.h" 29 | 30 | namespace lczero { 31 | float LayerAdapter::Iterator::ExtractValue(const uint16_t* ptr, 32 | const LayerAdapter* adapter) { 33 | return *ptr / static_cast(0xffff) * adapter->range_ + adapter->min_; 34 | } 35 | 36 | LayerAdapter::LayerAdapter(const pblczero::Weights::Layer& layer) 37 | : data_(reinterpret_cast(layer.params().data())), 38 | size_(layer.params().size() / sizeof(uint16_t)), 39 | min_(layer.min_val()), 40 | range_(layer.max_val() - min_) {} 41 | 42 | std::vector LayerAdapter::as_vector() const { 43 | return std::vector(begin(), end()); 44 | } 45 | float LayerAdapter::Iterator::operator*() const { 46 | return ExtractValue(data_, adapter_); 47 | } 48 | float LayerAdapter::Iterator::operator[](size_t idx) const { 49 | return ExtractValue(data_ + idx, adapter_); 50 | } 51 | 52 | } // namespace lczero 53 | -------------------------------------------------------------------------------- /src/version.cc: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | #include "version.h" 28 | 29 | std::uint32_t GetVersionInt(int major, int minor, int patch) { 30 | return major * 1000000 + minor * 1000 + patch; 31 | } 32 | 33 | std::string GetVersionStr(int major, int minor, int patch, 34 | const std::string& postfix, 35 | const std::string& build_id) { 36 | auto v = std::to_string(major) + "." + std::to_string(minor) + "." + 37 | std::to_string(patch); 38 | if (!postfix.empty()) v += "-" + postfix; 39 | if (!build_id.empty()) v += "+" + build_id; 40 | return v; 41 | } 42 | -------------------------------------------------------------------------------- /src/version.h: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of Leela Chess Zero. 3 | Copyright (C) 2018 The LCZero Authors 4 | 5 | Leela Chess is free software: you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation, either version 3 of the License, or 8 | (at your option) any later version. 9 | 10 | Leela Chess is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with Leela Chess. If not, see . 17 | 18 | Additional permission under GNU GPL version 3 section 7 19 | 20 | If you modify this Program, or any covered work, by linking or 21 | combining it with NVIDIA Corporation's libraries from the NVIDIA CUDA 22 | Toolkit and the NVIDIA CUDA Deep Neural Network library (or a 23 | modified version of those libraries), containing parts covered by the 24 | terms of the respective license agreement, the licensors of this 25 | Program grant you additional permission to convey the resulting work. 26 | */ 27 | #pragma once 28 | 29 | // Versioning is performed according to the standard at 30 | // Creating a new version should be performed using scripts/bumpversion.py. 31 | 32 | #include 33 | #include 34 | #include "version.inc" 35 | #include "build_id.h" 36 | 37 | std::uint32_t GetVersionInt(int major = LC0_VERSION_MAJOR, 38 | int minor = LC0_VERSION_MINOR, 39 | int patch = LC0_VERSION_PATCH); 40 | 41 | std::string GetVersionStr(int major = LC0_VERSION_MAJOR, 42 | int minor = LC0_VERSION_MINOR, 43 | int patch = LC0_VERSION_PATCH, 44 | const std::string& postfix = LC0_VERSION_POSTFIX, 45 | const std::string& build_id = BUILD_IDENTIFIER); 46 | -------------------------------------------------------------------------------- /src/version.inc: -------------------------------------------------------------------------------- 1 | #define LC0_VERSION_MAJOR 0 2 | #define LC0_VERSION_MINOR 32 3 | #define LC0_VERSION_PATCH 0 4 | #define LC0_VERSION_POSTFIX "dev" 5 | -------------------------------------------------------------------------------- /subprojects/eigen.wrap: -------------------------------------------------------------------------------- 1 | [wrap-file] 2 | directory = eigen-3.4.0 3 | source_url = https://gitlab.com/libeigen/eigen/-/archive/3.4.0/eigen-3.4.0.tar.bz2 4 | source_filename = eigen-3.4.0.tar.bz2 5 | source_hash = b4c198460eba6f28d34894e3a5710998818515104d6e74e5cc331ce31e46e626 6 | patch_filename = eigen_3.4.0-1_patch.zip 7 | patch_url = https://wrapdb.mesonbuild.com/v2/eigen_3.4.0-1/get_patch 8 | patch_hash = fae999acdb3ea23eada3becdbde7f7f76755e94ad85fee7775b7ab1cf12e84e3 9 | 10 | [provide] 11 | eigen3 = eigen_dep 12 | 13 | -------------------------------------------------------------------------------- /subprojects/gaviotatb.wrap: -------------------------------------------------------------------------------- 1 | [wrap-git] 2 | url = https://github.com/michiguel/Gaviota-Tablebases 3 | revision = head 4 | 5 | patch_directory = gaviotatb 6 | 7 | 8 | -------------------------------------------------------------------------------- /subprojects/gtest.wrap: -------------------------------------------------------------------------------- 1 | [wrap-file] 2 | directory = googletest-1.15.2 3 | source_url = https://github.com/google/googletest/archive/refs/tags/v1.15.2.tar.gz 4 | source_filename = gtest-1.15.2.tar.gz 5 | source_hash = 7b42b4d6ed48810c5362c265a17faebe90dc2373c885e5216439d37927f02926 6 | patch_filename = gtest_1.15.2-4_patch.zip 7 | patch_url = https://wrapdb.mesonbuild.com/v2/gtest_1.15.2-4/get_patch 8 | patch_hash = a5151324b97e6a98fa7a0e8095523e6d5c4bb3431210d6ac4ad9800c345acf40 9 | source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/gtest_1.15.2-4/gtest-1.15.2.tar.gz 10 | wrapdb_version = 1.15.2-4 11 | 12 | [provide] 13 | gtest = gtest_dep 14 | gtest_main = gtest_main_dep 15 | gmock = gmock_dep 16 | gmock_main = gmock_main_dep 17 | -------------------------------------------------------------------------------- /subprojects/packagefiles/gaviotatb/meson.build: -------------------------------------------------------------------------------- 1 | project('gaviotatb', 'c') 2 | 3 | gaviotatb_src = [ 4 | 'gtb-probe.c', 5 | 'gtb-dec.c', 6 | 'gtb-att.c', 7 | 'sysport/sysport.c', 8 | 'compression/wrap.c', 9 | 'compression/huffman/hzip.c', 10 | 'compression/liblzf/lzf_c.c', 11 | 'compression/liblzf/lzf_d.c', 12 | 'compression/lzma/LzmaEnc.c', 13 | 'compression/lzma/LzmaDec.c', 14 | 'compression/lzma/Alloc.c', 15 | 'compression/lzma/LzFind.c', 16 | 'compression/lzma/Lzma86Enc.c', 17 | 'compression/lzma/Lzma86Dec.c', 18 | 'compression/lzma/Bra86.c' 19 | ] 20 | 21 | gaviotatb_includes = [ 22 | '.', 23 | 'sysport', 24 | 'compression', 25 | 'compression/huffman', 26 | 'compression/liblzf', 27 | 'compression/lzma', 28 | 'compression/zlib' 29 | ] 30 | 31 | gaviota_lib = static_library('gaviota', 32 | gaviotatb_src, 33 | c_args : '-Dz_uLong=uLong', 34 | include_directories : gaviotatb_includes) 35 | 36 | incdir = include_directories('.') 37 | 38 | gaviotatb_dep = declare_dependency( 39 | link_with : gaviota_lib, 40 | include_directories : gaviotatb_includes) 41 | -------------------------------------------------------------------------------- /subprojects/protobuf-3.6.0.wrap: -------------------------------------------------------------------------------- 1 | [wrap-file] 2 | directory = protobuf-3.6.0 3 | 4 | source_url = https://github.com/protocolbuffers/protobuf/releases/download/v3.6.0/protobuf-all-3.6.0.tar.gz 5 | source_filename = protobuf-all-3.6.0.tar.gz 6 | source_hash = 1532154addf85080330fdd037949d4653dfce16550df5c70ea0cd212d8aff3af 7 | 8 | patch_url = https://github.com/borg323/protobuf/releases/download/3.6.0/protobuf-3.6.0-wrap.zip 9 | patch_filename = protobuf-3.6.0-wrap.zip 10 | patch_hash = a14730d2e3702c4a0d7b3f05a380ec6b2c0b138a5b00539705b5c3a8df9885e3 11 | -------------------------------------------------------------------------------- /subprojects/protobuf.wrap: -------------------------------------------------------------------------------- 1 | [wrap-file] 2 | directory = protobuf-3.5.1 3 | 4 | source_url = https://github.com/google/protobuf/releases/download/v3.5.1/protobuf-all-3.5.1.tar.gz 5 | source_filename = protobuf-all-3.5.1.tar.gz 6 | source_hash = 72d43863f58567a9ea2054671fdb667867f9cf7865df623c7be630978ff97dff 7 | 8 | patch_url = https://github.com/borg323/protobuf/releases/download/3.5.1-2w/protobuf-3.5.1-2w-wrap.zip 9 | patch_filename = protobuf-3.5.1-2w-wrap.zip 10 | patch_hash = 5185ae7252941e252b075d3f845768296b079516f9f6feb0bd3ae63de7e9a52e 11 | -------------------------------------------------------------------------------- /subprojects/zlib.wrap: -------------------------------------------------------------------------------- 1 | [wrap-file] 2 | directory = zlib-1.2.11 3 | 4 | source_url = http://zlib.net/fossils/zlib-1.2.11.tar.gz 5 | source_filename = zlib-1.2.11.tar.gz 6 | source_hash = c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1 7 | 8 | patch_url = https://wrapdb.mesonbuild.com/v1/projects/zlib/1.2.11/4/get_zip 9 | patch_filename = zlib-1.2.11-4-wrap.zip 10 | patch_hash = f733976fbfc59e0bcde01aa9469a24eeb16faf0a4280b17e9eaa60a301d75657 11 | -------------------------------------------------------------------------------- /tensorflow.md: -------------------------------------------------------------------------------- 1 | To build with tensorflow under linux you need to install Tensorflow_cc from 2 | . Either release v1.9.0, v1.12.0 or 3 | v1.13.0. Tensorflow_cc requires a specific version of protobuf, which constrains 4 | the build. Release v1.9.0 works out of the box, since the default protobuf 5 | subproject (v3.5.1) is compatible and is used instead of a system installed 6 | version. In contrast release v1.12.0 needs protobuf v3.6.0 and release v1.13.0 7 | is built with protobuf 3.6.1 but also works with 3.6.0. For those versions 8 | `-Dprotobuf-3-6-0=true` should be added to the build command line. Note that 9 | this protobuf version has issues with static builds and crashes so is not 10 | recommended for normal use. The crashes look very similar to: 11 | * 12 | * 13 | --------------------------------------------------------------------------------