Skip to content

Commit

Permalink
Add homeDataDir option to config
Browse files Browse the repository at this point in the history
  • Loading branch information
lightvector committed Jun 4, 2020
1 parent 28085b8 commit 4a79cde
Show file tree
Hide file tree
Showing 20 changed files with 98 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ Run a JSON-based [analysis engine](docs/Analysis_Engine.md) that can do efficien

For OpenCL only: run or re-run the tuner to optimize for your particular GPU.

* `./katago tuner -model <NEURALNET>.gz`
* `./katago tuner -config <GTP_CONFIG>.cfg -model <NEURALNET>.gz`


### Tuning for Performance
Expand Down
22 changes: 17 additions & 5 deletions cpp/command/commandline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,7 @@ string KataGoCommandLine::getConfigFile() const {
return configFile;
}

//cfg must be uninitialized, this will initialize it based on user-provided arguments
void KataGoCommandLine::getConfig(ConfigParser& cfg) const {
string configFile = getConfigFile();
cfg.initialize(configFile);

void KataGoCommandLine::maybeApplyOverrideConfigArg(ConfigParser& cfg) const {
if(overrideConfigArg != NULL) {
string overrideConfig = overrideConfigArg->getValue();
if(overrideConfig != "") {
Expand All @@ -308,5 +304,21 @@ void KataGoCommandLine::getConfig(ConfigParser& cfg) const {
cfg.overrideKeys(newkvs,mutexKeySets);
}
}
}

//cfg must be uninitialized, this will initialize it based on user-provided arguments
void KataGoCommandLine::getConfig(ConfigParser& cfg) const {
string configFile = getConfigFile();
cfg.initialize(configFile);
maybeApplyOverrideConfigArg(cfg);
}

void KataGoCommandLine::getConfigAllowEmpty(ConfigParser& cfg) const {
if(configFileArg->getValue().empty() && defaultConfigFileName.empty()) {
cfg.initialize(std::map<string,string>());
maybeApplyOverrideConfigArg(cfg);
}
else {
getConfig(cfg);
}
}
2 changes: 2 additions & 0 deletions cpp/command/commandline.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ class KataGoCommandLine : public TCLAP::CmdLine
bool modelFileIsDefault() const;
//cfg must be uninitialized, this will initialize it based on user-provided arguments
void getConfig(ConfigParser& cfg) const;
void getConfigAllowEmpty(ConfigParser& cfg) const;

private:
std::string getConfigFile() const;
void maybeApplyOverrideConfigArg(ConfigParser& cfg) const;
};

#endif //COMMANDLINE_H_
10 changes: 9 additions & 1 deletion cpp/command/tune.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "../main.h"

#ifdef USE_OPENCL_BACKEND
#include "../program/setup.h"
#include "../neuralnet/opencltuner.h"
#endif

Expand All @@ -19,6 +20,7 @@ int MainCmds::tuner(int argc, const char* const* argv) {
return 0;
#else

ConfigParser cfg;
string modelFile;
string outputFile;
string gpuIdxsStr;
Expand All @@ -30,6 +32,7 @@ int MainCmds::tuner(int argc, const char* const* argv) {
bool full;
try {
KataGoCommandLine cmd("Perform GPU tuning for OpenCL.");
cmd.addConfigFileArg(KataGoCommandLine::defaultGtpConfigFileName(),"gtp_example.cfg");
cmd.addModelFileArg();

TCLAP::ValueArg<string> outputFileArg("","output","Filename to output tuning configration to",false,string(),"FILE");
Expand All @@ -41,6 +44,7 @@ int MainCmds::tuner(int argc, const char* const* argv) {
TCLAP::SwitchArg fullArg("","full","Test more possible configurations");

cmd.setShortUsageArgLimit();
cmd.addOverrideConfigArg();

cmd.add(outputFileArg);
cmd.add(gpuIdxsArg);
Expand Down Expand Up @@ -79,12 +83,16 @@ int MainCmds::tuner(int argc, const char* const* argv) {
gpuIdxs.push_back(parsed);
}
}

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

string homeDataDirOverride = Setup::loadHomeDataDirOverride(cfg);

Logger logger;
logger.setLogToStdout(true);

Expand Down Expand Up @@ -123,7 +131,7 @@ int MainCmds::tuner(int argc, const char* const* argv) {
cout << "Tuning device " << gpuIdx << ": " << device->info.name << endl;

if(outputFile == "") {
string dir = OpenCLTuner::defaultDirectory(true);
string dir = OpenCLTuner::defaultDirectory(true,homeDataDirOverride);
outputFile = dir + "/" + OpenCLTuner::defaultFileName(device->info.name, nnXLen, nnYLen, &modelDesc);
}

Expand Down
7 changes: 5 additions & 2 deletions cpp/configs/gtp_example.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
# the command line arguments:
# -override-config KEY=VALUE,KEY=VALUE,...

# Logs------------------------------------------------------------------------------------
# Logs and files--------------------------------------------------------------------------

# Where to output log?
logDir = gtp_logs # Each run of KataGo will log to a separate file in this dir
Expand All @@ -60,6 +60,9 @@ logToStderr = false
# Chat some stuff to stderr, for use in things like malkovich chat to OGS.
# ogsChatToStderr = true

# Optionally override where KataGo will attempt to save things like openCLTuner files and other cached data.
# homeDataDir = DIRECTORY

# Analysis------------------------------------------------------------------------------------

# Configure the maximum length of analysis printed out by lz-analyze and other places.
Expand Down Expand Up @@ -133,7 +136,7 @@ resignConsecTurns = 3
# Defaults to true! Uncomment and set to false to disable this behavior.
# assumeMultipleStartingBlackMovesAreHandicap = true

# Makes katago dynamically adjust in handicap or altered-komi games to assume based on those game settings that it
# Makes katago dynamically adjust in handicap or altered-komi games to assume based on those game settings that it
# must be stronger or weaker than the opponent and to play accordingly. Greatly improves handicap
# strength by biasing winrates and scores to favor appropriate safe/aggressive play.
# Does NOT affect analysis (lz-analyze, kata-analyze, used by programs like Lizzie) so analysis remains unbiased.
Expand Down
16 changes: 13 additions & 3 deletions cpp/dataio/homedata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ string HomeData::getDefaultFilesDirForHelpMessage() {


//On Windows, instead of home directory, we just make something inside the directory containing the executable
string HomeData::getHomeDataDir(bool makeDir) {
string HomeData::getHomeDataDir(bool makeDir, const string& homeDataDirOverride) {
if(homeDataDirOverride != "") {
if(makeDir) MakeDir::make(homeDataDirOverride);
return homeDataDirOverride;
}

//HACK: add 2048 to the buffer size to be resilient to longer paths, beyond MAX_PATH.
constexpr size_t bufSize = MAX_PATH + 2048;
wchar_t buf[bufSize];
Expand Down Expand Up @@ -97,15 +102,20 @@ vector<string> HomeData::getDefaultFilesDirs() {
string exeDir = path.parent_path().string();
ret.push_back(exeDir);
}
ret.push_back(getHomeDataDir(false));
ret.push_back(getHomeDataDir(false,""));
return ret;
}

string HomeData::getDefaultFilesDirForHelpMessage() {
return "(dir containing katago.exe, or else ~/.katago)";
}

string HomeData::getHomeDataDir(bool makeDir) {
string HomeData::getHomeDataDir(bool makeDir, const string& homeDataDirOverride) {
if(homeDataDirOverride != "") {
if(makeDir) MakeDir::make(homeDataDirOverride);
return homeDataDirOverride;
}

string homeDataDir;
const char* home = getenv("HOME");
if(home != NULL) {
Expand Down
3 changes: 2 additions & 1 deletion cpp/dataio/homedata.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ namespace HomeData {
//Returns a directory suitable for writing data that KataGo generates automatically, such as auto-tuning data.
//May throw StringError if filesystem access fails.
//If makeDir is true, will attempt to create the directory if it doesn't exist.
std::string getHomeDataDir(bool makeDir);
//If homeDataDirOverride is nonempty, then just uses homeDataDirOverride.
std::string getHomeDataDir(bool makeDir, const std::string& homeDataDirOverride);
}

#endif //DATAIO_HOMEDATA_H_
4 changes: 3 additions & 1 deletion cpp/neuralnet/cudabackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2619,7 +2619,8 @@ ComputeContext* NeuralNet::createComputeContext(
Logger* logger,
int nnXLen,
int nnYLen,
string openCLTunerFile,
const string& openCLTunerFile,
const string& homeDataDirOverride,
bool openCLReTunePerBoardSize,
enabled_t useFP16Mode,
enabled_t useNHWCMode,
Expand All @@ -2628,6 +2629,7 @@ ComputeContext* NeuralNet::createComputeContext(
(void)gpuIdxs;
(void)logger;
(void)openCLTunerFile;
(void)homeDataDirOverride;
(void)openCLReTunePerBoardSize;
(void)loadedModel;

Expand Down
4 changes: 3 additions & 1 deletion cpp/neuralnet/dummybackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ ComputeContext* NeuralNet::createComputeContext(
Logger* logger,
int nnXLen,
int nnYLen,
string openCLTunerFile,
const string& openCLTunerFile,
const string& homeDataDirOverride,
bool openCLReTunePerBoardSize,
enabled_t useFP16Mode,
enabled_t useNHWCMode,
Expand All @@ -29,6 +30,7 @@ ComputeContext* NeuralNet::createComputeContext(
(void)nnXLen;
(void)nnYLen;
(void)openCLTunerFile;
(void)homeDataDirOverride;
(void)openCLReTunePerBoardSize;
(void)useFP16Mode;
(void)useNHWCMode;
Expand Down
7 changes: 5 additions & 2 deletions cpp/neuralnet/nneval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ NNEvaluator::NNEvaluator(
int nnCacheSizePowerOfTwo,
int nnMutexPoolSizePowerofTwo,
bool skipNeuralNet,
string openCLTunerFile,
const string& openCLTunerFile,
const string& homeDataDirOverride,
bool openCLReTunePerBoardSize,
enabled_t useFP16Mode,
enabled_t useNHWCMode,
Expand Down Expand Up @@ -141,7 +142,9 @@ NNEvaluator::NNEvaluator(
modelVersion = NeuralNet::getModelVersion(loadedModel);
inputsVersion = NNModelVersion::getInputsVersion(modelVersion);
computeContext = NeuralNet::createComputeContext(
gpuIdxs,logger,nnXLen,nnYLen,openCLTunerFile,openCLReTunePerBoardSize,usingFP16Mode,usingNHWCMode,loadedModel
gpuIdxs,logger,nnXLen,nnYLen,
openCLTunerFile,homeDataDirOverride,openCLReTunePerBoardSize,
usingFP16Mode,usingNHWCMode,loadedModel
);
}
else {
Expand Down
3 changes: 2 additions & 1 deletion cpp/neuralnet/nneval.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ class NNEvaluator {
int nnCacheSizePowerOfTwo,
int nnMutexPoolSizePowerofTwo,
bool debugSkipNeuralNet,
std::string openCLTunerFile,
const std::string& openCLTunerFile,
const std::string& homeDataDirOverride,
bool openCLReTunePerBoardSize,
enabled_t useFP16Mode,
enabled_t useNHWCMode,
Expand Down
3 changes: 2 additions & 1 deletion cpp/neuralnet/nninterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ namespace NeuralNet {
Logger* logger,
int nnXLen,
int nnYLen,
std::string openCLTunerFile,
const std::string& openCLTunerFile,
const std::string& homeDataDirOverride,
bool openCLReTunePerBoardSize,
enabled_t useFP16Mode,
enabled_t useNHWCMode,
Expand Down
7 changes: 4 additions & 3 deletions cpp/neuralnet/openclbackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ ComputeContext* NeuralNet::createComputeContext(
Logger* logger,
int nnXLen,
int nnYLen,
string openCLTunerFile,
const string& openCLTunerFile,
const string& homeDataDirOverride,
bool openCLReTunePerBoardSize,
enabled_t useFP16Mode,
enabled_t useNHWCMode,
Expand All @@ -297,9 +298,9 @@ ComputeContext* NeuralNet::createComputeContext(
throw StringError("NeuralNet::createComputeContext - specified no gpus to use");

std::function<OpenCLTuneParams(const string&,int)> getParamsForDeviceName =
[&openCLTunerFile,openCLReTunePerBoardSize,logger,nnXLen,nnYLen,loadedModel](const string& name, int gpuIdxForTuning) {
[&openCLTunerFile,&homeDataDirOverride,openCLReTunePerBoardSize,logger,nnXLen,nnYLen,loadedModel](const string& name, int gpuIdxForTuning) {
bool full = false;
return OpenCLTuner::loadOrAutoTune(openCLTunerFile,name,gpuIdxForTuning,logger,openCLReTunePerBoardSize,nnXLen,nnYLen,&(loadedModel->modelDesc),full);
return OpenCLTuner::loadOrAutoTune(openCLTunerFile,homeDataDirOverride,name,gpuIdxForTuning,logger,openCLReTunePerBoardSize,nnXLen,nnYLen,&(loadedModel->modelDesc),full);
};
return new ComputeContext(gpuIdxs,logger,nnXLen,nnYLen,useFP16Mode,useNHWCMode,getParamsForDeviceName);
}
Expand Down
7 changes: 4 additions & 3 deletions cpp/neuralnet/opencltuner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1509,8 +1509,8 @@ void OpenCLTuner::tune(

}

string OpenCLTuner::defaultDirectory(bool makeDir) {
string dir = HomeData::getHomeDataDir(true);
string OpenCLTuner::defaultDirectory(bool makeDir, const string& homeDataDirOverride) {
string dir = HomeData::getHomeDataDir(true,homeDataDirOverride);
dir += "/opencltuning";
if(makeDir)
MakeDir::make(dir);
Expand Down Expand Up @@ -1542,6 +1542,7 @@ static OpenCLTuneParams loadFromTunerFile(const string& fileName, Logger* logger

OpenCLTuneParams OpenCLTuner::loadOrAutoTune(
string openCLTunerFile,
const string& homeDataDirOverride,
const string& gpuName,
int gpuIdxForTuning,
Logger* logger,
Expand All @@ -1555,7 +1556,7 @@ OpenCLTuneParams OpenCLTuner::loadOrAutoTune(
return loadFromTunerFile(openCLTunerFile,logger);
}

string dir = OpenCLTuner::defaultDirectory(true);
string dir = OpenCLTuner::defaultDirectory(true,homeDataDirOverride);
openCLTunerFile = dir + "/" + OpenCLTuner::defaultFileName(gpuName, nnXLen, nnYLen, model);

//Try loading the config for the proper size
Expand Down
3 changes: 2 additions & 1 deletion cpp/neuralnet/opencltuner.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,12 @@ namespace OpenCLTuner {
std::function<void(const OpenCLTuneParams&)> handleBestSoFar
);

std::string defaultDirectory(bool makeDir);
std::string defaultDirectory(bool makeDir, const std::string& homeDataDirOverride);
std::string defaultFileName(const std::string& gpuName, int nnXLen, int nnYLen, const ModelDesc* model);

OpenCLTuneParams loadOrAutoTune(
std::string openCLTunerFile,
const std::string& homeDataDirOverride,
const std::string& gpuName,
int gpuIdxForTuning,
Logger* logger,
Expand Down
5 changes: 4 additions & 1 deletion cpp/program/gtpconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using namespace std;

static const string gtpBase = R"%%(
# Logs------------------------------------------------------------------------------------
# Logs and files--------------------------------------------------------------------------
# Where to output log?
logDir = gtp_logs # Each run of KataGo will log to a separate file in this dir
Expand All @@ -15,6 +15,9 @@ logAllGTPCommunication = true
logSearchInfo = true
logToStderr = false
# Optionally override where KataGo will attempt to save things like openCLTuner files and other cached data.
# homeDataDir = DIRECTORY
# Analysis------------------------------------------------------------------------------------
# Configure the maximum length of analysis printed out by lz-analyze and other places.
Expand Down
12 changes: 12 additions & 0 deletions cpp/program/setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ vector<NNEvaluator*> Setup::initializeNNEvaluators(
gpuIdxByServerThread.push_back(-1);
}

string homeDataDirOverride = loadHomeDataDirOverride(cfg);

string openCLTunerFile;
if(cfg.contains("openclTunerFile"))
openCLTunerFile = cfg.getString("openclTunerFile");
Expand Down Expand Up @@ -254,6 +256,7 @@ vector<NNEvaluator*> Setup::initializeNNEvaluators(
nnMutexPoolSizePowerOfTwo,
debugSkipNeuralNet,
openCLTunerFile,
homeDataDirOverride,
openCLReTunePerBoardSize,
useFP16Mode,
useNHWCMode,
Expand All @@ -272,6 +275,15 @@ vector<NNEvaluator*> Setup::initializeNNEvaluators(
return nnEvals;
}

string Setup::loadHomeDataDirOverride(
ConfigParser& cfg
){
string homeDataDirOverride;
if(cfg.contains("homeDataDir"))
homeDataDirOverride = cfg.getString("homeDataDir");
return homeDataDirOverride;
}

SearchParams Setup::loadSingleParams(
ConfigParser& cfg
) {
Expand Down
4 changes: 4 additions & 0 deletions cpp/program/setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ namespace Setup {
ConfigParser& cfg
);

std::string loadHomeDataDirOverride(
ConfigParser& cfg
);

//Get sets of options that are mutually exclusive. Intended for use in configParser
std::vector<std::pair<std::set<std::string>,std::set<std::string>>> getMutexKeySets();
}
Expand Down
Loading

0 comments on commit 4a79cde

Please sign in to comment.