-
Notifications
You must be signed in to change notification settings - Fork 475
Expand file tree
/
Copy pathprimitives.cc
More file actions
1235 lines (1094 loc) · 42.5 KB
/
primitives.cc
File metadata and controls
1235 lines (1094 loc) · 42.5 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 "ctranslate2/primitives.h"
#include <cmath>
#include <functional>
#include <numeric>
#include <stdexcept>
#ifdef CT2_WITH_MKL
# include <mkl.h>
#endif
#ifdef CT2_WITH_DNNL
# include <dnnl.h>
#endif
#ifdef CT2_WITH_ACCELERATE
# include <Accelerate/Accelerate.h>
#endif
#ifdef CT2_WITH_OPENBLAS
# include <cblas.h>
#endif
#ifdef CT2_WITH_RUY
# include <ruy/ruy.h>
#endif
#include "ctranslate2/allocator.h"
#include "cpu/backend.h"
#include "cpu/kernels.h"
#include "cpu/parallel.h"
#include "type_dispatch.h"
namespace ctranslate2 {
static Allocator& allocator = get_allocator<Device::CPU>();
template<>
template <typename T>
T primitives<Device::CPU>::at(const T* x, dim_t index) {
return x[index];
}
template<>
template <typename T>
void primitives<Device::CPU>::fill(T* x, T a, dim_t size) {
std::fill(x, x + size, a);
}
template<>
template <typename T>
void primitives<Device::CPU>::strided_fill(T* x, T a, dim_t inc_x, dim_t size) {
for (dim_t i = 0; i < size; i++, x += inc_x) {
*x = a;
}
}
template<>
template <typename T>
void primitives<Device::CPU>::indexed_fill(T* x, T a, const int32_t* indices, dim_t num_indices) {
for (dim_t i = 0; i < num_indices; ++i)
x[indices[i]] = a;
}
template<>
template <typename T>
void primitives<Device::CPU>::indexed_pointwise_multiply(T* x, const T* values, const int32_t* indices, dim_t num_indices) {
for (dim_t i = 0; i < num_indices; ++i) {
x[indices[i]] = x[indices[i]] * values[i];
}
}
template<>
template <typename T>
void primitives<Device::CPU>::copy(const T* x, T* y, dim_t size) {
std::copy(x, x + size, y);
}
template<>
template <typename U, typename V>
void primitives<Device::CPU>::convert(const U* x, V* y, dim_t size) {
std::copy(x, x + size, y);
}
template void primitives<Device::CPU>::convert(const float*, float16_t*, dim_t);
template void primitives<Device::CPU>::convert(const float16_t*, float*, dim_t);
template void primitives<Device::CPU>::convert(const float*, bfloat16_t*, dim_t);
template void primitives<Device::CPU>::convert(const bfloat16_t*, float*, dim_t);
template void primitives<Device::CPU>::convert(const float16_t*, bfloat16_t*, dim_t);
template void primitives<Device::CPU>::convert(const bfloat16_t*, float16_t*, dim_t);
template<>
template <typename T>
T primitives<Device::CPU>::sum(const T* array, dim_t size) {
auto sum = T(0);
CPU_ISA_DISPATCH((sum = cpu::reduce_sum<ISA>(array, size)));
return sum;
}
template<>
template <typename T>
dim_t primitives<Device::CPU>::max_element(const T* array, dim_t size) {
return std::distance(array, std::max_element(array, array + size));
}
template<>
template <typename T>
T primitives<Device::CPU>::max(const T* array, dim_t size) {
auto max = T(0);
CPU_ISA_DISPATCH((max = cpu::reduce_max<ISA>(array, size)));
return max;
}
template<>
template <typename T>
T primitives<Device::CPU>::amax(const T* array, dim_t size) {
auto max = T(0);
CPU_ISA_DISPATCH((max = cpu::reduce_amax<ISA>(array, size)));
return max;
}
template<>
template<>
float primitives<Device::CPU>::amax(const float* x, dim_t size) {
#ifdef CT2_WITH_MKL
if (cpu::mayiuse_mkl())
return std::abs(x[cblas_isamax(size, x, /*incx=*/1)]);
#endif
float max = 0;
CPU_ISA_DISPATCH((max = cpu::reduce_amax<ISA>(x, size)));
return max;
}
template<>
template <typename T>
void primitives<Device::CPU>::add(T a, const T* x, T* y, dim_t size) {
CPU_ISA_DISPATCH((cpu::add<ISA>(a, x, y, size)));
}
template<>
template <typename T>
void primitives<Device::CPU>::add(const T* a, const T* b, T* c, dim_t size) {
CPU_ISA_DISPATCH((cpu::add<ISA>(a, b, c, size)));
}
template<>
template<>
void primitives<Device::CPU>::add(const float* a, const float* b, float* c, dim_t size) {
#ifdef CT2_WITH_MKL
if (cpu::mayiuse_mkl())
return vsAdd(size, a, b, c);
#endif
CPU_ISA_DISPATCH((cpu::add<ISA>(a, b, c, size)));
}
template<>
template <typename T>
void primitives<Device::CPU>::add_batch_broadcast(const T* a, const T* b, T* c,
dim_t a_size, dim_t b_size) {
const dim_t iter_size = b_size / a_size;
cpu::parallel_for(0, iter_size, 1, [&](dim_t begin, dim_t end) {
for (dim_t i = begin; i < end; ++i) {
const dim_t offset = i * a_size;
add(a, b + offset, c + offset, a_size);
}
});
}
template<>
template <typename T>
void primitives<Device::CPU>::add_depth_broadcast(const T* a, const T* b, T* c,
dim_t a_size, dim_t b_size) {
const dim_t iter_size = a_size;
const dim_t depth = b_size / a_size;
cpu::parallel_for(0, iter_size, 1, [&](dim_t begin, dim_t end) {
for (dim_t i = begin; i < end; ++i) {
const dim_t offset = i * depth;
add(a[i], b + offset, c + offset, depth);
}
});
}
template<>
template <typename T>
void primitives<Device::CPU>::sub(const T* a, const T* b, T* c, dim_t size) {
CPU_ISA_DISPATCH((cpu::sub<ISA>(a, b, c, size)));
}
template<>
template<>
void primitives<Device::CPU>::sub(const float* a, const float* b, float* c, dim_t size) {
#ifdef CT2_WITH_MKL
if (cpu::mayiuse_mkl())
return vsSub(size, a, b, c);
#endif
CPU_ISA_DISPATCH((cpu::sub<ISA>(a, b, c, size)));
}
template<>
template <typename T>
void primitives<Device::CPU>::max(T a, const T* x, T* y, dim_t size){
CPU_ISA_DISPATCH((cpu::max<ISA>(a, x, y, size)));
}
template<>
template <typename T>
void primitives<Device::CPU>::max(const T* a, const T* b, T* c, dim_t size){
CPU_ISA_DISPATCH((cpu::max<ISA>(a, b, c, size)));
}
template<>
template<>
void primitives<Device::CPU>::max(const float* a, const float* b, float* c, dim_t size) {
#ifdef CT2_WITH_MKL
if (cpu::mayiuse_mkl())
return vsFmax(size, a, b, c);
#endif
CPU_ISA_DISPATCH((cpu::max<ISA>(a, b, c, size)));
}
template<>
template <typename T>
void primitives<Device::CPU>::min(T a, const T* x, T* y, dim_t size){
CPU_ISA_DISPATCH((cpu::min<ISA>(a, x, y, size)));
}
template<>
template <typename T>
void primitives<Device::CPU>::min(const T* a, const T* b, T* c, dim_t size){
CPU_ISA_DISPATCH((cpu::min<ISA>(a, b, c, size)));
}
template<>
template<>
void primitives<Device::CPU>::min(const float* a, const float* b, float* c, dim_t size) {
#ifdef CT2_WITH_MKL
if (cpu::mayiuse_mkl())
return vsFmin(size, a, b, c);
#endif
CPU_ISA_DISPATCH((cpu::min<ISA>(a, b, c, size)));
}
template<>
template <typename T>
void primitives<Device::CPU>::mul(T a, const T* x, T* y, dim_t size) {
CPU_ISA_DISPATCH((cpu::mul<ISA>(a, x, y, size)));
}
template<>
template<>
void primitives<Device::CPU>::mul(float a, const float* x, float* y, dim_t size) {
#ifdef CT2_WITH_MKL
if (cpu::mayiuse_mkl())
return cblas_saxpby(size, a, x, 1 /* incx */, 0 /* b */, y, 1 /* incy */);
#endif
CPU_ISA_DISPATCH((cpu::mul<ISA>(a, x, y, size)));
}
template<>
template <typename T>
void primitives<Device::CPU>::mul(const T* a, const T* b, T* c, dim_t size) {
CPU_ISA_DISPATCH((cpu::mul<ISA>(a, b, c, size)));
}
template<>
template<>
void primitives<Device::CPU>::mul(const float* a, const float* b, float* c, dim_t size) {
#ifdef CT2_WITH_MKL
if (cpu::mayiuse_mkl())
return vsMul(size, a, b, c);
#endif
CPU_ISA_DISPATCH((cpu::mul<ISA>(a, b, c, size)));
}
template<>
template <typename T>
void primitives<Device::CPU>::mul_batch_broadcast(const T* a, const T* b, T* c,
dim_t a_size, dim_t b_size) {
const dim_t iter_size = b_size / a_size;
cpu::parallel_for(0, iter_size, 1, [&](dim_t begin, dim_t end) {
for (dim_t i = begin; i < end; ++i) {
const dim_t offset = i * a_size;
mul(a, b + offset, c + offset, a_size);
}
});
}
template<>
template<>
void primitives<Device::CPU>::relu(const float* x, float* y, dim_t size) {
cpu::parallel_for(0, size, cpu::GRAIN_SIZE,
[x, y](dim_t begin, dim_t end) {
max(float(0), x + begin, y + begin, end - begin);
});
}
template<>
template<>
void primitives<Device::CPU>::gelu(const float* x, float* y, dim_t size) {
cpu::parallel_for(0, size, /*grain_size=*/512,
[x, y](dim_t begin, dim_t end) {
CPU_ISA_DISPATCH((cpu::gelu<ISA>(x + begin, y + begin, end - begin)));
});
}
template<>
template<>
void primitives<Device::CPU>::gelu_tanh(const float* x, float* y, dim_t size) {
cpu::parallel_for(0, size, /*grain_size=*/512,
[x, y](dim_t begin, dim_t end) {
CPU_ISA_DISPATCH((cpu::gelu_tanh<ISA>(x + begin, y + begin, end - begin)));
});
}
template<>
template<>
void primitives<Device::CPU>::gelu_sigmoid(const float* x, float* y, dim_t size) {
cpu::parallel_for(0, size, /*grain_size=*/512,
[x, y](dim_t begin, dim_t end) {
CPU_ISA_DISPATCH((cpu::gelu_sigmoid<ISA>(x + begin, y + begin, end - begin)));
});
}
template<>
template<>
void primitives<Device::CPU>::sigmoid(const float* x, float* y, dim_t size) {
cpu::parallel_for(0, size, cpu::GRAIN_SIZE / 10,
[x, y](dim_t begin, dim_t end) {
CPU_ISA_DISPATCH((cpu::sigmoid<ISA>(x + begin, y + begin, end - begin)));
});
}
template<>
template<>
void primitives<Device::CPU>::swish(const float* x, float* y, dim_t size) {
cpu::parallel_for(0, size, cpu::GRAIN_SIZE / 10,
[x, y](dim_t begin, dim_t end) {
CPU_ISA_DISPATCH((cpu::swish<ISA>(x + begin, y + begin, end - begin)));
});
}
template<>
template<>
float primitives<Device::CPU>::logsumexp(const float* x, dim_t size) {
float result = 0;
CPU_ISA_DISPATCH((result = cpu::reduce_logsumexp<ISA>(x, size)));
return result;
}
template<>
template<>
void primitives<Device::CPU>::exp(const float* x, float* y, dim_t size) {
#ifdef CT2_WITH_MKL
if (cpu::mayiuse_mkl())
return vsExp(size, x, y);
#endif
CPU_ISA_DISPATCH((cpu::exp<ISA>(x, y, size)));
}
template<>
template<>
void primitives<Device::CPU>::log(const float* x, float* y, dim_t size) {
#ifdef CT2_WITH_MKL
if (cpu::mayiuse_mkl())
return vsLn(size, x, y);
#endif
CPU_ISA_DISPATCH((cpu::log<ISA>(x, y, size)));
}
template<>
template<>
void primitives<Device::CPU>::cos(const float* x, float* y, dim_t size) {
#ifdef CT2_WITH_MKL
if (cpu::mayiuse_mkl())
return vsCos(size, x, y);
#endif
CPU_ISA_DISPATCH((cpu::cos<ISA>(x, y, size)));
}
template<>
template<>
void primitives<Device::CPU>::sin(const float* x, float* y, dim_t size) {
#ifdef CT2_WITH_MKL
if (cpu::mayiuse_mkl())
return vsSin(size, x, y);
#endif
CPU_ISA_DISPATCH((cpu::sin<ISA>(x, y, size)));
}
template<>
template<>
void primitives<Device::CPU>::tanh(const float* x, float* y, dim_t size) {
#ifdef CT2_WITH_MKL
if (cpu::mayiuse_mkl())
return vsTanh(size, x, y);
#endif
cpu::parallel_for(0, size, /*grain_size=*/512,
[x, y](dim_t begin, dim_t end) {
CPU_ISA_DISPATCH((cpu::tanh<ISA>(x + begin, y + begin, end - begin)));
});
}
template<>
template <typename T>
void primitives<Device::CPU>::penalize_previous_tokens(T* scores,
const T* previous_scores,
const int32_t* previous_ids,
T penalty,
dim_t batch_size,
dim_t length,
dim_t vocabulary_size) {
cpu::parallel_for(0, batch_size, 1, [&](dim_t begin, dim_t end) {
for (dim_t i = begin; i < end; ++i) {
for (dim_t j = 0; j < length; ++j) {
const dim_t read_index = i * length + j;
const dim_t write_index = i * vocabulary_size + previous_ids[read_index];
const auto score = previous_scores[read_index];
scores[write_index] = (score < T(0) ? score * penalty : score / penalty);
}
}
});
}
template<>
void primitives<Device::CPU>::prepare_length_mask(const int32_t* lengths,
dim_t batch_size,
dim_t num_heads,
dim_t num_queries,
bool mask_future,
bool multi_query,
int32_t* mask) {
for (dim_t b = 0; b < batch_size; ++b) {
const auto length = lengths[b];
auto* batch_mask = mask + b * num_heads * num_queries;
for (dim_t i = 0; i < num_heads * num_queries; ++i) {
batch_mask[i] = (mask_future
? std::min(length,
int32_t((multi_query ? i / num_heads : i % num_queries) + 1))
: length);
}
}
}
template<>
template <typename T>
void primitives<Device::CPU>::transpose_2d(const T* a, const dim_t* dims, T* b) {
cpu::parallel_for(0, dims[0], 1, [&](dim_t begin, dim_t end) {
for (dim_t i0 = begin; i0 < end; ++i0) {
for (dim_t i1 = 0; i1 < dims[1]; ++i1) {
b[i1 * dims[0] + i0] = a[i0 * dims[1] + i1];
}
}
});
}
template<>
template <typename T>
void primitives<Device::CPU>::transpose_3d(const T* a,
const dim_t* dims,
const dim_t* perm,
T* b) {
dim_t perm_ind[3];
for (dim_t i = 0; i < 3; ++i)
perm_ind[perm[i]] = i;
const dim_t a_stride[3] = {dims[1] * dims[2], dims[2], 1};
const dim_t b_stride[3] = {dims[perm[1]] * dims[perm[2]], dims[perm[2]], 1};
const dim_t perm_b_stride[3] = {b_stride[perm_ind[0]], b_stride[perm_ind[1]],
b_stride[perm_ind[2]]};
cpu::parallel_for(0, dims[0], 1, [&](dim_t begin, dim_t end) {
for (dim_t i0 = begin; i0 < end; ++i0) {
for (dim_t i1 = 0; i1 < dims[1]; ++i1) {
for (dim_t i2 = 0; i2 < dims[2]; ++i2) {
const dim_t b_i = (i0 * perm_b_stride[0] + i1 * perm_b_stride[1] +
i2 * perm_b_stride[2]);
const dim_t a_i = (i0 * a_stride[0] + i1 * a_stride[1] +
i2 * a_stride[2]);
b[b_i] = a[a_i];
}
}
}
});
}
template<>
template <typename T>
void primitives<Device::CPU>::transpose_4d(const T* a,
const dim_t* dims,
const dim_t* perm,
T* b) {
if (perm[0] == 0 && perm[1] == 2 && perm[2] == 1 && perm[3] == 3) {
// Optimize the permutation used in multi-head attention.
const dim_t r1 = dims[2];
const dim_t r2 = dims[1];
const dim_t depth = dims[3];
cpu::parallel_for(0, dims[0], 1, [&](dim_t begin, dim_t end) {
for (dim_t i = begin; i < end; ++i) {
const dim_t offset = i * r1 * r2;
for (dim_t j = 0; j < r1 * r2; ++j) {
const dim_t a_offset = depth * (offset + j);
const dim_t b_offset = depth * (offset + j / r1 + (j % r1) * r2);
copy(a + a_offset, b + b_offset, depth);
}
}
});
return;
}
dim_t perm_ind[4];
for (dim_t i = 0; i < 4; ++i)
perm_ind[perm[i]] = i;
const dim_t a_stride[4] = {dims[1] * dims[2] * dims[3], dims[2] * dims[3], dims[3], 1};
const dim_t b_stride[4] = {dims[perm[1]] * dims[perm[2]] * dims[perm[3]],
dims[perm[2]] * dims[perm[3]], dims[perm[3]], 1};
const dim_t perm_b_stride[4] = {b_stride[perm_ind[0]], b_stride[perm_ind[1]],
b_stride[perm_ind[2]], b_stride[perm_ind[3]]};
cpu::parallel_for(0, dims[0], 1, [&](dim_t begin, dim_t end) {
for (dim_t i0 = begin; i0 < end; ++i0) {
for (dim_t i1 = 0; i1 < dims[1]; ++i1) {
for (dim_t i2 = 0; i2 < dims[2]; ++i2) {
for (dim_t i3 = 0; i3 < dims[3]; ++i3) {
const dim_t b_i = (i0 * perm_b_stride[0] + i1 * perm_b_stride[1] +
i2 * perm_b_stride[2] + i3 * perm_b_stride[3]);
const dim_t a_i = (i0 * a_stride[0] + i1 * a_stride[1] +
i2 * a_stride[2] + i3 * a_stride[3]);
b[b_i] = a[a_i];
}
}
}
}
});
}
static cpu::GemmBackend sgemm_backend = cpu::get_gemm_backend(ComputeType::FLOAT32);
static cpu::GemmBackend gemm_s8_backend = cpu::get_gemm_backend(ComputeType::INT8);
static cpu::GemmBackend gemm_s16_backend = cpu::get_gemm_backend(ComputeType::INT16);
#ifdef CT2_WITH_MKL
// m value used to pack the b matrix.
constexpr MKL_INT mkl_gemm_pack_b_m = 1;
#endif
template<>
template<>
dim_t primitives<Device::CPU>::gemm_pack_b(const float* b,
const bool transpose_b,
const dim_t k,
const dim_t n,
const float alpha,
float* dest) {
#ifdef CT2_WITH_MKL
if (sgemm_backend == cpu::GemmBackend::MKL) {
if (!dest)
return cblas_sgemm_pack_get_size(CblasBMatrix, mkl_gemm_pack_b_m, n, k);
cblas_sgemm_pack(CblasRowMajor,
CblasBMatrix,
transpose_b ? CblasTrans : CblasNoTrans,
mkl_gemm_pack_b_m, n, k,
alpha,
b,
transpose_b ? k : n,
dest);
}
#else
(void)b;
(void)transpose_b;
(void)k;
(void)n;
(void)alpha;
(void)dest;
#endif
return 0;
}
template<>
template<>
dim_t primitives<Device::CPU>::gemm_pack_b(const int16_t* b,
const bool transpose_b,
const dim_t k,
const dim_t n,
const float,
int16_t* dest) {
#ifdef CT2_WITH_MKL
if (gemm_s16_backend == cpu::GemmBackend::MKL) {
if (!dest)
return cblas_gemm_s16s16s32_pack_get_size(CblasBMatrix, mkl_gemm_pack_b_m, n, k);
cblas_gemm_s16s16s32_pack(CblasRowMajor,
CblasBMatrix,
transpose_b ? CblasTrans : CblasNoTrans,
mkl_gemm_pack_b_m, n, k,
b,
transpose_b ? k : n,
dest);
}
#else
(void)b;
(void)transpose_b;
(void)k;
(void)n;
(void)dest;
#endif
return 0;
}
template<>
template<>
dim_t primitives<Device::CPU>::gemm_pack_b(const int8_t* b,
const bool transpose_b,
const dim_t k,
const dim_t n,
const float,
int8_t* dest) {
#ifdef CT2_WITH_MKL
if (gemm_s8_backend == cpu::GemmBackend::MKL) {
if (!dest)
return cblas_gemm_s8u8s32_pack_get_size(CblasBMatrix, mkl_gemm_pack_b_m, n, k);
cblas_gemm_s8u8s32_pack(CblasRowMajor,
CblasBMatrix,
transpose_b ? CblasTrans : CblasNoTrans,
mkl_gemm_pack_b_m, n, k,
b,
transpose_b ? k : n,
dest);
}
#else
(void)b;
(void)transpose_b;
(void)k;
(void)n;
(void)dest;
#endif
return 0;
}
template<>
template<>
void primitives<Device::CPU>::gemm(bool a_is_packed, bool b_is_packed,
bool transpose_a, bool transpose_b,
dim_t m, dim_t n, dim_t k,
float alpha,
const float* a, dim_t lda,
const float* b, dim_t ldb,
float beta,
float* c, dim_t ldc,
const float*) {
#ifndef CT2_WITH_MKL
(void)a_is_packed;
(void)b_is_packed;
#endif
switch (sgemm_backend) {
#ifdef CT2_WITH_MKL
case cpu::GemmBackend::MKL: {
CBLAS_TRANSPOSE trans_a = transpose_a ? CblasTrans : CblasNoTrans;
CBLAS_TRANSPOSE trans_b = transpose_b ? CblasTrans : CblasNoTrans;
if (a_is_packed || b_is_packed) {
cblas_sgemm_compute(CblasRowMajor,
a_is_packed ? (MKL_INT)CblasPacked : (MKL_INT)trans_a,
b_is_packed ? (MKL_INT)CblasPacked : (MKL_INT)trans_b,
m, n, k,
a, lda,
b, ldb,
beta, c, ldc);
} else {
cblas_sgemm(CblasRowMajor,
trans_a, trans_b,
m, n, k,
alpha,
a, lda,
b, ldb,
beta,
c, ldc);
}
break;
}
#endif
#ifdef CT2_WITH_DNNL
case cpu::GemmBackend::DNNL: {
dnnl_sgemm(transpose_a ? 'T' : 'N',
transpose_b ? 'T' : 'N',
m, n, k,
alpha,
a, lda,
b, ldb,
beta,
c, ldc);
break;
}
#endif
#ifdef CT2_WITH_ACCELERATE
case cpu::GemmBackend::ACCELERATE: {
cblas_sgemm(CblasRowMajor,
transpose_a ? CblasTrans : CblasNoTrans,
transpose_b ? CblasTrans : CblasNoTrans,
m, n, k,
alpha,
a, lda,
b, ldb,
beta,
c, ldc);
break;
}
#endif
#ifdef CT2_WITH_OPENBLAS
case cpu::GemmBackend::OPENBLAS: {
cblas_sgemm(CblasRowMajor,
transpose_a ? CblasTrans : CblasNoTrans,
transpose_b ? CblasTrans : CblasNoTrans,
m, n, k,
alpha,
a, lda,
b, ldb,
beta,
c, ldc);
break;
}
#endif
#ifdef CT2_WITH_RUY
case cpu::GemmBackend::RUY: {
if (lda != (transpose_a ? m : k)
|| ldb != (transpose_b ? k : n)
|| ldc != n)
throw std::invalid_argument("Ruy GEMM does not support custom leading dimensions");
ruy::Context *context = cpu::get_ruy_context();
const ruy::Order order_a = transpose_a ? ruy::Order::kColMajor : ruy::Order::kRowMajor;
const ruy::Order order_b = transpose_b ? ruy::Order::kColMajor : ruy::Order::kRowMajor;
ruy::Matrix<float> lhs;
ruy::MakeSimpleLayout(m, k, order_a, lhs.mutable_layout());
lhs.set_data(a);
ruy::Matrix<float> rhs;
ruy::MakeSimpleLayout(k, n, order_b, rhs.mutable_layout());
rhs.set_data(b);
ruy::Matrix<float> dst;
ruy::MakeSimpleLayout(m, n, ruy::Order::kRowMajor, dst.mutable_layout());
dst.set_data(c);
float *tmp_c = nullptr;
ruy::MulParams<float, float> mul_params;
if (beta != 0.0f) {
// this block sets `(beta / alpha) * c` as bias
// and multiplication by `alpha` below generates correct value:
// C <- alpha * (AB + (beta/alpha) * C)
// <- alpha * AB + beta * C
// there is no guard for alpha = 0.0 case, as it is unlikely to
// call this function with that value.
auto beta_prime = beta / alpha;
tmp_c = static_cast<float*>(allocator.allocate(m * n * sizeof (float)));
mul(beta_prime, c, tmp_c, m * n);
mul_params.set_bias(tmp_c);
}
ruy::Mul(lhs, rhs, mul_params, context, &dst);
if (alpha != 1.0f) {
mul(alpha, c, m * n);
}
if (tmp_c) {
allocator.free(tmp_c);
}
break;
}
#endif
default:
throw std::runtime_error("No SGEMM backend on CPU");
}
}
template<>
template<>
void primitives<Device::CPU>::gemm(bool a_is_packed, bool b_is_packed,
bool transpose_a, bool transpose_b,
dim_t m, dim_t n, dim_t k,
float alpha,
const int16_t* a, dim_t lda,
const int16_t* b, dim_t ldb,
float beta,
int32_t* c, dim_t ldc,
const int32_t*) {
#ifndef CT2_WITH_MKL
(void)a_is_packed;
(void)b_is_packed;
(void)transpose_a;
(void)transpose_b;
(void)m;
(void)n;
(void)k;
(void)alpha;
(void)a;
(void)lda;
(void)b;
(void)ldb;
(void)beta;
(void)c;
(void)ldc;
#endif
switch (gemm_s16_backend) {
#ifdef CT2_WITH_MKL
case cpu::GemmBackend::MKL: {
CBLAS_TRANSPOSE trans_a = transpose_a ? CblasTrans : CblasNoTrans;
CBLAS_TRANSPOSE trans_b = transpose_b ? CblasTrans : CblasNoTrans;
CBLAS_OFFSET offsetc = CblasFixOffset;
MKL_INT16 oa = 0;
MKL_INT16 ob = 0;
MKL_INT32 oc = 0;
if (a_is_packed || b_is_packed) {
cblas_gemm_s16s16s32_compute(CblasRowMajor,
a_is_packed ? (MKL_INT)CblasPacked : (MKL_INT)trans_a,
b_is_packed ? (MKL_INT)CblasPacked : (MKL_INT)trans_b,
offsetc, m, n, k,
alpha,
a, lda, oa,
b, ldb, ob,
beta,
c, ldc, &oc);
} else {
cblas_gemm_s16s16s32(CblasRowMajor,
trans_a, trans_b,
offsetc, m, n, k,
alpha,
a, lda, oa,
b, ldb, ob,
beta,
c, ldc, &oc);
}
break;
}
#endif
default:
throw std::runtime_error("No INT16 GEMM backend on CPU");
}
}
#ifdef CT2_WITH_MKL
static void shift_to_u8(const int8_t* x, uint8_t* ux, dim_t size) {
cpu::unary_transform(x, ux, size, [](int8_t v) { return static_cast<uint8_t>(v + 128); });
}
#endif
template<>
void primitives<Device::CPU>::compute_u8_compensation(const int8_t* b,
bool transpose_b,
dim_t k,
dim_t n,
float alpha,
int32_t* compensation) {
cpu::parallel_for(0, n, 1, [&](dim_t begin, dim_t end) {
for (dim_t i = begin; i < end; ++i) {
int32_t val = 0;
if (transpose_b) {
const int8_t* row = b + i * k;
val = std::accumulate(row, row + k, static_cast<int32_t>(0));
} else {
for (dim_t j = 0; j < k; ++j) {
val += b[j * n + i];
}
}
if (alpha != 1) {
val = std::nearbyintf(static_cast<float>(val) * alpha * -128.f);
} else {
val *= -128;
}
compensation[i] = val;
}
});
}
template<>
template<>
void primitives<Device::CPU>::gemm(bool a_is_packed, bool b_is_packed,
bool transpose_a, bool transpose_b,
dim_t m, dim_t n, dim_t k,
float alpha,
const int8_t* a, dim_t lda,
const int8_t* b, dim_t ldb,
float beta,
int32_t* c, dim_t ldc,
const int32_t* a_shift_compensation) {
#ifndef CT2_WITH_MKL
(void)a_is_packed;
(void)b_is_packed;
#endif
switch (gemm_s8_backend) {
#ifdef CT2_WITH_MKL
case cpu::GemmBackend::MKL: {
// We are implementing s8s8s32 GEMM with cblas_gemm_s8u8s32. In row major mode,
// it expects a to be unsigned and b to be signed. So we need to shift a to the
// uint8 domain and add a compensation term. For more details, see
// https://intel.github.io/mkl-dnn/dev_guide_int8_computations.html
const bool use_packed_api = a_is_packed || b_is_packed;
const uint8_t* ua = nullptr;
uint8_t* tmp_ua = nullptr;
int32_t* tmp_a_shift_compensation = nullptr;
if (a_shift_compensation) {
// If the compensation term is passed as argument, we assume a is already shifted.
ua = reinterpret_cast<const uint8_t*>(a);
} else if (use_packed_api) {
throw std::invalid_argument("Packed cblas_gemm_s8u8s32 requires the uint8 shift "
"compensation term to be passed as argument");
} else {
const dim_t a_size = m * k;
tmp_ua = static_cast<uint8_t*>(allocator.allocate(a_size));
shift_to_u8(a, tmp_ua, a_size);
ua = tmp_ua;
tmp_a_shift_compensation = static_cast<int32_t*>(allocator.allocate(n * sizeof (int32_t)));
compute_u8_compensation(b, transpose_b, k, n, alpha, tmp_a_shift_compensation);
a_shift_compensation = tmp_a_shift_compensation;
}
const CBLAS_TRANSPOSE trans_a = transpose_a ? CblasTrans : CblasNoTrans;
const CBLAS_TRANSPOSE trans_b = transpose_b ? CblasTrans : CblasNoTrans;
if (use_packed_api) {
cblas_gemm_s8u8s32_compute(CblasRowMajor,
a_is_packed ? (MKL_INT)CblasPacked : (MKL_INT)trans_a,
b_is_packed ? (MKL_INT)CblasPacked : (MKL_INT)trans_b,
CblasRowOffset,
m, n, k,
alpha,
ua, lda, 0,
b, ldb, 0,
beta,
c, ldc, a_shift_compensation);
} else {
cblas_gemm_s8u8s32(CblasRowMajor,
trans_a, trans_b,
CblasRowOffset,
m, n, k,
alpha,
ua, lda, 0,
b, ldb, 0,
beta,
c, ldc, a_shift_compensation);
}
if (tmp_ua)
allocator.free(tmp_ua);
if (tmp_a_shift_compensation)
allocator.free(tmp_a_shift_compensation);
break;
}
#endif
#ifdef CT2_WITH_DNNL
case cpu::GemmBackend::DNNL: {
const char transa = transpose_a ? 'T' : 'N';
const char transb = transpose_b ? 'T' : 'N';
if (a_shift_compensation) {
// If the compensation term is passed as argument, we assume a is already shifted.
dnnl_gemm_u8s8s32(transa, transb,
'R',
m, n, k,
alpha,
reinterpret_cast<const uint8_t*>(a), lda, 0,
b, ldb, 0,
beta,
c, ldc, a_shift_compensation);
} else {
const int32_t co = 0;
dnnl_gemm_s8s8s32(transa, transb,
'F',
m, n, k,
alpha,
a, lda, 0,
b, ldb, 0,
beta,
c, ldc, &co);