Skip to content

Commit b9998f7

Browse files
Disservinwindfishballad
authored andcommitted
Cleanup Evalfile handling
This cleans up the EvalFile handling after the merge of official-stockfish#4915, which has become a bit confusing on what it is actually doing. closes official-stockfish#4971 No functional change
1 parent f47e64a commit b9998f7

File tree

4 files changed

+49
-32
lines changed

4 files changed

+49
-32
lines changed

src/evaluate.cpp

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
#include <cmath>
2424
#include <cstdlib>
2525
#include <fstream>
26-
#include <initializer_list>
2726
#include <iomanip>
2827
#include <iostream>
2928
#include <sstream>
29+
#include <unordered_map>
3030
#include <vector>
3131

3232
#include "incbin/incbin.h"
@@ -62,9 +62,10 @@ namespace Stockfish {
6262

6363
namespace Eval {
6464

65-
std::string currentEvalFileName[2] = {"None", "None"};
66-
const std::string EvFiles[2] = {"EvalFile", "EvalFileSmall"};
67-
const std::string EvFileNames[2] = {EvalFileDefaultNameBig, EvalFileDefaultNameSmall};
65+
std::unordered_map<NNUE::NetSize, EvalFile> EvalFiles = {
66+
{NNUE::Big, {"EvalFile", EvalFileDefaultNameBig, "None"}},
67+
{NNUE::Small, {"EvalFileSmall", EvalFileDefaultNameSmall, "None"}}};
68+
6869

6970
// Tries to load a NNUE network at startup time, or when the engine
7071
// receives a UCI command "setoption name EvalFile value nn-[a-z0-9]{12}.nnue"
@@ -75,13 +76,16 @@ const std::string EvFileNames[2] = {EvalFileDefaultNameBig, EvalFileDefa
7576
// variable to have the engine search in a special directory in their distro.
7677
void NNUE::init() {
7778

78-
for (NetSize netSize : {Big, Small})
79+
for (auto& [netSize, evalFile] : EvalFiles)
7980
{
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];
81+
// Replace with
82+
// Options[evalFile.option_name]
83+
// once fishtest supports the uci option EvalFileSmall
84+
std::string user_eval_file =
85+
netSize == Small ? evalFile.default_name : Options[evalFile.option_name];
86+
87+
if (user_eval_file.empty())
88+
user_eval_file = evalFile.default_name;
8589

8690
#if defined(DEFAULT_NNUE_DIRECTORY)
8791
std::vector<std::string> dirs = {"<internal>", "", CommandLine::binaryDirectory,
@@ -92,16 +96,16 @@ void NNUE::init() {
9296

9397
for (const std::string& directory : dirs)
9498
{
95-
if (currentEvalFileName[netSize] != eval_file)
99+
if (evalFile.selected_name != user_eval_file)
96100
{
97101
if (directory != "<internal>")
98102
{
99-
std::ifstream stream(directory + eval_file, std::ios::binary);
100-
if (NNUE::load_eval(eval_file, stream, netSize))
101-
currentEvalFileName[netSize] = eval_file;
103+
std::ifstream stream(directory + user_eval_file, std::ios::binary);
104+
if (NNUE::load_eval(user_eval_file, stream, netSize))
105+
evalFile.selected_name = user_eval_file;
102106
}
103107

104-
if (directory == "<internal>" && eval_file == EvFileNames[netSize])
108+
if (directory == "<internal>" && user_eval_file == evalFile.default_name)
105109
{
106110
// C++ way to prepare a buffer for a memory stream
107111
class MemoryBuffer: public std::basic_streambuf<char> {
@@ -120,8 +124,8 @@ void NNUE::init() {
120124
(void) gEmbeddedNNUESmallEnd;
121125

122126
std::istream stream(&buffer);
123-
if (NNUE::load_eval(eval_file, stream, netSize))
124-
currentEvalFileName[netSize] = eval_file;
127+
if (NNUE::load_eval(user_eval_file, stream, netSize))
128+
evalFile.selected_name = user_eval_file;
125129
}
126130
}
127131
}
@@ -131,24 +135,27 @@ void NNUE::init() {
131135
// Verifies that the last net used was loaded successfully
132136
void NNUE::verify() {
133137

134-
for (NetSize netSize : {Big, Small})
138+
for (const auto& [netSize, evalFile] : EvalFiles)
135139
{
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];
141-
142-
if (currentEvalFileName[netSize] != eval_file)
140+
// Replace with
141+
// Options[evalFile.option_name]
142+
// once fishtest supports the uci option EvalFileSmall
143+
std::string user_eval_file =
144+
netSize == Small ? evalFile.default_name : Options[evalFile.option_name];
145+
if (user_eval_file.empty())
146+
user_eval_file = evalFile.default_name;
147+
148+
if (evalFile.selected_name != user_eval_file)
143149
{
144150
std::string msg1 =
145151
"Network evaluation parameters compatible with the engine must be available.";
146-
std::string msg2 = "The network file " + eval_file + " was not loaded successfully.";
152+
std::string msg2 =
153+
"The network file " + user_eval_file + " was not loaded successfully.";
147154
std::string msg3 = "The UCI option EvalFile might need to specify the full path, "
148155
"including the directory name, to the network file.";
149156
std::string msg4 = "The default net can be downloaded from: "
150157
"https://tests.stockfishchess.org/api/nn/"
151-
+ std::string(EvFileNames[netSize]);
158+
+ evalFile.default_name;
152159
std::string msg5 = "The engine will be terminated now.";
153160

154161
sync_cout << "info string ERROR: " << msg1 << sync_endl;
@@ -160,7 +167,7 @@ void NNUE::verify() {
160167
exit(EXIT_FAILURE);
161168
}
162169

163-
sync_cout << "info string NNUE evaluation using " << eval_file << sync_endl;
170+
sync_cout << "info string NNUE evaluation using " << user_eval_file << sync_endl;
164171
}
165172
}
166173
}

src/evaluate.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define EVALUATE_H_INCLUDED
2121

2222
#include <string>
23+
#include <unordered_map>
2324

2425
#include "types.h"
2526

@@ -34,8 +35,6 @@ std::string trace(Position& pos);
3435
int simple_eval(const Position& pos, Color c);
3536
Value evaluate(const Position& pos);
3637

37-
extern std::string currentEvalFileName[2];
38-
3938
// The default net name MUST follow the format nn-[SHA256 first 12 digits].nnue
4039
// for the build process (profile-build and fishtest) to work. Do not change the
4140
// name of the macro, as it is used in the Makefile.
@@ -44,11 +43,21 @@ extern std::string currentEvalFileName[2];
4443

4544
namespace NNUE {
4645

46+
enum NetSize : int;
47+
4748
void init();
4849
void verify();
4950

5051
} // namespace NNUE
5152

53+
struct EvalFile {
54+
std::string option_name;
55+
std::string default_name;
56+
std::string selected_name;
57+
};
58+
59+
extern std::unordered_map<NNUE::NetSize, EvalFile> EvalFiles;
60+
5261
} // namespace Eval
5362

5463
} // namespace Stockfish

src/nnue/evaluate_nnue.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <iostream>
2929
#include <sstream>
3030
#include <string_view>
31+
#include <unordered_map>
3132

3233
#include "../evaluate.h"
3334
#include "../misc.h"
@@ -449,7 +450,7 @@ bool save_eval(const std::optional<std::string>& filename, NetSize netSize) {
449450
actualFilename = filename.value();
450451
else
451452
{
452-
if (currentEvalFileName[netSize]
453+
if (EvalFiles.at(netSize).selected_name
453454
!= (netSize == Small ? EvalFileDefaultNameSmall : EvalFileDefaultNameBig))
454455
{
455456
msg = "Failed to export a net. "

src/nnue/nnue_architecture.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace Stockfish::Eval::NNUE {
3737
// Input features used in evaluation function
3838
using FeatureSet = Features::HalfKAv2_hm;
3939

40-
enum NetSize {
40+
enum NetSize : int {
4141
Big,
4242
Small
4343
};

0 commit comments

Comments
 (0)