From 026e1b0d41ad275b55b3427662a2fb6625013d3d Mon Sep 17 00:00:00 2001 From: Tilps Date: Sun, 6 May 2018 17:42:44 +1000 Subject: [PATCH 1/2] Add history to the NNCache key. --- src/Network.cpp | 10 ++++++++++ src/Parameters.cpp | 2 ++ src/Parameters.h | 1 + src/main.cpp | 5 +++++ 4 files changed, 18 insertions(+) diff --git a/src/Network.cpp b/src/Network.cpp index a0ce3e900..35824e9e3 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -1028,6 +1028,16 @@ void Network::softmax(const std::vector& input, Network::Netresult Network::get_scored_moves(const BoardHistory& pos, DebugRawData* debug_data, bool skip_cache) { Netresult result; auto full_key = pos.cur().full_key(); + if (cfg_cache_key_history) { + // Create a key which represents the history which is sent to the NN. + int history_count = pos.positions.size(); + if (history_count > 1) { + for (int i = history_count - 2; i >= 0 && i >= history_count - 8; i--) { + full_key *= 31; + full_key ^= pos.positions[i].full_key(); + } + } + } // See if we already have this in the cache. if (!skip_cache) { diff --git a/src/Parameters.cpp b/src/Parameters.cpp index 8bc08a18c..723acb259 100644 --- a/src/Parameters.cpp +++ b/src/Parameters.cpp @@ -75,6 +75,7 @@ std::string cfg_supervise; FILE* cfg_logfile_handle; bool cfg_quiet; bool cfg_go_nodes_as_playouts; +bool cfg_cache_key_history; void Parameters::setup_default_parameters() { cfg_allow_pondering = false; @@ -112,5 +113,6 @@ void Parameters::setup_default_parameters() { cfg_weightsfile = "weights.txt"; cfg_syzygypath = "syzygy"; cfg_go_nodes_as_playouts = false; + cfg_cache_key_history = false; } diff --git a/src/Parameters.h b/src/Parameters.h index 0db8c4542..a6118a7ec 100644 --- a/src/Parameters.h +++ b/src/Parameters.h @@ -56,6 +56,7 @@ extern std::string cfg_supervise; extern FILE* cfg_logfile_handle; extern bool cfg_quiet; extern bool cfg_go_nodes_as_playouts; +extern bool cfg_cache_key_history; class Parameters { public: diff --git a/src/main.cpp b/src/main.cpp index bed968cd5..7a0e0638c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -95,6 +95,7 @@ static std::string parse_commandline(int argc, char *argv[]) { "GUI is freezing on startup.") ("start", po::value(), "Start command {train, bench}.") ("supervise", po::value(), "Dump supervised learning data from the pgn.") + ("cachekeyhistory", "Include history in NN cache key.") #ifdef USE_OPENCL ("gpu", po::value >(), "ID of the OpenCL device(s) to use (disables autodetection).") @@ -151,6 +152,10 @@ static std::string parse_commandline(int argc, char *argv[]) { if (vm.count("quiet")) { cfg_quiet = true; } + if (vm.count("cachekeyhistory")) + { + cfg_cache_key_history = true; + } #ifdef USE_TUNER if (vm.count("puct")) { From e39ebd0e0c721a3c2e69c7507ccede6bad976cfe Mon Sep 17 00:00:00 2001 From: Tilps Date: Mon, 7 May 2018 17:27:55 +1000 Subject: [PATCH 2/2] Cleanup back down to just the key change. --- src/Network.cpp | 15 ++++++--------- src/Parameters.cpp | 2 -- src/Parameters.h | 1 - src/main.cpp | 5 ----- 4 files changed, 6 insertions(+), 17 deletions(-) diff --git a/src/Network.cpp b/src/Network.cpp index 35824e9e3..34e01857c 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -1028,15 +1028,12 @@ void Network::softmax(const std::vector& input, Network::Netresult Network::get_scored_moves(const BoardHistory& pos, DebugRawData* debug_data, bool skip_cache) { Netresult result; auto full_key = pos.cur().full_key(); - if (cfg_cache_key_history) { - // Create a key which represents the history which is sent to the NN. - int history_count = pos.positions.size(); - if (history_count > 1) { - for (int i = history_count - 2; i >= 0 && i >= history_count - 8; i--) { - full_key *= 31; - full_key ^= pos.positions[i].full_key(); - } - } + // Mix in the history to try and ensure no false positive lookups where + // the cached values would differ from the NN evals. + int history_count = pos.positions.size(); + for (int i = history_count - 2; i >= std::max(0, history_count - T_HISTORY); i--) { + full_key *= 31; + full_key ^= pos.positions[i].full_key(); } // See if we already have this in the cache. diff --git a/src/Parameters.cpp b/src/Parameters.cpp index 723acb259..8bc08a18c 100644 --- a/src/Parameters.cpp +++ b/src/Parameters.cpp @@ -75,7 +75,6 @@ std::string cfg_supervise; FILE* cfg_logfile_handle; bool cfg_quiet; bool cfg_go_nodes_as_playouts; -bool cfg_cache_key_history; void Parameters::setup_default_parameters() { cfg_allow_pondering = false; @@ -113,6 +112,5 @@ void Parameters::setup_default_parameters() { cfg_weightsfile = "weights.txt"; cfg_syzygypath = "syzygy"; cfg_go_nodes_as_playouts = false; - cfg_cache_key_history = false; } diff --git a/src/Parameters.h b/src/Parameters.h index a6118a7ec..0db8c4542 100644 --- a/src/Parameters.h +++ b/src/Parameters.h @@ -56,7 +56,6 @@ extern std::string cfg_supervise; extern FILE* cfg_logfile_handle; extern bool cfg_quiet; extern bool cfg_go_nodes_as_playouts; -extern bool cfg_cache_key_history; class Parameters { public: diff --git a/src/main.cpp b/src/main.cpp index 7a0e0638c..bed968cd5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -95,7 +95,6 @@ static std::string parse_commandline(int argc, char *argv[]) { "GUI is freezing on startup.") ("start", po::value(), "Start command {train, bench}.") ("supervise", po::value(), "Dump supervised learning data from the pgn.") - ("cachekeyhistory", "Include history in NN cache key.") #ifdef USE_OPENCL ("gpu", po::value >(), "ID of the OpenCL device(s) to use (disables autodetection).") @@ -152,10 +151,6 @@ static std::string parse_commandline(int argc, char *argv[]) { if (vm.count("quiet")) { cfg_quiet = true; } - if (vm.count("cachekeyhistory")) - { - cfg_cache_key_history = true; - } #ifdef USE_TUNER if (vm.count("puct")) {