2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2020 The Stockfish developers (see AUTHORS file)
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.
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.
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/>.
19 // Definition of layer AffineTransform of NNUE evaluation function
21 #ifndef NNUE_LAYERS_AFFINE_TRANSFORM_H_INCLUDED
22 #define NNUE_LAYERS_AFFINE_TRANSFORM_H_INCLUDED
25 #include "../nnue_common.h"
27 namespace Eval::NNUE::Layers {
29 // Affine transformation layer
30 template <typename PreviousLayer, IndexType OutputDimensions>
31 class AffineTransform {
34 using InputType = typename PreviousLayer::OutputType;
35 using OutputType = std::int32_t;
36 static_assert(std::is_same<InputType, std::uint8_t>::value, "");
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);
45 // Size of forward propagation buffer used in this layer
46 static constexpr std::size_t kSelfBufferSize =
47 CeilToMultiple(kOutputDimensions * sizeof(OutputType), kCacheLineSize);
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;
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;
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();
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);
78 #if defined (USE_AVX512)
80 [[maybe_unused]] const __m512i kOnes512 = _mm512_set1_epi16(1);
82 [[maybe_unused]] auto m512_hadd = [](__m512i sum, int bias) -> int {
83 return _mm512_reduce_add_epi32(sum) + bias;
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]
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)
98 [[maybe_unused]] auto m512_hadd128x16_interleave = [](
99 __m512i sum0, __m512i sum1, __m512i sum2, __m512i sum3) -> __m512i {
101 __m512i sum01a = _mm512_unpacklo_epi32(sum0, sum1);
102 __m512i sum01b = _mm512_unpackhi_epi32(sum0, sum1);
104 __m512i sum23a = _mm512_unpacklo_epi32(sum2, sum3);
105 __m512i sum23b = _mm512_unpackhi_epi32(sum2, sum3);
107 __m512i sum01 = _mm512_add_epi32(sum01a, sum01b);
108 __m512i sum23 = _mm512_add_epi32(sum23a, sum23b);
110 __m512i sum0123a = _mm512_unpacklo_epi64(sum01, sum23);
111 __m512i sum0123b = _mm512_unpackhi_epi64(sum01, sum23);
113 return _mm512_add_epi32(sum0123a, sum0123b);
116 [[maybe_unused]] auto m512_haddx4 = [m512_hadd128x16_interleave](
117 __m512i sum0, __m512i sum1, __m512i sum2, __m512i sum3, __m128i bias) -> __m128i {
119 __m512i sum = m512_hadd128x16_interleave(sum0, sum1, sum2, sum3);
121 __m256i sum256lo = _mm512_castsi512_si256(sum);
122 __m256i sum256hi = _mm512_extracti64x4_epi64(sum, 1);
124 sum256lo = _mm256_add_epi32(sum256lo, sum256hi);
126 __m128i sum128lo = _mm256_castsi256_si128(sum256lo);
127 __m128i sum128hi = _mm256_extracti128_si256(sum256lo, 1);
129 return _mm_add_epi32(_mm_add_epi32(sum128lo, sum128hi), bias);
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 {
136 __m512i suma = m512_hadd128x16_interleave(sum0, sum1, sum2, sum3);
137 __m512i sumb = m512_hadd128x16_interleave(sum4, sum5, sum6, sum7);
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));
145 __m256i sum256lo = _mm512_castsi512_si256(x);
146 __m256i sum256hi = _mm512_extracti64x4_epi64(x, 1);
148 return _mm256_add_epi32(_mm256_add_epi32(sum256lo, sum256hi), bias);
151 [[maybe_unused]] auto m512_hadd256x8 =[m512_hadd128x16_interleave](
152 __m512i sum0, __m512i sum1, __m512i sum2, __m512i sum3, __m256i bias) -> __m256i {
154 __m512i sum = m512_hadd128x16_interleave(sum0, sum1, sum2, sum3);
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);
161 __m256i sum256lo = _mm512_castsi512_si256(sum);
162 __m256i sum256hi = _mm512_extracti64x4_epi64(sum, 1);
164 return _mm256_add_epi32(_mm256_hadd_epi32(sum256lo, sum256hi), bias);
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 {
171 __m512i suma = m512_hadd128x16_interleave(sum0, sum1, sum2, sum3);
172 __m512i sumb = m512_hadd128x16_interleave(sum4, sum5, sum6, sum7);
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));
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);
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);
188 __m512i product0 = _mm512_maddubs_epi16(a, b);
189 product0 = _mm512_madd_epi16(product0, kOnes512);
190 acc = _mm512_add_epi32(acc, product0);
195 #if defined (USE_AVX2)
197 [[maybe_unused]] const __m256i kOnes256 = _mm256_set1_epi16(1);
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;
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);
210 sum0 = _mm256_hadd_epi32(sum0, sum2);
212 __m128i sum128lo = _mm256_castsi256_si128(sum0);
213 __m128i sum128hi = _mm256_extracti128_si256(sum0, 1);
215 return _mm_add_epi32(_mm_add_epi32(sum128lo, sum128hi), bias);
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);
222 __m256i product0 = _mm256_maddubs_epi16(a, b);
223 product0 = _mm256_madd_epi16(product0, kOnes256);
224 acc = _mm256_add_epi32(acc, product0);
230 #if defined (USE_SSSE3)
232 [[maybe_unused]] const __m128i kOnes128 = _mm_set1_epi16(1);
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;
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);
244 sum0 = _mm_hadd_epi32(sum0, sum2);
246 return _mm_add_epi32(sum0, bias);
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);
257 #if defined (USE_AVX512)
259 constexpr IndexType kNumChunks512 = kPaddedInputDimensions / (kSimdWidth * 2);
260 constexpr IndexType kNumChunks256 = kPaddedInputDimensions / kSimdWidth;
262 const auto output = reinterpret_cast<OutputType*>(buffer);
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);
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)
282 for (IndexType i = 0; i < kOutputDimensions; i += 16)
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;
293 const __m512i bias = *reinterpret_cast<const __m512i*>(&biases_[i]);
294 __m512i* outptr = reinterpret_cast<__m512i*>(&output[i]);
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();
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]);
314 const __m256i in256 = input_vector256[0];
315 const __m512i in = _mm512_inserti64x4(_mm512_castsi256_si512(in256), in256, 1);
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);
326 *outptr = m512_hadd256x16(
327 sum01a, sum23a, sum45a, sum67a,
328 sum01b, sum23b, sum45b, sum67b, bias);
331 else if constexpr (kOutputDimensions % 4 == 0)
333 for (IndexType i = 0; i < kOutputDimensions; i += 4)
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;
340 const __m128i bias = *reinterpret_cast<const __m128i*>(&biases_[i]);
341 __m128i* outptr = reinterpret_cast<__m128i*>(&output[i]);
343 if constexpr (kPaddedInputDimensions % (kSimdWidth * 2) == 0)
345 __m512i sum0 = _mm512_setzero_si512();
346 __m512i sum1 = _mm512_setzero_si512();
347 __m512i sum2 = _mm512_setzero_si512();
348 __m512i sum3 = _mm512_setzero_si512();
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]);
355 for (IndexType j = 0; j < kNumChunks512; ++j)
357 const __m512i in = input_vector512[j];
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]);
365 *outptr = m512_haddx4(sum0, sum1, sum2, sum3, bias);
369 __m256i sum0 = _mm256_setzero_si256();
370 __m256i sum1 = _mm256_setzero_si256();
371 __m256i sum2 = _mm256_setzero_si256();
372 __m256i sum3 = _mm256_setzero_si256();
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]);
379 for (IndexType j = 0; j < kNumChunks256; ++j)
381 const __m256i in = input_vector256[j];
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]);
389 *outptr = m256_haddx4(sum0, sum1, sum2, sum3, bias);
393 else if constexpr (kOutputDimensions == 1)
395 if constexpr (kPaddedInputDimensions % (kSimdWidth * 2) == 0)
397 __m512i sum0 = _mm512_setzero_si512();
399 const auto row0 = reinterpret_cast<const __m512i*>(&weights_[0]);
401 for (IndexType j = 0; j < kNumChunks512; ++j)
403 const __m512i in = input_vector512[j];
405 m512_add_dpbusd_epi32(sum0, in, row0[j]);
408 output[0] = m512_hadd(sum0, biases_[0]);
412 __m256i sum0 = _mm256_setzero_si256();
414 const auto row0 = reinterpret_cast<const __m256i*>(&weights_[0]);
416 for (IndexType j = 0; j < kNumChunks256; ++j)
418 const __m256i in = input_vector256[j];
420 m256_add_dpbusd_epi32(sum0, in, row0[j]);
423 output[0] = m256_hadd(sum0, biases_[0]);
428 // This case can never happen because kOutputDimensions
429 // is always 1 or a multiple of kSimdWidth.
433 #elif defined (USE_AVX2)
435 constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
437 const auto output = reinterpret_cast<OutputType*>(buffer);
438 const auto input_vector = reinterpret_cast<const __m256i*>(input);
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)
444 for (IndexType i = 0; i < kOutputDimensions; i += 4)
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;
451 const __m128i bias = *reinterpret_cast<const __m128i*>(&biases_[i]);
452 __m128i* outptr = reinterpret_cast<__m128i*>(&output[i]);
454 __m256i sum0 = _mm256_setzero_si256();
455 __m256i sum1 = _mm256_setzero_si256();
456 __m256i sum2 = _mm256_setzero_si256();
457 __m256i sum3 = _mm256_setzero_si256();
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]);
464 for (IndexType j = 0; j < kNumChunks; ++j)
466 const __m256i in = input_vector[j];
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]);
474 *outptr = m256_haddx4(sum0, sum1, sum2, sum3, bias);
477 else if constexpr (kOutputDimensions == 1)
479 __m256i sum0 = _mm256_setzero_si256();
481 const auto row0 = reinterpret_cast<const __m256i*>(&weights_[0]);
483 for (IndexType j = 0; j < kNumChunks; ++j)
485 const __m256i in = input_vector[j];
487 m256_add_dpbusd_epi32(sum0, in, row0[j]);
490 output[0] = m256_hadd(sum0, biases_[0]);
494 // This case can never happen because kOutputDimensions
495 // is always 1 or a multiple of kSimdWidth.
499 #elif defined (USE_SSSE3)
501 constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
503 auto output = reinterpret_cast<OutputType*>(buffer);
504 const auto input_vector = reinterpret_cast<const __m128i*>(input);
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)
510 for (IndexType i = 0; i < kOutputDimensions; i += 4)
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;
517 const __m128i bias = *reinterpret_cast<const __m128i*>(&biases_[i]);
518 __m128i* outptr = reinterpret_cast<__m128i*>(&output[i]);
520 __m128i sum0 = _mm_setzero_si128();
521 __m128i sum1 = _mm_setzero_si128();
522 __m128i sum2 = _mm_setzero_si128();
523 __m128i sum3 = _mm_setzero_si128();
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]);
530 for (int j = 0; j < (int)kNumChunks; j += 1)
532 const __m128i in = input_vector[j];
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]);
540 *outptr = m128_haddx4(sum0, sum1, sum2, sum3, bias);
543 else if constexpr (kOutputDimensions == 1)
545 __m128i sum0 = _mm_setzero_si128();
547 const auto row0 = reinterpret_cast<const __m128i*>(&weights_[0]);
549 for (int j = 0; j < (int)kNumChunks; j += 1)
551 const __m128i in = input_vector[j];
553 m128_add_dpbusd_epi32(sum0, in, row0[j]);
556 output[0] = m128_hadd(sum0, biases_[0]);
560 // This case can never happen because kOutputDimensions
561 // is always 1 or a multiple of kSimdWidth.
567 // Use old implementation for the other architectures.
569 auto output = reinterpret_cast<OutputType*>(buffer);
571 #if defined(USE_SSE2)
572 constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
574 const __m128i kZeros = _mm_setzero_si128();
576 const __m128i kOnes = _mm_set1_epi16(1);
578 const auto input_vector = reinterpret_cast<const __m128i*>(input);
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);
585 #elif defined(USE_NEON)
586 constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
587 const auto input_vector = reinterpret_cast<const int8x8_t*>(input);
590 for (IndexType i = 0; i < kOutputDimensions; ++i) {
591 const IndexType offset = i * kPaddedInputDimensions;
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);
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);
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);
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);
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);
646 output[i] = sum[0] + sum[1] + sum[2] + sum[3];
649 OutputType sum = biases_[i];
650 for (IndexType j = 0; j < kInputDimensions; ++j) {
651 sum += weights_[offset + j] * input[j];
667 using BiasType = OutputType;
668 using WeightType = std::int8_t;
670 PreviousLayer previous_layer_;
672 alignas(kCacheLineSize) BiasType biases_[kOutputDimensions];
673 alignas(kCacheLineSize)
674 WeightType weights_[kOutputDimensions * kPaddedInputDimensions];
677 } // namespace Eval::NNUE::Layers
679 #endif // #ifndef NNUE_LAYERS_AFFINE_TRANSFORM_H_INCLUDED