forked from official-stockfish/nnue-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
training_data_loader.cpp
1107 lines (937 loc) · 34.8 KB
/
training_data_loader.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
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 <iostream>
#include <memory>
#include <string>
#include <algorithm>
#include <iterator>
#include <future>
#include <mutex>
#include <thread>
#include <deque>
#include <random>
#include "lib/nnue_training_data_formats.h"
#include "lib/nnue_training_data_stream.h"
#include "lib/rng.h"
#if defined (__x86_64__)
#define EXPORT
#define CDECL
#else
#if defined (_MSC_VER)
#define EXPORT __declspec(dllexport)
#define CDECL __cdecl
#else
#define EXPORT
#define CDECL __attribute__ ((__cdecl__))
#endif
#endif
using namespace binpack;
using namespace chess;
static Square orient(Color color, Square sq)
{
if (color == Color::White)
{
return sq;
}
else
{
// IMPORTANT: for now we use rotate180 instead of rank flip
// for compatibility with the stockfish master branch.
// Note that this is inconsistent with nodchip/master.
return sq.flippedVertically().flippedHorizontally();
}
}
static Square orient_flip(Color color, Square sq)
{
if (color == Color::White)
{
return sq;
}
else
{
return sq.flippedVertically();
}
}
struct HalfKP {
static constexpr int NUM_SQ = 64;
static constexpr int NUM_PT = 10;
static constexpr int NUM_PLANES = (NUM_SQ * NUM_PT + 1);
static constexpr int INPUTS = NUM_PLANES * NUM_SQ;
static constexpr int MAX_ACTIVE_FEATURES = 32;
static int feature_index(Color color, Square ksq, Square sq, Piece p)
{
auto p_idx = static_cast<int>(p.type()) * 2 + (p.color() != color);
return 1 + static_cast<int>(orient(color, sq)) + p_idx * NUM_SQ + static_cast<int>(ksq) * NUM_PLANES;
}
static std::pair<int, int> fill_features_sparse(const TrainingDataEntry& e, int* features, float* values, Color color)
{
auto& pos = e.pos;
auto pieces = pos.piecesBB() & ~(pos.piecesBB(Piece(PieceType::King, Color::White)) | pos.piecesBB(Piece(PieceType::King, Color::Black)));
auto ksq = pos.kingSquare(color);
// We order the features so that the resulting sparse
// tensor is coalesced.
int j = 0;
for(Square sq : pieces)
{
auto p = pos.pieceAt(sq);
values[j] = 1.0f;
features[j] = feature_index(color, orient(color, ksq), sq, p);
++j;
}
return { j, INPUTS };
}
};
struct HalfKPFactorized {
// Factorized features
static constexpr int K_INPUTS = HalfKP::NUM_SQ;
static constexpr int PIECE_INPUTS = HalfKP::NUM_SQ * HalfKP::NUM_PT;
static constexpr int INPUTS = HalfKP::INPUTS + K_INPUTS + PIECE_INPUTS;
static constexpr int MAX_K_FEATURES = 1;
static constexpr int MAX_PIECE_FEATURES = 32;
static constexpr int MAX_ACTIVE_FEATURES = HalfKP::MAX_ACTIVE_FEATURES + MAX_K_FEATURES + MAX_PIECE_FEATURES;
static std::pair<int, int> fill_features_sparse(const TrainingDataEntry& e, int* features, float* values, Color color)
{
auto [start_j, offset] = HalfKP::fill_features_sparse(e, features, values, color);
int j = start_j;
auto& pos = e.pos;
{
// king square factor
auto ksq = pos.kingSquare(color);
features[j] = offset + static_cast<int>(orient(color, ksq));
values[j] = static_cast<float>(start_j);
++j;
}
offset += K_INPUTS;
auto pieces = pos.piecesBB() & ~(pos.piecesBB(Piece(PieceType::King, Color::White)) | pos.piecesBB(Piece(PieceType::King, Color::Black)));
// We order the features so that the resulting sparse
// tensor is coalesced. Note that we can just sort
// the parts where values are all 1.0f and leave the
// halfk feature where it was.
for(Square sq : pieces)
{
auto p = pos.pieceAt(sq);
auto p_idx = static_cast<int>(p.type()) * 2 + (p.color() != color);
values[j] = 1.0f;
features[j] = offset + (p_idx * HalfKP::NUM_SQ) + static_cast<int>(orient(color, sq));
++j;
}
return { j, INPUTS };
}
};
struct HalfKA {
static constexpr int NUM_SQ = 64;
static constexpr int NUM_PT = 12;
static constexpr int NUM_PLANES = (NUM_SQ * NUM_PT + 1);
static constexpr int INPUTS = NUM_PLANES * NUM_SQ;
static constexpr int MAX_ACTIVE_FEATURES = 32;
static int feature_index(Color color, Square ksq, Square sq, Piece p)
{
auto p_idx = static_cast<int>(p.type()) * 2 + (p.color() != color);
return 1 + static_cast<int>(orient_flip(color, sq)) + p_idx * NUM_SQ + static_cast<int>(ksq) * NUM_PLANES;
}
static std::pair<int, int> fill_features_sparse(const TrainingDataEntry& e, int* features, float* values, Color color)
{
auto& pos = e.pos;
auto pieces = pos.piecesBB();
auto ksq = pos.kingSquare(color);
int j = 0;
for(Square sq : pieces)
{
auto p = pos.pieceAt(sq);
values[j] = 1.0f;
features[j] = feature_index(color, orient_flip(color, ksq), sq, p);
++j;
}
return { j, INPUTS };
}
};
struct HalfKAFactorized {
// Factorized features
static constexpr int PIECE_INPUTS = HalfKA::NUM_SQ * HalfKA::NUM_PT;
static constexpr int INPUTS = HalfKA::INPUTS + PIECE_INPUTS;
static constexpr int MAX_PIECE_FEATURES = 32;
static constexpr int MAX_ACTIVE_FEATURES = HalfKA::MAX_ACTIVE_FEATURES + MAX_PIECE_FEATURES;
static std::pair<int, int> fill_features_sparse(const TrainingDataEntry& e, int* features, float* values, Color color)
{
const auto [start_j, offset] = HalfKA::fill_features_sparse(e, features, values, color);
auto& pos = e.pos;
auto pieces = pos.piecesBB();
int j = start_j;
for(Square sq : pieces)
{
auto p = pos.pieceAt(sq);
auto p_idx = static_cast<int>(p.type()) * 2 + (p.color() != color);
values[j] = 1.0f;
features[j] = offset + (p_idx * HalfKA::NUM_SQ) + static_cast<int>(orient_flip(color, sq));
++j;
}
return { j, INPUTS };
}
};
struct HalfKAv2 {
static constexpr int NUM_SQ = 64;
static constexpr int NUM_PT = 11;
static constexpr int NUM_PLANES = NUM_SQ * NUM_PT;
static constexpr int INPUTS = NUM_PLANES * NUM_SQ;
static constexpr int MAX_ACTIVE_FEATURES = 32;
static int feature_index(Color color, Square ksq, Square sq, Piece p)
{
auto p_idx = static_cast<int>(p.type()) * 2 + (p.color() != color);
if (p_idx == 11)
--p_idx; // pack the opposite king into the same NUM_SQ * NUM_SQ
return static_cast<int>(orient_flip(color, sq)) + p_idx * NUM_SQ + static_cast<int>(ksq) * NUM_PLANES;
}
static std::pair<int, int> fill_features_sparse(const TrainingDataEntry& e, int* features, float* values, Color color)
{
auto& pos = e.pos;
auto pieces = pos.piecesBB();
auto ksq = pos.kingSquare(color);
int j = 0;
for(Square sq : pieces)
{
auto p = pos.pieceAt(sq);
values[j] = 1.0f;
features[j] = feature_index(color, orient_flip(color, ksq), sq, p);
++j;
}
return { j, INPUTS };
}
};
struct HalfKAv2Factorized {
// Factorized features
static constexpr int NUM_PT = 12;
static constexpr int PIECE_INPUTS = HalfKAv2::NUM_SQ * NUM_PT;
static constexpr int INPUTS = HalfKAv2::INPUTS + PIECE_INPUTS;
static constexpr int MAX_PIECE_FEATURES = 32;
static constexpr int MAX_ACTIVE_FEATURES = HalfKAv2::MAX_ACTIVE_FEATURES + MAX_PIECE_FEATURES;
static std::pair<int, int> fill_features_sparse(const TrainingDataEntry& e, int* features, float* values, Color color)
{
const auto [start_j, offset] = HalfKAv2::fill_features_sparse(e, features, values, color);
auto& pos = e.pos;
auto pieces = pos.piecesBB();
int j = start_j;
for(Square sq : pieces)
{
auto p = pos.pieceAt(sq);
auto p_idx = static_cast<int>(p.type()) * 2 + (p.color() != color);
values[j] = 1.0f;
features[j] = offset + (p_idx * HalfKAv2::NUM_SQ) + static_cast<int>(orient_flip(color, sq));
++j;
}
return { j, INPUTS };
}
};
// ksq must not be oriented
static Square orient_flip_2(Color color, Square sq, Square ksq)
{
bool h = ksq.file() < fileE;
if (color == Color::Black)
sq = sq.flippedVertically();
if (h)
sq = sq.flippedHorizontally();
return sq;
}
struct HalfKAv2_hm {
static constexpr int NUM_SQ = 64;
static constexpr int NUM_PT = 11;
static constexpr int NUM_PLANES = NUM_SQ * NUM_PT;
static constexpr int INPUTS = NUM_PLANES * NUM_SQ / 2;
static constexpr int MAX_ACTIVE_FEATURES = 32;
static constexpr int KingBuckets[64] = {
-1, -1, -1, -1, 31, 30, 29, 28,
-1, -1, -1, -1, 27, 26, 25, 24,
-1, -1, -1, -1, 23, 22, 21, 20,
-1, -1, -1, -1, 19, 18, 17, 16,
-1, -1, -1, -1, 15, 14, 13, 12,
-1, -1, -1, -1, 11, 10, 9, 8,
-1, -1, -1, -1, 7, 6, 5, 4,
-1, -1, -1, -1, 3, 2, 1, 0
};
static int feature_index(Color color, Square ksq, Square sq, Piece p)
{
Square o_ksq = orient_flip_2(color, ksq, ksq);
auto p_idx = static_cast<int>(p.type()) * 2 + (p.color() != color);
if (p_idx == 11)
--p_idx; // pack the opposite king into the same NUM_SQ * NUM_SQ
return static_cast<int>(orient_flip_2(color, sq, ksq)) + p_idx * NUM_SQ + KingBuckets[static_cast<int>(o_ksq)] * NUM_PLANES;
}
static std::pair<int, int> fill_features_sparse(const TrainingDataEntry& e, int* features, float* values, Color color)
{
auto& pos = e.pos;
auto pieces = pos.piecesBB();
auto ksq = pos.kingSquare(color);
int j = 0;
for(Square sq : pieces)
{
auto p = pos.pieceAt(sq);
values[j] = 1.0f;
features[j] = feature_index(color, ksq, sq, p);
++j;
}
return { j, INPUTS };
}
};
struct HalfKAv2_hmFactorized {
// Factorized features
static constexpr int NUM_PT = 12;
static constexpr int PIECE_INPUTS = HalfKAv2_hm::NUM_SQ * NUM_PT;
static constexpr int INPUTS = HalfKAv2_hm::INPUTS + PIECE_INPUTS;
static constexpr int MAX_PIECE_FEATURES = 32;
static constexpr int MAX_ACTIVE_FEATURES = HalfKAv2_hm::MAX_ACTIVE_FEATURES + MAX_PIECE_FEATURES;
static std::pair<int, int> fill_features_sparse(const TrainingDataEntry& e, int* features, float* values, Color color)
{
const auto [start_j, offset] = HalfKAv2_hm::fill_features_sparse(e, features, values, color);
auto& pos = e.pos;
auto pieces = pos.piecesBB();
auto ksq = pos.kingSquare(color);
int j = start_j;
for(Square sq : pieces)
{
auto p = pos.pieceAt(sq);
auto p_idx = static_cast<int>(p.type()) * 2 + (p.color() != color);
values[j] = 1.0f;
features[j] = offset + (p_idx * HalfKAv2_hm::NUM_SQ) + static_cast<int>(orient_flip_2(color, sq, ksq));
++j;
}
return { j, INPUTS };
}
};
template <typename T, typename... Ts>
struct FeatureSet
{
static_assert(sizeof...(Ts) == 0, "Currently only one feature subset supported.");
static constexpr int INPUTS = T::INPUTS;
static constexpr int MAX_ACTIVE_FEATURES = T::MAX_ACTIVE_FEATURES;
static std::pair<int, int> fill_features_sparse(const TrainingDataEntry& e, int* features, float* values, Color color)
{
return T::fill_features_sparse(e, features, values, color);
}
};
struct SparseBatch
{
static constexpr bool IS_BATCH = true;
template <typename... Ts>
SparseBatch(FeatureSet<Ts...>, const std::vector<TrainingDataEntry>& entries)
{
num_inputs = FeatureSet<Ts...>::INPUTS;
size = entries.size();
is_white = new float[size];
outcome = new float[size];
score = new float[size];
sharpness = new float[size];
white = new int[size * FeatureSet<Ts...>::MAX_ACTIVE_FEATURES];
black = new int[size * FeatureSet<Ts...>::MAX_ACTIVE_FEATURES];
white_values = new float[size * FeatureSet<Ts...>::MAX_ACTIVE_FEATURES];
black_values = new float[size * FeatureSet<Ts...>::MAX_ACTIVE_FEATURES];
psqt_indices = new int[size];
layer_stack_indices = new int[size];
num_active_white_features = 0;
num_active_black_features = 0;
max_active_features = FeatureSet<Ts...>::MAX_ACTIVE_FEATURES;
for (std::size_t i = 0; i < size * FeatureSet<Ts...>::MAX_ACTIVE_FEATURES; ++i)
white[i] = -1;
for (std::size_t i = 0; i < size * FeatureSet<Ts...>::MAX_ACTIVE_FEATURES; ++i)
black[i] = -1;
for (std::size_t i = 0; i < size * FeatureSet<Ts...>::MAX_ACTIVE_FEATURES; ++i)
white_values[i] = 0.0f;
for (std::size_t i = 0; i < size * FeatureSet<Ts...>::MAX_ACTIVE_FEATURES; ++i)
black_values[i] = 0.0f;
for(int i = 0; i < entries.size(); ++i)
{
fill_entry(FeatureSet<Ts...>{}, i, entries[i]);
}
}
int num_inputs;
int size;
float* is_white;
float* outcome;
float* score;
float* sharpness;
int num_active_white_features;
int num_active_black_features;
int max_active_features;
int* white;
int* black;
float* white_values;
float* black_values;
int* psqt_indices;
int* layer_stack_indices;
~SparseBatch()
{
delete[] is_white;
delete[] outcome;
delete[] score;
delete[] sharpness;
delete[] white;
delete[] black;
delete[] white_values;
delete[] black_values;
delete[] psqt_indices;
delete[] layer_stack_indices;
}
private:
template <typename... Ts>
void fill_entry(FeatureSet<Ts...>, int i, const TrainingDataEntry& e)
{
is_white[i] = static_cast<float>(e.pos.sideToMove() == Color::White);
outcome[i] = (e.result + 1.0f) / 2.0f;
score[i] = e.score;
sharpness[i] = e.sharpness;
psqt_indices[i] = (e.pos.piecesBB().count() - 1) / 4;
layer_stack_indices[i] = psqt_indices[i];
fill_features(FeatureSet<Ts...>{}, i, e);
}
template <typename... Ts>
void fill_features(FeatureSet<Ts...>, int i, const TrainingDataEntry& e)
{
const int offset = i * FeatureSet<Ts...>::MAX_ACTIVE_FEATURES;
num_active_white_features +=
FeatureSet<Ts...>::fill_features_sparse(e, white + offset, white_values + offset, Color::White)
.first;
num_active_black_features +=
FeatureSet<Ts...>::fill_features_sparse(e, black + offset, black_values + offset, Color::Black)
.first;
}
};
struct AnyStream
{
virtual ~AnyStream() = default;
};
template <typename StorageT>
struct Stream : AnyStream
{
using StorageType = StorageT;
Stream(int concurrency, const std::vector<std::string>& filenames, bool cyclic, std::function<bool(const TrainingDataEntry&)> skipPredicate) :
m_stream(training_data::open_sfen_input_file_parallel(concurrency, filenames, cyclic, skipPredicate))
{
}
virtual StorageT* next() = 0;
protected:
std::unique_ptr<training_data::BasicSfenInputStream> m_stream;
};
template <typename StorageT>
struct AsyncStream : Stream<StorageT>
{
using BaseType = Stream<StorageT>;
AsyncStream(int concurrency, const std::vector<std::string>& filenames, bool cyclic, std::function<bool(const TrainingDataEntry&)> skipPredicate) :
BaseType(1, filenames, cyclic, skipPredicate)
{
}
~AsyncStream()
{
if (m_next.valid())
{
delete m_next.get();
}
}
protected:
std::future<StorageT*> m_next;
};
template <typename FeatureSetT, typename StorageT>
struct FeaturedBatchStream : Stream<StorageT>
{
static_assert(StorageT::IS_BATCH);
using FeatureSet = FeatureSetT;
using BaseType = Stream<StorageT>;
static constexpr int num_feature_threads_per_reading_thread = 2;
FeaturedBatchStream(int concurrency, const std::vector<std::string>& filenames, int batch_size, bool cyclic, std::function<bool(const TrainingDataEntry&)> skipPredicate) :
BaseType(
std::max(
1,
concurrency / num_feature_threads_per_reading_thread
),
filenames,
cyclic,
skipPredicate
),
m_concurrency(concurrency),
m_batch_size(batch_size)
{
m_stop_flag.store(false);
auto worker = [this]()
{
std::vector<TrainingDataEntry> entries;
entries.reserve(m_batch_size);
while(!m_stop_flag.load())
{
entries.clear();
{
std::unique_lock lock(m_stream_mutex);
BaseType::m_stream->fill(entries, m_batch_size);
if (entries.empty())
{
break;
}
}
auto batch = new StorageT(FeatureSet{}, entries);
{
std::unique_lock lock(m_batch_mutex);
m_batches_not_full.wait(lock, [this]() { return m_batches.size() < m_concurrency + 1 || m_stop_flag.load(); });
m_batches.emplace_back(batch);
lock.unlock();
m_batches_any.notify_one();
}
}
m_num_workers.fetch_sub(1);
m_batches_any.notify_one();
};
const int num_feature_threads = std::max(
1,
concurrency - std::max(1, concurrency / num_feature_threads_per_reading_thread)
);
for (int i = 0; i < num_feature_threads; ++i)
{
m_workers.emplace_back(worker);
// This cannot be done in the thread worker. We need
// to have a guarantee that this is incremented, but if
// we did it in the worker there's no guarantee
// that it executed.
m_num_workers.fetch_add(1);
}
}
StorageT* next() override
{
std::unique_lock lock(m_batch_mutex);
m_batches_any.wait(lock, [this]() { return !m_batches.empty() || m_num_workers.load() == 0; });
if (!m_batches.empty())
{
auto batch = m_batches.front();
m_batches.pop_front();
lock.unlock();
m_batches_not_full.notify_one();
return batch;
}
return nullptr;
}
~FeaturedBatchStream()
{
m_stop_flag.store(true);
m_batches_not_full.notify_all();
for (auto& worker : m_workers)
{
if (worker.joinable())
{
worker.join();
}
}
for (auto& batch : m_batches)
{
delete batch;
}
}
private:
int m_batch_size;
int m_concurrency;
std::deque<StorageT*> m_batches;
std::mutex m_batch_mutex;
std::mutex m_stream_mutex;
std::condition_variable m_batches_not_full;
std::condition_variable m_batches_any;
std::atomic_bool m_stop_flag;
std::atomic_int m_num_workers;
std::vector<std::thread> m_workers;
};
// Very simple fixed size string wrapper with a stable ABI to pass to python.
struct Fen
{
Fen() :
m_fen(nullptr)
{
}
Fen(const std::string& fen) :
m_size(fen.size()),
m_fen(new char[fen.size() + 1])
{
std::memcpy(m_fen, fen.c_str(), fen.size() + 1);
}
Fen& operator=(const std::string& fen)
{
if (m_fen != nullptr)
{
delete m_fen;
}
m_size = fen.size();
m_fen = new char[fen.size() + 1];
std::memcpy(m_fen, fen.c_str(), fen.size() + 1);
return *this;
}
~Fen()
{
delete[] m_fen;
}
private:
int m_size;
char* m_fen;
};
struct FenBatch
{
FenBatch(const std::vector<TrainingDataEntry>& entries) :
m_size(entries.size()),
m_fens(new Fen[entries.size()])
{
for (int i = 0; i < m_size; ++i)
{
m_fens[i] = entries[i].pos.fen();
}
}
~FenBatch()
{
delete[] m_fens;
}
private:
int m_size;
Fen* m_fens;
};
struct FenBatchStream : Stream<FenBatch>
{
static constexpr int num_feature_threads_per_reading_thread = 2;
using BaseType = Stream<FenBatch>;
FenBatchStream(int concurrency, const std::vector<std::string>& filenames, int batch_size, bool cyclic, std::function<bool(const TrainingDataEntry&)> skipPredicate) :
BaseType(
std::max(
1,
concurrency / num_feature_threads_per_reading_thread
),
filenames,
cyclic,
skipPredicate
),
m_concurrency(concurrency),
m_batch_size(batch_size)
{
m_stop_flag.store(false);
auto worker = [this]()
{
std::vector<TrainingDataEntry> entries;
entries.reserve(m_batch_size);
while(!m_stop_flag.load())
{
entries.clear();
{
std::unique_lock lock(m_stream_mutex);
BaseType::m_stream->fill(entries, m_batch_size);
if (entries.empty())
{
break;
}
}
auto batch = new FenBatch(entries);
{
std::unique_lock lock(m_batch_mutex);
m_batches_not_full.wait(lock, [this]() { return m_batches.size() < m_concurrency + 1 || m_stop_flag.load(); });
m_batches.emplace_back(batch);
lock.unlock();
m_batches_any.notify_one();
}
}
m_num_workers.fetch_sub(1);
m_batches_any.notify_one();
};
const int num_feature_threads = std::max(
1,
concurrency - std::max(1, concurrency / num_feature_threads_per_reading_thread)
);
for (int i = 0; i < num_feature_threads; ++i)
{
m_workers.emplace_back(worker);
// This cannot be done in the thread worker. We need
// to have a guarantee that this is incremented, but if
// we did it in the worker there's no guarantee
// that it executed.
m_num_workers.fetch_add(1);
}
}
FenBatch* next()
{
std::unique_lock lock(m_batch_mutex);
m_batches_any.wait(lock, [this]() { return !m_batches.empty() || m_num_workers.load() == 0; });
if (!m_batches.empty())
{
auto batch = m_batches.front();
m_batches.pop_front();
lock.unlock();
m_batches_not_full.notify_one();
return batch;
}
return nullptr;
}
~FenBatchStream()
{
m_stop_flag.store(true);
m_batches_not_full.notify_all();
for (auto& worker : m_workers)
{
if (worker.joinable())
{
worker.join();
}
}
for (auto& batch : m_batches)
{
delete batch;
}
}
private:
int m_batch_size;
int m_concurrency;
std::deque<FenBatch*> m_batches;
std::mutex m_batch_mutex;
std::mutex m_stream_mutex;
std::condition_variable m_batches_not_full;
std::condition_variable m_batches_any;
std::atomic_bool m_stop_flag;
std::atomic_int m_num_workers;
std::vector<std::thread> m_workers;
};
std::function<bool(const TrainingDataEntry&)> make_skip_predicate(bool filtered, int random_fen_skipping, bool wld_filtered, int early_fen_skipping, int param_index)
{
if (filtered || random_fen_skipping || wld_filtered || early_fen_skipping)
{
return [
random_fen_skipping,
prob = double(random_fen_skipping) / (random_fen_skipping + 1),
filtered,
wld_filtered,
early_fen_skipping
](const TrainingDataEntry& e){
// VALUE_NONE from Stockfish.
// We need to allow a way to skip predetermined positions without
// having to remove them from the dataset, as otherwise the we lose some
// compression ability.
static constexpr int VALUE_NONE = 32002;
static constexpr int VALUE_MAX_SHARPNESS = 255;
static constexpr double desired_piece_count_weights[33] = {
1.000000,
1.121094, 1.234375, 1.339844, 1.437500, 1.527344, 1.609375, 1.683594, 1.750000,
1.808594, 1.859375, 1.902344, 1.937500, 1.964844, 1.984375, 1.996094, 2.000000,
1.996094, 1.984375, 1.964844, 1.937500, 1.902344, 1.859375, 1.808594, 1.750000,
1.683594, 1.609375, 1.527344, 1.437500, 1.339844, 1.234375, 1.121094, 1.000000
};
static constexpr double desired_piece_count_weights_total = [](){
double tot = 0;
for (auto w : desired_piece_count_weights)
tot += w;
return tot;
}();
static thread_local std::mt19937 gen(std::random_device{}());
// keep stats on passing pieces
static thread_local double alpha = 1;
static thread_local double piece_count_history_all[33] = {0};
static thread_local double piece_count_history_passed[33] = {0};
static thread_local double piece_count_history_all_total = 0;
static thread_local double piece_count_history_passed_total = 0;
// max skipping rate
static constexpr double max_skipping_rate = 10.0;
auto do_wld_skip = [&]() {
std::bernoulli_distribution distrib(1.0 - e.score_result_prob());
auto& prng = rng::get_thread_local_rng();
return distrib(prng);
};
auto do_skip = [&]() {
std::bernoulli_distribution distrib(prob);
auto& prng = rng::get_thread_local_rng();
return distrib(prng);
};
auto do_filter = [&]() {
return (e.isCapturingMove() || e.isInCheck());
};
// Allow for predermined filtering without the need to remove positions from the dataset.
if (e.score == VALUE_NONE)
return true;
if (e.sharpness == 0 || e.sharpness >= VALUE_MAX_SHARPNESS)
return true;
if (e.ply <= early_fen_skipping)
return true;
if (random_fen_skipping && do_skip())
return true;
if (filtered && do_filter())
return true;
if (wld_filtered && do_wld_skip())
return true;
constexpr bool do_debug_print = false;
if (do_debug_print) {
if (uint64_t(piece_count_history_all_total) % 10000 == 0) {
std::cout << "Total : " << piece_count_history_all_total << '\n';
std::cout << "Passed: " << piece_count_history_passed_total << '\n';
for (int i = 0; i < 33; ++i)
std::cout << i << ' ' << piece_count_history_passed[i] << '\n';
}
}
const int pc = e.pos.piecesBB().count();
piece_count_history_all[pc] += 1;
piece_count_history_all_total += 1;
// update alpha, which scales the filtering probability, to a maximum rate.
if (uint64_t(piece_count_history_all_total) % 10000 == 0) {
double pass = piece_count_history_all_total * desired_piece_count_weights_total;
for (int i = 0; i < 33; ++i)
{
if (desired_piece_count_weights[pc] > 0)
{
double tmp = piece_count_history_all_total * desired_piece_count_weights[pc] /
(desired_piece_count_weights_total * piece_count_history_all[pc]);
if (tmp < pass)
pass = tmp;
}
}
alpha = 1.0 / (pass * max_skipping_rate);
}
double tmp = alpha * piece_count_history_all_total * desired_piece_count_weights[pc] /
(desired_piece_count_weights_total * piece_count_history_all[pc]);
tmp = std::min(1.0, tmp);
std::bernoulli_distribution distrib(1.0 - tmp);
auto& prng = rng::get_thread_local_rng();
if (distrib(prng))
return true;
piece_count_history_passed[pc] += 1;
piece_count_history_passed_total += 1;
return false;
};
}
return nullptr;
}
extern "C" {
EXPORT SparseBatch* get_sparse_batch_from_fens(
const char* feature_set_c,
int num_fens,
const char* const* fens,
int* scores,
int* sharpnesses,
int* plies,
int* results
)
{
std::vector<TrainingDataEntry> entries;
entries.reserve(num_fens);
for (int i = 0; i < num_fens; ++i)
{
auto& e = entries.emplace_back();
e.pos = Position::fromFen(fens[i]);
movegen::forEachLegalMove(e.pos, [&](Move m){e.move = m;});
e.score = scores[i];
e.sharpness = sharpnesses[i];
e.ply = plies[i];
e.result = results[i];
}
std::string_view feature_set(feature_set_c);
if (feature_set == "HalfKP")
{
return new SparseBatch(FeatureSet<HalfKP>{}, entries);
}
else if (feature_set == "HalfKP^")
{
return new SparseBatch(FeatureSet<HalfKPFactorized>{}, entries);
}
else if (feature_set == "HalfKA")
{
return new SparseBatch(FeatureSet<HalfKA>{}, entries);
}
else if (feature_set == "HalfKA^")
{
return new SparseBatch(FeatureSet<HalfKAFactorized>{}, entries);
}
else if (feature_set == "HalfKAv2")
{
return new SparseBatch(FeatureSet<HalfKAv2>{}, entries);
}
else if (feature_set == "HalfKAv2^")
{
return new SparseBatch(FeatureSet<HalfKAv2Factorized>{}, entries);
}
else if (feature_set == "HalfKAv2_hm")
{
return new SparseBatch(FeatureSet<HalfKAv2_hm>{}, entries);
}
else if (feature_set == "HalfKAv2_hm^")
{