-
Notifications
You must be signed in to change notification settings - Fork 16
/
MCPolicy.cpp
565 lines (492 loc) · 18.9 KB
/
MCPolicy.cpp
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#include "config.h"
#include <memory>
#include <cmath>
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <string.h>
#include <map>
#include <unordered_map>
#if defined(_OPENMP)
#include <omp.h>
#endif
#include "GTP.h"
#include "MCPolicy.h"
#include "SGFParser.h"
#include "SGFTree.h"
#include "Utils.h"
#include "Random.h"
#include "Network.h"
#include "Playout.h"
using namespace Utils;
#include "PolicyWeights.h"
#include "PolicyWeightsSL.h"
#include "PatternsLive.h"
alignas(64) std::array<float, NUM_PATTERNS> PolicyWeights::pattern_gradients;
alignas(64) std::array<float, NUM_FEATURES> PolicyWeights::feature_gradients;
// Filled by Matcher.cpp from PolicyWeights::live_patterns
// alignas(64) const std::array<float, NUM_PATTERNS> PolicyWeights::pattern_weights_sl;
// Adam
alignas(64) std::array<std::pair<float, float>, NUM_PATTERNS> pattern_adam{};
alignas(64) std::array<std::pair<float, float>, NUM_FEATURES> feature_adam{};
int t{0};
void MCPolicy::mse_from_file(std::string filename) {
std::vector<std::string> games = SGFParser::chop_all(filename);
size_t gametotal = games.size();
myprintf("Total games in file: %d\n", gametotal);
#if defined(_OPENMP)
omp_set_num_threads(cfg_num_threads);
#endif
double sum_sq_pp = 0.0;
//double sum_sq_nn = 0.0;
int count = 0;
PolicyWeights::feature_weights.fill(1.0f);
PolicyWeights::pattern_weights.fill(1.0f);
Time start;
while (1) {
size_t pick = Random::get_Rng()->randuint32(gametotal);
std::unique_ptr<SGFTree> sgftree(new SGFTree);
try {
sgftree->load_from_string(games[pick]);
} catch (...) {
};
int who_won = sgftree->get_winner();
int handicap = sgftree->get_state()->get_handicap();
int movecount = sgftree->count_mainline_moves();
int move_pick = Random::get_Rng()->randuint16(movecount);
// GameState state = sgftree->follow_mainline_state(move_pick);
KoState * state = sgftree->get_state_from_mainline(move_pick);
if (who_won != FastBoard::BLACK && who_won != FastBoard::WHITE) {
continue;
}
bool blackwon = (who_won == FastBoard::BLACK);
constexpr int iterations = 512;
PolicyWeights::feature_gradients.fill(0.0f);
PolicyWeights::pattern_gradients.fill(0.0f);
float bwins = 0.0f;
//float nwscore;
float black_score = blackwon ? 1.0f : 0.0f;
#pragma omp parallel
{
#if 0
#pragma omp single nowait
{
FastState workstate = *state;
nwscore = Network::get_Network()->get_value(
&workstate, Network::Ensemble::AVERAGE_ALL);
if (workstate.get_to_move() == FastBoard::WHITE) {
nwscore = 1.0f - nwscore;
}
black_score = ((blackwon ? 1.0f : 0.0f) + nwscore) / 2.0f;
}
#endif
#if 1
// Get EV (V)
#pragma omp for reduction(+:bwins) schedule(dynamic, 8)
for (int i = 0; i < iterations; i++) {
FastState tmp = *state;
Playout p;
p.run(tmp, false, true, nullptr);
float score = p.get_score();
if (score > 0.0f) {
bwins += 1.0f / (float)iterations;
}
}
#endif
#if 1
// Policy Trace per thread
#pragma omp for schedule(dynamic, 4)
for (int i = 0; i < iterations; i++) {
FastState tmp = *state;
PolicyTrace policy_trace;
Playout p;
p.run(tmp, false, true, &policy_trace);
bool black_won = p.get_score() > 0.0f;
policy_trace.trace_process(iterations, bwins, black_won);
}
#endif
}
MCPolicy::adjust_weights(black_score, bwins);
//MCPolicy::adjust_weights(1.0f, 0.0f);
// myprintf("n=%d BW: %d Score: %1.4f NN: %1.4f ",
// count, blackwon, bwins, nwscore);
sum_sq_pp += std::pow((blackwon ? 1.0f : 0.0f) - bwins, 2.0f);
// sum_sq_nn += std::pow((blackwon ? 1.0f : 0.0f) - nwscore, 2.0f);
count++;
if (count % 10000 == 0) {
Time end;
float timediff = Time::timediff(start, end) / 100.0f;
float ips = 10000.0f / timediff;
start = end;
myprintf("n=%d MSE MC=%1.4f ips=%f\n",
count,
sum_sq_pp/10000.0,
// sum_sq_nn/10000.0,
ips);
sum_sq_pp = 0.0;
// sum_sq_nn = 0.0;
}
if (count % 10000 == 0) {
std::string filename = "rltune_" + std::to_string(count) + ".txt";
std::ofstream out(filename);
out.precision(std::numeric_limits<float>::max_digits10);
out << std::defaultfloat;
for (int w = 0; w < NUM_FEATURES; w++) {
out << PolicyWeights::feature_weights[w]
<< "f, /* = " << w << " */"
<< std::endl;
}
out << std::endl;
out << std::scientific;
//std::map<int, float> active_pats;
for (size_t i = 0; i < PolicyWeights::pattern_weights.size(); i++) {
//if (PolicyWeights::pattern_weights[i] != 1.0f) {
// active_pats.emplace(i, PolicyWeights::pattern_weights[i]);
//}
//}
//for (auto & pat : active_pats) {
out << PolicyWeights::pattern_weights[i] << "f," << std::endl;
}
out.close();
}
}
}
void MCPolicy::mse_from_file2(std::string filename) {
std::vector<std::string> games = SGFParser::chop_all(filename);
size_t gametotal = games.size();
myprintf("Total games in file: %d\n", gametotal);
#if defined(_OPENMP)
omp_set_num_threads(cfg_num_threads);
#endif
int count = 0;
int picks = 0;
int correct = 0;
PolicyWeights::feature_weights.fill(1.0f);
PolicyWeights::pattern_weights.fill(1.0f);
PolicyWeights::feature_gradients.fill(0.0f);
PolicyWeights::pattern_gradients.fill(0.0f);
Time start;
for (;;) {
#pragma omp parallel for
for (int gameid = 0; gameid < 128; gameid++) {
std::unique_ptr<SGFTree> sgftree(new SGFTree);
try {
sgftree->load_from_string(games[Random::get_Rng()->randuint32(gametotal)]);
} catch (...) {
#pragma omp atomic
count++;
continue;
};
SGFTree * treewalk = &(*sgftree);
size_t counter = 0;
size_t movecount = sgftree->count_mainline_moves();
std::vector<int> tree_moves = sgftree->get_mainline();
PolicyTrace policy_trace;
while (counter < movecount) {
KoState * state = treewalk->get_state();
int tomove = state->get_to_move();
int move;
if (treewalk->get_child(0) != NULL) {
move = treewalk->get_child(0)->get_move(tomove);
if (move == SGFTree::EOT) {
break;
}
} else {
break;
}
assert(move == tree_moves[counter]);
state->generate_trace(tomove, policy_trace, move);
counter++;
treewalk = treewalk->get_child(0);
}
policy_trace.accumulate_sl_gradient(correct, picks);
#pragma omp atomic
count++;
}
MCPolicy::adjust_weights(1.0f, 0.0f);
PolicyWeights::feature_gradients.fill(0.0f);
PolicyWeights::pattern_gradients.fill(0.0f);
std::cout << ".";
if ((count % (128*128)) == 0) {
std::cout << std::endl << "Pred: " << correct*100.0/picks;
picks = 0;
correct = 0;
std::cout << "w";
std::string filename = "sltune_" + std::to_string(count) + ".txt";
std::ofstream out(filename);
out.precision(std::numeric_limits<float>::max_digits10);
out << std::defaultfloat;
for (int w = 0; w < NUM_FEATURES; w++) {
out << PolicyWeights::feature_weights[w]
<< "f, /* = " << w << " */"
<< std::endl;
}
out << std::endl;
out << std::scientific;
for (int w = 0; w < NUM_PATTERNS; w++) {
out << PolicyWeights::pattern_weights[w] << "f," << std::endl;
}
out.close();
}
}
}
void PolicyTrace::accumulate_sl_gradient(int & correct, int & picks) {
if (trace.empty()) return;
float positions = trace.size();
assert(positions > 0.0f);
float factor = 1.0f / positions;
alignas(64) std::array<float, NUM_FEATURES> policy_feature_gradient{};
std::vector<float> candidate_scores;
std::vector<int> patterns;
for (auto & decision : trace) {
candidate_scores.clear();
candidate_scores.reserve(decision.candidates.size());
// get real probabilities
float sum_scores = 0.0f;
float max_score = 0.0f;
int max_move = -1;
for (auto & mwf : decision.candidates) {
float score = mwf.get_score();
sum_scores += score;
if (score > max_score) {
max_score = score;
max_move = mwf.get_sq();
}
assert(!std::isnan(score));
candidate_scores.push_back(score);
}
assert(sum_scores > 0.0f);
assert(!std::isnan(sum_scores));
if (max_move == decision.pick.get_sq()) {
#pragma omp atomic
correct++;
}
#pragma omp atomic
picks++;
// transform to probabilities
for (float & score : candidate_scores) {
score /= sum_scores;
}
// loop over features, get prob of feature
alignas(64) std::array<float, NUM_FEATURES> feature_probabilities{};
for (size_t c = 0; c < candidate_scores.size(); c++) {
float candidate_probability = candidate_scores[c];
MovewFeatures & mwf = decision.candidates[c];
for (int i = 0; i < NUM_FEATURES; i++) {
if (mwf.has_flag(i)) {
feature_probabilities[i] += candidate_probability;
}
}
}
// get policy gradient
for (int i = 0; i < NUM_FEATURES; i++) {
float observed = 0.0f;
if (decision.pick.has_flag(i)) {
observed = 1.0f;
}
policy_feature_gradient[i] += (observed - feature_probabilities[i]);
assert(!std::isnan(policy_feature_gradient[i]));
}
patterns.clear();
// get pat probabilities
std::array<float, NUM_PATTERNS> pattern_probabilities;
for (size_t c = 0; c < candidate_scores.size(); c++) {
if (!decision.candidates[c].is_pass()) {
int pat = decision.candidates[c].get_pattern();
if (std::find(patterns.cbegin(), patterns.cend(), pat)
!= patterns.cend()) {
pattern_probabilities[pat] += candidate_scores[c];
} else {
patterns.push_back(pat);
pattern_probabilities[pat] = candidate_scores[c];
}
}
}
for (int pat : patterns) {
float observed = 0.0f;
if (!decision.pick.is_pass()) {
if (decision.pick.get_pattern() == pat) {
observed = 1.0f;
}
}
float grad = factor * (observed - pattern_probabilities[pat]);
#pragma omp atomic
PolicyWeights::pattern_gradients[pat] += grad;
}
}
for (int i = 0; i < NUM_FEATURES; i++) {
float grad = policy_feature_gradient[i] * factor;
// accumulate total
#pragma omp atomic
PolicyWeights::feature_gradients[i] += grad;
}
}
void PolicyTrace::trace_process(const int iterations, const float baseline,
const bool blackwon) {
#if 1
float z = 1.0f;
if (!blackwon) {
z = 0.0f;
}
float sign = z - baseline;
#endif
if (trace.empty()) return;
float positions = trace.size();
float iters = iterations;
assert(positions > 0.0f && iters > 0.0f);
// scale by N*T
float factor = 1.0f / (positions * iters);
alignas(64) std::array<float, NUM_FEATURES> policy_feature_gradient{};
//alignas(64) std::array<float, NUM_PATTERNS> policy_pattern_gradient{};
std::vector<float> candidate_scores;
std::vector<int> patterns;
for (auto & decision : trace) {
#if 0
float z = 0.0f;
// Side to move won
if (decision.black_to_move == blackwon) {
z = 1.0f;
}
// Baseline for side to move
float adj_baseline = baseline;
if (!decision.black_to_move) {
adj_baseline = 1.0f - baseline;
}
float sign = z - adj_baseline;
#endif
candidate_scores.clear();
candidate_scores.reserve(decision.candidates.size());
// get real probabilities
float sum_scores = 0.0f;
for (auto & mwf : decision.candidates) {
float score = mwf.get_score();
sum_scores += score;
assert(!std::isnan(score));
candidate_scores.push_back(score);
}
assert(sum_scores > 0.0f);
assert(!std::isnan(sum_scores));
// transform to probabilities
for (float & score : candidate_scores) {
score /= sum_scores;
}
// loop over features, get prob of feature
alignas(64) std::array<float, NUM_FEATURES> feature_probabilities{};
for (size_t c = 0; c < candidate_scores.size(); c++) {
float candidate_probability = candidate_scores[c];
MovewFeatures & mwf = decision.candidates[c];
for (int i = 0; i < NUM_FEATURES; i++) {
if (mwf.has_flag(i)) {
feature_probabilities[i] += candidate_probability;
}
}
}
// get policy gradient
for (int i = 0; i < NUM_FEATURES; i++) {
float observed = 0.0f;
if (decision.pick.has_flag(i)) {
observed = 1.0f;
}
policy_feature_gradient[i] += sign *
(observed - feature_probabilities[i]);
assert(!std::isnan(policy_feature_gradient[i]));
}
patterns.clear();
// get pat probabilities
std::array<float, NUM_PATTERNS> pattern_probabilities;
//std::unordered_map<int, float> pattern_probabilities;
for (size_t c = 0; c < candidate_scores.size(); c++) {
if (!decision.candidates[c].is_pass()) {
int pat = decision.candidates[c].get_pattern();
if (std::find(patterns.cbegin(), patterns.cend(), pat)
!= patterns.cend()) {
pattern_probabilities[pat] += candidate_scores[c];
} else {
patterns.push_back(pat);
pattern_probabilities[pat] = candidate_scores[c];
}
}
}
for (int pat : patterns) {
float observed = 0.0f;
if (!decision.pick.is_pass()) {
if (decision.pick.get_pattern() == pat) {
observed = 1.0f;
}
}
float grad = factor * sign *
(observed - pattern_probabilities[pat]);
#pragma omp atomic
PolicyWeights::pattern_gradients[pat] += grad;
}
}
for (int i = 0; i < NUM_FEATURES; i++) {
float grad = policy_feature_gradient[i] * factor;
// accumulate total
#pragma omp atomic
PolicyWeights::feature_gradients[i] += grad;
}
}
void MCPolicy::adjust_weights(float black_eval, float black_winrate) {
constexpr float alpha = 0.002f;
constexpr float beta_1 = 0.9f;
constexpr float beta_2 = 0.999f;
constexpr float delta = 1e-8f;
constexpr float lambda = 2e-7;
// Timestep for Adam (total updates)
t++;
float Vdelta = black_eval - black_winrate;
for (int i = 0; i < NUM_FEATURES; i++) {
float orig_weight = PolicyWeights::feature_weights[i];
float gradient = PolicyWeights::feature_gradients[i];
feature_adam[i].first = beta_1 * feature_adam[i].first
+ (1.0f - beta_1) * gradient;
feature_adam[i].second = beta_2 * feature_adam[i].second
+ (1.0f - beta_2) * gradient * gradient;
float bc_m1 = feature_adam[i].first / (1.0f - (float)std::pow(beta_1, (double)t));
float bc_m2 = feature_adam[i].second / (1.0f - (float)std::pow(beta_2, (double)t));
float bc_m1_nag = beta_1 * bc_m1
+ gradient * (1.0f - beta_1) / (1.0f - (float)std::pow(beta_1, (double)t));
float adam_grad = alpha * bc_m1_nag / (std::sqrt(bc_m2) + delta);
// Convert to theta
float theta = std::log(orig_weight);
theta -= std::abs(Vdelta) * theta * lambda;
theta += Vdelta * adam_grad;
float gamma = std::exp(theta);
gamma = std::max(gamma, 1e-12f);
gamma = std::min(gamma, 1e12f);
assert(!std::isnan(gamma));
PolicyWeights::feature_weights[i] = gamma;
}
// Give the vectorizer a chance
alignas(64) std::array<float, NUM_PATTERNS> adam_grad;
//std::unique_ptr<float[]> adam_grad(new float[NUM_PATTERNS]);
#pragma omp parallel for
for (int i = 0; i < NUM_PATTERNS; i++) {
float gradient = PolicyWeights::pattern_gradients[i];
pattern_adam[i].first = beta_1 * pattern_adam[i].first
+ (1.0f - beta_1) * gradient;
pattern_adam[i].second = beta_2 * pattern_adam[i].second
+ (1.0f - beta_2) * gradient * gradient;
float bc_m1 = pattern_adam[i].first / (1.0f - (float)std::pow(beta_1, (double)t));
float bc_m2 = pattern_adam[i].second / (1.0f - (float)std::pow(beta_2, (double)t));
float bc_m1_nag = beta_1 * bc_m1
+ gradient * (1.0f - beta_1) / (1.0f - (float)std::pow(beta_1, (double)t));
adam_grad[i] = alpha * bc_m1_nag / (std::sqrt(bc_m2) + delta);
}
#pragma omp parallel for
for (int i = 0; i < NUM_PATTERNS; i++) {
float orig_weight = PolicyWeights::pattern_weights[i];
// Convert to theta
float theta = std::log(orig_weight);
theta -= std::abs(Vdelta) * theta * lambda;
theta += Vdelta * adam_grad[i];
float gamma = std::exp(theta);
gamma = std::max(gamma, 1e-12f);
gamma = std::min(gamma, 1e12f);
assert(!std::isnan(gamma));
PolicyWeights::pattern_weights[i] = gamma;
}
}