2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2022 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 // Input features and network structure used in NNUE evaluation function
21 #ifndef NNUE_ARCHITECTURE_H_INCLUDED
22 #define NNUE_ARCHITECTURE_H_INCLUDED
24 #include "nnue_common.h"
26 #include "features/half_ka_v2_hm.h"
28 #include "layers/affine_transform.h"
29 #include "layers/clipped_relu.h"
33 namespace Stockfish::Eval::NNUE {
35 // Input features used in evaluation function
36 using FeatureSet = Features::HalfKAv2_hm;
38 // Number of input feature dimensions after conversion
39 constexpr IndexType TransformedFeatureDimensions = 1024;
40 constexpr IndexType PSQTBuckets = 8;
41 constexpr IndexType LayerStacks = 8;
45 static constexpr int FC_0_OUTPUTS = 15;
46 static constexpr int FC_1_OUTPUTS = 32;
48 Layers::AffineTransform<TransformedFeatureDimensions, FC_0_OUTPUTS + 1> fc_0;
49 Layers::ClippedReLU<FC_0_OUTPUTS> ac_0;
50 Layers::AffineTransform<FC_0_OUTPUTS, FC_1_OUTPUTS> fc_1;
51 Layers::ClippedReLU<FC_1_OUTPUTS> ac_1;
52 Layers::AffineTransform<FC_1_OUTPUTS, 1> fc_2;
54 // Hash value embedded in the evaluation file
55 static constexpr std::uint32_t get_hash_value() {
57 std::uint32_t hashValue = 0xEC42E90Du;
58 hashValue ^= TransformedFeatureDimensions * 2;
60 hashValue = decltype(fc_0)::get_hash_value(hashValue);
61 hashValue = decltype(ac_0)::get_hash_value(hashValue);
62 hashValue = decltype(fc_1)::get_hash_value(hashValue);
63 hashValue = decltype(ac_1)::get_hash_value(hashValue);
64 hashValue = decltype(fc_2)::get_hash_value(hashValue);
69 // Read network parameters
70 bool read_parameters(std::istream& stream) {
71 if (!fc_0.read_parameters(stream)) return false;
72 if (!ac_0.read_parameters(stream)) return false;
73 if (!fc_1.read_parameters(stream)) return false;
74 if (!ac_1.read_parameters(stream)) return false;
75 if (!fc_2.read_parameters(stream)) return false;
79 // Read network parameters
80 bool write_parameters(std::ostream& stream) const {
81 if (!fc_0.write_parameters(stream)) return false;
82 if (!ac_0.write_parameters(stream)) return false;
83 if (!fc_1.write_parameters(stream)) return false;
84 if (!ac_1.write_parameters(stream)) return false;
85 if (!fc_2.write_parameters(stream)) return false;
89 std::int32_t propagate(const TransformedFeatureType* transformedFeatures)
91 constexpr uint64_t alignment = CacheLineSize;
95 alignas(CacheLineSize) decltype(fc_0)::OutputBuffer fc_0_out;
96 alignas(CacheLineSize) decltype(ac_0)::OutputBuffer ac_0_out;
97 alignas(CacheLineSize) decltype(fc_1)::OutputBuffer fc_1_out;
98 alignas(CacheLineSize) decltype(ac_1)::OutputBuffer ac_1_out;
99 alignas(CacheLineSize) decltype(fc_2)::OutputBuffer fc_2_out;
102 #if defined(ALIGNAS_ON_STACK_VARIABLES_BROKEN)
103 char bufferRaw[sizeof(Buffer) + alignment];
104 char* bufferRawAligned = align_ptr_up<alignment>(&bufferRaw[0]);
105 Buffer& buffer = *(new (bufferRawAligned) Buffer);
107 alignas(alignment) Buffer buffer;
110 fc_0.propagate(transformedFeatures, buffer.fc_0_out);
111 ac_0.propagate(buffer.fc_0_out, buffer.ac_0_out);
112 fc_1.propagate(buffer.ac_0_out, buffer.fc_1_out);
113 ac_1.propagate(buffer.fc_1_out, buffer.ac_1_out);
114 fc_2.propagate(buffer.ac_1_out, buffer.fc_2_out);
116 // buffer.fc_0_out[FC_0_OUTPUTS] is such that 1.0 is equal to 127*(1<<WeightScaleBits) in quantized form
117 // but we want 1.0 to be equal to 600*OutputScale
118 std::int32_t fwdOut = int(buffer.fc_0_out[FC_0_OUTPUTS]) * (600*OutputScale) / (127*(1<<WeightScaleBits));
119 std::int32_t outputValue = buffer.fc_2_out[0] + fwdOut;
121 #if defined(ALIGNAS_ON_STACK_VARIABLES_BROKEN)
129 } // namespace Stockfish::Eval::NNUE
131 #endif // #ifndef NNUE_ARCHITECTURE_H_INCLUDED