-
Notifications
You must be signed in to change notification settings - Fork 725
Expand file tree
/
Copy pathnneval.cpp
More file actions
1259 lines (1120 loc) · 43.2 KB
/
Copy pathnneval.cpp
File metadata and controls
1259 lines (1120 loc) · 43.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 "../neuralnet/nneval.h"
#include "../neuralnet/modelversion.h"
using namespace std;
//-------------------------------------------------------------------------------------
NNResultBuf::NNResultBuf()
: clientWaitingForResult(),
resultMutex(),
hasResult(false),
includeOwnerMap(false),
boardXSizeForServer(0),
boardYSizeForServer(0),
rowSpatialBuf(),
rowGlobalBuf(),
rowMetaBuf(),
hasRowMeta(false),
result(nullptr),
errorLogLockout(false),
// If no symmetry is specified, it will use default or random based on config.
symmetry(NNInputs::SYMMETRY_NOTSPECIFIED),
policyOptimism(0.0)
{}
NNResultBuf::~NNResultBuf() {
}
//-------------------------------------------------------------------------------------
NNServerBuf::NNServerBuf(const NNEvaluator& nnEval, const LoadedModel* model)
:inputBuffers(NULL)
{
int maxBatchSize = nnEval.getMaxBatchSize();
if(model != NULL)
inputBuffers = NeuralNet::createInputBuffers(model,maxBatchSize,nnEval.getNNXLen(),nnEval.getNNYLen());
}
NNServerBuf::~NNServerBuf() {
if(inputBuffers != NULL)
NeuralNet::freeInputBuffers(inputBuffers);
inputBuffers = NULL;
}
//-------------------------------------------------------------------------------------
NNEvaluator::NNEvaluator(
const string& mName,
const string& mFileName,
const string& expectedSha256,
Logger* lg,
int maxBatchSz,
int xLen,
int yLen,
bool rExactNNLen,
bool iUseNHWC,
int nnCacheSizePowerOfTwo,
int nnMutexPoolSizePowerofTwo,
bool skipNeuralNet,
const string& openCLTunerFile,
const string& homeDataDirOverride,
bool openCLReTunePerBoardSize,
enabled_t useFP16Mode,
enabled_t useNHWCMode,
int numThr,
const vector<int>& gpuIdxByServerThr,
const string& rSeed,
bool doRandomize,
int defaultSymmetry
)
:modelName(mName),
modelFileName(mFileName),
nnXLen(xLen),
nnYLen(yLen),
requireExactNNLen(rExactNNLen),
policySize(NNPos::getPolicySize(xLen,yLen)),
inputsUseNHWC(iUseNHWC),
usingFP16Mode(useFP16Mode),
usingNHWCMode(useNHWCMode),
numThreads(numThr),
gpuIdxByServerThread(gpuIdxByServerThr),
randSeed(rSeed),
debugSkipNeuralNet(skipNeuralNet),
computeContext(NULL),
loadedModel(NULL),
nnCacheTable(NULL),
logger(lg),
internalModelName(),
modelVersion(-1),
inputsVersion(-1),
numInputMetaChannels(0),
postProcessParams(),
numServerThreadsEverSpawned(0),
serverThreads(),
maxBatchSize(maxBatchSz),
m_numRowsProcessed(0),
m_numBatchesProcessed(0),
bufferMutex(),
isKilled(false),
numServerThreadsStartingUp(0),
mainThreadWaitingForSpawn(),
numOngoingEvals(0),
numWaitingEvals(0),
numEvalsToAwaken(0),
waitingForFinish(),
currentDoRandomize(doRandomize),
currentDefaultSymmetry(defaultSymmetry),
currentBatchSize(maxBatchSz),
queryQueue()
{
if(nnXLen > NNPos::MAX_BOARD_LEN)
throw StringError("Maximum supported nnEval board size is " + Global::intToString(NNPos::MAX_BOARD_LEN));
if(nnYLen > NNPos::MAX_BOARD_LEN)
throw StringError("Maximum supported nnEval board size is " + Global::intToString(NNPos::MAX_BOARD_LEN));
if(maxBatchSize <= 0)
throw StringError("maxBatchSize is negative: " + Global::intToString(maxBatchSize));
if(gpuIdxByServerThread.size() != numThreads)
throw StringError("gpuIdxByServerThread.size() != numThreads");
if(logger != NULL) {
logger->write(
"Initializing neural net buffer to be size " +
Global::intToString(nnXLen) + " * " + Global::intToString(nnYLen) +
(requireExactNNLen ? " exactly" : " allowing smaller boards")
);
}
if(nnCacheSizePowerOfTwo >= 0)
nnCacheTable = new NNCacheTable(nnCacheSizePowerOfTwo, nnMutexPoolSizePowerofTwo);
if(!debugSkipNeuralNet) {
vector<int> gpuIdxs = gpuIdxByServerThread;
std::sort(gpuIdxs.begin(), gpuIdxs.end());
auto last = std::unique(gpuIdxs.begin(), gpuIdxs.end());
gpuIdxs.erase(last,gpuIdxs.end());
loadedModel = NeuralNet::loadModelFile(modelFileName,expectedSha256);
const ModelDesc& desc = NeuralNet::getModelDesc(loadedModel);
if(desc.onnxHeader.isOnnx)
{
desc.onnxHeader.maybeChangeNNLen(*this);
if(nnXLen > NNPos::MAX_BOARD_LEN)
throw StringError("Maximum supported nnEval board size is " + Global::intToString(NNPos::MAX_BOARD_LEN));
if(nnYLen > NNPos::MAX_BOARD_LEN)
throw StringError("Maximum supported nnEval board size is " + Global::intToString(NNPos::MAX_BOARD_LEN));
}
internalModelName = desc.name;
modelVersion = desc.modelVersion;
inputsVersion = NNModelVersion::getInputsVersion(modelVersion);
numInputMetaChannels = desc.numInputMetaChannels;
postProcessParams = desc.postProcessParams;
computeContext = NeuralNet::createComputeContext(
gpuIdxs,logger,nnXLen,nnYLen,
openCLTunerFile,homeDataDirOverride,openCLReTunePerBoardSize,
usingFP16Mode,usingNHWCMode,loadedModel
);
}
else {
internalModelName = "random";
modelVersion = NNModelVersion::defaultModelVersion;
inputsVersion = NNModelVersion::getInputsVersion(modelVersion);
}
//Reserve a decent amount above the batch size so that allocation is unlikely.
queryQueue.reserve(maxBatchSize * 4 * gpuIdxByServerThread.size());
//Starts readonly. Becomes writable once we spawn server threads
queryQueue.setReadOnly();
}
NNEvaluator::~NNEvaluator() {
killServerThreads();
if(computeContext != NULL)
NeuralNet::freeComputeContext(computeContext);
computeContext = NULL;
if(loadedModel != NULL)
NeuralNet::freeLoadedModel(loadedModel);
loadedModel = NULL;
delete nnCacheTable;
}
string NNEvaluator::getModelName() const {
return modelName;
}
string NNEvaluator::getModelFileName() const {
return modelFileName;
}
string NNEvaluator::getInternalModelName() const {
return internalModelName;
}
static bool tryAbbreviateStepString(const string& input, string& buf) {
size_t i = 0;
while(i < input.length() && !Global::isDigit(input[i]))
i++;
if(i > 1)
return false;
string prefix = input.substr(0, i);
int64_t number;
bool suc = Global::tryStringToInt64(input.substr(i),number);
if(!suc)
return false;
if(number >= 10000000000LL)
buf = prefix + std::to_string(number / 1000000000LL) + "G";
if(number >= 10000000)
buf = prefix + std::to_string(number / 1000000) + "M";
else if(number >= 10000)
buf = prefix + std::to_string(number / 1000) + "K";
else
buf = input;
return true;
}
string NNEvaluator::getAbbrevInternalModelName() const {
string name = getInternalModelName();
std::vector<string> pieces = Global::split(name,'-');
std::vector<string> newPieces;
for(const string& piece: pieces) {
string buf;
if(piece == "kata1") {
// skip
}
else if(piece.size() > 1 && piece[0] == 's' && tryAbbreviateStepString(piece,buf)) {
newPieces.push_back(buf);
}
else if(piece.size() > 1 && piece[0] == 'd' && tryAbbreviateStepString(piece,buf)) {
// skip
}
else {
newPieces.push_back(piece);
}
}
return Global::concat(newPieces,"-");
}
Logger* NNEvaluator::getLogger() {
return logger;
}
bool NNEvaluator::isNeuralNetLess() const {
return debugSkipNeuralNet;
}
int NNEvaluator::getMaxBatchSize() const {
return maxBatchSize;
}
int NNEvaluator::getCurrentBatchSize() const {
return currentBatchSize.load(std::memory_order_acquire);
}
void NNEvaluator::setCurrentBatchSize(int batchSize) {
if(batchSize <= 0 || batchSize > maxBatchSize)
throw StringError("Invalid setting for batch size");
currentBatchSize.store(batchSize,std::memory_order_release);
}
bool NNEvaluator::requiresSGFMetadata() const {
return numInputMetaChannels > 0;
}
int NNEvaluator::getNumGpus() const {
#ifdef USE_EIGEN_BACKEND
return 1;
#else
std::set<int> gpuIdxs;
for(int i = 0; i<gpuIdxByServerThread.size(); i++) {
gpuIdxs.insert(gpuIdxByServerThread[i]);
}
return (int)gpuIdxs.size();
#endif
}
int NNEvaluator::getNumServerThreads() const {
return (int)gpuIdxByServerThread.size();
}
std::set<int> NNEvaluator::getGpuIdxs() const {
std::set<int> gpuIdxs;
#ifdef USE_EIGEN_BACKEND
gpuIdxs.insert(0);
#else
for(int i = 0; i<gpuIdxByServerThread.size(); i++) {
gpuIdxs.insert(gpuIdxByServerThread[i]);
}
#endif
return gpuIdxs;
}
int NNEvaluator::getNNXLen() const {
return nnXLen;
}
int NNEvaluator::getNNYLen() const {
return nnYLen;
}
int NNEvaluator::getModelVersion() const {
return modelVersion;
}
double NNEvaluator::getTrunkSpatialConvDepth() const {
return NeuralNet::getModelDesc(loadedModel).getTrunkSpatialConvDepth();
}
enabled_t NNEvaluator::getUsingFP16Mode() const {
return usingFP16Mode;
}
enabled_t NNEvaluator::getUsingNHWCMode() const {
return usingNHWCMode;
}
bool NNEvaluator::supportsShorttermError() const {
return modelVersion >= 9;
}
bool NNEvaluator::getDoRandomize() const {
return currentDoRandomize.load(std::memory_order_acquire);
}
int NNEvaluator::getDefaultSymmetry() const {
return currentDefaultSymmetry.load(std::memory_order_acquire);
}
void NNEvaluator::setDoRandomize(bool b) {
currentDoRandomize.store(b, std::memory_order_release);
}
void NNEvaluator::setDefaultSymmetry(int s) {
currentDefaultSymmetry.store(s, std::memory_order_release);
}
Rules NNEvaluator::getSupportedRules(const Rules& desiredRules, bool& supported) {
if(loadedModel == NULL) {
supported = true;
return desiredRules;
}
return NeuralNet::getModelDesc(loadedModel).getSupportedRules(desiredRules, supported);
}
uint64_t NNEvaluator::numRowsProcessed() const {
return m_numRowsProcessed.load(std::memory_order_relaxed);
}
uint64_t NNEvaluator::numBatchesProcessed() const {
return m_numBatchesProcessed.load(std::memory_order_relaxed);
}
double NNEvaluator::averageProcessedBatchSize() const {
return (double)numRowsProcessed() / (double)numBatchesProcessed();
}
void NNEvaluator::clearStats() {
m_numRowsProcessed.store(0);
m_numBatchesProcessed.store(0);
}
void NNEvaluator::clearCache() {
if(nnCacheTable != NULL)
nnCacheTable->clear();
}
bool NNEvaluator::isAnyThreadUsingFP16() const {
lock_guard<std::mutex> lock(bufferMutex);
for(const int& isUsingFP16: serverThreadsIsUsingFP16) {
if(isUsingFP16)
return true;
}
return false;
}
static void serveEvals(
string randSeedThisThread,
NNEvaluator* nnEval, const LoadedModel* loadedModel,
int gpuIdxForThisThread,
int serverThreadIdx
) {
NNServerBuf* buf = new NNServerBuf(*nnEval,loadedModel);
Rand rand(randSeedThisThread);
//Used to have a try catch around this but actually we're in big trouble if this raises an exception
//and causes possibly the only nnEval thread to die, so actually go ahead and let the exception escape to
//toplevel for easier debugging
nnEval->serve(*buf,rand,gpuIdxForThisThread,serverThreadIdx);
delete buf;
}
void NNEvaluator::setNumThreads(const vector<int>& gpuIdxByServerThr) {
if(serverThreads.size() != 0)
throw StringError("NNEvaluator::setNumThreads called when threads were already running!");
numThreads = (int)gpuIdxByServerThr.size();
gpuIdxByServerThread = gpuIdxByServerThr;
}
void NNEvaluator::spawnServerThreads() {
if(serverThreads.size() != 0)
throw StringError("NNEvaluator::spawnServerThreads called when threads were already running!");
{
lock_guard<std::mutex> lock(bufferMutex);
serverThreadsIsUsingFP16.resize(numThreads,0);
}
queryQueue.unsetReadOnly();
numServerThreadsStartingUp = numThreads;
for(int i = 0; i<numThreads; i++) {
int gpuIdxForThisThread = gpuIdxByServerThread[i];
string randSeedThisThread = randSeed + ":NNEvalServerThread:" + Global::intToString(numServerThreadsEverSpawned);
numServerThreadsEverSpawned++;
std::thread* thread = new std::thread(
&serveEvals,randSeedThisThread,this,loadedModel,gpuIdxForThisThread,i
);
serverThreads.push_back(thread);
}
unique_lock<std::mutex> lock(bufferMutex);
while(numServerThreadsStartingUp > 0)
mainThreadWaitingForSpawn.wait(lock);
}
void NNEvaluator::killServerThreads() {
unique_lock<std::mutex> lock(bufferMutex);
isKilled = true;
lock.unlock();
queryQueue.setReadOnly();
waitingForFinish.notify_all();
for(size_t i = 0; i<serverThreads.size(); i++)
serverThreads[i]->join();
for(size_t i = 0; i<serverThreads.size(); i++)
delete serverThreads[i];
serverThreads.clear();
serverThreadsIsUsingFP16.clear();
//Can unset now that threads are dead
isKilled = false;
assert(numOngoingEvals == 0);
assert(numWaitingEvals == 0);
assert(numEvalsToAwaken == 0);
}
void NNEvaluator::serve(
NNServerBuf& buf, Rand& rand,
int gpuIdxForThisThread,
int serverThreadIdx
) {
int64_t numBatchesHandledThisThread = 0;
int64_t numRowsHandledThisThread = 0;
ComputeHandle* gpuHandle = NULL;
if(loadedModel != NULL)
gpuHandle = NeuralNet::createComputeHandle(
computeContext,
loadedModel,
logger,
maxBatchSize,
requireExactNNLen,
inputsUseNHWC,
gpuIdxForThisThread,
serverThreadIdx
);
{
lock_guard<std::mutex> lock(bufferMutex);
assert(serverThreadIdx < serverThreadsIsUsingFP16.size());
serverThreadsIsUsingFP16[serverThreadIdx] = gpuHandle == NULL ? 0 : NeuralNet::isUsingFP16(gpuHandle) ? 1 : 0;
numServerThreadsStartingUp--;
if(numServerThreadsStartingUp <= 0)
mainThreadWaitingForSpawn.notify_all();
}
vector<NNResultBuf*> resultBufs;
resultBufs.reserve(maxBatchSize);
vector<NNOutput*> outputBuf;
unique_lock<std::mutex> lock(bufferMutex,std::defer_lock);
while(true) {
resultBufs.clear();
int desiredBatchSize = std::min(maxBatchSize, currentBatchSize.load(std::memory_order_acquire));
bool gotAnything = queryQueue.waitPopUpToN(resultBufs,desiredBatchSize);
//Queue being closed is a signal that we're done.
if(!gotAnything)
break;
int numRows = (int)resultBufs.size();
assert(numRows > 0);
bool doRandomize = currentDoRandomize.load(std::memory_order_acquire);
int defaultSymmetry = currentDefaultSymmetry.load(std::memory_order_acquire);
if(debugSkipNeuralNet) {
for(int row = 0; row < numRows; row++) {
assert(resultBufs[row] != NULL);
NNResultBuf* resultBuf = resultBufs[row];
resultBufs[row] = NULL;
int boardXSize = resultBuf->boardXSizeForServer;
int boardYSize = resultBuf->boardYSizeForServer;
unique_lock<std::mutex> resultLock(resultBuf->resultMutex);
assert(resultBuf->hasResult == false);
resultBuf->result = std::make_shared<NNOutput>();
float* policyProbs = resultBuf->result->policyProbs;
for(int i = 0; i<NNPos::MAX_NN_POLICY_SIZE; i++)
policyProbs[i] = 0;
//At this point, these aren't probabilities, since this is before the postprocessing
//that happens for each result. These just need to be unnormalized log probabilities.
//Illegal move filtering happens later.
for(int y = 0; y<boardYSize; y++) {
for(int x = 0; x<boardXSize; x++) {
int pos = NNPos::xyToPos(x,y,nnXLen);
policyProbs[pos] = (float)rand.nextGaussian();
}
}
policyProbs[NNPos::locToPos(Board::PASS_LOC,boardXSize,nnXLen,nnYLen)] = (float)rand.nextGaussian();
resultBuf->result->nnXLen = nnXLen;
resultBuf->result->nnYLen = nnYLen;
if(resultBuf->includeOwnerMap) {
float* whiteOwnerMap = new float[nnXLen*nnYLen];
for(int i = 0; i<nnXLen*nnYLen; i++)
whiteOwnerMap[i] = 0.0;
for(int y = 0; y<boardYSize; y++) {
for(int x = 0; x<boardXSize; x++) {
int pos = NNPos::xyToPos(x,y,nnXLen);
whiteOwnerMap[pos] = (float)rand.nextGaussian() * 0.20f;
}
}
resultBuf->result->whiteOwnerMap = whiteOwnerMap;
}
else {
resultBuf->result->whiteOwnerMap = NULL;
}
//These aren't really probabilities. Win/Loss/NoResult will get softmaxed later
double whiteWinProb = 0.0 + rand.nextGaussian() * 0.20;
double whiteLossProb = 0.0 + rand.nextGaussian() * 0.20;
double whiteScoreMean = 0.0 + rand.nextGaussian() * 0.20;
double whiteScoreMeanSq = 0.0 + rand.nextGaussian() * 0.20;
double whiteNoResultProb = 0.0 + rand.nextGaussian() * 0.20;
double varTimeLeft = 0.5 * boardXSize * boardYSize;
resultBuf->result->whiteWinProb = (float)whiteWinProb;
resultBuf->result->whiteLossProb = (float)whiteLossProb;
resultBuf->result->whiteNoResultProb = (float)whiteNoResultProb;
resultBuf->result->whiteScoreMean = (float)whiteScoreMean;
resultBuf->result->whiteScoreMeanSq = (float)whiteScoreMeanSq;
resultBuf->result->whiteLead = (float)whiteScoreMean;
resultBuf->result->varTimeLeft = (float)varTimeLeft;
resultBuf->result->shorttermWinlossError = 0.0f;
resultBuf->result->shorttermScoreError = 0.0f;
resultBuf->result->policyOptimismUsed = (float)resultBuf->policyOptimism;
resultBuf->hasResult = true;
resultBuf->clientWaitingForResult.notify_all();
resultLock.unlock();
}
}
else {
outputBuf.clear();
for(int row = 0; row<numRows; row++) {
NNOutput* emptyOutput = new NNOutput();
assert(resultBufs[row] != NULL);
emptyOutput->nnXLen = nnXLen;
emptyOutput->nnYLen = nnYLen;
if(resultBufs[row]->includeOwnerMap)
emptyOutput->whiteOwnerMap = new float[nnXLen*nnYLen];
else
emptyOutput->whiteOwnerMap = NULL;
outputBuf.push_back(emptyOutput);
}
for(int row = 0; row<numRows; row++) {
if(resultBufs[row]->symmetry == NNInputs::SYMMETRY_NOTSPECIFIED) {
if(doRandomize)
resultBufs[row]->symmetry = rand.nextUInt(SymmetryHelpers::NUM_SYMMETRIES);
else {
assert(defaultSymmetry >= 0 && defaultSymmetry <= SymmetryHelpers::NUM_SYMMETRIES-1);
resultBufs[row]->symmetry = defaultSymmetry;
}
}
}
NeuralNet::getOutput(gpuHandle, buf.inputBuffers, numRows, resultBufs.data(), outputBuf);
assert(outputBuf.size() == numRows);
m_numRowsProcessed.fetch_add(numRows, std::memory_order_relaxed);
m_numBatchesProcessed.fetch_add(1, std::memory_order_relaxed);
numRowsHandledThisThread += numRows;
numBatchesHandledThisThread += 1;
for(int row = 0; row < numRows; row++) {
assert(resultBufs[row] != NULL);
NNResultBuf* resultBuf = resultBufs[row];
resultBufs[row] = NULL;
unique_lock<std::mutex> resultLock(resultBuf->resultMutex);
assert(resultBuf->hasResult == false);
resultBuf->result = std::shared_ptr<NNOutput>(outputBuf[row]);
resultBuf->hasResult = true;
resultBuf->clientWaitingForResult.notify_all();
resultLock.unlock();
}
}
//Lock and update stats before looping again
lock.lock();
numOngoingEvals -= numRows;
if(numWaitingEvals > 0) {
numEvalsToAwaken += numWaitingEvals;
numWaitingEvals = 0;
waitingForFinish.notify_all();
}
lock.unlock();
continue;
}
NeuralNet::freeComputeHandle(gpuHandle);
if(logger != NULL) {
logger->write(
"GPU " + Global::intToString(gpuIdxForThisThread) + " finishing, processed " +
Global::int64ToString(numRowsHandledThisThread) + " rows " +
Global::int64ToString(numBatchesHandledThisThread) + " batches"
);
}
}
void NNEvaluator::waitForNextNNEvalIfAny() {
unique_lock<std::mutex> lock(bufferMutex);
if(numOngoingEvals <= 0)
return;
numWaitingEvals++;
while(numEvalsToAwaken <= 0 && !isKilled)
waitingForFinish.wait(lock);
numEvalsToAwaken--;
}
static double softPlus(double x) {
//Avoid blowup
if(x > 40.0)
return x;
else
return log(1.0 + exp(x));
}
static const int daggerPattern[9][8] = {
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,2,1,0,0,0,0},
{0,0,2,1,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,2,1,0,0,0,0,0},
{0,3,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
};
static bool daggerMatch(const Board& board, Player nextPla, Loc& banned, int symmetry) {
for(int yi = 0; yi < 9; yi++) {
for(int xi = 0; xi < 8; xi++) {
int y = yi;
int x = xi;
if((symmetry & 0x1) != 0)
std::swap(x,y);
if((symmetry & 0x2) != 0)
x = board.x_size-1-x;
if((symmetry & 0x4) != 0)
y = board.y_size-1-y;
Loc loc = Location::getLoc(x,y,board.x_size);
int m = daggerPattern[yi][xi];
if(m == 0 && board.colors[loc] != C_EMPTY)
return false;
if(m == 1 && board.colors[loc] != nextPla)
return false;
if(m == 2 && board.colors[loc] != getOpp(nextPla))
return false;
if(m == 3)
banned = loc;
}
}
return true;
}
std::shared_ptr<NNOutput>* NNEvaluator::averageMultipleSymmetries(
Board& board,
const BoardHistory& history,
Player nextPlayer,
const SGFMetadata* sgfMeta,
const MiscNNInputParams& baseNNInputParams,
NNResultBuf& buf,
bool includeOwnerMap,
Rand& rand,
int numSymmetriesToSample
) {
MiscNNInputParams nnInputParams = baseNNInputParams;
vector<std::shared_ptr<NNOutput>> ptrs;
std::array<int, SymmetryHelpers::NUM_SYMMETRIES> symmetryIndexes;
std::iota(symmetryIndexes.begin(), symmetryIndexes.end(), 0);
for(int i = 0; i<numSymmetriesToSample; i++) {
std::swap(symmetryIndexes[i], symmetryIndexes[rand.nextInt(i,SymmetryHelpers::NUM_SYMMETRIES-1)]);
nnInputParams.symmetry = symmetryIndexes[i];
bool skipCacheThisIteration = true; //Skip cache since there's no guarantee which symmetry is in the cache
evaluate(
board, history, nextPlayer, sgfMeta,
nnInputParams,
buf, skipCacheThisIteration, includeOwnerMap
);
ptrs.push_back(std::move(buf.result));
}
return new std::shared_ptr<NNOutput>(new NNOutput(ptrs));
}
void NNEvaluator::evaluate(
Board& board,
const BoardHistory& history,
Player nextPlayer,
const MiscNNInputParams& nnInputParams,
NNResultBuf& buf,
bool skipCache,
bool includeOwnerMap
) {
evaluate(
board,
history,
nextPlayer,
NULL,
nnInputParams,
buf,
skipCache,
includeOwnerMap
);
}
void NNEvaluator::evaluate(
Board& board,
const BoardHistory& history,
Player nextPlayer,
const SGFMetadata* sgfMeta,
const MiscNNInputParams& nnInputParamsArg,
NNResultBuf& buf,
bool skipCache,
bool includeOwnerMap
) {
assert(!isKilled);
buf.hasResult = false;
if(board.x_size > nnXLen || board.y_size > nnYLen)
throw StringError("NNEvaluator was configured with nnXLen = " + Global::intToString(nnXLen) +
" nnYLen = " + Global::intToString(nnYLen) +
" but was asked to evaluate board with larger x or y size");
if(requireExactNNLen) {
if(board.x_size != nnXLen || board.y_size != nnYLen)
throw StringError("NNEvaluator was configured with nnXLen = " + Global::intToString(nnXLen) +
" nnYLen = " + Global::intToString(nnYLen) +
" and requireExactNNLen, but was asked to evaluate board with different x or y size");
}
// Avoid using policy optimism for humanSL
MiscNNInputParams nnInputParams = nnInputParamsArg;
if(numInputMetaChannels > 0)
nnInputParams.policyOptimism = 0.0;
Hash128 nnHash = NNInputs::getHash(board, history, nextPlayer, nnInputParams);
if(numInputMetaChannels > 0) {
if(sgfMeta == NULL)
Global::fatalError("SGFMetadata is required for " + modelName + " but was not provided");
if(!sgfMeta->initialized)
Global::fatalError("SGFMetadata is required for " + modelName + " but was not initialized. Did you specify humanSLProfile=... in katago's config or via overrides?");
nnHash ^= sgfMeta->getHash(nextPlayer);
}
bool hadResultWithoutOwnerMap = false;
shared_ptr<NNOutput> resultWithoutOwnerMap;
if(nnCacheTable != NULL && !skipCache && nnCacheTable->get(nnHash,buf.result)) {
if(!(includeOwnerMap && buf.result->whiteOwnerMap == NULL))
{
buf.hasResult = true;
return;
}
else {
hadResultWithoutOwnerMap = true;
resultWithoutOwnerMap = std::move(buf.result);
buf.result = nullptr;
}
}
buf.includeOwnerMap = includeOwnerMap;
buf.boardXSizeForServer = board.x_size;
buf.boardYSizeForServer = board.y_size;
if(!debugSkipNeuralNet) {
const int rowSpatialLen = NNModelVersion::getNumSpatialFeatures(modelVersion) * nnXLen * nnYLen;
if(buf.rowSpatialBuf.size() < rowSpatialLen)
buf.rowSpatialBuf.resize(rowSpatialLen);
const int rowGlobalLen = NNModelVersion::getNumGlobalFeatures(modelVersion);
if(buf.rowGlobalBuf.size() < rowGlobalLen)
buf.rowGlobalBuf.resize(rowGlobalLen);
const int rowMetaLen = numInputMetaChannels;
if(buf.rowMetaBuf.size() < rowMetaLen)
buf.rowMetaBuf.resize(rowMetaLen);
static_assert(NNModelVersion::latestInputsVersionImplemented == 7, "");
if(inputsVersion == 3)
NNInputs::fillRowV3(board, history, nextPlayer, nnInputParams, nnXLen, nnYLen, inputsUseNHWC, buf.rowSpatialBuf.data(), buf.rowGlobalBuf.data());
else if(inputsVersion == 4)
NNInputs::fillRowV4(board, history, nextPlayer, nnInputParams, nnXLen, nnYLen, inputsUseNHWC, buf.rowSpatialBuf.data(), buf.rowGlobalBuf.data());
else if(inputsVersion == 5)
NNInputs::fillRowV5(board, history, nextPlayer, nnInputParams, nnXLen, nnYLen, inputsUseNHWC, buf.rowSpatialBuf.data(), buf.rowGlobalBuf.data());
else if(inputsVersion == 6)
NNInputs::fillRowV6(board, history, nextPlayer, nnInputParams, nnXLen, nnYLen, inputsUseNHWC, buf.rowSpatialBuf.data(), buf.rowGlobalBuf.data());
else if(inputsVersion == 7)
NNInputs::fillRowV7(board, history, nextPlayer, nnInputParams, nnXLen, nnYLen, inputsUseNHWC, buf.rowSpatialBuf.data(), buf.rowGlobalBuf.data());
else
ASSERT_UNREACHABLE;
if(rowMetaLen > 0) {
if(sgfMeta == NULL)
Global::fatalError("SGFMetadata is required for " + modelName + " but was not provided");
if(!sgfMeta->initialized)
Global::fatalError("SGFMetadata is required for " + modelName + " but was not initialized. Did you specify humanSLProfile=... in katago's config or via overrides?");
SGFMetadata::fillMetadataRow(
sgfMeta,
buf.rowMetaBuf.data(),
nextPlayer,
board.x_size*board.y_size
);
buf.hasRowMeta = true;
}
else {
buf.hasRowMeta = false;
}
}
buf.symmetry = nnInputParams.symmetry;
buf.policyOptimism = nnInputParams.policyOptimism;
unique_lock<std::mutex> lock(bufferMutex);
numOngoingEvals += 1;
lock.unlock();
bool suc = queryQueue.forcePush(&buf);
assert(suc);
unique_lock<std::mutex> resultLock(buf.resultMutex);
while(!buf.hasResult)
buf.clientWaitingForResult.wait(resultLock);
resultLock.unlock();
//Perform postprocessing on the result - turn the nn output into probabilities
//As a hack though, if the only thing we were missing was the ownermap, just grab the old policy and values
//and use those. This avoids recomputing in a randomly different orientation when we just need the ownermap
//and causing policy weights to be different, which would reduce performance of successive searches in a game
//by making the successive searches distribute their playouts less coherently and using the cache more poorly.
if(hadResultWithoutOwnerMap) {
buf.result->whiteWinProb = resultWithoutOwnerMap->whiteWinProb;
buf.result->whiteLossProb = resultWithoutOwnerMap->whiteLossProb;
buf.result->whiteNoResultProb = resultWithoutOwnerMap->whiteNoResultProb;
buf.result->whiteScoreMean = resultWithoutOwnerMap->whiteScoreMean;
buf.result->whiteScoreMeanSq = resultWithoutOwnerMap->whiteScoreMeanSq;
buf.result->whiteLead = resultWithoutOwnerMap->whiteLead;
buf.result->varTimeLeft = resultWithoutOwnerMap->varTimeLeft;
buf.result->shorttermWinlossError = resultWithoutOwnerMap->shorttermWinlossError;
buf.result->shorttermScoreError = resultWithoutOwnerMap->shorttermScoreError;
std::copy(resultWithoutOwnerMap->policyProbs, resultWithoutOwnerMap->policyProbs + NNPos::MAX_NN_POLICY_SIZE, buf.result->policyProbs);
buf.result->policyOptimismUsed = (float)resultWithoutOwnerMap->policyOptimismUsed;
buf.result->nnXLen = resultWithoutOwnerMap->nnXLen;
buf.result->nnYLen = resultWithoutOwnerMap->nnYLen;
assert(buf.result->whiteOwnerMap != NULL);
}
else {
float* policy = buf.result->policyProbs;
float policyOutputScaling = postProcessParams.outputScaleMultiplier / nnInputParams.nnPolicyTemperature;
int xSize = board.x_size;
int ySize = board.y_size;
float maxPolicy = -1e25f;
bool isLegal[NNPos::MAX_NN_POLICY_SIZE];
int legalCount = 0;
assert(nextPlayer == history.presumedNextMovePla);
for(int i = 0; i<policySize; i++) {
Loc loc = NNPos::posToLoc(i,xSize,ySize,nnXLen,nnYLen);
isLegal[i] = history.isLegal(board,loc,nextPlayer);
}
if(nnInputParams.avoidMYTDaggerHack && xSize >= 13 && ySize >= 13) {
for(int symmetry = 0; symmetry < 8; symmetry++) {
Loc banned = Board::NULL_LOC;
if(daggerMatch(board, nextPlayer, banned, symmetry)) {
if(banned != Board::NULL_LOC) {
isLegal[NNPos::locToPos(banned,xSize,nnXLen,nnYLen)] = false;
}
}
}
}
for(int i = 0; i<policySize; i++) {
float policyValue;
if(isLegal[i]) {
legalCount += 1;
policyValue = policy[i] * policyOutputScaling;
}
else
policyValue = -1e30f;
policy[i] = policyValue;
if(policyValue > maxPolicy)
maxPolicy = policyValue;
}
assert(legalCount > 0);
float policySum = 0.0f;
if(nnInputParams.enablePassingHacks) {
//Cap passing prior policy at 95% (19x other moves)
float maxPassPolicySumFactor = 19.0f;
for(int i = 0; i<policySize-1; i++) {
policy[i] = exp(policy[i] - maxPolicy);
policySum += policy[i];
}
int passPos = NNPos::locToPos(Board::PASS_LOC, xSize, nnXLen, nnYLen);
assert(passPos == policySize-1);
int i = passPos;
policy[i] = std::max(1e-20f, std::min(exp(policy[i] - maxPolicy), policySum * maxPassPolicySumFactor));
policySum += policy[i];
}
else {
for(int i = 0; i<policySize; i++) {
policy[i] = exp(policy[i] - maxPolicy);
policySum += policy[i];
}
}
if(!isfinite(policySum)) {
cout << "Got nonfinite for policy sum" << endl;
history.printDebugInfo(cout,board);
throw StringError("Got nonfinite for policy sum");
}
//Somehow all legal moves rounded to 0 probability
if(policySum <= 0.0) {
if(!buf.errorLogLockout && logger != NULL) {
buf.errorLogLockout = true;
logger->write("Warning: all legal moves rounded to 0 probability for " + string(modelFileName));
}
float uniform = 1.0f / legalCount;
for(int i = 0; i<policySize; i++) {
policy[i] = isLegal[i] ? uniform : -1.0f;
}
}
//Normal case
else {
for(int i = 0; i<policySize; i++)
policy[i] = isLegal[i] ? (policy[i] / policySum) : -1.0f;
}
//Fill everything out-of-bounds too, for robustness.
for(int i = policySize; i<NNPos::MAX_NN_POLICY_SIZE; i++)
policy[i] = -1.0f;
buf.result->policyOptimismUsed = (float)nnInputParams.policyOptimism;
//Fix up the value as well. Note that the neural net gives us back the value from the perspective
//of the player so we need to negate that to make it the white value.
if(modelVersion == 3) {
const double twoOverPi = 0.63661977236758134308;
double winProb;
double lossProb;
double noResultProb;
//Version 3 neural nets just pack the pre-arctanned scoreValue into the whiteScoreMean field
double scoreValue = atan(buf.result->whiteScoreMean * postProcessParams.outputScaleMultiplier) * twoOverPi;
{
double winLogits = buf.result->whiteWinProb * postProcessParams.outputScaleMultiplier;
double lossLogits = buf.result->whiteLossProb * postProcessParams.outputScaleMultiplier;
double noResultLogits = buf.result->whiteNoResultProb * postProcessParams.outputScaleMultiplier;
//Softmax
double maxLogits = std::max(std::max(winLogits,lossLogits),noResultLogits);
winProb = exp(winLogits - maxLogits);
lossProb = exp(lossLogits - maxLogits);
noResultProb = exp(noResultLogits - maxLogits);
double probSum = winProb + lossProb + noResultProb;
winProb /= probSum;
lossProb /= probSum;
noResultProb /= probSum;
if(!isfinite(probSum) || !isfinite(scoreValue)) {
cout << "Got nonfinite for nneval value" << endl;
cout << winLogits << " " << lossLogits << " " << noResultLogits << " " << scoreValue << endl;
throw StringError("Got nonfinite for nneval value");
}
}
if(nextPlayer == P_WHITE) {
buf.result->whiteWinProb = (float)winProb;
buf.result->whiteLossProb = (float)lossProb;
buf.result->whiteNoResultProb = (float)noResultProb;
buf.result->whiteScoreMean = (float)ScoreValue::approxWhiteScoreOfScoreValueSmooth(scoreValue,0.0,2.0,board.sqrtBoardArea());
buf.result->whiteScoreMeanSq = buf.result->whiteScoreMean * buf.result->whiteScoreMean;
buf.result->whiteLead = buf.result->whiteScoreMean;