|
2 | 2 |
|
3 | 3 |  |
4 | 4 |
|
5 | | -The **Cerebrum library** can be used to train and utilize **“NNUE-like” neural networks** for chess engines. It was initially designed and created for the Orion UCI chess engine. |
| 5 | +The **Cerebrum library** can be used to train and utilize "[NNUE](https://www.chessprogramming.org/NNUE)-like" neural networks for chess engines. It was initially designed and created for the [Orion UCI chess engine](https://www.orionchess.com/). |
6 | 6 |
|
7 | | -The library consists of three main parts: |
| 7 | +<br/> |
8 | 8 |
|
9 | | - **Training code** (Python script) |
10 | | - **Inference code** (C files) |
11 | | - A **basic UCI chess engine** for demonstration purposes (Python script). |
| 9 | +## Changes in 1.0 |
12 | 10 |
|
13 | | -To use the library, you will need: |
| 11 | +- **Training now relies on game results** (from which a win ratio is deduced for each position during a game) and **material only** ! |
| 12 | +- **Data preparation** scripts are provided to automate the preparation of training data (using one or several pgn files) |
| 13 | +- **Network quantization** is performed at the end of each training epoch, allowing the choice between better accuracy or increased inference speed |
| 14 | +- A **basic UCI chess engine** is provided in two versions (standard or quantized) to demonstrate how to load and use the network |
| 15 | +- Inference C code is now also available in two versions (standard or quantized) |
14 | 16 |
|
15 | | - a **Python** runtime: https://www.python.org/ |
16 | | - some Python librairies: `pip install torch numpy scipy tqdm chess` |
| 17 | +<br/> |
17 | 18 |
|
18 | | -If you want to obtain the exact same neural network used in Orion 1.0, additional steps are required (here, for Windows): |
| 19 | +## Content and prerequisites (Windows) |
19 | 20 |
|
20 | | - download the **pgn-extract** tool: https://www.cs.kent.ac.uk/people/staff/djb/pgn-extract/ |
21 | | - put the `pgn-extract.exe` file in the folder "./cerebrum/v1.0/1. training/1. data preparation" |
22 | | - download the 3, 4, 5 pieces endgame **Syzygy tablebases**: http://tablebase.sesse.net/syzygy/3-4-5/ |
23 | | - put the downloaded files in the folder "./cerebrum/v1.0/1. training/1. data preparation/syzygy/3-4-5/" |
24 | | - download the 6 pieces endgame **Syzygy tablebases**: http://tablebase.sesse.net/syzygy/6-WDL/ |
25 | | - put the downloaded files in the folder "./cerebrum/v1.0/1. training/1. data preparation/syzygy/6-pieces/" |
| 21 | +The library consists of four main parts: |
| 22 | + |
| 23 | +1. **Data preparation code** (Python scripts) |
| 24 | +2. **Training code** (Python script) |
| 25 | +3. **Inference code** (C files) |
| 26 | +4. A **basic UCI chess engine** for demonstration purposes (Python script) |
| 27 | + |
| 28 | +<br/> |
| 29 | + |
| 30 | +To use the library, you will first need to: |
| 31 | + |
| 32 | +- Download the `v1.0` folder of this repository |
| 33 | +- Install a Python runtime: https://www.python.org/ |
| 34 | +- Install some Python librairies: `pip install torch numpy scipy tqdm chess` |
| 35 | +- Download the [pgn-extract](https://www.cs.kent.ac.uk/people/staff/djb/pgn-extract/) tool and put the `pgn-extract.exe` file in the folder `./1. data preparation/` |
| 36 | + |
| 37 | +<br/> |
| 38 | + |
| 39 | +Note that, **by default, training is forced to use the CPU**. If you have an NVIDIA GPU, you can: |
| 40 | + |
| 41 | +- Install the appropriate version of the torch library (see links below) to improve training speed |
| 42 | +- Set `FORCE_CPU_DEVICE` to `False` in the script `train.py` (line 53) to enable GPU usage |
| 43 | + |
| 44 | +<br/> |
| 45 | + |
| 46 | +## Usage (Windows) |
| 47 | + |
| 48 | +### Input (1<sup>st</sup> alternative) |
| 49 | + |
| 50 | +You can choose to provide one or several pgn files containing full games in the folder `./1. data preparation/pgn/`. |
| 51 | + |
| 52 | +Then launch the script `prepare.bat` in the folder `./1. data preparation/` to obtain a file named `positions-shuffled.txt` which will be stored in the same folder. |
| 53 | + |
| 54 | +This script will parse games and compute the average win ratio for each encountered position in all the games. It will also add some other statistical information (popcount, number of occurences of each position). |
| 55 | + |
| 56 | +Copy the `positions-shuffled.txt` file to the folder `./2. training/positions/` |
| 57 | + |
| 58 | +Note that, **by default, all games after 31.12.2023 will be ignored**. You can easily modify this by editing the script `prepare.bat` (see source to discover how). |
| 59 | + |
| 60 | +<br/> |
| 61 | + |
| 62 | +### Input (2<sup>nd</sup> alternative) |
| 63 | + |
| 64 | +Prepare your own handcrafted file containing position (fenstring), side to move, popcount, number of occurences of this position, win ratio: |
| 65 | + |
| 66 | +``` |
| 67 | +2r3k1/1q3p1p/6p1/3p4/1r3N2/2n5/2PQ1PPP/R2R2K1 w 18 1 1.0 |
| 68 | +1n1r1rk1/1pp3p1/p2bppp1/1q6/1P1P1P1P/P3PBP1/1Q3NK1/2R2R2 w 26 1 1.0 |
| 69 | +r7/pp2bNpr/2n3Rp/4pk1P/2n5/2N1B3/PP3P2/2KR4 w 21 1 0.5 |
| 70 | +rn2qr2/6b1/2pkBp1p/p1N4P/1p1P4/PQ3P2/1P6/2R1K2R b 22 1 0.5 |
| 71 | +6k1/3n1pp1/pb6/3Pp2p/4N3/4P1P1/2r2PKP/1R2B3 w 18 1 0.5 |
| 72 | +3k1n2/4q3/4pp2/1Np1p1p1/2P1P3/3P2P1/1PK2P2/7Q w 17 1 1.0 |
| 73 | +r5k1/pb3rb1/3Rn1p1/Rp2p2p/4PP1P/2P1BNP1/4NK2/8 b 22 1 0.5 |
| 74 | +8/3k4/7R/8/5PK1/8/r7/8 w 5 1 0.5 |
| 75 | +5rk1/5pbp/1qN1b1p1/1PQnP3/r3B3/3p1N2/p2P1PPP/2R2RK1 w 25 1 0.5 |
| 76 | +2b1k3/2p5/1pP5/4rp2/2B1pNpP/1P6/PKP5/3R4 w 17 1 1.0 |
| 77 | +``` |
| 78 | + |
| 79 | +<br/> |
| 80 | + |
| 81 | +This alternative can be useful when you already have a good idea of the win ratio associated to a (large) bunch of positions. |
| 82 | + |
| 83 | +The provided file must be called `positions-shuffled.txt` and be placed in the folder `./2. training/positions/`. |
| 84 | + |
| 85 | +<br/> |
| 86 | + |
| 87 | +### Training |
| 88 | + |
| 89 | +Launch the script `train.bat` in the folder `./2. training/`. |
| 90 | + |
| 91 | +This script will parse the `positions-shuffled.txt` file in the folder `./2. training/positions/`, split it in batches of training data (this step can be long), and then use these data to train the neural network. |
| 92 | + |
| 93 | +<br/> |
| 94 | + |
| 95 | +### Output |
| 96 | + |
| 97 | +Trained networks will be located in the folder `./2. training/networks/`. One network will be saved at the end of each training epoch. |
| 98 | + |
| 99 | +By default: |
| 100 | + |
| 101 | +- `epoch-5.txt` will be the last standard network (i.e. full precision: weights and biases are stored as `float`) |
| 102 | +- `epoch-5-q.txt` will be the last quantized network (i.e. less precision, but high inference speed: weights and biases are stored as `int8`) |
| 103 | + |
| 104 | +<br/> |
| 105 | + |
| 106 | +### How to use trained networks |
| 107 | + |
| 108 | +These networks can now be used in your own engine, using your own code, or: |
| 109 | + |
| 110 | +- using the provided inference C code in `./3. inference/1. standard/` or `./3. inference/2. quantized/` folders |
| 111 | +- using the provided inference Python code located in the `./4. engine/1. standard/` or `./4. engine/2. quantized/` folders |
| 112 | + |
| 113 | +<br/> |
| 114 | + |
| 115 | +In order to use your own trained network with the provided Cerebrum UCI chess engine: |
| 116 | + |
| 117 | +- Copy the `epoch-5.txt` (resp. the `epoch-5-q.txt`) file in the folder `4. engine/1. standard/` (resp. `4. engine/2. quantized/` |
| 118 | +- Rename it to `network.txt` |
| 119 | +- Launch the engine |
| 120 | + |
| 121 | +<br/> |
| 122 | + |
| 123 | +In order to use your own trained network with Orion UCI chess engine (assuming you did not change the network architecture): |
| 124 | + |
| 125 | +- Install a copy of Orion in a distinct folder of the regular Orion |
| 126 | +- Remove any `orion64-v1.0.nn` or `network.txt` existing file in that folder |
| 127 | +- Copy the `epoch-5-q.txt` file in the folder |
| 128 | +- Rename it to `network.txt` |
| 129 | +- Launch the copy of Orion: Orion will not find any `orion64-v1.0.nn` file, but will find a `network.txt` file, and then will convert it to a new `orion64-v1.0.nn` file |
| 130 | +- Rename `orion64-v1.0.nn` as you want |
| 131 | + |
| 132 | +Note: this is not super user-friendly, and will be enhanced in a next version ;-) |
| 133 | + |
| 134 | +<br/> |
| 135 | + |
| 136 | +## How to configure name and author |
| 137 | + |
| 138 | +You can adjust the name and author of the trained networks: |
| 139 | + |
| 140 | +- Before training, by modifying the `NN_NAME` (default = "Orion 1.0") and `NN_AUTHOR` (default = "David Carteau") variables in the script `train.py` located in the folder `./2. training/` (see lines 59 and 60) |
| 141 | +- After training, by modifying the first two lines of the generated networks (default = "name=Orion 1.0" and "author=David Carteau") |
| 142 | + |
| 143 | +<br/> |
| 144 | + |
| 145 | +## How to replicate Orion 1.0's neural network |
| 146 | + |
| 147 | +If you want to obtain the exact same neural network used in Orion 1.0, additional steps are required: |
| 148 | + |
| 149 | +- Download the [3, 4, 5 pieces](http://tablebase.sesse.net/syzygy/3-4-5/) endgame **Syzygy tablebases** and put them in the folder `./1. data preparation/syzygy/3-4-5/` |
| 150 | +- Download the [6 pieces](http://tablebase.sesse.net/syzygy/6-WDL/) endgame Syzygy tablebases and put them in the folder `./1. data preparation/syzygy/6-pieces/` |
| 151 | +- Download [CCRL 40/4 archive](https://computerchess.org.uk/ccrl/402.archive/games.html) + [CCRL BLITZ](https://computerchess.org.uk/ccrl/404/games.html) + [CCRL 40/15](https://computerchess.org.uk/ccrl/4040/games.html) games, unzip the 3 files to the folder `./1. data preparation/pgn/` |
| 152 | + |
| 153 | +<br/> |
26 | 154 |
|
27 | 155 | You will also need to install these specific versions of Python librairies: |
28 | 156 |
|
29 | | - `torch==2.1.2 --index-url https://download.pytorch.org/whl/cpu` (or `torch==2.1.2 --index-url https://download.pytorch.org/whl/cu121` if you have a **NVIDA GPU**) |
30 | | - `numpy==1.26.3` |
31 | | - `scipy==1.11.4` |
32 | | - `tqdm==4.66.1` |
33 | | - `chess==1.10.0` |
| 157 | +- `torch==2.1.2 --index-url https://download.pytorch.org/whl/cpu` |
| 158 | +- or, if you have an **NVIDIA GPU** `torch==2.1.2 --index-url https://download.pytorch.org/whl/cu121` |
| 159 | +- `numpy==1.26.3` |
| 160 | +- `scipy==1.11.4` |
| 161 | +- `tqdm==4.66.1` |
| 162 | +- `chess==1.10.0` |
34 | 163 |
|
35 | | -Note that, by default, training is forced to use the CPU. If you have a NVIDIA GPU, be sure to install the correct version of the **torch** library and set `FORCE_CPU_DEVICE` to `False` in the script `train.py` (line 53). |
| 164 | +<br/> |
36 | 165 |
|
37 | | -Feel free to adapt the library to meet your specific needs! 🌟 |
| 166 | +Important: to obtain the exact same neural network, let the `FORCE_CPU_DEVICE` variable set to `True` in the script `train.py` (line 53). |
| 167 | + |
| 168 | +<br/> |
38 | 169 |
|
39 | 170 | ## Copyright, license |
40 | 171 |
|
| 172 | +Feel free to adapt the library to meet your specific needs, and do not hesitate to provide feedback ! 🌟 |
| 173 | + |
| 174 | +<br/> |
| 175 | + |
41 | 176 | Copyright 2024 by David Carteau. All rights reserved. |
42 | 177 |
|
43 | 178 | The Cerebrum library is licensed under the **MIT License** (see "LICENSE" and "/v1.0/license.txt" files). |
0 commit comments