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 [[maybe_unused]] auto m512_haddx4 = [](__m512i sum0, __m512i sum1, __m512i sum2, __m512i sum3, __m128i bias) -> __m128i {
87 __m512i sum01a = _mm512_unpacklo_epi32(sum0, sum1);
88 __m512i sum01b = _mm512_unpackhi_epi32(sum0, sum1);
90 __m512i sum23a = _mm512_unpacklo_epi32(sum2, sum3);
91 __m512i sum23b = _mm512_unpackhi_epi32(sum2, sum3);
93 __m512i sum01 = _mm512_add_epi32(sum01a, sum01b);
94 __m512i sum23 = _mm512_add_epi32(sum23a, sum23b);
96 __m512i sum0123a = _mm512_unpacklo_epi64(sum01, sum23);
97 __m512i sum0123b = _mm512_unpackhi_epi64(sum01, sum23);
99 __m512i sum = _mm512_add_epi32(sum0123a, sum0123b);
101 __m256i sum256lo = _mm512_castsi512_si256(sum);
102 __m256i sum256hi = _mm512_extracti64x4_epi64(sum, 1);
104 sum256lo = _mm256_add_epi32(sum256lo, sum256hi);
106 __m128i sum128lo = _mm256_castsi256_si128(sum256lo);
107 __m128i sum128hi = _mm256_extracti128_si256(sum256lo, 1);
109 return _mm_add_epi32(_mm_add_epi32(sum128lo, sum128hi), bias);
112 [[maybe_unused]] auto m512_add_dpbusd_epi32 = [=](__m512i& acc, __m512i a, __m512i b) {
113 #if defined (USE_VNNI)
114 acc = _mm512_dpbusd_epi32(acc, a, b);
116 __m512i product0 = _mm512_maddubs_epi16(a, b);
117 product0 = _mm512_madd_epi16(product0, kOnes512);
118 acc = _mm512_add_epi32(acc, product0);
123 #if defined (USE_AVX2)
125 [[maybe_unused]] const __m256i kOnes256 = _mm256_set1_epi16(1);
127 [[maybe_unused]] auto m256_hadd = [](__m256i sum, int bias) -> int {
128 __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(sum), _mm256_extracti128_si256(sum, 1));
129 sum128 = _mm_add_epi32(sum128, _mm_shuffle_epi32(sum128, _MM_PERM_BADC));
130 sum128 = _mm_add_epi32(sum128, _mm_shuffle_epi32(sum128, _MM_PERM_CDAB));
131 return _mm_cvtsi128_si32(sum128) + bias;
134 [[maybe_unused]] auto m256_haddx4 = [](__m256i sum0, __m256i sum1, __m256i sum2, __m256i sum3, __m128i bias) -> __m128i {
135 sum0 = _mm256_hadd_epi32(sum0, sum1);
136 sum2 = _mm256_hadd_epi32(sum2, sum3);
138 sum0 = _mm256_hadd_epi32(sum0, sum2);
140 __m128i sum128lo = _mm256_castsi256_si128(sum0);
141 __m128i sum128hi = _mm256_extracti128_si256(sum0, 1);
143 return _mm_add_epi32(_mm_add_epi32(sum128lo, sum128hi), bias);
146 [[maybe_unused]] auto m256_add_dpbusd_epi32 = [=](__m256i& acc, __m256i a, __m256i b) {
147 #if defined (USE_VNNI)
148 acc = _mm256_dpbusd_epi32(acc, a, b);
150 __m256i product0 = _mm256_maddubs_epi16(a, b);
151 product0 = _mm256_madd_epi16(product0, kOnes256);
152 acc = _mm256_add_epi32(acc, product0);
158 #if defined (USE_SSSE3)
160 [[maybe_unused]] const __m128i kOnes128 = _mm_set1_epi16(1);
162 [[maybe_unused]] auto m128_hadd = [](__m128i sum, int bias) -> int {
163 sum = _mm_add_epi32(sum, _mm_shuffle_epi32(sum, 0x4E)); //_MM_PERM_BADC
164 sum = _mm_add_epi32(sum, _mm_shuffle_epi32(sum, 0xB1)); //_MM_PERM_CDAB
165 return _mm_cvtsi128_si32(sum) + bias;
168 [[maybe_unused]] auto m128_haddx4 = [](__m128i sum0, __m128i sum1, __m128i sum2, __m128i sum3, __m128i bias) -> __m128i {
169 sum0 = _mm_hadd_epi32(sum0, sum1);
170 sum2 = _mm_hadd_epi32(sum2, sum3);
172 sum0 = _mm_hadd_epi32(sum0, sum2);
174 return _mm_add_epi32(sum0, bias);
177 [[maybe_unused]] auto m128_add_dpbusd_epi32 = [=](__m128i& acc, __m128i a, __m128i b) {
178 __m128i product0 = _mm_maddubs_epi16(a, b);
179 product0 = _mm_madd_epi16(product0, kOnes128);
180 acc = _mm_add_epi32(acc, product0);
185 #if defined (USE_AVX512)
187 constexpr IndexType kNumChunks512 = kPaddedInputDimensions / (kSimdWidth * 2);
188 constexpr IndexType kNumChunks256 = kPaddedInputDimensions / kSimdWidth;
190 const auto output = reinterpret_cast<OutputType*>(buffer);
192 // Since to saturate a zmm register it takes 64 bytes we
193 // cannot use AVX512 for the smaller affine transforms.
194 // Instead we fallback to a AVX2 implementation if the
195 // kInputDimensions isn't a multiple of 64.
196 // Note that this means that for example for
197 // kInputDimensions of 96 we fallback to AVX2 even though
198 // the first 64 elements could be processed with AVX512.
199 // This is caused by mixing the __m256 and __m512 variables
200 // required to better handle that case and it would
201 // require handling more cases statically not to lose performance.
202 // This should be revisited if such input dimensions are to be considered.
203 [[maybe_unused]] const auto input_vector512 = reinterpret_cast<const __m512i*>(input);
204 [[maybe_unused]] const auto input_vector256 = reinterpret_cast<const __m256i*>(input);
206 // kOutputDimensions is either 1 or a multiple of kSimdWidth
207 // because then it is also an input dimension.
208 if constexpr (kOutputDimensions % 4 == 0)
210 for (IndexType i = 0; i < kOutputDimensions; i += 4)
212 const IndexType offset0 = (i + 0) * kPaddedInputDimensions;
213 const IndexType offset1 = (i + 1) * kPaddedInputDimensions;
214 const IndexType offset2 = (i + 2) * kPaddedInputDimensions;
215 const IndexType offset3 = (i + 3) * kPaddedInputDimensions;
217 const __m128i bias = *reinterpret_cast<const __m128i*>(&biases_[i]);
218 __m128i* outptr = reinterpret_cast<__m128i*>(&output[i]);
220 if constexpr (kPaddedInputDimensions % (kSimdWidth * 2) == 0)
222 __m512i sum0 = _mm512_setzero_si512();
223 __m512i sum1 = _mm512_setzero_si512();
224 __m512i sum2 = _mm512_setzero_si512();
225 __m512i sum3 = _mm512_setzero_si512();
227 const auto row0 = reinterpret_cast<const __m512i*>(&weights_[offset0]);
228 const auto row1 = reinterpret_cast<const __m512i*>(&weights_[offset1]);
229 const auto row2 = reinterpret_cast<const __m512i*>(&weights_[offset2]);
230 const auto row3 = reinterpret_cast<const __m512i*>(&weights_[offset3]);
232 for (IndexType j = 0; j < kNumChunks512; ++j)
234 const __m512i in = input_vector512[j];
236 m512_add_dpbusd_epi32(sum0, in, row0[j]);
237 m512_add_dpbusd_epi32(sum1, in, row1[j]);
238 m512_add_dpbusd_epi32(sum2, in, row2[j]);
239 m512_add_dpbusd_epi32(sum3, in, row3[j]);
242 *outptr = m512_haddx4(sum0, sum1, sum2, sum3, bias);
246 __m256i sum0 = _mm256_setzero_si256();
247 __m256i sum1 = _mm256_setzero_si256();
248 __m256i sum2 = _mm256_setzero_si256();
249 __m256i sum3 = _mm256_setzero_si256();
251 const auto row0 = reinterpret_cast<const __m256i*>(&weights_[offset0]);
252 const auto row1 = reinterpret_cast<const __m256i*>(&weights_[offset1]);
253 const auto row2 = reinterpret_cast<const __m256i*>(&weights_[offset2]);
254 const auto row3 = reinterpret_cast<const __m256i*>(&weights_[offset3]);
256 for (IndexType j = 0; j < kNumChunks256; ++j)
258 const __m256i in = input_vector256[j];
260 m256_add_dpbusd_epi32(sum0, in, row0[j]);
261 m256_add_dpbusd_epi32(sum1, in, row1[j]);
262 m256_add_dpbusd_epi32(sum2, in, row2[j]);
263 m256_add_dpbusd_epi32(sum3, in, row3[j]);
266 *outptr = m256_haddx4(sum0, sum1, sum2, sum3, bias);
270 else if constexpr (kOutputDimensions == 1)
272 if constexpr (kPaddedInputDimensions % (kSimdWidth * 2) == 0)
274 __m512i sum0 = _mm512_setzero_si512();
276 const auto row0 = reinterpret_cast<const __m512i*>(&weights_[0]);
278 for (IndexType j = 0; j < kNumChunks512; ++j)
280 const __m512i in = input_vector512[j];
282 m512_add_dpbusd_epi32(sum0, in, row0[j]);
285 output[0] = m512_hadd(sum0, biases_[0]);
289 __m256i sum0 = _mm256_setzero_si256();
291 const auto row0 = reinterpret_cast<const __m256i*>(&weights_[0]);
293 for (IndexType j = 0; j < kNumChunks256; ++j)
295 const __m256i in = input_vector256[j];
297 m256_add_dpbusd_epi32(sum0, in, row0[j]);
300 output[0] = m256_hadd(sum0, biases_[0]);
305 // This case can never happen because kOutputDimensions
306 // is always 1 or a multiple of kSimdWidth.
310 #elif defined (USE_AVX2)
312 constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
314 const auto output = reinterpret_cast<OutputType*>(buffer);
315 const auto input_vector = reinterpret_cast<const __m256i*>(input);
317 // kOutputDimensions is either 1 or a multiple of kSimdWidth
318 // because then it is also an input dimension.
319 if constexpr (kOutputDimensions % 4 == 0)
321 for (IndexType i = 0; i < kOutputDimensions; i += 4)
323 const IndexType offset0 = (i + 0) * kPaddedInputDimensions;
324 const IndexType offset1 = (i + 1) * kPaddedInputDimensions;
325 const IndexType offset2 = (i + 2) * kPaddedInputDimensions;
326 const IndexType offset3 = (i + 3) * kPaddedInputDimensions;
328 const __m128i bias = *reinterpret_cast<const __m128i*>(&biases_[i]);
329 __m128i* outptr = reinterpret_cast<__m128i*>(&output[i]);
331 __m256i sum0 = _mm256_setzero_si256();
332 __m256i sum1 = _mm256_setzero_si256();
333 __m256i sum2 = _mm256_setzero_si256();
334 __m256i sum3 = _mm256_setzero_si256();
336 const auto row0 = reinterpret_cast<const __m256i*>(&weights_[offset0]);
337 const auto row1 = reinterpret_cast<const __m256i*>(&weights_[offset1]);
338 const auto row2 = reinterpret_cast<const __m256i*>(&weights_[offset2]);
339 const auto row3 = reinterpret_cast<const __m256i*>(&weights_[offset3]);
341 for (IndexType j = 0; j < kNumChunks; ++j)
343 const __m256i in = input_vector[j];
345 m256_add_dpbusd_epi32(sum0, in, row0[j]);
346 m256_add_dpbusd_epi32(sum1, in, row1[j]);
347 m256_add_dpbusd_epi32(sum2, in, row2[j]);
348 m256_add_dpbusd_epi32(sum3, in, row3[j]);
351 *outptr = m256_haddx4(sum0, sum1, sum2, sum3, bias);
354 else if constexpr (kOutputDimensions == 1)
356 __m256i sum0 = _mm256_setzero_si256();
358 const auto row0 = reinterpret_cast<const __m256i*>(&weights_[0]);
360 for (IndexType j = 0; j < kNumChunks; ++j)
362 const __m256i in = input_vector[j];
364 m256_add_dpbusd_epi32(sum0, in, row0[j]);
367 output[0] = m256_hadd(sum0, biases_[0]);
371 // This case can never happen because kOutputDimensions
372 // is always 1 or a multiple of kSimdWidth.
376 #elif defined (USE_SSSE3)
378 constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
380 auto output = reinterpret_cast<OutputType*>(buffer);
381 const auto input_vector = reinterpret_cast<const __m128i*>(input);
383 // kOutputDimensions is either 1 or a multiple of kSimdWidth
384 // because then it is also an input dimension.
385 if constexpr (kOutputDimensions % 4 == 0)
387 for (IndexType i = 0; i < kOutputDimensions; i += 4)
389 const IndexType offset0 = (i + 0) * kPaddedInputDimensions;
390 const IndexType offset1 = (i + 1) * kPaddedInputDimensions;
391 const IndexType offset2 = (i + 2) * kPaddedInputDimensions;
392 const IndexType offset3 = (i + 3) * kPaddedInputDimensions;
394 const __m128i bias = *reinterpret_cast<const __m128i*>(&biases_[i]);
395 __m128i* outptr = reinterpret_cast<__m128i*>(&output[i]);
397 __m128i sum0 = _mm_setzero_si128();
398 __m128i sum1 = _mm_setzero_si128();
399 __m128i sum2 = _mm_setzero_si128();
400 __m128i sum3 = _mm_setzero_si128();
402 const auto row0 = reinterpret_cast<const __m128i*>(&weights_[offset0]);
403 const auto row1 = reinterpret_cast<const __m128i*>(&weights_[offset1]);
404 const auto row2 = reinterpret_cast<const __m128i*>(&weights_[offset2]);
405 const auto row3 = reinterpret_cast<const __m128i*>(&weights_[offset3]);
407 for (int j = 0; j < (int)kNumChunks; j += 1)
409 const __m128i in = input_vector[j];
411 m128_add_dpbusd_epi32(sum0, in, row0[j]);
412 m128_add_dpbusd_epi32(sum1, in, row1[j]);
413 m128_add_dpbusd_epi32(sum2, in, row2[j]);
414 m128_add_dpbusd_epi32(sum3, in, row3[j]);
417 *outptr = m128_haddx4(sum0, sum1, sum2, sum3, bias);
420 else if constexpr (kOutputDimensions == 1)
422 __m128i sum0 = _mm_setzero_si128();
424 const auto row0 = reinterpret_cast<const __m128i*>(&weights_[0]);
426 for (int j = 0; j < (int)kNumChunks; j += 1)
428 const __m128i in = input_vector[j];
430 m128_add_dpbusd_epi32(sum0, in, row0[j]);
433 output[0] = m128_hadd(sum0, biases_[0]);
437 // This case can never happen because kOutputDimensions
438 // is always 1 or a multiple of kSimdWidth.
444 // Use old implementation for the other architectures.
446 auto output = reinterpret_cast<OutputType*>(buffer);
448 #if defined(USE_SSE2)
449 constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
451 const __m128i kZeros = _mm_setzero_si128();
453 const __m128i kOnes = _mm_set1_epi16(1);
455 const auto input_vector = reinterpret_cast<const __m128i*>(input);
457 #elif defined(USE_MMX)
458 constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
459 const __m64 kZeros = _mm_setzero_si64();
460 const auto input_vector = reinterpret_cast<const __m64*>(input);
462 #elif defined(USE_NEON)
463 constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
464 const auto input_vector = reinterpret_cast<const int8x8_t*>(input);
467 for (IndexType i = 0; i < kOutputDimensions; ++i) {
468 const IndexType offset = i * kPaddedInputDimensions;
470 #if defined(USE_SSE2)
471 __m128i sum_lo = _mm_cvtsi32_si128(biases_[i]);
472 __m128i sum_hi = kZeros;
473 const auto row = reinterpret_cast<const __m128i*>(&weights_[offset]);
474 for (IndexType j = 0; j < kNumChunks; ++j) {
475 __m128i row_j = _mm_load_si128(&row[j]);
476 __m128i input_j = _mm_load_si128(&input_vector[j]);
477 __m128i row_signs = _mm_cmpgt_epi8(kZeros, row_j);
478 __m128i extended_row_lo = _mm_unpacklo_epi8(row_j, row_signs);
479 __m128i extended_row_hi = _mm_unpackhi_epi8(row_j, row_signs);
480 __m128i extended_input_lo = _mm_unpacklo_epi8(input_j, kZeros);
481 __m128i extended_input_hi = _mm_unpackhi_epi8(input_j, kZeros);
482 __m128i product_lo = _mm_madd_epi16(extended_row_lo, extended_input_lo);
483 __m128i product_hi = _mm_madd_epi16(extended_row_hi, extended_input_hi);
484 sum_lo = _mm_add_epi32(sum_lo, product_lo);
485 sum_hi = _mm_add_epi32(sum_hi, product_hi);
487 __m128i sum = _mm_add_epi32(sum_lo, sum_hi);
488 __m128i sum_high_64 = _mm_shuffle_epi32(sum, _MM_SHUFFLE(1, 0, 3, 2));
489 sum = _mm_add_epi32(sum, sum_high_64);
490 __m128i sum_second_32 = _mm_shufflelo_epi16(sum, _MM_SHUFFLE(1, 0, 3, 2));
491 sum = _mm_add_epi32(sum, sum_second_32);
492 output[i] = _mm_cvtsi128_si32(sum);
494 #elif defined(USE_MMX)
495 __m64 sum_lo = _mm_cvtsi32_si64(biases_[i]);
496 __m64 sum_hi = kZeros;
497 const auto row = reinterpret_cast<const __m64*>(&weights_[offset]);
498 for (IndexType j = 0; j < kNumChunks; ++j) {
499 __m64 row_j = row[j];
500 __m64 input_j = input_vector[j];
501 __m64 row_signs = _mm_cmpgt_pi8(kZeros, row_j);
502 __m64 extended_row_lo = _mm_unpacklo_pi8(row_j, row_signs);
503 __m64 extended_row_hi = _mm_unpackhi_pi8(row_j, row_signs);
504 __m64 extended_input_lo = _mm_unpacklo_pi8(input_j, kZeros);
505 __m64 extended_input_hi = _mm_unpackhi_pi8(input_j, kZeros);
506 __m64 product_lo = _mm_madd_pi16(extended_row_lo, extended_input_lo);
507 __m64 product_hi = _mm_madd_pi16(extended_row_hi, extended_input_hi);
508 sum_lo = _mm_add_pi32(sum_lo, product_lo);
509 sum_hi = _mm_add_pi32(sum_hi, product_hi);
511 __m64 sum = _mm_add_pi32(sum_lo, sum_hi);
512 sum = _mm_add_pi32(sum, _mm_unpackhi_pi32(sum, sum));
513 output[i] = _mm_cvtsi64_si32(sum);
515 #elif defined(USE_NEON)
516 int32x4_t sum = {biases_[i]};
517 const auto row = reinterpret_cast<const int8x8_t*>(&weights_[offset]);
518 for (IndexType j = 0; j < kNumChunks; ++j) {
519 int16x8_t product = vmull_s8(input_vector[j * 2], row[j * 2]);
520 product = vmlal_s8(product, input_vector[j * 2 + 1], row[j * 2 + 1]);
521 sum = vpadalq_s16(sum, product);
523 output[i] = sum[0] + sum[1] + sum[2] + sum[3];
526 OutputType sum = biases_[i];
527 for (IndexType j = 0; j < kInputDimensions; ++j) {
528 sum += weights_[offset + j] * input[j];
544 using BiasType = OutputType;
545 using WeightType = std::int8_t;
547 PreviousLayer previous_layer_;
549 alignas(kCacheLineSize) BiasType biases_[kOutputDimensions];
550 alignas(kCacheLineSize)
551 WeightType weights_[kOutputDimensions * kPaddedInputDimensions];
554 } // namespace Eval::NNUE::Layers
556 #endif // #ifndef NNUE_LAYERS_AFFINE_TRANSFORM_H_INCLUDED