]> git.sesse.net Git - stockfish/blob - src/nnue/layers/affine_transform.h
Assorted search and eval parameter tune
[stockfish] / src / nnue / layers / affine_transform.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 // Definition of layer AffineTransform of NNUE evaluation function
20
21 #ifndef NNUE_LAYERS_AFFINE_TRANSFORM_H_INCLUDED
22 #define NNUE_LAYERS_AFFINE_TRANSFORM_H_INCLUDED
23
24 #include <iostream>
25 #include "../nnue_common.h"
26
27 namespace Eval::NNUE::Layers {
28
29   // Affine transformation layer
30   template <typename PreviousLayer, IndexType OutputDimensions>
31   class AffineTransform {
32    public:
33     // Input/output type
34     using InputType = typename PreviousLayer::OutputType;
35     using OutputType = std::int32_t;
36     static_assert(std::is_same<InputType, std::uint8_t>::value, "");
37
38     // Number of input/output dimensions
39     static constexpr IndexType kInputDimensions =
40         PreviousLayer::kOutputDimensions;
41     static constexpr IndexType kOutputDimensions = OutputDimensions;
42     static constexpr IndexType kPaddedInputDimensions =
43         CeilToMultiple<IndexType>(kInputDimensions, kMaxSimdWidth);
44
45     // Size of forward propagation buffer used in this layer
46     static constexpr std::size_t kSelfBufferSize =
47         CeilToMultiple(kOutputDimensions * sizeof(OutputType), kCacheLineSize);
48
49     // Size of the forward propagation buffer used from the input layer to this layer
50     static constexpr std::size_t kBufferSize =
51         PreviousLayer::kBufferSize + kSelfBufferSize;
52
53     // Hash value embedded in the evaluation file
54     static constexpr std::uint32_t GetHashValue() {
55       std::uint32_t hash_value = 0xCC03DAE4u;
56       hash_value += kOutputDimensions;
57       hash_value ^= PreviousLayer::GetHashValue() >> 1;
58       hash_value ^= PreviousLayer::GetHashValue() << 31;
59       return hash_value;
60     }
61
62    // Read network parameters
63     bool ReadParameters(std::istream& stream) {
64       if (!previous_layer_.ReadParameters(stream)) return false;
65       for (std::size_t i = 0; i < kOutputDimensions; ++i)
66         biases_[i] = read_little_endian<BiasType>(stream);
67       for (std::size_t i = 0; i < kOutputDimensions * kPaddedInputDimensions; ++i)
68         weights_[i] = read_little_endian<WeightType>(stream);
69       return !stream.fail();
70     }
71
72     // Forward propagation
73     const OutputType* Propagate(
74         const TransformedFeatureType* transformed_features, char* buffer) const {
75       const auto input = previous_layer_.Propagate(
76           transformed_features, buffer + kSelfBufferSize);
77
78 #if defined (USE_AVX512)
79
80       [[maybe_unused]] const __m512i kOnes512 = _mm512_set1_epi16(1);
81
82       [[maybe_unused]] auto m512_hadd = [](__m512i sum, int bias) -> int {
83         return _mm512_reduce_add_epi32(sum) + bias;
84       };
85
86       // This function takes
87       //   sum0 = [xmm0a, xmm0b, xmm0c, xmm0d]
88       //   sum1 = [xmm1a, xmm1b, xmm1c, xmm1d]
89       //   sum2 = [xmm2a, xmm2b, xmm2c, xmm2d]
90       //   sum3 = [xmm3a, xmm3b, xmm3c, xmm3d]
91       // and returns
92       //   ret = [
93       //     reduce_add_epi32(xmm0a), reduce_add_epi32(xmm1a), reduce_add_epi32(xmm2a), reduce_add_epi32(xmm3a),
94       //     reduce_add_epi32(xmm0b), reduce_add_epi32(xmm1b), reduce_add_epi32(xmm2b), reduce_add_epi32(xmm3b),
95       //     reduce_add_epi32(xmm0c), reduce_add_epi32(xmm1c), reduce_add_epi32(xmm2c), reduce_add_epi32(xmm3c),
96       //     reduce_add_epi32(xmm0d), reduce_add_epi32(xmm1d), reduce_add_epi32(xmm2d), reduce_add_epi32(xmm3d)
97       //   ]
98       [[maybe_unused]] auto m512_hadd128x16_interleave = [](
99         __m512i sum0, __m512i sum1, __m512i sum2, __m512i sum3) -> __m512i {
100
101         __m512i sum01a = _mm512_unpacklo_epi32(sum0, sum1);
102         __m512i sum01b = _mm512_unpackhi_epi32(sum0, sum1);
103
104         __m512i sum23a = _mm512_unpacklo_epi32(sum2, sum3);
105         __m512i sum23b = _mm512_unpackhi_epi32(sum2, sum3);
106
107         __m512i sum01 = _mm512_add_epi32(sum01a, sum01b);
108         __m512i sum23 = _mm512_add_epi32(sum23a, sum23b);
109
110         __m512i sum0123a = _mm512_unpacklo_epi64(sum01, sum23);
111         __m512i sum0123b = _mm512_unpackhi_epi64(sum01, sum23);
112
113         return _mm512_add_epi32(sum0123a, sum0123b);
114       };
115
116       [[maybe_unused]] auto m512_haddx4 = [m512_hadd128x16_interleave](
117         __m512i sum0, __m512i sum1, __m512i sum2, __m512i sum3, __m128i bias) -> __m128i {
118
119         __m512i sum = m512_hadd128x16_interleave(sum0, sum1, sum2, sum3);
120
121         __m256i sum256lo = _mm512_castsi512_si256(sum);
122         __m256i sum256hi = _mm512_extracti64x4_epi64(sum, 1);
123
124         sum256lo = _mm256_add_epi32(sum256lo, sum256hi);
125
126         __m128i sum128lo = _mm256_castsi256_si128(sum256lo);
127         __m128i sum128hi = _mm256_extracti128_si256(sum256lo, 1);
128
129         return _mm_add_epi32(_mm_add_epi32(sum128lo, sum128hi), bias);
130       };
131
132       [[maybe_unused]] auto m512_haddx8 = [m512_hadd128x16_interleave](
133         __m512i sum0, __m512i sum1, __m512i sum2, __m512i sum3,
134         __m512i sum4, __m512i sum5, __m512i sum6, __m512i sum7, __m256i bias) -> __m256i {
135
136         __m512i suma = m512_hadd128x16_interleave(sum0, sum1, sum2, sum3);
137         __m512i sumb = m512_hadd128x16_interleave(sum4, sum5, sum6, sum7);
138
139         __m512i indices0 = _mm512_setr_epi64(0, 1, 8, 9, 4, 5, 12, 13);
140         __m512i indices1 = _mm512_setr_epi64(2, 3, 10, 11, 6, 7, 14, 15);
141         __m512i x = _mm512_add_epi32(
142           _mm512_permutex2var_epi64(suma, indices0, sumb),
143           _mm512_permutex2var_epi64(suma, indices1, sumb));
144
145         __m256i sum256lo = _mm512_castsi512_si256(x);
146         __m256i sum256hi = _mm512_extracti64x4_epi64(x, 1);
147
148         return _mm256_add_epi32(_mm256_add_epi32(sum256lo, sum256hi), bias);
149       };
150
151       [[maybe_unused]] auto m512_hadd256x8 =[m512_hadd128x16_interleave](
152         __m512i sum0, __m512i sum1, __m512i sum2, __m512i sum3, __m256i bias) -> __m256i {
153
154         __m512i sum = m512_hadd128x16_interleave(sum0, sum1, sum2, sum3);
155
156         __m512i indices = _mm512_setr_epi32(
157           0, 4, 8, 12, 2, 6, 10, 14,
158           1, 5, 9, 13, 3, 7, 11, 15);
159         sum = _mm512_permutexvar_epi32(indices, sum);
160
161         __m256i sum256lo = _mm512_castsi512_si256(sum);
162         __m256i sum256hi = _mm512_extracti64x4_epi64(sum, 1);
163
164         return _mm256_add_epi32(_mm256_hadd_epi32(sum256lo, sum256hi), bias);
165       };
166
167       [[maybe_unused]] auto m512_hadd256x16 = [m512_hadd128x16_interleave](
168         __m512i sum0, __m512i sum1, __m512i sum2, __m512i sum3,
169         __m512i sum4, __m512i sum5, __m512i sum6, __m512i sum7, __m512i bias) -> __m512i {
170
171         __m512i suma = m512_hadd128x16_interleave(sum0, sum1, sum2, sum3);
172         __m512i sumb = m512_hadd128x16_interleave(sum4, sum5, sum6, sum7);
173
174         __m512i indices0 = _mm512_setr_epi64(0, 1, 8, 9, 4, 5, 12, 13);
175         __m512i indices1 = _mm512_setr_epi64(2, 3, 10, 11, 6, 7, 14, 15);
176         __m512i x = _mm512_add_epi32(
177           _mm512_permutex2var_epi64(suma, indices0, sumb),
178           _mm512_permutex2var_epi64(suma, indices1, sumb));
179
180         __m512i indices = _mm512_setr_epi32(0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15);
181         return _mm512_add_epi32(_mm512_permutexvar_epi32(indices, x), bias);
182       };
183
184       [[maybe_unused]] auto m512_add_dpbusd_epi32 = [=](__m512i& acc, __m512i a, __m512i b) {
185 #if defined (USE_VNNI)
186         acc = _mm512_dpbusd_epi32(acc, a, b);
187 #else
188         __m512i product0 = _mm512_maddubs_epi16(a, b);
189         product0 = _mm512_madd_epi16(product0, kOnes512);
190         acc = _mm512_add_epi32(acc, product0);
191 #endif
192       };
193
194 #endif
195 #if defined (USE_AVX2)
196
197       [[maybe_unused]] const __m256i kOnes256 = _mm256_set1_epi16(1);
198
199       [[maybe_unused]] auto m256_hadd = [](__m256i sum, int bias) -> int {
200         __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(sum), _mm256_extracti128_si256(sum, 1));
201         sum128 = _mm_add_epi32(sum128, _mm_shuffle_epi32(sum128, _MM_PERM_BADC));
202         sum128 = _mm_add_epi32(sum128, _mm_shuffle_epi32(sum128, _MM_PERM_CDAB));
203         return _mm_cvtsi128_si32(sum128) + bias;
204       };
205
206       [[maybe_unused]] auto m256_haddx4 = [](__m256i sum0, __m256i sum1, __m256i sum2, __m256i sum3, __m128i bias) -> __m128i {
207         sum0 = _mm256_hadd_epi32(sum0, sum1);
208         sum2 = _mm256_hadd_epi32(sum2, sum3);
209
210         sum0 = _mm256_hadd_epi32(sum0, sum2);
211
212         __m128i sum128lo = _mm256_castsi256_si128(sum0);
213         __m128i sum128hi = _mm256_extracti128_si256(sum0, 1);
214
215         return _mm_add_epi32(_mm_add_epi32(sum128lo, sum128hi), bias);
216       };
217
218       [[maybe_unused]] auto m256_add_dpbusd_epi32 = [=](__m256i& acc, __m256i a, __m256i b) {
219 #if defined (USE_VNNI)
220         acc = _mm256_dpbusd_epi32(acc, a, b);
221 #else
222         __m256i product0 = _mm256_maddubs_epi16(a, b);
223         product0 = _mm256_madd_epi16(product0, kOnes256);
224         acc = _mm256_add_epi32(acc, product0);
225 #endif
226       };
227
228 #endif
229
230 #if defined (USE_SSSE3)
231
232       [[maybe_unused]] const __m128i kOnes128 = _mm_set1_epi16(1);
233
234       [[maybe_unused]] auto m128_hadd = [](__m128i sum, int bias) -> int {
235         sum = _mm_add_epi32(sum, _mm_shuffle_epi32(sum, 0x4E)); //_MM_PERM_BADC
236         sum = _mm_add_epi32(sum, _mm_shuffle_epi32(sum, 0xB1)); //_MM_PERM_CDAB
237         return _mm_cvtsi128_si32(sum) + bias;
238       };
239
240       [[maybe_unused]] auto m128_haddx4 = [](__m128i sum0, __m128i sum1, __m128i sum2, __m128i sum3, __m128i bias) -> __m128i {
241         sum0 = _mm_hadd_epi32(sum0, sum1);
242         sum2 = _mm_hadd_epi32(sum2, sum3);
243
244         sum0 = _mm_hadd_epi32(sum0, sum2);
245
246         return _mm_add_epi32(sum0, bias);
247       };
248
249       [[maybe_unused]] auto m128_add_dpbusd_epi32 = [=](__m128i& acc, __m128i a, __m128i b) {
250         __m128i product0 = _mm_maddubs_epi16(a, b);
251         product0 = _mm_madd_epi16(product0, kOnes128);
252         acc = _mm_add_epi32(acc, product0);
253       };
254
255 #endif
256
257 #if defined (USE_AVX512)
258
259       constexpr IndexType kNumChunks512 = kPaddedInputDimensions / (kSimdWidth * 2);
260       constexpr IndexType kNumChunks256 = kPaddedInputDimensions / kSimdWidth;
261
262       const auto output = reinterpret_cast<OutputType*>(buffer);
263
264       // Since to saturate a zmm register it takes 64 bytes we
265       // cannot use AVX512 for the smaller affine transforms.
266       // Instead we fallback to a AVX2 implementation if the
267       // kInputDimensions isn't a multiple of 64.
268       // Note that this means that for example for
269       // kInputDimensions of 96 we fallback to AVX2 even though
270       // the first 64 elements could be processed with AVX512.
271       // This is caused by mixing the __m256 and __m512 variables
272       // required to better handle that case and it would
273       // require handling more cases statically not to lose performance.
274       // This should be revisited if such input dimensions are to be considered.
275       [[maybe_unused]] const auto input_vector512 = reinterpret_cast<const __m512i*>(input);
276       [[maybe_unused]] const auto input_vector256 = reinterpret_cast<const __m256i*>(input);
277
278       // kOutputDimensions is either 1 or a multiple of kSimdWidth
279       // because then it is also an input dimension.
280       if constexpr (kOutputDimensions % 16 == 0 && kNumChunks256 == 1)
281       {
282         for (IndexType i = 0; i < kOutputDimensions; i += 16)
283         {
284           const IndexType offset01a = (i + 0) * kPaddedInputDimensions;
285           const IndexType offset23a = (i + 2) * kPaddedInputDimensions;
286           const IndexType offset45a = (i + 4) * kPaddedInputDimensions;
287           const IndexType offset67a = (i + 6) * kPaddedInputDimensions;
288           const IndexType offset01b = (i + 8) * kPaddedInputDimensions;
289           const IndexType offset23b = (i + 10) * kPaddedInputDimensions;
290           const IndexType offset45b = (i + 12) * kPaddedInputDimensions;
291           const IndexType offset67b = (i + 14) * kPaddedInputDimensions;
292
293           const __m512i bias = *reinterpret_cast<const __m512i*>(&biases_[i]);
294           __m512i* outptr = reinterpret_cast<__m512i*>(&output[i]);
295
296           __m512i sum01a = _mm512_setzero_si512();
297           __m512i sum23a = _mm512_setzero_si512();
298           __m512i sum45a = _mm512_setzero_si512();
299           __m512i sum67a = _mm512_setzero_si512();
300           __m512i sum01b = _mm512_setzero_si512();
301           __m512i sum23b = _mm512_setzero_si512();
302           __m512i sum45b = _mm512_setzero_si512();
303           __m512i sum67b = _mm512_setzero_si512();
304
305           const auto row01a = *reinterpret_cast<const __m512i*>(&weights_[offset01a]);
306           const auto row23a = *reinterpret_cast<const __m512i*>(&weights_[offset23a]);
307           const auto row45a = *reinterpret_cast<const __m512i*>(&weights_[offset45a]);
308           const auto row67a = *reinterpret_cast<const __m512i*>(&weights_[offset67a]);
309           const auto row01b = *reinterpret_cast<const __m512i*>(&weights_[offset01b]);
310           const auto row23b = *reinterpret_cast<const __m512i*>(&weights_[offset23b]);
311           const auto row45b = *reinterpret_cast<const __m512i*>(&weights_[offset45b]);
312           const auto row67b = *reinterpret_cast<const __m512i*>(&weights_[offset67b]);
313
314           const __m256i in256 = input_vector256[0];
315           const __m512i in = _mm512_inserti64x4(_mm512_castsi256_si512(in256), in256, 1);
316
317           m512_add_dpbusd_epi32(sum01a, in, row01a);
318           m512_add_dpbusd_epi32(sum23a, in, row23a);
319           m512_add_dpbusd_epi32(sum45a, in, row45a);
320           m512_add_dpbusd_epi32(sum67a, in, row67a);
321           m512_add_dpbusd_epi32(sum01b, in, row01b);
322           m512_add_dpbusd_epi32(sum23b, in, row23b);
323           m512_add_dpbusd_epi32(sum45b, in, row45b);
324           m512_add_dpbusd_epi32(sum67b, in, row67b);
325
326           *outptr = m512_hadd256x16(
327             sum01a, sum23a, sum45a, sum67a,
328             sum01b, sum23b, sum45b, sum67b, bias);
329         }
330       }
331       else if constexpr (kOutputDimensions % 4 == 0)
332       {
333         for (IndexType i = 0; i < kOutputDimensions; i += 4)
334         {
335           const IndexType offset0 = (i + 0) * kPaddedInputDimensions;
336           const IndexType offset1 = (i + 1) * kPaddedInputDimensions;
337           const IndexType offset2 = (i + 2) * kPaddedInputDimensions;
338           const IndexType offset3 = (i + 3) * kPaddedInputDimensions;
339
340           const __m128i bias = *reinterpret_cast<const __m128i*>(&biases_[i]);
341           __m128i* outptr = reinterpret_cast<__m128i*>(&output[i]);
342
343           if constexpr (kPaddedInputDimensions % (kSimdWidth * 2) == 0)
344           {
345             __m512i sum0 = _mm512_setzero_si512();
346             __m512i sum1 = _mm512_setzero_si512();
347             __m512i sum2 = _mm512_setzero_si512();
348             __m512i sum3 = _mm512_setzero_si512();
349
350             const auto row0 = reinterpret_cast<const __m512i*>(&weights_[offset0]);
351             const auto row1 = reinterpret_cast<const __m512i*>(&weights_[offset1]);
352             const auto row2 = reinterpret_cast<const __m512i*>(&weights_[offset2]);
353             const auto row3 = reinterpret_cast<const __m512i*>(&weights_[offset3]);
354
355             for (IndexType j = 0; j < kNumChunks512; ++j)
356             {
357               const __m512i in = input_vector512[j];
358
359               m512_add_dpbusd_epi32(sum0, in, row0[j]);
360               m512_add_dpbusd_epi32(sum1, in, row1[j]);
361               m512_add_dpbusd_epi32(sum2, in, row2[j]);
362               m512_add_dpbusd_epi32(sum3, in, row3[j]);
363             }
364
365             *outptr = m512_haddx4(sum0, sum1, sum2, sum3, bias);
366           }
367           else
368           {
369             __m256i sum0 = _mm256_setzero_si256();
370             __m256i sum1 = _mm256_setzero_si256();
371             __m256i sum2 = _mm256_setzero_si256();
372             __m256i sum3 = _mm256_setzero_si256();
373
374             const auto row0 = reinterpret_cast<const __m256i*>(&weights_[offset0]);
375             const auto row1 = reinterpret_cast<const __m256i*>(&weights_[offset1]);
376             const auto row2 = reinterpret_cast<const __m256i*>(&weights_[offset2]);
377             const auto row3 = reinterpret_cast<const __m256i*>(&weights_[offset3]);
378
379             for (IndexType j = 0; j < kNumChunks256; ++j)
380             {
381               const __m256i in = input_vector256[j];
382
383               m256_add_dpbusd_epi32(sum0, in, row0[j]);
384               m256_add_dpbusd_epi32(sum1, in, row1[j]);
385               m256_add_dpbusd_epi32(sum2, in, row2[j]);
386               m256_add_dpbusd_epi32(sum3, in, row3[j]);
387             }
388
389             *outptr = m256_haddx4(sum0, sum1, sum2, sum3, bias);
390           }
391         }
392       }
393       else if constexpr (kOutputDimensions == 1)
394       {
395         if constexpr (kPaddedInputDimensions % (kSimdWidth * 2) == 0)
396         {
397           __m512i sum0 = _mm512_setzero_si512();
398
399           const auto row0 = reinterpret_cast<const __m512i*>(&weights_[0]);
400
401           for (IndexType j = 0; j < kNumChunks512; ++j)
402           {
403             const __m512i in = input_vector512[j];
404
405             m512_add_dpbusd_epi32(sum0, in, row0[j]);
406           }
407
408           output[0] = m512_hadd(sum0, biases_[0]);
409         }
410         else
411         {
412           __m256i sum0 = _mm256_setzero_si256();
413
414           const auto row0 = reinterpret_cast<const __m256i*>(&weights_[0]);
415
416           for (IndexType j = 0; j < kNumChunks256; ++j)
417           {
418             const __m256i in = input_vector256[j];
419
420             m256_add_dpbusd_epi32(sum0, in, row0[j]);
421           }
422
423           output[0] = m256_hadd(sum0, biases_[0]);
424         }
425       }
426       else
427       {
428         // This case can never happen because kOutputDimensions
429         // is always 1 or a multiple of kSimdWidth.
430         assert(false);
431       }
432
433 #elif defined (USE_AVX2)
434
435       constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
436
437       const auto output = reinterpret_cast<OutputType*>(buffer);
438       const auto input_vector = reinterpret_cast<const __m256i*>(input);
439
440       // kOutputDimensions is either 1 or a multiple of kSimdWidth
441       // because then it is also an input dimension.
442       if constexpr (kOutputDimensions % 4 == 0)
443       {
444         for (IndexType i = 0; i < kOutputDimensions; i += 4)
445         {
446           const IndexType offset0 = (i + 0) * kPaddedInputDimensions;
447           const IndexType offset1 = (i + 1) * kPaddedInputDimensions;
448           const IndexType offset2 = (i + 2) * kPaddedInputDimensions;
449           const IndexType offset3 = (i + 3) * kPaddedInputDimensions;
450
451           const __m128i bias = *reinterpret_cast<const __m128i*>(&biases_[i]);
452           __m128i* outptr = reinterpret_cast<__m128i*>(&output[i]);
453
454           __m256i sum0 = _mm256_setzero_si256();
455           __m256i sum1 = _mm256_setzero_si256();
456           __m256i sum2 = _mm256_setzero_si256();
457           __m256i sum3 = _mm256_setzero_si256();
458
459           const auto row0 = reinterpret_cast<const __m256i*>(&weights_[offset0]);
460           const auto row1 = reinterpret_cast<const __m256i*>(&weights_[offset1]);
461           const auto row2 = reinterpret_cast<const __m256i*>(&weights_[offset2]);
462           const auto row3 = reinterpret_cast<const __m256i*>(&weights_[offset3]);
463
464           for (IndexType j = 0; j < kNumChunks; ++j)
465           {
466             const __m256i in = input_vector[j];
467
468             m256_add_dpbusd_epi32(sum0, in, row0[j]);
469             m256_add_dpbusd_epi32(sum1, in, row1[j]);
470             m256_add_dpbusd_epi32(sum2, in, row2[j]);
471             m256_add_dpbusd_epi32(sum3, in, row3[j]);
472           }
473
474           *outptr = m256_haddx4(sum0, sum1, sum2, sum3, bias);
475         }
476       }
477       else if constexpr (kOutputDimensions == 1)
478       {
479         __m256i sum0 = _mm256_setzero_si256();
480
481         const auto row0 = reinterpret_cast<const __m256i*>(&weights_[0]);
482
483         for (IndexType j = 0; j < kNumChunks; ++j)
484         {
485           const __m256i in = input_vector[j];
486
487             m256_add_dpbusd_epi32(sum0, in, row0[j]);
488         }
489
490         output[0] = m256_hadd(sum0, biases_[0]);
491       }
492       else
493       {
494         // This case can never happen because kOutputDimensions
495         // is always 1 or a multiple of kSimdWidth.
496         assert(false);
497       }
498
499 #elif defined (USE_SSSE3)
500
501       constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
502
503       auto output = reinterpret_cast<OutputType*>(buffer);
504       const auto input_vector = reinterpret_cast<const __m128i*>(input);
505
506       // kOutputDimensions is either 1 or a multiple of kSimdWidth
507       // because then it is also an input dimension.
508       if constexpr (kOutputDimensions % 4 == 0)
509       {
510         for (IndexType i = 0; i < kOutputDimensions; i += 4)
511         {
512           const IndexType offset0 = (i + 0) * kPaddedInputDimensions;
513           const IndexType offset1 = (i + 1) * kPaddedInputDimensions;
514           const IndexType offset2 = (i + 2) * kPaddedInputDimensions;
515           const IndexType offset3 = (i + 3) * kPaddedInputDimensions;
516
517           const __m128i bias = *reinterpret_cast<const __m128i*>(&biases_[i]);
518           __m128i* outptr = reinterpret_cast<__m128i*>(&output[i]);
519
520           __m128i sum0 = _mm_setzero_si128();
521           __m128i sum1 = _mm_setzero_si128();
522           __m128i sum2 = _mm_setzero_si128();
523           __m128i sum3 = _mm_setzero_si128();
524
525           const auto row0 = reinterpret_cast<const __m128i*>(&weights_[offset0]);
526           const auto row1 = reinterpret_cast<const __m128i*>(&weights_[offset1]);
527           const auto row2 = reinterpret_cast<const __m128i*>(&weights_[offset2]);
528           const auto row3 = reinterpret_cast<const __m128i*>(&weights_[offset3]);
529
530           for (int j = 0; j < (int)kNumChunks; j += 1)
531           {
532             const __m128i in = input_vector[j];
533
534             m128_add_dpbusd_epi32(sum0, in, row0[j]);
535             m128_add_dpbusd_epi32(sum1, in, row1[j]);
536             m128_add_dpbusd_epi32(sum2, in, row2[j]);
537             m128_add_dpbusd_epi32(sum3, in, row3[j]);
538           }
539
540           *outptr = m128_haddx4(sum0, sum1, sum2, sum3, bias);
541         }
542       }
543       else if constexpr (kOutputDimensions == 1)
544       {
545         __m128i sum0 = _mm_setzero_si128();
546
547         const auto row0 = reinterpret_cast<const __m128i*>(&weights_[0]);
548
549         for (int j = 0; j < (int)kNumChunks; j += 1)
550         {
551           const __m128i in = input_vector[j];
552
553           m128_add_dpbusd_epi32(sum0, in, row0[j]);
554         }
555
556         output[0] = m128_hadd(sum0, biases_[0]);
557       }
558       else
559       {
560         // This case can never happen because kOutputDimensions
561         // is always 1 or a multiple of kSimdWidth.
562         assert(false);
563       }
564
565 #else
566
567 // Use old implementation for the other architectures.
568
569       auto output = reinterpret_cast<OutputType*>(buffer);
570
571 #if defined(USE_SSE2)
572       constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
573 #ifndef USE_SSSE3
574       const __m128i kZeros = _mm_setzero_si128();
575 #else
576       const __m128i kOnes = _mm_set1_epi16(1);
577 #endif
578       const auto input_vector = reinterpret_cast<const __m128i*>(input);
579
580 #elif defined(USE_MMX)
581       constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
582       const __m64 kZeros = _mm_setzero_si64();
583       const auto input_vector = reinterpret_cast<const __m64*>(input);
584
585 #elif defined(USE_NEON)
586       constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
587       const auto input_vector = reinterpret_cast<const int8x8_t*>(input);
588 #endif
589
590       for (IndexType i = 0; i < kOutputDimensions; ++i) {
591         const IndexType offset = i * kPaddedInputDimensions;
592
593 #if defined(USE_SSE2)
594         __m128i sum_lo = _mm_cvtsi32_si128(biases_[i]);
595         __m128i sum_hi = kZeros;
596         const auto row = reinterpret_cast<const __m128i*>(&weights_[offset]);
597         for (IndexType j = 0; j < kNumChunks; ++j) {
598           __m128i row_j = _mm_load_si128(&row[j]);
599           __m128i input_j = _mm_load_si128(&input_vector[j]);
600           __m128i row_signs = _mm_cmpgt_epi8(kZeros, row_j);
601           __m128i extended_row_lo = _mm_unpacklo_epi8(row_j, row_signs);
602           __m128i extended_row_hi = _mm_unpackhi_epi8(row_j, row_signs);
603           __m128i extended_input_lo = _mm_unpacklo_epi8(input_j, kZeros);
604           __m128i extended_input_hi = _mm_unpackhi_epi8(input_j, kZeros);
605           __m128i product_lo = _mm_madd_epi16(extended_row_lo, extended_input_lo);
606           __m128i product_hi = _mm_madd_epi16(extended_row_hi, extended_input_hi);
607           sum_lo = _mm_add_epi32(sum_lo, product_lo);
608           sum_hi = _mm_add_epi32(sum_hi, product_hi);
609         }
610         __m128i sum = _mm_add_epi32(sum_lo, sum_hi);
611         __m128i sum_high_64 = _mm_shuffle_epi32(sum, _MM_SHUFFLE(1, 0, 3, 2));
612         sum = _mm_add_epi32(sum, sum_high_64);
613         __m128i sum_second_32 = _mm_shufflelo_epi16(sum, _MM_SHUFFLE(1, 0, 3, 2));
614         sum = _mm_add_epi32(sum, sum_second_32);
615         output[i] = _mm_cvtsi128_si32(sum);
616
617 #elif defined(USE_MMX)
618         __m64 sum_lo = _mm_cvtsi32_si64(biases_[i]);
619         __m64 sum_hi = kZeros;
620         const auto row = reinterpret_cast<const __m64*>(&weights_[offset]);
621         for (IndexType j = 0; j < kNumChunks; ++j) {
622           __m64 row_j = row[j];
623           __m64 input_j = input_vector[j];
624           __m64 row_signs = _mm_cmpgt_pi8(kZeros, row_j);
625           __m64 extended_row_lo = _mm_unpacklo_pi8(row_j, row_signs);
626           __m64 extended_row_hi = _mm_unpackhi_pi8(row_j, row_signs);
627           __m64 extended_input_lo = _mm_unpacklo_pi8(input_j, kZeros);
628           __m64 extended_input_hi = _mm_unpackhi_pi8(input_j, kZeros);
629           __m64 product_lo = _mm_madd_pi16(extended_row_lo, extended_input_lo);
630           __m64 product_hi = _mm_madd_pi16(extended_row_hi, extended_input_hi);
631           sum_lo = _mm_add_pi32(sum_lo, product_lo);
632           sum_hi = _mm_add_pi32(sum_hi, product_hi);
633         }
634         __m64 sum = _mm_add_pi32(sum_lo, sum_hi);
635         sum = _mm_add_pi32(sum, _mm_unpackhi_pi32(sum, sum));
636         output[i] = _mm_cvtsi64_si32(sum);
637
638 #elif defined(USE_NEON)
639         int32x4_t sum = {biases_[i]};
640         const auto row = reinterpret_cast<const int8x8_t*>(&weights_[offset]);
641         for (IndexType j = 0; j < kNumChunks; ++j) {
642           int16x8_t product = vmull_s8(input_vector[j * 2], row[j * 2]);
643           product = vmlal_s8(product, input_vector[j * 2 + 1], row[j * 2 + 1]);
644           sum = vpadalq_s16(sum, product);
645         }
646         output[i] = sum[0] + sum[1] + sum[2] + sum[3];
647
648 #else
649         OutputType sum = biases_[i];
650         for (IndexType j = 0; j < kInputDimensions; ++j) {
651           sum += weights_[offset + j] * input[j];
652         }
653         output[i] = sum;
654 #endif
655
656       }
657 #if defined(USE_MMX)
658       _mm_empty();
659 #endif
660
661 #endif
662
663       return output;
664     }
665
666    private:
667     using BiasType = OutputType;
668     using WeightType = std::int8_t;
669
670     PreviousLayer previous_layer_;
671
672     alignas(kCacheLineSize) BiasType biases_[kOutputDimensions];
673     alignas(kCacheLineSize)
674         WeightType weights_[kOutputDimensions * kPaddedInputDimensions];
675   };
676
677 }  // namespace Eval::NNUE::Layers
678
679 #endif // #ifndef NNUE_LAYERS_AFFINE_TRANSFORM_H_INCLUDED