forked from pytorch/FBGEMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmbeddingSpMDMAvx2.cc
164 lines (146 loc) · 5.33 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
/*
* Copyright (c) Meta Platforms, Inc. and 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 <cassert>
#include <cmath>
#include "fbgemm/Types.h"
namespace fbgemm {
namespace internal {
template <typename InType, typename IndexType, typename OffsetType>
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 OffsetType* offsets_or_lengths,
const float* weights, // optional, can be null for non-weighted sum
bool normalize_by_lengths,
float* out,
bool is_weight_positional,
bool use_offsets) {
int64_t current = 0;
for (int m = 0; m < output_size; ++m) {
out[m] = 0;
int len = use_offsets ? offsets_or_lengths[m + 1] - offsets_or_lengths[m]
: offsets_or_lengths[m];
if (current + len > 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
float temp = out[m];
for (; i < len; ++i) {
int64_t idx = indices[current];
if (idx < 0 || idx >= data_size) {
out[m] = temp;
return false;
}
float w = 1.f;
if (weights) {
w = weights[is_weight_positional ? i : current];
}
const InType* inptr = input + indices[current];
temp = std::fma(
w,
std::is_same<InType, float16>::value ? cpu_half2float(*inptr)
: *inptr,
temp);
++current;
}
if (normalize_by_lengths && len) {
float scale = 1.f / len;
temp *= scale;
}
out[m] = temp;
}
return current == index_size;
}
#define INSTANTIATE_SPMDM_BASE(IN_TYPE, INDEX_TYPE, OFFSET_TYPE) \
template bool EmbeddingSpMDMBlockSize1_( \
const std::int64_t output_size, \
const std::int64_t index_size, \
const std::int64_t data_size, \
const IN_TYPE* input, \
const INDEX_TYPE* indices, \
const OFFSET_TYPE* offsets_or_lengths, \
const float* weights, \
bool normalize_by_lengths, \
float* out, \
bool is_weight_positional, \
bool use_offsets);
#define INSTANTIATE_SPMDM_OFFSET_T(IN_TYPE, INDEX_TYPE) \
INSTANTIATE_SPMDM_BASE(IN_TYPE, INDEX_TYPE, std::int32_t) \
INSTANTIATE_SPMDM_BASE(IN_TYPE, INDEX_TYPE, std::int64_t)
#define INSTANTIATE_SPMDM_INDEX_T(IN_TYPE) \
INSTANTIATE_SPMDM_OFFSET_T(IN_TYPE, std::int32_t) \
INSTANTIATE_SPMDM_OFFSET_T(IN_TYPE, std::int64_t)
INSTANTIATE_SPMDM_INDEX_T(float)
INSTANTIATE_SPMDM_INDEX_T(float16)
INSTANTIATE_SPMDM_INDEX_T(std::uint8_t)
#undef INSTANTIATE_SPMDM_INDEX_T
#undef INSTANTIATE_SPMDM_OFFSET_T
#undef INSTANTIATE_SPMDM_BASE
} // namespace internal
} // namespace fbgemm