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:
1 parent
807ddd90c1
commit
8ceee77166
3 files changed
+106
-16
No files matched your search
+71
-11
@@ -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
|
||||
Reference in new issue
Block a user