Simplify Forward and Backward

Forward and Backward are not independent so simplify to a bool.

Cleanup some MSVC warnings like "warning C4267: 'argument': conversion
from 'size_t' to 'int', possible loss of data" Other minor formatting
stuff.

This is a rebase of https://github.com/official-stockfish/Stockfish/pull/5912

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

No functional change
This commit is contained in:
mstembera
2025-04-18 14:32:19 +02:00
committed by Disservin
parent fb6a3e04ec
commit 1577fa0470
2 changed files with 15 additions and 30 deletions
+15 -25
View File
@@ -29,7 +29,6 @@
#include "../types.h" #include "../types.h"
#include "network.h" #include "network.h"
#include "nnue_architecture.h" #include "nnue_architecture.h"
#include "nnue_common.h"
#include "nnue_feature_transformer.h" #include "nnue_feature_transformer.h"
namespace Stockfish::Eval::NNUE { namespace Stockfish::Eval::NNUE {
@@ -48,9 +47,7 @@ namespace Stockfish::Eval::NNUE {
namespace { namespace {
template<Color Perspective, template<Color Perspective, bool Forward, IndexType TransformedFeatureDimensions>
IncUpdateDirection Direction = FORWARD,
IndexType TransformedFeatureDimensions>
void update_accumulator_incremental( void update_accumulator_incremental(
const FeatureTransformer<TransformedFeatureDimensions>& featureTransformer, const FeatureTransformer<TransformedFeatureDimensions>& featureTransformer,
const Square ksq, const Square ksq,
@@ -161,8 +158,8 @@ void AccumulatorStack::forward_update_incremental(
const Square ksq = pos.square<KING>(Perspective); const Square ksq = pos.square<KING>(Perspective);
for (std::size_t next = begin + 1; next < m_current_idx; next++) for (std::size_t next = begin + 1; next < m_current_idx; next++)
update_accumulator_incremental<Perspective>(featureTransformer, ksq, m_accumulators[next], update_accumulator_incremental<Perspective, true>(
m_accumulators[next - 1]); featureTransformer, ksq, m_accumulators[next], m_accumulators[next - 1]);
assert((latest().acc<Dimensions>()).computed[Perspective]); assert((latest().acc<Dimensions>()).computed[Perspective]);
} }
@@ -180,7 +177,7 @@ void AccumulatorStack::backward_update_incremental(
const Square ksq = pos.square<KING>(Perspective); const Square ksq = pos.square<KING>(Perspective);
for (std::size_t next = m_current_idx - 2; next >= end; next--) for (std::size_t next = m_current_idx - 2; next >= end; next--)
update_accumulator_incremental<Perspective, BACKWARD>( update_accumulator_incremental<Perspective, false>(
featureTransformer, ksq, m_accumulators[next], m_accumulators[next + 1]); featureTransformer, ksq, m_accumulators[next], m_accumulators[next + 1]);
assert((m_accumulators[end].acc<Dimensions>()).computed[Perspective]); assert((m_accumulators[end].acc<Dimensions>()).computed[Perspective]);
@@ -259,16 +256,12 @@ auto make_accumulator_update_context(const FeatureTransformer<Dimensions>& featu
accumulatorTo}; accumulatorTo};
} }
template<Color Perspective, IncUpdateDirection Direction, IndexType TransformedFeatureDimensions> template<Color Perspective, bool Forward, IndexType TransformedFeatureDimensions>
void update_accumulator_incremental( void update_accumulator_incremental(
const FeatureTransformer<TransformedFeatureDimensions>& featureTransformer, const FeatureTransformer<TransformedFeatureDimensions>& featureTransformer,
const Square ksq, const Square ksq,
AccumulatorState& target_state, AccumulatorState& target_state,
const AccumulatorState& computed) { const AccumulatorState& computed) {
[[maybe_unused]] constexpr bool Forward = Direction == FORWARD;
[[maybe_unused]] constexpr bool Backward = Direction == BACKWARD;
assert(Forward != Backward);
assert((computed.acc<TransformedFeatureDimensions>()).computed[Perspective]); assert((computed.acc<TransformedFeatureDimensions>()).computed[Perspective]);
assert(!(target_state.acc<TransformedFeatureDimensions>()).computed[Perspective]); assert(!(target_state.acc<TransformedFeatureDimensions>()).computed[Perspective]);
@@ -288,11 +281,8 @@ void update_accumulator_incremental(
assert(added.size() == 1 || added.size() == 2); assert(added.size() == 1 || added.size() == 2);
assert(removed.size() == 1 || removed.size() == 2); assert(removed.size() == 1 || removed.size() == 2);
assert((Forward && added.size() <= removed.size())
if (Forward) || (!Forward && added.size() >= removed.size()));
assert(added.size() <= removed.size());
else
assert(removed.size() <= added.size());
// Workaround compiler warning for uninitialized variables, replicated on // Workaround compiler warning for uninitialized variables, replicated on
// profile builds on windows with gcc 14.2.0. // profile builds on windows with gcc 14.2.0.
@@ -303,7 +293,7 @@ void update_accumulator_incremental(
auto updateContext = auto updateContext =
make_accumulator_update_context<Perspective>(featureTransformer, computed, target_state); make_accumulator_update_context<Perspective>(featureTransformer, computed, target_state);
if ((Forward && removed.size() == 1) || (Backward && added.size() == 1)) if ((Forward && removed.size() == 1) || (!Forward && added.size() == 1))
{ {
assert(added.size() == 1 && removed.size() == 1); assert(added.size() == 1 && removed.size() == 1);
updateContext.template apply<Add, Sub>(added[0], removed[0]); updateContext.template apply<Add, Sub>(added[0], removed[0]);
@@ -313,7 +303,7 @@ void update_accumulator_incremental(
assert(removed.size() == 2); assert(removed.size() == 2);
updateContext.template apply<Add, Sub, Sub>(added[0], removed[0], removed[1]); updateContext.template apply<Add, Sub, Sub>(added[0], removed[0], removed[1]);
} }
else if (Backward && removed.size() == 1) else if (!Forward && removed.size() == 1)
{ {
assert(added.size() == 2); assert(added.size() == 2);
updateContext.template apply<Add, Add, Sub>(added[0], added[1], removed[0]); updateContext.template apply<Add, Add, Sub>(added[0], added[1], removed[0]);
@@ -380,7 +370,7 @@ void update_accumulator_refresh_cache(const FeatureTransformer<Dimensions>& feat
for (IndexType k = 0; k < Tiling::NumRegs; ++k) for (IndexType k = 0; k < Tiling::NumRegs; ++k)
acc[k] = entryTile[k]; acc[k] = entryTile[k];
std::size_t i = 0; IndexType i = 0;
for (; i < std::min(removed.size(), added.size()) - combineLast3; ++i) for (; i < std::min(removed.size(), added.size()) - combineLast3; ++i)
{ {
IndexType indexR = removed[i]; IndexType indexR = removed[i];
@@ -460,10 +450,10 @@ void update_accumulator_refresh_cache(const FeatureTransformer<Dimensions>& feat
auto* entryTilePsqt = auto* entryTilePsqt =
reinterpret_cast<psqt_vec_t*>(&entry.psqtAccumulation[j * Tiling::PsqtTileHeight]); reinterpret_cast<psqt_vec_t*>(&entry.psqtAccumulation[j * Tiling::PsqtTileHeight]);
for (std::size_t k = 0; k < Tiling::NumPsqtRegs; ++k) for (IndexType k = 0; k < Tiling::NumPsqtRegs; ++k)
psqt[k] = entryTilePsqt[k]; psqt[k] = entryTilePsqt[k];
for (std::size_t i = 0; i < removed.size(); ++i) for (IndexType i = 0; i < removed.size(); ++i)
{ {
IndexType index = removed[i]; IndexType index = removed[i];
const IndexType offset = PSQTBuckets * index + j * Tiling::PsqtTileHeight; const IndexType offset = PSQTBuckets * index + j * Tiling::PsqtTileHeight;
@@ -473,7 +463,7 @@ void update_accumulator_refresh_cache(const FeatureTransformer<Dimensions>& feat
for (std::size_t k = 0; k < Tiling::NumPsqtRegs; ++k) for (std::size_t k = 0; k < Tiling::NumPsqtRegs; ++k)
psqt[k] = vec_sub_psqt_32(psqt[k], columnPsqt[k]); psqt[k] = vec_sub_psqt_32(psqt[k], columnPsqt[k]);
} }
for (std::size_t i = 0; i < added.size(); ++i) for (IndexType i = 0; i < added.size(); ++i)
{ {
IndexType index = added[i]; IndexType index = added[i];
const IndexType offset = PSQTBuckets * index + j * Tiling::PsqtTileHeight; const IndexType offset = PSQTBuckets * index + j * Tiling::PsqtTileHeight;
@@ -484,9 +474,9 @@ void update_accumulator_refresh_cache(const FeatureTransformer<Dimensions>& feat
psqt[k] = vec_add_psqt_32(psqt[k], columnPsqt[k]); psqt[k] = vec_add_psqt_32(psqt[k], columnPsqt[k]);
} }
for (std::size_t k = 0; k < Tiling::NumPsqtRegs; ++k) for (IndexType k = 0; k < Tiling::NumPsqtRegs; ++k)
vec_store_psqt(&entryTilePsqt[k], psqt[k]); vec_store_psqt(&entryTilePsqt[k], psqt[k]);
for (std::size_t k = 0; k < Tiling::NumPsqtRegs; ++k) for (IndexType k = 0; k < Tiling::NumPsqtRegs; ++k)
vec_store_psqt(&accTilePsqt[k], psqt[k]); vec_store_psqt(&accTilePsqt[k], psqt[k]);
} }
-5
View File
@@ -279,11 +279,6 @@ inline void write_leb_128(std::ostream& stream, const IntType* values, std::size
flush(); flush();
} }
enum IncUpdateDirection {
FORWARD,
BACKWARD
};
} // namespace Stockfish::Eval::NNUE } // namespace Stockfish::Eval::NNUE
#endif // #ifndef NNUE_COMMON_H_INCLUDED #endif // #ifndef NNUE_COMMON_H_INCLUDED