Merge pull request 'Value hash for transpositions' (#20) from hash-table into main

SPRT https://bench.rektdie.com/test/5307/
Elo   | 21.53 +- 9.13 (95%)
SPRT  | 8.0+0.08s Threads=1 Hash=64MB
LLR   | 3.15 (-2.94, 2.94) [0.00, 5.00]
Games | N: 2698 W: 952 L: 785 D: 961
Penta | [72, 251, 571, 348, 107]

Bench: 615835
This commit was merged in pull request #20.
This commit is contained in:
claude committed 2026-11-16 00:00:31 +01:00
commit d7f97ee80b
2 files changed
+52 -5

No files matched your search

+9 -3
View File
@@ -16,8 +16,8 @@ namespace rigby {
// Tree
// ---------------------------------------------------------------------------
void Tree::resize_mb(size_t mb) {
const size_t count = std::max<size_t>(mb * 1024 * 1024 / sizeof(Node), 1024);
void Tree::resize_bytes(size_t bytes) {
const size_t count = std::max<size_t>(bytes / sizeof(Node), 1024);
nodes_.assign(count, Node{});
nodes_.shrink_to_fit();
clear();
@@ -129,7 +129,7 @@ size_t Tree::compact(u32 new_root, u32 min_visits) {
// Searcher
// ---------------------------------------------------------------------------
Searcher::Searcher() { tree_.resize_mb(64); }
Searcher::Searcher() { set_hash(64); }
i64 Searcher::elapsed_ms() const {
return std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - start_time_).count();
@@ -315,6 +315,9 @@ float Searcher::evaluate_leaf(u32 idx, const Position& pos, int depth) {
return 0.0f;
}
// A position searched before (a transposition, or from an earlier search)
// brings its Q along.
if (float q; value_hash_.probe(pos.key, q)) return q;
return eval::value(pos);
}
@@ -443,6 +446,9 @@ void Searcher::playout() {
Node& node = tree_[path_[i]];
node.visits++;
node.q += (value - node.q) / float(node.visits);
// The Q from the view of the side to move at the node. Draws by
// repetition or the 50-move rule belong to the path, not the position.
if (node.state != TERMINAL_DRAW) value_hash_.store(history_[root_history_size_ - 1 + i], -node.q, node.visits);
if (i) butterfly_.update(root_pos_.stm ^ int((i - 1) & 1), Move::from_raw(node.move), bonus);
}
+43 -2
View File
@@ -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_;