feat: policy network inference

src/policy.cpp runs the network train/policy writes: 3072 threat
inputs (side to move's view, flipped horizontally when its king is on
files e-h, +768 attacked, +1536 defended), a hidden layer with CReLU
and pairwise product, and one output row per move, i8 weights scaled
by 128. The hidden width is read from the file's size, so any trained
width loads. When a network is loaded, eval::policy uses its logits
plus the butterfly-history bias instead of the move heuristics; the
temperature and softmax are unchanged.

A network is built in with make POLICYFILE=<net> (default: none, so
the engine plays as before) or loaded at runtime with the UCI option
PolicyFile. policyfens prints the priors of the trainer's --check
positions.

Checked: with a smoke-trained network policyfens matches the trainer's
float output to a few tenths of a percent in all six positions (black
to move, mirrored king, castling, promotion). Speed with a network:
-13% nps at 512 hidden, -41% at 2048. A zero network built in with
POLICYFILE gives uniform priors. Without a network the bench is
unchanged.

Bench: 529903

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rektdieandclaude committed 2026-11-15 21:27:38 +01:00
1 parent 1c9d2beec3
commit 15258f952a
8 files changed
+313 -5

No files matched your search

+43
View File
@@ -13,6 +13,7 @@
#include "eval.h"
#include "mcts.h"
#include "perft.h"
#include "policy.h"
#include "position.h"
#include "pretty.h"
#include "uci_io.h"
@@ -54,6 +55,8 @@ void print_id() {
send("option name Threads type spin default 1 min 1 max 1");
send("option name MoveOverhead type spin default 20 min 0 max 5000");
send("option name UCI_Chess960 type check default false");
// A policy network file to use instead of the built-in one (or the move heuristics).
send("option name PolicyFile type string default <empty>");
// Search constants, exposed as integers (x100) so SPSA can tune them.
send("option name CPuct type spin default 160 min 1 max 1000");
send("option name RootCPuct type spin default 233 min 1 max 1000");
@@ -75,6 +78,12 @@ void set_option(Engine& e, std::istringstream& is) {
chess960 = value == "true";
return;
}
if (lname == "policyfile") {
if (!policy::load(value)) send("info string cannot load policy network " + value);
else if (policy::loaded()) send("info string policy network " + std::to_string(policy::hidden()) + " hidden");
else send("info string no policy network, using move heuristics");
return;
}
int v = 0;
try {
@@ -269,6 +278,40 @@ void uci_loop() {
std::snprintf(line, sizeof(line), "%6d cp %s", eval::static_eval(pos), fen);
send(line);
}
} else if (cmd == "policyfens") {
// Same positions as the policy 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/4P3/8/PPPP1PPP/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/2P2k2/8/8/8/8/5K2/8 w - - 0 1",
};
if (!policy::loaded()) {
send("info string no policy network loaded");
continue;
}
for (const char* fen : FENS) {
Position pos;
pos.set_fen(fen);
MoveList moves;
generate_legal(pos, moves);
float priors[256];
eval::policy(pos, moves, 1.0f, nullptr, priors);
std::vector<std::pair<float, std::string>> lines;
for (int i = 0; i < moves.size(); i++) lines.push_back({priors[i], pos.uci(moves[i])});
std::sort(lines.begin(), lines.end(), [](const auto& a, const auto& b) {
return a.first != b.first ? a.first > b.first : a.second < b.second;
});
send("");
send(fen);
for (const auto& [p, uci] : lines) {
char line[64];
std::snprintf(line, sizeof(line), " %-6s %6.2f%%", uci.c_str(), p * 100.0f);
send(line);
}
}
} else if (cmd == "perft") {
int depth = 5;
is >> depth;