perf: incremental NNUE accumulator in quiescence

Every quiescence node built the network's hidden layer from the whole
board: about 30 pieces x 2 perspectives x 128 weights, a third of a
middlegame playout. The accumulator (both perspectives) is now built
once at the root of the quiescence search and passed down; each child
gets a copy updated with the two to four features the move changes
(normal move, capture, en passant, promotion, castling). i16 sums wrap
the same way in any order, so the values, and the search, are exactly
the same as before.

Playouts per second at 100k playouts, main -> this (two runs each):

  startpos           688k -> 705k   +2%
  QGD middlegame     453k -> 495k   +9%
  Kiwipete           128k -> 161k   +26%
  Sicilian middle    385k -> 440k   +14%
  rook endgame       881k -> 905k   +3%
  bench              3.87M -> 4.39M +14%

Checked: on 3,240,535 moves from random games (276,756 captures, 3,676
castles, 433 en passant, 30,900 promotions) the updated accumulator is
byte for byte equal to a fresh one, and a whole game played on updates
alone does not drift. The bench is identical to main.

Bench: 546966

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rektdieandclaude committed 2026-11-15 15:03:40 +01:00
1 parent 807ddd90c1
commit 8ceee77166
3 files changed
+106 -16

No files matched your search

+18 -4
View File
@@ -116,13 +116,17 @@ void init() {
int static_eval(const Position& pos) { return nnue::evaluate(pos); }
int quiescence(const Position& pos, int alpha, int beta, int ply) {
namespace {
// `acc` is the network's accumulator for `pos`; each child gets its own,
// updated from it, instead of being rebuilt from the whole board.
int qsearch(const Position& pos, const nnue::Accumulator& acc, int alpha, int beta, int ply) {
const bool in_check = pos.in_check();
int best = -MATE + ply;
int stand_pat = 0;
if (!in_check) {
stand_pat = static_eval(pos);
stand_pat = nnue::evaluate(acc, pos.stm);
if (stand_pat >= beta || ply >= QS_MAX_PLY) return stand_pat;
best = stand_pat;
alpha = std::max(alpha, stand_pat);
@@ -135,7 +139,7 @@ int quiescence(const Position& pos, int alpha, int beta, int ply) {
if (in_check) {
generate_legal(pos, list);
if (list.empty()) return -MATE + ply;
if (ply >= QS_MAX_PLY) return static_eval(pos);
if (ply >= QS_MAX_PLY) return nnue::evaluate(acc, pos.stm);
} else {
generate_noisy(pos, list);
if (list.empty()) return has_legal_move(pos) ? best : 0;
@@ -168,9 +172,11 @@ int quiescence(const Position& pos, int alpha, int beta, int ply) {
std::swap(moves[i], moves[best_idx]);
std::swap(scores[i], scores[best_idx]);
nnue::Accumulator child_acc;
nnue::update(acc, pos, moves[i], child_acc);
Position child = pos;
child.make_move(moves[i]);
const int score = -quiescence(child, -beta, -alpha, ply + 1);
const int score = -qsearch(child, child_acc, -beta, -alpha, ply + 1);
if (score > best) {
best = score;
if (score > alpha) {
@@ -182,6 +188,14 @@ int quiescence(const Position& pos, int alpha, int beta, int ply) {
return best;
}
} // namespace
int quiescence(const Position& pos, int alpha, int beta, int ply) {
nnue::Accumulator acc;
nnue::refresh(pos, acc);
return qsearch(pos, acc, alpha, beta, ply);
}
float value(const Position& pos) { return cp_to_q(quiescence(pos, -MATE, MATE, 0)); }
void policy(const Position& pos, const MoveList& moves, float temperature, float* out) {