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