feat: value network trained on Lc0 data

The static evaluation behind the quiescence search is now a
(768 -> 128)x2 -> 1 SCReLU network instead of the hand-written
evaluation. It was trained with bullet on linrock's Lc0-derived test80
2024-02 binpack, 40 superbatches (10 min on an RTX 3070), final loss
0.0220. The quiescence search itself is unchanged.

The network is embedded with incbin (EVALFILE, default
networks/rigby-value-v1.bin). Q is tanh(cp / 800), the same scale the
network was trained on (sigmoid(cp / 400) = expected score).

Checked: bullet's float evaluation of fixed positions (trainer --check)
and the engine's quantised inference (evalfens) agree within 6 cp; an
integer re-implementation in Python matches the engine exactly. Perft
passes, bench is deterministic.

Bench: 545914

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rektdieandclaude committed 2026-11-15 04:12:52 +01:00
1 parent 530c4d67ed
commit 30b0c36cd5
14 files changed
+700 -68

No files matched your search

+17 -53
View File
@@ -4,26 +4,24 @@
#include <cmath>
#include <cstdlib>
#include "nnue.h"
namespace rigby::eval {
namespace {
constexpr int MATE = 30000;
constexpr int QS_MAX_PLY = 32;
constexpr float Q_SCALE = 300.0f;
// The network is trained so that sigmoid(cp / 400) is the expected score, so
// Q = 2 * sigmoid(cp / 400) - 1 = tanh(cp / 800).
constexpr float Q_SCALE = 2.0f * float(nnue::SCALE);
constexpr int MG_VALUE[6] = {90, 320, 335, 480, 980, 0};
constexpr int EG_VALUE[6] = {110, 290, 305, 520, 940, 0};
constexpr int SEE_VALUE[6] = {100, 300, 310, 500, 900, 0};
constexpr int PHASE_WEIGHT[6] = {0, 1, 1, 2, 4, 0};
// Material folded into piece-square tables, indexed [piece][square].
int mg_table[12][64];
int eg_table[12][64];
// Positional part only (no material), white's view, used by the policy.
// Positional piece-square bonuses, white's view, used by the policy.
int mg_pst[6][64];
void pst_for(int type, int sq, int& mg, int& eg) {
int pst_mg(int type, int sq) {
const int f = file_of(sq);
const int r = rank_of(sq);
const int center = std::abs(2 * f - 7) + std::abs(2 * r - 7); // 2..14
@@ -31,29 +29,17 @@ void pst_for(int type, int sq, int& mg, int& eg) {
switch (type) {
case PAWN:
mg = (r >= 1 && r <= 6) ? (r - 1) * 6 + (central_file ? (r - 1) * 4 : 0) : 0;
eg = (r >= 1 && r <= 6) ? (r - 1) * 14 : 0;
break;
return (r >= 1 && r <= 6) ? (r - 1) * 6 + (central_file ? (r - 1) * 4 : 0) : 0;
case KNIGHT:
mg = 24 - center * 4;
eg = 18 - center * 3;
break;
return 24 - center * 4;
case BISHOP:
mg = 12 - center * 2;
eg = 10 - center * 2;
break;
return 12 - center * 2;
case ROOK:
mg = (r == 6 ? 20 : 0) + (central_file ? 6 : 0);
eg = r == 6 ? 15 : 0;
break;
return (r == 6 ? 20 : 0) + (central_file ? 6 : 0);
case QUEEN:
mg = 6 - center;
eg = 20 - center * 2;
break;
default: // KING: shelter in the middlegame, centralise in the endgame
mg = -std::min(r, 3) * 25 + ((f <= 2 || f >= 6) ? 15 : -15);
eg = 30 - center * 4;
break;
return 6 - center;
default: // KING: stay sheltered
return -std::min(r, 3) * 25 + ((f <= 2 || f >= 6) ? 15 : -15);
}
}
@@ -64,35 +50,13 @@ int victim_type(const Position& pos, Move m) { return m.is_en_passant() ? PAWN :
void init() {
for (int type = PAWN; type <= KING; type++) {
for (int sq = 0; sq < 64; sq++) {
int mg, eg;
pst_for(type, sq, mg, eg);
mg_pst[type][sq] = mg;
mg_table[make_piece(WHITE, type)][sq] = MG_VALUE[type] + mg;
eg_table[make_piece(WHITE, type)][sq] = EG_VALUE[type] + eg;
mg_table[make_piece(BLACK, type)][sq ^ 56] = -(MG_VALUE[type] + mg);
eg_table[make_piece(BLACK, type)][sq ^ 56] = -(EG_VALUE[type] + eg);
mg_pst[type][sq] = pst_mg(type, sq);
}
}
nnue::init();
}
int static_eval(const Position& pos) {
int mg = 0, eg = 0, phase = 0;
u64 occ = pos.occupancy();
while (occ) {
const int sq = pop_lsb(occ);
const int piece = pos.piece_on(sq);
mg += mg_table[piece][sq];
eg += eg_table[piece][sq];
phase += PHASE_WEIGHT[piece_type(piece)];
}
phase = std::min(phase, 24);
int score = (mg * phase + eg * (24 - phase)) / 24;
if (popcount(pos.pieces_of(WHITE, BISHOP)) >= 2) score += 30;
if (popcount(pos.pieces_of(BLACK, BISHOP)) >= 2) score -= 30;
return (pos.stm == WHITE ? score : -score) + 10;
}
int static_eval(const Position& pos) { return nnue::evaluate(pos); }
int quiescence(const Position& pos, int alpha, int beta, int ply) {
const bool in_check = pos.in_check();
+3 -3
View File
@@ -4,12 +4,12 @@
namespace rigby::eval {
// Stand-in evaluation until the network arrives. Everything here is replaced by
// the value and policy heads of the net trained on Lc0 data.
// Value: the network trained on Lc0 data, behind a capture-only quiescence
// search. Policy: move heuristics until a policy network exists.
void init();
// Static evaluation in centipawns, from the side to move's view.
// Static evaluation (the value network) in centipawns, side to move's view.
int static_eval(const Position& pos);
// Capture-only search on top of the static evaluation, side to move's view.
+85
View File
@@ -0,0 +1,85 @@
#include "nnue.h"
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "incbin.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;
}
inline i64 screlu(i16 x) {
const i64 y = std::clamp<i64>(x, 0, QA);
return y * y;
}
} // 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));
}
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));
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]);
}
}
// 256 products of up to QA^2 * |w| overflow 32 bits in the worst case.
i64 out = 0;
for (int i = 0; i < HIDDEN; i++) out += screlu(us[i]) * net.l1w[i];
for (int i = 0; i < HIDDEN; i++) out += screlu(them[i]) * net.l1w[HIDDEN + i];
out /= QA;
out += net.l1b;
out *= SCALE;
out /= i64(QA) * QB;
return int(out);
}
} // namespace rigby::nnue
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include "position.h"
namespace rigby::nnue {
// Value network trained with bullet on Lc0 data (see train/README.md):
// (768 -> HIDDEN)x2 -> 1, SCReLU, dual perspective, quantised i16.
constexpr int HIDDEN = 128;
constexpr int SCALE = 400;
constexpr int QA = 255;
constexpr int QB = 64;
// 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:
// sigmoid(cp / SCALE) is the expected score.
int evaluate(const Position& pos);
} // namespace rigby::nnue
+1
View File
@@ -11,6 +11,7 @@ using u32 = std::uint32_t;
using u16 = std::uint16_t;
using u8 = std::uint8_t;
using i64 = std::int64_t;
using i16 = std::int16_t;
constexpr std::string_view START_FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
+20
View File
@@ -1,6 +1,7 @@
#include "uci.h"
#include <algorithm>
#include <cstdio>
#include <chrono>
#include <iostream>
#include <memory>
@@ -229,6 +230,25 @@ void uci_loop() {
} else if (cmd == "eval") {
send("static " + std::to_string(eval::static_eval(e.pos)) + " cp, qsearch " +
std::to_string(eval::quiescence(e.pos, -30000, 30000, 0)) + " cp (side to move)");
} else if (cmd == "evalfens") {
// Same positions as the trainer's --check, to compare inference
static constexpr const char* FENS[] = {
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1",
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1",
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R b KQkq - 0 1",
"6k1/5pp1/1p2p2p/1Pq1Pn2/2P2P2/6P1/4Q1BP/6K1 w - - 1 30",
"8/8/3k4/2p1p3/2P1P3/3K4/8/8 w - - 0 1",
"4k3/8/8/8/8/8/8/R3K3 w - - 0 1",
"4k3/8/8/8/8/8/8/R3K3 b - - 0 1",
};
for (const char* fen : FENS) {
Position pos;
pos.set_fen(fen);
char line[160];
std::snprintf(line, sizeof(line), "%6d cp %s", eval::static_eval(pos), fen);
send(line);
}
} else if (cmd == "perft") {
int depth = 5;
is >> depth;