feat: value hash for transpositions
Rigby evaluated every new leaf from scratch, although most leaves are positions it has already searched: with temporary instrumentation, 37-85% of the leaf evaluations in a 5-second search repeated a position evaluated earlier in the same search (85% from the start position, 81% in a quiet middlegame, 37% in Kiwipete). Monty, Vine and Jackal all keep a hash table of positions' Q for this. Rigby now does the same: every node on a playout's path stores its Q (from the view of the side to move there) with its visit count under the position's key; a different position always replaces an entry, the same one only with at least as many visits. A leaf whose position is found takes the stored Q instead of the value network and quiescence search, so it starts from what a deeper search already knew. Draws by repetition or the 50-move rule are not stored (they belong to the path, not the position). The table lives across searches and is cleared on ucinewgame. It takes a 32nd of Hash (12-byte entries; 174k entries at 64 MB), the tree the rest. Checked: bench deterministic; RIGBY_VERIFY_TREE build driven over UCI with pauses (tree reuse up to 83k nodes, collections at Hash 1 MB, Hash changes, wtime/winc, movetime, mate-in-one positions found, go infinite + stop) without an abort. Bench: 615835 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
74fb5eb2fb
commit
db228fc5f9
2 files changed
+52
-5
No files matched your search
+43
-2
@@ -35,7 +35,7 @@ struct Node {
|
||||
|
||||
class Tree {
|
||||
public:
|
||||
void resize_mb(size_t mb);
|
||||
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);
|
||||
@@ -85,6 +85,42 @@ private:
|
||||
std::array<std::array<std::array<i16, 64>, 64>, 2> table_{};
|
||||
};
|
||||
|
||||
// Q of positions searched before, from the view of the side to move there
|
||||
// (as in Monty and Vine). Every node of a playout's path stores its Q here;
|
||||
// a leaf whose position is found takes that value instead of a fresh
|
||||
// evaluation. Transpositions are common: 37-85% of leaf evaluations in a
|
||||
// 5-second search repeat a position already evaluated in the same search.
|
||||
class ValueHash {
|
||||
public:
|
||||
void resize(size_t entries) {
|
||||
table_.assign(std::max<size_t>(entries, 1), Entry{});
|
||||
table_.shrink_to_fit();
|
||||
}
|
||||
void clear() { std::fill(table_.begin(), table_.end(), Entry{}); }
|
||||
bool probe(u64 key, float& q) const {
|
||||
const Entry& e = table_[key % table_.size()];
|
||||
if (!e.visits || e.check != u32(key >> 32)) return false;
|
||||
q = e.q;
|
||||
return true;
|
||||
}
|
||||
// A different position always replaces the entry; the same one only with
|
||||
// at least as many visits.
|
||||
void store(u64 key, float q, u32 visits) {
|
||||
Entry& e = table_[key % table_.size()];
|
||||
if (e.check == u32(key >> 32) && visits < e.visits) return;
|
||||
e = Entry{u32(key >> 32), visits, q};
|
||||
}
|
||||
static constexpr size_t ENTRY_BYTES = 12;
|
||||
|
||||
private:
|
||||
struct Entry {
|
||||
u32 check = 0;
|
||||
u32 visits = 0;
|
||||
float q = 0.0f;
|
||||
};
|
||||
std::vector<Entry> table_;
|
||||
};
|
||||
|
||||
struct SearchLimits {
|
||||
u64 nodes = 0;
|
||||
i64 movetime = 0;
|
||||
@@ -124,7 +160,10 @@ public:
|
||||
Searcher();
|
||||
|
||||
void set_hash(size_t mb) {
|
||||
tree_.resize_mb(mb);
|
||||
// A 32nd of the memory goes to the value hash, the rest to the tree.
|
||||
const size_t bytes = mb * 1024 * 1024;
|
||||
value_hash_.resize(bytes / 32 / ValueHash::ENTRY_BYTES);
|
||||
tree_.resize_bytes(bytes - bytes / 32);
|
||||
tree_valid_ = false;
|
||||
}
|
||||
|
||||
@@ -132,6 +171,7 @@ public:
|
||||
void new_game() {
|
||||
tree_valid_ = false;
|
||||
butterfly_.clear();
|
||||
value_hash_.clear();
|
||||
}
|
||||
|
||||
// Synchronous search. `history` holds the keys of every position that led to
|
||||
@@ -146,6 +186,7 @@ private:
|
||||
// Kept between moves of a game, like the tree.
|
||||
ButterflyHistory butterfly_;
|
||||
Position root_pos_;
|
||||
ValueHash value_hash_;
|
||||
std::vector<u64> history_;
|
||||
size_t root_history_size_ = 0;
|
||||
std::vector<u32> path_;
|
||||
|
||||
Reference in new issue
Block a user