| 89 | } |
| 90 | |
| 91 | void 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 | |
| 122 | Status QuantizeEmbeddingVariable(const string& input_prefix, |
| 123 | const string& output_prefix, |
no test coverage detected