mirror of
https://github.com/HChaZZY/Stockfish.git
synced 2025-12-23 02:27:00 +08: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:
@@ -23,10 +23,10 @@
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <initializer_list>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "incbin/incbin.h"
|
||||
@@ -62,9 +62,10 @@ namespace Stockfish {
|
||||
|
||||
namespace Eval {
|
||||
|
||||
std::string currentEvalFileName[2] = {"None", "None"};
|
||||
const std::string EvFiles[2] = {"EvalFile", "EvalFileSmall"};
|
||||
const std::string EvFileNames[2] = {EvalFileDefaultNameBig, EvalFileDefaultNameSmall};
|
||||
std::unordered_map<NNUE::NetSize, EvalFile> EvalFiles = {
|
||||
{NNUE::Big, {"EvalFile", EvalFileDefaultNameBig, "None"}},
|
||||
{NNUE::Small, {"EvalFileSmall", EvalFileDefaultNameSmall, "None"}}};
|
||||
|
||||
|
||||
// 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"
|
||||
@@ -75,13 +76,16 @@ const std::string EvFileNames[2] = {EvalFileDefaultNameBig, EvalFileDefa
|
||||
// variable to have the engine search in a special directory in their distro.
|
||||
void NNUE::init() {
|
||||
|
||||
for (NetSize netSize : {Big, Small})
|
||||
for (auto& [netSize, evalFile] : EvalFiles)
|
||||
{
|
||||
// change after fishtest supports EvalFileSmall
|
||||
std::string eval_file =
|
||||
std::string(netSize == Small ? EvalFileDefaultNameSmall : Options[EvFiles[netSize]]);
|
||||
if (eval_file.empty())
|
||||
eval_file = EvFileNames[netSize];
|
||||
// Replace with
|
||||
// Options[evalFile.option_name]
|
||||
// once fishtest supports the uci option EvalFileSmall
|
||||
std::string user_eval_file =
|
||||
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)
|
||||
std::vector<std::string> dirs = {"<internal>", "", CommandLine::binaryDirectory,
|
||||
@@ -92,16 +96,16 @@ void NNUE::init() {
|
||||
|
||||
for (const std::string& directory : dirs)
|
||||
{
|
||||
if (currentEvalFileName[netSize] != eval_file)
|
||||
if (evalFile.selected_name != user_eval_file)
|
||||
{
|
||||
if (directory != "<internal>")
|
||||
{
|
||||
std::ifstream stream(directory + eval_file, std::ios::binary);
|
||||
if (NNUE::load_eval(eval_file, stream, netSize))
|
||||
currentEvalFileName[netSize] = eval_file;
|
||||
std::ifstream stream(directory + user_eval_file, std::ios::binary);
|
||||
if (NNUE::load_eval(user_eval_file, stream, netSize))
|
||||
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
|
||||
class MemoryBuffer: public std::basic_streambuf<char> {
|
||||
@@ -120,8 +124,8 @@ void NNUE::init() {
|
||||
(void) gEmbeddedNNUESmallEnd;
|
||||
|
||||
std::istream stream(&buffer);
|
||||
if (NNUE::load_eval(eval_file, stream, netSize))
|
||||
currentEvalFileName[netSize] = eval_file;
|
||||
if (NNUE::load_eval(user_eval_file, stream, netSize))
|
||||
evalFile.selected_name = user_eval_file;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -131,24 +135,27 @@ void NNUE::init() {
|
||||
// Verifies that the last net used was loaded successfully
|
||||
void NNUE::verify() {
|
||||
|
||||
for (NetSize netSize : {Big, Small})
|
||||
for (const auto& [netSize, evalFile] : EvalFiles)
|
||||
{
|
||||
// change after fishtest supports EvalFileSmall
|
||||
std::string eval_file =
|
||||
std::string(netSize == Small ? EvalFileDefaultNameSmall : Options[EvFiles[netSize]]);
|
||||
if (eval_file.empty())
|
||||
eval_file = EvFileNames[netSize];
|
||||
// Replace with
|
||||
// Options[evalFile.option_name]
|
||||
// once fishtest supports the uci option EvalFileSmall
|
||||
std::string user_eval_file =
|
||||
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 =
|
||||
"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, "
|
||||
"including the directory name, to the network file.";
|
||||
std::string msg4 = "The default net can be downloaded from: "
|
||||
"https://tests.stockfishchess.org/api/nn/"
|
||||
+ std::string(EvFileNames[netSize]);
|
||||
+ evalFile.default_name;
|
||||
std::string msg5 = "The engine will be terminated now.";
|
||||
|
||||
sync_cout << "info string ERROR: " << msg1 << sync_endl;
|
||||
@@ -160,7 +167,7 @@ void NNUE::verify() {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user