-
Notifications
You must be signed in to change notification settings - Fork 687
Expand file tree
/
Copy pathselfplay.cpp
More file actions
391 lines (329 loc) · 14.5 KB
/
selfplay.cpp
File metadata and controls
391 lines (329 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include "../core/global.h"
#include "../core/datetime.h"
#include "../core/fileutils.h"
#include "../core/makedir.h"
#include "../core/config_parser.h"
#include "../dataio/sgf.h"
#include "../dataio/trainingwrite.h"
#include "../dataio/loadmodel.h"
#include "../neuralnet/modelversion.h"
#include "../search/asyncbot.h"
#include "../program/setup.h"
#include "../program/play.h"
#include "../program/selfplaymanager.h"
#include "../command/commandline.h"
#include "../main.h"
#include <chrono>
#include <csignal>
using namespace std;
static std::atomic<bool> sigReceived(false);
static std::atomic<bool> shouldStop(false);
static void signalHandler(int signal)
{
if(signal == SIGINT || signal == SIGTERM) {
sigReceived.store(true);
shouldStop.store(true);
}
}
//-----------------------------------------------------------------------------------------
int MainCmds::selfplay(const vector<string>& args) {
Board::initHash();
ScoreValue::initTables();
Rand seedRand;
ConfigParser cfg;
string modelsDir;
string outputDir;
int64_t maxGamesTotal = ((int64_t)1) << 62;
try {
KataGoCommandLine cmd("Generate training data via self play.");
cmd.addConfigFileArg("","");
cmd.addOverrideConfigArg();
TCLAP::ValueArg<string> modelsDirArg("","models-dir","Dir to poll and load models from",true,string(),"DIR");
TCLAP::ValueArg<string> outputDirArg("","output-dir","Dir to output files",true,string(),"DIR");
TCLAP::ValueArg<string> maxGamesTotalArg("","max-games-total","Terminate after this many games",false,string(),"NGAMES");
cmd.add(modelsDirArg);
cmd.add(outputDirArg);
cmd.add(maxGamesTotalArg);
cmd.parseArgs(args);
modelsDir = modelsDirArg.getValue();
outputDir = outputDirArg.getValue();
string maxGamesTotalStr = maxGamesTotalArg.getValue();
if(maxGamesTotalStr != "") {
bool suc = Global::tryStringToInt64(maxGamesTotalStr,maxGamesTotal);
if(!suc || maxGamesTotal <= 0)
throw StringError("-max-games-total must be a positive integer");
}
auto checkDirNonEmpty = [](const char* flag, const string& s) {
if(s.length() <= 0)
throw StringError("Empty directory specified for " + string(flag));
};
checkDirNonEmpty("models-dir",modelsDir);
checkDirNonEmpty("output-dir",outputDir);
cmd.getConfig(cfg);
}
catch (TCLAP::ArgException &e) {
cerr << "Error: " << e.error() << " for argument " << e.argId() << endl;
return 1;
}
MakeDir::make(outputDir);
MakeDir::make(modelsDir);
Logger logger(&cfg);
//Log to random file name to better support starting/stopping as well as multiple parallel runs
logger.addFile(outputDir + "/log" + DateTime::getCompactDateTimeString() + "-" + Global::uint64ToHexString(seedRand.nextUInt64()) + ".log");
logger.write("Self Play Engine starting...");
logger.write(string("Git revision: ") + Version::getGitRevision());
//Load runner settings
const int numGameThreads = cfg.getInt("numGameThreads",1,16384);
const string gameSeedBase = Global::uint64ToHexString(seedRand.nextUInt64());
//Width and height of the board to use when writing data, typically 19
const int dataBoardLen = cfg.getInt("dataBoardLen",3,Board::MAX_LEN);
const int inputsVersion =
cfg.contains("inputsVersion") ?
cfg.getInt("inputsVersion",0,10000) :
NNModelVersion::getInputsVersion(NNModelVersion::defaultModelVersion);
//Max number of games that we will allow to be queued up and not written out
const int maxDataQueueSize = cfg.getInt("maxDataQueueSize",1,1000000);
const int maxRowsPerTrainFile = cfg.getInt("maxRowsPerTrainFile",1,100000000);
const double firstFileRandMinProp = cfg.getDouble("firstFileRandMinProp",0.0,1.0);
const int64_t logGamesEvery = cfg.getInt64("logGamesEvery",1,1000000);
const bool switchNetsMidGame = cfg.getBool("switchNetsMidGame");
const SearchParams baseParams = Setup::loadSingleParams(cfg,Setup::SETUP_FOR_OTHER);
//Initialize object for randomizing game settings and running games
const bool isDistributed = false;
PlaySettings playSettings = PlaySettings::loadForSelfplay(cfg, isDistributed);
GameRunner* gameRunner = new GameRunner(cfg, playSettings, logger);
bool autoCleanupAllButLatestIfUnused = true;
SelfplayManager* manager = new SelfplayManager(maxDataQueueSize, &logger, logGamesEvery, autoCleanupAllButLatestIfUnused);
const int minBoardXSizeUsed = gameRunner->getGameInitializer()->getMinBoardXSize();
const int minBoardYSizeUsed = gameRunner->getGameInitializer()->getMinBoardYSize();
const int maxBoardXSizeUsed = gameRunner->getGameInitializer()->getMaxBoardXSize();
const int maxBoardYSizeUsed = gameRunner->getGameInitializer()->getMaxBoardYSize();
Setup::initializeSession(cfg);
//Done loading!
//------------------------------------------------------------------------------------
logger.write("Loaded all config stuff, starting self play");
if(!logger.isLoggingToStdout())
cout << "Loaded all config stuff, starting self play" << endl;
if(!std::atomic_is_lock_free(&shouldStop))
throw StringError("shouldStop is not lock free, signal-quitting mechanism for terminating matches will NOT work!");
std::signal(SIGINT, signalHandler);
std::signal(SIGTERM, signalHandler);
//Returns true if a new net was loaded.
auto loadLatestNeuralNetIntoManager =
[inputsVersion,&manager,maxRowsPerTrainFile,firstFileRandMinProp,dataBoardLen,
&modelsDir,&outputDir,&logger,&cfg,numGameThreads,
minBoardXSizeUsed,maxBoardXSizeUsed,minBoardYSizeUsed,maxBoardYSizeUsed](const string* lastNetName) -> bool {
string modelName;
string modelFile;
string modelDir;
time_t modelTime;
bool foundModel = LoadModel::findLatestModel(modelsDir, logger, modelName, modelFile, modelDir, modelTime);
//No new neural nets yet
if(!foundModel || (lastNetName != NULL && *lastNetName == modelName))
return false;
if(modelName == "random" && lastNetName != NULL && *lastNetName != "random") {
logger.write("WARNING: " + *lastNetName + " was the previous model, but now no model was found. Continuing with prev model instead of using random");
return false;
}
logger.write("Found new neural net " + modelName);
const int expectedConcurrentEvals = cfg.getInt("numSearchThreads") * numGameThreads;
const bool defaultRequireExactNNLen = minBoardXSizeUsed == maxBoardXSizeUsed && minBoardYSizeUsed == maxBoardYSizeUsed;
const int defaultMaxBatchSize = -1;
const bool disableFP16 = false;
const string expectedSha256 = "";
Rand rand;
NNEvaluator* nnEval = Setup::initializeNNEvaluator(
modelName,modelFile,expectedSha256,cfg,logger,rand,expectedConcurrentEvals,
maxBoardXSizeUsed,maxBoardYSizeUsed,defaultMaxBatchSize,defaultRequireExactNNLen,disableFP16,
Setup::SETUP_FOR_OTHER
);
logger.write("Loaded latest neural net " + modelName + " from: " + modelFile);
string modelOutputDir = outputDir + "/" + modelName;
string sgfOutputDir = modelOutputDir + "/sgfs";
string tdataOutputDir = modelOutputDir + "/tdata";
//Try repeatedly to make directories, in case the filesystem is unhappy with us as we try to make the same dirs as another process.
//Wait a random amount of time in between each failure.
int maxTries = 5;
for(int i = 0; i<maxTries; i++) {
bool success = false;
try {
MakeDir::make(modelOutputDir);
MakeDir::make(sgfOutputDir);
MakeDir::make(tdataOutputDir);
success = true;
}
catch(const StringError& e) {
logger.write(string("WARNING, error making directories, trying again shortly: ") + e.what());
success = false;
}
if(success)
break;
else {
if(i == maxTries-1) {
logger.write("ERROR: Could not make selfplay model directories, is something wrong with the filesystem?");
//Just give up and wait for the next model.
return false;
}
double sleepTime = 10.0 + rand.nextDouble() * 30.0;
std::this_thread::sleep_for(std::chrono::duration<double>(sleepTime));
continue;
}
}
{
ofstream out;
FileUtils::open(out,modelOutputDir + "/" + "selfplay-" + Global::uint64ToHexString(rand.nextUInt64()) + ".cfg");
out << cfg.getContents();
out.close();
}
//Note that this inputsVersion passed here is NOT necessarily the same as the one used in the neural net self play, it
//simply controls the input feature version for the written data
auto tdataWriter = new TrainingDataWriter(
tdataOutputDir, nullptr, inputsVersion, maxRowsPerTrainFile, firstFileRandMinProp, dataBoardLen, dataBoardLen, Global::uint64ToHexString(rand.nextUInt64()));
ofstream* sgfOut = NULL;
if(sgfOutputDir.length() > 0) {
sgfOut = new ofstream();
FileUtils::open(*sgfOut, sgfOutputDir + "/" + Global::uint64ToHexString(rand.nextUInt64()) + ".sgfs");
}
logger.write("Model loading loop thread loaded new neural net " + nnEval->getModelName());
manager->loadModelAndStartDataWriting(nnEval, tdataWriter, sgfOut);
return true;
};
//Initialize the initial neural net
{
bool success = loadLatestNeuralNetIntoManager(NULL);
if(!success)
throw StringError("Either could not load latest neural net or access/write appopriate directories");
}
//Check for unused config keys
cfg.warnUnusedKeys(cerr,&logger);
//Shared across all game loop threads
std::atomic<int64_t> numGamesStarted(0);
ForkData* forkData = new ForkData();
auto gameLoop = [
&gameRunner,
&manager,
&logger,
switchNetsMidGame,
&numGamesStarted,
&forkData,
maxGamesTotal,
&baseParams,
&gameSeedBase
](int threadIdx) {
auto shouldStopFunc = []() noexcept {
return shouldStop.load();
};
WaitableFlag* shouldPause = nullptr;
string prevModelName;
Rand thisLoopSeedRand;
while(true) {
if(shouldStop.load())
break;
NNEvaluator* nnEval = manager->acquireLatest();
assert(nnEval != NULL);
if(prevModelName != nnEval->getModelName()) {
prevModelName = nnEval->getModelName();
logger.write("Game loop thread " + Global::intToString(threadIdx) + " starting game on new neural net: " + prevModelName);
}
//Callback that runGame will call periodically to ask us if we have a new neural net
std::function<NNEvaluator*()> checkForNewNNEval = [&manager,&nnEval,&prevModelName,&logger,&threadIdx]() -> NNEvaluator* {
NNEvaluator* newNNEval = manager->acquireLatest();
assert(newNNEval != NULL);
if(newNNEval == nnEval) {
manager->release(newNNEval);
return NULL;
}
manager->release(nnEval);
nnEval = newNNEval;
prevModelName = nnEval->getModelName();
logger.write("Game loop thread " + Global::intToString(threadIdx) + " changing midgame to new neural net: " + prevModelName);
return nnEval;
};
FinishedGameData* gameData = NULL;
int64_t gameIdx = numGamesStarted.fetch_add(1,std::memory_order_acq_rel);
if(gameIdx < maxGamesTotal) {
manager->countOneGameStarted(nnEval);
MatchPairer::BotSpec botSpecB;
botSpecB.botIdx = 0;
botSpecB.botName = nnEval->getModelName();
botSpecB.nnEval = nnEval;
botSpecB.baseParams = baseParams;
MatchPairer::BotSpec botSpecW = botSpecB;
string seed = gameSeedBase + ":" + Global::uint64ToHexString(thisLoopSeedRand.nextUInt64());
gameData = gameRunner->runGame(
seed, botSpecB, botSpecW, forkData, NULL, logger,
shouldStopFunc,
shouldPause,
(switchNetsMidGame ? checkForNewNNEval : nullptr),
nullptr,
nullptr
);
}
//NULL gamedata will happen when the game is interrupted by shouldStop, which means we should also stop.
//Or when we run out of total games.
bool shouldContinue = gameData != NULL;
//Note that if we've gotten a newNNEval, we're actually pushing the game as data for the new one, rather than the old one!
if(gameData != NULL)
manager->enqueueDataToWrite(nnEval,gameData);
manager->release(nnEval);
if(!shouldContinue)
break;
}
logger.write("Game loop thread " + Global::intToString(threadIdx) + " terminating");
};
auto gameLoopProtected = [&logger,&gameLoop](int threadIdx) {
Logger::logThreadUncaught("game loop", &logger, [&](){ gameLoop(threadIdx); });
};
//Looping thread for polling for new neural nets and loading them in
std::mutex modelLoadMutex;
std::condition_variable modelLoadSleepVar;
auto modelLoadLoop = [&modelLoadMutex,&modelLoadSleepVar,&logger,&manager,&loadLatestNeuralNetIntoManager]() {
logger.write("Model loading loop thread starting");
while(true) {
if(shouldStop.load())
break;
string lastNetName = manager->getLatestModelName();
bool success = loadLatestNeuralNetIntoManager(&lastNetName);
(void)success;
if(shouldStop.load())
break;
//Sleep for a while and then re-poll
std::unique_lock<std::mutex> lock(modelLoadMutex);
modelLoadSleepVar.wait_for(lock, std::chrono::seconds(20), [](){return shouldStop.load();});
}
logger.write("Model loading loop thread terminating");
};
auto modelLoadLoopProtected = [&logger,&modelLoadLoop]() {
Logger::logThreadUncaught("model load loop", &logger, modelLoadLoop);
};
vector<std::thread> threads;
for(int i = 0; i<numGameThreads; i++) {
threads.push_back(std::thread(gameLoopProtected,i));
}
std::thread modelLoadLoopThread(modelLoadLoopProtected);
//Wait for all game threads to stop
for(int i = 0; i<threads.size(); i++)
threads[i].join();
//If by now somehow shouldStop is not true, set it to be true since all game threads are toast
shouldStop.store(true);
//Wake up the model loading thread rather than waiting for it to wake up on its own, and
//wait for it to die.
{
//Lock so that we don't race where we notify the loading thread to wake when it's still in
//its own critical section but not yet slept, and to ensure the two agree on shouldStop.
std::lock_guard<std::mutex> lock(modelLoadMutex);
modelLoadSleepVar.notify_all();
}
modelLoadLoopThread.join();
//At this point, nothing else except possibly data write loops are running, within the selfplay manager.
delete manager;
//Delete and clean up everything else
NeuralNet::globalCleanup();
delete forkData;
delete gameRunner;
ScoreValue::freeTables();
if(sigReceived.load())
logger.write("Exited cleanly after signal");
logger.write("All cleaned up, quitting");
return 0;
}