feat: collect the tree when the node arena is full
Tree reuse keeps most of the previous tree, so the arena fills up fast. In a 240-move self-play diagnostic of main at 8+0.08 with Hash=64, the reused subtree alone was often 3.0-3.3M of the 3.35M nodes, and the tree was full on 90 moves (37%). A full tree cannot expand anything any more; for the rest of the move it only re-evaluates leaves. Now a failed allocation triggers a collection after the playout. The children of every node below a visit threshold are dropped (the node keeps its visits and Q and becomes a leaf again, expanded anew on its next visit), and the rest is packed in place with the same block compaction as tree reuse. The threshold is the smallest one that brings the tree down to half the arena; visits only shrink going down, so one pass over the tree gives the size kept for every threshold. In the same diagnostic with this change the tree was collected on 27 of 158 moves, once each, and search kept expanding afterwards; a collection of a full 64 MB tree costs about 20-30 ms. Checked: the -DRIGBY_VERIFY_TREE build verifies the tree after every collection; UCI-driven self-play passed 700 checks / 428 collections with 1 MB Hash and 476 checks / 205 collections with 16 MB, including repeated searches of the same position and a root at halfmove 99. uci, go wtime/nodes/infinite, stop and quit work. The bench is unchanged because the bench never fills the tree. Rebased from the earlier tree-gc branch onto main with the value network and second-visit expansion. Bench: 546018 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0d5713f7f0
commit
967e0e0b90
3 files changed
+76
-11
No files matched your search
@@ -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
@@ -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
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user