]> git.sesse.net Git - stockfish/blob - src/nnue/nnue_architecture.h
Update architecture to "SFNNv4". Update network to nn-6877cd24400e.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 "nnue_common.h"
25
26 #include "features/half_ka_v2_hm.h"
27
28 #include "layers/affine_transform.h"
29 #include "layers/clipped_relu.h"
30
31 #include "../misc.h"
32
33 namespace Stockfish::Eval::NNUE {
34
35 // Input features used in evaluation function
36 using FeatureSet = Features::HalfKAv2_hm;
37
38 // Number of input feature dimensions after conversion
39 constexpr IndexType TransformedFeatureDimensions = 1024;
40 constexpr IndexType PSQTBuckets = 8;
41 constexpr IndexType LayerStacks = 8;
42
43 struct Network
44 {
45   static constexpr int FC_0_OUTPUTS = 15;
46   static constexpr int FC_1_OUTPUTS = 32;
47
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;
53
54   // Hash value embedded in the evaluation file
55   static constexpr std::uint32_t get_hash_value() {
56     // input slice hash
57     std::uint32_t hashValue = 0xEC42E90Du;
58     hashValue ^= TransformedFeatureDimensions * 2;
59
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);
65
66     return hashValue;
67   }
68
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;
76     return true;
77   }
78
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;
86     return true;
87   }
88
89   std::int32_t propagate(const TransformedFeatureType* transformedFeatures)
90   {
91     constexpr uint64_t alignment = CacheLineSize;
92
93     struct Buffer
94     {
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;
100     };
101
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);
106 #else
107     alignas(alignment) Buffer buffer;
108 #endif
109
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);
115
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;
120
121 #if defined(ALIGNAS_ON_STACK_VARIABLES_BROKEN)
122     buffer.~Buffer();
123 #endif
124
125     return outputValue;
126   }
127 };
128
129 }  // namespace Stockfish::Eval::NNUE
130
131 #endif // #ifndef NNUE_ARCHITECTURE_H_INCLUDED