Author SHA1 Message Date
rektdieandclaude e7f2bfbf18 mcts: edges cache their child's Q (MCGS variant 5b)
Selection and the recompute of a node's Q read every visited child node,
scattered over the node arena. Step 3 runs at 0.64x main's playouts/s at
5 s; selection and backprop were about 40 % of playout time in step 1.

Each edge now keeps the child's Q as of the last playout through it (a
20-byte edge). Selection and the recompute read only the parent's
contiguous edges. A cached Q goes stale when the child is updated through
another parent, until a playout passes the edge again. Root decisions
(best move, score, PV, dashboard) still read the children's current Q.
The recompute stays a loop over the edges (no running sum), so there is no
drift to correct.

The bench changes because selection sees the cached values.

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. The VERIFY build also checks every cached Q is finite, in [-1, 1],
and 0 on an edge without a child; check (b) compares the cached Q too.

Bench: 769193

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 21:36:01 +01:00
rektdieandclaude 95e49098ec docs: 5312 result and step 3b (reroot, test 5313) in mcgs-log
Bench: 764208

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 21:20:26 +01:00
rektdieandclaude d38f0b133a mcts: reroot moves only the root index (no compaction between moves)
Step 3 rerooted by compacting what the new root reaches (plan 3.6.6,
mark/compact with T = 0). In a graph, a reversible endgame keeps almost the
whole graph reachable from the new root, so every move paid O(graph) before
its first playout. Replaying a 2+0.02 game that step 3 lost on time (Hash 16,
rook endgame, half-move clock about 70): the reroot averaged 3.3 ms per move,
max 14.5 ms, against 0.3 ms for main. Under load that is more than the
increment, and step 3 ran out of time in 2 of 100 games.

Now reroot only sets the root. Nodes the new root no longer reaches keep
their memory and their table entries (search may link to them again) until
the next collection, which keeps only what the root reaches. In the same
replay, the time before the first playout is 0 ms on average.

VERIFY: verify_tree requires every node to be reachable only right after a
collection; the table must always hold every node of the arena. Check (b)
now checks that a reroot changes nothing and that the next collection keeps
exactly the nodes the new root reaches, unchanged.

Checked: zero warnings, bench unchanged (bench never reuses a graph),
`verify` (a)-(d), the VERIFY searches of plan section 6 including 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: 764208

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 21:19:17 +01:00
rektdieandclaude f4b79a472c docs: step 3 SPRT is test 5312 (STC vs main) in mcgs-log
Bench: 764208

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 20:59:36 +01:00
3 changed files with 142 additions and 39 deletions

No files matched your search

+71 -8
View File
@@ -7,7 +7,8 @@ only from results the owner provides.
| --- | --- | --- | --- | --- | --- |
| 1 | `mcgs-stats` | `bec9871` | 663779 (normal and `STATS=1` builds) | none (never merged) | measurement only; the branch stays on its own |
| 2 | `mcgs-step2-split` | `fb1b8a5` | 663779 (identical to main) | | |
| 3 | `mcgs-step3-core` | `14f672a` | 764208 | [5310](https://bench.rektdie.com/test/5310/) STC 8.0+0.08 `[0.00, 5.00]`; [5311](https://bench.rektdie.com/test/5311/) LTC 40.0+0.40 `[0.00, 5.00]`; base `mcgs-step2-split` | |
| 3 | `mcgs-step3-core` | `14f672a` | 764208 | [5312](https://bench.rektdie.com/test/5312/) STC 8.0+0.08 `[0.00, 5.00]`, base `main` | failed: LLR −3.19, 4758 games, 106 lost on time |
| 3b | `mcgs-step3-reroot` | `d38f0b1` | 764208 | [5313](https://bench.rektdie.com/test/5313/) STC 8.0+0.08 `[0.00, 5.00]`, base `main` | |
## Step 1 measurements
@@ -617,11 +618,73 @@ the counter build.
The plan recommends these tests, both with Threads=1 and
`UHO_Lichess_4852_v1.epd`:
- STC 8.0+0.08, `[0.00, 5.00]`, Hash=64:
https://bench.rektdie.com/test/5310/, outcome to be filled in.
- LTC 40.0+0.40, `[0.00, 5.00]`, Hash=256:
https://bench.rektdie.com/test/5311/, outcome to be filled in.
- STC 8.0+0.08, `[0.00, 5.00]`, Threads=1, Hash=64, base `main` (bench
663779): https://bench.rektdie.com/test/5312/. Result as shown on the test
page:
Both tests use base `mcgs-step2-split` (bench 663779), so they measure step 3
alone. The owner started them knowing about the failed smoke gate; the reroot
is unchanged.
```
Elo | -8.91 +- 6.44 (95%)
SPRT | 8.0+0.08s Threads=1 Hash=64MB
LLR | -3.19 (-2.94, 2.94) [0.00, 5.00]
Games | N: 4758 W: 1504 L: 1626 D: 1628
```
The workers report 106 games lost on time (18 + 88). The earlier Rigby STC
test 5308 lost none in 1618 games.
The owner chose a single STC test against `main`, so it measures steps 2 and
3 together. Tests 5310 (STC) and 5311 (LTC) against `mcgs-step2-split` were
deleted soon after they started. The owner started the test knowing about the
failed smoke gate; the reroot is unchanged.
## Step 3b: reroot moves only the root (`mcgs-step3-reroot`)
Code at `d38f0b1`, on top of `mcgs-step3-core` (`f4b79a4`). This deviates from
plan 3.6.6, on the owner's decision after the time forfeits above.
**Bench: 764208**, unchanged, because bench never reuses a graph.
What changed:
- `Tree::reroot` only sets the root index. It no longer compacts.
- Nodes the new root no longer reaches keep their memory and their table
entries until the next collection. Search may link to them again in the
meantime. The collection keeps only what the root reaches.
- `info string reused N` now counts every node in the graph, reachable or
not.
- **VERIFY:**
- `verify_tree` requires full reachability only right after a collection.
The table must always hold every node in the arena.
- Check (b) now verifies two things: a reroot changes nothing, and the next
collection keeps exactly the nodes the new root reaches, unchanged.
Gates (owner's machine, 2026-11-16):
- Zero warnings. Bench 764208.
- `verify` (a)–(d) passed. The reroot check reported, for example, 10,803
nodes in the pawn endgame, with 10,802 reachable from the new root and
10,802 kept by the collection.
- The VERIFY searches of section 6 all passed, including forced GC: Hash 1
gave 190 and 145 collections, Hash 2 gave 104 and 77.
- The reuse sequence passed at Hash 64, at Hash 1 and from the 960 start.
- The `go nodes 1/2` and `go depth 1` smoke positions passed.
- Perft was not re-run, because move generation is untouched.
- No local games: the owner asked for the SPRT directly.
Replay of the 2+0.02 game that step 3 lost on time (game 31, Hash 16), measuring the time
before the first playout (`go nodes 1` after each move, 90 moves). This is a
local measurement:
| | mean | max |
| --- | ---: | ---: |
| main | 0.2 ms | 1 ms |
| step 3 | 3.5 ms | 13 ms |
| step 3b | 0 ms | 0 ms |
At `go movetime 40` in the same replay, mean playouts per move were: main
12,083, step 3 5,047, step 3b 5,899.
### SPRT (owner)
- STC 8.0+0.08, `[0.00, 5.00]`, Threads=1, Hash=64, base `main` (bench
663779): https://bench.rektdie.com/test/5313/, outcome to be filled in.
+49 -23
View File
@@ -106,8 +106,13 @@ u32 Tree::alloc_edges(u32 count) {
}
size_t Tree::reroot(u32 new_root) {
if (new_root == root_) return nodes_used_ - 1;
return compact(new_root, 0);
// Only the root moves. Packing what stays reachable would cost O(graph) on
// every move, because in a reversible endgame almost the whole graph stays
// reachable from the new root. Nodes that are no longer reachable keep
// their memory and their table entries (search may link to them again)
// until the next collection, which keeps only what the root reaches.
root_ = new_root;
return nodes_used_ - 1;
}
size_t Tree::collect_garbage(double keep_fraction) {
@@ -406,9 +411,12 @@ size_t Searcher::reuse_tree(const Position& root) {
// history `history` (ending with the root's key). Each node is checked once,
// with the position of the first path that reaches it (positions do not
// depend on the path, apart from the clocks):
// - every node is reachable, and the arenas hold nothing else;
// - right after a collection every node is reachable, and the arenas hold
// nothing else; at other times a reroot may have left nodes the root no
// longer reaches (they wait for the next collection);
// - a node's key and check match its position, and the table finds it there;
// the table has one entry per node and nothing else;
// the table has one entry per node in the arena, reachable or not, and
// nothing else;
// - its state is what the position itself decides: checkmate, stalemate or
// insufficient material (not at the root, nor at an earlier root, i.e. a
// position of the game history). Repetitions and the 50-move rule never
@@ -418,7 +426,7 @@ size_t Searcher::reuse_tree(const Position& root) {
// a visit that was not a draw;
// - N = base + the edges' visits; a node without edges has q = u;
// - Q and u are finite and in [-1, 1].
void Searcher::verify_tree(const std::vector<u64>& history) const {
void Searcher::verify_tree(const std::vector<u64>& history, bool collected) const {
const auto fail = [&](u32 idx, const Position& pos, const std::string& what) {
const Node& n = tree_.node(idx);
std::cerr << "GRAPH CORRUPT at node " << idx << " (" << pos.fen() << "): " << what << " visits=" << n.visits
@@ -478,6 +486,8 @@ void Searcher::verify_tree(const std::vector<u64>& history) const {
if (it == legal.end() || legal_seen[it - legal.begin()]) fail(idx, pos, "illegal or repeated edge move");
legal_seen[it - legal.begin()] = true;
if (e.draws > e.visits) fail(idx, pos, "edge with more draws than visits");
if (!std::isfinite(e.q) || std::abs(e.q) > 1.0f) fail(idx, pos, "edge q out of range");
if (!e.child && e.q != 0.0f) fail(idx, pos, "edge q without a child");
if (e.child >= tree_.nodes_used()) fail(idx, pos, "edge child out of range");
if (e.visits > e.draws && !e.child) fail(idx, pos, "edge with non-draw visits and no child");
edge_visits += e.visits;
@@ -493,10 +503,11 @@ void Searcher::verify_tree(const std::vector<u64>& history) const {
fail(idx, pos, "visits != base + edge visits " + std::to_string(edge_visits));
}
if (nodes != tree_.nodes_used() - 1 || edges != tree_.edges_used() - 1)
const u64 all_nodes = tree_.nodes_used() - 1, all_edges = tree_.edges_used() - 1;
if (collected ? nodes != all_nodes || edges != all_edges : nodes > all_nodes || edges > all_edges)
fail(root, root_pos_,
"unreachable nodes or edges: reached " + std::to_string(nodes) + " nodes, " + std::to_string(edges) +
" edges");
" edges of " + std::to_string(all_nodes) + " and " + std::to_string(all_edges));
// The table: one entry per node, each under its node's key.
size_t entries = 0;
@@ -507,12 +518,13 @@ void Searcher::verify_tree(const std::vector<u64>& history) const {
if (n >= tree_.nodes_used() || tree_.table_key_hi(s) != u32(tree_.node(n).key >> 32))
fail(root, root_pos_, "table slot " + std::to_string(s) + " points to a wrong node");
}
if (entries != nodes || tree_.table_entries() != nodes)
if (entries != all_nodes || tree_.table_entries() != all_nodes)
fail(root, root_pos_,
"table has " + std::to_string(entries) + " entries for " + std::to_string(nodes) + " nodes");
"table has " + std::to_string(entries) + " entries for " + std::to_string(all_nodes) + " nodes");
uci_io::send("info string tree verified: " + std::to_string(nodes) + " nodes, " + std::to_string(edges) +
" edges, " + std::to_string(entries) + " table entries");
" edges reachable, " + std::to_string(all_nodes) + " nodes in the arena, " +
std::to_string(entries) + " table entries");
}
#endif
@@ -610,7 +622,7 @@ u32 Searcher::select_edge(u32 idx) 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.
const float q = e.visits ? visited_edge_q(e) : fpu;
const float q = e.visits ? cached_edge_q(e) : fpu;
const float score = q + explore * e.prior() / float(1 + e.visits);
if (score > best_score) {
best_score = score;
@@ -628,7 +640,7 @@ void Searcher::recompute_q(Node& node) const {
for (u32 i = 0; i < node.num_edges; i++) {
const Edge& e = tree_.edge(node.first_edge + i);
const u32 w = e.visits - e.draws;
if (w) sum -= double(w) * double(tree_.node(e.child).q);
if (w) sum -= double(w) * double(e.q);
}
node.q = float(sum / double(node.visits));
}
@@ -734,6 +746,8 @@ void Searcher::playout() {
Edge& e = tree_.edge(edge_path_[j]);
e.visits++;
if (leaf == PATH_DRAW && j + 1 == edge_path_.size()) e.draws++;
// The child is final for this playout (updated in the step before).
if (e.child) e.q = tree_.node(e.child).q;
butterfly_.update(root_pos_.stm ^ int(j & 1), Move::from_raw(e.move), bonus);
Node& node = tree_.node(path_[j]);
@@ -910,7 +924,8 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
gc_pending_ = false;
gc_runs_++;
#ifdef RIGBY_VERIFY_TREE
verify_tree(std::vector<u64>(history_.begin(), history_.begin() + std::ptrdiff_t(root_history_size_)));
verify_tree(std::vector<u64>(history_.begin(), history_.begin() + std::ptrdiff_t(root_history_size_)),
true);
#endif
}
if (print && (iterations_ & info_mask) == 0) {
@@ -961,8 +976,8 @@ struct NodeSnapshot {
u32 base;
u8 state;
u16 check;
// (move, policy, visits, draws, child's key or 0) per edge
std::vector<std::tuple<u16, u16, u32, u32, u64>> edges;
// (move, policy, visits, draws, child's key or 0, cached q) per edge
std::vector<std::tuple<u16, u16, u32, u32, u64, float>> edges;
};
} // namespace
@@ -1021,7 +1036,7 @@ void Searcher::self_test() {
NodeSnapshot s{n.key, n.visits, n.q, n.u, n.base, n.state, n.check, {}};
for (u32 i = 0; i < n.num_edges; i++) {
const Edge& e = tree_.edge(n.first_edge + i);
s.edges.emplace_back(e.move, e.policy, e.visits, e.draws, e.child ? tree_.node(e.child).key : 0);
s.edges.emplace_back(e.move, e.policy, e.visits, e.draws, e.child ? tree_.node(e.child).key : 0, e.q);
if (e.child && !seen[e.child]) {
seen[e.child] = 1;
stack.push_back(e.child);
@@ -1066,7 +1081,7 @@ void Searcher::self_test() {
if (n.num_edges && idx != tree_.root()) kept_min = std::min(kept_min, n.visits);
for (u32 i = 0; i < n.num_edges; i++) {
const Edge& e = tree_.edge(n.first_edge + i);
if (std::make_tuple(e.move, e.policy, e.visits, e.draws, e.child ? tree_.node(e.child).key : 0) !=
if (std::make_tuple(e.move, e.policy, e.visits, e.draws, e.child ? tree_.node(e.child).key : 0, e.q) !=
s.edges[i])
self_test_failed("edge changed");
if (e.child && !seen[e.child]) {
@@ -1104,7 +1119,7 @@ void Searcher::self_test() {
const size_t count = compare(before, true, pruned_max, kept_min);
if (count != tree_.nodes_used() - 1) self_test_failed("node count differs after collection");
if (pruned_max >= kept_min) self_test_failed("pruned a node with more visits than one kept");
verify_tree({root.key});
verify_tree({root.key}, true);
uci_io::send("info string verify (b) gc " + std::string(fen) + ": " + std::to_string(before.size()) +
" -> " + std::to_string(count) + " nodes ok");
uci_io::send("info string verify (d) table after gc: " + std::to_string(tree_.table_entries()) +
@@ -1127,16 +1142,27 @@ void Searcher::self_test() {
}
if (idx == tree_.root() || tree_.node(idx).state != EXPANDED) self_test_failed("no node to reroot to");
const auto before = snapshot(idx);
const size_t nodes_before = tree_.nodes_used(), edges_before = tree_.edges_used();
tree_.reroot(idx);
// A reroot only moves the root: everything stays where it was.
u32 pruned_max, kept_min;
const size_t count = compare(before, false, pruned_max, kept_min);
if (count != before.size() || count != tree_.nodes_used() - 1) self_test_failed("reroot lost nodes");
if (count != before.size() || tree_.nodes_used() != nodes_before || tree_.edges_used() != edges_before)
self_test_failed("reroot changed the graph");
root_pos_ = pos;
verify_tree(history);
uci_io::send("info string verify (b) reroot " + std::string(fen) + ": " + std::to_string(count) +
" nodes ok");
uci_io::send("info string verify (d) table after reroot: " + std::to_string(tree_.table_entries()) +
" entries for " + std::to_string(count) + " nodes ok");
// The next collection (with room for everything) keeps exactly what the
// new root reaches, unchanged.
tree_.collect_garbage(tree_.node_capacity(), tree_.edge_capacity());
const size_t kept = compare(before, false, pruned_max, kept_min);
if (kept != before.size() || kept != tree_.nodes_used() - 1)
self_test_failed("collection after a reroot kept the wrong nodes");
verify_tree(history, true);
uci_io::send("info string verify (b) reroot " + std::string(fen) + ": " + std::to_string(nodes_before - 1) +
" nodes, " + std::to_string(count) + " reachable from the new root, collected to " +
std::to_string(kept) + " ok");
uci_io::send("info string verify (d) table after reroot and collection: " +
std::to_string(tree_.table_entries()) + " entries for " + std::to_string(kept) + " nodes ok");
}
// (c) Draws of the path never leak into nodes: search positions where
+22 -8
View File
@@ -41,12 +41,17 @@ struct Edge {
u32 child = 0; // node index, 0 = not linked yet
u32 visits = 0; // N(n,a): how often search chose this move here
u32 draws = 0; // D(n,a): of those, playouts that ended in a draw of the path after this move
// The child's Q as of the last playout through this edge (plan variant 5b).
// Selection and the parent's recompute read it instead of the child, so
// they only touch the parent's contiguous edges. It goes stale when the
// child is updated through another parent, until a playout passes here.
float q = 0.0f;
u16 move = 0;
u16 policy = 0; // prior scaled to 0..65535
float prior() const { return float(policy) * (1.0f / 65535.0f); }
};
static_assert(sizeof(Edge) == 16);
static_assert(sizeof(Edge) == 20);
// A position, created (and evaluated) when search first reaches it.
struct Node {
@@ -71,8 +76,9 @@ u16 position_check(const Position& pos);
// check, about 80 bits.
class Tree {
public:
// Edges per node the arenas are sized for: the edge arena gets 128 of
// every 160 bytes, the node arena 32. Measured in step 1 of the MCGS plan
// Edges per node the arenas are sized for: with 20-byte edges the edge
// arena gets 160 of every 192 bytes, the node arena 32 (plus the table).
// Measured in step 1 of the MCGS plan
// (docs/mcgs-log.md, edges per visited node): 13 on average in 5 s
// searches without collections, 19-21 in middlegames, 3.6-5.5 in simple
// endgames, and 3-9 right after collections (pruned nodes keep their node
@@ -94,9 +100,9 @@ public:
u32 find(u64 key, u16 check) const;
// Enters a node, which must not be in the table yet, by its key.
void insert(u32 idx);
// Makes `new_root` the root and packs what is reachable from it to the
// front of the arenas; everything else is dropped. Returns the number of
// nodes kept.
// Makes `new_root` the root. Nothing is moved or dropped: what the new
// root no longer reaches goes at the next collection. Returns the number
// of nodes in the graph.
size_t reroot(u32 new_root);
// Frees memory when an arena is full: the least visited parts of the graph
// are cut back to leaves until at most `keep_fraction` of each arena is in
@@ -337,7 +343,8 @@ private:
void recompute_q(Node& node) const;
bool is_repetition(const Position& pos) const;
// Finds the node for `root` in the previous graph and makes it the root.
// Returns the number of nodes kept, 0 when nothing could be reused.
// Returns the number of nodes in the graph (some may no longer be
// reachable), 0 when nothing could be reused.
size_t reuse_tree(const Position& root);
// True when the root node's edges are exactly the legal moves of `root`.
bool root_edges_valid(const Position& root) const;
@@ -345,7 +352,8 @@ private:
void clear_tree(const Position& root);
#ifdef RIGBY_VERIFY_TREE
// Checks the graph from root_pos_; `history` ends with the root's key.
void verify_tree(const std::vector<u64>& history) const;
// `collected`: right after a collection, when every node must be reachable.
void verify_tree(const std::vector<u64>& history, bool collected = false) const;
#endif
void compute_time_limit(const SearchLimits& limits);
@@ -364,6 +372,12 @@ private:
}
// The same, 0 when the edge is unvisited.
float edge_q(const Edge& e) const { return e.visits ? visited_edge_q(e) : 0.0f; }
// The same from the Q the edge last saw (selection and recompute).
static float cached_edge_q(const Edge& e) {
if (!e.draws) return e.q;
const u32 w = e.visits - e.draws;
return w ? float(w) / float(e.visits) * e.q : 0.0f;
}
void print_info() const;
std::vector<Move> principal_variation() const;
void draw_dashboard(bool done);