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
|
/*
This file is part of Leela Zero.
Copyright (C) 2017-2018 Marco Calignano
Leela Zero is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Leela Zero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Leela Zero. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Validation.h"
#include <QFile>
#include <QDir>
#include <QUuid>
using VersionTuple = std::tuple<int, int, int>;
// Minimal Leela Zero version we expect to see
const VersionTuple min_leelaz_version{0, 16, 0};
void ValidationWorker::run() {
do {
Game first(m_engines[0]);
if (!first.gameStart(min_leelaz_version)) {
emit resultReady(Sprt::NoResult, Game::BLACK);
return;
}
Game second(m_engines[1]);
if (!second.gameStart(min_leelaz_version)) {
emit resultReady(Sprt::NoResult, Game::BLACK);
return;
}
QTextStream(stdout) << "starting:" << endl <<
m_engines[0].getCmdLine() << endl <<
"vs" << endl <<
m_engines[1].getCmdLine() << endl;
QString wmove = "play white ";
QString bmove = "play black ";
do {
first.move();
if (!first.waitForMove()) {
emit resultReady(Sprt::NoResult, Game::BLACK);
return;
}
first.readMove();
if (first.checkGameEnd()) {
break;
}
second.setMove(bmove + first.getMove());
second.move();
if (!second.waitForMove()) {
emit resultReady(Sprt::NoResult, Game::BLACK);
return;
}
second.readMove();
first.setMove(wmove + second.getMove());
second.nextMove();
} while (first.nextMove() && m_state.load() == RUNNING);
if (m_state.load() == RUNNING) {
QTextStream(stdout) << "Game has ended." << endl;
int result = 0;
if (first.getScore()) {
result = first.getWinner();
if (!m_keepPath.isEmpty()) {
first.writeSgf();
QString prefix = m_keepPath + '/';
if (m_expected == Game::BLACK) {
prefix.append("black_");
} else {
prefix.append("white_");
}
QFile(first.getFile() + ".sgf").rename(prefix + first.getFile() + ".sgf");
}
}
QTextStream(stdout) << "Stopping engine." << endl;
first.gameQuit();
second.gameQuit();
// Game is finished, send the result
if (result == m_expected) {
emit resultReady(Sprt::Win, m_expected);
} else {
emit resultReady(Sprt::Loss, m_expected);
}
// Change color and play again
std::swap(m_engines[0], m_engines[1]);
if (m_expected == Game::BLACK) {
m_expected = Game::WHITE;
} else {
m_expected = Game::BLACK;
}
} else {
first.gameQuit();
second.gameQuit();
}
} while (m_state.load() != FINISHING);
}
void ValidationWorker::init(const QString& gpuIndex,
const QVector<Engine>& engines,
const QString& keep,
int expected) {
m_engines = engines;
if (!gpuIndex.isEmpty()) {
m_engines[0].m_options.prepend(" --gpu=" + gpuIndex + " ");
m_engines[1].m_options.prepend(" --gpu=" + gpuIndex + " ");
}
m_expected = expected;
m_keepPath = keep;
m_state.store(RUNNING);
}
Validation::Validation(const int gpus,
const int games,
const QStringList& gpuslist,
QVector<Engine>& engines,
const QString& keep,
QMutex* mutex,
const float& h0,
const float& h1) :
m_mainMutex(mutex),
m_syncMutex(),
m_gamesThreads(gpus*games),
m_games(games),
m_gpus(gpus),
m_gpusList(gpuslist),
m_engines(engines),
m_keepPath(keep) {
m_statistic.initialize(h0, h1, 0.05, 0.05);
m_statistic.addGameResult(Sprt::Draw);
}
void Validation::startGames() {
for (int gpu = 0; gpu < m_gpus; ++gpu) {
for (int game = 0; game < m_games; ++game) {
auto thread_index = gpu * m_games + game;
connect(&m_gamesThreads[thread_index],
&ValidationWorker::resultReady,
this,
&Validation::getResult,
Qt::DirectConnection);
auto engines = m_engines;
auto expected = Game::BLACK;
if (game & 1) {
std::swap(engines[0], engines[1]);
expected = Game::WHITE;
}
auto myGpu = QString("");
if (!m_gpusList.isEmpty()) {
myGpu = m_gpusList.at(gpu);
}
m_gamesThreads[thread_index].init(
myGpu, engines, m_keepPath, expected);
m_gamesThreads[thread_index].start();
}
}
}
void Validation::saveSprt() {
QFile f("sprtsave" + QUuid::createUuid().toRfc4122().toHex() + ".bin");
if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) {
return;
}
QTextStream out(&f);
out << m_statistic;
out << m_results;
f.close();
m_results.printResults(m_engines[0].m_network, m_engines[1].m_network);
printSprtStatus(m_statistic.status());
}
void Validation::loadSprt() {
QDir dir;
QStringList filters;
filters << "sprtsave*.bin";
dir.setNameFilters(filters);
dir.setFilter(QDir::Files | QDir::NoSymLinks);
if (dir.entryInfoList().isEmpty()) {
return;
}
QFileInfo fi = dir.entryInfoList().takeFirst();
QFile f(fi.fileName());
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
return;
}
QTextStream in(&f);
in >> m_statistic;
in >> m_results;
f.close();
QFile::remove(fi.fileName());
QTextStream(stdout) << "Initial Statistics" << endl;
m_results.printResults(m_engines[0].m_network, m_engines[1].m_network);
printSprtStatus(m_statistic.status());
}
void Validation::printSprtStatus(const Sprt::Status& status) {
QTextStream(stdout)
<< m_results.getGamesPlayed() << " games played." << endl;
QTextStream(stdout)
<< "Status: " << status.result
<< " LLR " << status.llr
<< " Lower Bound " << status.lBound
<< " Upper Bound " << status.uBound << endl;
}
void Validation::getResult(Sprt::GameResult result, int net_one_color) {
if (result == Sprt::NoResult) {
QTextStream(stdout) << "Engine Error." << endl;
return;
}
m_syncMutex.lock();
m_statistic.addGameResult(result);
m_results.addGameResult(result, net_one_color);
Sprt::Status status = m_statistic.status();
auto wdl = m_statistic.getWDL();
QTextStream(stdout) << std::get<0>(wdl) << " wins, "
<< std::get<2>(wdl) << " losses" << endl;
if (status.result != Sprt::Continue) {
quitThreads();
QTextStream(stdout)
<< "The first net is "
<< ((status.result == Sprt::AcceptH0) ? "worse " : "better ")
<< "than the second" << endl;
m_results.printResults(m_engines[0].m_network, m_engines[1].m_network);
//sendQuit();
} else {
printSprtStatus(status);
}
m_syncMutex.unlock();
}
void Validation::quitThreads() {
for (int gpu = 0; gpu < m_gpus * m_games; ++gpu) {
m_gamesThreads[gpu].doFinish();
}
}
void Validation::wait() {
for (int gpu = 0; gpu < m_gpus * m_games; ++gpu) {
m_gamesThreads[gpu].wait();
}
}
void Validation::storeSprt() {
QTextStream(stdout) << "storeSprt" << endl;
m_syncMutex.lock();
saveSprt();
m_syncMutex.unlock();
quitThreads();
wait();
}
|