1 /* Copyright 2019 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_DEQUANTIZE_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEQUANTIZE_H_
17
18 #include <limits.h>
19
20 #include <vector>
21
22 #include "tensorflow/lite/kernels/internal/common.h"
23 #include "tensorflow/lite/kernels/internal/types.h"
24
25 namespace tflite {
26
27 namespace reference_ops {
28
29 // Dequantizes into a float without rounding.
30 template <typename InputT, typename OutputT>
Dequantize(const tflite::DequantizationParams & op_params,const RuntimeShape & input_shape,const InputT * input_data,const RuntimeShape & output_shape,OutputT * output_data)31 inline void Dequantize(const tflite::DequantizationParams& op_params,
32 const RuntimeShape& input_shape,
33 const InputT* input_data,
34 const RuntimeShape& output_shape, OutputT* output_data) {
35 int32_t zero_point = op_params.zero_point;
36 const double scale = op_params.scale;
37 const int flat_size = MatchingFlatSize(input_shape, output_shape);
38
39 for (int i = 0; i < flat_size; i++) {
40 const int32_t val = input_data[i];
41 const OutputT result = static_cast<OutputT>(scale * (val - zero_point));
42 output_data[i] = result;
43 }
44 }
45
46 // Dequantizes per-channel quantized tensor to float.
47 template <typename T>
PerChannelDequantize(const tflite::PerChannelDequantizationParams & op_params,const RuntimeShape & input_shape,const T * input_data,const RuntimeShape & output_shape,float * output_data)48 inline void PerChannelDequantize(
49 const tflite::PerChannelDequantizationParams& op_params,
50 const RuntimeShape& input_shape, const T* input_data,
51 const RuntimeShape& output_shape, float* output_data) {
52 // Ensure flat size is same.
53 MatchingFlatSize(input_shape, output_shape);
54
55 const int32_t* zero_point = op_params.zero_point;
56 const float* scale = op_params.scale;
57 const int32_t quantized_dimension = op_params.quantized_dimension;
58 const int32_t num_dims = input_shape.DimensionsCount();
59 const int32_t* dims_data = input_shape.DimsData();
60 std::vector<int> current_dim(num_dims, 0);
61
62 do {
63 size_t offset =
64 ReducedOutputOffset(num_dims, reinterpret_cast<const int*>(dims_data),
65 current_dim.data(), 0, nullptr);
66 const int channel = current_dim[quantized_dimension];
67 const int32_t val = input_data[offset];
68 const float result =
69 static_cast<float>(scale[channel] * (val - zero_point[channel]));
70 output_data[offset] = result;
71 } while (NextIndex(num_dims, reinterpret_cast<const int*>(dims_data),
72 current_dim.data()));
73 }
74
75 } // namespace reference_ops
76
77 } // namespace tflite
78 #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_DEQUANTIZE_H_
79