2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2021 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"
30 #include "evaluate_nnue.h"
32 namespace Stockfish::Eval::NNUE {
34 // Input feature converter
35 LargePagePtr<FeatureTransformer> featureTransformer;
37 // Evaluation function
38 AlignedPtr<Network> network[LayerStacks];
40 // Evaluation function file name
42 std::string netDescription;
46 // Initialize the evaluation function parameters
48 void initialize(AlignedPtr<T>& pointer) {
50 pointer.reset(reinterpret_cast<T*>(std_aligned_alloc(alignof(T), sizeof(T))));
51 std::memset(pointer.get(), 0, sizeof(T));
55 void initialize(LargePagePtr<T>& pointer) {
57 static_assert(alignof(T) <= 4096, "aligned_large_pages_alloc() may fail for such a big alignment requirement of T");
58 pointer.reset(reinterpret_cast<T*>(aligned_large_pages_alloc(sizeof(T))));
59 std::memset(pointer.get(), 0, sizeof(T));
62 // Read evaluation function parameters
64 bool read_parameters(std::istream& stream, T& reference) {
67 header = read_little_endian<std::uint32_t>(stream);
68 if (!stream || header != T::get_hash_value()) return false;
69 return reference.read_parameters(stream);
72 // Write evaluation function parameters
74 bool write_parameters(std::ostream& stream, const T& reference) {
76 write_little_endian<std::uint32_t>(stream, T::get_hash_value());
77 return reference.write_parameters(stream);
82 // Initialize the evaluation function parameters
85 Detail::initialize(featureTransformer);
86 for (std::size_t i = 0; i < LayerStacks; ++i)
87 Detail::initialize(network[i]);
90 // Read network header
91 bool read_header(std::istream& stream, std::uint32_t* hashValue, std::string* desc)
93 std::uint32_t version, size;
95 version = read_little_endian<std::uint32_t>(stream);
96 *hashValue = read_little_endian<std::uint32_t>(stream);
97 size = read_little_endian<std::uint32_t>(stream);
98 if (!stream || version != Version) return false;
100 stream.read(&(*desc)[0], size);
101 return !stream.fail();
104 // Write network header
105 bool write_header(std::ostream& stream, std::uint32_t hashValue, const std::string& desc)
107 write_little_endian<std::uint32_t>(stream, Version);
108 write_little_endian<std::uint32_t>(stream, hashValue);
109 write_little_endian<std::uint32_t>(stream, desc.size());
110 stream.write(&desc[0], desc.size());
111 return !stream.fail();
114 // Read network parameters
115 bool read_parameters(std::istream& stream) {
117 std::uint32_t hashValue;
118 if (!read_header(stream, &hashValue, &netDescription)) return false;
119 if (hashValue != HashValue) return false;
120 if (!Detail::read_parameters(stream, *featureTransformer)) return false;
121 for (std::size_t i = 0; i < LayerStacks; ++i)
122 if (!Detail::read_parameters(stream, *(network[i]))) return false;
123 return stream && stream.peek() == std::ios::traits_type::eof();
126 // Write network parameters
127 bool write_parameters(std::ostream& stream) {
129 if (!write_header(stream, HashValue, netDescription)) return false;
130 if (!Detail::write_parameters(stream, *featureTransformer)) return false;
131 for (std::size_t i = 0; i < LayerStacks; ++i)
132 if (!Detail::write_parameters(stream, *(network[i]))) return false;
136 // Evaluation function. Perform differential calculation.
137 Value evaluate(const Position& pos, bool adjusted) {
139 // We manually align the arrays on the stack because with gcc < 9.3
140 // overaligning stack variables with alignas() doesn't work correctly.
142 constexpr uint64_t alignment = CacheLineSize;
144 #if defined(ALIGNAS_ON_STACK_VARIABLES_BROKEN)
145 TransformedFeatureType transformedFeaturesUnaligned[
146 FeatureTransformer::BufferSize + alignment / sizeof(TransformedFeatureType)];
147 char bufferUnaligned[Network::BufferSize + alignment];
149 auto* transformedFeatures = align_ptr_up<alignment>(&transformedFeaturesUnaligned[0]);
150 auto* buffer = align_ptr_up<alignment>(&bufferUnaligned[0]);
153 TransformedFeatureType transformedFeatures[FeatureTransformer::BufferSize];
154 alignas(alignment) char buffer[Network::BufferSize];
157 ASSERT_ALIGNED(transformedFeatures, alignment);
158 ASSERT_ALIGNED(buffer, alignment);
160 const std::size_t bucket = (pos.count<ALL_PIECES>() - 1) / 4;
161 const auto [psqt, lazy] = featureTransformer->transform(pos, transformedFeatures, bucket);
164 return static_cast<Value>(psqt / OutputScale);
167 const auto output = network[bucket]->propagate(transformedFeatures, buffer);
169 int materialist = psqt;
170 int positional = output[0];
172 int delta_npm = abs(pos.non_pawn_material(WHITE) - pos.non_pawn_material(BLACK));
173 int entertainment = (adjusted && delta_npm <= BishopValueMg - KnightValueMg ? 7 : 0);
175 int A = 128 - entertainment;
176 int B = 128 + entertainment;
178 int sum = (A * materialist + B * positional) / 128;
180 return static_cast<Value>( sum / OutputScale );
184 // Load eval, from a file stream or a memory stream
185 bool load_eval(std::string name, std::istream& stream) {
189 return read_parameters(stream);
192 // Save eval, to a file stream or a memory stream
193 bool save_eval(std::ostream& stream) {
195 if (fileName.empty())
198 return write_parameters(stream);
201 } // namespace Stockfish::Eval::NNUE