Author SHA1 Message Date
rektdieandclaude 2d88bf3030 feat: policy temperature by depth (Monty's base PST)
With the policy network the priors are sharp everywhere, and one
temperature (1.0) served every depth. Monty softens the policy most at
the root and less further down: temperature = 1 - 0.0960 +
(depth - 0.3349)^-1.5777 with the root at depth 1, about 2.80 at the
root, 1.35 one ply down, 1.12 at two plies and 0.90 deep in the tree.
Rigby now does the same; PolicyTemp still multiplies it (x1.00). The
butterfly-history bias is added to the logits before the temperature,
as in Monty. Monty's raised temperature in winning positions is left
for a separate patch.

Tree reuse brings nodes closer to the root that were expanded deeper,
with a lower temperature, so after a reuse the new root and its
children get their priors recomputed for their new depth (Monty's
relabel_policy does the same for depths 1 and 2).

Checked: bench deterministic; RIGBY_VERIFY_TREE build driven over UCI
with pauses between commands, so each search runs: tree reuse of 6k
to 64k nodes (relabel), collections at Hash 1 MB, wtime/winc,
movetime, go infinite + stop, without an abort.

Bench: 598576

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 22:28:09 +01:00
2 changed files with 61 additions and 15 deletions

No files matched your search

+44 -13
View File
@@ -318,7 +318,32 @@ float Searcher::evaluate_leaf(u32 idx, const Position& pos, int depth) {
return eval::value(pos); return eval::value(pos);
} }
bool Searcher::expand(u32 idx, const Position& pos) { float Searcher::policy_temperature(int depth) const {
const float d = float(depth + 1); // Monty counts the root as depth 1
return params.policy_temperature * (1.0f - params.pst_base + std::pow(d - params.pst_root, -params.pst_power));
}
void Searcher::priors(const Position& pos, const MoveList& moves, int depth, float* out) const {
// The butterfly history of each move shifts its logit. The priors are a
// snapshot: later history updates do not change them.
float bias[256];
for (int i = 0; i < moves.size(); i++)
bias[i] = float(butterfly_.get(pos.stm, moves[i])) / ButterflyHistory::LOGIT_SCALE;
eval::policy(pos, moves, policy_temperature(depth), bias, out);
}
void Searcher::relabel(u32 idx, const Position& pos, int depth) {
const Node& node = tree_[idx];
if (node.state != EXPANDED) return;
MoveList moves;
for (u32 i = 0; i < node.num_children; i++) moves.add(Move::from_raw(tree_[node.first_child + i].move));
float p[256];
priors(pos, moves, depth, p);
for (u32 i = 0; i < node.num_children; i++)
tree_[node.first_child + i].policy = u16(std::lround(std::clamp(p[i], 0.0f, 1.0f) * 65535.0f));
}
bool Searcher::expand(u32 idx, const Position& pos, int depth) {
Node& node = tree_[idx]; Node& node = tree_[idx];
MoveList moves; MoveList moves;
@@ -332,20 +357,14 @@ bool Searcher::expand(u32 idx, const Position& pos) {
return false; return false;
} }
// The butterfly history of each move shifts its logit. The priors are a float p[256];
// snapshot: later history updates do not change them. priors(pos, moves, depth, p);
float bias[256];
for (int i = 0; i < moves.size(); i++)
bias[i] = float(butterfly_.get(pos.stm, moves[i])) / ButterflyHistory::LOGIT_SCALE;
float priors[256];
eval::policy(pos, moves, params.policy_temperature, bias, priors);
for (int i = 0; i < moves.size(); i++) { for (int i = 0; i < moves.size(); i++) {
Node& child = tree_[first + i]; Node& child = tree_[first + i];
child = Node{}; child = Node{};
child.move = moves[i].raw(); child.move = moves[i].raw();
child.policy = u16(std::lround(std::clamp(priors[i], 0.0f, 1.0f) * 65535.0f)); child.policy = u16(std::lround(std::clamp(p[i], 0.0f, 1.0f) * 65535.0f));
} }
node.first_child = first; node.first_child = first;
@@ -418,7 +437,7 @@ void Searcher::playout() {
value = evaluate_leaf(idx, pos, depth); value = evaluate_leaf(idx, pos, depth);
break; break;
} }
if (!expand(idx, pos)) { if (!expand(idx, pos, depth)) {
value = eval::value(pos); value = eval::value(pos);
break; break;
} }
@@ -574,10 +593,22 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
// The root needs its children before the first playout, so that even a // The root needs its children before the first playout, so that even a
// single playout leaves a move to play. // single playout leaves a move to play.
if (tree_[Tree::root()].state == UNEXPANDED && !expand(Tree::root(), root)) { if (tree_[Tree::root()].state == UNEXPANDED && !expand(Tree::root(), root, 0)) {
tree_.clear(); tree_.clear();
gc_pending_ = false; gc_pending_ = false;
expand(Tree::root(), root); expand(Tree::root(), root, 0);
}
// A reused root and its children were expanded deeper in the old tree,
// with a lower policy temperature: give them the one for their new depth.
if (reused) {
relabel(Tree::root(), root, 0);
const Node& r = tree_[Tree::root()];
for (u32 i = 0; i < r.num_children; i++) {
Position child = root;
child.make_move(Move::from_raw(tree_[r.first_child + i].move));
relabel(r.first_child + i, child, 1);
}
} }
// Root move visits brought along by tree reuse, for smart pruning. The // Root move visits brought along by tree reuse, for smart pruning. The
+17 -2
View File
@@ -103,7 +103,14 @@ struct SearchParams {
// (Monty: 64 x 128). // (Monty: 64 x 128).
float cpuct_visit_scale = 8192.0f; float cpuct_visit_scale = 8192.0f;
float fpu_reduction = 0.30f; float fpu_reduction = 0.30f;
// Multiplies the depth-dependent policy softmax temperature below.
float policy_temperature = 1.00f; float policy_temperature = 1.00f;
// Policy softmax temperature by depth, Monty's base PST (root = depth 1):
// 1 - base + (depth - root)^-power, about 2.80 at the root, 1.35 one ply
// down, 1.12 at two plies and 0.90 deep in the tree.
float pst_base = 0.0960f;
float pst_root = 0.3349f;
float pst_power = 1.5777f;
i64 move_overhead = 20; i64 move_overhead = 20;
}; };
@@ -180,8 +187,16 @@ private:
// First visit of a node: settles terminal states, otherwise evaluates. // First visit of a node: settles terminal states, otherwise evaluates.
float evaluate_leaf(u32 idx, const Position& pos, int depth); float evaluate_leaf(u32 idx, const Position& pos, int depth);
// Second visit: creates the children with their priors. False when the // Second visit: creates the children with their priors. False when the
// arena is full (a collection is then scheduled). // arena is full (a collection is then scheduled). `depth` is the node's
bool expand(u32 idx, const Position& pos); // distance from the root and sets the policy temperature.
bool expand(u32 idx, const Position& pos, int depth);
// Recomputes the priors of an expanded node's children for its depth (and
// the current butterfly history), for nodes that tree reuse moved closer
// to the root.
void relabel(u32 idx, const Position& pos, int depth);
float policy_temperature(int depth) const;
// Writes the priors of `moves` at `pos` for a node at `depth`.
void priors(const Position& pos, const MoveList& moves, int depth, float* out) const;
u32 select_child(u32 idx) const; u32 select_child(u32 idx) const;
bool is_repetition(const Position& pos) const; bool is_repetition(const Position& pos) const;
// Finds the node for `root` in the previous tree and makes it the root. // Finds the node for `root` in the previous tree and makes it the root.