Skip to content
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

Add recursive search depth, remove FPU VL bug #466

Merged
merged 7 commits into from
May 2, 2018
Merged
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
2 changes: 1 addition & 1 deletion src/Parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void Parameters::setup_default_parameters() {
#endif
cfg_puct = 0.6f;
cfg_softmax_temp = 1.0f;
cfg_fpu_reduction = 0.0f;
cfg_fpu_reduction = 0.1f;
cfg_fpu_dynamic_eval = true;
cfg_root_temp_decay = 0;
cfg_min_resign_moves = 20;
Expand Down
2 changes: 1 addition & 1 deletion src/UCTNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ UCTNode* UCTNode::uct_select_child(Color color, bool is_root) {

// Estimated eval for unknown nodes = original parent NN eval - reduction
// Or curent parent eval - reduction if dynamic_eval is enabled.
auto fpu_eval = (cfg_fpu_dynamic_eval ? get_eval(color) : net_eval) - fpu_reduction;
auto fpu_eval = (cfg_fpu_dynamic_eval ? get_raw_eval(color) : net_eval) - fpu_reduction;

for (const auto& child : m_children) {
if (!child->active()) {
Expand Down
20 changes: 11 additions & 9 deletions src/UCTSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@ void UCTSearch::set_quiet(bool quiet) {
quiet_ = quiet;
}

SearchResult UCTSearch::play_simulation(BoardHistory& bh, UCTNode* const node) {
SearchResult UCTSearch::play_simulation(BoardHistory& bh, UCTNode* const node, int ndepth) {
const auto& cur = bh.cur();
const auto color = cur.side_to_move();

auto result = SearchResult{};

node->virtual_loss();
if (ndepth > m_maxdepth) {
m_maxdepth = ndepth;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you actually want to make this truly threadsafe, it would need to be something like....
int cur_depth;
do { cur_depth = m_maxdepth; } while(ndepth > cur_depth && !m_maxdepth.compare_exchange_strong(cur_depth, ndepth);

But maybe we don't care that much?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, it probably isn't really thread safe at the moment. However, @killerducky indicates it won't be pulled as is anyway, he prefers the solution defining search depth on PV length since that is more compatible to the Tensorflow implementation.

I'm just keeping this open now until I post the last tuning tests with it, and then close it. Making additional tuning options available without UCI can be its own PR, and fixing the VL bug will probably have to wait a bit...


if (!node->has_children()) {
bool drawn = cur.is_draw();
Expand Down Expand Up @@ -102,7 +105,7 @@ SearchResult UCTSearch::play_simulation(BoardHistory& bh, UCTNode* const node) {
auto next = node->uct_select_child(color, node == m_root.get());
auto move = next->get_move();
bh.do_move(move);
result = play_simulation(bh, next);
result = play_simulation(bh, next, ndepth+1);
}

if (result.valid()) {
Expand Down Expand Up @@ -266,8 +269,7 @@ void UCTSearch::dump_analysis(int64_t elapsed, bool force_output) {
float feval = m_root->get_raw_eval(color);
// UCI-like output wants a depth and a cp, so convert winrate to a cp estimate.
int cp = 290.680623072 * tan(3.096181612 * (feval - 0.5));
// same for nodes to depth, assume nodes = 1.8 ^ depth.
int depth = log(float(m_nodes)) / log(1.8);
int depth = m_maxdepth;
// To report nodes, use visits.
// - Only includes expanded nodes.
// - Includes nodes carried over from tree reuse.
Expand Down Expand Up @@ -364,7 +366,7 @@ bool UCTSearch::pv_limit_reached() const {
void UCTWorker::operator()() {
do {
BoardHistory bh = bh_.shallow_clone();
auto result = m_search->play_simulation(bh, m_root);
auto result = m_search->play_simulation(bh, m_root, 0);
if (result.valid()) {
m_search->increment_playouts();
}
Expand All @@ -390,6 +392,7 @@ Move UCTSearch::think(BoardHistory&& new_bh) {
}

m_playouts = 0;
m_maxdepth = 0;
m_nodes = m_root->count_nodes();
m_tbhits = 0;
// TODO: Both UCI and the next line do shallow_clone.
Expand Down Expand Up @@ -447,13 +450,12 @@ Move UCTSearch::think(BoardHistory&& new_bh) {
int last_update = 0;
do {
auto currstate = bh_.shallow_clone();
auto result = play_simulation(currstate, m_root.get());
auto result = play_simulation(currstate, m_root.get(), 0);
if (result.valid()) {
increment_playouts();
}

// assume nodes = 1.8 ^ depth.
int depth = log(float(m_nodes)) / log(1.8);
int depth = m_maxdepth;
if (depth != last_update) {
last_update = depth;
dump_analysis(Time.elapsed(), false);
Expand Down Expand Up @@ -506,7 +508,7 @@ void UCTSearch::ponder() {
}
do {
auto bh = bh_.shallow_clone();
auto result = play_simulation(bh, m_root.get());
auto result = play_simulation(bh, m_root.get(), 0);
if (result.valid()) {
increment_playouts();
}
Expand Down
3 changes: 2 additions & 1 deletion src/UCTSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class UCTSearch {
void increment_playouts();
bool should_halt_search();
void please_stop();
SearchResult play_simulation(BoardHistory& bh, UCTNode* const node);
SearchResult play_simulation(BoardHistory& bh, UCTNode* const node, int sdepth);

private:
void dump_stats(BoardHistory& pos, UCTNode& parent);
Expand All @@ -95,6 +95,7 @@ class UCTSearch {
std::unique_ptr<UCTNode> m_root;
std::atomic<int> m_nodes{0};
std::atomic<int> m_playouts{0};
std::atomic<int> m_maxdepth{0};
std::atomic<int> m_tbhits{0};
int64_t m_target_time{0};
int64_t m_max_time{0};
Expand Down
2 changes: 1 addition & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#endif
static constexpr int SELFCHECK_PROBABILITY = 2000;
static constexpr int SELFCHECK_MIN_EXPANSIONS = 2'000'000;
//#define USE_TUNER
#define USE_TUNER

#define PROGRAM_VERSION "v0.7"

Expand Down
4 changes: 4 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ static std::string parse_commandline(int argc, char *argv[]) {
#endif
#ifdef USE_TUNER
("puct", po::value<float>())
("fpu_reduction", po::value<float>())
("softmax_temp", po::value<float>())
#endif
;
Expand Down Expand Up @@ -150,6 +151,9 @@ static std::string parse_commandline(int argc, char *argv[]) {
if (vm.count("puct")) {
cfg_puct = vm["puct"].as<float>();
}
if (vm.count("fpu_reduction")) {
cfg_fpu_reduction = vm["fpu_reduction"].as<float>();
}
if (vm.count("softmax_temp")) {
cfg_softmax_temp = vm["softmax_temp"].as<float>();
}
Expand Down