Simplify Accumulator Updates

Passed Non-regression STC:
LLR: 2.93 (-2.94,2.94) <-1.75,0.25>
Total: 146848 W: 37915 L: 37820 D: 71113
Ptnml(0-2): 415, 16159, 40186, 16244, 420
https://tests.stockfishchess.org/tests/view/691772217ca878185233202b

closes https://github.com/official-stockfish/Stockfish/pull/6421

No functional change
This commit is contained in:
Shawn Xu
2025-11-30 21:41:51 +01:00
committed by Disservin
parent d9fd516547
commit 1132d893e0
2 changed files with 26 additions and 32 deletions
+22 -28
View File
@@ -18,7 +18,6 @@
#include "nnue_accumulator.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <new>
@@ -338,22 +337,23 @@ struct AccumulatorUpdateContext {
};
fused_row_reduce<Vec16Wrapper, Dimensions, ops...>(
(from.template acc<Dimensions>()).accumulation[perspective],
(to.template acc<Dimensions>()).accumulation[perspective], to_weight_vector(indices)...);
(from.template acc<Dimensions>()).accumulation[perspective].data(),
(to.template acc<Dimensions>()).accumulation[perspective].data(),
to_weight_vector(indices)...);
fused_row_reduce<Vec32Wrapper, PSQTBuckets, ops...>(
(from.template acc<Dimensions>()).psqtAccumulation[perspective],
(to.template acc<Dimensions>()).psqtAccumulation[perspective],
(from.template acc<Dimensions>()).psqtAccumulation[perspective].data(),
(to.template acc<Dimensions>()).psqtAccumulation[perspective].data(),
to_psqt_weight_vector(indices)...);
}
void apply(const typename FeatureSet::IndexList& added,
const typename FeatureSet::IndexList& removed) {
const auto fromAcc = from.template acc<Dimensions>().accumulation[perspective];
const auto toAcc = to.template acc<Dimensions>().accumulation[perspective];
const auto& fromAcc = from.template acc<Dimensions>().accumulation[perspective];
auto& toAcc = to.template acc<Dimensions>().accumulation[perspective];
const auto fromPsqtAcc = from.template acc<Dimensions>().psqtAccumulation[perspective];
const auto toPsqtAcc = to.template acc<Dimensions>().psqtAccumulation[perspective];
const auto& fromPsqtAcc = from.template acc<Dimensions>().psqtAccumulation[perspective];
auto& toPsqtAcc = to.template acc<Dimensions>().psqtAccumulation[perspective];
#ifdef VECTOR
using Tiling = SIMDTiling<Dimensions, Dimensions, PSQTBuckets>;
@@ -448,8 +448,8 @@ struct AccumulatorUpdateContext {
#else
std::copy_n(fromAcc, Dimensions, toAcc);
std::copy_n(fromPsqtAcc, PSQTBuckets, toPsqtAcc);
toAcc = fromAcc;
toPsqtAcc = fromPsqtAcc;
for (const auto index : removed)
{
@@ -589,13 +589,10 @@ void update_accumulator_incremental(
auto& targetAcc = target_state.template acc<TransformedFeatureDimensions>();
const auto& sourceAcc = computed.template acc<TransformedFeatureDimensions>();
std::memcpy(targetAcc.accumulation[perspective], sourceAcc.accumulation[perspective],
sizeof(targetAcc.accumulation[perspective]));
std::memcpy(targetAcc.psqtAccumulation[perspective],
sourceAcc.psqtAccumulation[perspective],
sizeof(targetAcc.psqtAccumulation[perspective]));
targetAcc.accumulation[perspective] = sourceAcc.accumulation[perspective];
targetAcc.psqtAccumulation[perspective] = sourceAcc.psqtAccumulation[perspective];
targetAcc.computed[perspective] = true;
targetAcc.computed[perspective] = true;
return;
}
@@ -643,15 +640,16 @@ void update_accumulator_incremental(
(target_state.template acc<TransformedFeatureDimensions>()).computed[perspective] = true;
}
Bitboard get_changed_pieces(const Piece oldPieces[SQUARE_NB], const Piece newPieces[SQUARE_NB]) {
Bitboard get_changed_pieces(const std::array<Piece, SQUARE_NB>& oldPieces,
const std::array<Piece, SQUARE_NB>& newPieces) {
#if defined(USE_AVX512) || defined(USE_AVX2)
static_assert(sizeof(Piece) == 1);
Bitboard sameBB = 0;
for (int i = 0; i < 64; i += 32)
{
const __m256i old_v = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(oldPieces + i));
const __m256i new_v = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(newPieces + i));
const __m256i old_v = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&oldPieces[i]));
const __m256i new_v = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&newPieces[i]));
const __m256i cmpEqual = _mm256_cmpeq_epi8(old_v, new_v);
const std::uint32_t equalMask = _mm256_movemask_epi8(cmpEqual);
sameBB |= static_cast<Bitboard>(equalMask) << i;
@@ -680,7 +678,7 @@ void update_accumulator_refresh_cache(Color pers
auto& entry = cache[ksq][perspective];
PSQFeatureSet::IndexList removed, added;
const Bitboard changedBB = get_changed_pieces(entry.pieces, pos.piece_array().data());
const Bitboard changedBB = get_changed_pieces(entry.pieces, pos.piece_array());
Bitboard removedBB = changedBB & entry.pieceBB;
Bitboard addedBB = changedBB & pos.pieces();
@@ -696,7 +694,7 @@ void update_accumulator_refresh_cache(Color pers
}
entry.pieceBB = pos.pieces();
std::copy_n(pos.piece_array().begin(), SQUARE_NB, entry.pieces);
entry.pieces = pos.piece_array();
auto& accumulator = accumulatorState.acc<Dimensions>();
accumulator.computed[perspective] = true;
@@ -812,12 +810,8 @@ void update_accumulator_refresh_cache(Color pers
// The accumulator of the refresh entry has been updated.
// Now copy its content to the actual accumulator we were refreshing.
std::memcpy(accumulator.accumulation[perspective], entry.accumulation.data(),
sizeof(BiasType) * Dimensions);
std::memcpy(accumulator.psqtAccumulation[perspective], entry.psqtAccumulation.data(),
sizeof(int32_t) * PSQTBuckets);
accumulator.accumulation[perspective] = entry.accumulation;
accumulator.psqtAccumulation[perspective] = entry.psqtAccumulation;
#endif
}
+4 -4
View File
@@ -46,9 +46,9 @@ class FeatureTransformer;
// Class that holds the result of affine transformation of input features
template<IndexType Size>
struct alignas(CacheLineSize) Accumulator {
std::int16_t accumulation[COLOR_NB][Size];
std::int32_t psqtAccumulation[COLOR_NB][PSQTBuckets];
std::array<bool, COLOR_NB> computed = {};
std::array<std::array<std::int16_t, Size>, COLOR_NB> accumulation;
std::array<std::array<std::int32_t, PSQTBuckets>, COLOR_NB> psqtAccumulation;
std::array<bool, COLOR_NB> computed = {};
};
@@ -71,7 +71,7 @@ struct AccumulatorCaches {
struct alignas(CacheLineSize) Entry {
std::array<BiasType, Size> accumulation;
std::array<PSQTWeightType, PSQTBuckets> psqtAccumulation;
Piece pieces[SQUARE_NB];
std::array<Piece, SQUARE_NB> pieces;
Bitboard pieceBB;
// To initialize a refresh entry, we set all its bitboards empty,