xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/xnnpack/quantization_util.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 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 
16 #include "tensorflow/lite/delegates/xnnpack/quantization_util.h"
17 
18 #include <algorithm>
19 
20 #include "fp16.h"  // from @FP16
21 #include "tensorflow/lite/kernels/internal/optimized/optimized_ops.h"
22 #include "tensorflow/lite/kernels/internal/reference/reference_ops.h"
23 #include "tensorflow/lite/kernels/internal/types.h"
24 
25 namespace tflite {
26 namespace xnnpack {
27 
DequantizeFloat16(const uint16_t * packed_fp16_data,float * unpacked_fp32_data,size_t tensor_elements)28 void DequantizeFloat16(const uint16_t *packed_fp16_data,
29                        float *unpacked_fp32_data, size_t tensor_elements) {
30   std::transform(packed_fp16_data, packed_fp16_data + tensor_elements,
31                  unpacked_fp32_data, fp16_ieee_to_fp32_value);
32 }
33 
DequantizeInt8(const int8_t * packed_s8_data,float * unpacked_fp32_data,const RuntimeShape & tensor_shape,int32_t zero_point,double scale)34 void DequantizeInt8(const int8_t *packed_s8_data, float *unpacked_fp32_data,
35                     const RuntimeShape &tensor_shape, int32_t zero_point,
36                     double scale) {
37   DequantizationParams op_params;
38   op_params.zero_point = zero_point;
39   op_params.scale = scale;
40   optimized_ops::Dequantize(op_params, tensor_shape, packed_s8_data,
41                             tensor_shape, unpacked_fp32_data);
42 }
43 
PerChannelDequantizeInt8(const int8_t * packed_s8_data,float * unpacked_fp32_data,const RuntimeShape & tensor_shape,const int32_t * zero_points,const float * scales,int32_t quantized_dimension)44 void PerChannelDequantizeInt8(const int8_t* packed_s8_data,
45                               float* unpacked_fp32_data,
46                               const RuntimeShape& tensor_shape,
47                               const int32_t* zero_points, const float* scales,
48                               int32_t quantized_dimension) {
49   PerChannelDequantizationParams op_params;
50   op_params.zero_point = zero_points;
51   op_params.scale = scales;
52   op_params.quantized_dimension = quantized_dimension;
53   reference_ops::PerChannelDequantize(op_params, tensor_shape, packed_s8_data,
54                                       tensor_shape, unpacked_fp32_data);
55 }
56 
57 }  // namespace xnnpack
58 }  // namespace tflite
59