Skip to content

Commit 584d9ef

Browse files
linrockmstembera
authored andcommitted
Dual NNUE with L1-128 smallnet
Credit goes to @mstembera for: - writing the code enabling dual NNUE: #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 closes https://github.com/official-stockfish/Stockfish/pulls Bench: 1330050 Co-Authored-By: mstembera <5421953+mstembera@users.noreply.github.com>
1 parent a5a76a6 commit 584d9ef

File tree

12 files changed

+297
-198
lines changed

12 files changed

+297
-198
lines changed

src/Makefile

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ help:
806806
@echo "help > Display architecture details"
807807
@echo "profile-build > standard build with profile-guided optimization"
808808
@echo "build > skip profile-guided optimization"
809-
@echo "net > Download the default nnue net"
809+
@echo "net > Download the default nnue nets"
810810
@echo "strip > Strip executable"
811811
@echo "install > Install executable"
812812
@echo "clean > Clean up"
@@ -922,16 +922,7 @@ profileclean:
922922
@rm -f stockfish.res
923923
@rm -f ./-lstdc++.res
924924

925-
# set up shell variables for the net stuff
926-
netvariables:
927-
$(eval nnuenet := $(shell grep EvalFileDefaultName evaluate.h | grep define | sed 's/.*\(nn-[a-z0-9]\{12\}.nnue\).*/\1/'))
928-
$(eval nnuedownloadurl1 := https://tests.stockfishchess.org/api/nn/$(nnuenet))
929-
$(eval nnuedownloadurl2 := https://github.com/official-stockfish/networks/raw/master/$(nnuenet))
930-
$(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))
931-
$(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))
932-
933-
# evaluation network (nnue)
934-
net: netvariables
925+
define fetch_network
935926
@echo "Default net: $(nnuenet)"
936927
@if [ "x$(curl_or_wget)" = "x" ]; then \
937928
echo "Neither curl nor wget is installed. Install one of these tools unless the net has been downloaded manually"; \
@@ -966,7 +957,24 @@ net: netvariables
966957
if [ "$(nnuenet)" = "nn-"`$(shasum_command) $(nnuenet) | cut -c1-12`".nnue" ]; then \
967958
echo "Network validated"; break; \
968959
fi; \
969-
fi; \
960+
fi;
961+
endef
962+
963+
# set up shell variables for the net stuff
964+
define netvariables
965+
$(eval nnuenet := $(shell grep $(1) evaluate.h | grep define | sed 's/.*\(nn-[a-z0-9]\{12\}.nnue\).*/\1/'))
966+
$(eval nnuedownloadurl1 := https://tests.stockfishchess.org/api/nn/$(nnuenet))
967+
$(eval nnuedownloadurl2 := https://github.com/official-stockfish/networks/raw/master/$(nnuenet))
968+
$(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))
969+
$(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))
970+
endef
971+
972+
# evaluation network (nnue)
973+
net:
974+
$(call netvariables, EvalFileDefaultNameBig)
975+
$(call fetch_network)
976+
$(call netvariables, EvalFileDefaultNameSmall)
977+
$(call fetch_network)
970978

971979
format:
972980
$(CLANG-FORMAT) -i $(SRCS) $(HEADERS) -style=file

src/evaluate.cpp

Lines changed: 92 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,96 @@ 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+
// change after fishtest supports EvalFileSmall
81+
std::string eval_file =
82+
std::string(netSize == Small ? EvalFileDefaultNameSmall : Options[EvFiles[netSize]]);
83+
if (eval_file.empty())
84+
eval_file = EvFileNames[netSize];
7385

7486
#if defined(DEFAULT_NNUE_DIRECTORY)
75-
std::vector<std::string> dirs = {"<internal>", "", CommandLine::binaryDirectory,
76-
stringify(DEFAULT_NNUE_DIRECTORY)};
87+
std::vector<std::string> dirs = {"<internal>", "", CommandLine::binaryDirectory,
88+
stringify(DEFAULT_NNUE_DIRECTORY)};
7789
#else
78-
std::vector<std::string> dirs = {"<internal>", "", CommandLine::binaryDirectory};
90+
std::vector<std::string> dirs = {"<internal>", "", CommandLine::binaryDirectory};
7991
#endif
8092

81-
for (const std::string& directory : dirs)
82-
if (currentEvalFileName != eval_file)
93+
for (const std::string& directory : dirs)
8394
{
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)
95+
if (currentEvalFileName[netSize] != eval_file)
9296
{
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;
97+
if (directory != "<internal>")
98+
{
99+
std::ifstream stream(directory + eval_file, std::ios::binary);
100+
if (NNUE::load_eval(eval_file, stream, netSize))
101+
currentEvalFileName[netSize] = eval_file;
102+
}
103+
104+
if (directory == "<internal>" && eval_file == EvFileNames[netSize])
105+
{
106+
// C++ way to prepare a buffer for a memory stream
107+
class MemoryBuffer: public std::basic_streambuf<char> {
108+
public:
109+
MemoryBuffer(char* p, size_t n) {
110+
setg(p, p, p + n);
111+
setp(p, p + n);
112+
}
113+
};
114+
115+
MemoryBuffer buffer(
116+
const_cast<char*>(reinterpret_cast<const char*>(
117+
netSize == Small ? gEmbeddedNNUESmallData : gEmbeddedNNUEBigData)),
118+
size_t(netSize == Small ? gEmbeddedNNUESmallSize : gEmbeddedNNUEBigSize));
119+
(void) gEmbeddedNNUEBigEnd; // Silence warning on unused variable
120+
(void) gEmbeddedNNUESmallEnd;
121+
122+
std::istream stream(&buffer);
123+
if (NNUE::load_eval(eval_file, stream, netSize))
124+
currentEvalFileName[netSize] = eval_file;
125+
}
110126
}
111127
}
128+
}
112129
}
113130

114131
// Verifies that the last net used was loaded successfully
115132
void NNUE::verify() {
116133

117-
std::string eval_file = std::string(Options["EvalFile"]);
118-
if (eval_file.empty())
119-
eval_file = EvalFileDefaultName;
120-
121-
if (currentEvalFileName != eval_file)
134+
for (NetSize netSize : {Big, Small})
122135
{
136+
// change after fishtest supports EvalFileSmall
137+
std::string eval_file =
138+
std::string(netSize == Small ? EvalFileDefaultNameSmall : Options[EvFiles[netSize]]);
139+
if (eval_file.empty())
140+
eval_file = EvFileNames[netSize];
123141

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-
}
142+
if (currentEvalFileName[netSize] != eval_file)
143+
{
144+
std::string msg1 =
145+
"Network evaluation parameters compatible with the engine must be available.";
146+
std::string msg2 = "The network file " + eval_file + " was not loaded successfully.";
147+
std::string msg3 = "The UCI option EvalFile might need to specify the full path, "
148+
"including the directory name, to the network file.";
149+
std::string msg4 = "The default net can be downloaded from: "
150+
"https://tests.stockfishchess.org/api/nn/"
151+
+ std::string(EvFileNames[netSize]);
152+
std::string msg5 = "The engine will be terminated now.";
153+
154+
sync_cout << "info string ERROR: " << msg1 << sync_endl;
155+
sync_cout << "info string ERROR: " << msg2 << sync_endl;
156+
sync_cout << "info string ERROR: " << msg3 << sync_endl;
157+
sync_cout << "info string ERROR: " << msg4 << sync_endl;
158+
sync_cout << "info string ERROR: " << msg5 << sync_endl;
159+
160+
exit(EXIT_FAILURE);
161+
}
142162

143-
sync_cout << "info string NNUE evaluation using " << eval_file << sync_endl;
163+
sync_cout << "info string NNUE evaluation using " << eval_file << sync_endl;
164+
}
144165
}
145166
}
146167

147-
148168
// Returns a static, purely materialistic evaluation of the position from
149169
// the point of view of the given color. It can be divided by PawnValue to get
150170
// an approximation of the material advantage on the board in terms of pawns.
@@ -163,18 +183,19 @@ Value Eval::evaluate(const Position& pos) {
163183
int v;
164184
Color stm = pos.side_to_move();
165185
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);
186+
int simpleEval = simple_eval(pos, stm);
171187

188+
bool lazy = std::abs(simpleEval) > 2300;
172189
if (lazy)
173190
v = simpleEval;
174191
else
175192
{
176-
int nnueComplexity;
177-
Value nnue = NNUE::evaluate(pos, true, &nnueComplexity);
193+
bool smallNet = std::abs(simpleEval) > 1100;
194+
195+
int nnueComplexity;
196+
197+
Value nnue = smallNet ? NNUE::evaluate<NNUE::Small>(pos, true, &nnueComplexity)
198+
: NNUE::evaluate<NNUE::Big>(pos, true, &nnueComplexity);
178199

179200
int optimism = pos.this_thread()->optimism[stm];
180201

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

219240
Value v;
220-
v = NNUE::evaluate(pos, false);
241+
v = NNUE::evaluate<NNUE::Big>(pos, false);
221242
v = pos.side_to_move() == WHITE ? v : -v;
222243
ss << "NNUE evaluation " << 0.01 * UCI::to_cp(v) << " (white side)\n";
223244

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
int 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-b1e55edbea57.nnue"
42+
#define EvalFileDefaultNameBig "nn-b1e55edbea57.nnue"
43+
#define EvalFileDefaultNameSmall "nn-c01dc0ffeede.nnue"
4344

4445
namespace NNUE {
4546

0 commit comments

Comments
 (0)