Skip to content

Commit f23343d

Browse files
linrockmstembera
authored andcommitted
Dual NNUE with L1-128 smallnet
Credit goes to @mstembera for: - writing the code enabling dual NNUE: official-stockfish#4898 - the idea of trying L1-128 trained exclusively on high simple eval positions The L1-128 smallnet is: - epoch 399 of a single-stage training from scratch - trained only on positions from filtered data with high material difference - defined by abs(simple_eval) > 1000 ```yaml experiment-name: 128--S1-only-hse-v2 training-dataset: - /data/hse/S3/dfrc99-16tb7p-eval-filt-v2.min.high-simple-eval-1k.binpack - /data/hse/S3/leela96-filt-v2.min.high-simple-eval-1k.binpack - /data/hse/S3/test80-apr2022-16tb7p.min.high-simple-eval-1k.binpack - /data/hse/S7/test60-2020-2tb7p.v6-3072.high-simple-eval-1k.binpack - /data/hse/S7/test60-novdec2021-12tb7p-filter-v6-dd.min-mar2023.unmin.high-simple-eval-1k.binpack - /data/hse/S7/test77-nov2021-2tb7p.v6-3072.min.high-simple-eval-1k.binpack - /data/hse/S7/test77-dec2021-16tb7p-filter-v6-dd.min-mar2023.unmin.high-simple-eval-1k.binpack - /data/hse/S7/test77-jan2022-2tb7p.high-simple-eval-1k.binpack - /data/hse/S7/test78-jantomay2022-16tb7p-filter-v6-dd.min-mar2023.unmin.high-simple-eval-1k.binpack - /data/hse/S7/test78-juntosep2022-16tb7p-filter-v6-dd.min-mar2023.unmin.high-simple-eval-1k.binpack - /data/hse/S7/test79-apr2022-16tb7p-filter-v6-dd.min-mar2023.unmin.high-simple-eval-1k.binpack - /data/hse/S7/test79-may2022-16tb7p-filter-v6-dd.min-mar2023.unmin.high-simple-eval-1k.binpack # T80 2022 - /data/hse/S7/test80-may2022-16tb7p.high-simple-eval-1k.binpack - /data/hse/S7/test80-jun2022-16tb7p-filter-v6-dd.min-mar2023.unmin.high-simple-eval-1k.binpack - /data/hse/S7/test80-jul2022-16tb7p.v6-dd.min.high-simple-eval-1k.binpack - /data/hse/S7/test80-aug2022-16tb7p-filter-v6-dd.min-mar2023.unmin.high-simple-eval-1k.binpack - /data/hse/S7/test80-sep2022-16tb7p-filter-v6-dd.min-mar2023.unmin.high-simple-eval-1k.binpack - /data/hse/S7/test80-oct2022-16tb7p.v6-dd.high-simple-eval-1k.binpack - /data/hse/S7/test80-nov2022-16tb7p-v6-dd.min.high-simple-eval-1k.binpack # T80 2023 - /data/hse/S7/test80-jan2023-3of3-16tb7p-filter-v6-dd.min-mar2023.unmin.high-simple-eval-1k.binpack - /data/hse/S7/test80-feb2023-16tb7p-filter-v6-dd.min-mar2023.unmin.high-simple-eval-1k.binpack - /data/hse/S7/test80-mar2023-2tb7p.v6-sk16.min.high-simple-eval-1k.binpack - /data/hse/S7/test80-apr2023-2tb7p-filter-v6-sk16.min.high-simple-eval-1k.binpack - /data/hse/S7/test80-may2023-2tb7p.v6.min.high-simple-eval-1k.binpack - /data/hse/S7/test80-jun2023-2tb7p.v6-3072.min.high-simple-eval-1k.binpack - /data/hse/S7/test80-jul2023-2tb7p.v6-3072.min.high-simple-eval-1k.binpack - /data/hse/S7/test80-aug2023-2tb7p.v6.min.high-simple-eval-1k.binpack - /data/hse/S7/test80-sep2023-2tb7p.high-simple-eval-1k.binpack - /data/hse/S7/test80-oct2023-2tb7p.high-simple-eval-1k.binpack start-from-engine-test-net: False nnue-pytorch-branch: linrock/nnue-pytorch/L1-128 engine-test-branch: linrock/Stockfish/L1-128-nolazy engine-base-branch: linrock/Stockfish/L1-128 num-epochs: 500 lambda: 1.0 ``` Experiment yaml configs converted to easy_train.sh commands with: https://github.com/linrock/nnue-tools/blob/4339954/yaml_easy_train.py Binpacks interleaved at training time with: official-stockfish/nnue-pytorch#259 Data filtered for high simple eval positions with: https://github.com/linrock/nnue-data/blob/32d6a68/filter_high_simple_eval_plain.py https://github.com/linrock/Stockfish/blob/61dbfe/src/tools/transform.cpp#L626-L655 Training data can be found at: https://robotmoon.com/nnue-training-data/ Local elo at 25k nodes per move of L1-128 smallnet (nnue-only eval) vs. L1-128 trained on standard S1 data: nn-epoch399.nnue : -318.1 +/- 2.1 Passed STC: https://tests.stockfishchess.org/tests/view/6574cb9d95ea6ba1fcd49e3b LLR: 2.93 (-2.94,2.94) <0.00,2.00> Total: 62432 W: 15875 L: 15521 D: 31036 Ptnml(0-2): 177, 7331, 15872, 7633, 203 Passed LTC: https://tests.stockfishchess.org/tests/view/6575da2d4d789acf40aaac6e LLR: 2.94 (-2.94,2.94) <0.50,2.50> Total: 64830 W: 16118 L: 15738 D: 32974 Ptnml(0-2): 43, 7129, 17697, 7497, 49 bench 1485866 Co-authored-by: mstembera <m_stembera@yahoo.com>
1 parent 9be0360 commit f23343d

File tree

12 files changed

+292
-198
lines changed

12 files changed

+292
-198
lines changed

src/Makefile

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ help:
798798
@echo "help > Display architecture details"
799799
@echo "profile-build > standard build with profile-guided optimization"
800800
@echo "build > skip profile-guided optimization"
801-
@echo "net > Download the default nnue net"
801+
@echo "net > Download the default nnue nets"
802802
@echo "strip > Strip executable"
803803
@echo "install > Install executable"
804804
@echo "clean > Clean up"
@@ -914,16 +914,7 @@ profileclean:
914914
@rm -f stockfish.res
915915
@rm -f ./-lstdc++.res
916916

917-
# set up shell variables for the net stuff
918-
netvariables:
919-
$(eval nnuenet := $(shell grep EvalFileDefaultName evaluate.h | grep define | sed 's/.*\(nn-[a-z0-9]\{12\}.nnue\).*/\1/'))
920-
$(eval nnuedownloadurl1 := https://tests.stockfishchess.org/api/nn/$(nnuenet))
921-
$(eval nnuedownloadurl2 := https://github.com/official-stockfish/networks/raw/master/$(nnuenet))
922-
$(eval curl_or_wget := $(shell if hash curl 2>/dev/null; then echo "curl -skL"; elif hash wget 2>/dev/null; then echo "wget -qO-"; fi))
923-
$(eval shasum_command := $(shell if hash shasum 2>/dev/null; then echo "shasum -a 256 "; elif hash sha256sum 2>/dev/null; then echo "sha256sum "; fi))
924-
925-
# evaluation network (nnue)
926-
net: netvariables
917+
define fetch_network
927918
@echo "Default net: $(nnuenet)"
928919
@if [ "x$(curl_or_wget)" = "x" ]; then \
929920
echo "Neither curl nor wget is installed. Install one of these tools unless the net has been downloaded manually"; \
@@ -958,7 +949,24 @@ net: netvariables
958949
if [ "$(nnuenet)" = "nn-"`$(shasum_command) $(nnuenet) | cut -c1-12`".nnue" ]; then \
959950
echo "Network validated"; break; \
960951
fi; \
961-
fi; \
952+
fi;
953+
endef
954+
955+
# set up shell variables for the net stuff
956+
define netvariables
957+
$(eval nnuenet := $(shell grep $(1) evaluate.h | grep define | sed 's/.*\(nn-[a-z0-9]\{12\}.nnue\).*/\1/'))
958+
$(eval nnuedownloadurl1 := https://tests.stockfishchess.org/api/nn/$(nnuenet))
959+
$(eval nnuedownloadurl2 := https://github.com/official-stockfish/networks/raw/master/$(nnuenet))
960+
$(eval curl_or_wget := $(shell if hash curl 2>/dev/null; then echo "curl -skL"; elif hash wget 2>/dev/null; then echo "wget -qO-"; fi))
961+
$(eval shasum_command := $(shell if hash shasum 2>/dev/null; then echo "shasum -a 256 "; elif hash sha256sum 2>/dev/null; then echo "sha256sum "; fi))
962+
endef
963+
964+
# evaluation network (nnue)
965+
net:
966+
$(call netvariables, EvalFileDefaultNameBig)
967+
$(call fetch_network)
968+
$(call netvariables, EvalFileDefaultNameSmall)
969+
$(call fetch_network)
962970

963971
format:
964972
$(CLANG-FORMAT) -i $(SRCS) $(HEADERS) -style=file

src/evaluate.cpp

Lines changed: 88 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <cmath>
2424
#include <cstdlib>
2525
#include <fstream>
26+
#include <initializer_list>
2627
#include <iomanip>
2728
#include <iostream>
2829
#include <sstream>
@@ -31,6 +32,7 @@
3132
#include "incbin/incbin.h"
3233
#include "misc.h"
3334
#include "nnue/evaluate_nnue.h"
35+
#include "nnue/nnue_architecture.h"
3436
#include "position.h"
3537
#include "thread.h"
3638
#include "types.h"
@@ -44,19 +46,25 @@
4446
// const unsigned int gEmbeddedNNUESize; // the size of the embedded file
4547
// Note that this does not work in Microsoft Visual Studio.
4648
#if !defined(_MSC_VER) && !defined(NNUE_EMBEDDING_OFF)
47-
INCBIN(EmbeddedNNUE, EvalFileDefaultName);
49+
INCBIN(EmbeddedNNUEBig, EvalFileDefaultNameBig);
50+
INCBIN(EmbeddedNNUESmall, EvalFileDefaultNameSmall);
4851
#else
49-
const unsigned char gEmbeddedNNUEData[1] = {0x0};
50-
const unsigned char* const gEmbeddedNNUEEnd = &gEmbeddedNNUEData[1];
51-
const unsigned int gEmbeddedNNUESize = 1;
52+
const unsigned char gEmbeddedNNUEBigData[1] = {0x0};
53+
const unsigned char* const gEmbeddedNNUEBigEnd = &gEmbeddedNNUEBigData[1];
54+
const unsigned int gEmbeddedNNUEBigSize = 1;
55+
const unsigned char gEmbeddedNNUESmallData[1] = {0x0};
56+
const unsigned char* const gEmbeddedNNUESmallEnd = &gEmbeddedNNUESmallData[1];
57+
const unsigned int gEmbeddedNNUESmallSize = 1;
5258
#endif
5359

5460

5561
namespace Stockfish {
5662

5763
namespace Eval {
5864

59-
std::string currentEvalFileName = "None";
65+
std::string currentEvalFileName[2] = {"None", "None"};
66+
const std::string EvFiles[2] = {"EvalFile", "EvalFileSmall"};
67+
const std::string EvFileNames[2] = {EvalFileDefaultNameBig, EvalFileDefaultNameSmall};
6068

6169
// Tries to load a NNUE network at startup time, or when the engine
6270
// receives a UCI command "setoption name EvalFile value nn-[a-z0-9]{12}.nnue"
@@ -67,84 +75,92 @@ std::string currentEvalFileName = "None";
6775
// variable to have the engine search in a special directory in their distro.
6876
void NNUE::init() {
6977

70-
std::string eval_file = std::string(Options["EvalFile"]);
71-
if (eval_file.empty())
72-
eval_file = EvalFileDefaultName;
78+
for (NetSize netSize : {Big, Small})
79+
{
80+
std::string eval_file = std::string(Options[EvFiles[netSize]]);
81+
if (eval_file.empty())
82+
eval_file = EvFileNames[netSize];
7383

7484
#if defined(DEFAULT_NNUE_DIRECTORY)
75-
std::vector<std::string> dirs = {"<internal>", "", CommandLine::binaryDirectory,
76-
stringify(DEFAULT_NNUE_DIRECTORY)};
85+
std::vector<std::string> dirs = {"<internal>", "", CommandLine::binaryDirectory,
86+
stringify(DEFAULT_NNUE_DIRECTORY)};
7787
#else
78-
std::vector<std::string> dirs = {"<internal>", "", CommandLine::binaryDirectory};
88+
std::vector<std::string> dirs = {"<internal>", "", CommandLine::binaryDirectory};
7989
#endif
8090

81-
for (const std::string& directory : dirs)
82-
if (currentEvalFileName != eval_file)
91+
for (const std::string& directory : dirs)
8392
{
84-
if (directory != "<internal>")
85-
{
86-
std::ifstream stream(directory + eval_file, std::ios::binary);
87-
if (NNUE::load_eval(eval_file, stream))
88-
currentEvalFileName = eval_file;
89-
}
90-
91-
if (directory == "<internal>" && eval_file == EvalFileDefaultName)
93+
if (currentEvalFileName[netSize] != eval_file)
9294
{
93-
// C++ way to prepare a buffer for a memory stream
94-
class MemoryBuffer: public std::basic_streambuf<char> {
95-
public:
96-
MemoryBuffer(char* p, size_t n) {
97-
setg(p, p, p + n);
98-
setp(p, p + n);
99-
}
100-
};
101-
102-
MemoryBuffer buffer(
103-
const_cast<char*>(reinterpret_cast<const char*>(gEmbeddedNNUEData)),
104-
size_t(gEmbeddedNNUESize));
105-
(void) gEmbeddedNNUEEnd; // Silence warning on unused variable
106-
107-
std::istream stream(&buffer);
108-
if (NNUE::load_eval(eval_file, stream))
109-
currentEvalFileName = eval_file;
95+
if (directory != "<internal>")
96+
{
97+
std::ifstream stream(directory + eval_file, std::ios::binary);
98+
if (NNUE::load_eval(eval_file, stream, netSize))
99+
currentEvalFileName[netSize] = eval_file;
100+
}
101+
102+
if (directory == "<internal>" && eval_file == EvFileNames[netSize])
103+
{
104+
// C++ way to prepare a buffer for a memory stream
105+
class MemoryBuffer: public std::basic_streambuf<char> {
106+
public:
107+
MemoryBuffer(char* p, size_t n) {
108+
setg(p, p, p + n);
109+
setp(p, p + n);
110+
}
111+
};
112+
113+
MemoryBuffer buffer(
114+
const_cast<char*>(reinterpret_cast<const char*>(
115+
netSize == Small ? gEmbeddedNNUESmallData : gEmbeddedNNUEBigData)),
116+
size_t(netSize == Small ? gEmbeddedNNUESmallSize : gEmbeddedNNUEBigSize));
117+
(void) gEmbeddedNNUEBigEnd; // Silence warning on unused variable
118+
(void) gEmbeddedNNUESmallEnd;
119+
120+
std::istream stream(&buffer);
121+
if (NNUE::load_eval(eval_file, stream, netSize))
122+
currentEvalFileName[netSize] = eval_file;
123+
}
110124
}
111125
}
126+
}
112127
}
113128

114129
// Verifies that the last net used was loaded successfully
115130
void NNUE::verify() {
116131

117-
std::string eval_file = std::string(Options["EvalFile"]);
118-
if (eval_file.empty())
119-
eval_file = EvalFileDefaultName;
120-
121-
if (currentEvalFileName != eval_file)
132+
for (NetSize netSize : {Big, Small})
122133
{
134+
std::string eval_file = std::string(Options[EvFiles[netSize]]);
135+
if (eval_file.empty())
136+
eval_file = EvFileNames[netSize];
123137

124-
std::string msg1 =
125-
"Network evaluation parameters compatible with the engine must be available.";
126-
std::string msg2 = "The network file " + eval_file + " was not loaded successfully.";
127-
std::string msg3 = "The UCI option EvalFile might need to specify the full path, "
128-
"including the directory name, to the network file.";
129-
std::string msg4 = "The default net can be downloaded from: "
130-
"https://tests.stockfishchess.org/api/nn/"
131-
+ std::string(EvalFileDefaultName);
132-
std::string msg5 = "The engine will be terminated now.";
133-
134-
sync_cout << "info string ERROR: " << msg1 << sync_endl;
135-
sync_cout << "info string ERROR: " << msg2 << sync_endl;
136-
sync_cout << "info string ERROR: " << msg3 << sync_endl;
137-
sync_cout << "info string ERROR: " << msg4 << sync_endl;
138-
sync_cout << "info string ERROR: " << msg5 << sync_endl;
139-
140-
exit(EXIT_FAILURE);
141-
}
138+
if (currentEvalFileName[netSize] != eval_file)
139+
{
140+
std::string msg1 =
141+
"Network evaluation parameters compatible with the engine must be available.";
142+
std::string msg2 = "The network file " + eval_file + " was not loaded successfully.";
143+
std::string msg3 = "The UCI option EvalFile might need to specify the full path, "
144+
"including the directory name, to the network file.";
145+
std::string msg4 = "The default net can be downloaded from: "
146+
"https://tests.stockfishchess.org/api/nn/"
147+
+ std::string(EvFileNames[netSize]);
148+
std::string msg5 = "The engine will be terminated now.";
149+
150+
sync_cout << "info string ERROR: " << msg1 << sync_endl;
151+
sync_cout << "info string ERROR: " << msg2 << sync_endl;
152+
sync_cout << "info string ERROR: " << msg3 << sync_endl;
153+
sync_cout << "info string ERROR: " << msg4 << sync_endl;
154+
sync_cout << "info string ERROR: " << msg5 << sync_endl;
155+
156+
exit(EXIT_FAILURE);
157+
}
142158

143-
sync_cout << "info string NNUE evaluation using " << eval_file << sync_endl;
159+
sync_cout << "info string NNUE evaluation using " << eval_file << sync_endl;
160+
}
144161
}
145162
}
146163

147-
148164
// Returns a static, purely materialistic evaluation of the position from
149165
// the point of view of the given color. It can be divided by PawnValue to get
150166
// an approximation of the material advantage on the board in terms of pawns.
@@ -163,18 +179,19 @@ Value Eval::evaluate(const Position& pos) {
163179
Value v;
164180
Color stm = pos.side_to_move();
165181
int shuffling = pos.rule50_count();
166-
int simpleEval = simple_eval(pos, stm) + (int(pos.key() & 7) - 3);
167-
168-
bool lazy = std::abs(simpleEval) >= RookValue + KnightValue + 16 * shuffling * shuffling
169-
+ std::abs(pos.this_thread()->bestValue)
170-
+ std::abs(pos.this_thread()->rootSimpleEval);
182+
int simpleEval = simple_eval(pos, stm);
171183

184+
bool lazy = std::abs(simpleEval) > 2300;
172185
if (lazy)
173186
v = Value(simpleEval);
174187
else
175188
{
176-
int nnueComplexity;
177-
Value nnue = NNUE::evaluate(pos, true, &nnueComplexity);
189+
bool smallNet = std::abs(simpleEval) > 1100;
190+
191+
int nnueComplexity;
192+
193+
Value nnue = smallNet ? NNUE::evaluate<NNUE::Small>(pos, true, &nnueComplexity)
194+
: NNUE::evaluate<NNUE::Big>(pos, true, &nnueComplexity);
178195

179196
Value optimism = pos.this_thread()->optimism[stm];
180197

@@ -217,7 +234,7 @@ std::string Eval::trace(Position& pos) {
217234
ss << std::showpoint << std::showpos << std::fixed << std::setprecision(2) << std::setw(15);
218235

219236
Value v;
220-
v = NNUE::evaluate(pos, false);
237+
v = NNUE::evaluate<NNUE::Big>(pos, false);
221238
v = pos.side_to_move() == WHITE ? v : -v;
222239
ss << "NNUE evaluation " << 0.01 * UCI::to_cp(v) << " (white side)\n";
223240

src/evaluate.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ std::string trace(Position& pos);
3434
Value simple_eval(const Position& pos, Color c);
3535
Value evaluate(const Position& pos);
3636

37-
extern std::string currentEvalFileName;
37+
extern std::string currentEvalFileName[2];
3838

3939
// The default net name MUST follow the format nn-[SHA256 first 12 digits].nnue
4040
// for the build process (profile-build and fishtest) to work. Do not change the
4141
// name of the macro, as it is used in the Makefile.
42-
#define EvalFileDefaultName "nn-0000000000a0.nnue"
42+
#define EvalFileDefaultNameBig "nn-0000000000a0.nnue"
43+
#define EvalFileDefaultNameSmall "nn-c01dc0ffeede.nnue"
4344

4445
namespace NNUE {
4546

0 commit comments

Comments
 (0)