Skip to content

Commit

Permalink
Combine remove and add in update_accumulator_refresh_cache()
Browse files Browse the repository at this point in the history
Move remove before add to match other parts of the code.
  • Loading branch information
PikaCat-OuO committed Apr 29, 2024
1 parent a1d656d commit 5db5450
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions src/nnue/nnue_feature_transformer.h
Original file line number Diff line number Diff line change
Expand Up @@ -712,16 +712,20 @@ class FeatureTransformer {
for (IndexType k = 0; k < NumRegs; ++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];
const IndexType offset = HalfDimensions * index + j * TileHeight;
auto column = reinterpret_cast<const vec_t*>(&weights[offset]);
IndexType indexR = removed[i0];
const IndexType offsetR = HalfDimensions * indexR + j * TileHeight;
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)
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];
const IndexType offset = HalfDimensions * index + j * TileHeight;
Expand All @@ -730,6 +734,15 @@ class FeatureTransformer {
for (unsigned k = 0; k < NumRegs; ++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++)
vec_store(&entryTile[k], acc[k]);
Expand All @@ -742,23 +755,23 @@ class FeatureTransformer {
for (std::size_t k = 0; k < NumPsqtRegs; ++k)
psqt[k] = entryTilePsqt[k];

for (int i = 0; i < int(added.size()); ++i)
for (int i = 0; i < int(removed.size()); ++i)
{
IndexType index = added[i];
IndexType index = removed[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]);
psqt[k] = vec_sub_psqt_32(psqt[k], columnPsqt[k]);
}
for (int i = 0; i < int(removed.size()); ++i)
for (int i = 0; i < int(added.size()); ++i)
{
IndexType index = removed[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_sub_psqt_32(psqt[k], columnPsqt[k]);
psqt[k] = vec_add_psqt_32(psqt[k], columnPsqt[k]);
}

for (std::size_t k = 0; k < NumPsqtRegs; ++k)
Expand All @@ -767,23 +780,23 @@ class FeatureTransformer {

#else

for (const auto index : added)
for (const auto index : removed)
{
const IndexType offset = HalfDimensions * index;
for (IndexType j = 0; j < HalfDimensions; ++j)
entry.accumulation[Perspective][j] += weights[offset + 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];
entry.psqtAccumulation[Perspective][k] -= psqtWeights[index * PSQTBuckets + k];
}
for (const auto index : removed)
for (const auto index : added)
{
const IndexType offset = HalfDimensions * index;
for (IndexType j = 0; j < HalfDimensions; ++j)
entry.accumulation[Perspective][j] -= weights[offset + 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];
entry.psqtAccumulation[Perspective][k] += psqtWeights[index * PSQTBuckets + k];
}

#endif
Expand Down

1 comment on commit 5db5450

@giangapo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Please sign in to comment.