Skip to content

PR925 + PR956 update #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/mcts/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "chess/position.h"
#include "neural/encoder.h"
#include "neural/writer.h"
#include "utils/fastmath.h"
#include "utils/mutex.h"

namespace lczero {
Expand Down Expand Up @@ -477,8 +478,10 @@ class EdgeAndNode {
Node* node() const { return node_; }

// Proxy functions for easier access to node/edge.
float GetQ(float default_q) const {
return (node_ && node_->GetN() > 0) ? node_->GetQ() : default_q;
float GetQ(float default_q, bool logit_q = false) const {
return (node_ && node_->GetN() > 0)
? (logit_q ? FastLogit(node_->GetQ()) : node_->GetQ())
: default_q;
}
float GetD() const {
return (node_ && node_->GetN() > 0) ? node_->GetD() : 0.0f;
Expand Down Expand Up @@ -521,7 +524,7 @@ class EdgeAndNode {

int GetVisitsToReachU(float target_score, float numerator,
float default_q) const {
const auto q = GetQ(default_q);
const auto q = GetQ(default_q, true);
if (q >= target_score) return std::numeric_limits<int>::max();
const auto n1 = GetNStarted() + 1;
return std::max(
Expand Down
22 changes: 12 additions & 10 deletions src/mcts/search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,13 @@ int64_t Search::GetTimeToDeadline() const {
}

namespace {
inline float GetFpu(const SearchParams& params, Node* node, bool is_root_node) {
inline float GetFpu(const SearchParams& params, Node* node,
bool is_root_node, bool logit_q = false) {
const auto value = params.GetFpuValue(is_root_node);
const auto Q = logit_q ? FastLogit(node->GetQ()) : node->GetQ();
return params.GetFpuAbsolute(is_root_node)
? value
: -node->GetQ() - value * std::sqrt(node->GetVisitedPolicy());
: -Q - value * std::sqrt(node->GetVisitedPolicy());
}

inline float ComputeCpuct(const SearchParams& params, uint32_t N) {
Expand All @@ -230,8 +232,8 @@ std::vector<std::string> Search::GetVerboseStats(Node* node,
std::sort(
edges.begin(), edges.end(),
[&fpu, &U_coeff](EdgeAndNode a, EdgeAndNode b) {
return std::forward_as_tuple(a.GetN(), a.GetQ(fpu) + a.GetU(U_coeff)) <
std::forward_as_tuple(b.GetN(), b.GetQ(fpu) + b.GetU(U_coeff));
return std::forward_as_tuple(a.GetN(), a.GetQ(fpu, true) + a.GetU(U_coeff)) <
std::forward_as_tuple(b.GetN(), b.GetQ(fpu, true) + b.GetU(U_coeff));
});

std::vector<std::string> infos;
Expand Down Expand Up @@ -260,7 +262,7 @@ std::vector<std::string> Search::GetVerboseStats(Node* node,
<< ") ";

oss << "(Q+U: " << std::setw(8) << std::setprecision(5)
<< edge.GetQ(fpu) + edge.GetU(U_coeff) << ") ";
<< edge.GetQ(fpu, true) + edge.GetU(U_coeff) << ") ";

oss << "(V: ";
optional<float> v;
Expand Down Expand Up @@ -981,7 +983,7 @@ SearchWorker::NodeToProcess SearchWorker::PickNodeToExtend(
float best = std::numeric_limits<float>::lowest();
float second_best = std::numeric_limits<float>::lowest();
int possible_moves = 0;
const float fpu = GetFpu(params_, node, is_root_node);
const float fpu = GetFpu(params_, node, is_root_node, true);
bool parent_upperbounded = node->IsOnlyUBounded();
for (auto child : node->Edges()) {
if (is_root_node) {
Expand Down Expand Up @@ -1015,7 +1017,7 @@ SearchWorker::NodeToProcess SearchWorker::PickNodeToExtend(
}
++possible_moves;
}
float Q = child.GetQ(fpu);
float Q = child.GetQ(fpu, true);

// Certainty Propagation. Avoid suboptimal childs.
if (params_.GetCertaintyPropagation()) {
Expand Down Expand Up @@ -1243,11 +1245,11 @@ int SearchWorker::PrefetchIntoCache(Node* node, int budget) {
const float cpuct = ComputeCpuct(params_, node->GetN());
const float puct_mult =
cpuct * std::sqrt(std::max(node->GetChildrenVisits(), 1u));
const float fpu = GetFpu(params_, node, node == search_->root_node_);
const float fpu = GetFpu(params_, node, node == search_->root_node_, true);
for (auto edge : node->Edges()) {
if (edge.GetP() == 0.0f) continue;
// Flip the sign of a score to be able to easily sort.
scores.emplace_back(-edge.GetU(puct_mult) - edge.GetQ(fpu), edge);
scores.emplace_back(-edge.GetU(puct_mult) - edge.GetQ(fpu, true), edge);
}

size_t first_unsorted_index = 0;
Expand Down Expand Up @@ -1277,7 +1279,7 @@ int SearchWorker::PrefetchIntoCache(Node* node, int budget) {
if (i != scores.size() - 1) {
// Sign of the score was flipped for sorting, so flip it back.
const float next_score = -scores[i + 1].first;
const float q = edge.GetQ(-fpu);
const float q = edge.GetQ(-fpu, true);
if (next_score > q) {
budget_to_spend =
std::min(budget, int(edge.GetP() * puct_mult / (next_score - q) -
Expand Down
8 changes: 8 additions & 0 deletions src/utils/fastmath.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,12 @@ inline float FastLog(const float a) {
return 0.6931471805599453f * FastLog2(a);
}

// Fast logit for more readable code.
// This is actually 0.5 * logit((a+1)/2) = atanh(a).
inline float FastLogit(const float a) {
// Scale Q slightly to avoid logit(1) = infinity.
float b = 0.9999999f * a;
return 0.5f * FastLog((1.0f + b) / (1.0f - b));
}

} // namespace lczero