Skip to content

Commit

Permalink
Refactor global variables
Browse files Browse the repository at this point in the history
This aims to remove some of the annoying global structure which Stockfish has.

Overall there is no major elo regression to be expected.

Non regression SMP STC (paused, early version):
https://tests.stockfishchess.org/tests/view/65983d7979aa8af82b9608f1
LLR: 0.23 (-2.94,2.94) <-1.75,0.25>
Total: 76232 W: 19035 L: 19096 D: 38101
Ptnml(0-2): 92, 8735, 20515, 8690, 84

Non regression STC (early version):
https://tests.stockfishchess.org/tests/view/6595b3a479aa8af82b95da7f
LLR: 2.93 (-2.94,2.94) <-1.75,0.25>
Total: 185344 W: 47027 L: 46972 D: 91345
Ptnml(0-2): 571, 21285, 48943, 21264, 609

Non regression SMP STC:
https://tests.stockfishchess.org/tests/view/65a0715c79aa8af82b96b7e4
LLR: 2.94 (-2.94,2.94) <-1.75,0.25>
Total: 142936 W: 35761 L: 35662 D: 71513
Ptnml(0-2): 209, 16400, 38135, 16531, 193

These global structures/variables add hidden dependencies and allow data
to be mutable from where it shouldn't it be (i.e. options). They also
prevent Stockfish from internal selfplay, which would be a nice thing to
be able to do, i.e. instantiate two Stockfish instances and let them
play against each other. It will also allow us to make Stockfish a
library, which can be easier used on other platforms.

For consistency with the old search code, `thisThread` has been kept,
even though it is not strictly necessary anymore. This the first major
refactor of this kind (in recent time), and future changes are required,
to achieve the previously described goals. This includes cleaning up the
dependencies, transforming the network to be self contained and coming
up with a plan to deal with proper tablebase memory management (see
comments for more information on this).

The removal of these global structures has been discussed in parts with
Vondele and Sopel.

closes official-stockfish#4968

No functional change
  • Loading branch information
Disservin authored and windfishballad committed Jan 23, 2024
1 parent 3a63835 commit 60c5294
Show file tree
Hide file tree
Showing 27 changed files with 1,175 additions and 976 deletions.
2 changes: 1 addition & 1 deletion src/Makefile
Expand Up @@ -63,7 +63,7 @@ HEADERS = benchmark.h bitboard.h evaluate.h misc.h movegen.h movepick.h \
nnue/layers/sqr_clipped_relu.h nnue/nnue_accumulator.h nnue/nnue_architecture.h \
nnue/nnue_common.h nnue/nnue_feature_transformer.h position.h \
search.h syzygy/tbprobe.h thread.h thread_win32_osx.h timeman.h \
tt.h tune.h types.h uci.h
tt.h tune.h types.h uci.h ucioption.h

OBJS = $(notdir $(SRCS:.cpp=.o))

Expand Down
79 changes: 46 additions & 33 deletions src/evaluate.cpp
Expand Up @@ -25,6 +25,7 @@
#include <fstream>
#include <iomanip>
#include <iostream>
#include <optional>
#include <sstream>
#include <unordered_map>
#include <vector>
Expand All @@ -34,9 +35,10 @@
#include "nnue/evaluate_nnue.h"
#include "nnue/nnue_architecture.h"
#include "position.h"
#include "thread.h"
#include "search.h"
#include "types.h"
#include "uci.h"
#include "ucioption.h"

// Macro to embed the default efficiently updatable neural network (NNUE) file
// data in the engine binary (using incbin.h, by Dale Weiler).
Expand All @@ -62,10 +64,6 @@ namespace Stockfish {

namespace Eval {

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"
Expand All @@ -74,38 +72,45 @@ std::unordered_map<NNUE::NetSize, EvalFile> EvalFiles = {
// 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() {
NNUE::EvalFiles NNUE::load_networks(const std::string& rootDirectory,
const OptionsMap& options,
NNUE::EvalFiles evalFiles) {

for (auto& [netSize, evalFile] : EvalFiles)
for (auto& [netSize, evalFile] : evalFiles)
{
// Replace with
// Options[evalFile.option_name]
// options[evalFile.optionName]
// once fishtest supports the uci option EvalFileSmall
std::string user_eval_file =
netSize == Small ? evalFile.default_name : Options[evalFile.option_name];
netSize == Small ? evalFile.defaultName : options[evalFile.optionName];

if (user_eval_file.empty())
user_eval_file = evalFile.default_name;
user_eval_file = evalFile.defaultName;

#if defined(DEFAULT_NNUE_DIRECTORY)
std::vector<std::string> dirs = {"<internal>", "", CommandLine::binaryDirectory,
std::vector<std::string> dirs = {"<internal>", "", rootDirectory,
stringify(DEFAULT_NNUE_DIRECTORY)};
#else
std::vector<std::string> dirs = {"<internal>", "", CommandLine::binaryDirectory};
std::vector<std::string> dirs = {"<internal>", "", rootDirectory};
#endif

for (const std::string& directory : dirs)
{
if (evalFile.selected_name != user_eval_file)
if (evalFile.current != user_eval_file)
{
if (directory != "<internal>")
{
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;
auto description = NNUE::load_eval(stream, netSize);

if (description.has_value())
{
evalFile.current = user_eval_file;
evalFile.netDescription = description.value();
}
}

if (directory == "<internal>" && user_eval_file == evalFile.default_name)
if (directory == "<internal>" && user_eval_file == evalFile.defaultName)
{
// C++ way to prepare a buffer for a memory stream
class MemoryBuffer: public std::basic_streambuf<char> {
Expand All @@ -124,28 +129,36 @@ void NNUE::init() {
(void) gEmbeddedNNUESmallEnd;

std::istream stream(&buffer);
if (NNUE::load_eval(user_eval_file, stream, netSize))
evalFile.selected_name = user_eval_file;
auto description = NNUE::load_eval(stream, netSize);

if (description.has_value())
{
evalFile.current = user_eval_file;
evalFile.netDescription = description.value();
}
}
}
}
}

return evalFiles;
}

// Verifies that the last net used was loaded successfully
void NNUE::verify() {
void NNUE::verify(const OptionsMap& options,
const std::unordered_map<Eval::NNUE::NetSize, EvalFile>& evalFiles) {

for (const auto& [netSize, evalFile] : EvalFiles)
for (const auto& [netSize, evalFile] : evalFiles)
{
// Replace with
// Options[evalFile.option_name]
// options[evalFile.optionName]
// once fishtest supports the uci option EvalFileSmall
std::string user_eval_file =
netSize == Small ? evalFile.default_name : Options[evalFile.option_name];
netSize == Small ? evalFile.defaultName : options[evalFile.optionName];
if (user_eval_file.empty())
user_eval_file = evalFile.default_name;
user_eval_file = evalFile.defaultName;

if (evalFile.selected_name != user_eval_file)
if (evalFile.current != user_eval_file)
{
std::string msg1 =
"Network evaluation parameters compatible with the engine must be available.";
Expand All @@ -155,7 +168,7 @@ void NNUE::verify() {
"including the directory name, to the network file.";
std::string msg4 = "The default net can be downloaded from: "
"https://tests.stockfishchess.org/api/nn/"
+ evalFile.default_name;
+ evalFile.defaultName;
std::string msg5 = "The engine will be terminated now.";

sync_cout << "info string ERROR: " << msg1 << sync_endl;
Expand Down Expand Up @@ -183,7 +196,7 @@ int Eval::simple_eval(const Position& pos, Color c) {

// Evaluate is the evaluator for the outer world. It returns a static evaluation
// of the position from the point of view of the side to move.
Value Eval::evaluate(const Position& pos) {
Value Eval::evaluate(const Position& pos, const Search::Worker& workerThread) {

assert(!pos.checkers());

Expand All @@ -204,7 +217,7 @@ Value Eval::evaluate(const Position& pos) {
Value nnue = smallNet ? NNUE::evaluate<NNUE::Small>(pos, true, &nnueComplexity)
: NNUE::evaluate<NNUE::Big>(pos, true, &nnueComplexity);

int optimism = pos.this_thread()->optimism[stm];
int optimism = workerThread.optimism[stm];

// Blend optimism and eval with nnue complexity and material imbalance
optimism += optimism * (nnueComplexity + std::abs(simpleEval - nnue)) / 512;
Expand All @@ -227,16 +240,16 @@ Value Eval::evaluate(const Position& pos) {
// a string (suitable for outputting to stdout) that contains the detailed
// descriptions and values of each evaluation term. Useful for debugging.
// Trace scores are from white's point of view
std::string Eval::trace(Position& pos) {
std::string Eval::trace(Position& pos, Search::Worker& workerThread) {

if (pos.checkers())
return "Final evaluation: none (in check)";

// Reset any global variable used in eval
pos.this_thread()->bestValue = VALUE_ZERO;
pos.this_thread()->rootSimpleEval = VALUE_ZERO;
pos.this_thread()->optimism[WHITE] = VALUE_ZERO;
pos.this_thread()->optimism[BLACK] = VALUE_ZERO;
workerThread.iterBestValue = VALUE_ZERO;
workerThread.rootSimpleEval = VALUE_ZERO;
workerThread.optimism[WHITE] = VALUE_ZERO;
workerThread.optimism[BLACK] = VALUE_ZERO;

std::stringstream ss;
ss << std::showpoint << std::noshowpos << std::fixed << std::setprecision(2);
Expand All @@ -249,7 +262,7 @@ std::string Eval::trace(Position& pos) {
v = pos.side_to_move() == WHITE ? v : -v;
ss << "NNUE evaluation " << 0.01 * UCI::to_cp(v) << " (white side)\n";

v = evaluate(pos);
v = evaluate(pos, workerThread);
v = pos.side_to_move() == WHITE ? v : -v;
ss << "Final evaluation " << 0.01 * UCI::to_cp(v) << " (white side)";
ss << " [with scaled NNUE, ...]";
Expand Down
34 changes: 22 additions & 12 deletions src/evaluate.h
Expand Up @@ -27,36 +27,46 @@
namespace Stockfish {

class Position;
class OptionsMap;

namespace Search {
class Worker;
}

namespace Eval {

std::string trace(Position& pos);
std::string trace(Position& pos, Search::Worker& workerThread);

int simple_eval(const Position& pos, Color c);
Value evaluate(const Position& pos);
Value evaluate(const Position& pos, const Search::Worker& workerThread);

// 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
// name of the macro, as it is used in the Makefile.
#define EvalFileDefaultNameBig "nn-baff1edbea57.nnue"
#define EvalFileDefaultNameSmall "nn-baff1ede1f90.nnue"

struct EvalFile {
// UCI option name
std::string optionName;
// Default net name, will use one of the macros above
std::string defaultName;
// Selected net name, either via uci option or default
std::string current;
// Net description extracted from the net file
std::string netDescription;
};

namespace NNUE {

enum NetSize : int;

void init();
void verify();
using EvalFiles = std::unordered_map<Eval::NNUE::NetSize, EvalFile>;

} // namespace NNUE
EvalFiles load_networks(const std::string&, const OptionsMap&, EvalFiles);
void verify(const OptionsMap&, const EvalFiles&);

struct EvalFile {
std::string option_name;
std::string default_name;
std::string selected_name;
};

extern std::unordered_map<NNUE::NetSize, EvalFile> EvalFiles;
} // namespace NNUE

} // namespace Eval

Expand Down
19 changes: 8 additions & 11 deletions src/main.cpp
Expand Up @@ -16,15 +16,13 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <cstddef>
#include <iostream>
#include <unordered_map>

#include "bitboard.h"
#include "evaluate.h"
#include "misc.h"
#include "position.h"
#include "search.h"
#include "thread.h"
#include "tune.h"
#include "types.h"
#include "uci.h"
Expand All @@ -35,17 +33,16 @@ int main(int argc, char* argv[]) {

std::cout << engine_info() << std::endl;

CommandLine::init(argc, argv);
UCI::init(Options);
Tune::init();
Bitboards::init();
Position::init();
Threads.set(size_t(Options["Threads"]));
Search::clear(); // After threads are up
Eval::NNUE::init();

UCI::loop(argc, argv);
UCI uci(argc, argv);

Tune::init(uci.options);

uci.evalFiles = Eval::NNUE::load_networks(uci.workingDirectory(), uci.options, uci.evalFiles);

uci.loop();

Threads.set(0);
return 0;
}
15 changes: 4 additions & 11 deletions src/misc.cpp
Expand Up @@ -721,17 +721,13 @@ void bindThisThread(size_t idx) {
#define GETCWD getcwd
#endif

namespace CommandLine {

std::string argv0; // path+name of the executable binary, as given by argv[0]
std::string binaryDirectory; // path of the executable directory
std::string workingDirectory; // path of the working directory

void init([[maybe_unused]] int argc, char* argv[]) {
CommandLine::CommandLine(int _argc, char** _argv) :
argc(_argc),
argv(_argv) {
std::string pathSeparator;

// Extract the path+name of the executable binary
argv0 = argv[0];
std::string argv0 = argv[0];

#ifdef _WIN32
pathSeparator = "\\";
Expand Down Expand Up @@ -766,7 +762,4 @@ void init([[maybe_unused]] int argc, char* argv[]) {
binaryDirectory.replace(0, 1, workingDirectory);
}


} // namespace CommandLine

} // namespace Stockfish
15 changes: 10 additions & 5 deletions src/misc.h
Expand Up @@ -176,12 +176,17 @@ namespace WinProcGroup {
void bindThisThread(size_t idx);
}

namespace CommandLine {
void init(int argc, char* argv[]);

extern std::string binaryDirectory; // path of the executable directory
extern std::string workingDirectory; // path of the working directory
}
struct CommandLine {
public:
CommandLine(int, char**);

int argc;
char** argv;

std::string binaryDirectory; // path of the executable directory
std::string workingDirectory; // path of the working directory
};

} // namespace Stockfish

Expand Down

0 comments on commit 60c5294

Please sign in to comment.