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
6363namespace 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.
7677void 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
132136void 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}
0 commit comments