2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2020 The Stockfish developers (see AUTHORS file)
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.
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.
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/>.
19 // Code for calculating NNUE evaluation function
24 #include "../evaluate.h"
25 #include "../position.h"
29 #include "evaluate_nnue.h"
31 namespace Eval::NNUE {
33 const uint32_t kpp_board_index[PIECE_NB][COLOR_NB] = {
34 // convention: W - us, B - them
35 // viewed from other side, W and B are reversed
37 { PS_W_PAWN, PS_B_PAWN },
38 { PS_W_KNIGHT, PS_B_KNIGHT },
39 { PS_W_BISHOP, PS_B_BISHOP },
40 { PS_W_ROOK, PS_B_ROOK },
41 { PS_W_QUEEN, PS_B_QUEEN },
42 { PS_W_KING, PS_B_KING },
45 { PS_B_PAWN, PS_W_PAWN },
46 { PS_B_KNIGHT, PS_W_KNIGHT },
47 { PS_B_BISHOP, PS_W_BISHOP },
48 { PS_B_ROOK, PS_W_ROOK },
49 { PS_B_QUEEN, PS_W_QUEEN },
50 { PS_B_KING, PS_W_KING },
54 // Input feature converter
55 LargePagePtr<FeatureTransformer> feature_transformer;
57 // Evaluation function
58 AlignedPtr<Network> network;
60 // Evaluation function file name
65 // Initialize the evaluation function parameters
67 void Initialize(AlignedPtr<T>& pointer) {
69 pointer.reset(reinterpret_cast<T*>(std_aligned_alloc(alignof(T), sizeof(T))));
70 std::memset(pointer.get(), 0, sizeof(T));
74 void Initialize(LargePagePtr<T>& pointer) {
76 static_assert(alignof(T) <= 4096, "aligned_large_pages_alloc() may fail for such a big alignment requirement of T");
77 pointer.reset(reinterpret_cast<T*>(aligned_large_pages_alloc(sizeof(T))));
78 std::memset(pointer.get(), 0, sizeof(T));
81 // Read evaluation function parameters
83 bool ReadParameters(std::istream& stream, T& reference) {
86 header = read_little_endian<std::uint32_t>(stream);
87 if (!stream || header != T::GetHashValue()) return false;
88 return reference.ReadParameters(stream);
93 // Initialize the evaluation function parameters
96 Detail::Initialize(feature_transformer);
97 Detail::Initialize(network);
100 // Read network header
101 bool ReadHeader(std::istream& stream, std::uint32_t* hash_value, std::string* architecture)
103 std::uint32_t version, size;
105 version = read_little_endian<std::uint32_t>(stream);
106 *hash_value = read_little_endian<std::uint32_t>(stream);
107 size = read_little_endian<std::uint32_t>(stream);
108 if (!stream || version != kVersion) return false;
109 architecture->resize(size);
110 stream.read(&(*architecture)[0], size);
111 return !stream.fail();
114 // Read network parameters
115 bool ReadParameters(std::istream& stream) {
117 std::uint32_t hash_value;
118 std::string architecture;
119 if (!ReadHeader(stream, &hash_value, &architecture)) return false;
120 if (hash_value != kHashValue) return false;
121 if (!Detail::ReadParameters(stream, *feature_transformer)) return false;
122 if (!Detail::ReadParameters(stream, *network)) return false;
123 return stream && stream.peek() == std::ios::traits_type::eof();
126 // Evaluation function. Perform differential calculation.
127 Value evaluate(const Position& pos) {
129 alignas(kCacheLineSize) TransformedFeatureType
130 transformed_features[FeatureTransformer::kBufferSize];
131 feature_transformer->Transform(pos, transformed_features);
132 alignas(kCacheLineSize) char buffer[Network::kBufferSize];
133 const auto output = network->Propagate(transformed_features, buffer);
135 return static_cast<Value>(output[0] / FV_SCALE);
138 // Load eval, from a file stream or a memory stream
139 bool load_eval(std::string name, std::istream& stream) {
143 return ReadParameters(stream);
146 } // namespace Eval::NNUE