xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/kernels/internal/reference/quantize.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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_QUANTIZE_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_QUANTIZE_H_
17 
18 #include <algorithm>
19 #include <limits>
20 #include <vector>
21 
22 #include "tensorflow/lite/kernels/internal/common.h"
23 #include "tensorflow/lite/kernels/internal/compatibility.h"
24 #include "tensorflow/lite/kernels/internal/cppmath.h"
25 #include "tensorflow/lite/kernels/internal/types.h"
26 
27 namespace tflite {
28 
29 namespace reference_ops {
30 
31 template <typename InputT, typename OutputT>
AffineQuantize(const tflite::QuantizationParams & op_params,const RuntimeShape & input_shape,const InputT * input_data,const RuntimeShape & output_shape,OutputT * output_data)32 inline void AffineQuantize(const tflite::QuantizationParams& op_params,
33                            const RuntimeShape& input_shape,
34                            const InputT* input_data,
35                            const RuntimeShape& output_shape,
36                            OutputT* output_data) {
37   const int32_t zero_point = op_params.zero_point;
38   const double scale = op_params.scale;
39   const int flat_size = MatchingFlatSize(input_shape, output_shape);
40   static constexpr int32_t min_val = std::numeric_limits<OutputT>::min();
41   static constexpr int32_t max_val = std::numeric_limits<OutputT>::max();
42 
43   for (int i = 0; i < flat_size; i++) {
44     const InputT val = input_data[i];
45     int32_t unclamped =
46         static_cast<int32_t>(TfLiteRound(val / static_cast<float>(scale))) +
47         zero_point;
48     int32_t clamped = std::min(std::max(unclamped, min_val), max_val);
49     output_data[i] = clamped;
50   }
51 }
52 
53 // Quantizes per-channel.
54 template <typename InputT, typename OutputT>
PerChannelQuantize(const tflite::PerChannelQuantizationParams & op_params,const RuntimeShape & input_shape,const InputT * input_data,const RuntimeShape & output_shape,OutputT * output_data)55 inline void PerChannelQuantize(
56     const tflite::PerChannelQuantizationParams& op_params,
57     const RuntimeShape& input_shape, const InputT* input_data,
58     const RuntimeShape& output_shape, OutputT* output_data) {
59   // Ensure flat size is same.
60   MatchingFlatSize(input_shape, output_shape);
61 
62   const int32_t* zero_point = op_params.zero_point;
63   const float* scale = op_params.scale;
64   const int32_t quantized_dimension = op_params.quantized_dimension;
65   const int32_t num_dims = input_shape.DimensionsCount();
66   const int32_t* dims_data = input_shape.DimsData();
67   std::vector<int> current_dim(num_dims, 0);
68   static constexpr int32_t min_val = std::numeric_limits<OutputT>::min();
69   static constexpr int32_t max_val = std::numeric_limits<OutputT>::max();
70 
71   do {
72     size_t offset =
73         ReducedOutputOffset(num_dims, reinterpret_cast<const int*>(dims_data),
74                             current_dim.data(), 0, nullptr);
75     const InputT val = input_data[offset];
76     const int channel = current_dim[quantized_dimension];
77     int32_t unclamped = static_cast<int32_t>(TfLiteRound(
78                             val / static_cast<float>(scale[channel]))) +
79                         zero_point[channel];
80     int32_t clamped = std::min(std::max(unclamped, min_val), max_val);
81     output_data[offset] = static_cast<OutputT>(clamped);
82   } while (NextIndex(num_dims, reinterpret_cast<const int*>(dims_data),
83                      current_dim.data()));
84 }
85 
86 }  // namespace reference_ops
87 
88 }  // namespace tflite
89 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_QUANTIZE_H_
90