Skip to content

Commit 1a26d69

Browse files
committed
Refactor Network Usage
Continuing from PR #4968, this update improves how Stockfish handles network usage, making it easier to manage and modify networks in the future. With the introduction of a dedicated Network class, creating networks has become straightforward. See uci.cpp: ```cpp NN::NetworkBig({EvalFileDefaultNameBig, "None", ""}, NN::embeddedNNUEBig) ``` The new `Network` encapsulates all network-related logic, significantly reducing the complexity previously required to support multiple network types, such as the distinction between small and big networks #4915. Non-Regression STC: https://tests.stockfishchess.org/tests/view/65edd26c0ec64f0526c43584 LLR: 2.94 (-2.94,2.94) <-1.75,0.25> Total: 33760 W: 8887 L: 8661 D: 16212 Ptnml(0-2): 143, 3795, 8808, 3961, 173 Non-Regression SMP STC: https://tests.stockfishchess.org/tests/view/65ed71970ec64f0526c42fdd LLR: 2.96 (-2.94,2.94) <-1.75,0.25> Total: 59088 W: 15121 L: 14931 D: 29036 Ptnml(0-2): 110, 6640, 15829, 6880, 85 Compiled with `make -j profile-build` ``` bash ./bench_parallel.sh ./stockfish ./stockfish-nnue 13 50 sf_base = 1568540 +/- 7637 (95%) sf_test = 1573129 +/- 7301 (95%) diff = 4589 +/- 8720 (95%) speedup = 0.29260% +/- 0.556% (95%) ``` Compiled with `make -j build` ``` bash ./bench_parallel.sh ./stockfish ./stockfish-nnue 13 50 sf_base = 1472653 +/- 7293 (95%) sf_test = 1491928 +/- 7661 (95%) diff = 19275 +/- 7154 (95%) speedup = 1.30886% +/- 0.486% (95%) ``` closes #5100 No functional change
1 parent f072634 commit 1a26d69

18 files changed

+948
-826
lines changed

src/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ PGOBENCH = $(WINE_PATH) ./$(EXE) bench
5555
SRCS = benchmark.cpp bitboard.cpp evaluate.cpp main.cpp \
5656
misc.cpp movegen.cpp movepick.cpp position.cpp \
5757
search.cpp thread.cpp timeman.cpp tt.cpp uci.cpp ucioption.cpp tune.cpp syzygy/tbprobe.cpp \
58-
nnue/evaluate_nnue.cpp nnue/features/half_ka_v2_hm.cpp
58+
nnue/nnue_misc.cpp nnue/features/half_ka_v2_hm.cpp nnue/network.cpp
5959

6060
HEADERS = benchmark.h bitboard.h evaluate.h misc.h movegen.h movepick.h \
61-
nnue/evaluate_nnue.h nnue/features/half_ka_v2_hm.h nnue/layers/affine_transform.h \
61+
nnue/nnue_misc.h nnue/features/half_ka_v2_hm.h nnue/layers/affine_transform.h \
6262
nnue/layers/affine_transform_sparse_input.h nnue/layers/clipped_relu.h nnue/layers/simd.h \
6363
nnue/layers/sqr_clipped_relu.h nnue/nnue_accumulator.h nnue/nnue_architecture.h \
6464
nnue/nnue_common.h nnue/nnue_feature_transformer.h position.h \
6565
search.h syzygy/tbprobe.h thread.h thread_win32_osx.h timeman.h \
66-
tt.h tune.h types.h uci.h ucioption.h perft.h
66+
tt.h tune.h types.h uci.h ucioption.h perft.h nnue/network.cpp
6767

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

@@ -502,7 +502,7 @@ endif
502502
# In earlier NDK versions, you'll need to pass -fno-addrsig if using GNU binutils.
503503
# Currently we don't know how to make PGO builds with the NDK yet.
504504
ifeq ($(COMP),ndk)
505-
CXXFLAGS += -stdlib=libc++ -fPIE
505+
CXXFLAGS += -stdlib=libc++ -fPIE -mcmodel=large
506506
comp=clang
507507
ifeq ($(arch),armv7)
508508
CXX=armv7a-linux-androideabi16-clang++

src/evaluate.cpp

Lines changed: 10 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -22,161 +22,18 @@
2222
#include <cassert>
2323
#include <cmath>
2424
#include <cstdlib>
25-
#include <fstream>
2625
#include <iomanip>
2726
#include <iostream>
28-
#include <optional>
2927
#include <sstream>
30-
#include <unordered_map>
31-
#include <vector>
3228

33-
#include "incbin/incbin.h"
34-
#include "misc.h"
35-
#include "nnue/evaluate_nnue.h"
36-
#include "nnue/nnue_architecture.h"
29+
#include "nnue/network.h"
30+
#include "nnue/nnue_misc.h"
3731
#include "position.h"
3832
#include "types.h"
3933
#include "uci.h"
40-
#include "ucioption.h"
41-
42-
// Macro to embed the default efficiently updatable neural network (NNUE) file
43-
// data in the engine binary (using incbin.h, by Dale Weiler).
44-
// This macro invocation will declare the following three variables
45-
// const unsigned char gEmbeddedNNUEData[]; // a pointer to the embedded data
46-
// const unsigned char *const gEmbeddedNNUEEnd; // a marker to the end
47-
// const unsigned int gEmbeddedNNUESize; // the size of the embedded file
48-
// Note that this does not work in Microsoft Visual Studio.
49-
#if !defined(_MSC_VER) && !defined(NNUE_EMBEDDING_OFF)
50-
INCBIN(EmbeddedNNUEBig, EvalFileDefaultNameBig);
51-
INCBIN(EmbeddedNNUESmall, EvalFileDefaultNameSmall);
52-
#else
53-
const unsigned char gEmbeddedNNUEBigData[1] = {0x0};
54-
const unsigned char* const gEmbeddedNNUEBigEnd = &gEmbeddedNNUEBigData[1];
55-
const unsigned int gEmbeddedNNUEBigSize = 1;
56-
const unsigned char gEmbeddedNNUESmallData[1] = {0x0};
57-
const unsigned char* const gEmbeddedNNUESmallEnd = &gEmbeddedNNUESmallData[1];
58-
const unsigned int gEmbeddedNNUESmallSize = 1;
59-
#endif
60-
6134

6235
namespace Stockfish {
6336

64-
namespace Eval {
65-
66-
67-
// Tries to load a NNUE network at startup time, or when the engine
68-
// receives a UCI command "setoption name EvalFile value nn-[a-z0-9]{12}.nnue"
69-
// The name of the NNUE network is always retrieved from the EvalFile option.
70-
// We search the given network in three locations: internally (the default
71-
// network may be embedded in the binary), in the active working directory and
72-
// in the engine directory. Distro packagers may define the DEFAULT_NNUE_DIRECTORY
73-
// variable to have the engine search in a special directory in their distro.
74-
NNUE::EvalFiles NNUE::load_networks(const std::string& rootDirectory,
75-
const OptionsMap& options,
76-
NNUE::EvalFiles evalFiles) {
77-
78-
for (auto& [netSize, evalFile] : evalFiles)
79-
{
80-
std::string user_eval_file = options[evalFile.optionName];
81-
82-
if (user_eval_file.empty())
83-
user_eval_file = evalFile.defaultName;
84-
85-
#if defined(DEFAULT_NNUE_DIRECTORY)
86-
std::vector<std::string> dirs = {"<internal>", "", rootDirectory,
87-
stringify(DEFAULT_NNUE_DIRECTORY)};
88-
#else
89-
std::vector<std::string> dirs = {"<internal>", "", rootDirectory};
90-
#endif
91-
92-
for (const std::string& directory : dirs)
93-
{
94-
if (evalFile.current != user_eval_file)
95-
{
96-
if (directory != "<internal>")
97-
{
98-
std::ifstream stream(directory + user_eval_file, std::ios::binary);
99-
auto description = NNUE::load_eval(stream, netSize);
100-
101-
if (description.has_value())
102-
{
103-
evalFile.current = user_eval_file;
104-
evalFile.netDescription = description.value();
105-
}
106-
}
107-
108-
if (directory == "<internal>" && user_eval_file == evalFile.defaultName)
109-
{
110-
// C++ way to prepare a buffer for a memory stream
111-
class MemoryBuffer: public std::basic_streambuf<char> {
112-
public:
113-
MemoryBuffer(char* p, size_t n) {
114-
setg(p, p, p + n);
115-
setp(p, p + n);
116-
}
117-
};
118-
119-
MemoryBuffer buffer(
120-
const_cast<char*>(reinterpret_cast<const char*>(
121-
netSize == Small ? gEmbeddedNNUESmallData : gEmbeddedNNUEBigData)),
122-
size_t(netSize == Small ? gEmbeddedNNUESmallSize : gEmbeddedNNUEBigSize));
123-
(void) gEmbeddedNNUEBigEnd; // Silence warning on unused variable
124-
(void) gEmbeddedNNUESmallEnd;
125-
126-
std::istream stream(&buffer);
127-
auto description = NNUE::load_eval(stream, netSize);
128-
129-
if (description.has_value())
130-
{
131-
evalFile.current = user_eval_file;
132-
evalFile.netDescription = description.value();
133-
}
134-
}
135-
}
136-
}
137-
}
138-
139-
return evalFiles;
140-
}
141-
142-
// Verifies that the last net used was loaded successfully
143-
void NNUE::verify(const OptionsMap& options,
144-
const std::unordered_map<Eval::NNUE::NetSize, EvalFile>& evalFiles) {
145-
146-
for (const auto& [netSize, evalFile] : evalFiles)
147-
{
148-
std::string user_eval_file = options[evalFile.optionName];
149-
150-
if (user_eval_file.empty())
151-
user_eval_file = evalFile.defaultName;
152-
153-
if (evalFile.current != user_eval_file)
154-
{
155-
std::string msg1 =
156-
"Network evaluation parameters compatible with the engine must be available.";
157-
std::string msg2 =
158-
"The network file " + user_eval_file + " was not loaded successfully.";
159-
std::string msg3 = "The UCI option EvalFile might need to specify the full path, "
160-
"including the directory name, to the network file.";
161-
std::string msg4 = "The default net can be downloaded from: "
162-
"https://tests.stockfishchess.org/api/nn/"
163-
+ evalFile.defaultName;
164-
std::string msg5 = "The engine will be terminated now.";
165-
166-
sync_cout << "info string ERROR: " << msg1 << sync_endl;
167-
sync_cout << "info string ERROR: " << msg2 << sync_endl;
168-
sync_cout << "info string ERROR: " << msg3 << sync_endl;
169-
sync_cout << "info string ERROR: " << msg4 << sync_endl;
170-
sync_cout << "info string ERROR: " << msg5 << sync_endl;
171-
172-
exit(EXIT_FAILURE);
173-
}
174-
175-
sync_cout << "info string NNUE evaluation using " << user_eval_file << sync_endl;
176-
}
177-
}
178-
}
179-
18037
// Returns a static, purely materialistic evaluation of the position from
18138
// the point of view of the given color. It can be divided by PawnValue to get
18239
// an approximation of the material advantage on the board in terms of pawns.
@@ -188,7 +45,7 @@ int Eval::simple_eval(const Position& pos, Color c) {
18845

18946
// Evaluate is the evaluator for the outer world. It returns a static evaluation
19047
// of the position from the point of view of the side to move.
191-
Value Eval::evaluate(const Position& pos, int optimism) {
48+
Value Eval::evaluate(const Eval::NNUE::Networks& networks, const Position& pos, int optimism) {
19249

19350
assert(!pos.checkers());
19451

@@ -198,8 +55,8 @@ Value Eval::evaluate(const Position& pos, int optimism) {
19855

19956
int nnueComplexity;
20057

201-
Value nnue = smallNet ? NNUE::evaluate<NNUE::Small>(pos, true, &nnueComplexity, psqtOnly)
202-
: NNUE::evaluate<NNUE::Big>(pos, true, &nnueComplexity, false);
58+
Value nnue = smallNet ? networks.small.evaluate(pos, true, &nnueComplexity, psqtOnly)
59+
: networks.big.evaluate(pos, true, &nnueComplexity, false);
20360

20461
// Blend optimism and eval with nnue complexity and material imbalance
20562
optimism += optimism * (nnueComplexity + std::abs(simpleEval - nnue)) / 512;
@@ -222,23 +79,22 @@ Value Eval::evaluate(const Position& pos, int optimism) {
22279
// a string (suitable for outputting to stdout) that contains the detailed
22380
// descriptions and values of each evaluation term. Useful for debugging.
22481
// Trace scores are from white's point of view
225-
std::string Eval::trace(Position& pos) {
82+
std::string Eval::trace(Position& pos, const Eval::NNUE::Networks& networks) {
22683

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

23087
std::stringstream ss;
23188
ss << std::showpoint << std::noshowpos << std::fixed << std::setprecision(2);
232-
ss << '\n' << NNUE::trace(pos) << '\n';
89+
ss << '\n' << NNUE::trace(pos, networks) << '\n';
23390

23491
ss << std::showpoint << std::showpos << std::fixed << std::setprecision(2) << std::setw(15);
23592

236-
Value v;
237-
v = NNUE::evaluate<NNUE::Big>(pos, false);
238-
v = pos.side_to_move() == WHITE ? v : -v;
93+
Value v = networks.big.evaluate(pos, false);
94+
v = pos.side_to_move() == WHITE ? v : -v;
23995
ss << "NNUE evaluation " << 0.01 * UCI::to_cp(v) << " (white side)\n";
24096

241-
v = evaluate(pos, VALUE_ZERO);
97+
v = evaluate(networks, pos, VALUE_ZERO);
24298
v = pos.side_to_move() == WHITE ? v : -v;
24399
ss << "Final evaluation " << 0.01 * UCI::to_cp(v) << " (white side)";
244100
ss << " [with scaled NNUE, ...]";

src/evaluate.h

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,51 +20,33 @@
2020
#define EVALUATE_H_INCLUDED
2121

2222
#include <string>
23-
#include <unordered_map>
2423

2524
#include "types.h"
2625

2726
namespace Stockfish {
2827

2928
class Position;
30-
class OptionsMap;
3129

3230
namespace Eval {
3331

3432
constexpr inline int SmallNetThreshold = 1139, PsqtOnlyThreshold = 2500;
3533

36-
std::string trace(Position& pos);
37-
38-
int simple_eval(const Position& pos, Color c);
39-
Value evaluate(const Position& pos, int optimism);
40-
4134
// The default net name MUST follow the format nn-[SHA256 first 12 digits].nnue
4235
// for the build process (profile-build and fishtest) to work. Do not change the
43-
// name of the macro, as it is used in the Makefile.
36+
// name of the macro or the location where this macro is defined, as it is used
37+
// in the Makefile/Fishtest.
4438
#define EvalFileDefaultNameBig "nn-1ceb1ade0001.nnue"
4539
#define EvalFileDefaultNameSmall "nn-baff1ede1f90.nnue"
4640

47-
struct EvalFile {
48-
// UCI option name
49-
std::string optionName;
50-
// Default net name, will use one of the macros above
51-
std::string defaultName;
52-
// Selected net name, either via uci option or default
53-
std::string current;
54-
// Net description extracted from the net file
55-
std::string netDescription;
56-
};
57-
5841
namespace NNUE {
42+
struct Networks;
43+
}
5944

60-
enum NetSize : int;
61-
62-
using EvalFiles = std::unordered_map<Eval::NNUE::NetSize, EvalFile>;
45+
std::string trace(Position& pos, const Eval::NNUE::Networks& networks);
6346

64-
EvalFiles load_networks(const std::string&, const OptionsMap&, EvalFiles);
65-
void verify(const OptionsMap&, const EvalFiles&);
47+
int simple_eval(const Position& pos, Color c);
48+
Value evaluate(const NNUE::Networks& networks, const Position& pos, int optimism);
6649

67-
} // namespace NNUE
6850

6951
} // namespace Eval
7052

src/main.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include <iostream>
2020

2121
#include "bitboard.h"
22-
#include "evaluate.h"
2322
#include "misc.h"
2423
#include "position.h"
2524
#include "tune.h"
@@ -39,8 +38,6 @@ int main(int argc, char* argv[]) {
3938

4039
Tune::init(uci.options);
4140

42-
uci.evalFiles = Eval::NNUE::load_networks(uci.working_directory(), uci.options, uci.evalFiles);
43-
4441
uci.loop();
4542

4643
return 0;

src/misc.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <cstddef>
2626
#include <cstdint>
2727
#include <iosfwd>
28+
#include <memory>
2829
#include <string>
2930
#include <vector>
3031

@@ -49,6 +50,30 @@ void* aligned_large_pages_alloc(size_t size);
4950
// nop if mem == nullptr
5051
void aligned_large_pages_free(void* mem);
5152

53+
// Deleter for automating release of memory area
54+
template<typename T>
55+
struct AlignedDeleter {
56+
void operator()(T* ptr) const {
57+
ptr->~T();
58+
std_aligned_free(ptr);
59+
}
60+
};
61+
62+
template<typename T>
63+
struct LargePageDeleter {
64+
void operator()(T* ptr) const {
65+
ptr->~T();
66+
aligned_large_pages_free(ptr);
67+
}
68+
};
69+
70+
template<typename T>
71+
using AlignedPtr = std::unique_ptr<T, AlignedDeleter<T>>;
72+
73+
template<typename T>
74+
using LargePagePtr = std::unique_ptr<T, LargePageDeleter<T>>;
75+
76+
5277
void dbg_hit_on(bool cond, int slot = 0);
5378
void dbg_mean_of(int64_t value, int slot = 0);
5479
void dbg_stdev_of(int64_t value, int slot = 0);

0 commit comments

Comments
 (0)