-namespace Eval {
-
- string currentEvalFileName = "None";
-
- /// NNUE::init() 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"
- /// The name of the NNUE network is always retrieved from the EvalFile option.
- /// We search the given network in three locations: internally (the default
- /// network may be embedded in the binary), in the active working directory and
- /// in the engine directory. Distro packagers may define the DEFAULT_NNUE_DIRECTORY
- /// variable to have the engine search in a special directory in their distro.
-
- void NNUE::init() {
-
- string eval_file = string(Options["EvalFile"]);
- if (eval_file.empty())
- eval_file = EvalFileDefaultName;
-
- #if defined(DEFAULT_NNUE_DIRECTORY)
- vector<string> dirs = { "<internal>" , "" , CommandLine::binaryDirectory , stringify(DEFAULT_NNUE_DIRECTORY) };
- #else
- vector<string> dirs = { "<internal>" , "" , CommandLine::binaryDirectory };
- #endif
-
- for (const string& directory : dirs)
- if (currentEvalFileName != eval_file)
- {
- if (directory != "<internal>")
- {
- ifstream stream(directory + eval_file, ios::binary);
- if (NNUE::load_eval(eval_file, stream))
- currentEvalFileName = eval_file;
- }
-
- if (directory == "<internal>" && eval_file == EvalFileDefaultName)
- {
- // C++ way to prepare a buffer for a memory stream
- class MemoryBuffer : public basic_streambuf<char> {
- public: MemoryBuffer(char* p, size_t n) { setg(p, p, p + n); setp(p, p + n); }
- };
-
- MemoryBuffer buffer(const_cast<char*>(reinterpret_cast<const char*>(gEmbeddedNNUEData)),
- size_t(gEmbeddedNNUESize));
- (void) gEmbeddedNNUEEnd; // Silence warning on unused variable
-
- istream stream(&buffer);
- if (NNUE::load_eval(eval_file, stream))
- currentEvalFileName = eval_file;
- }
- }
- }
-
- /// NNUE::verify() verifies that the last net used was loaded successfully
- void NNUE::verify() {
-
- string eval_file = string(Options["EvalFile"]);
- if (eval_file.empty())
- eval_file = EvalFileDefaultName;
-
- if (currentEvalFileName != eval_file)
- {
-
- string msg1 = "Network evaluation parameters compatible with the engine must be available.";
- string msg2 = "The network file " + eval_file + " was not loaded successfully.";
- string msg3 = "The UCI option EvalFile might need to specify the full path, including the directory name, to the network file.";
- string msg4 = "The default net can be downloaded from: https://tests.stockfishchess.org/api/nn/" + std::string(EvalFileDefaultName);
- string msg5 = "The engine will be terminated now.";
-
- sync_cout << "info string ERROR: " << msg1 << sync_endl;
- sync_cout << "info string ERROR: " << msg2 << sync_endl;
- sync_cout << "info string ERROR: " << msg3 << sync_endl;
- sync_cout << "info string ERROR: " << msg4 << sync_endl;
- sync_cout << "info string ERROR: " << msg5 << sync_endl;
-
- exit(EXIT_FAILURE);
- }
-
- sync_cout << "info string NNUE evaluation using " << eval_file << sync_endl;
- }