]> git.sesse.net Git - stockfish/blob - src/nnue/nnue_feature_transformer.h
Update copyright years
[stockfish] / src / nnue / nnue_feature_transformer.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2023 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
27 #include <cstring> // std::memset()
28
29 namespace Stockfish::Eval::NNUE {
30
31   using BiasType       = std::int16_t;
32   using WeightType     = std::int16_t;
33   using PSQTWeightType = std::int32_t;
34
35   // If vector instructions are enabled, we update and refresh the
36   // accumulator tile by tile such that each tile fits in the CPU's
37   // vector registers.
38   #define VECTOR
39
40   static_assert(PSQTBuckets % 8 == 0,
41     "Per feature PSQT values cannot be processed at granularity lower than 8 at a time.");
42
43   #ifdef USE_AVX512
44   typedef __m512i vec_t;
45   typedef __m256i psqt_vec_t;
46   #define vec_load(a) _mm512_load_si512(a)
47   #define vec_store(a,b) _mm512_store_si512(a,b)
48   #define vec_add_16(a,b) _mm512_add_epi16(a,b)
49   #define vec_sub_16(a,b) _mm512_sub_epi16(a,b)
50   #define vec_mul_16(a,b) _mm512_mullo_epi16(a,b)
51   #define vec_zero() _mm512_setzero_epi32()
52   #define vec_set_16(a) _mm512_set1_epi16(a)
53   #define vec_max_16(a,b) _mm512_max_epi16(a,b)
54   #define vec_min_16(a,b) _mm512_min_epi16(a,b)
55   inline vec_t vec_msb_pack_16(vec_t a, vec_t b){
56     vec_t compacted = _mm512_packs_epi16(_mm512_srli_epi16(a,7),_mm512_srli_epi16(b,7));
57     return _mm512_permutexvar_epi64(_mm512_setr_epi64(0, 2, 4, 6, 1, 3, 5, 7), compacted);
58   }
59   #define vec_load_psqt(a) _mm256_load_si256(a)
60   #define vec_store_psqt(a,b) _mm256_store_si256(a,b)
61   #define vec_add_psqt_32(a,b) _mm256_add_epi32(a,b)
62   #define vec_sub_psqt_32(a,b) _mm256_sub_epi32(a,b)
63   #define vec_zero_psqt() _mm256_setzero_si256()
64   #define NumRegistersSIMD 32
65   #define MaxChunkSize 64
66
67   #elif USE_AVX2
68   typedef __m256i vec_t;
69   typedef __m256i psqt_vec_t;
70   #define vec_load(a) _mm256_load_si256(a)
71   #define vec_store(a,b) _mm256_store_si256(a,b)
72   #define vec_add_16(a,b) _mm256_add_epi16(a,b)
73   #define vec_sub_16(a,b) _mm256_sub_epi16(a,b)
74   #define vec_mul_16(a,b) _mm256_mullo_epi16(a,b)
75   #define vec_zero() _mm256_setzero_si256()
76   #define vec_set_16(a) _mm256_set1_epi16(a)
77   #define vec_max_16(a,b) _mm256_max_epi16(a,b)
78   #define vec_min_16(a,b) _mm256_min_epi16(a,b)
79   inline vec_t vec_msb_pack_16(vec_t a, vec_t b){
80     vec_t compacted = _mm256_packs_epi16(_mm256_srli_epi16(a,7), _mm256_srli_epi16(b,7));
81     return _mm256_permute4x64_epi64(compacted, 0b11011000);
82   }
83   #define vec_load_psqt(a) _mm256_load_si256(a)
84   #define vec_store_psqt(a,b) _mm256_store_si256(a,b)
85   #define vec_add_psqt_32(a,b) _mm256_add_epi32(a,b)
86   #define vec_sub_psqt_32(a,b) _mm256_sub_epi32(a,b)
87   #define vec_zero_psqt() _mm256_setzero_si256()
88   #define NumRegistersSIMD 16
89   #define MaxChunkSize 32
90
91   #elif USE_SSE2
92   typedef __m128i vec_t;
93   typedef __m128i psqt_vec_t;
94   #define vec_load(a) (*(a))
95   #define vec_store(a,b) *(a)=(b)
96   #define vec_add_16(a,b) _mm_add_epi16(a,b)
97   #define vec_sub_16(a,b) _mm_sub_epi16(a,b)
98   #define vec_mul_16(a,b) _mm_mullo_epi16(a,b)
99   #define vec_zero() _mm_setzero_si128()
100   #define vec_set_16(a) _mm_set1_epi16(a)
101   #define vec_max_16(a,b) _mm_max_epi16(a,b)
102   #define vec_min_16(a,b) _mm_min_epi16(a,b)
103   #define vec_msb_pack_16(a,b) _mm_packs_epi16(_mm_srli_epi16(a,7),_mm_srli_epi16(b,7))
104   #define vec_load_psqt(a) (*(a))
105   #define vec_store_psqt(a,b) *(a)=(b)
106   #define vec_add_psqt_32(a,b) _mm_add_epi32(a,b)
107   #define vec_sub_psqt_32(a,b) _mm_sub_epi32(a,b)
108   #define vec_zero_psqt() _mm_setzero_si128()
109   #define NumRegistersSIMD (Is64Bit ? 16 : 8)
110   #define MaxChunkSize 16
111
112   #elif USE_MMX
113   typedef __m64 vec_t;
114   typedef __m64 psqt_vec_t;
115   #define vec_load(a) (*(a))
116   #define vec_store(a,b) *(a)=(b)
117   #define vec_add_16(a,b) _mm_add_pi16(a,b)
118   #define vec_sub_16(a,b) _mm_sub_pi16(a,b)
119   #define vec_mul_16(a,b) _mm_mullo_pi16(a,b)
120   #define vec_zero() _mm_setzero_si64()
121   #define vec_set_16(a) _mm_set1_pi16(a)
122   inline vec_t vec_max_16(vec_t a,vec_t b){
123     vec_t comparison = _mm_cmpgt_pi16(a,b);
124     return _mm_or_si64(_mm_and_si64(comparison, a), _mm_andnot_si64(comparison, b));
125   }
126   inline vec_t vec_min_16(vec_t a,vec_t b){
127     vec_t comparison = _mm_cmpgt_pi16(a,b);
128     return _mm_or_si64(_mm_and_si64(comparison, b), _mm_andnot_si64(comparison, a));
129   }
130   #define vec_msb_pack_16(a,b) _mm_packs_pi16(_mm_srli_pi16(a,7),_mm_srli_pi16(b,7))
131   #define vec_load_psqt(a) (*(a))
132   #define vec_store_psqt(a,b) *(a)=(b)
133   #define vec_add_psqt_32(a,b) _mm_add_pi32(a,b)
134   #define vec_sub_psqt_32(a,b) _mm_sub_pi32(a,b)
135   #define vec_zero_psqt() _mm_setzero_si64()
136   #define vec_cleanup() _mm_empty()
137   #define NumRegistersSIMD 8
138   #define MaxChunkSize 8
139
140   #elif USE_NEON
141   typedef int16x8_t vec_t;
142   typedef int32x4_t psqt_vec_t;
143   #define vec_load(a) (*(a))
144   #define vec_store(a,b) *(a)=(b)
145   #define vec_add_16(a,b) vaddq_s16(a,b)
146   #define vec_sub_16(a,b) vsubq_s16(a,b)
147   #define vec_mul_16(a,b) vmulq_s16(a,b)
148   #define vec_zero() vec_t{0}
149   #define vec_set_16(a) vdupq_n_s16(a)
150   #define vec_max_16(a,b) vmaxq_s16(a,b)
151   #define vec_min_16(a,b) vminq_s16(a,b)
152   inline vec_t vec_msb_pack_16(vec_t a, vec_t b){
153     const int8x8_t shifta = vshrn_n_s16(a, 7);
154     const int8x8_t shiftb = vshrn_n_s16(b, 7);
155     const int8x16_t compacted = vcombine_s8(shifta,shiftb);
156     return *reinterpret_cast<const vec_t*> (&compacted);
157   }
158   #define vec_load_psqt(a) (*(a))
159   #define vec_store_psqt(a,b) *(a)=(b)
160   #define vec_add_psqt_32(a,b) vaddq_s32(a,b)
161   #define vec_sub_psqt_32(a,b) vsubq_s32(a,b)
162   #define vec_zero_psqt() psqt_vec_t{0}
163   #define NumRegistersSIMD 16
164   #define MaxChunkSize 16
165
166   #else
167   #undef VECTOR
168
169   #endif
170
171
172   #ifdef VECTOR
173
174       // Compute optimal SIMD register count for feature transformer accumulation.
175
176       // We use __m* types as template arguments, which causes GCC to emit warnings
177       // about losing some attribute information. This is irrelevant to us as we
178       // only take their size, so the following pragma are harmless.
179       #if defined(__GNUC__)
180       #pragma GCC diagnostic push
181       #pragma GCC diagnostic ignored "-Wignored-attributes"
182       #endif
183
184       template <typename SIMDRegisterType,
185                 typename LaneType,
186                 int      NumLanes,
187                 int      MaxRegisters>
188       static constexpr int BestRegisterCount()
189       {
190           #define RegisterSize  sizeof(SIMDRegisterType)
191           #define LaneSize      sizeof(LaneType)
192
193           static_assert(RegisterSize >= LaneSize);
194           static_assert(MaxRegisters <= NumRegistersSIMD);
195           static_assert(MaxRegisters > 0);
196           static_assert(NumRegistersSIMD > 0);
197           static_assert(RegisterSize % LaneSize == 0);
198           static_assert((NumLanes * LaneSize) % RegisterSize == 0);
199
200           const int ideal = (NumLanes * LaneSize) / RegisterSize;
201           if (ideal <= MaxRegisters)
202             return ideal;
203
204           // Look for the largest divisor of the ideal register count that is smaller than MaxRegisters
205           for (int divisor = MaxRegisters; divisor > 1; --divisor)
206             if (ideal % divisor == 0)
207               return divisor;
208
209           return 1;
210       }
211
212       static constexpr int NumRegs     = BestRegisterCount<vec_t, WeightType, TransformedFeatureDimensions, NumRegistersSIMD>();
213       static constexpr int NumPsqtRegs = BestRegisterCount<psqt_vec_t, PSQTWeightType, PSQTBuckets, NumRegistersSIMD>();
214       #if defined(__GNUC__)
215       #pragma GCC diagnostic pop
216       #endif
217   #endif
218
219
220
221   // Input feature converter
222   class FeatureTransformer {
223
224    private:
225     // Number of output dimensions for one side
226     static constexpr IndexType HalfDimensions = TransformedFeatureDimensions;
227
228     #ifdef VECTOR
229     static constexpr IndexType TileHeight = NumRegs * sizeof(vec_t) / 2;
230     static constexpr IndexType PsqtTileHeight = NumPsqtRegs * sizeof(psqt_vec_t) / 4;
231     static_assert(HalfDimensions % TileHeight == 0, "TileHeight must divide HalfDimensions");
232     static_assert(PSQTBuckets % PsqtTileHeight == 0, "PsqtTileHeight must divide PSQTBuckets");
233     #endif
234
235    public:
236     // Output type
237     using OutputType = TransformedFeatureType;
238
239     // Number of input/output dimensions
240     static constexpr IndexType InputDimensions = FeatureSet::Dimensions;
241     static constexpr IndexType OutputDimensions = HalfDimensions;
242
243     // Size of forward propagation buffer
244     static constexpr std::size_t BufferSize =
245         OutputDimensions * sizeof(OutputType);
246
247     // Hash value embedded in the evaluation file
248     static constexpr std::uint32_t get_hash_value() {
249       return FeatureSet::HashValue ^ (OutputDimensions * 2);
250     }
251
252     // Read network parameters
253     bool read_parameters(std::istream& stream) {
254
255       read_little_endian<BiasType      >(stream, biases     , HalfDimensions                  );
256       read_little_endian<WeightType    >(stream, weights    , HalfDimensions * InputDimensions);
257       read_little_endian<PSQTWeightType>(stream, psqtWeights, PSQTBuckets    * InputDimensions);
258
259       return !stream.fail();
260     }
261
262     // Write network parameters
263     bool write_parameters(std::ostream& stream) const {
264
265       write_little_endian<BiasType      >(stream, biases     , HalfDimensions                  );
266       write_little_endian<WeightType    >(stream, weights    , HalfDimensions * InputDimensions);
267       write_little_endian<PSQTWeightType>(stream, psqtWeights, PSQTBuckets    * InputDimensions);
268
269       return !stream.fail();
270     }
271
272     // Convert input features
273     std::int32_t transform(const Position& pos, OutputType* output, int bucket) const {
274       update_accumulator<WHITE>(pos);
275       update_accumulator<BLACK>(pos);
276
277       const Color perspectives[2] = {pos.side_to_move(), ~pos.side_to_move()};
278       const auto& accumulation = pos.state()->accumulator.accumulation;
279       const auto& psqtAccumulation = pos.state()->accumulator.psqtAccumulation;
280
281       const auto psqt = (
282             psqtAccumulation[perspectives[0]][bucket]
283           - psqtAccumulation[perspectives[1]][bucket]
284         ) / 2;
285
286
287       for (IndexType p = 0; p < 2; ++p)
288       {
289           const IndexType offset = (HalfDimensions / 2) * p;
290
291 #if defined(VECTOR)
292
293           constexpr IndexType OutputChunkSize = MaxChunkSize;
294           static_assert((HalfDimensions / 2) % OutputChunkSize == 0);
295           constexpr IndexType NumOutputChunks = HalfDimensions / 2 / OutputChunkSize;
296
297           vec_t Zero = vec_zero();
298           vec_t One = vec_set_16(127);
299
300           const vec_t* in0 = reinterpret_cast<const vec_t*>(&(accumulation[perspectives[p]][0]));
301           const vec_t* in1 = reinterpret_cast<const vec_t*>(&(accumulation[perspectives[p]][HalfDimensions / 2]));
302                 vec_t* out = reinterpret_cast<      vec_t*>(output + offset);
303
304           for (IndexType j = 0; j < NumOutputChunks; j += 1)
305           {
306               const vec_t sum0a = vec_max_16(vec_min_16(in0[j * 2 + 0], One), Zero);
307               const vec_t sum0b = vec_max_16(vec_min_16(in0[j * 2 + 1], One), Zero);
308               const vec_t sum1a = vec_max_16(vec_min_16(in1[j * 2 + 0], One), Zero);
309               const vec_t sum1b = vec_max_16(vec_min_16(in1[j * 2 + 1], One), Zero);
310
311               const vec_t pa = vec_mul_16(sum0a, sum1a);
312               const vec_t pb = vec_mul_16(sum0b, sum1b);
313
314               out[j] = vec_msb_pack_16(pa, pb);
315           }
316
317 #else
318
319           for (IndexType j = 0; j < HalfDimensions / 2; ++j) {
320               BiasType sum0 = accumulation[static_cast<int>(perspectives[p])][j + 0];
321               BiasType sum1 = accumulation[static_cast<int>(perspectives[p])][j + HalfDimensions / 2];
322               sum0 = std::max<int>(0, std::min<int>(127, sum0));
323               sum1 = std::max<int>(0, std::min<int>(127, sum1));
324               output[offset + j] = static_cast<OutputType>(sum0 * sum1 / 128);
325           }
326
327 #endif
328       }
329
330 #if defined(vec_cleanup)
331       vec_cleanup();
332 #endif
333
334       return psqt;
335
336    } // end of function transform()
337
338
339
340    private:
341     template<Color Perspective>
342     void update_accumulator(const Position& pos) const {
343
344       // The size must be enough to contain the largest possible update.
345       // That might depend on the feature set and generally relies on the
346       // feature set's update cost calculation to be correct and never
347       // allow updates with more added/removed features than MaxActiveDimensions.
348
349   #ifdef VECTOR
350       // Gcc-10.2 unnecessarily spills AVX2 registers if this array
351       // is defined in the VECTOR code below, once in each branch
352       vec_t acc[NumRegs];
353       psqt_vec_t psqt[NumPsqtRegs];
354   #endif
355
356       // Look for a usable accumulator of an earlier position. We keep track
357       // of the estimated gain in terms of features to be added/subtracted.
358       StateInfo *st = pos.state(), *next = nullptr;
359       int gain = FeatureSet::refresh_cost(pos);
360       while (st->previous && !st->accumulator.computed[Perspective])
361       {
362         // This governs when a full feature refresh is needed and how many
363         // updates are better than just one full refresh.
364         if (   FeatureSet::requires_refresh(st, Perspective)
365             || (gain -= FeatureSet::update_cost(st) + 1) < 0)
366           break;
367         next = st;
368         st = st->previous;
369       }
370
371       if (st->accumulator.computed[Perspective])
372       {
373         if (next == nullptr)
374           return;
375
376         // Update incrementally in two steps. First, we update the "next"
377         // accumulator. Then, we update the current accumulator (pos.state()).
378
379         // Gather all features to be updated.
380         const Square ksq = pos.square<KING>(Perspective);
381         FeatureSet::IndexList removed[2], added[2];
382         FeatureSet::append_changed_indices<Perspective>(
383           ksq, next->dirtyPiece, removed[0], added[0]);
384         for (StateInfo *st2 = pos.state(); st2 != next; st2 = st2->previous)
385           FeatureSet::append_changed_indices<Perspective>(
386             ksq, st2->dirtyPiece, removed[1], added[1]);
387
388         // Mark the accumulators as computed.
389         next->accumulator.computed[Perspective] = true;
390         pos.state()->accumulator.computed[Perspective] = true;
391
392         // Now update the accumulators listed in states_to_update[], where the last element is a sentinel.
393         StateInfo *states_to_update[3] =
394           { next, next == pos.state() ? nullptr : pos.state(), nullptr };
395   #ifdef VECTOR
396         for (IndexType j = 0; j < HalfDimensions / TileHeight; ++j)
397         {
398           // Load accumulator
399           auto accTile = reinterpret_cast<vec_t*>(
400             &st->accumulator.accumulation[Perspective][j * TileHeight]);
401           for (IndexType k = 0; k < NumRegs; ++k)
402             acc[k] = vec_load(&accTile[k]);
403
404           for (IndexType i = 0; states_to_update[i]; ++i)
405           {
406             // Difference calculation for the deactivated features
407             for (const auto index : removed[i])
408             {
409               const IndexType offset = HalfDimensions * index + j * TileHeight;
410               auto column = reinterpret_cast<const vec_t*>(&weights[offset]);
411               for (IndexType k = 0; k < NumRegs; ++k)
412                 acc[k] = vec_sub_16(acc[k], column[k]);
413             }
414
415             // Difference calculation for the activated features
416             for (const auto index : added[i])
417             {
418               const IndexType offset = HalfDimensions * index + j * TileHeight;
419               auto column = reinterpret_cast<const vec_t*>(&weights[offset]);
420               for (IndexType k = 0; k < NumRegs; ++k)
421                 acc[k] = vec_add_16(acc[k], column[k]);
422             }
423
424             // Store accumulator
425             accTile = reinterpret_cast<vec_t*>(
426               &states_to_update[i]->accumulator.accumulation[Perspective][j * TileHeight]);
427             for (IndexType k = 0; k < NumRegs; ++k)
428               vec_store(&accTile[k], acc[k]);
429           }
430         }
431
432         for (IndexType j = 0; j < PSQTBuckets / PsqtTileHeight; ++j)
433         {
434           // Load accumulator
435           auto accTilePsqt = reinterpret_cast<psqt_vec_t*>(
436             &st->accumulator.psqtAccumulation[Perspective][j * PsqtTileHeight]);
437           for (std::size_t k = 0; k < NumPsqtRegs; ++k)
438             psqt[k] = vec_load_psqt(&accTilePsqt[k]);
439
440           for (IndexType i = 0; states_to_update[i]; ++i)
441           {
442             // Difference calculation for the deactivated features
443             for (const auto index : removed[i])
444             {
445               const IndexType offset = PSQTBuckets * index + j * PsqtTileHeight;
446               auto columnPsqt = reinterpret_cast<const psqt_vec_t*>(&psqtWeights[offset]);
447               for (std::size_t k = 0; k < NumPsqtRegs; ++k)
448                 psqt[k] = vec_sub_psqt_32(psqt[k], columnPsqt[k]);
449             }
450
451             // Difference calculation for the activated features
452             for (const auto index : added[i])
453             {
454               const IndexType offset = PSQTBuckets * index + j * PsqtTileHeight;
455               auto columnPsqt = reinterpret_cast<const psqt_vec_t*>(&psqtWeights[offset]);
456               for (std::size_t k = 0; k < NumPsqtRegs; ++k)
457                 psqt[k] = vec_add_psqt_32(psqt[k], columnPsqt[k]);
458             }
459
460             // Store accumulator
461             accTilePsqt = reinterpret_cast<psqt_vec_t*>(
462               &states_to_update[i]->accumulator.psqtAccumulation[Perspective][j * PsqtTileHeight]);
463             for (std::size_t k = 0; k < NumPsqtRegs; ++k)
464               vec_store_psqt(&accTilePsqt[k], psqt[k]);
465           }
466         }
467
468   #else
469         for (IndexType i = 0; states_to_update[i]; ++i)
470         {
471           std::memcpy(states_to_update[i]->accumulator.accumulation[Perspective],
472               st->accumulator.accumulation[Perspective],
473               HalfDimensions * sizeof(BiasType));
474
475           for (std::size_t k = 0; k < PSQTBuckets; ++k)
476             states_to_update[i]->accumulator.psqtAccumulation[Perspective][k] = st->accumulator.psqtAccumulation[Perspective][k];
477
478           st = states_to_update[i];
479
480           // Difference calculation for the deactivated features
481           for (const auto index : removed[i])
482           {
483             const IndexType offset = HalfDimensions * index;
484
485             for (IndexType j = 0; j < HalfDimensions; ++j)
486               st->accumulator.accumulation[Perspective][j] -= weights[offset + j];
487
488             for (std::size_t k = 0; k < PSQTBuckets; ++k)
489               st->accumulator.psqtAccumulation[Perspective][k] -= psqtWeights[index * PSQTBuckets + k];
490           }
491
492           // Difference calculation for the activated features
493           for (const auto index : added[i])
494           {
495             const IndexType offset = HalfDimensions * index;
496
497             for (IndexType j = 0; j < HalfDimensions; ++j)
498               st->accumulator.accumulation[Perspective][j] += weights[offset + j];
499
500             for (std::size_t k = 0; k < PSQTBuckets; ++k)
501               st->accumulator.psqtAccumulation[Perspective][k] += psqtWeights[index * PSQTBuckets + k];
502           }
503         }
504   #endif
505       }
506       else
507       {
508         // Refresh the accumulator
509         auto& accumulator = pos.state()->accumulator;
510         accumulator.computed[Perspective] = true;
511         FeatureSet::IndexList active;
512         FeatureSet::append_active_indices<Perspective>(pos, active);
513
514   #ifdef VECTOR
515         for (IndexType j = 0; j < HalfDimensions / TileHeight; ++j)
516         {
517           auto biasesTile = reinterpret_cast<const vec_t*>(
518               &biases[j * TileHeight]);
519           for (IndexType k = 0; k < NumRegs; ++k)
520             acc[k] = biasesTile[k];
521
522           for (const auto index : active)
523           {
524             const IndexType offset = HalfDimensions * index + j * TileHeight;
525             auto column = reinterpret_cast<const vec_t*>(&weights[offset]);
526
527             for (unsigned k = 0; k < NumRegs; ++k)
528               acc[k] = vec_add_16(acc[k], column[k]);
529           }
530
531           auto accTile = reinterpret_cast<vec_t*>(
532               &accumulator.accumulation[Perspective][j * TileHeight]);
533           for (unsigned k = 0; k < NumRegs; k++)
534             vec_store(&accTile[k], acc[k]);
535         }
536
537         for (IndexType j = 0; j < PSQTBuckets / PsqtTileHeight; ++j)
538         {
539           for (std::size_t k = 0; k < NumPsqtRegs; ++k)
540             psqt[k] = vec_zero_psqt();
541
542           for (const auto index : active)
543           {
544             const IndexType offset = PSQTBuckets * index + j * PsqtTileHeight;
545             auto columnPsqt = reinterpret_cast<const psqt_vec_t*>(&psqtWeights[offset]);
546
547             for (std::size_t k = 0; k < NumPsqtRegs; ++k)
548               psqt[k] = vec_add_psqt_32(psqt[k], columnPsqt[k]);
549           }
550
551           auto accTilePsqt = reinterpret_cast<psqt_vec_t*>(
552             &accumulator.psqtAccumulation[Perspective][j * PsqtTileHeight]);
553           for (std::size_t k = 0; k < NumPsqtRegs; ++k)
554             vec_store_psqt(&accTilePsqt[k], psqt[k]);
555         }
556
557   #else
558         std::memcpy(accumulator.accumulation[Perspective], biases,
559             HalfDimensions * sizeof(BiasType));
560
561         for (std::size_t k = 0; k < PSQTBuckets; ++k)
562           accumulator.psqtAccumulation[Perspective][k] = 0;
563
564         for (const auto index : active)
565         {
566           const IndexType offset = HalfDimensions * index;
567
568           for (IndexType j = 0; j < HalfDimensions; ++j)
569             accumulator.accumulation[Perspective][j] += weights[offset + j];
570
571           for (std::size_t k = 0; k < PSQTBuckets; ++k)
572             accumulator.psqtAccumulation[Perspective][k] += psqtWeights[index * PSQTBuckets + k];
573         }
574   #endif
575       }
576
577   #if defined(USE_MMX)
578       _mm_empty();
579   #endif
580     }
581
582     alignas(CacheLineSize) BiasType biases[HalfDimensions];
583     alignas(CacheLineSize) WeightType weights[HalfDimensions * InputDimensions];
584     alignas(CacheLineSize) PSQTWeightType psqtWeights[InputDimensions * PSQTBuckets];
585   };
586
587 }  // namespace Stockfish::Eval::NNUE
588
589 #endif // #ifndef NNUE_FEATURE_TRANSFORMER_H_INCLUDED