feat: expand nodes on their second visit
Most leaves are visited only once. Their first visit now only settles terminal states and evaluates the position; the move list, the priors and the child nodes are created when search comes back a second time. The root is expanded before the first playout, so a search always has a move. With the value network the tree is full on nearly every move at 8+0.08 with Hash=64 (hashfull 1000 in a 30-ply self-play diagnostic); with this change many moves stay at 320-890. The -DRIGBY_VERIFY_TREE build passed 1700 tree checks in UCI-driven self-play with 1 MB and 16 MB Hash, and a 1-playout search still returns a move. Rebased from the earlier expand-second-visit branch onto the network main, without the tree collection it was built on. Bench: 546018 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6057476214
commit
cdeb318016
2 files changed
+40
-10
No files matched your search
+35
-9
@@ -199,7 +199,10 @@ void Searcher::verify_tree() const {
|
|||||||
next.make_move(m);
|
next.make_move(m);
|
||||||
stack.push_back({node.first_child + c, next});
|
stack.push_back({node.first_child + c, next});
|
||||||
}
|
}
|
||||||
if (!ok || node.visits < child_visits + 1) {
|
// The root is expanded before its first visit, so it may have exactly
|
||||||
|
// as many visits as its children.
|
||||||
|
const u64 own_visit = item.idx == Tree::root() ? 0 : 1;
|
||||||
|
if (!ok || node.visits < child_visits + own_visit) {
|
||||||
std::cerr << "TREE CORRUPT at node " << item.idx << " (" << item.pos.fen() << ") ok=" << ok
|
std::cerr << "TREE CORRUPT at node " << item.idx << " (" << item.pos.fen() << ") ok=" << ok
|
||||||
<< " visits=" << node.visits << " child_visits=" << child_visits
|
<< " visits=" << node.visits << " child_visits=" << child_visits
|
||||||
<< " children=" << int(node.num_children) << " legal=" << legal.size()
|
<< " children=" << int(node.num_children) << " legal=" << legal.size()
|
||||||
@@ -211,7 +214,7 @@ void Searcher::verify_tree() const {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
float Searcher::expand(u32 idx, const Position& pos, int depth) {
|
float Searcher::evaluate_leaf(u32 idx, const Position& pos, int depth) {
|
||||||
Node& node = tree_[idx];
|
Node& node = tree_[idx];
|
||||||
|
|
||||||
MoveList moves;
|
MoveList moves;
|
||||||
@@ -228,11 +231,20 @@ float Searcher::expand(u32 idx, const Position& pos, int depth) {
|
|||||||
return 0.0f;
|
return 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return eval::value(pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Searcher::expand(u32 idx, const Position& pos) {
|
||||||
|
Node& node = tree_[idx];
|
||||||
|
|
||||||
|
MoveList moves;
|
||||||
|
generate_legal(pos, moves);
|
||||||
|
|
||||||
const u32 first = tree_.alloc(u32(moves.size()));
|
const u32 first = tree_.alloc(u32(moves.size()));
|
||||||
if (!first) {
|
if (!first) {
|
||||||
// Out of memory: keep the node as a leaf and just evaluate it again.
|
// Out of memory: the node stays a leaf and is evaluated again.
|
||||||
tree_full_ = true;
|
tree_full_ = true;
|
||||||
return eval::value(pos);
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
float priors[256];
|
float priors[256];
|
||||||
@@ -248,8 +260,7 @@ float Searcher::expand(u32 idx, const Position& pos, int depth) {
|
|||||||
node.first_child = first;
|
node.first_child = first;
|
||||||
node.num_children = u8(moves.size());
|
node.num_children = u8(moves.size());
|
||||||
node.state = EXPANDED;
|
node.state = EXPANDED;
|
||||||
|
return true;
|
||||||
return eval::value(pos);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 Searcher::select_child(u32 idx) const {
|
u32 Searcher::select_child(u32 idx) const {
|
||||||
@@ -304,8 +315,16 @@ void Searcher::playout() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (node.state == UNEXPANDED) {
|
if (node.state == UNEXPANDED) {
|
||||||
value = expand(idx, pos, depth);
|
// Most leaves are only ever visited once, so a first visit just
|
||||||
break;
|
// evaluates; children (moves, priors, memory) come on the second.
|
||||||
|
if (node.visits == 0) {
|
||||||
|
value = evaluate_leaf(idx, pos, depth);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!expand(idx, pos)) {
|
||||||
|
value = eval::value(pos);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
idx = select_child(idx);
|
idx = select_child(idx);
|
||||||
@@ -409,8 +428,15 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The root needs its children before the first playout, so that even a
|
||||||
|
// single playout leaves a move to play.
|
||||||
|
if (tree_[Tree::root()].state == UNEXPANDED && !expand(Tree::root(), root)) {
|
||||||
|
tree_.clear();
|
||||||
|
tree_full_ = false;
|
||||||
|
expand(Tree::root(), root);
|
||||||
|
}
|
||||||
|
|
||||||
i64 last_info = 0;
|
i64 last_info = 0;
|
||||||
// Always run at least one playout so the root gets expanded.
|
|
||||||
do {
|
do {
|
||||||
playout();
|
playout();
|
||||||
if (print && (iterations_ & 1023) == 0) {
|
if (print && (iterations_ & 1023) == 0) {
|
||||||
|
|||||||
+5
-1
@@ -117,7 +117,11 @@ private:
|
|||||||
bool tree_full_ = false;
|
bool tree_full_ = false;
|
||||||
|
|
||||||
void playout();
|
void playout();
|
||||||
float expand(u32 idx, const Position& pos, int depth);
|
// 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
|
||||||
|
// arena is full (a collection is then scheduled).
|
||||||
|
bool expand(u32 idx, const Position& pos);
|
||||||
u32 select_child(u32 idx) const;
|
u32 select_child(u32 idx) const;
|
||||||
bool is_repetition(const Position& pos) const;
|
bool is_repetition(const Position& pos) const;
|
||||||
// Finds the node for `root` in the previous tree and makes it the root.
|
// Finds the node for `root` in the previous tree and makes it the root.
|
||||||
|
|||||||
Reference in new issue
Block a user