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

Eval cache #992

Open
wants to merge 6 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
1 change: 1 addition & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ add_executable(katago
search/localpattern.cpp
search/searchnodetable.cpp
search/subtreevaluebiastable.cpp
search/evalcache.cpp
search/patternbonustable.cpp
search/analysisdata.cpp
search/reportedsearchvalues.cpp
Expand Down
140 changes: 138 additions & 2 deletions cpp/command/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,7 @@ int MainCmds::dataminesgfs(const vector<string>& args) {
double gameModeFastThreshold;
bool flipIfPassOrWFirst;
bool allowGameOver;
bool manualHintOnly;
double trainingWeight;

int minRank;
Expand Down Expand Up @@ -1396,6 +1397,7 @@ int MainCmds::dataminesgfs(const vector<string>& args) {
TCLAP::ValueArg<double> gameModeFastThresholdArg("","game-mode-fast-threshold","Utility threshold for game mode fast pass",false,0.005,"UTILS");
TCLAP::SwitchArg flipIfPassOrWFirstArg("","flip-if-pass","Try to heuristically find cases where an sgf passes to simulate white<->black");
TCLAP::SwitchArg allowGameOverArg("","allow-game-over","Allow sampling game over positions in sgf");
TCLAP::SwitchArg manualHintOnlyArg("","manual-hint-only","Allow only positions marked for hint in the sgf");
TCLAP::ValueArg<double> trainingWeightArg("","training-weight","Scale the loss function weight from data from games that originate from this position",false,1.0,"WEIGHT");
TCLAP::ValueArg<int> minRankArg("","min-rank","Require player making the move to have rank at least this",false,Sgf::RANK_UNKNOWN,"INT");
TCLAP::ValueArg<int> minMinRankArg("","min-min-rank","Require both players in a game to have rank at least this",false,Sgf::RANK_UNKNOWN,"INT");
Expand Down Expand Up @@ -1427,6 +1429,7 @@ int MainCmds::dataminesgfs(const vector<string>& args) {
cmd.add(gameModeFastThresholdArg);
cmd.add(flipIfPassOrWFirstArg);
cmd.add(allowGameOverArg);
cmd.add(manualHintOnlyArg);
cmd.add(trainingWeightArg);
cmd.add(minRankArg);
cmd.add(minMinRankArg);
Expand Down Expand Up @@ -1459,6 +1462,7 @@ int MainCmds::dataminesgfs(const vector<string>& args) {
gameModeFastThreshold = gameModeFastThresholdArg.getValue();
flipIfPassOrWFirst = flipIfPassOrWFirstArg.getValue();
allowGameOver = allowGameOverArg.getValue();
manualHintOnly = manualHintOnlyArg.getValue();
trainingWeight = trainingWeightArg.getValue();
minRank = minRankArg.getValue();
minMinRank = minMinRankArg.getValue();
Expand Down Expand Up @@ -1805,7 +1809,7 @@ int MainCmds::dataminesgfs(const vector<string>& args) {
// ---------------------------------------------------------------------------------------------------
//SGF MODE

auto processSgfGame = [&logger,&gameInit,&nnEval,&expensiveEvaluateMove,autoKomi,&gameModeFastThreshold,&maxDepth,&numFilteredSgfs,&maxHandicap,&maxPolicy,allowGameOver,trainingWeight](
auto processSgfGame = [&logger,&gameInit,&nnEval,&expensiveEvaluateMove,autoKomi,&gameModeFastThreshold,&maxDepth,&numFilteredSgfs,&maxHandicap,&maxPolicy,allowGameOver,manualHintOnly,trainingWeight](
Search* search, Rand& rand, const string& fileName, CompactSgf* sgf, bool blackOkay, bool whiteOkay
) {
//Don't use the SGF rules - randomize them for a bit more entropy
Expand Down Expand Up @@ -2286,14 +2290,18 @@ int MainCmds::dataminesgfs(const vector<string>& args) {
if((hist.moveHistory[hintIdx].pla == P_BLACK && !blackOkay) || (hist.moveHistory[hintIdx].pla == P_WHITE && !whiteOkay))
return;

bool markedAsHintPos = (comments.size() > 0 && comments.find("%HINT%") != string::npos);
if(manualHintOnly && !markedAsHintPos)
return;

//unusedSample doesn't have enough history, doesn't have hintloc the way we want it
int64_t numEnqueued = 1+numPosesEnqueued.fetch_add(1);
if(numEnqueued % 500 == 0)
logger.write("Enqueued " + Global::int64ToString(numEnqueued) + " poses");
PosQueueEntry entry;
entry.hist = new BoardHistory(hist);
assert(hist.getCurrentTurnNumber() == unusedSample.getCurrentTurnNumber());
entry.markedAsHintPos = (comments.size() > 0 && comments.find("%HINT%") != string::npos);
entry.markedAsHintPos = markedAsHintPos;
posQueue.waitPush(entry);
}
);
Expand Down Expand Up @@ -2806,3 +2814,131 @@ int MainCmds::sampleinitializations(const vector<string>& args) {
ScoreValue::freeTables();
return 0;
}


int MainCmds::checksgfhintpolicy(const vector<string>& args) {
Board::initHash();
ScoreValue::initTables();
Rand seedRand;

ConfigParser cfg;
string nnModelFile;
vector<string> sgfDirs;
try {
KataGoCommandLine cmd("Check policy for hint positions in sgfs");
cmd.addConfigFileArg("","");
cmd.addModelFileArg();
cmd.addOverrideConfigArg();

TCLAP::MultiArg<string> sgfDirArg("","sgfdir","Directory of sgf files",true,"DIR");
cmd.add(sgfDirArg);
cmd.parseArgs(args);

nnModelFile = cmd.getModelFile();
sgfDirs = sgfDirArg.getValue();

cmd.getConfig(cfg);
}
catch (TCLAP::ArgException &e) {
cerr << "Error: " << e.error() << " for argument " << e.argId() << endl;
return 1;
}

const bool logToStdoutDefault = true;
Logger logger(&cfg, logToStdoutDefault);

NNEvaluator* nnEval;
{
Setup::initializeSession(cfg);
const int expectedConcurrentEvals = 1;
const int defaultMaxBatchSize = 8;
const bool defaultRequireExactNNLen = false;
const bool disableFP16 = false;
const string expectedSha256 = "";
nnEval = Setup::initializeNNEvaluator(
nnModelFile,nnModelFile,expectedSha256,cfg,logger,seedRand,expectedConcurrentEvals,
NNPos::MAX_BOARD_LEN,NNPos::MAX_BOARD_LEN,defaultMaxBatchSize,defaultRequireExactNNLen,disableFP16,
Setup::SETUP_FOR_ANALYSIS
);
}
logger.write("Loaded neural net");

vector<string> sgfFiles;
FileHelpers::collectSgfsFromDirs(sgfDirs, sgfFiles);
logger.write("Found " + Global::int64ToString((int64_t)sgfFiles.size()) + " sgf files!");

int64_t numHintPositions = 0;
double logPolicySum = 0.0;
double logPolicyWeight = 0.0;

for(size_t i = 0; i<sgfFiles.size(); i++) {
Sgf* sgf = NULL;
try {
sgf = Sgf::loadFile(sgfFiles[i]);
}
catch(const StringError& e) {
logger.write("Invalid SGF " + sgfFiles[i] + ": " + e.what());
continue;
}

std::set<Hash128> uniqueHashes;
bool hashComments = true;
bool hashParent = true;
bool flipIfPassOrWFirst = false;
bool allowGameOver = false;
Rand rand;

const std::vector<Rules> rulesToUse = {
Rules::parseRules("chinese"),
Rules::parseRules("japanese")
};

logger.write("Processing sgf: " + sgfFiles[i] + " hint positions " + Global::int64ToString(numHintPositions));
sgf->iterAllUniquePositions(
uniqueHashes, hashComments, hashParent, flipIfPassOrWFirst, allowGameOver, &rand,
[&](Sgf::PositionSample& posSample, const BoardHistory& hist, const string& comments) {
if(comments.find("%HINT%") == string::npos)
return;
(void)hist; // Ignore, we want the position before the hint move

if(!posSample.hasPreviousPositions(1))
return;
numHintPositions++;
Sgf::PositionSample priorPosSample = posSample.previousPosition(1.0);

for(const Rules& rules: rulesToUse) {
Player nextPla;
BoardHistory histBefore = priorPosSample.getCurrentBoardHistory(rules,nextPla);
Board board = histBefore.getRecentBoard(0);

for(int symmetry = 0; symmetry < 8; symmetry++) {
MiscNNInputParams nnInputParams;
NNResultBuf buf;
bool skipCache = true;
bool includeOwnerMap = false;
nnEval->evaluate(board,histBefore,nextPla,nnInputParams,buf,skipCache,includeOwnerMap);

shared_ptr<NNOutput> nnOutput = std::move(buf.result);
int pos = NNPos::locToPos(posSample.moves[posSample.moves.size()-1].loc, board.x_size, nnOutput->nnXLen, nnOutput->nnYLen);
double policy = nnOutput->policyProbs[pos];
logPolicySum += log(policy + 1e-30);
logPolicyWeight += 1.0;
}
}
}
);

delete sgf;
}

double averageLogPolicy = logPolicySum / logPolicyWeight;

cout << "Total number of hint positions: " << numHintPositions << endl;
cout << "Average log policy across all hints: " << averageLogPolicy << endl;

delete nnEval;
NeuralNet::globalCleanup();
ScoreValue::freeTables();

return 0;
}
17 changes: 17 additions & 0 deletions cpp/dataio/sgf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,23 @@ Sgf::PositionSample Sgf::PositionSample::previousPosition(double newWeight) cons
return other;
}

BoardHistory Sgf::PositionSample::getCurrentBoardHistory(const Rules& rules, Player& nextPlaToMove) const {
int encorePhase = 0;
Player pla = nextPla;
Board boardCopy = board;
BoardHistory hist(boardCopy,pla,rules,encorePhase);
int numSampleMoves = (int)moves.size();
for(int i = 0; i<numSampleMoves; i++) {
if(!hist.isLegal(boardCopy,moves[i].loc,moves[i].pla))
return hist;
assert(moves[i].pla == pla);
hist.makeBoardMoveAssumeLegal(boardCopy,moves[i].loc,moves[i].pla,NULL);
pla = getOpp(pla);
}
nextPlaToMove = pla;
return hist;
}

int64_t Sgf::PositionSample::getCurrentTurnNumber() const {
return std::max((int64_t)0, initialTurnNumber + (int64_t)moves.size());
}
Expand Down
2 changes: 2 additions & 0 deletions cpp/dataio/sgf.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ struct Sgf {
Sgf::PositionSample previousPosition(double newWeight) const;
bool hasPreviousPositions(int numPrevious) const;

BoardHistory getCurrentBoardHistory(const Rules& rules, Player& nextPlaToMove) const;

int64_t getCurrentTurnNumber() const;

//For the moment, only used in testing since it does extra consistency checks.
Expand Down
2 changes: 2 additions & 0 deletions cpp/game/boardhistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,8 @@ Hash128 BoardHistory::getSituationRulesAndKoHash(const Board& board, const Board
hash ^= Rules::ZOBRIST_MULTI_STONE_SUICIDE_HASH;
if(hist.hasButton)
hash ^= Rules::ZOBRIST_BUTTON_HASH;
if(hist.rules.friendlyPassOk)
hash ^= Rules::ZOBRIST_FRIENDLY_PASS_OK_HASH;

return hash;
}
Expand Down
4 changes: 4 additions & 0 deletions cpp/game/rules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,7 @@ const Hash128 Rules::ZOBRIST_MULTI_STONE_SUICIDE_HASH = //Based on sha256 hash

const Hash128 Rules::ZOBRIST_BUTTON_HASH = //Based on sha256 hash of Rules::ZOBRIST_BUTTON_HASH
Hash128(0xb8b914c9234ece84ULL, 0x3d759cddebe29c14ULL);

const Hash128 Rules::ZOBRIST_FRIENDLY_PASS_OK_HASH = //Based on sha256 hash of Rules::ZOBRIST_FRIENDLY_PASS_OK_HASH
Hash128(0x0113655998ef0a25ULL, 0x99c9d04ecd964874ULL);

1 change: 1 addition & 0 deletions cpp/game/rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ struct Rules {
static const Hash128 ZOBRIST_TAX_RULE_HASH[3];
static const Hash128 ZOBRIST_MULTI_STONE_SUICIDE_HASH;
static const Hash128 ZOBRIST_BUTTON_HASH;
static const Hash128 ZOBRIST_FRIENDLY_PASS_OK_HASH;

private:
nlohmann::json toJsonHelper(bool omitKomi, bool omitDefaults) const;
Expand Down
2 changes: 2 additions & 0 deletions cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ static int handleSubcommand(const string& subcommand, const vector<string>& args
return MainCmds::trystartposes(subArgs);
else if(subcommand == "viewstartposes")
return MainCmds::viewstartposes(subArgs);
else if(subcommand == "checksgfhintpolicy")
return MainCmds::checksgfhintpolicy(subArgs);
else if(subcommand == "demoplay")
return MainCmds::demoplay(subArgs);
else if(subcommand == "writetrainingdata")
Expand Down
1 change: 1 addition & 0 deletions cpp/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace MainCmds {

int trystartposes(const std::vector<std::string>& args);
int viewstartposes(const std::vector<std::string>& args);
int checksgfhintpolicy(const std::vector<std::string>& args);

int demoplay(const std::vector<std::string>& args);
int printclockinfo(const std::vector<std::string>& args);
Expand Down
8 changes: 8 additions & 0 deletions cpp/program/setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,14 @@ vector<SearchParams> Setup::loadParams(
else if(cfg.contains("subtreeValueBiasWeightExponent")) params.subtreeValueBiasWeightExponent = cfg.getDouble("subtreeValueBiasWeightExponent", 0.0, 1.0);
else params.subtreeValueBiasWeightExponent = 0.85;

if(cfg.contains("useEvalCache"+idxStr)) params.useEvalCache = cfg.getBool("useEvalCache"+idxStr);
else if(cfg.contains("useEvalCache")) params.useEvalCache = cfg.getBool("useEvalCache");
else params.useEvalCache = false;

if(cfg.contains("evalCacheMinVisits"+idxStr)) params.evalCacheMinVisits = cfg.getInt64("evalCacheMinVisits"+idxStr, (int64_t)1, (int64_t)1 << 50);
else if(cfg.contains("evalCacheMinVisits")) params.evalCacheMinVisits = cfg.getInt64("evalCacheMinVisits", (int64_t)1, (int64_t)1 << 50);
else params.evalCacheMinVisits = 100;

if(cfg.contains("nodeTableShardsPowerOfTwo"+idxStr)) params.nodeTableShardsPowerOfTwo = cfg.getInt("nodeTableShardsPowerOfTwo"+idxStr, 8, 24);
else if(cfg.contains("nodeTableShardsPowerOfTwo")) params.nodeTableShardsPowerOfTwo = cfg.getInt("nodeTableShardsPowerOfTwo", 8, 24);
else params.nodeTableShardsPowerOfTwo = 16;
Expand Down
Loading