1/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations 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
25namespace tflite {
26namespace xnnpack {
27
28void 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
34void 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
44void 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