]> git.sesse.net Git - stockfish/blob - src/nnue/evaluate_nnue.cpp
Modernize code base a little bit
[stockfish] / src / nnue / evaluate_nnue.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
4
5   Stockfish is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9
10   Stockfish is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 // Code for calculating NNUE evaluation function
20
21 #include <iostream>
22 #include <set>
23 #include <sstream>
24 #include <iomanip>
25 #include <fstream>
26
27 #include "../evaluate.h"
28 #include "../position.h"
29 #include "../uci.h"
30 #include "../types.h"
31
32 #include "evaluate_nnue.h"
33
34 namespace Stockfish::Eval::NNUE {
35
36   // Input feature converter
37   LargePagePtr<FeatureTransformer> featureTransformer;
38
39   // Evaluation function
40   AlignedPtr<Network> network[LayerStacks];
41
42   // Evaluation function file name
43   std::string fileName;
44   std::string netDescription;
45
46   namespace Detail {
47
48   // Initialize the evaluation function parameters
49   template <typename T>
50   void initialize(AlignedPtr<T>& pointer) {
51
52     pointer.reset(reinterpret_cast<T*>(std_aligned_alloc(alignof(T), sizeof(T))));
53     std::memset(pointer.get(), 0, sizeof(T));
54   }
55
56   template <typename T>
57   void initialize(LargePagePtr<T>& pointer) {
58
59     static_assert(alignof(T) <= 4096, "aligned_large_pages_alloc() may fail for such a big alignment requirement of T");
60     pointer.reset(reinterpret_cast<T*>(aligned_large_pages_alloc(sizeof(T))));
61     std::memset(pointer.get(), 0, sizeof(T));
62   }
63
64   // Read evaluation function parameters
65   template <typename T>
66   bool read_parameters(std::istream& stream, T& reference) {
67
68     std::uint32_t header;
69     header = read_little_endian<std::uint32_t>(stream);
70     if (!stream || header != T::get_hash_value()) return false;
71     return reference.read_parameters(stream);
72   }
73
74   // Write evaluation function parameters
75   template <typename T>
76   bool write_parameters(std::ostream& stream, const T& reference) {
77
78     write_little_endian<std::uint32_t>(stream, T::get_hash_value());
79     return reference.write_parameters(stream);
80   }
81
82   }  // namespace Detail
83
84   // Initialize the evaluation function parameters
85   static void initialize() {
86
87     Detail::initialize(featureTransformer);
88     for (std::size_t i = 0; i < LayerStacks; ++i)
89       Detail::initialize(network[i]);
90   }
91
92   // Read network header
93   static bool read_header(std::istream& stream, std::uint32_t* hashValue, std::string* desc)
94   {
95     std::uint32_t version, size;
96
97     version     = read_little_endian<std::uint32_t>(stream);
98     *hashValue  = read_little_endian<std::uint32_t>(stream);
99     size        = read_little_endian<std::uint32_t>(stream);
100     if (!stream || version != Version) return false;
101     desc->resize(size);
102     stream.read(&(*desc)[0], size);
103     return !stream.fail();
104   }
105
106   // Write network header
107   static bool write_header(std::ostream& stream, std::uint32_t hashValue, const std::string& desc)
108   {
109     write_little_endian<std::uint32_t>(stream, Version);
110     write_little_endian<std::uint32_t>(stream, hashValue);
111     write_little_endian<std::uint32_t>(stream, (std::uint32_t)desc.size());
112     stream.write(&desc[0], desc.size());
113     return !stream.fail();
114   }
115
116   // Read network parameters
117   static bool read_parameters(std::istream& stream) {
118
119     std::uint32_t hashValue;
120     if (!read_header(stream, &hashValue, &netDescription)) return false;
121     if (hashValue != HashValue) return false;
122     if (!Detail::read_parameters(stream, *featureTransformer)) return false;
123     for (std::size_t i = 0; i < LayerStacks; ++i)
124       if (!Detail::read_parameters(stream, *(network[i]))) return false;
125     return stream && stream.peek() == std::ios::traits_type::eof();
126   }
127
128   // Write network parameters
129   static bool write_parameters(std::ostream& stream) {
130
131     if (!write_header(stream, HashValue, netDescription)) return false;
132     if (!Detail::write_parameters(stream, *featureTransformer)) return false;
133     for (std::size_t i = 0; i < LayerStacks; ++i)
134       if (!Detail::write_parameters(stream, *(network[i]))) return false;
135     return (bool)stream;
136   }
137
138   // Evaluation function. Perform differential calculation.
139   Value evaluate(const Position& pos, bool adjusted, int* complexity) {
140
141     // We manually align the arrays on the stack because with gcc < 9.3
142     // overaligning stack variables with alignas() doesn't work correctly.
143
144     constexpr uint64_t alignment = CacheLineSize;
145     int delta = 24 - pos.non_pawn_material() / 9560;
146
147 #if defined(ALIGNAS_ON_STACK_VARIABLES_BROKEN)
148     TransformedFeatureType transformedFeaturesUnaligned[
149       FeatureTransformer::BufferSize + alignment / sizeof(TransformedFeatureType)];
150
151     auto* transformedFeatures = align_ptr_up<alignment>(&transformedFeaturesUnaligned[0]);
152 #else
153     alignas(alignment)
154       TransformedFeatureType transformedFeatures[FeatureTransformer::BufferSize];
155 #endif
156
157     ASSERT_ALIGNED(transformedFeatures, alignment);
158
159     const int bucket = (pos.count<ALL_PIECES>() - 1) / 4;
160     const auto psqt = featureTransformer->transform(pos, transformedFeatures, bucket);
161     const auto positional = network[bucket]->propagate(transformedFeatures);
162
163     if (complexity)
164         *complexity = abs(psqt - positional) / OutputScale;
165
166     // Give more value to positional evaluation when adjusted flag is set
167     if (adjusted)
168         return static_cast<Value>(((1024 - delta) * psqt + (1024 + delta) * positional) / (1024 * OutputScale));
169     else
170         return static_cast<Value>((psqt + positional) / OutputScale);
171   }
172
173   struct NnueEvalTrace {
174     static_assert(LayerStacks == PSQTBuckets);
175
176     Value psqt[LayerStacks];
177     Value positional[LayerStacks];
178     std::size_t correctBucket;
179   };
180
181   static NnueEvalTrace trace_evaluate(const Position& pos) {
182
183     // We manually align the arrays on the stack because with gcc < 9.3
184     // overaligning stack variables with alignas() doesn't work correctly.
185
186     constexpr uint64_t alignment = CacheLineSize;
187
188 #if defined(ALIGNAS_ON_STACK_VARIABLES_BROKEN)
189     TransformedFeatureType transformedFeaturesUnaligned[
190       FeatureTransformer::BufferSize + alignment / sizeof(TransformedFeatureType)];
191
192     auto* transformedFeatures = align_ptr_up<alignment>(&transformedFeaturesUnaligned[0]);
193 #else
194     alignas(alignment)
195       TransformedFeatureType transformedFeatures[FeatureTransformer::BufferSize];
196 #endif
197
198     ASSERT_ALIGNED(transformedFeatures, alignment);
199
200     NnueEvalTrace t{};
201     t.correctBucket = (pos.count<ALL_PIECES>() - 1) / 4;
202     for (IndexType bucket = 0; bucket < LayerStacks; ++bucket) {
203       const auto materialist = featureTransformer->transform(pos, transformedFeatures, bucket);
204       const auto positional = network[bucket]->propagate(transformedFeatures);
205
206       t.psqt[bucket] = static_cast<Value>( materialist / OutputScale );
207       t.positional[bucket] = static_cast<Value>( positional / OutputScale );
208     }
209
210     return t;
211   }
212
213   static const std::string PieceToChar(" PNBRQK  pnbrqk");
214
215
216   // format_cp_compact() converts a Value into (centi)pawns and writes it in a buffer.
217   // The buffer must have capacity for at least 5 chars.
218   static void format_cp_compact(Value v, char* buffer) {
219
220     buffer[0] = (v < 0 ? '-' : v > 0 ? '+' : ' ');
221
222     int cp = std::abs(100 * v / UCI::NormalizeToPawnValue);
223     if (cp >= 10000)
224     {
225         buffer[1] = '0' + cp / 10000; cp %= 10000;
226         buffer[2] = '0' + cp / 1000; cp %= 1000;
227         buffer[3] = '0' + cp / 100;
228         buffer[4] = ' ';
229     }
230     else if (cp >= 1000)
231     {
232         buffer[1] = '0' + cp / 1000; cp %= 1000;
233         buffer[2] = '0' + cp / 100; cp %= 100;
234         buffer[3] = '.';
235         buffer[4] = '0' + cp / 10;
236     }
237     else
238     {
239         buffer[1] = '0' + cp / 100; cp %= 100;
240         buffer[2] = '.';
241         buffer[3] = '0' + cp / 10; cp %= 10;
242         buffer[4] = '0' + cp / 1;
243     }
244   }
245
246
247   // format_cp_aligned_dot() converts a Value into (centi)pawns, always keeping two decimals.
248   static void format_cp_aligned_dot(Value v, std::stringstream &stream) {
249     const double cp = 1.0 * std::abs(int(v)) / UCI::NormalizeToPawnValue;
250
251     stream << (v < 0 ? '-' : v > 0 ? '+' : ' ')
252            << std::setiosflags(std::ios::fixed)
253            << std::setw(6)
254            << std::setprecision(2)
255            << cp;
256   }
257
258
259   // trace() returns a string with the value of each piece on a board,
260   // and a table for (PSQT, Layers) values bucket by bucket.
261
262   std::string trace(Position& pos) {
263
264     std::stringstream ss;
265
266     char board[3*8+1][8*8+2];
267     std::memset(board, ' ', sizeof(board));
268     for (int row = 0; row < 3*8+1; ++row)
269       board[row][8*8+1] = '\0';
270
271     // A lambda to output one box of the board
272     auto writeSquare = [&board](File file, Rank rank, Piece pc, Value value) {
273
274       const int x = ((int)file) * 8;
275       const int y = (7 - (int)rank) * 3;
276       for (int i = 1; i < 8; ++i)
277          board[y][x+i] = board[y+3][x+i] = '-';
278       for (int i = 1; i < 3; ++i)
279          board[y+i][x] = board[y+i][x+8] = '|';
280       board[y][x] = board[y][x+8] = board[y+3][x+8] = board[y+3][x] = '+';
281       if (pc != NO_PIECE)
282         board[y+1][x+4] = PieceToChar[pc];
283       if (value != VALUE_NONE)
284         format_cp_compact(value, &board[y+2][x+2]);
285     };
286
287     // We estimate the value of each piece by doing a differential evaluation from
288     // the current base eval, simulating the removal of the piece from its square.
289     Value base = evaluate(pos);
290     base = pos.side_to_move() == WHITE ? base : -base;
291
292     for (File f = FILE_A; f <= FILE_H; ++f)
293       for (Rank r = RANK_1; r <= RANK_8; ++r)
294       {
295         Square sq = make_square(f, r);
296         Piece pc = pos.piece_on(sq);
297         Value v = VALUE_NONE;
298
299         if (pc != NO_PIECE && type_of(pc) != KING)
300         {
301           auto st = pos.state();
302
303           pos.remove_piece(sq);
304           st->accumulator.computed[WHITE] = false;
305           st->accumulator.computed[BLACK] = false;
306
307           Value eval = evaluate(pos);
308           eval = pos.side_to_move() == WHITE ? eval : -eval;
309           v = base - eval;
310
311           pos.put_piece(pc, sq);
312           st->accumulator.computed[WHITE] = false;
313           st->accumulator.computed[BLACK] = false;
314         }
315
316         writeSquare(f, r, pc, v);
317       }
318
319     ss << " NNUE derived piece values:\n";
320     for (int row = 0; row < 3*8+1; ++row)
321         ss << board[row] << '\n';
322     ss << '\n';
323
324     auto t = trace_evaluate(pos);
325
326     ss << " NNUE network contributions "
327        << (pos.side_to_move() == WHITE ? "(White to move)" : "(Black to move)") << std::endl
328        << "+------------+------------+------------+------------+\n"
329        << "|   Bucket   |  Material  | Positional |   Total    |\n"
330        << "|            |   (PSQT)   |  (Layers)  |            |\n"
331        << "+------------+------------+------------+------------+\n";
332
333     for (std::size_t bucket = 0; bucket < LayerStacks; ++bucket)
334     {
335       ss <<  "|  " << bucket    << "        ";
336       ss << " |  "; format_cp_aligned_dot(t.psqt[bucket], ss); ss << "  "
337          << " |  "; format_cp_aligned_dot(t.positional[bucket], ss); ss << "  "
338          << " |  "; format_cp_aligned_dot(t.psqt[bucket] + t.positional[bucket], ss); ss << "  "
339          << " |";
340       if (bucket == t.correctBucket)
341           ss << " <-- this bucket is used";
342       ss << '\n';
343     }
344
345     ss << "+------------+------------+------------+------------+\n";
346
347     return ss.str();
348   }
349
350
351   // Load eval, from a file stream or a memory stream
352   bool load_eval(std::string name, std::istream& stream) {
353
354     initialize();
355     fileName = name;
356     return read_parameters(stream);
357   }
358
359   // Save eval, to a file stream or a memory stream
360   bool save_eval(std::ostream& stream) {
361
362     if (fileName.empty())
363       return false;
364
365     return write_parameters(stream);
366   }
367
368   /// Save eval, to a file given by its name
369   bool save_eval(const std::optional<std::string>& filename) {
370
371     std::string actualFilename;
372     std::string msg;
373
374     if (filename.has_value())
375         actualFilename = filename.value();
376     else
377     {
378         if (currentEvalFileName != EvalFileDefaultName)
379         {
380              msg = "Failed to export a net. A non-embedded net can only be saved if the filename is specified";
381
382              sync_cout << msg << sync_endl;
383              return false;
384         }
385         actualFilename = EvalFileDefaultName;
386     }
387
388     std::ofstream stream(actualFilename, std::ios_base::binary);
389     bool saved = save_eval(stream);
390
391     msg = saved ? "Network saved successfully to " + actualFilename
392                 : "Failed to export a net";
393
394     sync_cout << msg << sync_endl;
395     return saved;
396   }
397
398
399 } // namespace Stockfish::Eval::NNUE