Provide vectorized NNUE code for SSE2 and MMX targets
[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_SSE2)
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_MMX)
101       constexpr IndexType kNumChunks = kHalfDimensions / kSimdWidth;
102       const __m64 k0x80s = _mm_set1_pi8(-128);
103
104   #elif defined(USE_NEON)
105       constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth / 2);
106       const int8x8_t kZero = {0};
107   #endif
108
109       const Color perspectives[2] = {pos.side_to_move(), ~pos.side_to_move()};
110       for (IndexType p = 0; p < 2; ++p) {
111         const IndexType offset = kHalfDimensions * p;
112
113   #if defined(USE_AVX2)
114         auto out = reinterpret_cast<__m256i*>(&output[offset]);
115         for (IndexType j = 0; j < kNumChunks; ++j) {
116           __m256i sum0 = _mm256_loadA_si256(
117               &reinterpret_cast<const __m256i*>(accumulation[perspectives[p]][0])[j * 2 + 0]);
118           __m256i sum1 = _mm256_loadA_si256(
119             &reinterpret_cast<const __m256i*>(accumulation[perspectives[p]][0])[j * 2 + 1]);
120           _mm256_storeA_si256(&out[j], _mm256_permute4x64_epi64(_mm256_max_epi8(
121               _mm256_packs_epi16(sum0, sum1), kZero), kControl));
122         }
123
124   #elif defined(USE_SSE2)
125         auto out = reinterpret_cast<__m128i*>(&output[offset]);
126         for (IndexType j = 0; j < kNumChunks; ++j) {
127           __m128i sum0 = _mm_load_si128(&reinterpret_cast<const __m128i*>(
128               accumulation[perspectives[p]][0])[j * 2 + 0]);
129           __m128i sum1 = _mm_load_si128(&reinterpret_cast<const __m128i*>(
130               accumulation[perspectives[p]][0])[j * 2 + 1]);
131       const __m128i packedbytes = _mm_packs_epi16(sum0, sum1);
132
133           _mm_store_si128(&out[j],
134
135   #ifdef USE_SSE41
136             _mm_max_epi8(packedbytes, kZero)
137   #else
138             _mm_subs_epi8(_mm_adds_epi8(packedbytes, k0x80s), k0x80s)
139   #endif
140
141           );
142         }
143
144   #elif defined(USE_MMX)
145         auto out = reinterpret_cast<__m64*>(&output[offset]);
146         for (IndexType j = 0; j < kNumChunks; ++j) {
147           __m64 sum0 = *(&reinterpret_cast<const __m64*>(
148               accumulation[perspectives[p]][0])[j * 2 + 0]);
149           __m64 sum1 = *(&reinterpret_cast<const __m64*>(
150               accumulation[perspectives[p]][0])[j * 2 + 1]);
151           const __m64 packedbytes = _mm_packs_pi16(sum0, sum1);
152           out[j] = _mm_subs_pi8(_mm_adds_pi8(packedbytes, k0x80s), k0x80s);
153         }
154
155   #elif defined(USE_NEON)
156         const auto out = reinterpret_cast<int8x8_t*>(&output[offset]);
157         for (IndexType j = 0; j < kNumChunks; ++j) {
158           int16x8_t sum = reinterpret_cast<const int16x8_t*>(
159               accumulation[perspectives[p]][0])[j];
160           out[j] = vmax_s8(vqmovn_s16(sum), kZero);
161         }
162
163   #else
164         for (IndexType j = 0; j < kHalfDimensions; ++j) {
165           BiasType sum = accumulation[static_cast<int>(perspectives[p])][0][j];
166           output[offset + j] = static_cast<OutputType>(
167               std::max<int>(0, std::min<int>(127, sum)));
168         }
169   #endif
170
171       }
172   #if defined(USE_MMX)
173       _mm_empty();
174   #endif
175     }
176
177    private:
178     // Calculate cumulative value without using difference calculation
179     void RefreshAccumulator(const Position& pos) const {
180       auto& accumulator = pos.state()->accumulator;
181       IndexType i = 0;
182       Features::IndexList active_indices[2];
183       RawFeatures::AppendActiveIndices(pos, kRefreshTriggers[i],
184                                        active_indices);
185       for (Color perspective : { WHITE, BLACK }) {
186         std::memcpy(accumulator.accumulation[perspective][i], biases_,
187                    kHalfDimensions * sizeof(BiasType));
188         for (const auto index : active_indices[perspective]) {
189           const IndexType offset = kHalfDimensions * index;
190   #if defined(USE_AVX512)
191           auto accumulation = reinterpret_cast<__m512i*>(
192               &accumulator.accumulation[perspective][i][0]);
193           auto column = reinterpret_cast<const __m512i*>(&weights_[offset]);
194           constexpr IndexType kNumChunks = kHalfDimensions / kSimdWidth;
195           for (IndexType j = 0; j < kNumChunks; ++j)
196             _mm512_storeA_si512(&accumulation[j], _mm512_add_epi16(_mm512_loadA_si512(&accumulation[j]), column[j]));
197
198   #elif defined(USE_AVX2)
199           auto accumulation = reinterpret_cast<__m256i*>(
200               &accumulator.accumulation[perspective][i][0]);
201           auto column = reinterpret_cast<const __m256i*>(&weights_[offset]);
202           constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth / 2);
203           for (IndexType j = 0; j < kNumChunks; ++j)
204             _mm256_storeA_si256(&accumulation[j], _mm256_add_epi16(_mm256_loadA_si256(&accumulation[j]), column[j]));
205
206   #elif defined(USE_SSE2)
207           auto accumulation = reinterpret_cast<__m128i*>(
208               &accumulator.accumulation[perspective][i][0]);
209           auto column = reinterpret_cast<const __m128i*>(&weights_[offset]);
210           constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth / 2);
211           for (IndexType j = 0; j < kNumChunks; ++j)
212             accumulation[j] = _mm_add_epi16(accumulation[j], column[j]);
213
214   #elif defined(USE_MMX)
215           auto accumulation = reinterpret_cast<__m64*>(
216               &accumulator.accumulation[perspective][i][0]);
217           auto column = reinterpret_cast<const __m64*>(&weights_[offset]);
218           constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth / 2);
219           for (IndexType j = 0; j < kNumChunks; ++j) {
220             accumulation[j] = _mm_add_pi16(accumulation[j], column[j]);
221           }
222
223   #elif defined(USE_NEON)
224           auto accumulation = reinterpret_cast<int16x8_t*>(
225               &accumulator.accumulation[perspective][i][0]);
226           auto column = reinterpret_cast<const int16x8_t*>(&weights_[offset]);
227           constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth / 2);
228           for (IndexType j = 0; j < kNumChunks; ++j)
229             accumulation[j] = vaddq_s16(accumulation[j], column[j]);
230
231   #else
232           for (IndexType j = 0; j < kHalfDimensions; ++j)
233             accumulator.accumulation[perspective][i][j] += weights_[offset + j];
234   #endif
235
236         }
237       }
238   #if defined(USE_MMX)
239       _mm_empty();
240   #endif
241
242       accumulator.computed_accumulation = true;
243       accumulator.computed_score = false;
244     }
245
246     // Calculate cumulative value using difference calculation
247     void UpdateAccumulator(const Position& pos) const {
248       const auto prev_accumulator = pos.state()->previous->accumulator;
249       auto& accumulator = pos.state()->accumulator;
250       IndexType i = 0;
251       Features::IndexList removed_indices[2], added_indices[2];
252       bool reset[2];
253       RawFeatures::AppendChangedIndices(pos, kRefreshTriggers[i],
254                                         removed_indices, added_indices, reset);
255       for (Color perspective : { WHITE, BLACK }) {
256
257   #if defined(USE_AVX2)
258         constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth / 2);
259         auto accumulation = reinterpret_cast<__m256i*>(
260             &accumulator.accumulation[perspective][i][0]);
261
262   #elif defined(USE_SSE2)
263         constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth / 2);
264         auto accumulation = reinterpret_cast<__m128i*>(
265             &accumulator.accumulation[perspective][i][0]);
266
267   #elif defined(USE_MMX)
268         constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth / 2);
269         auto accumulation = reinterpret_cast<__m64*>(
270             &accumulator.accumulation[perspective][i][0]);
271
272   #elif defined(USE_NEON)
273         constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth / 2);
274         auto accumulation = reinterpret_cast<int16x8_t*>(
275             &accumulator.accumulation[perspective][i][0]);
276   #endif
277
278         if (reset[perspective]) {
279           std::memcpy(accumulator.accumulation[perspective][i], biases_,
280                       kHalfDimensions * sizeof(BiasType));
281         } else {
282           std::memcpy(accumulator.accumulation[perspective][i],
283                       prev_accumulator.accumulation[perspective][i],
284                       kHalfDimensions * sizeof(BiasType));
285           // Difference calculation for the deactivated features
286           for (const auto index : removed_indices[perspective]) {
287             const IndexType offset = kHalfDimensions * index;
288
289   #if defined(USE_AVX2)
290             auto column = reinterpret_cast<const __m256i*>(&weights_[offset]);
291             for (IndexType j = 0; j < kNumChunks; ++j) {
292               accumulation[j] = _mm256_sub_epi16(accumulation[j], column[j]);
293             }
294
295   #elif defined(USE_SSE2)
296             auto column = reinterpret_cast<const __m128i*>(&weights_[offset]);
297             for (IndexType j = 0; j < kNumChunks; ++j) {
298               accumulation[j] = _mm_sub_epi16(accumulation[j], column[j]);
299             }
300
301   #elif defined(USE_MMX)
302             auto column = reinterpret_cast<const __m64*>(&weights_[offset]);
303             for (IndexType j = 0; j < kNumChunks; ++j) {
304               accumulation[j] = _mm_sub_pi16(accumulation[j], column[j]);
305             }
306
307   #elif defined(USE_NEON)
308             auto column = reinterpret_cast<const int16x8_t*>(&weights_[offset]);
309             for (IndexType j = 0; j < kNumChunks; ++j) {
310               accumulation[j] = vsubq_s16(accumulation[j], column[j]);
311             }
312
313   #else
314             for (IndexType j = 0; j < kHalfDimensions; ++j) {
315               accumulator.accumulation[perspective][i][j] -=
316                   weights_[offset + j];
317             }
318   #endif
319
320           }
321         }
322         { // Difference calculation for the activated features
323           for (const auto index : added_indices[perspective]) {
324             const IndexType offset = kHalfDimensions * index;
325
326   #if defined(USE_AVX2)
327             auto column = reinterpret_cast<const __m256i*>(&weights_[offset]);
328             for (IndexType j = 0; j < kNumChunks; ++j) {
329               accumulation[j] = _mm256_add_epi16(accumulation[j], column[j]);
330             }
331
332   #elif defined(USE_SSE2)
333             auto column = reinterpret_cast<const __m128i*>(&weights_[offset]);
334             for (IndexType j = 0; j < kNumChunks; ++j) {
335               accumulation[j] = _mm_add_epi16(accumulation[j], column[j]);
336             }
337
338   #elif defined(USE_MMX)
339             auto column = reinterpret_cast<const __m64*>(&weights_[offset]);
340             for (IndexType j = 0; j < kNumChunks; ++j) {
341               accumulation[j] = _mm_add_pi16(accumulation[j], column[j]);
342             }
343
344   #elif defined(USE_NEON)
345             auto column = reinterpret_cast<const int16x8_t*>(&weights_[offset]);
346             for (IndexType j = 0; j < kNumChunks; ++j) {
347               accumulation[j] = vaddq_s16(accumulation[j], column[j]);
348             }
349
350   #else
351             for (IndexType j = 0; j < kHalfDimensions; ++j) {
352               accumulator.accumulation[perspective][i][j] +=
353                   weights_[offset + j];
354             }
355   #endif
356
357           }
358         }
359       }
360   #if defined(USE_MMX)
361       _mm_empty();
362   #endif
363
364       accumulator.computed_accumulation = true;
365       accumulator.computed_score = false;
366     }
367
368     using BiasType = std::int16_t;
369     using WeightType = std::int16_t;
370
371     alignas(kCacheLineSize) BiasType biases_[kHalfDimensions];
372     alignas(kCacheLineSize)
373         WeightType weights_[kHalfDimensions * kInputDimensions];
374   };
375
376 }  // namespace Eval::NNUE
377
378 #endif // #ifndef NNUE_FEATURE_TRANSFORMER_H_INCLUDED