From 8d517bddfffffd9d292fb5362ce452e5eb056ce9 Mon Sep 17 00:00:00 2001 From: Shawn Xu Date: Thu, 9 Jan 2025 20:22:41 -0800 Subject: [PATCH] Simplify accumulator updates AMD Ryzen 5 7600X ``` sf_base = 1902646 +/- 2114 (95%) sf_test = 1920873 +/- 2515 (95%) diff = 18227 +/- 3067 (95%) speedup = 0.95800% +/- 0.161% (95%) ``` Ryzen 9 5950X ``` sf_base = 1413387 +/- 3592 (95%) sf_test = 1437893 +/- 3355 (95%) diff = 24505 +/- 4669 (95%) speedup = 1.73380% +/- 0.330% (95%) ``` Intel Core i7-6700K ``` sf_base = 912476 +/- 1863 (95%) sf_test = 921864 +/- 2042 (95%) diff = 9388 +/- 3333 (95%) speedup = 1.02893% +/- 0.365% (95%) ``` Raspberry Pi 5 ``` sf_base = 260993 +/- 1508 (95%) sf_test = 262912 +/- 1746 (95%) diff = 1918 +/- 1221 (95%) speedup = 0.73504% +/- 0.468% (95%) ``` Passed Non-regression STC: LLR: 2.95 (-2.94,2.94) <-1.75,0.25> Total: 23072 W: 6041 L: 5813 D: 11218 Ptnml(0-2): 61, 2435, 6319, 2657, 64 https://tests.stockfishchess.org/tests/view/6780a0ca9168c8bf30927757 closes https://github.com/official-stockfish/Stockfish/pull/5759 No functional change --- src/nnue/nnue_feature_transformer.h | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/nnue/nnue_feature_transformer.h b/src/nnue/nnue_feature_transformer.h index b047f62c..8649d952 100644 --- a/src/nnue/nnue_feature_transformer.h +++ b/src/nnue/nnue_feature_transformer.h @@ -472,9 +472,8 @@ class FeatureTransformer { return st; } - // It computes the accumulator of the next position, or updates the - // current position's accumulator if CurrentOnly is true. - template + // Computes the accumulator of the next position. + template void update_accumulator_incremental(const Position& pos, StateInfo* computed) const { assert((computed->*accPtr).computed[Perspective]); assert(computed->next != nullptr); @@ -493,16 +492,10 @@ class FeatureTransformer { // feature set's update cost calculation to be correct and never allow // updates with more added/removed features than MaxActiveDimensions. FeatureSet::IndexList removed, added; + FeatureSet::append_changed_indices(ksq, computed->next->dirtyPiece, removed, + added); - if constexpr (CurrentOnly) - for (StateInfo* st = pos.state(); st != computed; st = st->previous) - FeatureSet::append_changed_indices(ksq, st->dirtyPiece, removed, - added); - else - FeatureSet::append_changed_indices(ksq, computed->next->dirtyPiece, - removed, added); - - StateInfo* next = CurrentOnly ? pos.state() : computed->next; + StateInfo* next = computed->next; assert(!(next->*accPtr).computed[Perspective]); #ifdef VECTOR @@ -665,8 +658,8 @@ class FeatureTransformer { (next->*accPtr).computed[Perspective] = true; - if (!CurrentOnly && next != pos.state()) - update_accumulator_incremental(pos, next); + if (next != pos.state()) + update_accumulator_incremental(pos, next); } template @@ -844,7 +837,7 @@ class FeatureTransformer { StateInfo* oldest = try_find_computed_accumulator(pos); if ((oldest->*accPtr).computed[Perspective] && oldest != pos.state()) - update_accumulator_incremental(pos, oldest); + update_accumulator_incremental(pos, oldest); else update_accumulator_refresh_cache(pos, cache); } @@ -858,7 +851,7 @@ class FeatureTransformer { if ((oldest->*accPtr).computed[Perspective] && oldest != pos.state()) // Start from the oldest computed accumulator, update all the // accumulators up to the current position. - update_accumulator_incremental(pos, oldest); + update_accumulator_incremental(pos, oldest); else update_accumulator_refresh_cache(pos, cache); }