Merge pull request 'Collect the tree when the node arena is full' (#4) from tree-gc-nn into main

SPRT https://bench.rektdie.com/test/5286/
Elo   | 8.74 +- 6.47 (95%)
SPRT  | 8.0+0.08s Threads=1 Hash=64MB
LLR   | 3.00 (-2.94, 2.94) [0.00, 10.00]
Games | N: 3262 W: 996 L: 914 D: 1352
Penta | [48, 226, 1023, 264, 70]

Bench: 546018
This commit was merged in pull request #4.
This commit is contained in:
claude committed 2026-11-15 11:56:36 +01:00
commit 1754753a6e
3 files changed
+76 -11

No files matched your search

+5 -2
View File
@@ -19,6 +19,9 @@ Every change is validated with SPRT on OpenBench.
promotions, checks, pawn threats) until a policy network exists.
- Tree reuse between moves: the subtree of the new position is packed to the
front of the node arena in place, so it costs no extra memory.
- When the arena is full, the least visited parts of the tree are cut back to
leaves (keeping their visits and Q) and the rest is packed, so search never
stops expanding.
- Single-threaded.
## Building
@@ -35,8 +38,8 @@ make EVALFILE=path/to/quantised.bin
`EVALFILE` defaults to `networks/rigby-value-v1.bin`.
Compiling with `-DRIGBY_VERIFY_TREE` checks the whole tree before and after every
reuse (legal moves, visit counts) and aborts on the first inconsistency. It is slow,
for debugging only.
reuse and after every collection (legal moves, visit counts) and aborts on the first
inconsistency. It is slow, for debugging only.
## UCI
+59 -8
View File
@@ -36,14 +36,55 @@ u32 Tree::alloc(u32 count) {
size_t Tree::reroot(u32 new_root) {
if (new_root == root()) return used_;
return compact(new_root, 0);
}
// Every child block of the subtree, as (old first index, size).
std::vector<std::pair<u32, u32>> blocks;
std::vector<u32> stack{new_root};
size_t Tree::collect_garbage(double keep_fraction) {
const size_t target = size_t(double(nodes_.size()) * keep_fraction);
return compact(root(), prune_threshold(target));
}
u32 Tree::prune_threshold(size_t target) const {
// A node's children survive a collection with threshold T when the node has
// at least T visits. Visits only shrink going down the tree, so every
// ancestor of such a node survives too, and the size kept for a threshold is
// just the children of all nodes with at least that many visits.
constexpr u32 CAP = 1 << 16;
std::vector<u64> children_by_visits(CAP + 1, 0);
std::vector<u32> stack{root()};
while (!stack.empty()) {
const Node& node = nodes_[stack.back()];
stack.pop_back();
if (!node.first_child) continue;
children_by_visits[std::min(node.visits, CAP)] += node.num_children;
for (u32 i = 0; i < node.num_children; i++) stack.push_back(node.first_child + i);
}
u64 kept = 2; // null slot and root
for (u32 v = CAP; v >= 1; v--) {
if (kept + children_by_visits[v] > target) return v + 1;
kept += children_by_visits[v];
}
return 1;
}
size_t Tree::compact(u32 new_root, u32 min_visits) {
// Every child block that stays, as (old first index, size). Nodes below the
// visit threshold lose their children and become leaves again; they keep
// their visits and Q, and get expanded anew if search comes back to them.
std::vector<std::pair<u32, u32>> blocks;
std::vector<u32> stack{new_root};
while (!stack.empty()) {
const u32 idx = stack.back();
stack.pop_back();
Node& node = nodes_[idx];
if (!node.first_child) continue;
if (idx != new_root && node.visits < min_visits) {
node.first_child = 0;
node.num_children = 0;
node.state = UNEXPANDED;
continue;
}
blocks.emplace_back(node.first_child, node.num_children);
for (u32 i = 0; i < node.num_children; i++) stack.push_back(node.first_child + i);
}
@@ -242,8 +283,9 @@ bool Searcher::expand(u32 idx, const Position& pos) {
const u32 first = tree_.alloc(u32(moves.size()));
if (!first) {
// Out of memory: the node stays a leaf and is evaluated again.
tree_full_ = true;
// Out of memory: this time the node stays a leaf; the tree is collected
// right after this playout, so the next visit can expand it.
gc_pending_ = true;
return false;
}
@@ -415,7 +457,8 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
iterations_ = 0;
depth_sum_ = 0;
seldepth_ = 0;
tree_full_ = false;
gc_pending_ = false;
gc_runs_ = 0;
compute_time_limit(limits);
SearchResult result;
@@ -432,13 +475,21 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
// single playout leaves a move to play.
if (tree_[Tree::root()].state == UNEXPANDED && !expand(Tree::root(), root)) {
tree_.clear();
tree_full_ = false;
gc_pending_ = false;
expand(Tree::root(), root);
}
i64 last_info = 0;
do {
playout();
if (gc_pending_) {
tree_.collect_garbage(GC_KEEP_FRACTION);
gc_pending_ = false;
gc_runs_++;
#ifdef RIGBY_VERIFY_TREE
verify_tree();
#endif
}
if (print && (iterations_ & 1023) == 0) {
const i64 ms = elapsed_ms();
if (ms - last_info >= 1000) {
@@ -450,7 +501,7 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
if (print) {
print_info();
if (tree_full_) uci_io::send("info string tree memory full, raise Hash");
if (gc_runs_) uci_io::send("info string tree collected " + std::to_string(gc_runs_) + " times");
}
const u32 best = best_child(Tree::root());
+12 -1
View File
@@ -38,6 +38,9 @@ public:
// Makes `new_root` the root and packs its subtree to the front of the arena;
// everything else is dropped. Returns the number of nodes kept.
size_t reroot(u32 new_root);
// Frees memory when the arena is full: the least visited parts of the tree
// are cut back to leaves until at most `keep_fraction` of it is in use.
size_t collect_garbage(double keep_fraction);
Node& operator[](u32 idx) { return nodes_[idx]; }
const Node& operator[](u32 idx) const { return nodes_[idx]; }
@@ -49,6 +52,11 @@ public:
private:
std::vector<Node> nodes_;
size_t used_ = 0;
// Keeps the subtree of `new_root`, dropping the children of every node with
// fewer than `min_visits` visits, and packs it to the front in place.
size_t compact(u32 new_root, u32 min_visits);
u32 prune_threshold(size_t target) const;
};
struct SearchLimits {
@@ -114,7 +122,10 @@ private:
u64 iterations_ = 0;
u64 depth_sum_ = 0;
int seldepth_ = 0;
bool tree_full_ = false;
bool gc_pending_ = false;
int gc_runs_ = 0;
// Share of the arena kept after a collection.
static constexpr double GC_KEEP_FRACTION = 0.5;
void playout();
// First visit of a node: settles terminal states, otherwise evaluates.