1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 00:33:09 +00:00

Cleanup Evalfile handling

This cleans up the EvalFile handling after the merge of #4915,
which has become a bit confusing on what it is actually doing.

closes https://github.com/official-stockfish/Stockfish/pull/4971

No functional change
This commit is contained in:
Disservin 2024-01-07 23:08:33 +01:00
parent 7c5e3f2865
commit 99cdb920fc
4 changed files with 48 additions and 31 deletions

View file

@ -23,10 +23,10 @@
#include <cmath> #include <cmath>
#include <cstdlib> #include <cstdlib>
#include <fstream> #include <fstream>
#include <initializer_list>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <unordered_map>
#include <vector> #include <vector>
#include "incbin/incbin.h" #include "incbin/incbin.h"
@ -62,9 +62,10 @@ namespace Stockfish {
namespace Eval { namespace Eval {
std::string currentEvalFileName[2] = {"None", "None"}; std::unordered_map<NNUE::NetSize, EvalFile> EvalFiles = {
const std::string EvFiles[2] = {"EvalFile", "EvalFileSmall"}; {NNUE::Big, {"EvalFile", EvalFileDefaultNameBig, "None"}},
const std::string EvFileNames[2] = {EvalFileDefaultNameBig, EvalFileDefaultNameSmall}; {NNUE::Small, {"EvalFileSmall", EvalFileDefaultNameSmall, "None"}}};
// Tries to load a NNUE network at startup time, or when the engine // Tries to load a NNUE network at startup time, or when the engine
// receives a UCI command "setoption name EvalFile value nn-[a-z0-9]{12}.nnue" // 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
// variable to have the engine search in a special directory in their distro. // variable to have the engine search in a special directory in their distro.
void NNUE::init() { void NNUE::init() {
for (NetSize netSize : {Big, Small}) for (auto& [netSize, evalFile] : EvalFiles)
{ {
// change after fishtest supports EvalFileSmall // Replace with
std::string eval_file = // Options[evalFile.option_name]
std::string(netSize == Small ? EvalFileDefaultNameSmall : Options[EvFiles[netSize]]); // once fishtest supports the uci option EvalFileSmall
if (eval_file.empty()) std::string user_eval_file =
eval_file = EvFileNames[netSize]; netSize == Small ? evalFile.default_name : Options[evalFile.option_name];
if (user_eval_file.empty())
user_eval_file = evalFile.default_name;
#if defined(DEFAULT_NNUE_DIRECTORY) #if defined(DEFAULT_NNUE_DIRECTORY)
std::vector<std::string> dirs = {"<internal>", "", CommandLine::binaryDirectory, std::vector<std::string> dirs = {"<internal>", "", CommandLine::binaryDirectory,
@ -92,16 +96,16 @@ void NNUE::init() {
for (const std::string& directory : dirs) for (const std::string& directory : dirs)
{ {
if (currentEvalFileName[netSize] != eval_file) if (evalFile.selected_name != user_eval_file)
{ {
if (directory != "<internal>") if (directory != "<internal>")
{ {
std::ifstream stream(directory + eval_file, std::ios::binary); std::ifstream stream(directory + user_eval_file, std::ios::binary);
if (NNUE::load_eval(eval_file, stream, netSize)) if (NNUE::load_eval(user_eval_file, stream, netSize))
currentEvalFileName[netSize] = eval_file; evalFile.selected_name = user_eval_file;
} }
if (directory == "<internal>" && eval_file == EvFileNames[netSize]) if (directory == "<internal>" && user_eval_file == evalFile.default_name)
{ {
// C++ way to prepare a buffer for a memory stream // C++ way to prepare a buffer for a memory stream
class MemoryBuffer: public std::basic_streambuf<char> { class MemoryBuffer: public std::basic_streambuf<char> {
@ -120,8 +124,8 @@ void NNUE::init() {
(void) gEmbeddedNNUESmallEnd; (void) gEmbeddedNNUESmallEnd;
std::istream stream(&buffer); std::istream stream(&buffer);
if (NNUE::load_eval(eval_file, stream, netSize)) if (NNUE::load_eval(user_eval_file, stream, netSize))
currentEvalFileName[netSize] = eval_file; evalFile.selected_name = user_eval_file;
} }
} }
} }
@ -131,24 +135,27 @@ void NNUE::init() {
// Verifies that the last net used was loaded successfully // Verifies that the last net used was loaded successfully
void NNUE::verify() { void NNUE::verify() {
for (NetSize netSize : {Big, Small}) for (const auto& [netSize, evalFile] : EvalFiles)
{ {
// change after fishtest supports EvalFileSmall // Replace with
std::string eval_file = // Options[evalFile.option_name]
std::string(netSize == Small ? EvalFileDefaultNameSmall : Options[EvFiles[netSize]]); // once fishtest supports the uci option EvalFileSmall
if (eval_file.empty()) std::string user_eval_file =
eval_file = EvFileNames[netSize]; netSize == Small ? evalFile.default_name : Options[evalFile.option_name];
if (user_eval_file.empty())
user_eval_file = evalFile.default_name;
if (currentEvalFileName[netSize] != eval_file) if (evalFile.selected_name != user_eval_file)
{ {
std::string msg1 = std::string msg1 =
"Network evaluation parameters compatible with the engine must be available."; "Network evaluation parameters compatible with the engine must be available.";
std::string msg2 = "The network file " + eval_file + " was not loaded successfully."; std::string msg2 =
"The network file " + user_eval_file + " was not loaded successfully.";
std::string msg3 = "The UCI option EvalFile might need to specify the full path, " std::string msg3 = "The UCI option EvalFile might need to specify the full path, "
"including the directory name, to the network file."; "including the directory name, to the network file.";
std::string msg4 = "The default net can be downloaded from: " std::string msg4 = "The default net can be downloaded from: "
"https://tests.stockfishchess.org/api/nn/" "https://tests.stockfishchess.org/api/nn/"
+ std::string(EvFileNames[netSize]); + evalFile.default_name;
std::string msg5 = "The engine will be terminated now."; std::string msg5 = "The engine will be terminated now.";
sync_cout << "info string ERROR: " << msg1 << sync_endl; sync_cout << "info string ERROR: " << msg1 << sync_endl;
@ -160,7 +167,7 @@ void NNUE::verify() {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
sync_cout << "info string NNUE evaluation using " << eval_file << sync_endl; sync_cout << "info string NNUE evaluation using " << user_eval_file << sync_endl;
} }
} }
} }

View file

@ -20,6 +20,7 @@
#define EVALUATE_H_INCLUDED #define EVALUATE_H_INCLUDED
#include <string> #include <string>
#include <unordered_map>
#include "types.h" #include "types.h"
@ -34,8 +35,6 @@ std::string trace(Position& pos);
int simple_eval(const Position& pos, Color c); int simple_eval(const Position& pos, Color c);
Value evaluate(const Position& pos); Value evaluate(const Position& pos);
extern std::string currentEvalFileName[2];
// The default net name MUST follow the format nn-[SHA256 first 12 digits].nnue // The default net name MUST follow the format nn-[SHA256 first 12 digits].nnue
// for the build process (profile-build and fishtest) to work. Do not change the // for the build process (profile-build and fishtest) to work. Do not change the
// name of the macro, as it is used in the Makefile. // name of the macro, as it is used in the Makefile.
@ -44,11 +43,21 @@ extern std::string currentEvalFileName[2];
namespace NNUE { namespace NNUE {
enum NetSize : int;
void init(); void init();
void verify(); void verify();
} // namespace NNUE } // namespace NNUE
struct EvalFile {
std::string option_name;
std::string default_name;
std::string selected_name;
};
extern std::unordered_map<NNUE::NetSize, EvalFile> EvalFiles;
} // namespace Eval } // namespace Eval
} // namespace Stockfish } // namespace Stockfish

View file

@ -28,6 +28,7 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <string_view> #include <string_view>
#include <unordered_map>
#include "../evaluate.h" #include "../evaluate.h"
#include "../misc.h" #include "../misc.h"
@ -449,7 +450,7 @@ bool save_eval(const std::optional<std::string>& filename, NetSize netSize) {
actualFilename = filename.value(); actualFilename = filename.value();
else else
{ {
if (currentEvalFileName[netSize] if (EvalFiles.at(netSize).selected_name
!= (netSize == Small ? EvalFileDefaultNameSmall : EvalFileDefaultNameBig)) != (netSize == Small ? EvalFileDefaultNameSmall : EvalFileDefaultNameBig))
{ {
msg = "Failed to export a net. " msg = "Failed to export a net. "

View file

@ -37,7 +37,7 @@ namespace Stockfish::Eval::NNUE {
// Input features used in evaluation function // Input features used in evaluation function
using FeatureSet = Features::HalfKAv2_hm; using FeatureSet = Features::HalfKAv2_hm;
enum NetSize { enum NetSize : int {
Big, Big,
Small Small
}; };