Author SHA1 Message Date
rektdieandclaude 9e7c3ef64a mcts: remove the value hash (MCGS variant 5c)
The graph shares a position's node, and with it its Q, between move
orders, which the value hash did for the tree. It still took 1/32 of Hash
and a store for every node of every playout path (about 6 % of playout
time in step 1). Its remaining use was positions the graph dropped in a
collection, or a new search without reuse.

All of Hash now goes to the graph. A simplification: Non-Regression SPRT.

The bench changes slightly (one Searcher runs all 16 bench positions, so
the value hash carried a few values between them).

Checked: zero warnings; VERIFY build: `verify` (a)-(d), the VERIFY searches of plan section 6 with forced GC at Hash 1 and 2, the reuse sequence at Hash 64, Hash 1 and from the 960 start, and the go nodes 1/2, go depth 1 smoke positions.

Bench: 765326

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 21:36:02 +01:00
2 changed files with 10 additions and 78 deletions

No files matched your search

+3 -32
View File
@@ -543,10 +543,6 @@ u32 Searcher::create_node(const Position& pos, bool is_root, float& value) {
} else if (!is_root && pos.insufficient_material()) {
node.state = TERMINAL_DRAW;
value = 0.0f;
} else if (float q; value_hash_.probe(pos.key, q)) {
// A position searched before (dropped by a collection, or from an
// earlier search) brings its Q along.
value = q;
} else {
value = eval::value(pos);
}
@@ -592,27 +588,9 @@ bool Searcher::expand(u32 idx, const Position& pos) {
return true;
}
u32 Searcher::select_edge(u32 idx, const Position& pos) const {
u32 Searcher::select_edge(u32 idx) const {
const Node& node = tree_.node(idx);
// Repetitions of this path: a child whose position already occurred on
// this playout's path or in the game, with the same side to move and
// within the reversible window, would be a draw if chosen now, whatever
// its Q says about other paths. Such an edge scores Q = 0. The window
// keys are those history_ holds 3, 5, ... plies before `pos` (4, 6, ...
// before the child), up to pos.halfmove. A 64-bit filter skips the exact
// search for most children.
const int n_hist = int(history_.size());
const int max_back = std::min(pos.halfmove, n_hist - 1);
u64 window_filter = 0;
for (int back = 3; back <= max_back; back += 2) window_filter |= 1ULL << (history_[n_hist - 1 - back] & 63);
const auto repeats = [&](u64 key) {
if (!(window_filter & (1ULL << (key & 63)))) return false;
for (int back = 3; back <= max_back; back += 2)
if (history_[n_hist - 1 - back] == key) return true;
return false;
};
// node.q is from the view of the side that moved into this node, so the side
// to move here sees -q. Visits are N(n), the node's own evaluation included.
const float parent_q = node.visits ? -node.q : 0.0f;
@@ -638,8 +616,7 @@ u32 Searcher::select_edge(u32 idx, const Position& pos) const {
const u32 c = node.first_edge + i;
const Edge& e = tree_.edge(c);
// The child's Q is from the view of the side to move here.
float q = e.visits ? visited_edge_q(e) : fpu;
if (window_filter && e.child && repeats(tree_.node(e.child).key)) q = 0.0f;
const float q = e.visits ? visited_edge_q(e) : fpu;
const float score = q + explore * e.prior() / float(1 + e.visits);
if (score > best_score) {
best_score = score;
@@ -690,7 +667,7 @@ void Searcher::playout() {
break;
}
const u32 e = select_edge(idx, pos);
const u32 e = select_edge(idx);
Edge& edge = tree_.edge(e);
pos.make_move(Move::from_raw(edge.move));
history_.push_back(pos.key);
@@ -747,10 +724,6 @@ void Searcher::playout() {
node.visits++;
node.base++;
}
if (leaf == NEW_NODE || leaf == TERMINAL || leaf == NOT_EXPANDED) {
const Node& node = tree_.node(path_.back());
value_hash_.store(node.key, -node.q, node.visits);
}
// Up the path: each edge counts the visit (and the draw), and its parent
// node recomputes its Q from its children. `value` is from the view of
@@ -768,8 +741,6 @@ void Searcher::playout() {
Node& node = tree_.node(path_[j]);
node.visits++;
recompute_q(node);
// The Q from the view of the side to move at the node.
value_hash_.store(node.key, -node.q, node.visits);
}
iterations_++;
+7 -46
View File
@@ -175,42 +175,6 @@ private:
std::array<std::array<std::array<i16, 64>, 64>, 2> table_{};
};
// Q of positions searched before, from the view of the side to move there
// (as in Monty and Vine). Every node of a playout's path stores its Q here;
// a leaf whose position is found takes that value instead of a fresh
// evaluation. Transpositions are common: 37-85% of leaf evaluations in a
// 5-second search repeat a position already evaluated in the same search.
class ValueHash {
public:
void resize(size_t entries) {
table_.assign(std::max<size_t>(entries, 1), Entry{});
table_.shrink_to_fit();
}
void clear() { std::fill(table_.begin(), table_.end(), Entry{}); }
bool probe(u64 key, float& q) const {
const Entry& e = table_[key % table_.size()];
if (!e.visits || e.check != u32(key >> 32)) return false;
q = e.q;
return true;
}
// A different position always replaces the entry; the same one only with
// at least as many visits.
void store(u64 key, float q, u32 visits) {
Entry& e = table_[key % table_.size()];
if (e.check == u32(key >> 32) && visits < e.visits) return;
e = Entry{u32(key >> 32), visits, q};
}
static constexpr size_t ENTRY_BYTES = 12;
private:
struct Entry {
u32 check = 0;
u32 visits = 0;
float q = 0.0f;
};
std::vector<Entry> table_;
};
struct SearchLimits {
u64 nodes = 0;
i64 movetime = 0;
@@ -250,11 +214,11 @@ public:
Searcher();
void set_hash(size_t mb) {
// A 32nd of the memory goes to the value hash, the rest to the graph
// (its arenas, split by Tree::EDGES_PER_NODE, and its table).
const size_t bytes = mb * 1024 * 1024;
value_hash_.resize(bytes / 32 / ValueHash::ENTRY_BYTES);
tree_.resize_bytes(bytes - bytes / 32);
// All of it goes to the graph (its arenas, split by
// Tree::EDGES_PER_NODE, and its table). The graph shares a position's
// value between move orders, which a separate value hash did in the
// tree (plan variant 5c removed it).
tree_.resize_bytes(mb * 1024 * 1024);
tree_valid_ = false;
}
@@ -262,7 +226,6 @@ public:
void new_game() {
tree_valid_ = false;
butterfly_.clear();
value_hash_.clear();
}
// Synchronous search. `history` holds the keys of every position that led to
@@ -283,7 +246,6 @@ private:
// Kept between moves of a game, like the tree.
ButterflyHistory butterfly_;
Position root_pos_;
ValueHash value_hash_;
std::vector<u64> history_;
size_t root_history_size_ = 0;
// The nodes of a playout, from the root, and the edges it took
@@ -331,9 +293,8 @@ private:
// Second visit: creates the edges with their priors. False when the
// arena is full (a collection is then scheduled).
bool expand(u32 idx, const Position& pos);
// The edge PUCT picks at node `idx`, whose position is `pos` (the last
// entry of history_).
u32 select_edge(u32 idx, const Position& pos) const;
// The edge PUCT picks at node `idx`.
u32 select_edge(u32 idx) const;
// A node's Q from its own value and its children's (see Edge).
void recompute_q(Node& node) const;
bool is_repetition(const Position& pos) const;