]> git.sesse.net Git - stockfish/blob - src/nnue/evaluate_nnue.cpp
Try to match relative magnitude of NNUE eval to classical
[stockfish] / src / nnue / evaluate_nnue.cpp
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2020 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 <fstream>
22 #include <iostream>
23 #include <set>
24
25 #include "../evaluate.h"
26 #include "../position.h"
27 #include "../misc.h"
28 #include "../uci.h"
29
30 #include "evaluate_nnue.h"
31
32 ExtPieceSquare kpp_board_index[PIECE_NB] = {
33  // convention: W - us, B - them
34  // viewed from other side, W and B are reversed
35     { PS_NONE,     PS_NONE     },
36     { PS_W_PAWN,   PS_B_PAWN   },
37     { PS_W_KNIGHT, PS_B_KNIGHT },
38     { PS_W_BISHOP, PS_B_BISHOP },
39     { PS_W_ROOK,   PS_B_ROOK   },
40     { PS_W_QUEEN,  PS_B_QUEEN  },
41     { PS_W_KING,   PS_B_KING   },
42     { PS_NONE,     PS_NONE     },
43     { PS_NONE,     PS_NONE     },
44     { PS_B_PAWN,   PS_W_PAWN   },
45     { PS_B_KNIGHT, PS_W_KNIGHT },
46     { PS_B_BISHOP, PS_W_BISHOP },
47     { PS_B_ROOK,   PS_W_ROOK   },
48     { PS_B_QUEEN,  PS_W_QUEEN  },
49     { PS_B_KING,   PS_W_KING   },
50     { PS_NONE,     PS_NONE     }
51 };
52
53
54 namespace Eval::NNUE {
55
56   // Input feature converter
57   AlignedPtr<FeatureTransformer> feature_transformer;
58
59   // Evaluation function
60   AlignedPtr<Network> network;
61
62   // Evaluation function file name
63   std::string fileName;
64
65   namespace Detail {
66
67   // Initialize the evaluation function parameters
68   template <typename T>
69   void Initialize(AlignedPtr<T>& pointer) {
70
71     pointer.reset(reinterpret_cast<T*>(std_aligned_alloc(alignof(T), sizeof(T))));
72     std::memset(pointer.get(), 0, sizeof(T));
73   }
74
75   // Read evaluation function parameters
76   template <typename T>
77   bool ReadParameters(std::istream& stream, const AlignedPtr<T>& pointer) {
78
79     std::uint32_t header;
80     stream.read(reinterpret_cast<char*>(&header), sizeof(header));
81     if (!stream || header != T::GetHashValue()) return false;
82     return pointer->ReadParameters(stream);
83   }
84
85   }  // namespace Detail
86
87   // Initialize the evaluation function parameters
88   void Initialize() {
89
90     Detail::Initialize(feature_transformer);
91     Detail::Initialize(network);
92   }
93
94   // Read network header
95   bool ReadHeader(std::istream& stream,
96     std::uint32_t* hash_value, std::string* architecture) {
97
98     std::uint32_t version, size;
99     stream.read(reinterpret_cast<char*>(&version), sizeof(version));
100     stream.read(reinterpret_cast<char*>(hash_value), sizeof(*hash_value));
101     stream.read(reinterpret_cast<char*>(&size), sizeof(size));
102     if (!stream || version != kVersion) return false;
103     architecture->resize(size);
104     stream.read(&(*architecture)[0], size);
105     return !stream.fail();
106   }
107
108   // Read network parameters
109   bool ReadParameters(std::istream& stream) {
110
111     std::uint32_t hash_value;
112     std::string architecture;
113     if (!ReadHeader(stream, &hash_value, &architecture)) return false;
114     if (hash_value != kHashValue) return false;
115     if (!Detail::ReadParameters(stream, feature_transformer)) return false;
116     if (!Detail::ReadParameters(stream, network)) return false;
117     return stream && stream.peek() == std::ios::traits_type::eof();
118   }
119
120   // Proceed with the difference calculation if possible
121   static void UpdateAccumulatorIfPossible(const Position& pos) {
122
123     feature_transformer->UpdateAccumulatorIfPossible(pos);
124   }
125
126   // Calculate the evaluation value
127   static Value ComputeScore(const Position& pos, bool refresh) {
128
129     auto& accumulator = pos.state()->accumulator;
130     if (!refresh && accumulator.computed_score) {
131       return accumulator.score;
132     }
133
134     alignas(kCacheLineSize) TransformedFeatureType
135         transformed_features[FeatureTransformer::kBufferSize];
136     feature_transformer->Transform(pos, transformed_features, refresh);
137     alignas(kCacheLineSize) char buffer[Network::kBufferSize];
138     const auto output = network->Propagate(transformed_features, buffer);
139
140     auto score = static_cast<Value>(output[0] / FV_SCALE);
141
142     accumulator.score = score;
143     accumulator.computed_score = true;
144     return accumulator.score;
145   }
146
147   // Load the evaluation function file
148   bool load_eval_file(const std::string& evalFile) {
149
150     Initialize();
151     fileName = evalFile;
152
153     std::ifstream stream(evalFile, std::ios::binary);
154
155     const bool result = ReadParameters(stream);
156
157     return result;
158   }
159
160   // Evaluation function. Perform differential calculation.
161   Value evaluate(const Position& pos) {
162     return ComputeScore(pos, false);
163   }
164
165   // Evaluation function. Perform full calculation.
166   Value compute_eval(const Position& pos) {
167     return ComputeScore(pos, true);
168   }
169
170   // Proceed with the difference calculation if possible
171   void update_eval(const Position& pos) {
172     UpdateAccumulatorIfPossible(pos);
173   }
174
175 } // namespace Eval::NNUE