You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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/).
5
+
The **Cerebrum library** can be used to easily train a **first** "[NNUE](https://www.chessprogramming.org/NNUE)-like" neural network for a chess engine. It was originally designed and built for the [Orion UCI chess engine](https://www.orionchess.com/).
6
+
7
+
Its originality lies in using only game results, parsed from pgn files provided by the user, and material values, computed on the fly, as targets for prediction. Both predicted values (game result and material) can be used for board evaluation, either separately or together, with coefficients that can be set at inference time.
8
+
9
+
Inference code is provided for embedding and using the trained network in a C/C++ or Python project, in two alternatives: standard (for accuracy) or quantized (for speed).
10
+
11
+
By default, since v1.1, the network architecture is `2x(768x256)x2`.
12
+
13
+
Do not hesitate to adapt the library to your own needs, and/or to use newer/better NNUE libraries for more flexibility/performance (e.g. [Bullet](https://github.com/jw1912/bullet/tree/main))!
14
+
15
+
<br/>
16
+
17
+
## Changes in 1.1
18
+
19
+
-**Less memory requirements** for data preparation
20
+
-**Small fixes** in training and inference code
21
+
-**Simpler network architecture**, down from `2x(768x128)x32x32x2` to `2x(768x256)x2`
22
+
-**Better UX** during training, with more information on progress
6
23
7
24
<br/>
8
25
9
26
## Changes in 1.0
10
27
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
28
+
- Training now relies on game results (from which a win ratio is deduced for each position during a game) and material only !
29
+
- Data preparation scripts are provided to automate the preparation of training data (using one or several pgn files)
30
+
- Network quantization is performed at the end of each training epoch, allowing the choice between better accuracy or increased inference speed
31
+
- A basic UCI chess engine is provided in two versions (standard or quantized) to demonstrate how to load and use the network
15
32
- Inference C code is now also available in two versions (standard or quantized)
16
33
17
34
<br/>
@@ -20,75 +37,47 @@ The **Cerebrum library** can be used to train and utilize "[NNUE](https://www.ch
20
37
21
38
The library consists of four main parts:
22
39
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)
40
+
1. Data preparation code (Python scripts)
41
+
2. Training code (Python script)
42
+
3. Inference code (C files)
43
+
4. A basic UCI chess engine for demonstration purposes (Python script)
27
44
28
45
<br/>
29
46
30
47
To use the library, you will first need to:
31
48
32
-
- Download the `v1.0` folder of this repository
49
+
- Download the `v1.1` folder of this repository
33
50
- Install a Python runtime: https://www.python.org/
- Install some Python librairies: `pip install torch tqdm chess`
35
52
- 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
53
37
54
<br/>
38
55
39
-
Note that, **by default, training is forced to use the CPU**. If you have an NVIDIA GPU, you can:
56
+
Optionally (for better results):
57
+
58
+
- 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/`
59
+
- 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/`
40
60
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
61
44
62
<br/>
45
63
46
64
## Usage (Windows)
47
65
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/`.
66
+
Prepare one or several pgn files containing full games and put it/them in the folder `./1. data preparation/pgn/`.
51
67
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.
68
+
Then launch the script `prepare.bat` in the folder `./1. data preparation/` to obtain a file named `positions.txt` which will be stored in the same folder.
53
69
54
70
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
71
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/`.
72
+
Copy the `positions.txt` file to the folder `./2. training/positions/`.
84
73
85
74
<br/>
86
75
87
76
### Training
88
77
89
78
Launch the script `train.bat` in the folder `./2. training/`.
90
79
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.
80
+
This script will parse the `positions.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
81
93
82
<br/>
94
83
@@ -114,70 +103,23 @@ These networks can now be used in your own engine, using your own code, or:
114
103
115
104
In order to use your own trained network with the provided Cerebrum UCI chess engine:
116
105
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/`
106
+
- 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
107
- Rename it to `network.txt`
119
108
- Launch the engine
120
109
121
110
<br/>
122
111
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
112
## How to configure name and author
137
113
138
114
You can adjust the name and author of the trained networks:
139
115
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/>
154
-
155
-
You will also need to use these specific versions of Python and Python librairies:
- or, if you have an **NVIDIA GPU**`torch==2.1.2 --index-url https://download.pytorch.org/whl/cu121`
160
-
-`numpy==1.26.3`
161
-
-`scipy==1.11.4`
162
-
-`tqdm==4.66.1`
163
-
-`chess==1.10.0`
164
-
165
-
<br/>
166
-
167
-
Important: to obtain the exact same neural network, let the `FORCE_CPU_DEVICE` variable set to `True` in the script `train.py` (line 53).
168
-
169
-
<br/>
170
-
171
-
Additionnal information can be found on the [Orion's website](https://www.orionchess.com/).
116
+
- Before training, by modifying the `NN_NAME` (default = "Cerebrum 1.1") and `NN_AUTHOR` (default = "David Carteau") variables in the script `train.py` located in the folder `./2. training/`
117
+
- After training, by modifying the first two lines of the generated networks (default = "name=Cerebrum 1.1" and "author=David Carteau")
172
118
173
119
<br/>
174
120
175
121
## Copyright, license
176
122
177
-
Feel free to adapt the library to meet your specific needs, and do not hesitate to provide feedback ! 🌟
178
-
179
-
<br/>
180
-
181
-
The Cerebrum library is licensed under the **MIT License** (see "LICENSE" and "/v1.0/license.txt" files).
123
+
Copyright 2025 by David Carteau. All rights reserved.
182
124
183
-
Copyright 2024 by David Carteau. All rights reserved.
125
+
The Cerebrum library is licensed under the **MIT License** (see "LICENSE" and "/v1.1/license.txt" files).
0 commit comments