-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBotzone.cpp
More file actions
1276 lines (1123 loc) · 38.3 KB
/
Copy pathBotzone.cpp
File metadata and controls
1276 lines (1123 loc) · 38.3 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
#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
#include <immintrin.h>
#include "jsoncpp/json.h"
enum
{
SIMD_FACTOR = 8,
COLS_PER_LOOP = 3,
COLS_STEPS_PER_CORE = 4,
SIMD_ELEM_PEC_COL = COLS_PER_LOOP * COLS_STEPS_PER_CORE,
bb_nCols = SIMD_ELEM_PEC_COL * SIMD_FACTOR,
bb_nRows = 35,
cc_nRows = 32,
};
struct noncblas_sgemm_prm_t
{
int M;
int lda;
int ldc;
float alpha;
__m256 bb[SIMD_ELEM_PEC_COL * bb_nRows];
__m256 cc[cc_nRows * SIMD_ELEM_PEC_COL];
};
static void avx256_noncblas_sgemm_core(
const noncblas_sgemm_prm_t *pPrm,
const float *A,
float *C)
{
int lda = pPrm->lda;
int ldc = pPrm->ldc;
int m;
for (m = 0; m < pPrm->M - 1; A += lda * 2, C += ldc * 2, m += 2)
{
float *Crow0 = C;
float *Crow1 = C + ldc;
for (int n = 0; n < SIMD_ELEM_PEC_COL; n += COLS_PER_LOOP)
{
const __m256 *Bcol = &pPrm->bb[n];
__m256 a0 = _mm256_broadcast_ss(&A[0]);
__m256 a1 = _mm256_broadcast_ss(&A[lda]);
__m256 b;
b = Bcol[0];
__m256 acc00 = _mm256_mul_ps(a0, b);
__m256 acc01 = _mm256_mul_ps(a1, b);
b = Bcol[1];
__m256 acc10 = _mm256_mul_ps(a0, b);
__m256 acc11 = _mm256_mul_ps(a1, b);
b = Bcol[2];
__m256 acc20 = _mm256_mul_ps(a0, b);
__m256 acc21 = _mm256_mul_ps(a1, b);
for (int k = 1; k < bb_nRows; k += 2)
{
Bcol += SIMD_ELEM_PEC_COL;
a0 = _mm256_broadcast_ss(&A[k]);
a1 = _mm256_broadcast_ss(&A[k + lda]);
b = Bcol[0];
acc00 = _mm256_add_ps(acc00, _mm256_mul_ps(a0, b));
acc01 = _mm256_add_ps(acc01, _mm256_mul_ps(a1, b));
b = Bcol[1];
acc10 = _mm256_add_ps(acc10, _mm256_mul_ps(a0, b));
acc11 = _mm256_add_ps(acc11, _mm256_mul_ps(a1, b));
b = Bcol[2];
acc20 = _mm256_add_ps(acc20, _mm256_mul_ps(a0, b));
acc21 = _mm256_add_ps(acc21, _mm256_mul_ps(a1, b));
Bcol += SIMD_ELEM_PEC_COL;
a0 = _mm256_broadcast_ss(&A[k + 1]);
a1 = _mm256_broadcast_ss(&A[k + lda + 1]);
b = Bcol[0];
acc00 = _mm256_add_ps(acc00, _mm256_mul_ps(a0, b));
acc01 = _mm256_add_ps(acc01, _mm256_mul_ps(a1, b));
b = Bcol[1];
acc10 = _mm256_add_ps(acc10, _mm256_mul_ps(a0, b));
acc11 = _mm256_add_ps(acc11, _mm256_mul_ps(a1, b));
b = Bcol[2];
acc20 = _mm256_add_ps(acc20, _mm256_mul_ps(a0, b));
acc21 = _mm256_add_ps(acc21, _mm256_mul_ps(a1, b));
}
__m256 alpha_ps = _mm256_broadcast_ss(&pPrm->alpha);
_mm256_storeu_ps(&Crow0[SIMD_FACTOR * 0], _mm256_add_ps(_mm256_mul_ps(acc00, alpha_ps), _mm256_loadu_ps(&Crow0[SIMD_FACTOR * 0])));
_mm256_storeu_ps(&Crow0[SIMD_FACTOR * 1], _mm256_add_ps(_mm256_mul_ps(acc10, alpha_ps), _mm256_loadu_ps(&Crow0[SIMD_FACTOR * 1])));
_mm256_storeu_ps(&Crow0[SIMD_FACTOR * 2], _mm256_add_ps(_mm256_mul_ps(acc20, alpha_ps), _mm256_loadu_ps(&Crow0[SIMD_FACTOR * 2])));
_mm256_storeu_ps(&Crow1[SIMD_FACTOR * 0], _mm256_add_ps(_mm256_mul_ps(acc01, alpha_ps), _mm256_loadu_ps(&Crow1[SIMD_FACTOR * 0])));
_mm256_storeu_ps(&Crow1[SIMD_FACTOR * 1], _mm256_add_ps(_mm256_mul_ps(acc11, alpha_ps), _mm256_loadu_ps(&Crow1[SIMD_FACTOR * 1])));
_mm256_storeu_ps(&Crow1[SIMD_FACTOR * 2], _mm256_add_ps(_mm256_mul_ps(acc21, alpha_ps), _mm256_loadu_ps(&Crow1[SIMD_FACTOR * 2])));
Crow0 += COLS_PER_LOOP * SIMD_FACTOR;
Crow1 += COLS_PER_LOOP * SIMD_FACTOR;
}
}
if (m < pPrm->M)
{
float *Crow0 = C;
for (int n = 0; n < SIMD_ELEM_PEC_COL; n += COLS_PER_LOOP)
{
const __m256 *Bcol = &pPrm->bb[n];
__m256 acc00 = _mm256_setzero_ps();
__m256 acc10 = _mm256_setzero_ps();
__m256 acc20 = _mm256_setzero_ps();
for (int k = 0; k < bb_nRows; ++k)
{
__m256 a0 = _mm256_broadcast_ss(&A[k]);
__m256 b;
b = Bcol[0];
acc00 = _mm256_add_ps(acc00, _mm256_mul_ps(a0, b));
b = Bcol[1];
acc10 = _mm256_add_ps(acc10, _mm256_mul_ps(a0, b));
b = Bcol[2];
acc20 = _mm256_add_ps(acc20, _mm256_mul_ps(a0, b));
Bcol += SIMD_ELEM_PEC_COL;
}
__m256 alpha_ps = _mm256_broadcast_ss(&pPrm->alpha);
_mm256_storeu_ps(&Crow0[SIMD_FACTOR * 0], _mm256_add_ps(_mm256_mul_ps(acc00, alpha_ps), _mm256_loadu_ps(&Crow0[SIMD_FACTOR * 0])));
_mm256_storeu_ps(&Crow0[SIMD_FACTOR * 1], _mm256_add_ps(_mm256_mul_ps(acc10, alpha_ps), _mm256_loadu_ps(&Crow0[SIMD_FACTOR * 1])));
_mm256_storeu_ps(&Crow0[SIMD_FACTOR * 2], _mm256_add_ps(_mm256_mul_ps(acc20, alpha_ps), _mm256_loadu_ps(&Crow0[SIMD_FACTOR * 2])));
Crow0 += COLS_PER_LOOP * SIMD_FACTOR;
}
}
}
static void avx256_noncblas_sgemm_core_bottomRows(
const noncblas_sgemm_prm_t *pPrm,
const float *A,
float *C,
int nRows)
{
int lda = pPrm->lda;
int ldc = pPrm->ldc;
int m;
for (m = 0; m < pPrm->M - 1; A += lda * 2, C += ldc * 2, m += 2)
{
float *Crow0 = C;
float *Crow1 = C + ldc;
for (int n = 0; n < SIMD_ELEM_PEC_COL; n += COLS_PER_LOOP)
{
const __m256 *Bcol = &pPrm->bb[n];
__m256 acc00 = _mm256_setzero_ps();
__m256 acc01 = _mm256_setzero_ps();
__m256 acc10 = _mm256_setzero_ps();
__m256 acc11 = _mm256_setzero_ps();
__m256 acc20 = _mm256_setzero_ps();
__m256 acc21 = _mm256_setzero_ps();
for (int k = 0; k < nRows; ++k)
{
__m256 a0 = _mm256_broadcast_ss(&A[k]);
__m256 a1 = _mm256_broadcast_ss(&A[k + lda]);
__m256 b;
b = Bcol[0];
acc00 = _mm256_add_ps(acc00, _mm256_mul_ps(a0, b));
acc01 = _mm256_add_ps(acc01, _mm256_mul_ps(a1, b));
b = Bcol[1];
acc10 = _mm256_add_ps(acc10, _mm256_mul_ps(a0, b));
acc11 = _mm256_add_ps(acc11, _mm256_mul_ps(a1, b));
b = Bcol[2];
acc20 = _mm256_add_ps(acc20, _mm256_mul_ps(a0, b));
acc21 = _mm256_add_ps(acc21, _mm256_mul_ps(a1, b));
Bcol += SIMD_ELEM_PEC_COL;
}
__m256 alpha_ps = _mm256_broadcast_ss(&pPrm->alpha);
_mm256_storeu_ps(&Crow0[SIMD_FACTOR * 0], _mm256_add_ps(_mm256_mul_ps(acc00, alpha_ps), _mm256_loadu_ps(&Crow0[SIMD_FACTOR * 0])));
_mm256_storeu_ps(&Crow0[SIMD_FACTOR * 1], _mm256_add_ps(_mm256_mul_ps(acc10, alpha_ps), _mm256_loadu_ps(&Crow0[SIMD_FACTOR * 1])));
_mm256_storeu_ps(&Crow0[SIMD_FACTOR * 2], _mm256_add_ps(_mm256_mul_ps(acc20, alpha_ps), _mm256_loadu_ps(&Crow0[SIMD_FACTOR * 2])));
_mm256_storeu_ps(&Crow1[SIMD_FACTOR * 0], _mm256_add_ps(_mm256_mul_ps(acc01, alpha_ps), _mm256_loadu_ps(&Crow1[SIMD_FACTOR * 0])));
_mm256_storeu_ps(&Crow1[SIMD_FACTOR * 1], _mm256_add_ps(_mm256_mul_ps(acc11, alpha_ps), _mm256_loadu_ps(&Crow1[SIMD_FACTOR * 1])));
_mm256_storeu_ps(&Crow1[SIMD_FACTOR * 2], _mm256_add_ps(_mm256_mul_ps(acc21, alpha_ps), _mm256_loadu_ps(&Crow1[SIMD_FACTOR * 2])));
Crow0 += COLS_PER_LOOP * SIMD_FACTOR;
Crow1 += COLS_PER_LOOP * SIMD_FACTOR;
}
}
if (m < pPrm->M)
{
float *Crow0 = C;
for (int n = 0; n < SIMD_ELEM_PEC_COL; n += COLS_PER_LOOP)
{
const __m256 *Bcol = &pPrm->bb[n];
__m256 acc00 = _mm256_setzero_ps();
__m256 acc10 = _mm256_setzero_ps();
__m256 acc20 = _mm256_setzero_ps();
for (int k = 0; k < nRows; ++k)
{
__m256 a0 = _mm256_broadcast_ss(&A[k]);
__m256 b;
b = Bcol[0];
acc00 = _mm256_add_ps(acc00, _mm256_mul_ps(a0, b));
b = Bcol[1];
acc10 = _mm256_add_ps(acc10, _mm256_mul_ps(a0, b));
b = Bcol[2];
acc20 = _mm256_add_ps(acc20, _mm256_mul_ps(a0, b));
Bcol += SIMD_ELEM_PEC_COL;
}
__m256 alpha_ps = _mm256_broadcast_ss(&pPrm->alpha);
_mm256_storeu_ps(&Crow0[SIMD_FACTOR * 0], _mm256_add_ps(_mm256_mul_ps(acc00, alpha_ps), _mm256_loadu_ps(&Crow0[SIMD_FACTOR * 0])));
_mm256_storeu_ps(&Crow0[SIMD_FACTOR * 1], _mm256_add_ps(_mm256_mul_ps(acc10, alpha_ps), _mm256_loadu_ps(&Crow0[SIMD_FACTOR * 1])));
_mm256_storeu_ps(&Crow0[SIMD_FACTOR * 2], _mm256_add_ps(_mm256_mul_ps(acc20, alpha_ps), _mm256_loadu_ps(&Crow0[SIMD_FACTOR * 2])));
Crow0 += COLS_PER_LOOP * SIMD_FACTOR;
}
}
}
static void avx256_noncblas_sgemm_core_rightmostColumns(
noncblas_sgemm_prm_t *pPrm,
const float *A,
float *C,
int nCols, // 0 < nCols < bb_nCols
int nRows) // nRows <= bb_nRows
{
int lda = pPrm->lda;
int ldc = pPrm->ldc;
int ldcc = ((nCols - 1) / (COLS_PER_LOOP * SIMD_FACTOR) + 1) * COLS_PER_LOOP;
for (int m0 = 0; m0 < pPrm->M; m0 += cc_nRows)
{
int mLast = m0 + cc_nRows <= pPrm->M ? m0 + cc_nRows : pPrm->M;
// calculate partial results and store in cc
__m256 *pCc = pPrm->cc;
int mLastEv = mLast & (-2);
for (int m = m0; m < mLastEv; A += lda * 2, m += 2)
{
for (int n = 0; n < ldcc; n += COLS_PER_LOOP)
{
const __m256 *Bcol = &pPrm->bb[n];
__m256 acc00 = _mm256_setzero_ps();
__m256 acc01 = _mm256_setzero_ps();
__m256 acc10 = _mm256_setzero_ps();
__m256 acc11 = _mm256_setzero_ps();
__m256 acc20 = _mm256_setzero_ps();
__m256 acc21 = _mm256_setzero_ps();
for (int k = 0; k < nRows; ++k)
{
__m256 a0 = _mm256_broadcast_ss(&A[k]);
__m256 a1 = _mm256_broadcast_ss(&A[k + lda]);
__m256 b;
b = Bcol[0];
acc00 = _mm256_add_ps(acc00, _mm256_mul_ps(a0, b));
acc01 = _mm256_add_ps(acc01, _mm256_mul_ps(a1, b));
b = Bcol[1];
acc10 = _mm256_add_ps(acc10, _mm256_mul_ps(a0, b));
acc11 = _mm256_add_ps(acc11, _mm256_mul_ps(a1, b));
b = Bcol[2];
acc20 = _mm256_add_ps(acc20, _mm256_mul_ps(a0, b));
acc21 = _mm256_add_ps(acc21, _mm256_mul_ps(a1, b));
Bcol += SIMD_ELEM_PEC_COL;
}
__m256 alpha_ps = _mm256_broadcast_ss(&pPrm->alpha);
pCc[0] = _mm256_mul_ps(acc00, alpha_ps);
pCc[1] = _mm256_mul_ps(acc10, alpha_ps);
pCc[2] = _mm256_mul_ps(acc20, alpha_ps);
pCc[ldcc + 0] = _mm256_mul_ps(acc01, alpha_ps);
pCc[ldcc + 1] = _mm256_mul_ps(acc11, alpha_ps);
pCc[ldcc + 2] = _mm256_mul_ps(acc21, alpha_ps);
pCc += COLS_PER_LOOP;
}
pCc += ldcc;
}
if ((mLast & 1) != 0)
{
// last row of A
for (int n = 0; n < ldcc; n += COLS_PER_LOOP)
{
const __m256 *Bcol = &pPrm->bb[n];
__m256 acc00 = _mm256_setzero_ps();
__m256 acc10 = _mm256_setzero_ps();
__m256 acc20 = _mm256_setzero_ps();
for (int k = 0; k < nRows; ++k)
{
__m256 a0 = _mm256_broadcast_ss(&A[k]);
__m256 b;
b = Bcol[0];
acc00 = _mm256_add_ps(acc00, _mm256_mul_ps(a0, b));
b = Bcol[1];
acc10 = _mm256_add_ps(acc10, _mm256_mul_ps(a0, b));
b = Bcol[2];
acc20 = _mm256_add_ps(acc20, _mm256_mul_ps(a0, b));
Bcol += SIMD_ELEM_PEC_COL;
}
__m256 alpha_ps = _mm256_broadcast_ss(&pPrm->alpha);
pCc[0] = _mm256_mul_ps(acc00, alpha_ps);
pCc[1] = _mm256_mul_ps(acc10, alpha_ps);
pCc[2] = _mm256_mul_ps(acc20, alpha_ps);
pCc += COLS_PER_LOOP;
}
}
// add partial result in cc to C
pCc = pPrm->cc;
for (int m = 0; m < mLast - m0; C += ldc, pCc += ldcc, ++m)
{
const float *res = (const float *)pCc;
for (int n = 0; n < nCols; ++n)
C[n] += res[n];
}
}
}
static void avx256_noncblas_sgemm_multC(
int M, int N,
float beta,
float *C, int ldc)
{
if (beta != 0)
{
for (int m = 0; m < M; ++m)
{
for (int n = 0; n < N; ++n)
C[n] *= beta;
C += ldc;
}
}
else
{
for (int m = 0; m < M; ++m)
{
for (int n = 0; n < N; ++n)
C[n] = 0;
C += ldc;
}
}
}
void avx256_noncblas_sgemm(
int M, int N, int K,
float alpha,
const float *A, int lda,
const float *B, int ldb,
float beta,
float *C, int ldc)
{
avx256_noncblas_sgemm_multC(M, N, beta, C, ldc);
noncblas_sgemm_prm_t prm;
prm.M = M;
prm.lda = lda;
prm.ldc = ldc;
prm.alpha = alpha;
int n_Rsteps = K / bb_nRows;
int n_Csteps = N / bb_nCols;
int row = 0;
for (int ri = 0; ri < n_Rsteps; ++ri)
{
int col = 0;
for (int ci = 0; ci < n_Csteps; ++ci)
{
// process full rectangles
const float *bSrc = &B[row * ldb + col];
for (int i = 0; i < bb_nRows; ++i)
{
memcpy(&prm.bb[SIMD_ELEM_PEC_COL * i], bSrc, bb_nCols * sizeof(*B));
bSrc += ldb;
}
avx256_noncblas_sgemm_core(&prm, &A[row], &C[col]);
col += bb_nCols;
}
if (col < N)
{
// process rightmost rectangle of the full-height band
const float *bSrc = &B[row * ldb + col];
for (int i = 0; i < bb_nRows; ++i)
{
memcpy(&prm.bb[SIMD_ELEM_PEC_COL * i], bSrc, (N - col) * sizeof(*B));
bSrc += ldb;
}
avx256_noncblas_sgemm_core_rightmostColumns(&prm, &A[row], &C[col], N - col, bb_nRows);
}
row += bb_nRows;
}
if (row < K)
{
// bottom band
int col = 0;
for (int ci = 0; ci < n_Csteps; ++ci)
{
// process full-width rectangles
const float *bSrc = &B[row * ldb + col];
for (int i = 0; i < K - row; ++i)
{
memcpy(&prm.bb[SIMD_ELEM_PEC_COL * i], bSrc, bb_nCols * sizeof(*B));
bSrc += ldb;
}
avx256_noncblas_sgemm_core_bottomRows(&prm, &A[row], &C[col], K - row);
col += bb_nCols;
}
if (col < N)
{
// process bottom-right corner rectangle
const float *bSrc = &B[row * ldb + col];
for (int i = 0; i < K - row; ++i)
{
memcpy(&prm.bb[SIMD_ELEM_PEC_COL * i], bSrc, (N - col) * sizeof(*B));
bSrc += ldb;
}
avx256_noncblas_sgemm_core_rightmostColumns(&prm, &A[row], &C[col], N - col, K - row);
}
}
}
class Layer
{
public:
float *output;
virtual float *forward(float *input) = 0;
};
class ConvolutionalLayer : public Layer
{
public:
int channels;
int filters;
int filter_size;
int width;
int height;
bool normalize;
bool relu;
float *biases;
float *scales;
float *mean;
float *stddev;
float *weights;
float *workspace;
ConvolutionalLayer(int channels, int filters, int filter_size, int width, int height, bool normalize, bool relu, float *workspace, std::ifstream *fp)
{
this->channels = channels;
this->filters = filters;
this->filter_size = filter_size;
this->width = width;
this->height = height;
this->normalize = normalize;
this->relu = relu;
this->workspace = workspace;
load(fp);
}
~ConvolutionalLayer()
{
free();
}
void load(std::ifstream *fp)
{
biases = new float[filters];
scales = new float[filters];
mean = new float[filters];
stddev = new float[filters];
weights = new float[filters * channels * filter_size * filter_size];
output = new float[filters * width * height];
fp->read((char *)biases, sizeof(float) * filters);
if (normalize)
{
fp->read((char *)scales, sizeof(float) * filters);
fp->read((char *)mean, sizeof(float) * filters);
fp->read((char *)stddev, sizeof(float) * filters);
for (int j = 0; j < filters; j++)
stddev[j] = sqrt(stddev[j]) + .000001f;
}
fp->read((char *)weights, sizeof(float) * filters * channels * filter_size * filter_size);
}
void free()
{
delete biases;
delete scales;
delete mean;
delete stddev;
delete weights;
delete output;
}
void im2col(float *data_im,
int channels, int height, int width,
int ksize, int stride, int pad, float *data_col)
{
int height_col = (height + 2 * pad - ksize) / stride + 1;
int width_col = (width + 2 * pad - ksize) / stride + 1;
int channels_col = channels * ksize * ksize;
for (int c = 0; c < channels_col; ++c)
{
int w_offset = c % ksize;
int h_offset = (c / ksize) % ksize;
int c_im = c / ksize / ksize;
for (int h = 0; h < height_col; ++h)
for (int w = 0; w < width_col; ++w)
{
int row = h_offset + h * stride - pad;
int col = w_offset + w * stride - pad;
int col_index = (c * height_col + h) * width_col + w;
if (row >= 0 && col >= 0 && row < height && col < width)
data_col[col_index] = data_im[col + width * (row + height * c_im)];
else
data_col[col_index] = 0;
}
}
}
float *forward(float *input)
{
int m = filters;
int k = filter_size * filter_size * channels;
int n = width * height;
if (filter_size == 1)
{
avx256_noncblas_sgemm(m, n, k, 1, weights, k, input, n, 0, output, n);
}
else
{
im2col(input, channels, height, width, filter_size, 1, 1, workspace);
avx256_noncblas_sgemm(m, n, k, 1, weights, k, workspace, n, 0, output, n);
}
if (normalize)
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
{
int index = i * n + j;
output[index] = (output[index] - mean[i]) / stddev[i] * scales[i];
}
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
output[i * n + j] += biases[i];
if (relu)
for (int i = 0; i < m * n; i++)
output[i] *= output[i] > 0;
return output;
}
};
class ConnectedLayer : public Layer
{
public:
int inputs;
int outputs;
bool normalize;
bool relu;
float *biases;
float *scales;
float *mean;
float *stddev;
float *weights;
float *workspace;
ConnectedLayer(int inputs, int outputs, bool normalize, bool relu, std::ifstream *fp)
{
this->inputs = inputs;
this->outputs = outputs;
this->normalize = normalize;
this->relu = relu;
load(fp);
}
~ConnectedLayer()
{
free();
}
void load(std::ifstream *fp)
{
biases = new float[outputs];
weights = new float[inputs * outputs];
scales = new float[outputs];
mean = new float[outputs];
stddev = new float[outputs];
output = new float[outputs];
fp->read((char *)biases, sizeof(float) * outputs);
fp->read((char *)weights, sizeof(float) * outputs * inputs);
if (normalize)
{
fp->read((char *)scales, sizeof(float) * outputs);
fp->read((char *)mean, sizeof(float) * outputs);
fp->read((char *)stddev, sizeof(float) * outputs);
}
}
void free()
{
delete biases;
delete scales;
delete mean;
delete stddev;
delete weights;
delete output;
}
float *forward(float *input)
{
int n = outputs;
int k = inputs;
memset(output, 0, outputs * sizeof(float));
for (int i = 0; i < n; i++)
for (int j = 0; j < k; j++)
output[i] += input[j] * weights[i * k + j];
if (normalize)
for (int i = 0; i < n; i++)
output[i] = (output[i] - mean[i]) / stddev[i] * scales[i];
for (int i = 0; i < n; i++)
output[i] += biases[i];
if (relu)
for (int i = 0; i < n; i++)
output[i] *= output[i] > 0;
return output;
}
};
class SoftmaxLayer : public Layer
{
public:
int n;
SoftmaxLayer(int n)
{
this->n = n;
load();
}
void load()
{
output = new float[n];
}
void free()
{
delete output;
}
float *forward(float *input)
{
float sum = 0;
float largest = -std::numeric_limits<double>::max();
for (int i = 0; i < n; i++)
largest = std::max(input[i], largest);
for (int i = 0; i < n; i++)
{
float e = std::exp(input[i] - largest);
sum += e;
output[i] = e;
}
for (int i = 0; i < n; i++)
output[i] /= sum;
return output;
}
};
class ShortcutLayer : public Layer
{
public:
Layer *out_layer;
int width;
int height;
int channels;
bool relu;
ShortcutLayer(Layer *out_layer, int width, int height, int channels, bool relu)
{
this->out_layer = out_layer;
this->width = width;
this->height = height;
this->channels = channels;
this->relu = relu;
load();
}
void load()
{
output = new float[width * height * channels];
}
void free()
{
delete output;
}
float *forward(float *input)
{
int w = width;
int h = height;
int c = channels;
for (int k = 0; k < channels; k++)
for (int j = 0; j < height; j++)
for (int i = 0; i < width; i++)
{
int index = i + w * (j + h * k);
output[index] = out_layer->output[index] + input[index];
}
if (relu)
for (int i = 0; i < w * h * c; i++)
output[i] *= output[i] > 0;
return output;
}
};
class Network
{
public:
float *workspace;
float *output;
std::vector<Layer *> layers;
std::vector<Layer *> head_value;
std::vector<Layer *> head_policy;
Network(std::string weight_file)
{
workspace = new float[81 * 9 * 64];
output = new float[81 + 1];
// 加载权重
std::ifstream file;
file.open(weight_file, std::ios::in | std::ios::binary);
file.seekg(20, std::ios::beg);
// 通用层
layers.push_back(new ConvolutionalLayer(6, 64, 3, 9, 9, true, true, workspace, &file));
for (int i = 0; i < 5; i++)
{
layers.push_back(new ConvolutionalLayer(64, 64, 3, 9, 9, true, true, workspace, &file));
layers.push_back(new ConvolutionalLayer(64, 64, 3, 9, 9, true, false, workspace, &file));
layers.push_back(new ShortcutLayer(layers[layers.size() - 3], 9, 9, 64, true));
}
layers.push_back(new ConvolutionalLayer(64, 64, 3, 9, 9, true, true, workspace, &file));
// 估值层
head_value.push_back(new ConvolutionalLayer(64, 1, 1, 9, 9, true, true, workspace, &file));
head_value.push_back(new ConnectedLayer(81, 64, false, true, &file));
head_value.push_back(new ConnectedLayer(64, 1, false, false, &file));
// 策略层
head_policy.push_back(new ConvolutionalLayer(64, 1, 1, 9, 9, false, false, workspace, &file));
head_policy.push_back(new SoftmaxLayer(81));
file.close();
};
~Network()
{
delete workspace;
delete output;
for (auto layer : layers)
delete layer;
for (auto layer : head_value)
delete layer;
for (auto layer : head_policy)
delete layer;
}
float *predict(float *input)
{
// 通用层
float *flow = input;
for (auto layer : layers)
flow = layer->forward(flow);
// 估值层
float *mid = flow;
for (auto layer : head_value)
flow = layer->forward(flow);
output[81] = tanh(*flow);
// 策略层
flow = mid;
for (auto layer : head_policy)
flow = layer->forward(flow);
memcpy(output, flow, sizeof(float) * 81);
return output;
}
};
const int AI = 1; // AI的棋子
const int OP = -1; // 对手的棋子
const int BL = 0; // 空白
class AlphaPig
{
public:
// 调试信息
int steps = 0;
double mcts_value = 0;
// 设置
double search_time = 0.95;
// 棋局状态
int board[81] = {0};
bool air_vis[81];
// 网络
Network *net;
// 蒙特卡洛树节点
struct TreeNode
{
// 是否为新节点
bool is_new = true;
// 棋盘及颜色
int board[81] = {0};
int color;
// 下一步可行位置
std::vector<int> available_last;
std::vector<int> available_next;
// 节点输赢统计
double value = 0;
int total = 0;
// 网络输出
float net_policy[81];
float net_value;
// 父节点
TreeNode *father = nullptr;
// 孩子节点
TreeNode *children[81] = {nullptr};
// 最后一手位置
int last_step = -1;
// 是否先手
bool first;
};
TreeNode *root = nullptr;
// 移动位置
int moveTo(int p, int dir)
{
switch (dir)
{
case 0:
return (p += 9) < 81 ? p : -1;
case 1:
return (p -= 9) >= 0 ? p : -1;
case 2:
return p % 9 < 8 ? p + 1 : -1;
case 3:
return p % 9 > 0 ? p - 1 : -1;
}
return p;
}
// 判断是否有气
bool hasAir(int m_board[], int p)
{
air_vis[p] = true;
bool flag = false;
for (int dir = 0; dir < 4; dir++)
{
int dp = moveTo(p, dir);
if (dp >= 0)
{
if (m_board[dp] == BL)
flag = true;
if (m_board[dp] == m_board[p] && !air_vis[dp])
if (hasAir(m_board, dp))
flag = true;
}
}
return flag;
}
// 判断是否可以下子
bool judgeAvailable(int m_board[], int p, int col)
{
if (m_board[p])
return false;
m_board[p] = col;
memset(air_vis, 0, sizeof(air_vis));
if (!hasAir(m_board, p))
{
m_board[p] = 0;
return false;
}
for (int dir = 0; dir < 4; dir++)
{
int dp = moveTo(p, dir);
if (dp >= 0)
{
if (m_board[dp] && !air_vis[dp])
if (!hasAir(m_board, dp))
{
m_board[p] = 0;
return false;
}
}
}
m_board[p] = 0;
return true;
}
// 扫描可以下子的位置
void scanAvailable(TreeNode *node)
{
int *board = node->board;
bool ban_his[81] = {false}, ban_me[81] = {false}; // 禁下
bool vis[81] = {false};
for (int dir = 0; dir < 4; dir++)
{
int p = moveTo(node->last_step, dir);
if (p < 0)
continue;
if (board[p] == BL)
{
ban_me[p] = !judgeAvailable(board, p, node->color);
ban_his[p] = !judgeAvailable(board, p, -node->color);
}
else if (!vis[p])
{
std::queue<int> queue;
bool tgas_vis[81] = {false};
int tgas = 0, tgas_size = 0;
queue.push(p);
while (!queue.empty())
{
int pq = queue.front();
queue.pop();
vis[pq] = true;
for (int dir = 0; dir < 4; dir++)
{
int dp = moveTo(pq, dir);
if (dp >= 0)
{
if (board[dp] == BL && !tgas_vis[dp])
{
tgas_vis[dp] = true;
tgas_size++;
tgas = dp;
}
else if (board[dp] == board[pq] && !vis[dp])
{
queue.push(dp);
}
}
}
}
if (tgas_size == 1)
{
ban_me[tgas] = !judgeAvailable(board, tgas, node->color);
ban_his[tgas] = !judgeAvailable(board, tgas, -node->color);
}
}
}
for (auto i : node->father->available_last)
if (board[i] == BL && !ban_his[i])