]> git.sesse.net Git - stockfish/blob - src/nnue/nnue_architecture.h
Update NNUE architecture to SFNNv5. Update network to nn-3c0aa92af1da.nnue.
[stockfish] / src / nnue / nnue_architecture.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2022 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 // Input features and network structure used in NNUE evaluation function
20
21 #ifndef NNUE_ARCHITECTURE_H_INCLUDED
22 #define NNUE_ARCHITECTURE_H_INCLUDED
23
24 #include <memory>
25
26 #include "nnue_common.h"
27
28 #include "features/half_ka_v2_hm.h"
29
30 #include "layers/affine_transform.h"
31 #include "layers/clipped_relu.h"
32 #include "layers/sqr_clipped_relu.h"
33
34 #include "../misc.h"
35
36 namespace Stockfish::Eval::NNUE {
37
38 // Input features used in evaluation function
39 using FeatureSet = Features::HalfKAv2_hm;
40
41 // Number of input feature dimensions after conversion
42 constexpr IndexType TransformedFeatureDimensions = 1024;
43 constexpr IndexType PSQTBuckets = 8;
44 constexpr IndexType LayerStacks = 8;
45
46 struct Network
47 {
48   static constexpr int FC_0_OUTPUTS = 15;
49   static constexpr int FC_1_OUTPUTS = 32;
50
51   Layers::AffineTransform<TransformedFeatureDimensions, FC_0_OUTPUTS + 1> fc_0;
52   Layers::SqrClippedReLU<FC_0_OUTPUTS + 1> ac_sqr_0;
53   Layers::ClippedReLU<FC_0_OUTPUTS + 1> ac_0;
54   Layers::AffineTransform<FC_0_OUTPUTS * 2, FC_1_OUTPUTS> fc_1;
55   Layers::ClippedReLU<FC_1_OUTPUTS> ac_1;
56   Layers::AffineTransform<FC_1_OUTPUTS, 1> fc_2;
57
58   // Hash value embedded in the evaluation file
59   static constexpr std::uint32_t get_hash_value() {
60     // input slice hash
61     std::uint32_t hashValue = 0xEC42E90Du;
62     hashValue ^= TransformedFeatureDimensions * 2;
63
64     hashValue = decltype(fc_0)::get_hash_value(hashValue);
65     hashValue = decltype(ac_0)::get_hash_value(hashValue);
66     hashValue = decltype(fc_1)::get_hash_value(hashValue);
67     hashValue = decltype(ac_1)::get_hash_value(hashValue);
68     hashValue = decltype(fc_2)::get_hash_value(hashValue);
69
70     return hashValue;
71   }
72
73   // Read network parameters
74   bool read_parameters(std::istream& stream) {
75     if (!fc_0.read_parameters(stream)) return false;
76     if (!ac_0.read_parameters(stream)) return false;
77     if (!fc_1.read_parameters(stream)) return false;
78     if (!ac_1.read_parameters(stream)) return false;
79     if (!fc_2.read_parameters(stream)) return false;
80     return true;
81   }
82
83   // Read network parameters
84   bool write_parameters(std::ostream& stream) const {
85     if (!fc_0.write_parameters(stream)) return false;
86     if (!ac_0.write_parameters(stream)) return false;
87     if (!fc_1.write_parameters(stream)) return false;
88     if (!ac_1.write_parameters(stream)) return false;
89     if (!fc_2.write_parameters(stream)) return false;
90     return true;
91   }
92
93   std::int32_t propagate(const TransformedFeatureType* transformedFeatures)
94   {
95     struct alignas(CacheLineSize) Buffer
96     {
97       alignas(CacheLineSize) decltype(fc_0)::OutputBuffer fc_0_out;
98       alignas(CacheLineSize) decltype(ac_sqr_0)::OutputType ac_sqr_0_out[ceil_to_multiple<IndexType>(FC_0_OUTPUTS * 2, 32)];
99       alignas(CacheLineSize) decltype(ac_0)::OutputBuffer ac_0_out;
100       alignas(CacheLineSize) decltype(fc_1)::OutputBuffer fc_1_out;
101       alignas(CacheLineSize) decltype(ac_1)::OutputBuffer ac_1_out;
102       alignas(CacheLineSize) decltype(fc_2)::OutputBuffer fc_2_out;
103
104       Buffer()
105       {
106           std::memset(this, 0, sizeof(*this));
107       }
108     };
109
110 #if defined(__clang__) && (__APPLE__)
111     // workaround for a bug reported with xcode 12
112     static thread_local auto tlsBuffer = std::make_unique<Buffer>();
113     // Access TLS only once, cache result.
114     Buffer& buffer = *tlsBuffer;
115 #else
116     alignas(CacheLineSize) static thread_local Buffer buffer;
117 #endif
118
119     fc_0.propagate(transformedFeatures, buffer.fc_0_out);
120     ac_sqr_0.propagate(buffer.fc_0_out, buffer.ac_sqr_0_out);
121     ac_0.propagate(buffer.fc_0_out, buffer.ac_0_out);
122     std::memcpy(buffer.ac_sqr_0_out + FC_0_OUTPUTS, buffer.ac_0_out, FC_0_OUTPUTS * sizeof(decltype(ac_0)::OutputType));
123     fc_1.propagate(buffer.ac_sqr_0_out, buffer.fc_1_out);
124     ac_1.propagate(buffer.fc_1_out, buffer.ac_1_out);
125     fc_2.propagate(buffer.ac_1_out, buffer.fc_2_out);
126
127     // buffer.fc_0_out[FC_0_OUTPUTS] is such that 1.0 is equal to 127*(1<<WeightScaleBits) in quantized form
128     // but we want 1.0 to be equal to 600*OutputScale
129     std::int32_t fwdOut = int(buffer.fc_0_out[FC_0_OUTPUTS]) * (600*OutputScale) / (127*(1<<WeightScaleBits));
130     std::int32_t outputValue = buffer.fc_2_out[0] + fwdOut;
131
132     return outputValue;
133   }
134 };
135
136 }  // namespace Stockfish::Eval::NNUE
137
138 #endif // #ifndef NNUE_ARCHITECTURE_H_INCLUDED