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_DEQUANTIZE_H_
16 #define TENSORFLOW_LITE_KERNELS_DEQUANTIZE_H_
17 
18 #include <stdint.h>
19 
20 #include "third_party/eigen3/Eigen/Core"
21 #include "tensorflow/lite/c/common.h"
22 #include "tensorflow/lite/kernels/internal/optimized/optimized_ops.h"
23 #include "tensorflow/lite/kernels/internal/reference/dequantize.h"
24 #include "tensorflow/lite/kernels/internal/reference/integer_ops/dequantize.h"
25 #include "tensorflow/lite/kernels/internal/reference/reference_ops.h"
26 #include "tensorflow/lite/kernels/internal/tensor.h"
27 #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
28 #include "tensorflow/lite/kernels/internal/types.h"
29 
30 namespace tflite {
31 namespace ops {
32 namespace builtin {
33 namespace dequantize {
34 
35 // This file has two implementation of Dequantize.
36 enum KernelType {
37   kReference,
38   kGenericOptimized,
39 };
40 
41 template <KernelType kernel_type>
DequantizeImpl(TfLiteContext * context,TfLiteNode * node,const TfLiteTensor * input,TfLiteTensor * output)42 TfLiteStatus DequantizeImpl(TfLiteContext* context, TfLiteNode* node,
43                             const TfLiteTensor* input, TfLiteTensor* output) {
44   DequantizationParams op_params;
45   op_params.zero_point = input->params.zero_point;
46   op_params.scale = input->params.scale;
47   switch (input->type) {
48     case kTfLiteUInt8:
49       if (kernel_type == kReference) {
50         reference_ops::Dequantize(
51             op_params, GetTensorShape(input), GetTensorData<uint8_t>(input),
52             GetTensorShape(output), GetTensorData<float>(output));
53       } else {
54         optimized_ops::Dequantize(
55             op_params, GetTensorShape(input), GetTensorData<uint8_t>(input),
56             GetTensorShape(output), GetTensorData<float>(output));
57       }
58       break;
59     case kTfLiteInt8:
60       if (kernel_type == kReference) {
61         reference_integer_ops::Dequantize<int8_t>(
62             op_params, GetTensorShape(input), GetTensorData<int8_t>(input),
63             GetTensorShape(output), GetTensorData<float>(output));
64       } else {
65         optimized_ops::Dequantize(
66             op_params, GetTensorShape(input), GetTensorData<int8_t>(input),
67             GetTensorShape(output), GetTensorData<float>(output));
68       }
69       break;
70     case kTfLiteInt16:
71       if (kernel_type == kReference) {
72         reference_integer_ops::Dequantize<int16_t>(
73             op_params, GetTensorShape(input), GetTensorData<int16_t>(input),
74             GetTensorShape(output), GetTensorData<float>(output));
75       } else {
76         optimized_ops::Dequantize(
77             op_params, GetTensorShape(input), GetTensorData<int16_t>(input),
78             GetTensorShape(output), GetTensorData<float>(output));
79       }
80       break;
81     case kTfLiteFloat16: {
82       const Eigen::half* half_data = reinterpret_cast<const Eigen::half*>(
83           GetTensorData<TfLiteFloat16>(input));
84       reference_ops::Dequantize(GetTensorShape(input), half_data,
85                                 GetTensorShape(output),
86                                 GetTensorData<float>(output));
87       break;
88     }
89     default:
90       context->ReportError(context, "Type %d not supported.", input->type);
91       return kTfLiteError;
92   }
93 
94   return kTfLiteOk;
95 }
96 
97 }  // namespace dequantize
98 }  // namespace builtin
99 }  // namespace ops
100 }  // namespace tflite
101 
102 #endif  // TENSORFLOW_LITE_KERNELS_DEQUANTIZE_H_
103