verify: add the verify command to VERIFY builds (checks a and b)
(a) Along random games from standard and Chess960 positions (50 games of up to 300 plies from each of 7 FENs), the incremental key and the position check equal those of the same position set up from its FEN. (b) After a 30000-playout search on four positions (standard, middlegame, pawn endgame, Chess960): three forced collections down to 1/2, 1/4 and 1/8 of the tree, each compared with a snapshot taken by move path (visits, q, u, base, state, key, check, and every edge). Survivors must be unchanged, pruned nodes must keep visits and q with u = q and base = visits, both arenas must end within their targets, and no pruned node may have more visits than a kept one. Then a reroot two plies down the best line must keep that subtree exactly. verify_tree runs after each step. Only compiled with -DRIGBY_VERIFY_TREE. Bench: 663779 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6629525938
commit
226106e50a
3 files changed
+205
No files matched your search
+194
@@ -5,7 +5,10 @@
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
#include <tuple>
|
||||
|
||||
#include "eval.h"
|
||||
#include "pretty.h"
|
||||
@@ -801,4 +804,195 @@ SearchResult Searcher::search(const Position& root, const std::vector<u64>& hist
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifdef RIGBY_VERIFY_TREE
|
||||
namespace {
|
||||
|
||||
[[noreturn]] void self_test_failed(const std::string& what) {
|
||||
std::cerr << "VERIFY FAILED: " << what << std::endl;
|
||||
std::abort();
|
||||
}
|
||||
|
||||
// What a node looked like, to compare after a collection or a reroot.
|
||||
struct NodeSnapshot {
|
||||
u64 key;
|
||||
u32 visits;
|
||||
float q, u;
|
||||
u32 base;
|
||||
u8 state;
|
||||
u16 check;
|
||||
// (move, policy, visits, draws, has a child) per edge
|
||||
std::vector<std::tuple<u16, u16, u32, u32, bool>> edges;
|
||||
};
|
||||
|
||||
using MovePath = std::vector<u16>;
|
||||
|
||||
} // namespace
|
||||
|
||||
void Searcher::self_test() {
|
||||
// (a) The incremental key equals the key of the same position set up from
|
||||
// its FEN, along random games, standard and Chess960.
|
||||
{
|
||||
static constexpr const char* FENS[] = {
|
||||
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
|
||||
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1",
|
||||
"rnbqkbnr/ppp1p1pp/8/3pPp2/8/8/PPPP1PPP/RNBQKBNR w KQkq f6 0 3",
|
||||
"bqnb1rkr/pp3ppp/3ppn2/2p5/5P2/P2P4/NPP1P1PP/BQ1BNRKR w HFhf - 2 9",
|
||||
"2nnrbkr/p1qppppp/8/1ppb4/6PP/3PP3/PPP2P2/BQNNRBKR w HEhe - 1 9",
|
||||
"qrbnnkrb/pppppppp/8/8/8/8/PPPPPPPP/QRBNNKRB w GBgb - 0 1",
|
||||
"rkrnnqbb/pppppppp/8/8/8/8/PPPPPPPP/RKRNNQBB w CAca - 0 1",
|
||||
};
|
||||
std::mt19937_64 rng(20261116);
|
||||
u64 plies = 0;
|
||||
for (const char* fen : FENS) {
|
||||
for (int game = 0; game < 50; game++) {
|
||||
Position pos;
|
||||
if (!pos.set_fen(fen)) self_test_failed(std::string("bad fen ") + fen);
|
||||
for (int ply = 0; ply < 300; ply++) {
|
||||
MoveList moves;
|
||||
generate_legal(pos, moves);
|
||||
if (moves.empty()) break;
|
||||
const Move m = moves[int(rng() % u64(moves.size()))];
|
||||
const std::string before = pos.fen();
|
||||
pos.make_move(m);
|
||||
Position fresh;
|
||||
if (!fresh.set_fen(pos.fen())) self_test_failed("fen does not parse: " + pos.fen());
|
||||
if (fresh.key != pos.key || position_check(fresh) != position_check(pos))
|
||||
self_test_failed("incremental key differs after " + before + " " + pos.uci(m) + " -> " +
|
||||
pos.fen());
|
||||
plies++;
|
||||
}
|
||||
}
|
||||
}
|
||||
uci_io::send("info string verify (a) keys: " + std::to_string(plies) + " plies ok");
|
||||
}
|
||||
|
||||
// (b) Garbage collection and rerooting keep everything they keep exactly.
|
||||
const auto snapshot = [&](u32 from) {
|
||||
std::map<MovePath, NodeSnapshot> snap;
|
||||
std::vector<std::pair<u32, MovePath>> stack{{from, {}}};
|
||||
while (!stack.empty()) {
|
||||
auto [idx, path] = stack.back();
|
||||
stack.pop_back();
|
||||
const Node& n = tree_.node(idx);
|
||||
NodeSnapshot s{n.key, n.visits, n.q, n.u, n.base, n.state, n.check, {}};
|
||||
for (u32 i = 0; i < n.num_edges; i++) {
|
||||
const Edge& e = tree_.edge(n.first_edge + i);
|
||||
s.edges.emplace_back(e.move, e.policy, e.visits, e.draws, e.child != 0);
|
||||
if (e.child) {
|
||||
MovePath next = path;
|
||||
next.push_back(e.move);
|
||||
stack.push_back({e.child, next});
|
||||
}
|
||||
}
|
||||
snap.emplace(path, std::move(s));
|
||||
}
|
||||
return snap;
|
||||
};
|
||||
|
||||
// Walks the current tree from the root and compares it with `before`.
|
||||
// Returns the number of nodes; `pruned_max` / `kept_min` get the largest
|
||||
// visits of a node that lost its edges and the smallest of a non-root node
|
||||
// that kept them.
|
||||
const auto compare = [&](const std::map<MovePath, NodeSnapshot>& before, bool allow_pruning, u32& pruned_max,
|
||||
u32& kept_min) {
|
||||
size_t count = 0;
|
||||
pruned_max = 0;
|
||||
kept_min = ~0u;
|
||||
std::vector<std::pair<u32, MovePath>> stack{{tree_.root(), {}}};
|
||||
while (!stack.empty()) {
|
||||
auto [idx, path] = stack.back();
|
||||
stack.pop_back();
|
||||
count++;
|
||||
const Node& n = tree_.node(idx);
|
||||
const auto it = before.find(path);
|
||||
if (it == before.end()) self_test_failed("a node appeared from nowhere");
|
||||
const NodeSnapshot& s = it->second;
|
||||
if (n.key != s.key || n.check != s.check || n.visits != s.visits || n.q != s.q)
|
||||
self_test_failed("node changed (key, visits or q)");
|
||||
if (!s.edges.empty() && !n.num_edges) {
|
||||
if (!allow_pruning) self_test_failed("a node lost its edges in a reroot");
|
||||
if (n.state != UNEXPANDED || n.first_edge || n.u != n.q || n.base != n.visits)
|
||||
self_test_failed("pruned node not reset properly");
|
||||
pruned_max = std::max(pruned_max, n.visits);
|
||||
continue;
|
||||
}
|
||||
if (n.state != s.state || n.u != s.u || n.base != s.base || n.num_edges != s.edges.size())
|
||||
self_test_failed("node changed (state, u, base or edges)");
|
||||
if (n.num_edges && !path.empty()) kept_min = std::min(kept_min, n.visits);
|
||||
for (u32 i = 0; i < n.num_edges; i++) {
|
||||
const Edge& e = tree_.edge(n.first_edge + i);
|
||||
if (std::make_tuple(e.move, e.policy, e.visits, e.draws, e.child != 0) != s.edges[i])
|
||||
self_test_failed("edge changed");
|
||||
if (e.child) {
|
||||
MovePath next = path;
|
||||
next.push_back(e.move);
|
||||
stack.push_back({e.child, next});
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
};
|
||||
|
||||
static constexpr const char* SEARCH_FENS[] = {
|
||||
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
|
||||
"r1bq1rk1/ppp2ppp/2np1n2/2b1p3/2B1P3/2NP1N2/PPP2PPP/R1BQ1RK1 w - - 0 7",
|
||||
"8/8/3k4/2p1p3/2P1P3/3K4/8/8 w - - 0 1",
|
||||
"bqnb1rkr/pp3ppp/3ppn2/2p5/5P2/P2P4/NPP1P1PP/BQ1BNRKR w HFhf - 2 9",
|
||||
};
|
||||
for (const char* fen : SEARCH_FENS) {
|
||||
Position root;
|
||||
root.set_fen(fen);
|
||||
SearchLimits limits;
|
||||
limits.nodes = 30000;
|
||||
|
||||
// Collections down to a half, a quarter and an eighth of the tree.
|
||||
new_game();
|
||||
stop_flag = false;
|
||||
search(root, {root.key}, limits, false);
|
||||
for (int round = 1; round <= 3; round++) {
|
||||
const auto before = snapshot(tree_.root());
|
||||
const size_t node_target = tree_.nodes_used() >> round, edge_target = tree_.edges_used() >> round;
|
||||
tree_.collect_garbage(node_target, edge_target);
|
||||
if (tree_.nodes_used() > node_target || tree_.edges_used() > edge_target)
|
||||
self_test_failed("collection kept more than its target");
|
||||
u32 pruned_max, kept_min;
|
||||
const size_t count = compare(before, true, pruned_max, kept_min);
|
||||
if (count != tree_.nodes_used() - 1) self_test_failed("node count differs after collection");
|
||||
if (pruned_max >= kept_min) self_test_failed("pruned a node with more visits than one kept");
|
||||
verify_tree({root.key});
|
||||
uci_io::send("info string verify (b) gc " + std::string(fen) + ": " + std::to_string(before.size()) +
|
||||
" -> " + std::to_string(count) + " nodes ok");
|
||||
}
|
||||
|
||||
// Reroot two plies down the most visited line, as between two moves of a game.
|
||||
new_game();
|
||||
stop_flag = false;
|
||||
search(root, {root.key}, limits, false);
|
||||
std::vector<u64> history{root.key};
|
||||
Position pos = root;
|
||||
u32 idx = tree_.root();
|
||||
for (int ply = 0; ply < 2; ply++) {
|
||||
const u32 e = best_edge(idx);
|
||||
if (!e || !tree_.edge(e).child) break;
|
||||
pos.make_move(Move::from_raw(tree_.edge(e).move));
|
||||
history.push_back(pos.key);
|
||||
idx = tree_.edge(e).child;
|
||||
}
|
||||
if (idx == tree_.root() || tree_.node(idx).state != EXPANDED) self_test_failed("no node to reroot to");
|
||||
const auto before = snapshot(idx);
|
||||
tree_.reroot(idx);
|
||||
u32 pruned_max, kept_min;
|
||||
const size_t count = compare(before, false, pruned_max, kept_min);
|
||||
if (count != before.size() || count != tree_.nodes_used() - 1) self_test_failed("reroot lost nodes");
|
||||
root_pos_ = pos;
|
||||
verify_tree(history);
|
||||
uci_io::send("info string verify (b) reroot " + std::string(fen) + ": " + std::to_string(count) +
|
||||
" nodes ok");
|
||||
}
|
||||
|
||||
new_game();
|
||||
uci_io::send("info string verify passed");
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace rigby
|
||||
Reference in new issue
Block a user