diff --git a/README.md b/README.md index c90c36f..099a509 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/mcts.cpp b/src/mcts.cpp index bb3c089..36c5791 100644 --- a/src/mcts.cpp +++ b/src/mcts.cpp @@ -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> blocks; - std::vector 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 children_by_visits(CAP + 1, 0); + std::vector 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> blocks; + std::vector 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& 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& 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& 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()); diff --git a/src/mcts.h b/src/mcts.h index abe53a5..e5f98c4 100644 --- a/src/mcts.h +++ b/src/mcts.h @@ -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 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.