1 #include "caffe2/perfkernels/embedding_lookup.h" 3 #include "caffe2/core/types.h" 4 #include "caffe2/perfkernels/common.h" 5 #include "caffe2/perfkernels/typed_axpy.h" 6 #include "caffe2/utils/cpuid.h" 7 #include "caffe2/utils/math.h" 16 bool IS_WEIGHT_POSITIONAL =
false>
17 static void EmbeddingLookupGenericSlow(
18 const TIndex block_size,
19 const TIndex output_size,
20 const TIndex index_size,
21 const TIndex data_size,
23 const IndexType* indices,
26 const float* scale_bias,
27 bool normalize_by_lengths,
30 for (
int m = 0; m < output_size; ++m) {
31 memset(out, 0,
sizeof(OutType) * block_size);
32 EigenVectorArrayMap<OutType> out_vector(out, block_size);
33 for (
int i = 0; i < lengths[m]; ++i) {
34 CAFFE_ENFORCE_LT(current, index_size);
35 TIndex idx = indices[current];
37 0 <= idx && idx < data_size,
40 " is out of bounds: ",
44 CAFFE_ENFORCE_LT(idx, data_size);
46 if (current + 1 < index_size) {
47 __builtin_prefetch(input + block_size * indices[current + 1], 0, 1);
51 float w = 1.f, b = 0.f;
53 w = weights[IS_WEIGHT_POSITIONAL ? i : current];
56 b = w * scale_bias[2 * indices[current] + 1];
57 w = w * scale_bias[2 * indices[current]];
60 TypedAxpy<InType, OutType>(
61 block_size, w, input + block_size * indices[current], out);
64 out_vector = out_vector + b;
69 if (normalize_by_lengths && lengths[m]) {
71 math::Scale<OutType, CPUContext>(
72 block_size, 1.f / lengths[m], out, out,
nullptr);
79 "Your input seems to be incorrect: the sum of lengths values should be " 80 "the size of the indices tensor, but it appears not.");
84 #define EMBEDDING_SPECIALIZATION( \ 85 IndexType, InType, OutType, IS_WEIGHT_POSITIONAL) \ 87 EmbeddingLookup_##IndexType##_##InType##_##OutType##_##IS_WEIGHT_POSITIONAL##__base( \ 88 const TIndex block_size, \ 89 const TIndex output_size, \ 90 const TIndex index_size, \ 91 const TIndex data_size, \ 92 const InType* input, \ 93 const IndexType* indices, \ 95 const float* weights, \ 96 const float* scale_bias, \ 97 bool normalize_by_lengths, \ 99 EmbeddingLookupGenericSlow< \ 103 IS_WEIGHT_POSITIONAL>( \ 113 normalize_by_lengths, \ 117 void EmbeddingLookup<IndexType, InType, OutType, IS_WEIGHT_POSITIONAL>( \ 118 const TIndex block_size, \ 119 const TIndex output_size, \ 120 const TIndex index_size, \ 121 const TIndex data_size, \ 122 const InType* input, \ 123 const IndexType* indices, \ 124 const int* lengths, \ 125 const float* weights, \ 126 const float* scale_bias, \ 127 bool normalize_by_lengths, \ 130 EmbeddingLookup_##IndexType##_##InType##_##OutType##_##IS_WEIGHT_POSITIONAL, \ 140 normalize_by_lengths, \ 143 EmbeddingLookup_##IndexType##_##InType##_##OutType##_##IS_WEIGHT_POSITIONAL, \ 153 normalize_by_lengths, \ 157 EMBEDDING_SPECIALIZATION(int32_t,
float,
float,
false);
158 EMBEDDING_SPECIALIZATION(int64_t,
float,
float,
false);
159 EMBEDDING_SPECIALIZATION(int32_t, float16,
float,
false);
160 EMBEDDING_SPECIALIZATION(int64_t, float16,
float,
false);
161 EMBEDDING_SPECIALIZATION(int32_t, uint8_t,
float,
false);
162 EMBEDDING_SPECIALIZATION(int64_t, uint8_t,
float,
false);
164 EMBEDDING_SPECIALIZATION(int32_t,
float,
float,
true);
165 EMBEDDING_SPECIALIZATION(int64_t,
float,
float,
true);
166 EMBEDDING_SPECIALIZATION(int32_t, float16,
float,
true);
167 EMBEDDING_SPECIALIZATION(int64_t, float16,
float,
true);
168 EMBEDDING_SPECIALIZATION(int32_t, uint8_t,
float,
true);
169 EMBEDDING_SPECIALIZATION(int64_t, uint8_t,
float,
true);
171 #undef EMBEDDING_SPECIALIZATION A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...