forked from pytorch/FBGEMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmbeddingSpMDM.cc
1088 lines (989 loc) · 36.6 KB
/
EmbeddingSpMDM.cc
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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
* All rights reserved.
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#define FBGEMM_EXPORTS
#include "fbgemm/FbgemmEmbedding.h"
#include <asmjit/asmjit.h>
#include <cpuinfo.h>
#include <immintrin.h>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <mutex>
#include <string>
#include <tuple>
#include "./CodeCache.h"
#include "./MaskAvx2.h"
#include "./RefImplementations.h"
#include "fbgemm/Types.h"
namespace fbgemm {
namespace {
namespace x86 = asmjit::x86;
template <typename inType, typename indxType, bool ROWWISE_SPARSE>
class ReturnFunctionSignature {};
template <typename inType, typename indxType>
class ReturnFunctionSignature<inType, indxType, false> {
public:
using jit_embedding_kernel = bool (*)(
std::int64_t output_size,
std::int64_t index_size,
std::int64_t data_size,
const inType* input,
const indxType* indices,
const int* lengths,
const float* weights,
float* out,
const int* mask);
};
template <typename inType, typename indxType>
class ReturnFunctionSignature<inType, indxType, true> {
public:
using jit_embedding_kernel = bool (*)(
std::int64_t output_size,
std::int64_t index_size,
int64_t uncompressed_data_size,
// int64_t compressed_data_size,
const inType* input,
const indxType* indices,
const int* lengths,
const float* weights,
float* out,
const std::int32_t* compressed_indices_table,
const int* mask);
};
template <
typename inType = std::uint8_t,
typename indxType = std::int64_t,
bool ROWWISE_SPARSE = false>
class GenEmbeddingSpMDMLookup {
public:
GenEmbeddingSpMDMLookup() {}
template <inst_set_t instSet>
typename ReturnFunctionSignature<inType, indxType, ROWWISE_SPARSE>::
jit_embedding_kernel
getOrCreate(
int block_size,
bool has_weight,
bool is_weight_positional,
bool normalize_by_lengths,
int prefetch);
private:
static asmjit::JitRuntime& runtime() {
static asmjit::JitRuntime rt; //< JIT Runtime for asmjit,
// depents on other static
// variables. Required to prevent
// initialization order fiasco
return rt;
}
static std::mutex rtMutex_; ///< Controll access to runtime;
// The hash depends on embedding dimension (block size), weighted sls,
// positional weights, normalize by lenths, and prefetch distance
static CodeCache<
std::tuple<int, bool, bool, bool, int>,
typename ReturnFunctionSignature<inType, indxType, ROWWISE_SPARSE>::
jit_embedding_kernel>
codeCache_; ///< JIT Code Cache for reuse.
}; // GenEmbeddingSpmDMLookup
template <typename inType, typename indxType, bool ROWWISE_SPARSE>
std::mutex GenEmbeddingSpMDMLookup<inType, indxType, ROWWISE_SPARSE>::rtMutex_;
template <typename inType, typename indxType, bool ROWWISE_SPARSE>
CodeCache<
std::tuple<int, bool, bool, bool, int>,
typename ReturnFunctionSignature<inType, indxType, ROWWISE_SPARSE>::
jit_embedding_kernel>
GenEmbeddingSpMDMLookup<inType, indxType, ROWWISE_SPARSE>::codeCache_;
template <typename inType, typename indxType, bool ROWWISE_SPARSE>
template <inst_set_t instSet>
typename ReturnFunctionSignature<inType, indxType, ROWWISE_SPARSE>::
jit_embedding_kernel
GenEmbeddingSpMDMLookup<inType, indxType, ROWWISE_SPARSE>::getOrCreate(
int block_size,
bool has_weight,
bool is_weight_positional,
bool normalize_by_lengths,
int prefetch) {
std::tuple<int, bool, bool, bool, int> kernelSig = std::make_tuple(
block_size,
has_weight,
is_weight_positional,
normalize_by_lengths,
prefetch);
return codeCache_.getOrCreate(
kernelSig,
[&]() -> typename ReturnFunctionSignature<
inType,
indxType,
ROWWISE_SPARSE>::jit_embedding_kernel {
bool is8bit = std::is_same<inType, std::uint8_t>::value;
bool is16bit = std::is_same<inType, float16>::value;
// TODO: Make this tunable
int pref_dist = prefetch;
bool areIndices64b = std::is_same<indxType, std::int64_t>::value;
asmjit::CodeHolder code;
code.init(runtime().codeInfo());
x86::Assembler assembler(&code);
x86::Emitter* a = assembler.as<x86::Emitter>();
#if defined(FBGEMM_LOG_CODE)
std::string filename = "embeddinglookup";
if (is8bit) {
filename += "_8bit";
} else if (is16bit) {
filename += "_fp16";
}
filename += "_emd_dim_" + std::to_string(block_size);
filename += areIndices64b ? "_64bit" : "_32bit";
filename += instSet == inst_set_t::avx512 ? "_avx512" : "_avx2";
if (prefetch) {
filename += "_prefetch";
}
if (has_weight) {
filename += "_hasweight";
}
if (normalize_by_lengths) {
filename += "_normalize_by_lengths";
}
if (ROWWISE_SPARSE) {
filename += "_rowwise_sparse";
}
filename += ".txt";
FILE* codeLogFile = fopen(filename.c_str(), "w");
asmjit::FileLogger* codeLogger = new asmjit::FileLogger(codeLogFile);
code.setLogger(codeLogger);
#endif
// arguments to the function created
x86::Gp output_size = a->zdi();
// index_size will be overwritten to hold the end address of indices
x86::Gp index_size = a->zsi();
x86::Gp data_size = a->zdx();
x86::Gp input = a->zcx();
int reg_id = 8;
x86::Gp indices = a->gpz(reg_id); // 8
++reg_id;
x86::Gp lengths = a->gpz(reg_id); // 9
++reg_id;
x86::Gp weights = a->gpz(reg_id); // 10
++reg_id;
x86::Gp out = a->gpz(reg_id); // 11
x86::Gp compressed_indices_table;
if (ROWWISE_SPARSE) {
++reg_id;
compressed_indices_table = a->gpz(reg_id); // 12
}
++reg_id;
x86::Gp scratchReg1_ = a->gpz(reg_id); // 12 or 13, also for mask
++reg_id;
x86::Gpd lengths_R_ = a->gpz(reg_id).r32(); // 13 or 14
++reg_id;
x86::Gp scratchReg2_ = a->gpz(reg_id); // 14 or 15
asmjit::FuncDetail func;
if (ROWWISE_SPARSE) {
func.init(
asmjit::FuncSignatureT<
bool,
std::int64_t, // output_size
std::int64_t, // index_size
std::int64_t, // uncompressed_data_size
const inType*, // input uint8_t or float
const indxType*, // indices
const int*, // lengths
const float*, // weights
float*, // out
const std::int32_t*, // compressed_indices_table and then mask
const int*>(asmjit::CallConv::kIdHost));
} else {
func.init(asmjit::FuncSignatureT<
bool,
std::int64_t, // output_size
std::int64_t, // index_size
std::int64_t, // data_size
const inType*, // input uint8_t or float
const indxType*, // indices
const int*, // lengths
const float*, // weights
float*, // out and then mask
const int*>(asmjit::CallConv::kIdHost));
}
asmjit::FuncFrame frame;
frame.init(func);
if (instSet == inst_set_t::avx2) {
frame.setDirtyRegs(
x86::Reg::kGroupVec,
asmjit::Support::bitMask(0, 1, 2, 3, 4, 5, 6, 7) |
asmjit::Support::bitMask(8, 9, 10, 11, 12, 13, 14, 15));
} else {
frame.setDirtyRegs(
x86::Reg::kGroupVec,
asmjit::Support::bitMask(0, 1, 2, 3, 4, 5, 6, 7) |
asmjit::Support::bitMask(8, 9, 10, 11, 12, 13, 14, 15) |
asmjit::Support::bitMask(16, 17, 18, 19, 20, 21, 22, 23) |
asmjit::Support::bitMask(24, 25, 26, 27, 28, 29, 30, 31));
}
frame.setDirtyRegs(
x86::Reg::kGroupGp,
reg_id == 15
? asmjit::Support::bitMask(8, 9, 10, 11, 12, 13, 14, 15)
: asmjit::Support::bitMask(8, 9, 10, 11, 12, 13, 14));
asmjit::FuncArgsAssignment args(&func);
if (ROWWISE_SPARSE) {
args.assignAll(
output_size,
index_size,
data_size,
input,
indices,
lengths,
weights,
out,
compressed_indices_table,
scratchReg1_);
} else {
args.assignAll(
output_size,
index_size,
data_size,
input,
indices,
lengths,
weights,
out,
scratchReg1_);
}
args.updateFuncFrame(frame);
frame.finalize();
a->emitProlog(frame);
a->emitArgsAssignment(frame, args);
constexpr int vlen = simd_info<instSet>::WIDTH_32BIT_ELEMS;
constexpr int NUM_VEC_REG = simd_info<instSet>::NUM_VEC_REGS;
int unroll_factor = NUM_VEC_REG;
typedef typename simd_info<instSet>::vec_reg_t vec_reg_t;
int num_vec_regs_per_block = (block_size + vlen - 1) / vlen;
int remainder = block_size % vlen;
vec_reg_t scale_vreg; // holds scale
vec_reg_t bias_vreg; // holds bias
vec_reg_t w_vreg; // for weighted sls -- weights
vec_reg_t
vlen_inv_vreg; // used for normalize by lengths -- 1/ lengths[i]
vec_reg_t src_vreg; // for holding embedding value temporarily
x86::Ymm mask_vreg; // mask for avx2
x86::Xmm mask_fp16_vreg; // mask for loading fp16 in avx2
if (is8bit) {
// We need 2 vec registers for 1. scale 2. bias
--unroll_factor;
scale_vreg = vec_reg_t(unroll_factor);
--unroll_factor;
bias_vreg = vec_reg_t(unroll_factor);
}
if (is8bit || is16bit || (remainder && instSet == inst_set_t::avx2)) {
--unroll_factor;
src_vreg = vec_reg_t(unroll_factor);
}
if (has_weight) {
--unroll_factor;
w_vreg = vec_reg_t(unroll_factor);
}
if (remainder && instSet == inst_set_t::avx2) {
// AVX512 doesn't need to use vector register for masking
--unroll_factor;
mask_vreg = x86::ymm(unroll_factor);
if (remainder > 1 && is16bit) {
--unroll_factor;
mask_fp16_vreg = x86::xmm(unroll_factor);
}
}
if (normalize_by_lengths) {
--unroll_factor;
vlen_inv_vreg = vec_reg_t(unroll_factor);
}
if (remainder) {
if (instSet == inst_set_t::avx2) {
a->vmovups(
mask_vreg,
x86::ymmword_ptr(
scratchReg1_, (vlen - remainder) % vlen * sizeof(int32_t)));
if (remainder > 1 && is16bit) {
a->vmovups(
mask_fp16_vreg,
x86::xmmword_ptr(
scratchReg1_,
(vlen / 2 - remainder / 2) % (vlen / 2) *
sizeof(int32_t)));
// We need to keep using the stack during the main loop
a->lea(
x86::rsp,
x86::dword_ptr(
x86::rsp, static_cast<int32_t>(-vlen * sizeof(int32_t))));
}
} else {
a->mov(scratchReg1_, (1 << remainder) - 1);
a->kmovw(x86::k(1), scratchReg1_);
}
}
// Compute the end address of indices
a->imul(
scratchReg1_,
index_size,
static_cast<asmjit::Imm>(sizeof(indxType)));
a->add(scratchReg1_, indices);
a->mov(index_size, scratchReg1_);
asmjit::Label exit = a->newLabel();
asmjit::Label error = a->newLabel();
asmjit::Label LoopRangeIndexBegin = a->newLabel();
asmjit::Label LoopRangeIndexEnd = a->newLabel();
// rangeIndex loop begins (iterate output_size times)
a->bind(LoopRangeIndexBegin);
a->dec(output_size);
a->jl(LoopRangeIndexEnd);
if (normalize_by_lengths) {
asmjit::Label IfLengthsBegin = a->newLabel();
asmjit::Label IfLengthsEnd = a->newLabel();
a->bind(IfLengthsBegin);
a->cmp(x86::dword_ptr(lengths), 1);
// Initialize vlen_inv as 0 in case lengths is 0
a->vxorps(vlen_inv_vreg, vlen_inv_vreg, vlen_inv_vreg);
a->jl(IfLengthsEnd);
if (instSet == inst_set_t::avx2) {
x86::Xmm vlen_inv_vreg_xmm(vlen_inv_vreg.id());
a->mov(lengths_R_, 1);
a->cvtsi2ss(vlen_inv_vreg_xmm, lengths_R_);
a->cvtsi2ss(x86::xmm0, x86::dword_ptr(lengths));
a->divss(vlen_inv_vreg_xmm, x86::xmm0);
a->vpbroadcastd(vlen_inv_vreg, vlen_inv_vreg_xmm);
} else { // avx512
vec_reg_t temp_zmm = vec_reg_t(0);
a->mov(lengths_R_, 1);
a->cvtsi2ss(x86::xmm(temp_zmm.id()), lengths_R_);
a->vpbroadcastd(vlen_inv_vreg, x86::xmm(temp_zmm.id()));
a->vpbroadcastd(temp_zmm, x86::dword_ptr(lengths));
a->vcvtdq2ps(temp_zmm, temp_zmm);
a->vdivps(vlen_inv_vreg, vlen_inv_vreg, temp_zmm);
}
a->bind(IfLengthsEnd);
}
for (int vec_idx = 0; vec_idx < num_vec_regs_per_block;
vec_idx += unroll_factor) {
int cur_unroll_factor =
std::min(unroll_factor, num_vec_regs_per_block - vec_idx);
// Initialize output regs
for (int v = 0; v < cur_unroll_factor; ++v) {
vec_reg_t out_vreg = vec_reg_t(v);
a->vxorps(out_vreg, out_vreg, out_vreg);
}
a->mov(lengths_R_, x86::dword_ptr(lengths));
// Array out of bound check
a->imul(
scratchReg1_,
lengths_R_,
static_cast<asmjit::Imm>(sizeof(indxType)));
a->add(scratchReg1_, indices);
a->cmp(scratchReg1_, index_size);
a->jg(error);
asmjit::Label LoopDataIndexBegin = a->newLabel();
asmjit::Label LoopDataIndexEnd = a->newLabel();
// dataIndex loop begins (iterate lengths_R_ times)
a->bind(LoopDataIndexBegin);
a->dec(lengths_R_);
a->jl(LoopDataIndexEnd);
// Array out of bound check
if (areIndices64b) {
a->mov(scratchReg1_, x86::qword_ptr(indices));
} else {
a->mov(scratchReg1_.r32(), x86::dword_ptr(indices));
}
a->cmp(scratchReg1_, 0);
a->jl(error);
a->cmp(scratchReg1_, data_size);
a->jge(error);
if (ROWWISE_SPARSE) {
a->mov(
scratchReg1_.r32(),
x86::dword_ptr(
compressed_indices_table,
scratchReg1_,
2)); // use of 2 is to multiply by 4
}
int fused_block_size = is8bit
? block_size * sizeof(uint8_t) + 2 * sizeof(float)
: block_size * sizeof(inType);
if (pref_dist) {
asmjit::Label pref_dist_reset_start = a->newLabel();
asmjit::Label pref_dist_reset_end = a->newLabel();
// out of bound handling for prefetch
a->mov(scratchReg2_, indices);
a->add(
scratchReg2_,
static_cast<asmjit::Imm>(pref_dist * sizeof(indxType)));
a->cmp(scratchReg2_, index_size);
a->jge(pref_dist_reset_start);
if (areIndices64b) {
a->mov(
scratchReg2_,
x86::qword_ptr(indices, pref_dist * sizeof(indxType)));
} else {
a->mov(
scratchReg2_.r32(),
x86::dword_ptr(indices, pref_dist * sizeof(indxType)));
}
a->cmp(scratchReg2_, 0);
a->jl(pref_dist_reset_start);
a->cmp(scratchReg2_, data_size);
a->jge(pref_dist_reset_start);
// everything is okay, prefetch a few rows ahead
a->jmp(pref_dist_reset_end);
a->bind(pref_dist_reset_start);
// things are not okay just get the current row
// this can be improved to getting the max dist row.
if (areIndices64b) {
a->mov(scratchReg2_, x86::qword_ptr(indices));
} else {
a->mov(scratchReg2_.r32(), x86::dword_ptr(indices));
}
a->bind(pref_dist_reset_end);
if (ROWWISE_SPARSE) {
a->mov(
scratchReg2_.r32(),
x86::dword_ptr(
compressed_indices_table,
scratchReg2_,
2)); // use of 2 is to multiply by 4
}
a->imul(scratchReg2_, static_cast<asmjit::Imm>(fused_block_size));
}
a->add(indices, static_cast<asmjit::Imm>(sizeof(indxType)));
if (has_weight) {
a->vbroadcastss(w_vreg, x86::dword_ptr(weights));
a->add(weights, static_cast<asmjit::Imm>(sizeof(float)));
}
if (ROWWISE_SPARSE) {
a->cmp(scratchReg1_.r32(), static_cast<asmjit::Imm>(-1));
a->je(LoopDataIndexBegin);
}
a->imul(scratchReg1_, static_cast<asmjit::Imm>(fused_block_size));
// broadcast the scale
x86::Mem scale_src, bias_src;
if (is8bit) {
scale_src = x86::dword_ptr(
input, scratchReg1_, 0, block_size * sizeof(uint8_t));
bias_src = x86::dword_ptr(
input,
scratchReg1_,
0,
block_size * sizeof(uint8_t) + sizeof(float));
a->vbroadcastss(scale_vreg, scale_src);
a->vbroadcastss(bias_vreg, bias_src);
}
if (has_weight && is8bit) {
a->vmulps(scale_vreg, scale_vreg, w_vreg);
a->vmulps(bias_vreg, bias_vreg, w_vreg);
}
// The main computation
for (int v = 0; v < cur_unroll_factor; ++v) {
constexpr int BYTES_PER_VLOAD = vlen * sizeof(inType);
auto src_addr = x86::dword_ptr(
input, scratchReg1_, 0, (vec_idx + v) * BYTES_PER_VLOAD);
vec_reg_t out_vreg = vec_reg_t(v);
// For 8bit SLS convert usigned 8-bit to 32bit int, then to float
// multiply with scale and then add with bias
if (is8bit) {
if (remainder && vec_idx + v == num_vec_regs_per_block - 1 &&
instSet == inst_set_t::avx512) {
a->k(x86::k(1)).z().vpmovzxbd(src_vreg, src_addr);
} else {
// We don't use a mask for AVX2 since we can use the extra
// "padding" of the 2 floats (= 8 chars) scale and bias
// this ensures we never access out of bound data
a->vpmovzxbd(src_vreg, src_addr);
}
a->vcvtdq2ps(src_vreg, src_vreg);
a->vaddps(out_vreg, out_vreg, bias_vreg);
a->vfmadd231ps(out_vreg, src_vreg, scale_vreg);
} else if (is16bit) {
if (remainder && vec_idx + v == num_vec_regs_per_block - 1) {
if (instSet == inst_set_t::avx2) {
if (remainder % 2 == 0) {
a->vmaskmovps(
x86::xmm(src_vreg.id()), mask_fp16_vreg, src_addr);
} else {
a->vpbroadcastw(
x86::xmm(src_vreg.id()),
x86::word_ptr(
input,
scratchReg1_,
0,
(vec_idx + v) * BYTES_PER_VLOAD +
(remainder - 1) * sizeof(inType)));
if (remainder > 1) {
// AVX2 can't do masking for the last 16-bit so we store
// them to a stack and reload.
// First put broadcasted last 16-bit element
a->vmovups(
x86::xmmword_ptr(x86::rsp), x86::xmm(src_vreg.id()));
// Mask store the remaining 16-bit elements
a->vmaskmovps(
x86::xmm(src_vreg.id()), mask_fp16_vreg, src_addr);
a->vmaskmovps(
x86::xmmword_ptr(x86::rsp),
mask_fp16_vreg,
x86::xmm(src_vreg.id()));
// Load combined 16-bit elements
a->vmovups(
x86::xmm(src_vreg.id()), x86::xmmword_ptr(x86::rsp));
} // remainder > 1
} // remainder % 2
a->vcvtph2ps(
x86::ymm(src_vreg.id()), x86::xmm(src_vreg.id()));
} else {
// avx512
a->k(x86::k(1)).z().vcvtph2ps(src_vreg, src_addr);
}
} else {
// no remainder
a->vcvtph2ps(src_vreg, src_addr);
}
if (has_weight) {
a->vfmadd231ps(out_vreg, w_vreg, src_vreg);
} else {
a->vaddps(out_vreg, out_vreg, src_vreg);
}
} else {
// This part for FP32 SLS
if (remainder && vec_idx + v == num_vec_regs_per_block - 1 &&
instSet == inst_set_t::avx2) {
a->vmaskmovps(
x86::ymm(src_vreg.id()),
x86::ymm(mask_vreg.id()),
src_addr);
}
if (has_weight) {
if (remainder && vec_idx + v == num_vec_regs_per_block - 1) {
if (instSet == inst_set_t::avx2) {
a->vfmadd231ps(out_vreg, w_vreg, src_vreg);
} else {
a->k(x86::k(1)).vfmadd231ps(out_vreg, w_vreg, src_addr);
}
} else {
a->vfmadd231ps(out_vreg, w_vreg, src_addr);
}
} else {
if (remainder && vec_idx + v == num_vec_regs_per_block - 1) {
if (instSet == inst_set_t::avx2) {
a->vaddps(out_vreg, out_vreg, src_vreg);
} else {
a->k(x86::k(1)).vaddps(out_vreg, out_vreg, src_addr);
}
} else {
a->vaddps(out_vreg, out_vreg, src_addr);
}
}
}
constexpr int CACHE_LINE_LEN = 64;
constexpr int VLOAD_PER_CACHE_LINE =
CACHE_LINE_LEN / BYTES_PER_VLOAD;
if (pref_dist && (vec_idx + v) % VLOAD_PER_CACHE_LINE == 0) {
a->prefetcht0(x86::dword_ptr(
input, scratchReg2_, 0, (vec_idx + v) * BYTES_PER_VLOAD));
}
}
a->jmp(LoopDataIndexBegin);
a->bind(LoopDataIndexEnd);
// This loop is for writing back out_vreg (results)
// back to memory
for (int v = 0; v < cur_unroll_factor; ++v) {
auto dst_addr =
x86::dword_ptr(out, (vec_idx + v) * vlen * sizeof(float));
vec_reg_t out_vreg = vec_reg_t(v);
if (normalize_by_lengths) {
a->vmulps(out_vreg, out_vreg, vlen_inv_vreg);
}
if (remainder && vec_idx + v == num_vec_regs_per_block - 1) {
if (instSet == inst_set_t::avx2) {
a->vmaskmovps(dst_addr, mask_vreg, x86::Ymm(out_vreg.id()));
} else {
a->k(x86::k(1)).vmovups(dst_addr, out_vreg);
}
} else {
a->vmovups(dst_addr, out_vreg);
}
}
if (vec_idx + unroll_factor < num_vec_regs_per_block ||
(has_weight && is_weight_positional)) {
// Reset lengths_R_, indices, weights to run the dataIndex loop
// again
a->mov(lengths_R_, x86::dword_ptr(lengths));
if (has_weight) {
a->imul(
scratchReg1_,
lengths_R_,
static_cast<asmjit::Imm>(sizeof(float)));
a->sub(weights, scratchReg1_);
if (vec_idx + unroll_factor < num_vec_regs_per_block) {
a->imul(
scratchReg1_,
static_cast<asmjit::Imm>(sizeof(indxType) / sizeof(float)));
a->sub(indices, scratchReg1_);
}
} else {
a->imul(
scratchReg1_,
lengths_R_,
static_cast<asmjit::Imm>(sizeof(indxType)));
a->sub(indices, scratchReg1_);
}
}
}
a->add(lengths, static_cast<asmjit::Imm>(sizeof(int)));
a->add(out, static_cast<asmjit::Imm>(block_size * sizeof(float)));
a->jmp(LoopRangeIndexBegin);
a->bind(LoopRangeIndexEnd);
a->cmp(indices, index_size);
a->jne(error);
a->mov(x86::eax, true);
a->jmp(exit);
a->bind(error);
a->mov(x86::eax, false);
a->bind(exit);
if (remainder > 1 && instSet == inst_set_t::avx2 && is16bit) {
a->lea(x86::rsp, x86::ymmword_ptr(x86::rsp, vlen * sizeof(int32_t)));
}
a->emitEpilog(frame);
// jit_fused8bitembedding_kernel fn;
typename ReturnFunctionSignature<inType, indxType, ROWWISE_SPARSE>::
jit_embedding_kernel fn;
asmjit::Error err;
{
std::unique_lock<std::mutex> lock(rtMutex_);
err = runtime().add(&fn, &code);
}
if (err) {
std::cout << "Error: in fn add" << std::endl;
return nullptr;
}
#if defined(FBGEMM_LOG_CODE)
fclose(codeLogFile);
delete codeLogger;
#endif
return fn;
});
}
} // namespace
template <typename inType, typename indxType>
typename EmbeddingSpMDMKernelSignature<inType, indxType>::Type
GenerateEmbeddingSpMDM(
const std::int64_t block_size,
bool has_weight,
bool normalize_by_lengths,
int prefetch,
bool is_weight_positional) {
static GenEmbeddingSpMDMLookup<inType, indxType> kernel_generator;
if (!cpuinfo_initialize()) {
throw std::runtime_error("Failed to initialize cpuinfo!");
}
if ((std::is_same<inType, float>::value ||
std::is_same<inType, float16>::value) &&
block_size == 1 && fbgemmHasAvx2Support()) {
return
[=](std::int64_t output_size,
std::int64_t index_size,
std::int64_t data_size,
const inType* input,
const indxType* indices,
const int* lengths,
const float* weights, // optional, can be null for non-weighted sum
float* out) {
return internal::EmbeddingSpMDMBlockSize1_(
output_size,
index_size,
data_size,
input,
indices,
lengths,
weights,
normalize_by_lengths,
out,
is_weight_positional);
};
} else if (fbgemmHasAvx512Support()) {
const auto original_func =
kernel_generator.template getOrCreate<inst_set_t::avx512>(
block_size,
has_weight,
is_weight_positional,
normalize_by_lengths,
prefetch);
return [=](std::int64_t output_size,
std::int64_t index_size,
std::int64_t data_size,
const inType* input,
const indxType* indices,
const int* lengths,
const float* weights,
float* out) {
return original_func(
output_size,
index_size,
data_size,
input,
indices,
lengths,
weights,
out,
nullptr /* mask not used in avx512 */);
};
} else if (fbgemmHasAvx2Support()) {
const auto original_func =
kernel_generator.template getOrCreate<inst_set_t::avx2>(
block_size,
has_weight,
is_weight_positional,
normalize_by_lengths,
prefetch);
return [=](std::int64_t output_size,
std::int64_t index_size,
std::int64_t data_size,
const inType* input,
const indxType* indices,
const int* lengths,
const float* weights,
float* out) {
return original_func(
output_size,
index_size,
data_size,
input,
indices,
lengths,
weights,
out,
internal::avx2_ps_or_epi32_combined_mask);
};
} else {
#ifdef VLOG
VLOG(0) << "AVX2 or AVX512 not found, taking the slow path";
#endif
return [=](std::int64_t output_size,
std::int64_t index_size,
std::int64_t data_size,
const inType* input,
const indxType* indices,
const int* lengths,
const float* weights,
float* out) {
return EmbeddingSpMDM_ref(
block_size,
output_size,
index_size,
data_size,
input,
indices,
lengths,
weights,
normalize_by_lengths,
out,
is_weight_positional);
};
}
}
template <typename inType, typename indxType>
typename EmbeddingSpMDMRowWiseSparseKernelSignature<inType, indxType>::Type
GenerateEmbeddingSpMDMRowWiseSparse(
const int64_t block_size,
bool has_weight,
bool normalize_by_lengths,
int prefetch,
bool is_weight_positional) {
if (!cpuinfo_initialize()) {
throw std::runtime_error("Failed to initialize cpuinfo!");
}
if (fbgemmHasAvx512Support()) {
static GenEmbeddingSpMDMLookup<inType, indxType, true /* rowwise_sparse */>
kernel_generator;
const auto original_func =
kernel_generator.template getOrCreate<inst_set_t::avx512>(
block_size,
has_weight,
is_weight_positional,
normalize_by_lengths,
prefetch);
return [=](std::int64_t output_size,
std::int64_t index_size,
std::int64_t uncompressed_data_size,
const inType* input,
const indxType* indices,
const int* lengths,
const float* weights,
float* out,
const std::int32_t* compressed_indices_table) {
return original_func(
output_size,
index_size,
uncompressed_data_size,
input,
indices,
lengths,
weights,
out,
compressed_indices_table,
nullptr /* mask not used in avx512 */);
};
} else if (fbgemmHasAvx2Support()) {
static GenEmbeddingSpMDMLookup<inType, indxType, true /* rowwise_sparse */>
kernel_generator;
const auto original_func =
kernel_generator.template getOrCreate<inst_set_t::avx2>(
block_size,
has_weight,
is_weight_positional,
normalize_by_lengths,
prefetch);
return [=](std::int64_t output_size,
std::int64_t index_size,
std::int64_t uncompressed_data_size,
const inType* input,
const indxType* indices,
const int* lengths,
const float* weights,
float* out,
const std::int32_t* compressed_indices_table) {
return original_func(
output_size,
index_size,
uncompressed_data_size,
input,
indices,
lengths,
weights,
out,
compressed_indices_table,
internal::avx2_ps_or_epi32_combined_mask);
};
} else {
#ifdef VLOG
VLOG(0) << "AVX2 or AVX512 not found, taking the slow path";
#endif
return
[=](int64_t output_size,
int64_t index_size,
int64_t uncompressed_data_size,
const inType* input,
const indxType* indices,
const int* lengths,
const float* weights, // optional, can be null for non-weighted sum
float* out,
const std::int32_t* compressed_indices_table) {
return EmbeddingSpMDMRowWiseSparse_ref(
block_size,
output_size,
index_size,
uncompressed_data_size,
// compressed_data_size,
input,
indices,
compressed_indices_table,
lengths,
weights,
normalize_by_lengths,
out,
is_weight_positional);
};
}
}
template FBGEMM_API
typename EmbeddingSpMDMKernelSignature<float, std::int64_t>::Type
GenerateEmbeddingSpMDM<float, std::int64_t>(
const std::int64_t block_size,
bool has_weight,
bool normalize_by_lengths,
int prefetch,
bool is_weight_positional);
template FBGEMM_API
typename EmbeddingSpMDMKernelSignature<float, std::int32_t>::Type
GenerateEmbeddingSpMDM<float, std::int32_t>(
const std::int64_t block_size,
bool has_weight,
bool normalize_by_lengths,
int prefetch,
bool is_weight_positional);
template FBGEMM_API
typename EmbeddingSpMDMKernelSignature<float16, std::int64_t>::Type
GenerateEmbeddingSpMDM<float16, std::int64_t>(