forked from pytorch/FBGEMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmbeddingSpMDMAvx2.cc
201 lines (182 loc) · 6.2 KB
/
EmbeddingSpMDMAvx2.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
/*
* 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.
*/
#include "fbgemm/FbgemmEmbedding.h"
#include <immintrin.h>
#include <cassert>
#include <cmath>
#include "fbgemm/Types.h"
namespace fbgemm {
namespace internal {
template <typename inType, typename IndexType>
bool EmbeddingSpMDMBlockSize1_(
const std::int64_t output_size,
const std::int64_t index_size,
const std::int64_t data_size, // the number of rows in input
const inType* input,
const IndexType* indices,
const int* lengths,
const float* weights, // optional, can be null for non-weighted sum
bool normalize_by_lengths,
float* out,
bool is_weight_positional) {
int64_t current = 0;
for (int m = 0; m < output_size; ++m) {
out[m] = 0;
if (current + lengths[m] > index_size) {
return false;
}
int i = 0;
// The following code doesn't speedup
#if 0
constexpr int VLEN = std::is_same<IndexType, std::int64_t>::value ? 4 : 8;
for (; i < lengths[m] / VLEN * VLEN; i += VLEN) {
if (std::is_same<IndexType, std::int64_t>::value) {
__m256i idx_v = _mm256_lddqu_si256(
reinterpret_cast<const __m256i*>(indices + current));
// Should be none true
int mask1 = _mm256_movemask_pd(_mm256_castsi256_pd(
_mm256_cmpgt_epi64(_mm256_setzero_si256(), idx_v)));
// Should be all true
int mask2 = _mm256_movemask_pd(_mm256_castsi256_pd(
_mm256_cmpgt_epi64(_mm256_set1_epi64x(data_size), idx_v)));
if (mask1 || mask2 != 0x0f) {
return false;
}
__m128 in_v = _mm256_i64gather_ps(input, idx_v, 4);
alignas(64) float in_buf[VLEN];
_mm_store_ps(in_buf, in_v);
for (int j = 0; j < VLEN; ++j) {
if (weights) {
out[m] = std::fma(
weights[is_weight_positional ? i + j : current + j],
in_buf[j],
out[m]);
} else {
out[m] += in_buf[j];
}
}
} else {
__m256i idx_v = _mm256_lddqu_si256(
reinterpret_cast<const __m256i*>(indices + current));
// Should be none true
int mask1 = _mm256_movemask_ps(_mm256_castsi256_ps(
_mm256_cmpgt_epi32(_mm256_setzero_si256(), idx_v)));
// Should be all true
int mask2 = _mm256_movemask_ps(_mm256_castsi256_ps(
_mm256_cmpgt_epi32(_mm256_set1_epi32(data_size), idx_v)));
if (mask1 || mask2 != 0x00ff) {
return false;
}
__m256 in_v = _mm256_i32gather_ps(input, idx_v, 4);
alignas(64) float in_buf[VLEN];
_mm256_store_ps(in_buf, in_v);
for (int j = 0; j < VLEN; ++j) {
if (weights) {
out[m] = std::fma(
weights[is_weight_positional ? i + j : current + j],
in_buf[j],
out[m]);
} else {
out[m] += in_buf[j];
}
}
}
current += VLEN;
}
#endif
for (; i < lengths[m]; ++i) {
int64_t idx = indices[current];
if (idx < 0 || idx >= data_size) {
return false;
}
float w = 1.f;
if (weights) {
w = weights[is_weight_positional ? i : current];
}
const inType* inptr = input + indices[current];
out[m] = std::fma(
w,
std::is_same<inType, float16>::value ? cpu_half2float(*inptr)
: *inptr,
out[m]);
++current;
}
if (normalize_by_lengths && lengths[m]) {
float scale = 1.f / lengths[m];
out[m] *= scale;
}
}
return current == index_size;
}
template bool EmbeddingSpMDMBlockSize1_(
const std::int64_t output_size,
const std::int64_t index_size,
const std::int64_t data_size, // the number of rows in input
const float* input,
const std::int64_t* indices,
const int* lengths,
const float* weights, // optional, can be null for non-weighted sum
bool normalize_by_lengths,
float* out,
bool is_weight_positional);
template bool EmbeddingSpMDMBlockSize1_(
const std::int64_t output_size,
const std::int64_t index_size,
const std::int64_t data_size, // the number of rows in input
const float* input,
const std::int32_t* indices,
const int* lengths,
const float* weights, // optional, can be null for non-weighted sum
bool normalize_by_lengths,
float* out,
bool is_weight_positional);
template bool EmbeddingSpMDMBlockSize1_(
const std::int64_t output_size,
const std::int64_t index_size,
const std::int64_t data_size, // the number of rows in input
const float16* input,
const std::int64_t* indices,
const int* lengths,
const float* weights, // optional, can be null for non-weighted sum
bool normalize_by_lengths,
float* out,
bool is_weight_positional);
template bool EmbeddingSpMDMBlockSize1_(
const std::int64_t output_size,
const std::int64_t index_size,
const std::int64_t data_size, // the number of rows in input
const float16* input,
const std::int32_t* indices,
const int* lengths,
const float* weights, // optional, can be null for non-weighted sum
bool normalize_by_lengths,
float* out,
bool is_weight_positional);
template bool EmbeddingSpMDMBlockSize1_(
const std::int64_t output_size,
const std::int64_t index_size,
const std::int64_t data_size, // the number of rows in input
const std::uint8_t* input,
const std::int64_t* indices,
const int* lengths,
const float* weights, // optional, can be null for non-weighted sum
bool normalize_by_lengths,
float* out,
bool is_weight_positional);
template bool EmbeddingSpMDMBlockSize1_(
const std::int64_t output_size,
const std::int64_t index_size,
const std::int64_t data_size, // the number of rows in input
const std::uint8_t* input,
const std::int32_t* indices,
const int* lengths,
const float* weights, // optional, can be null for non-weighted sum
bool normalize_by_lengths,
float* out,
bool is_weight_positional);
} // namespace internal
} // namespace fbgemm