diff --git a/src/eval.cpp b/src/eval.cpp index 612f14e..e3ac59a 100644 --- a/src/eval.cpp +++ b/src/eval.cpp @@ -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) { diff --git a/src/nnue.cpp b/src/nnue.cpp index b3b3045..958c1ae 100644 --- a/src/nnue.cpp +++ b/src/nnue.cpp @@ -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 diff --git a/src/nnue.h b/src/nnue.h index 72fea60..fc80716 100644 --- a/src/nnue.h +++ b/src/nnue.h @@ -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