mcts: split Node into Node and Edge arenas (MCGS step 2)
The tree now has two arenas. An Edge (16 bytes: child, visits, draws, move, policy) is written for every legal move at expansion; a Node (32 bytes: key, first_edge, visits, q, u, base, num_edges, state, check) is created only when its edge is first selected, and evaluated on that visit. Search is unchanged: edge visits are kept equal to the child's visits, and selection, FPU and the root decisions read edge.visits and edge.policy with the child's q, in the same float order. Path-dependent draws stay in node.state. key and check are set at node creation, u and base by pruning; nothing reads them yet, and edge.draws stays 0. The root index is a member. Garbage collection and rerooting use mark -> forwarding -> slide -> remap over both arenas, which no longer assumes that a block is allocated after its parent's. The threshold rule is the tree-exact one, with a histogram per arena; a pruned node keeps its visits and q. A playout that finds the node arena full ends at the parent, like a failed expansion, and schedules a collection. VERIFY builds check the whole tree after every reroot, collection and search: in-degree 1, nothing unreachable in either arena, key and check, the node state recomputed from the position and its path, one edge per legal move, edge visits equal to the child's, and |q|, |u| <= 1. info string reused now counts nodes (positions), not child slots. Bench: 663779 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
18911b0388
commit
6629525938
2 files changed
+411
-192
No files matched your search
+85
-29
@@ -19,48 +19,93 @@ enum NodeState : u8 {
|
||||
TERMINAL_DRAW,
|
||||
};
|
||||
|
||||
// 20 bytes. Children of a node sit next to each other in the arena, so a node
|
||||
// only needs the index of its first child.
|
||||
struct Node {
|
||||
u32 first_child = 0;
|
||||
u32 visits = 0;
|
||||
float q = 0.0f; // running mean, from the view of the side that played `move`
|
||||
// A move out of a node. A node's edges sit next to each other in the edge
|
||||
// arena, so the node only needs the index of the first one. The child node is
|
||||
// created the first time search selects the edge.
|
||||
struct Edge {
|
||||
u32 child = 0; // node index, 0 = no node yet
|
||||
u32 visits = 0; // how often search chose this move here (equals the child's visits in a tree)
|
||||
u32 draws = 0; // of those, playouts that ended in a draw of the path (unused while the search is a tree)
|
||||
u16 move = 0;
|
||||
u16 policy = 0; // prior scaled to 0..65535
|
||||
u8 num_children = 0;
|
||||
u8 state = UNEXPANDED;
|
||||
u16 policy = 0; // prior scaled to 0..65535
|
||||
|
||||
float prior() const { return float(policy) * (1.0f / 65535.0f); }
|
||||
};
|
||||
static_assert(sizeof(Edge) == 16);
|
||||
|
||||
// A position, created when search first reaches it.
|
||||
struct Node {
|
||||
u64 key = 0; // Zobrist key of the position
|
||||
u32 first_edge = 0; // 0 = not expanded
|
||||
u32 visits = 0;
|
||||
float q = 0.0f; // running mean, from the view of the side that moved into the node
|
||||
// `u` and `base` are set by garbage collection (the Q and visits a pruned
|
||||
// node keeps) but not read yet. `check` is a second fingerprint of the
|
||||
// position (see position_check). All three are for graph search.
|
||||
float u = 0.0f;
|
||||
u32 base = 0;
|
||||
u8 num_edges = 0;
|
||||
u8 state = UNEXPANDED;
|
||||
u16 check = 0;
|
||||
};
|
||||
static_assert(sizeof(Node) == 32);
|
||||
|
||||
// A 16-bit fingerprint of the position that does not depend on the Zobrist keys.
|
||||
u16 position_check(const Position& pos);
|
||||
|
||||
// Two arenas: nodes and edges. Index 0 of each is null.
|
||||
class Tree {
|
||||
public:
|
||||
// Edges per node the arenas are sized for. 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
|
||||
// but lose their edges). See set_hash for the choice.
|
||||
static constexpr size_t EDGES_PER_NODE = 8;
|
||||
|
||||
void resize_bytes(size_t bytes);
|
||||
void clear();
|
||||
// Returns the index of `count` fresh nodes, or 0 when the arena is full.
|
||||
u32 alloc(u32 count);
|
||||
// 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.
|
||||
// A fresh node, or 0 when the node arena is full.
|
||||
u32 alloc_node();
|
||||
// The index of `count` fresh edges, or 0 when the edge arena is full.
|
||||
u32 alloc_edges(u32 count);
|
||||
// Makes `new_root` the root and packs its subtree to the front of the
|
||||
// arenas; 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.
|
||||
// Frees memory when an arena is full: the least visited parts of the tree
|
||||
// are cut back to leaves until at most `keep_fraction` of each arena is in
|
||||
// use. Returns the number of nodes kept.
|
||||
size_t collect_garbage(double keep_fraction);
|
||||
// The same with explicit targets (in slots, null slots included).
|
||||
size_t collect_garbage(size_t node_target, size_t edge_target);
|
||||
|
||||
Node& operator[](u32 idx) { return nodes_[idx]; }
|
||||
const Node& operator[](u32 idx) const { return nodes_[idx]; }
|
||||
static constexpr u32 root() { return 1; }
|
||||
Node& node(u32 idx) { return nodes_[idx]; }
|
||||
const Node& node(u32 idx) const { return nodes_[idx]; }
|
||||
Edge& edge(u32 idx) { return edges_[idx]; }
|
||||
const Edge& edge(u32 idx) const { return edges_[idx]; }
|
||||
u32 root() const { return root_; }
|
||||
|
||||
size_t used() const { return used_; }
|
||||
size_t capacity() const { return nodes_.size(); }
|
||||
size_t nodes_used() const { return nodes_used_; }
|
||||
size_t edges_used() const { return edges_used_; }
|
||||
size_t node_capacity() const { return nodes_.size(); }
|
||||
size_t edge_capacity() const { return edges_.size(); }
|
||||
// How full the fuller arena is, per mille.
|
||||
int fill_permille() const {
|
||||
return int(std::max(nodes_used_ * 1000 / nodes_.size(), edges_used_ * 1000 / edges_.size()));
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<Node> nodes_;
|
||||
size_t used_ = 0;
|
||||
std::vector<Edge> edges_;
|
||||
size_t nodes_used_ = 0;
|
||||
size_t edges_used_ = 0;
|
||||
u32 root_ = 1;
|
||||
|
||||
// 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.
|
||||
// Keeps what is reachable from `new_root`, dropping the edges of every
|
||||
// node with fewer than `min_visits` visits, and packs both arenas to the
|
||||
// front in place.
|
||||
size_t compact(u32 new_root, u32 min_visits);
|
||||
u32 prune_threshold(size_t target) const;
|
||||
u32 prune_threshold(size_t node_target, size_t edge_target) const;
|
||||
};
|
||||
|
||||
// Butterfly history for MCTS (after Vine, aronpetkovski.com/blog/butterfly-history-mcts):
|
||||
@@ -160,7 +205,8 @@ public:
|
||||
Searcher();
|
||||
|
||||
void set_hash(size_t mb) {
|
||||
// A 32nd of the memory goes to the value hash, the rest to the tree.
|
||||
// A 32nd of the memory goes to the value hash, the rest to the tree
|
||||
// (split between its arenas by Tree::EDGES_PER_NODE).
|
||||
const size_t bytes = mb * 1024 * 1024;
|
||||
value_hash_.resize(bytes / 32 / ValueHash::ENTRY_BYTES);
|
||||
tree_.resize_bytes(bytes - bytes / 32);
|
||||
@@ -189,7 +235,10 @@ private:
|
||||
ValueHash value_hash_;
|
||||
std::vector<u64> history_;
|
||||
size_t root_history_size_ = 0;
|
||||
// The nodes of a playout, from the root, and the edges between them
|
||||
// (edge_path_[i] leads from path_[i] to path_[i + 1]).
|
||||
std::vector<u32> path_;
|
||||
std::vector<u32> edge_path_;
|
||||
|
||||
// The previous search, kept so its tree can be reused.
|
||||
bool tree_valid_ = false;
|
||||
@@ -220,16 +269,20 @@ private:
|
||||
void playout();
|
||||
// First visit of a node: settles terminal states, otherwise evaluates.
|
||||
float evaluate_leaf(u32 idx, const Position& pos, int depth);
|
||||
// Second visit: creates the children with their priors. False when the
|
||||
// Second visit: creates the edges with their priors. False when the
|
||||
// arena is full (a collection is then scheduled).
|
||||
bool expand(u32 idx, const Position& pos);
|
||||
u32 select_child(u32 idx) const;
|
||||
// The edge PUCT picks at node `idx`.
|
||||
u32 select_edge(u32 idx) const;
|
||||
bool is_repetition(const Position& pos) const;
|
||||
// Finds the node for `root` in the previous tree and makes it the root.
|
||||
// Returns the number of nodes kept, 0 when nothing could be reused.
|
||||
size_t reuse_tree(const Position& root);
|
||||
// Empties the tree, leaving an unexpanded root for `root`.
|
||||
void clear_tree(const Position& root);
|
||||
#ifdef RIGBY_VERIFY_TREE
|
||||
void verify_tree() const;
|
||||
// Checks the tree from root_pos_; `history` ends with the root's key.
|
||||
void verify_tree(const std::vector<u64>& history) const;
|
||||
#endif
|
||||
|
||||
void compute_time_limit(const SearchLimits& limits);
|
||||
@@ -237,7 +290,10 @@ private:
|
||||
bool should_stop(const SearchLimits& limits) const;
|
||||
bool best_move_settled(i64 ms, i64 limit_ms) const;
|
||||
|
||||
u32 best_child(u32 idx) const;
|
||||
// The edge with the most visits at node `idx`, 0 when it has none.
|
||||
u32 best_edge(u32 idx) const;
|
||||
// Q of an edge from the view of the side choosing it (0 when unvisited).
|
||||
float edge_q(const Edge& e) const { return e.visits ? tree_.node(e.child).q : 0.0f; }
|
||||
void print_info() const;
|
||||
std::vector<Move> principal_variation() const;
|
||||
void draw_dashboard(bool done);
|
||||
|
||||
Reference in new issue
Block a user