-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathwrite.cpp
More file actions
1412 lines (1260 loc) · 50.2 KB
/
Copy pathwrite.cpp
File metadata and controls
1412 lines (1260 loc) · 50.2 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
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "core/global.h"
#include "core/rand.h"
#include "game/board.h"
#include "neuralnet/nninputs.h"
#include "dataio/sgf.h"
#include "dataio/lzparse.h"
#include "dataio/datapool.h"
#include "program/gitinfo.h"
#include <fstream>
#include <algorithm>
#include <H5Cpp.h>
using namespace H5;
#define TCLAP_NAMESTARTSTRING "-" //Use single dashes for all flags
#include <tclap/CmdLine.h>
//Data and feature row parameters
static const int maxBoardSize = NNPos::MAX_BOARD_LEN;
static const int numFeatures = NNInputs::NUM_FEATURES_V2;
//Different segments of the data row
static const int inputStart = 0;
static const int inputLen = maxBoardSize * maxBoardSize * numFeatures;
static const int policyTargetStart = inputStart + inputLen;
static const int policyTargetLen = maxBoardSize * maxBoardSize + 1; //+1 for pass move
static const int ladderTargetStart = policyTargetStart + policyTargetLen;
static const int ladderTargetLen = 0;
// static const int ladderTargetLen = maxBoardSize * maxBoardSize;
static const int valueTargetStart = ladderTargetStart + ladderTargetLen;
static const int valueTargetLen = 1;
static const int targetWeightsStart = valueTargetStart + valueTargetLen;
static const int targetWeightsLen = 1;
static const int rankStart = targetWeightsStart + targetWeightsLen;
static const int rankLenGoGoD = 1; //pro
static const int rankLenKGS = 9; //1d-9d
static const int rankLenFox = 17 + 9; //17k-9d
static const int rankLenOGSPre2014 = 19 + 9; //19k-9d
static const int rankStartGoGoD = 0;
static const int rankStartKGS = rankLenGoGoD;
static const int rankStartFox = rankLenGoGoD + rankLenKGS;
static const int rankStartOGSPre2014 = rankLenGoGoD + rankLenKGS + rankLenFox;
static const int rankLen = rankLenGoGoD + rankLenKGS + rankLenFox + rankLenOGSPre2014;
static const int sideStart = rankStart + rankLen;
static const int sideLen = 1;
static const int turnNumberStart = sideStart + sideLen;
static const int turnNumberLen = 2;
static const int recentCapturesStart = turnNumberStart + turnNumberLen;
static const int recentCapturesLen = maxBoardSize * maxBoardSize;
static const int nextMovesStart = recentCapturesStart + recentCapturesLen;
static const int nextMovesLen = 12;
static const int sgfHashStart = nextMovesStart + nextMovesLen;
static const int sgfHashLen = 8;
static const int includeHistoryStart = sgfHashStart + sgfHashLen;
static const int includeHistoryLen = 5;
static const int totalRowLen = includeHistoryStart + includeHistoryLen;
//HDF5 parameters
static const int chunkHeight = 6000;
static const int deflateLevel = 6;
static const int h5Dimension = 2;
//SGF sources
static const int NUM_SOURCES = 6;
static const int SOURCE_GOGOD = 0;
static const int SOURCE_KGS = 1;
static const int SOURCE_FOX = 2;
static const int SOURCE_OGSPre2014 = 3;
static const int SOURCE_LEELAZERO = 4;
static const int SOURCE_UNKNOWN = 5;
static bool emittedSourceWarningYet = false;
static int parseSource(const string& fileName) {
if(fileName.find("GoGoD") != string::npos)
return SOURCE_GOGOD;
else if(fileName.find("/KGS/") != string::npos || fileName.find("/KGS4d/") != string::npos)
return SOURCE_KGS;
else if(fileName.find("FoxGo") != string::npos)
return SOURCE_FOX;
else if(fileName.find("OGSPre2014") != string::npos)
return SOURCE_OGSPre2014;
else {
if(!emittedSourceWarningYet) {
cerr << "Note: unknown source for sgf " << fileName << endl;
cerr << "There is some hardcoded logic for applying different filter conditions for known data sources (e.g. KGS, GoGoD, etc). If you would like to do filtering of your own, you can manually modify the parseSource function in write.cpp and/or add appropriate sources for your data, and add whatever conditions you like at appropriate points in the rest of write.cpp." << endl;
cerr << "Suppressing further warnings for unknown sgf sources" << endl;
emittedSourceWarningYet = true;
}
return SOURCE_UNKNOWN;
}
}
//When doing fancy conditions (cmdline flag -fancy-conditions), randomly keep games from source only with this prob
static const double sourceGameFancyProb[NUM_SOURCES] = {
1.00, /* GoGoD */
1.00, /* KGS */
0.15, /* FOX */ //Fox dataset is enormously large, only keep some of the games to prevent it from dwarfing all others in training and using lots of memory when writing
1.00, /* OGS */
1.00, /* Leela Zero - doesn't actually do anything since LZ doesn't come in sgf files */
1.00, /* Unknown */
};
//When doing fancy conditions (cmdline flag -fancy-conditions), randomly keep training instances from source only with this prob
//These numbers are tuned to try to balance the number of games in the training set coming from each different rank of player
//on each different server.
static const double rankOneHotFancyProb[rankLen] = {
1.00, /* GoGoD */
0.30, 0.30, 0.20, 0.10, 0.20, 0.10, 0.20, 0.50, 1.00, /* KGS */
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.20, /* FOX 17k-10k */
0.15, 0.15, 0.15, 0.15, 0.15, /* FOX 9k-5k */
0.14, 0.125, 0.080, 0.060, /* FOX 4k-1k */
0.040, 0.030, 0.025, 0.040, 0.060, /* FOX 1d-5d */
0.140, 0.350, 0.800, 0.400, /* FOX 6d-9d */
0.80, 0.80, 0.80, 0.80, /* OGS 19k-16k */
0.80, 0.80, 0.80, 0.80, 0.80, /* OGS 15k-11k */
0.80, 0.80, 0.80, 0.80, 0.80, /* OGS 10k-6k */
0.80, 1.00, 1.00, 1.00, 1.00, /* OGS 5k-1k */
1.00, 1.00, 1.00, 1.00, 1.00, /* OGS 1d-5d */
1.00, 1.00, 1.00, 1.00, /* OGS 6d-9d */
};
//Each row contains a one-hot segment that indicates the rank of the player that made this move, differentiated by
//rank since ranks mean different things on different servers.
//Computes the index of the one-hot entry to fill (or -1 indicating to fill none of them)
static int computeRankOneHot(int source, int rank) {
int rankOneHot = -1; //Fill nothing by default for unknown sources or ranks that are out-of-range
if(source == SOURCE_GOGOD)
rankOneHot = rankStartGoGoD;
else if(source == SOURCE_KGS && rank >= 0 && rank <= 8)
rankOneHot = rankStartKGS + rank;
else if(source == SOURCE_FOX && rank >= -17 && rank <= 8)
rankOneHot = rankStartFox + 17 + rank;
else if(source == SOURCE_OGSPre2014 && rank >= -19 && rank <= 8)
rankOneHot = rankStartOGSPre2014 + 19 + rank;
assert(rankOneHot >= -1 && rankOneHot < rankLen);
return rankOneHot;
}
static const int TARGET_NEXT_MOVE = 0;
static void fillRow(const Board& board, const BoardHistory& hist, const vector<Move>& moves, int nextMoveIdx, Player nextPlayer,
const float* policyTarget, float valueTarget,
int target, int rankOneHot, Hash128 sgfHash, float* row, Rand& rand, bool alwaysHistory) {
assert(nextMoveIdx < moves.size());
Player pla = nextPlayer;
int xSize = board.x_size;
int ySize = board.y_size;
int posLen = NNPos::MAX_BOARD_LEN;
bool inputsUseNHWC = true;
NNInputs::fillRowV2(board,hist,nextPlayer,posLen,inputsUseNHWC,row);
//Optionally some stuff we can multiply the history planes by to randomly exclude history from a few training samples
bool includeHistory[5];
includeHistory[0] = alwaysHistory || rand.nextDouble() < 0.95;
includeHistory[1] = alwaysHistory || (includeHistory[0] && rand.nextDouble() < 0.98);
includeHistory[2] = alwaysHistory || (includeHistory[1] && rand.nextDouble() < 0.98);
includeHistory[3] = alwaysHistory || (includeHistory[2] && rand.nextDouble() < 0.98);
includeHistory[4] = alwaysHistory || (includeHistory[3] && rand.nextDouble() < 0.98);
for(int i = 0; i<5; i++)
row[includeHistoryStart+i] = (includeHistory[i] ? 1.0f : 0.0f);
if(target == TARGET_NEXT_MOVE) {
for(int i = 0; i<policyTargetLen; i++)
row[policyTargetStart + i] = policyTarget[i];
}
//Value target, +1 or -1
row[valueTargetStart] = valueTarget;
//Weight of the row, currently always 1.0
row[targetWeightsStart] = 1.0;
//One-hot indicating rank
if(rankOneHot != -1)
row[rankStart + rankOneHot] = 1.0;
//Indicate the side to move, black = 0, white = 1
if(pla == P_BLACK)
row[sideStart] = 0.0;
else
row[sideStart] = 1.0;
//Record what turn out of what turn it is
row[turnNumberStart] = nextMoveIdx;
row[turnNumberStart+1] = moves.size();
//Record recent captures, by marking any positions where stones vanished between one board and the next
for(int i = (int)BoardHistory::NUM_RECENT_BOARDS-2; i >= 0; i--) {
const Board& b = hist.getRecentBoard(i);
const Board& bPrev = hist.getRecentBoard(i+1);
for(int y = 0; y<ySize; y++) {
for(int x = 0; x<xSize; x++) {
Loc loc = Location::getLoc(x,y,xSize);
if(b.colors[loc] == C_EMPTY && bPrev.colors[loc] != C_EMPTY) {
int pos = NNPos::xyToPos(x,y,posLen);
row[recentCapturesStart+pos] = i+1;
}
}
}
}
//Record next moves
for(int i = 0; i<nextMovesLen; i++) {
int idx = nextMoveIdx + i;
if(idx >= moves.size())
row[nextMovesStart+i] = NNPos::locToPos(Board::NULL_LOC,xSize,posLen);
else {
row[nextMovesStart+i] = NNPos::locToPos(moves[idx].loc,xSize,posLen);
}
}
//Record 16-bit chunks of sgf hash, so that later we can identify where this training example came from
row[sgfHashStart+0] = (float)((sgfHash.hash1 >> 0) & 0xFFFF);
row[sgfHashStart+1] = (float)((sgfHash.hash1 >> 16) & 0xFFFF);
row[sgfHashStart+2] = (float)((sgfHash.hash1 >> 32) & 0xFFFF);
row[sgfHashStart+3] = (float)((sgfHash.hash1 >> 48) & 0xFFFF);
row[sgfHashStart+4] = (float)((sgfHash.hash0 >> 0) & 0xFFFF);
row[sgfHashStart+5] = (float)((sgfHash.hash0 >> 16) & 0xFFFF);
row[sgfHashStart+6] = (float)((sgfHash.hash0 >> 32) & 0xFFFF);
row[sgfHashStart+7] = (float)((sgfHash.hash0 >> 48) & 0xFFFF);
}
static uint64_t parseHex64(const string& str) {
assert(str.length() == 16);
uint64_t x = 0;
for(int i = 0; i<16; i++) {
x *= 16;
if(str[i] >= '0' && str[i] <= '9')
x += str[i] - '0';
else if(str[i] >= 'a' && str[i] <= 'f')
x += str[i] - 'a' + 10;
else if(str[i] >= 'A' && str[i] <= 'F')
x += str[i] - 'A' + 10;
else
assert(false);
}
return x;
}
static int parseSource(const CompactSgf* sgf) {
return parseSource(sgf->fileName);
}
static int parseHandicap(const string& handicap) {
int h;
bool suc = Global::tryStringToInt(handicap,h);
if(!suc)
throw IOError("Unknown handicap: " + handicap);
return h;
}
static const int RANK_UNRANKED = -1000;
//2 kyu = -2, 1 kyu = -1, 1 dan = 0, 2 dan = 1, ... higher is stronger, pros are assumed to be 9d.
static int parseRank(const string& rank, bool isGoGoD) {
string r = Global::toLower(rank);
//Special case parsings
if(isGoGoD) {
if(r == "meijin" || r == "kisei" || r == "insei" || r == "judan" || r == "holder")
return 8;
}
if(r.length() < 2 || r.length() > 7)
throw IOError("Could not parse rank: " + rank);
int n = 0;
bool isK = false;
bool isD = false;
bool isP = false;
bool isA = false;
bool isAK = false;
if(r.length() == 2) {
if(r[1] != 'k' && r[1] != 'd' && r[1] != 'p' && r[1] != 'a')
throw IOError("Could not parse rank: " + rank);
if(!Global::isDigits(r,0,1))
throw IOError("Could not parse rank: " + rank);
n = Global::parseDigits(r,0,1);
isK = r[1] == 'k';
isD = r[1] == 'd';
isP = r[1] == 'p';
isA = r[1] == 'a'; //a few GoGoD records use 'a' to represent amateur dan
}
else if(r.length() == 3) {
if(r[2] != 'k' && r[2] != 'd' && r[2] != 'p' && r[2] != 'a')
throw IOError("Could not parse rank: " + rank);
if(!Global::isDigits(r,0,2))
throw IOError("Could not parse rank: " + rank);
n = Global::parseDigits(r,0,2);
isK = r[2] == 'k';
isD = r[2] == 'd';
isP = r[2] == 'p';
isA = r[2] == 'a'; //a few GoGoD records use 'a' to represent amateur dan
}
else if(r.length() == 4) {
//UTF-8 for 级(kyu/grade)
if(r[1] == '\xE7' && r[2] == '\xBA' && r[3] == '\xA7') {
if(!Global::isDigits(r,0,1))
throw IOError("Could not parse rank: " + rank);
isK = true;
n = Global::parseDigits(r,0,1);
}
//UTF-8 for 段(dan)
else if(r[1] == '\xE6' && r[2] == '\xAE' && r[3] == '\xB5') {
if(!Global::isDigits(r,0,1))
throw IOError("Could not parse rank: " + rank);
isD = true;
n = Global::parseDigits(r,0,1);
}
else
throw IOError("Could not parse rank: " + rank);
}
else if(r.length() == 5) {
//UTF-8 for 级(kyu/grade)
if(r[2] == '\xE7' && r[3] == '\xBA' && r[4] == '\xA7') {
if(!Global::isDigits(r,0,2))
throw IOError("Could not parse rank: " + rank);
isK = true;
n = Global::parseDigits(r,0,2);
}
//UTF-8 for 段(dan)
else if(r[2] == '\xE6' && r[3] == '\xAE' && r[4] == '\xB5') {
//FoxGo labels pro ranks like P6<chinese character for duan> for 6p
if(r[0] == 'p' && Global::isDigits(r,1,2)) {
isP = true;
n = Global::parseDigits(r,1,2);
}
else {
if(!Global::isDigits(r,0,2))
throw IOError("Could not parse rank: " + rank);
isD = true;
n = Global::parseDigits(r,0,2);
}
}
else
throw IOError("Could not parse rank: " + rank);
}
else if(r.length() == 6) {
//GoGoD often labels ranks like "6d ama"
if(r[1] == 'd' && r[2] == ' ' && r[3] == 'a' && r[4] == 'm' && r[5] == 'a' && Global::isDigits(r,0,1)) {
isA = true;
n = Global::parseDigits(r,0,1);
}
//GoGoD often labels ranks like "1k ama"
else if(r[1] == 'k' && r[2] == ' ' && r[3] == 'a' && r[4] == 'm' && r[5] == 'a' && Global::isDigits(r,0,1)) {
isAK = true;
n = Global::parseDigits(r,0,1);
}
else
throw IOError("Could not parse rank: " + rank);
}
//GoGoD has rengos between various pros
else if(r.length() == 7) {
if(r[1] == 'd' && r[2] == ' ' && r[3] == '&' && r[4] == ' ' && r[6] == 'd' &&
Global::isDigits(r,0,1) && Global::isDigits(r,5,6))
{
isD = true;
n = std::min(Global::parseDigits(r,0,1),Global::parseDigits(r,5,6));
}
else
throw IOError("Could not parse rank: " + rank);
}
else {
throw IOError("Could not parse rank: " + rank);
}
if(isGoGoD) {
if(isA)
return n >= 9 ? 8 : n-1;
//Treat GoGoD games as all 9d a large number of pros are labeled e.g. "3d" indicating 3 *professional* dan something like "3p".
//There are some games involving genuinely amateur dan players, but it's basically impossible to tell from the rank whether it's
//amateur or pro.
else if(isD)
return 8;
else if(isP)
return 8;
//Even kyu games can refer to the old korean kyu which is actually quite strong. We go ahead and exclude everything
//that's worse than 3k though, and anything else we label as 8d.
else if(isK)
return n >= 3 ? -n : 7;
else if(isAK)
return -n;
else {
throw IOError("Could not parse rank: " + rank);
}
}
else {
if(isK)
return -n;
else if(isD)
return n >= 9 ? 8 : n-1;
//Treat all professional dan ranks as 9d amateur
else if(isP)
return 8;
else {
assert(false);
return 0;
}
}
}
struct Stats {
size_t count;
map<int,int64_t> countBySource;
map<int,int64_t> countByRank;
map<int,int64_t> countByOppRank;
map<string,int64_t> countByUser;
map<int,int64_t> countByHandicap;
Stats()
:count(),countBySource(),countByRank(),countByOppRank(),countByUser(),countByHandicap() {
}
void print() {
cout << "Count: " << count << endl;
cout << "Sources:" << endl;
for(auto const& kv: countBySource) {
cout << kv.first << " " << kv.second << endl;
}
cout << "Ranks:" << endl;
for(auto const& kv: countByRank) {
cout << kv.first << " " << kv.second << endl;
}
cout << "OppRanks:" << endl;
for(auto const& kv: countByOppRank) {
cout << kv.first << " " << kv.second << endl;
}
cout << "Handicap:" << endl;
for(auto const& kv: countByHandicap) {
cout << kv.first << " " << kv.second << endl;
}
cout << "Major Users:" << endl;
for(auto const& kv: countByUser) {
if(kv.second > count / 2000)
cout << kv.first << " " << kv.second << endl;
}
}
};
typedef std::function<void(
//board,hist,source,rank,oppRank,user,handicap
const Board&,const BoardHistory&,int,int,int,const string&,int,
//date,moves,index within moves
const string&,const vector<Move>&,int,
//next player, policy target, value target, sgfhash
Player,const float*,float,Hash128
)> HandleRowFunc;
static void iterSgfMoves(
CompactSgf* sgf,
HandleRowFunc f
) {
int bSize;
int source;
int wRank;
int bRank;
string wUser;
string bUser;
int handicap;
string date;
const vector<Move>* placementsBuf = NULL;
const vector<Move>* movesBuf = NULL;
try {
bSize = sgf->bSize;
const SgfNode& root = sgf->rootNode;
source = parseSource(sgf);
if(source == SOURCE_GOGOD) {
//By default, assume pro rank in GoGod if not specified
wRank = 8;
bRank = 8;
bool isGoGoD = true;
try {
if(root.hasProperty("WR"))
wRank = parseRank(root.getSingleProperty("WR"),isGoGoD);
}
catch(const IOError &e) {
cout << "Warning: " << sgf->fileName << ": " << e.message << endl;
}
try {
if(root.hasProperty("BR"))
bRank = parseRank(root.getSingleProperty("BR"),isGoGoD);
}
catch(const IOError &e) {
cout << "Warning: " << sgf->fileName << ": " << e.message << endl;
}
}
else {
wRank = RANK_UNRANKED;
bRank = RANK_UNRANKED;
bool isGoGoD = false;
if(root.hasProperty("WR"))
wRank = parseRank(root.getSingleProperty("WR"),isGoGoD);
if(root.hasProperty("BR"))
bRank = parseRank(root.getSingleProperty("BR"),isGoGoD);
}
wUser = root.getSingleProperty("PW");
bUser = root.getSingleProperty("PB");
handicap = 0;
if(root.hasProperty("HA"))
handicap = parseHandicap(root.getSingleProperty("HA"));
if(root.hasProperty("DT"))
date = root.getSingleProperty("DT");
//Apply some filters
if(bSize != 19)
return;
placementsBuf = &(sgf->placements);
movesBuf = &(sgf->moves);
//OGS has a ton of garbage, for OGS require a minimum length
//to try to filter out random demos and problems and such
if(source == SOURCE_OGSPre2014) {
if(movesBuf->size() < 40)
return;
}
}
catch(const IOError &e) {
cout << "Skipping sgf file: " << sgf->fileName << ": " << e.message << endl;
return;
}
const vector<Move>& placements = *placementsBuf;
const vector<Move>& moves = *movesBuf;
Board initialBoard(bSize,bSize);
bool multiStoneSuicideLegal = false; //False for KGS,GoGoD, etc
for(int j = 0; j<placements.size(); j++) {
Move m = placements[j];
bool suc = initialBoard.setStone(m.loc,m.pla);
if(!suc) {
cout << sgf->fileName << endl;
cout << ("Illegal stone placement " + Global::intToString(j)) << endl;
cout << initialBoard << endl;
return;
}
}
//If there are multiple black moves in a row, then make them all right now.
//Sometimes sgfs break the standard and do handicap setup in this way.
int j = 0;
if(moves.size() > 1 && moves[0].pla == P_BLACK && moves[1].pla == P_BLACK) {
for(; j<moves.size(); j++) {
Move m = moves[j];
if(m.pla != P_BLACK)
break;
bool suc = initialBoard.playMove(m.loc,m.pla,multiStoneSuicideLegal);
if(!suc) {
cout << sgf->fileName << endl;
cout << ("Illegal move! " + Global::intToString(j)) << endl;
cout << initialBoard << endl;
}
}
}
Board board = initialBoard;
Rules rules;
rules.koRule = Rules::KO_SIMPLE;
rules.scoringRule = Rules::SCORING_AREA;
rules.multiStoneSuicideLegal = multiStoneSuicideLegal;
rules.komi = Rules::getTrompTaylorish().komi;
BoardHistory hist(initialBoard,(moves.size() > 0 ? moves[j].pla : P_BLACK),rules);
Player prevPla = C_EMPTY;
for(; j<moves.size(); j++) {
Move m = moves[j];
//Forbid consecutive moves by the same player
if(m.pla == prevPla) {
//Multiple-consecutive-move-by-same-player issues are super-common on FoxGo, so don't print on Fox
//Not actually sure how this happens. It's a large number of games, but still only a tiny percentage,
//and it often happens well into the middle of the game, and definitely before the end of the game.
if(source != SOURCE_FOX) {
cout << sgf->fileName << endl;
cout << ("Multiple moves in a row by same player at " + Global::intToString(j)) << endl;
cout << board << endl;
}
//Terminate reading from the game in this case
break;
}
int rank = m.pla == P_WHITE ? wRank : bRank;
int oppRank = m.pla == P_WHITE ? bRank : wRank;
const string& user = m.pla == P_WHITE ? wUser : bUser;
float policyTarget[policyTargetLen];
{
int posLen = NNPos::MAX_BOARD_LEN;
for(int k = 0; k<policyTargetLen; k++)
policyTarget[k] = 0.0;
assert(m.loc != Board::NULL_LOC);
int nextMovePos = NNPos::locToPos(m.loc,board.x_size,posLen);
assert(nextMovePos >= 0 && nextMovePos < policyTargetLen);
policyTarget[nextMovePos] = 1.0;
}
float valueTarget = 0.0; //value target not implemented for sgf
f(board,hist,source,rank,oppRank,user,handicap,date,moves,j,m.pla,policyTarget,valueTarget,sgf->hash);
Move mv = moves[j];
bool suc = board.isLegal(mv.loc,mv.pla,multiStoneSuicideLegal);
if(!suc) {
cout << sgf->fileName << endl;
cout << ("Illegal move! " + Global::intToString(j)) << endl;
cout << board << endl;
break;
}
hist.makeBoardMoveAssumeLegal(board, moves[j].loc, moves[j].pla, NULL);
prevPla = m.pla;
}
return;
}
static void iterSgfsAndLZMoves(
vector<CompactSgf*>& sgfs, vector<string>& lzFiles,
uint64_t shardSeed, int numShards,
const size_t& numMovesUsed, const size_t& curDataSetRow,
Stats& total, double keepProb, Rand& keepRand,
HandleRowFunc f
) {
size_t numMovesItered = 0;
size_t numMovesIteredOrSkipped = 0;
for(int shard = 0; shard < numShards; shard++) {
Rand shardRand(shardSeed);
HandleRowFunc g =
[f,shard,numShards,&shardRand,&numMovesIteredOrSkipped,&numMovesItered,&total,keepProb,&keepRand](
const Board& board, const BoardHistory& hist, int source, int rank, int oppRank, const string& user, int handicap, const string& date,
const vector<Move>& moves, int moveIdx,
Player nextPlayer, const float* policyTarget, float valueTarget, Hash128 sgfHash
) {
//Only use this move if it's within our shard.
numMovesIteredOrSkipped++;
if(numShards <= 1 || shard == shardRand.nextUInt(numShards)) {
numMovesItered++;
total.count += 1;
total.countBySource[source] += 1;
total.countByRank[rank] += 1;
total.countByOppRank[oppRank] += 1;
total.countByUser[user] += 1;
total.countByHandicap[handicap] += 1;
if(keepProb >= 1.0 || (keepRand.nextDouble() < keepProb)) {
f(board,hist,source,rank,oppRank,user,handicap,date,moves,moveIdx,nextPlayer,policyTarget,valueTarget,sgfHash);
}
}
};
for(int i = 0; i<sgfs.size(); i++) {
if(i % 5000 == 0)
cout << "Shard " << shard << " "
<< "processed " << i << "/" << sgfs.size() << " sgfs, "
<< "itered " << numMovesItered << " moves, "
<< "used " << numMovesUsed << " moves, "
<< "written " << curDataSetRow << " rows..." << endl;
iterSgfMoves(sgfs[i],g);
}
Board board;
BoardHistory hist;
vector<Move> moves;
const string lzname = string("Leela Zero");
const string lzdate = string("No date");
std::function<void(const LZSample& sample, const string& fileName, int sampleCount)> h =
[f,shard,numShards,&shardRand,&numMovesIteredOrSkipped,&numMovesItered,&lzname,&lzdate,&board,&hist,&moves,&total,keepProb,&keepRand]
(const LZSample& sample, const string& fileName, int sampleCount) {
//Only use this move if it's within our shard.
numMovesIteredOrSkipped++;
if(numShards <= 1 || shard == shardRand.nextUInt(numShards)) {
numMovesItered++;
int source = SOURCE_LEELAZERO;
//Leela zero is pro
int rank = 8;
int oppRank = 8;
const string& user = lzname;
//Leela zero games have no handicap
int handicap = 0;
total.count += 1;
total.countBySource[source] += 1;
total.countByRank[rank] += 1;
total.countByOppRank[oppRank] += 1;
total.countByUser[user] += 1;
total.countByHandicap[handicap] += 1;
if(keepProb >= 1.0 || (keepRand.nextDouble() < keepProb)) {
assert(policyTargetLen == 362);
float policyTarget[362];
Player nextPlayer;
Player winner;
try {
sample.parse(board,hist,moves,policyTarget,nextPlayer,winner);
}
catch(const IOError &e) {
cout << "Error reading: " << fileName << " sample " << sampleCount << ": " << e.message << endl;
return;
}
float valueTarget = 0.0;
if(winner == nextPlayer)
valueTarget = 1.0;
else if(winner == getOpp(nextPlayer))
valueTarget = -1.0;
//The "next" move is always the end of the sample's reported move history
int moveIdx = moves.size()-1;
// for(int n = 7; n >= 0; n--) {
// cout << boards[n] << endl;
// cout << Location::toString(moves[7-n].loc,19) << " " << (int)moves[7-n].pla << endl;
// }
// for(int y = 0; y<19; y++) {
// for(int x = 0; x<19; x++) {
// printf("%3.0f ", policyTarget[y*19+x]*100.0);
// }
// cout << endl;
// }
// cout << "Value target " << valueTarget << endl;
// cout << "Self komi " << selfKomi << endl;
Hash128 sgfHash = Hash128(0,0);
f(board,hist,source,rank,oppRank,user,handicap,lzdate,moves,moveIdx,nextPlayer,policyTarget,valueTarget,sgfHash);
}
}
};
for(int i = 0; i<lzFiles.size(); i++) {
if(i % 50 == 0)
cout << "Shard " << shard << " "
<< "processed " << i << "/" << lzFiles.size() << " lz files, "
<< "itered " << numMovesItered << " moves, "
<< "used " << numMovesUsed << " moves, "
<< "written " << curDataSetRow << " rows..." << endl;
LZSample::iterSamples(lzFiles[i],h);
}
}
assert(numMovesIteredOrSkipped == numMovesItered * numShards);
cout << "Over all shards, numMovesItered = " << numMovesItered
<< " numMovesIteredOrSkipped = " << numMovesIteredOrSkipped
<< " numMovesItered*numShards = " << (numMovesItered * numShards) << endl;
}
static void maybeUseRow(
const Board& board, const BoardHistory& hist, int source, int rank, int oppRank, const string& user, int handicap,
const string& date, const vector<Move>& movesBuf, int moveIdx,
Player nextPlayer, const float* policyTarget, float valueTarget, Hash128 sgfHash,
DataPool& dataPool,
Rand& rand, int minRank, int minOppRank, int maxHandicap, int target,
bool alwaysHistory, bool includePasses,
const set<string>& excludeUsers, bool fancyConditions, double fancyPosKeepFactor,
set<uint64_t>& posHashes, Stats& used
) {
//For now, only generate training rows for non-passes
//Also only use moves by this player if that player meets rank threshold
if((movesBuf[moveIdx].loc != Board::PASS_LOC || includePasses) &&
rank >= minRank &&
oppRank >= minOppRank &&
handicap <= maxHandicap &&
!contains(excludeUsers,user)
) {
assert(movesBuf[moveIdx].loc != Board::NULL_LOC);
int rankOneHot = computeRankOneHot(source,rank);
bool canUse = true;
//Apply special filtering for when we want to make a rank-balanced training set
if(fancyConditions) {
//Require that we have a good rank
if(rankOneHot < 0)
canUse = false;
//Some ranks have too many games, filter them down
if(rand.nextDouble() >= rankOneHotFancyProb[rankOneHot] * fancyPosKeepFactor)
canUse = false;
//No handicap games from GoGoD since they're less likely to be pro-level
if(source == SOURCE_GOGOD && handicap >= 2)
canUse = false;
//No kyu moves from GoGoD, no amateur moves that are too weak
if(source == SOURCE_GOGOD && rank < 4)
canUse = false;
//OGS had a major rank shift in 2014, only use games before
if(source == SOURCE_OGSPre2014) {
if(date.size() != 10)
canUse = false;
//Find year and month of date in format yyyy-mm-dd
else if(!Global::isDigits(date,0,4) || !Global::isDigits(date,5,7))
canUse = false;
else {
int year = Global::parseDigits(date,0,4);
int month = Global::parseDigits(date,5,7);
if((year >= 1990 && year <= 2013) || (year == 2014 && month <= 3))
{} //good
else
canUse = false;
}
}
//Fox Go has a bunch of games by usernameless people. Are they guests? Anyways let's filter that.
if(source == SOURCE_FOX) {
if(user.length() <= 0 || user == " ")
canUse = false;
}
}
if(canUse) {
float* newRow = dataPool.addNewRow(rand);
fillRow(board,hist,movesBuf,moveIdx,nextPlayer,policyTarget,valueTarget,target,rankOneHot,sgfHash,newRow,rand,alwaysHistory);
posHashes.insert(board.pos_hash.hash0);
used.count += 1;
used.countBySource[source] += 1;
used.countByRank[rank] += 1;
used.countByOppRank[oppRank] += 1;
used.countByUser[user] += 1;
used.countByHandicap[handicap] += 1;
}
}
}
static void processData(
vector<CompactSgf*>& sgfs, vector<string>& lzFiles, DataSet* dataSet,
size_t poolSize,
uint64_t shardSeed, int numShards,
Rand& rand, double keepProb,
int minRank, int minOppRank, int maxHandicap, int target,
bool alwaysHistory, bool includePasses,
const set<string>& excludeUsers, bool fancyConditions, double fancyPosKeepFactor,
set<uint64_t>& posHashes, Stats& total, Stats& used
) {
size_t curDataSetRow = 0;
std::function<void(const float*,size_t)> writeRow = [&curDataSetRow,&dataSet](const float* rows, size_t numRows) {
hsize_t newDims[h5Dimension] = {curDataSetRow+numRows,totalRowLen};
dataSet->extend(newDims);
DataSpace fileSpace = dataSet->getSpace();
hsize_t memDims[h5Dimension] = {numRows,totalRowLen};
DataSpace memSpace(h5Dimension,memDims);
hsize_t start[h5Dimension] = {curDataSetRow,0};
hsize_t count[h5Dimension] = {numRows,totalRowLen};
fileSpace.selectHyperslab(H5S_SELECT_SET, count, start);
dataSet->write(rows, PredType::NATIVE_FLOAT, memSpace, fileSpace);
curDataSetRow += numRows;
};
DataPool dataPool(totalRowLen,poolSize,chunkHeight,writeRow);
HandleRowFunc f =
[&dataPool,&rand,minRank,minOppRank,maxHandicap,target,&excludeUsers,fancyConditions,fancyPosKeepFactor,alwaysHistory,includePasses,&posHashes,&used](
const Board& board, const BoardHistory& hist, int source, int rank, int oppRank, const string& user, int handicap, const string& date,
const vector<Move>& moves, int moveIdx,
Player nextPlayer, const float* policyTarget, float valueTarget, Hash128 sgfHash
) {
maybeUseRow(
board,hist,source,rank,oppRank,user,handicap,date,moves,moveIdx,
nextPlayer,policyTarget,valueTarget,sgfHash,
dataPool,rand,minRank,minOppRank,maxHandicap,target,
alwaysHistory, includePasses,
excludeUsers,fancyConditions,fancyPosKeepFactor,
posHashes,used
);
};
iterSgfsAndLZMoves(
sgfs,lzFiles,
shardSeed,numShards,
used.count,curDataSetRow,
total,keepProb,rand,
f
);
cout << "Emptying pool" << endl;
dataPool.finishAndWritePool(rand);
}
int main(int argc, const char* argv[]) {
assert(sizeof(size_t) == 8);
Board::initHash();
// auto f = [](const LZSample& sample) {
// cout << sample.boards[0];
// cout << "Prev move: " << (int)sample.moves[sample.moves.size()-1].pla << " " << Location::toString(sample.moves[sample.moves.size()-1].loc,19) << " " << endl;
// cout << "Winner: " << (int)sample.winner << endl;
// vector<int> movePoses;
// for(int i = 0; i<19*19+1; i++)
// movePoses.push_back(i);
// std::sort(
// movePoses.begin(),movePoses.end(),
// [&](const int& a, const int& b) { return (sample.probs[a] > sample.probs[b]); }
// );
// for(int i = 0; i<3; i++) {
// int pos = movePoses[i];
// if(pos == 361)
// cout << "pass";
// else
// cout << Location::toString(Location::getLoc(pos%19,pos/19,19),19);
// cout << " " << sample.probs[pos] << endl;
// }
// };
// LZSample::iterSamples(string("~/data/GoDatasets/LZData/train_521b0868/train_521b0868_0.gz"),f);
// return 0;
// string s =
// ". . . . . O O O O . . . . . . O O X ."
// ". . . . X X O X O O . . . . . O X . X"
// ". . . X X O O X X . O O . X . O X X ."
// ". . X X . X X . . O . X O . . O X . X"
// ". X O O O X . X O . . O . O . O X . O"
// "X X X O O X . X O . X X O X O O X . O"
// ". X O O O X . X . O . . X . O X X X ."
// "X O O . O X O X X O . X X O . O . . ."
// ". X X O . O X X O X X . . X . O X X X"
// "X . X O O O O O O X . . . . . O O O ."
// ". X O O O X . O X X . . . . X X O . ."
// ". X O X . X . O O X X . X X . X O . ."
// "X . X . X . . O X X O O O O X X . . ."
// "X X O X X X . O . X X O . . O . X X ."
// "X O O X . O O . X . . X O . O O X O ."
// "O . O X O . O . X O . X O . O * O O ."
// ". O O X . O O X X X X O O O X O O . ."
// ". O X X X O O X O O O O O . . . . . ."
// ". O . . . O X X . . . . . . . . . . ."
// ;
// Board testBoard(19);
// int next = -1;
// for(int y = 0; y<19; y++) {
// for(int x = 0; x < 19; x++) {
// next += 1;
// while(s[next] != '.' && s[next] != '*' && s[next] != 'O' && s[next] != 'X')
// next += 1;
// if(s[next] == 'O')
// testBoard.setStone(Location::getLoc(x,y,19),P_WHITE);
// if(s[next] == 'X')
// testBoard.setStone(Location::getLoc(x,y,19),P_BLACK);
// }
// }
// cout << testBoard << endl;
// Board testCopy(testBoard);
// vector<Loc> buf;
// cout << testCopy << endl;
// cout << testCopy.searchIsLadderCaptured(Location::getLoc(11,4,19),true,buf) << endl;
// cout << testCopy.searchIsLadderCaptured(Location::getLoc(6,7,19),true,buf) << endl;
// return 0;
cout << "Command: ";
for(int i = 0; i<argc; i++)
cout << argv[i] << " ";
cout << endl;
vector<string> gamesDirs;
vector<string> lzDirs;
string outputFile;
string onlyFilesFile;
string excludeFilesFile;
vector<string> excludeHashesFiles;
size_t poolSize;
int trainShards;
double valGameProb;
double keepTrainProb;
double keepValProb;
int minRank;
int minOppRank;
int maxHandicap;