simplying hybrid condition
[stockfish] / src / nnue / nnue_feature_transformer.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2020 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 // A class that converts the input features of the NNUE evaluation function
20
21 #ifndef NNUE_FEATURE_TRANSFORMER_H_INCLUDED
22 #define NNUE_FEATURE_TRANSFORMER_H_INCLUDED
23
24 #include "nnue_common.h"
25 #include "nnue_architecture.h"
26 #include "features/index_list.h"
27
28 #include <cstring> // std::memset()
29
30 namespace Eval::NNUE {
31
32   // Input feature converter
33   class FeatureTransformer {
34
35    private:
36     // Number of output dimensions for one side
37     static constexpr IndexType kHalfDimensions = kTransformedFeatureDimensions;
38
39    public:
40     // Output type
41     using OutputType = TransformedFeatureType;
42
43     // Number of input/output dimensions
44     static constexpr IndexType kInputDimensions = RawFeatures::kDimensions;
45     static constexpr IndexType kOutputDimensions = kHalfDimensions * 2;
46
47     // Size of forward propagation buffer
48     static constexpr std::size_t kBufferSize =
49         kOutputDimensions * sizeof(OutputType);
50
51     // Hash value embedded in the evaluation file
52     static constexpr std::uint32_t GetHashValue() {
53       return RawFeatures::kHashValue ^ kOutputDimensions;
54     }
55
56     // Read network parameters
57     bool ReadParameters(std::istream& stream) {
58       stream.read(reinterpret_cast<char*>(biases_),
59                   kHalfDimensions * sizeof(BiasType));
60       stream.read(reinterpret_cast<char*>(weights_),
61                   kHalfDimensions * kInputDimensions * sizeof(WeightType));
62       return !stream.fail();
63     }
64
65     // Proceed with the difference calculation if possible
66     bool UpdateAccumulatorIfPossible(const Position& pos) const {
67       const auto now = pos.state();
68       if (now->accumulator.computed_accumulation) {
69         return true;
70       }
71       const auto prev = now->previous;
72       if (prev && prev->accumulator.computed_accumulation) {
73         UpdateAccumulator(pos);
74         return true;
75       }
76       return false;
77     }
78
79     // Convert input features
80     void Transform(const Position& pos, OutputType* output, bool refresh) const {
81       if (refresh || !UpdateAccumulatorIfPossible(pos)) {
82         RefreshAccumulator(pos);
83       }
84       const auto& accumulation = pos.state()->accumulator.accumulation;
85
86   #if defined(USE_AVX2)
87       constexpr IndexType kNumChunks = kHalfDimensions / kSimdWidth;
88       constexpr int kControl = 0b11011000;
89       const __m256i kZero = _mm256_setzero_si256();
90
91   #elif defined(USE_SSSE3)
92       constexpr IndexType kNumChunks = kHalfDimensions / kSimdWidth;
93
94   #ifdef USE_SSE41
95       const __m128i kZero = _mm_setzero_si128();
96   #else
97       const __m128i k0x80s = _mm_set1_epi8(-128);
98   #endif
99
100   #elif defined(USE_NEON)
101       constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth / 2);
102       const int8x8_t kZero = {0};
103   #endif
104
105       const Color perspectives[2] = {pos.side_to_move(), ~pos.side_to_move()};
106       for (IndexType p = 0; p < 2; ++p) {
107         const IndexType offset = kHalfDimensions * p;
108
109   #if defined(USE_AVX2)
110         auto out = reinterpret_cast<__m256i*>(&output[offset]);
111         for (IndexType j = 0; j < kNumChunks; ++j) {
112           __m256i sum0 =
113
114   #if defined(__MINGW32__) || defined(__MINGW64__)
115             // HACK: Use _mm256_loadu_si256() instead of _mm256_load_si256. Because the binary
116             //       compiled with g++ in MSYS2 crashes here because the output memory is not aligned
117             //       even though alignas is specified.
118             _mm256_loadu_si256
119   #else
120             _mm256_load_si256
121   #endif
122
123             (&reinterpret_cast<const __m256i*>(
124               accumulation[perspectives[p]][0])[j * 2 + 0]);
125           __m256i sum1 =
126
127   #if defined(__MINGW32__) || defined(__MINGW64__)
128             _mm256_loadu_si256
129   #else
130             _mm256_load_si256
131   #endif
132
133             (&reinterpret_cast<const __m256i*>(
134               accumulation[perspectives[p]][0])[j * 2 + 1]);
135
136   #if defined(__MINGW32__) || defined(__MINGW64__)
137           _mm256_storeu_si256
138   #else
139           _mm256_store_si256
140   #endif
141
142           (&out[j], _mm256_permute4x64_epi64(_mm256_max_epi8(
143               _mm256_packs_epi16(sum0, sum1), kZero), kControl));
144         }
145
146   #elif defined(USE_SSSE3)
147         auto out = reinterpret_cast<__m128i*>(&output[offset]);
148         for (IndexType j = 0; j < kNumChunks; ++j) {
149           __m128i sum0 = _mm_load_si128(&reinterpret_cast<const __m128i*>(
150               accumulation[perspectives[p]][0])[j * 2 + 0]);
151           __m128i sum1 = _mm_load_si128(&reinterpret_cast<const __m128i*>(
152               accumulation[perspectives[p]][0])[j * 2 + 1]);
153       const __m128i packedbytes = _mm_packs_epi16(sum0, sum1);
154
155           _mm_store_si128(&out[j],
156
157   #ifdef USE_SSE41
158             _mm_max_epi8(packedbytes, kZero)
159   #else
160             _mm_subs_epi8(_mm_adds_epi8(packedbytes, k0x80s), k0x80s)
161   #endif
162
163           );
164         }
165
166   #elif defined(USE_NEON)
167         const auto out = reinterpret_cast<int8x8_t*>(&output[offset]);
168         for (IndexType j = 0; j < kNumChunks; ++j) {
169           int16x8_t sum = reinterpret_cast<const int16x8_t*>(
170               accumulation[perspectives[p]][0])[j];
171           out[j] = vmax_s8(vqmovn_s16(sum), kZero);
172         }
173
174   #else
175         for (IndexType j = 0; j < kHalfDimensions; ++j) {
176           BiasType sum = accumulation[static_cast<int>(perspectives[p])][0][j];
177           output[offset + j] = static_cast<OutputType>(
178               std::max<int>(0, std::min<int>(127, sum)));
179         }
180   #endif
181
182       }
183     }
184
185    private:
186     // Calculate cumulative value without using difference calculation
187     void RefreshAccumulator(const Position& pos) const {
188       auto& accumulator = pos.state()->accumulator;
189       IndexType i = 0;
190       Features::IndexList active_indices[2];
191       RawFeatures::AppendActiveIndices(pos, kRefreshTriggers[i],
192                                        active_indices);
193       for (Color perspective : { WHITE, BLACK }) {
194         std::memcpy(accumulator.accumulation[perspective][i], biases_,
195                    kHalfDimensions * sizeof(BiasType));
196         for (const auto index : active_indices[perspective]) {
197           const IndexType offset = kHalfDimensions * index;
198
199   #if defined(USE_AVX2)
200           auto accumulation = reinterpret_cast<__m256i*>(
201               &accumulator.accumulation[perspective][i][0]);
202           auto column = reinterpret_cast<const __m256i*>(&weights_[offset]);
203           constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth / 2);
204           for (IndexType j = 0; j < kNumChunks; ++j) {
205   #if defined(__MINGW32__) || defined(__MINGW64__)
206             _mm256_storeu_si256(&accumulation[j], _mm256_add_epi16(_mm256_loadu_si256(&accumulation[j]), column[j]));
207   #else
208             accumulation[j] = _mm256_add_epi16(accumulation[j], column[j]);
209   #endif
210           }
211
212   #elif defined(USE_SSE2)
213           auto accumulation = reinterpret_cast<__m128i*>(
214               &accumulator.accumulation[perspective][i][0]);
215           auto column = reinterpret_cast<const __m128i*>(&weights_[offset]);
216           constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth / 2);
217           for (IndexType j = 0; j < kNumChunks; ++j) {
218             accumulation[j] = _mm_add_epi16(accumulation[j], column[j]);
219           }
220
221   #elif defined(USE_NEON)
222           auto accumulation = reinterpret_cast<int16x8_t*>(
223               &accumulator.accumulation[perspective][i][0]);
224           auto column = reinterpret_cast<const int16x8_t*>(&weights_[offset]);
225           constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth / 2);
226           for (IndexType j = 0; j < kNumChunks; ++j) {
227             accumulation[j] = vaddq_s16(accumulation[j], column[j]);
228           }
229
230   #else
231           for (IndexType j = 0; j < kHalfDimensions; ++j) {
232             accumulator.accumulation[perspective][i][j] += weights_[offset + j];
233           }
234   #endif
235
236         }
237       }
238
239       accumulator.computed_accumulation = true;
240       accumulator.computed_score = false;
241     }
242
243     // Calculate cumulative value using difference calculation
244     void UpdateAccumulator(const Position& pos) const {
245       const auto prev_accumulator = pos.state()->previous->accumulator;
246       auto& accumulator = pos.state()->accumulator;
247       IndexType i = 0;
248       Features::IndexList removed_indices[2], added_indices[2];
249       bool reset[2];
250       RawFeatures::AppendChangedIndices(pos, kRefreshTriggers[i],
251                                         removed_indices, added_indices, reset);
252       for (Color perspective : { WHITE, BLACK }) {
253
254   #if defined(USE_AVX2)
255         constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth / 2);
256         auto accumulation = reinterpret_cast<__m256i*>(
257             &accumulator.accumulation[perspective][i][0]);
258
259   #elif defined(USE_SSE2)
260         constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth / 2);
261         auto accumulation = reinterpret_cast<__m128i*>(
262             &accumulator.accumulation[perspective][i][0]);
263
264   #elif defined(USE_NEON)
265         constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth / 2);
266         auto accumulation = reinterpret_cast<int16x8_t*>(
267             &accumulator.accumulation[perspective][i][0]);
268   #endif
269
270         if (reset[perspective]) {
271           std::memcpy(accumulator.accumulation[perspective][i], biases_,
272                       kHalfDimensions * sizeof(BiasType));
273         } else {
274           std::memcpy(accumulator.accumulation[perspective][i],
275                       prev_accumulator.accumulation[perspective][i],
276                       kHalfDimensions * sizeof(BiasType));
277           // Difference calculation for the deactivated features
278           for (const auto index : removed_indices[perspective]) {
279             const IndexType offset = kHalfDimensions * index;
280
281   #if defined(USE_AVX2)
282             auto column = reinterpret_cast<const __m256i*>(&weights_[offset]);
283             for (IndexType j = 0; j < kNumChunks; ++j) {
284               accumulation[j] = _mm256_sub_epi16(accumulation[j], column[j]);
285             }
286
287   #elif defined(USE_SSE2)
288             auto column = reinterpret_cast<const __m128i*>(&weights_[offset]);
289             for (IndexType j = 0; j < kNumChunks; ++j) {
290               accumulation[j] = _mm_sub_epi16(accumulation[j], column[j]);
291             }
292
293   #elif defined(USE_NEON)
294             auto column = reinterpret_cast<const int16x8_t*>(&weights_[offset]);
295             for (IndexType j = 0; j < kNumChunks; ++j) {
296               accumulation[j] = vsubq_s16(accumulation[j], column[j]);
297             }
298
299   #else
300             for (IndexType j = 0; j < kHalfDimensions; ++j) {
301               accumulator.accumulation[perspective][i][j] -=
302                   weights_[offset + j];
303             }
304   #endif
305
306           }
307         }
308         { // Difference calculation for the activated features
309           for (const auto index : added_indices[perspective]) {
310             const IndexType offset = kHalfDimensions * index;
311
312   #if defined(USE_AVX2)
313             auto column = reinterpret_cast<const __m256i*>(&weights_[offset]);
314             for (IndexType j = 0; j < kNumChunks; ++j) {
315               accumulation[j] = _mm256_add_epi16(accumulation[j], column[j]);
316             }
317
318   #elif defined(USE_SSE2)
319             auto column = reinterpret_cast<const __m128i*>(&weights_[offset]);
320             for (IndexType j = 0; j < kNumChunks; ++j) {
321               accumulation[j] = _mm_add_epi16(accumulation[j], column[j]);
322             }
323
324   #elif defined(USE_NEON)
325             auto column = reinterpret_cast<const int16x8_t*>(&weights_[offset]);
326             for (IndexType j = 0; j < kNumChunks; ++j) {
327               accumulation[j] = vaddq_s16(accumulation[j], column[j]);
328             }
329
330   #else
331             for (IndexType j = 0; j < kHalfDimensions; ++j) {
332               accumulator.accumulation[perspective][i][j] +=
333                   weights_[offset + j];
334             }
335   #endif
336
337           }
338         }
339       }
340
341       accumulator.computed_accumulation = true;
342       accumulator.computed_score = false;
343     }
344
345     using BiasType = std::int16_t;
346     using WeightType = std::int16_t;
347
348     alignas(kCacheLineSize) BiasType biases_[kHalfDimensions];
349     alignas(kCacheLineSize)
350         WeightType weights_[kHalfDimensions * kInputDimensions];
351   };
352
353 }  // namespace Eval::NNUE
354
355 #endif // #ifndef NNUE_FEATURE_TRANSFORMER_H_INCLUDED