Files
Rigby/src/policy.h
T
rektdieandclaude 15258f952a 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>
2026-11-15 21:27:38 +01:00

34 lines
1.0 KiB
C++

#pragma once
#include <string>
#include "position.h"
namespace rigby::policy {
// Policy network trained on Lc0 visit distributions (train/policy):
// 3072 threat inputs -> HIDDEN, CReLU, pairwise product (HIDDEN / 2), then one
// output row per move, i8 weights quantised by 128. HIDDEN is read from the
// file's size, so any trained width loads.
constexpr int INPUTS = 3072;
constexpr int OUTPUTS = 3920;
// Loads the network built into the binary (Makefile POLICYFILE), if there is one.
void init();
// Loads a network file, replacing the current one; an empty path goes back to
// the built-in network (or none). False, keeping the old one, if the file does
// not have the size of a network.
bool load(const std::string& path);
bool loaded();
int hidden();
// Raw logits of `moves` in `pos` (before any temperature). Requires loaded().
void logits(const Position& pos, const MoveList& moves, float* out);
// The output row of a move, as the trainer maps it (exposed for tests).
int move_index(const Position& pos, Move m);
} // namespace rigby::policy