diff --git a/README.md b/README.md index 7849563..b88830d 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,9 @@ Every change is validated with SPRT on OpenBench. heuristics (captures, promotions, checks, pawn threats) for the policy. - 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 diff --git a/src/mcts.cpp b/src/mcts.cpp index 5fa3f50..ee0d025 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); } @@ -230,8 +271,9 @@ float Searcher::expand(u32 idx, const Position& pos, int depth) { const u32 first = tree_.alloc(u32(moves.size())); if (!first) { - // Out of memory: keep the node as a leaf and just evaluate it 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 eval::value(pos); } @@ -396,7 +438,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; @@ -413,6 +456,14 @@ SearchResult Searcher::search(const Position& root, const std::vector& hist // Always run at least one playout so the root gets expanded. 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) { @@ -424,7 +475,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 ed8c4fe..4898b85 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(); float expand(u32 idx, const Position& pos, int depth);