forked from tensorflow/minigo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.cc
More file actions
302 lines (261 loc) · 10.4 KB
/
Copy patheval.cc
File metadata and controls
302 lines (261 loc) · 10.4 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
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include <cmath>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <utility>
#include <vector>
#include "absl/memory/memory.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_split.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "cc/constants.h"
#include "cc/dual_net/batching_dual_net.h"
#include "cc/dual_net/factory.h"
#include "cc/file/path.h"
#include "cc/file/utils.h"
#include "cc/game.h"
#include "cc/game_utils.h"
#include "cc/init.h"
#include "cc/logging.h"
#include "cc/mcts_player.h"
#include "cc/random.h"
#include "cc/tf_utils.h"
#include "cc/zobrist.h"
#include "gflags/gflags.h"
// Game options flags.
DEFINE_double(resign_threshold, -0.999, "Resign threshold.");
DEFINE_uint64(seed, 0,
"Random seed. Use default value of 0 to use a time-based seed.");
// Tree search flags.
DEFINE_int32(num_readouts, 100,
"Number of readouts to make during tree search for each move.");
DEFINE_int32(virtual_losses, 8,
"Number of virtual losses when running tree search.");
DEFINE_double(value_init_penalty, 2.0,
"New children value initialization penalty.\n"
"Child value = parent's value - penalty * color, clamped to"
" [-1, 1]. Penalty should be in [0.0, 2.0].\n"
"0 is init-to-parent, 2.0 is init-to-loss [default].\n"
"This behaves similiarly to Leela's FPU \"First Play Urgency\".");
// Inference flags.
DEFINE_string(model, "",
"Path to a minigo model. The format of the model depends on the "
"inference engine. If parallel_games=1, this model is used for "
"black. For engine=tf, the model should be a GraphDef proto. For "
"engine=lite, the model should be .tflite flatbuffer.");
DEFINE_string(model_two, "", "Descriptor for the second model");
DEFINE_int32(parallel_games, 32, "Number of games to play in parallel.");
// Output flags.
DEFINE_string(output_bigtable, "",
"Output Bigtable specification, of the form: "
"project,instance,table. "
"If empty, no examples are written to Bigtable.");
DEFINE_string(sgf_dir, "",
"SGF directory for selfplay and puzzles. If empty in selfplay "
"mode, no SGF is written.");
DEFINE_string(bigtable_tag, "", "Used in Bigtable metadata");
namespace minigo {
namespace {
void ParseOptionsFromFlags(Game::Options* game_options,
MctsPlayer::Options* player_options) {
game_options->resign_threshold = -std::abs(FLAGS_resign_threshold);
game_options->ignore_repeated_moves = true;
player_options->virtual_losses = FLAGS_virtual_losses;
player_options->random_seed = FLAGS_seed;
player_options->num_readouts = FLAGS_num_readouts;
player_options->inject_noise = false;
player_options->soft_pick = false;
player_options->random_symmetry = true;
}
class Evaluator {
class Model {
public:
Model(BatchingDualNetFactory* batcher, const std::string& path)
: batcher_(batcher), path_(path) {}
BatchingDualNetFactory* batcher() { return batcher_; }
std::string name() {
absl::MutexLock lock(&mutex_);
if (name_.empty()) {
// The model's name is lazily initialized the first time we create a
// instance. Make sure it's valid.
NewDualNetImpl();
}
return name_;
}
WinStats GetWinStats() const {
absl::MutexLock lock(&mutex_);
return win_stats_;
}
void UpdateWinStats(const Game& game) {
absl::MutexLock lock(&mutex_);
win_stats_.Update(game);
}
std::unique_ptr<DualNet> NewDualNet() {
absl::MutexLock lock(&mutex_);
return NewDualNetImpl();
}
private:
std::unique_ptr<DualNet> NewDualNetImpl()
EXCLUSIVE_LOCKS_REQUIRED(&mutex_) {
auto dual_net = batcher_->NewDualNet(path_);
if (name_.empty()) {
name_ = dual_net->name();
}
return dual_net;
}
mutable absl::Mutex mutex_;
BatchingDualNetFactory* batcher_ GUARDED_BY(&mutex_);
const std::string path_;
std::string name_ GUARDED_BY(&mutex_);
WinStats win_stats_ GUARDED_BY(&mutex_);
};
public:
Evaluator(ModelDescriptor desc_a, ModelDescriptor desc_b)
: desc_a_(std::move(desc_a)), desc_b_(std::move(desc_b)) {
// Create a batcher for the first model.
batchers_.push_back(absl::make_unique<BatchingDualNetFactory>(
NewDualNetFactory(desc_a_.engine)));
// If the second model requires a different factory, create one & a second
// batcher too.
if (desc_b_.engine != desc_a_.engine) {
batchers_.push_back(absl::make_unique<BatchingDualNetFactory>(
NewDualNetFactory(desc_b_.engine)));
}
}
void Run() {
auto start_time = absl::Now();
Model model_a(batchers_.front().get(), desc_a_.model);
Model model_b(batchers_.back().get(), desc_b_.model);
MG_LOG(INFO) << "DualNet factories created from " << desc_a_ << "\n and "
<< desc_b_ << " in "
<< absl::ToDoubleSeconds(absl::Now() - start_time) << " sec.";
ParseOptionsFromFlags(&game_options_, &player_options_);
int num_games = FLAGS_parallel_games;
for (int thread_id = 0; thread_id < num_games; ++thread_id) {
bool swap_models = (thread_id & 1) != 0;
threads_.emplace_back(std::bind(&Evaluator::ThreadRun, this, thread_id,
swap_models ? &model_b : &model_a,
swap_models ? &model_a : &model_b));
}
for (auto& t : threads_) {
t.join();
}
MG_LOG(INFO) << "Evaluated " << num_games << " games, total time "
<< (absl::Now() - start_time);
MG_LOG(INFO) << FormatWinStatsTable(
{{model_a.name(), model_a.GetWinStats()},
{model_b.name(), model_b.GetWinStats()}});
}
private:
void ThreadRun(int thread_id, Model* black_model, Model* white_model) {
// Only print the board using ANSI colors if stderr is sent to the
// terminal.
const bool use_ansi_colors = FdSupportsAnsiColors(fileno(stderr));
// The player and other_player reference this pointer.
std::unique_ptr<DualNet> dual_net;
std::vector<std::string> bigtable_spec =
absl::StrSplit(FLAGS_output_bigtable, ',');
bool use_bigtable = bigtable_spec.size() == 3;
if (!FLAGS_output_bigtable.empty() && !use_bigtable) {
MG_LOG(FATAL)
<< "Bigtable output must be of the form: project,instance,table";
return;
}
Game game(black_model->name(), white_model->name(), game_options_);
auto player_options = player_options_;
// If an random seed was explicitly specified, make sure we use a
// different seed for each thread.
if (player_options.random_seed != 0) {
player_options.random_seed += 1299283 * thread_id;
}
const bool verbose = thread_id == 0;
auto black = absl::make_unique<MctsPlayer>(black_model->NewDualNet(),
nullptr, &game, player_options);
auto white = absl::make_unique<MctsPlayer>(white_model->NewDualNet(),
nullptr, &game, player_options);
BatchingDualNetFactory::StartGame(black->network(), white->network());
auto* curr_player = black.get();
auto* next_player = white.get();
while (!game.game_over() && !curr_player->root()->at_move_limit()) {
auto move = curr_player->SuggestMove();
if (verbose) {
std::cerr << curr_player->root()->Describe() << "\n";
}
curr_player->PlayMove(move);
if (!game.game_over()) {
next_player->PlayMove(move);
}
if (verbose) {
MG_LOG(INFO) << absl::StreamFormat(
"%d: %s by %s\nQ: %0.4f", curr_player->root()->position.n(),
move.ToGtp(), curr_player->name(), curr_player->root()->Q());
MG_LOG(INFO) << curr_player->root()->position.ToPrettyString(
use_ansi_colors);
}
std::swap(curr_player, next_player);
}
BatchingDualNetFactory::EndGame(black->network(), white->network());
if (game.result() > 0) {
black_model->UpdateWinStats(game);
} else {
white_model->UpdateWinStats(game);
}
if (verbose) {
MG_LOG(INFO) << game.result_string();
MG_LOG(INFO) << "Black was: " << game.black_name();
}
// Write SGF.
std::string output_name = "NO_SGF_SAVED";
if (!FLAGS_sgf_dir.empty()) {
output_name = absl::StrCat(GetOutputName(absl::Now(), thread_id), "-",
black->name(), "-", white->name());
game.AddComment(
absl::StrCat("B inferences: ", black->GetModelsUsedForInference()));
game.AddComment(
absl::StrCat("W inferences: ", white->GetModelsUsedForInference()));
WriteSgf(FLAGS_sgf_dir, output_name, game, true);
}
if (use_bigtable) {
const auto& gcp_project_name = bigtable_spec[0];
const auto& instance_name = bigtable_spec[1];
const auto& table_name = bigtable_spec[2];
tf_utils::WriteEvalRecord(gcp_project_name, instance_name, table_name,
game, output_name, FLAGS_bigtable_tag);
}
MG_LOG(INFO) << "Thread " << thread_id << " stopping";
}
Game::Options game_options_;
MctsPlayer::Options player_options_;
std::vector<std::thread> threads_;
const ModelDescriptor desc_a_;
const ModelDescriptor desc_b_;
std::vector<std::unique_ptr<BatchingDualNetFactory>> batchers_;
};
} // namespace
} // namespace minigo
int main(int argc, char* argv[]) {
minigo::Init(&argc, &argv);
minigo::zobrist::Init(FLAGS_seed * 614944751);
minigo::Evaluator evaluator(minigo::ParseModelDescriptor(FLAGS_model),
minigo::ParseModelDescriptor(FLAGS_model_two));
evaluator.Run();
return 0;
}