MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / ConvertToInt8Value

Function ConvertToInt8Value

tensorflow/c/quantize_embedding_variable.cc:91–120  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

89}
90
91void ConvertToInt8Value(const Tensor& in_tensor, const string name,
92 const string scale_name, BundleWriter& writer) {
93 auto in_data = in_tensor.flat<float>();
94 TensorShape shape = in_tensor.shape();
95 Tensor out_tensor(DataTypeToEnum<int8_t>::v(), shape);
96 auto out_data = out_tensor.flat<int8_t>();
97 int embed_dim = shape.dim_size(shape.dims() - 1);
98 Tensor scale_tensor(DT_FLOAT, TensorShape({embed_dim}));
99 auto scale_data = scale_tensor.flat<float>();
100 std::vector<float> max_val(embed_dim, 0.0);
101#if INTEL_MKL
102#pragma omp parallel for num_threads(omp_get_num_procs())
103#endif // INTEL_MKL
104 for (size_t i = 0; i < out_tensor.NumElements(); ++i) {
105 int embed_i = i % embed_dim;
106 max_val[embed_i] = std::max(max_val[embed_i], std::abs(in_data(i)));
107 }
108 for (size_t i = 0; i < embed_dim; ++i) {
109 scale_data(i) = max_val[i] / 127.0;
110 }
111#if INTEL_MKL
112#pragma omp parallel for num_threads(omp_get_num_procs())
113#endif // INTEL_MKL
114 for (size_t i = 0; i < out_tensor.NumElements(); ++i) {
115 int embed_i = i % embed_dim;
116 out_data(i) = static_cast<int8_t>(round(in_data(i) / scale_data(embed_i)));
117 }
118 writer.Add(scale_name, scale_tensor);
119 writer.Add(name, out_tensor);
120}
121
122Status QuantizeEmbeddingVariable(const string& input_prefix,
123 const string& output_prefix,

Callers 1

Calls 9

TensorShapeClass · 0.50
maxFunction · 0.50
absClass · 0.50
roundClass · 0.50
shapeMethod · 0.45
dim_sizeMethod · 0.45
dimsMethod · 0.45
NumElementsMethod · 0.45
AddMethod · 0.45

Tested by

no test coverage detected