1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-29 16:23:09 +00:00

Combine remove and add in update_accumulator_refresh_cache()

Combine remove and add in update_accumulator_refresh_cache().
Move remove before add to match other parts of the code.

STC:
https://tests.stockfishchess.org/tests/view/662d96dc6115ff6764c7f4ca
LLR: 2.95 (-2.94,2.94) <0.00,2.00>
Total: 364032 W: 94421 L: 93624 D: 175987
Ptnml(0-2): 1261, 41983, 94811, 42620, 1341

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

Bench: 1836777
This commit is contained in:
mstembera 2024-04-28 10:28:25 -07:00 committed by Disservin
parent 940a3a7383
commit a129c0695b
4 changed files with 48 additions and 33 deletions

View file

@ -60,8 +60,9 @@ Value Eval::evaluate(const Eval::NNUE::Networks& networks,
int nnueComplexity; int nnueComplexity;
int v; int v;
Value nnue = smallNet ? networks.small.evaluate(pos, &caches.small, true, &nnueComplexity, psqtOnly) Value nnue = smallNet
: networks.big.evaluate(pos, &caches.big, true, &nnueComplexity, false); ? networks.small.evaluate(pos, &caches.small, true, &nnueComplexity, psqtOnly)
: networks.big.evaluate(pos, &caches.big, true, &nnueComplexity, false);
const auto adjustEval = [&](int optDiv, int nnueDiv, int npmDiv, int pawnCountConstant, const auto adjustEval = [&](int optDiv, int nnueDiv, int npmDiv, int pawnCountConstant,
int pawnCountMul, int npmConstant, int evalDiv, int pawnCountMul, int npmConstant, int evalDiv,

View file

@ -102,7 +102,7 @@ struct AccumulatorCaches {
small.clear(networks.small); small.clear(networks.small);
} }
Cache<TransformedFeatureDimensionsBig> big; Cache<TransformedFeatureDimensionsBig> big;
Cache<TransformedFeatureDimensionsSmall> small; Cache<TransformedFeatureDimensionsSmall> small;
}; };

View file

@ -660,8 +660,8 @@ class FeatureTransformer {
bool psqtOnly) const { bool psqtOnly) const {
assert(cache != nullptr); assert(cache != nullptr);
Square ksq = pos.square<KING>(Perspective); Square ksq = pos.square<KING>(Perspective);
auto& entry = (*cache)[ksq]; auto& entry = (*cache)[ksq];
FeatureSet::IndexList removed, added; FeatureSet::IndexList removed, added;
if (entry.psqtOnly && !psqtOnly) if (entry.psqtOnly && !psqtOnly)
@ -712,16 +712,20 @@ class FeatureTransformer {
for (IndexType k = 0; k < NumRegs; ++k) for (IndexType k = 0; k < NumRegs; ++k)
acc[k] = entryTile[k]; acc[k] = entryTile[k];
for (int i = 0; i < int(added.size()); ++i) int i0 = 0;
for (; i0 < int(std::min(removed.size(), added.size())); ++i0)
{ {
IndexType index = added[i]; IndexType indexR = removed[i0];
const IndexType offset = HalfDimensions * index + j * TileHeight; const IndexType offsetR = HalfDimensions * indexR + j * TileHeight;
auto column = reinterpret_cast<const vec_t*>(&weights[offset]); auto columnR = reinterpret_cast<const vec_t*>(&weights[offsetR]);
IndexType indexA = added[i0];
const IndexType offsetA = HalfDimensions * indexA + j * TileHeight;
auto columnA = reinterpret_cast<const vec_t*>(&weights[offsetA]);
for (unsigned k = 0; k < NumRegs; ++k) for (unsigned k = 0; k < NumRegs; ++k)
acc[k] = vec_add_16(acc[k], column[k]); acc[k] = vec_add_16(vec_sub_16(acc[k], columnR[k]), columnA[k]);
} }
for (int i = 0; i < int(removed.size()); ++i) for (int i = i0; i < int(removed.size()); ++i)
{ {
IndexType index = removed[i]; IndexType index = removed[i];
const IndexType offset = HalfDimensions * index + j * TileHeight; const IndexType offset = HalfDimensions * index + j * TileHeight;
@ -730,6 +734,15 @@ class FeatureTransformer {
for (unsigned k = 0; k < NumRegs; ++k) for (unsigned k = 0; k < NumRegs; ++k)
acc[k] = vec_sub_16(acc[k], column[k]); acc[k] = vec_sub_16(acc[k], column[k]);
} }
for (int i = i0; i < int(added.size()); ++i)
{
IndexType index = added[i];
const IndexType offset = HalfDimensions * index + j * TileHeight;
auto column = reinterpret_cast<const vec_t*>(&weights[offset]);
for (unsigned k = 0; k < NumRegs; ++k)
acc[k] = vec_add_16(acc[k], column[k]);
}
for (IndexType k = 0; k < NumRegs; k++) for (IndexType k = 0; k < NumRegs; k++)
vec_store(&entryTile[k], acc[k]); vec_store(&entryTile[k], acc[k]);
@ -742,15 +755,6 @@ class FeatureTransformer {
for (std::size_t k = 0; k < NumPsqtRegs; ++k) for (std::size_t k = 0; k < NumPsqtRegs; ++k)
psqt[k] = entryTilePsqt[k]; psqt[k] = entryTilePsqt[k];
for (int i = 0; i < int(added.size()); ++i)
{
IndexType index = added[i];
const IndexType offset = PSQTBuckets * index + j * PsqtTileHeight;
auto columnPsqt = reinterpret_cast<const psqt_vec_t*>(&psqtWeights[offset]);
for (std::size_t k = 0; k < NumPsqtRegs; ++k)
psqt[k] = vec_add_psqt_32(psqt[k], columnPsqt[k]);
}
for (int i = 0; i < int(removed.size()); ++i) for (int i = 0; i < int(removed.size()); ++i)
{ {
IndexType index = removed[i]; IndexType index = removed[i];
@ -760,6 +764,15 @@ class FeatureTransformer {
for (std::size_t k = 0; k < NumPsqtRegs; ++k) for (std::size_t k = 0; k < NumPsqtRegs; ++k)
psqt[k] = vec_sub_psqt_32(psqt[k], columnPsqt[k]); psqt[k] = vec_sub_psqt_32(psqt[k], columnPsqt[k]);
} }
for (int i = 0; i < int(added.size()); ++i)
{
IndexType index = added[i];
const IndexType offset = PSQTBuckets * index + j * PsqtTileHeight;
auto columnPsqt = reinterpret_cast<const psqt_vec_t*>(&psqtWeights[offset]);
for (std::size_t k = 0; k < NumPsqtRegs; ++k)
psqt[k] = vec_add_psqt_32(psqt[k], columnPsqt[k]);
}
for (std::size_t k = 0; k < NumPsqtRegs; ++k) for (std::size_t k = 0; k < NumPsqtRegs; ++k)
vec_store_psqt(&entryTilePsqt[k], psqt[k]); vec_store_psqt(&entryTilePsqt[k], psqt[k]);
@ -767,18 +780,6 @@ class FeatureTransformer {
#else #else
for (const auto index : added)
{
if (!psqtOnly)
{
const IndexType offset = HalfDimensions * index;
for (IndexType j = 0; j < HalfDimensions; ++j)
entry.accumulation[Perspective][j] += weights[offset + j];
}
for (std::size_t k = 0; k < PSQTBuckets; ++k)
entry.psqtAccumulation[Perspective][k] += psqtWeights[index * PSQTBuckets + k];
}
for (const auto index : removed) for (const auto index : removed)
{ {
if (!psqtOnly) if (!psqtOnly)
@ -791,6 +792,18 @@ class FeatureTransformer {
for (std::size_t k = 0; k < PSQTBuckets; ++k) for (std::size_t k = 0; k < PSQTBuckets; ++k)
entry.psqtAccumulation[Perspective][k] -= psqtWeights[index * PSQTBuckets + k]; entry.psqtAccumulation[Perspective][k] -= psqtWeights[index * PSQTBuckets + k];
} }
for (const auto index : added)
{
if (!psqtOnly)
{
const IndexType offset = HalfDimensions * index;
for (IndexType j = 0; j < HalfDimensions; ++j)
entry.accumulation[Perspective][j] += weights[offset + j];
}
for (std::size_t k = 0; k < PSQTBuckets; ++k)
entry.psqtAccumulation[Perspective][k] += psqtWeights[index * PSQTBuckets + k];
}
#endif #endif

View file

@ -48,7 +48,8 @@ void hint_common_parent_position(const Position& pos,
int simpleEvalAbs = std::abs(simple_eval(pos, pos.side_to_move())); int simpleEvalAbs = std::abs(simple_eval(pos, pos.side_to_move()));
if (simpleEvalAbs > Eval::SmallNetThreshold) if (simpleEvalAbs > Eval::SmallNetThreshold)
networks.small.hint_common_access(pos, &caches.small, simpleEvalAbs > Eval::PsqtOnlyThreshold); networks.small.hint_common_access(pos, &caches.small,
simpleEvalAbs > Eval::PsqtOnlyThreshold);
else else
networks.big.hint_common_access(pos, &caches.big, false); networks.big.hint_common_access(pos, &caches.big, false);
} }