1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_REQUANTIZE_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_REQUANTIZE_H_
17
18 #include "ruy/profiler/instrumentation.h" // from @ruy
19 #include "tensorflow/lite/kernels/internal/common.h"
20 #include "tensorflow/lite/kernels/internal/types.h"
21
22 namespace tflite {
23 namespace reference_ops {
24
25 template <typename input_type, typename output_type>
Requantize(const input_type * input_data,int32_t size,int32_t effective_scale_multiplier,int32_t effective_scale_shift,int32_t input_zeropoint,int32_t output_zeropoint,output_type * output_data)26 inline void Requantize(const input_type* input_data, int32_t size,
27 int32_t effective_scale_multiplier,
28 int32_t effective_scale_shift, int32_t input_zeropoint,
29 int32_t output_zeropoint, output_type* output_data) {
30 ruy::profiler::ScopeLabel label("Requantize");
31 const bool same_scale =
32 (effective_scale_multiplier == 1 << 30 && effective_scale_shift == 1);
33 if (same_scale) {
34 const bool mixed_type_int8_uint8 =
35 std::is_same<input_type, int8_t>::value &&
36 std::is_same<output_type, uint8_t>::value;
37 const bool mixed_type_uint8_int8 =
38 std::is_same<input_type, uint8_t>::value &&
39 std::is_same<output_type, int8_t>::value;
40 const int32_t zero_point_diff = input_zeropoint - output_zeropoint;
41 // Fast path to do requantization for the case when just a shift of 128 is
42 // needed.
43 if ((mixed_type_int8_uint8 && zero_point_diff == -128) ||
44 (mixed_type_uint8_int8 && zero_point_diff == 128)) {
45 for (int i = 0; i < size; ++i) {
46 output_data[i] = input_data[i] ^ 0x80;
47 }
48 return;
49 }
50 }
51 static constexpr int32_t kMinOutput = std::numeric_limits<output_type>::min();
52 static constexpr int32_t kMaxOutput = std::numeric_limits<output_type>::max();
53 for (int i = 0; i < size; ++i) {
54 const int32_t input = input_data[i] - input_zeropoint;
55 const int32_t output =
56 MultiplyByQuantizedMultiplier(input, effective_scale_multiplier,
57 effective_scale_shift) +
58 output_zeropoint;
59 const int32_t clamped_output =
60 std::max(std::min(output, kMaxOutput), kMinOutput);
61 output_data[i] = static_cast<output_type>(clamped_output);
62 }
63 }
64
65 } // namespace reference_ops
66 } // namespace tflite
67
68 #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_REQUANTIZE_H_
69