#include "nnue.h" #include #include #include #include #include #include "incbin.h" #include "simd.h" #ifndef EVALFILE #error "EVALFILE must name the network file (the Makefile sets it)" #endif INCBIN(RigbyNet, EVALFILE); namespace rigby::nnue { namespace { // Layout of bullet's quantised.bin, padded by bullet to a multiple of 64 bytes. struct alignas(64) Network { i16 l0w[768][HIDDEN]; // feature-major: one HIDDEN-wide column per input i16 l0b[HIDDEN]; i16 l1w[2 * HIDDEN]; // side to move's half first i16 l1b; }; Network net; // Input index from one side's point of view: its own pieces first, and the // board flipped vertically for black (bullet's Chess768). inline int feature(int perspective, int piece, int sq) { const int theirs = piece_color(piece) != perspective; const int rel_sq = perspective == WHITE ? sq : sq ^ 56; return 384 * theirs + 64 * piece_type(piece) + rel_sq; } // The output layer squares the clamped hidden value with v * (v * w): v * w // must fit 16 bits, so the weights are checked on load. constexpr int MAX_L1W = 32767 / QA; // The output sum is taken in blocks that fit 32 bits, then added up in 64. constexpr int BLOCK = 128; static_assert(i64(2) * BLOCK * QA * QA * MAX_L1W <= INT_MAX); static_assert(HIDDEN % BLOCK == 0 && BLOCK % simd::LANES == 0); } // namespace void init() { if (gRigbyNetSize < sizeof(Network)) { std::fprintf(stderr, "network %s is %u bytes, expected at least %u\n", EVALFILE, unsigned(gRigbyNetSize), unsigned(sizeof(Network))); std::exit(1); } std::memcpy(&net, gRigbyNetData, sizeof(Network)); for (int i = 0; i < 2 * HIDDEN; i++) { if (std::abs(net.l1w[i]) > MAX_L1W) { std::fprintf(stderr, "network %s has an output weight of %d, the limit is %d\n", EVALFILE, net.l1w[i], MAX_L1W); std::exit(1); } } } void refresh(const Position& pos, Accumulator& acc) { const i16* rows[2][32]; int count = 0; u64 occ = pos.occupancy(); while (occ) { const int sq = pop_lsb(occ); const int piece = pos.piece_on(sq); rows[WHITE][count] = net.l0w[feature(WHITE, piece, sq)]; rows[BLACK][count++] = net.l0w[feature(BLACK, piece, sq)]; } for (int side : {WHITE, BLACK}) simd::accumulate(acc.values[side], net.l0b, rows[side], count, HIDDEN); } 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, pos.castle_rook_square(m)); 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]]; using simd::load, simd::store, simd::LANES; if (adds == 1 && subs == 1) { for (int i = 0; i < HIDDEN; i += LANES) store(dst + i, simd::sub(simd::add(load(src + i), load(a0 + i)), load(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 += LANES) { const simd::vi16 added = simd::add(load(src + i), load(a0 + i)); store(dst + i, simd::sub(simd::sub(added, load(s0 + i)), load(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 += LANES) { const simd::vi16 added = simd::add(simd::add(load(src + i), load(a0 + i)), load(a1 + i)); store(dst + i, simd::sub(simd::sub(added, load(s0 + i)), load(s1 + i))); } } } } int evaluate(const Accumulator& acc, int stm) { const i16* us = acc.values[stm]; const i16* them = acc.values[stm ^ 1]; // SCReLU as v * (v * w) with v clamped to [0, QA]: exact, since v * w fits // 16 bits and a block of both halves fits 32 (checked above). using namespace simd; const vi16 lo = set1(0), hi = set1(QA); i64 out = 0; for (int b = 0; b < HIDDEN; b += BLOCK) { vi32 sum_us = zero32(), sum_them = zero32(); for (int i = b; i < b + BLOCK; i += LANES) { const vi16 u = clamp(load(us + i), lo, hi); const vi16 t = clamp(load(them + i), lo, hi); sum_us = dot(sum_us, mullo(u, load(net.l1w + i)), u); sum_them = dot(sum_them, mullo(t, load(net.l1w + HIDDEN + i)), t); } out += reduce(add32(sum_us, sum_them)); } out /= QA; out += net.l1b; out *= SCALE; out /= i64(QA) * QB; return int(out); } int evaluate(const Position& pos) { Accumulator acc; refresh(pos, acc); return evaluate(acc, pos.stm); } } // namespace rigby::nnue