Merge pull request 'Incremental NNUE accumulator in quiescence' (#9) from nnue-incremental into main

SPRT https://bench.rektdie.com/test/5290/ (non-regression)
Elo   | 27.50 +- 10.61 (95%)
SPRT  | 8.0+0.08s Threads=1 Hash=64MB
LLR   | 3.07 (-2.94, 2.94) [-5.00, 0.00]
Games | N: 1608 W: 575 L: 448 D: 585
Penta | [28, 107, 443, 162, 64]

Bench: 546966
This commit was merged in pull request #9.
This commit is contained in:
claude committed 2026-11-15 15:18:27 +01:00
commit 786783d7d1
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) {
+71 -11
View File
@@ -51,24 +51,78 @@ void init() {
std::memcpy(&net, gRigbyNetData, sizeof(Network));
}
int evaluate(const Position& pos) {
alignas(64) i16 us[HIDDEN];
alignas(64) i16 them[HIDDEN];
std::memcpy(us, net.l0b, sizeof(us));
std::memcpy(them, net.l0b, sizeof(them));
void refresh(const Position& pos, Accumulator& acc) {
std::memcpy(acc.values[WHITE], net.l0b, sizeof(net.l0b));
std::memcpy(acc.values[BLACK], net.l0b, sizeof(net.l0b));
const int stm = pos.stm;
u64 occ = pos.occupancy();
while (occ) {
const int sq = pop_lsb(occ);
const int piece = pos.piece_on(sq);
const i16* w_us = net.l0w[feature(stm, piece, sq)];
const i16* w_them = net.l0w[feature(stm ^ 1, piece, sq)];
for (int i = 0; i < HIDDEN; i++) {
us[i] = i16(us[i] + w_us[i]);
them[i] = i16(them[i] + w_them[i]);
for (int side : {WHITE, BLACK}) {
const i16* w = net.l0w[feature(side, piece, sq)];
i16* v = acc.values[side];
for (int i = 0; i < HIDDEN; i++) v[i] = i16(v[i] + w[i]);
}
}
}
void update(const Accumulator& before, const Position& pos, Move m, Accumulator& after) {
// At most two features come and two go (castling); captures and
// promotions change three. i16 sums wrap the same way in any order, so
// this matches a refresh exactly.
int add[2][2], sub[2][3];
int adds = 0, subs = 0;
const auto add_piece = [&](int piece, int sq) {
add[WHITE][adds] = feature(WHITE, piece, sq);
add[BLACK][adds++] = feature(BLACK, piece, sq);
};
const auto sub_piece = [&](int piece, int sq) {
sub[WHITE][subs] = feature(WHITE, piece, sq);
sub[BLACK][subs++] = feature(BLACK, piece, sq);
};
const int us = pos.stm;
const int from = m.from();
const int to = m.to();
const int moving = pos.piece_on(from);
sub_piece(moving, from);
add_piece(m.is_promotion() ? make_piece(us, m.promo_type()) : moving, to);
if (m.is_en_passant()) {
const int captured_sq = us == WHITE ? to - 8 : to + 8;
sub_piece(pos.piece_on(captured_sq), captured_sq);
} else if (m.is_capture()) {
sub_piece(pos.piece_on(to), to);
} else if (m.is_castle()) {
const int rank_base = us == WHITE ? 0 : 56;
const int rook = make_piece(us, ROOK);
const bool king_side = m.flag() == Move::KING_CASTLE;
sub_piece(rook, rank_base + (king_side ? 7 : 0));
add_piece(rook, rank_base + (king_side ? 5 : 3));
}
for (int side : {WHITE, BLACK}) {
const i16* src = before.values[side];
i16* dst = after.values[side];
const i16* a0 = net.l0w[add[side][0]];
const i16* s0 = net.l0w[sub[side][0]];
if (adds == 1 && subs == 1) {
for (int i = 0; i < HIDDEN; i++) dst[i] = i16(src[i] + a0[i] - s0[i]);
} else if (adds == 1) { // capture or capture-promotion
const i16* s1 = net.l0w[sub[side][1]];
for (int i = 0; i < HIDDEN; i++) dst[i] = i16(src[i] + a0[i] - s0[i] - s1[i]);
} else { // castling
const i16* a1 = net.l0w[add[side][1]];
const i16* s1 = net.l0w[sub[side][1]];
for (int i = 0; i < HIDDEN; i++) dst[i] = i16(src[i] + a0[i] + a1[i] - s0[i] - s1[i]);
}
}
}
int evaluate(const Accumulator& acc, int stm) {
const i16* us = acc.values[stm];
const i16* them = acc.values[stm ^ 1];
// 256 products of up to QA^2 * |w| overflow 32 bits in the worst case.
i64 out = 0;
@@ -82,4 +136,10 @@ int evaluate(const Position& pos) {
return int(out);
}
int evaluate(const Position& pos) {
Accumulator acc;
refresh(pos, acc);
return evaluate(acc, pos.stm);
}
} // namespace rigby::nnue
+17 -1
View File
@@ -11,11 +11,27 @@ constexpr int SCALE = 400;
constexpr int QA = 255;
constexpr int QB = 64;
// The hidden layer before the activation, from white's and from black's point
// of view. Kept up to date move by move along the quiescence search.
struct Accumulator {
alignas(64) i16 values[2][HIDDEN];
};
// Checks and loads the embedded network. Exits with a message if it does not fit.
void init();
// Evaluation in centipawns from the side to move's view, on the network's scale:
// Builds the accumulator of `pos` from scratch.
void refresh(const Position& pos, Accumulator& acc);
// The accumulator after `m`, from the one before it; `pos` is the position
// before the move. Gives exactly what refresh would give for the new position.
void update(const Accumulator& before, const Position& pos, Move m, Accumulator& after);
// Evaluation in centipawns from the view of `stm`, on the network's scale:
// sigmoid(cp / SCALE) is the expected score.
int evaluate(const Accumulator& acc, int stm);
// Same, building the accumulator from scratch.
int evaluate(const Position& pos);
} // namespace rigby::nnue