1 /* Copyright 2018 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/c/builtin_op_data.h"
17 #include "tensorflow/lite/c/common.h"
18 #include "tensorflow/lite/micro/kernels/kernel_runner.h"
19 #include "tensorflow/lite/micro/test_helpers.h"
20 #include "tensorflow/lite/micro/testing/micro_test.h"
21 
22 namespace tflite {
23 namespace testing {
24 namespace {
25 
26 template <typename T>
ValidateDequantizeGoldens(TfLiteTensor * tensors,int tensors_size,const T * expected_output_data,T * output_data,int output_length,float tolerance=1e-5)27 void ValidateDequantizeGoldens(TfLiteTensor* tensors, int tensors_size,
28                                const T* expected_output_data, T* output_data,
29                                int output_length, float tolerance = 1e-5) {
30   int inputs_array_data[] = {1, 0};
31   TfLiteIntArray* inputs_array = IntArrayFromInts(inputs_array_data);
32   int outputs_array_data[] = {1, 1};
33   TfLiteIntArray* outputs_array = IntArrayFromInts(outputs_array_data);
34 
35   const TfLiteRegistration registration =
36       tflite::ops::micro::Register_DEQUANTIZE();
37   micro::KernelRunner runner(registration, tensors, tensors_size, inputs_array,
38                              outputs_array,
39                              /*builtin_data=*/nullptr);
40 
41   TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.InitAndPrepare());
42   TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.Invoke());
43 
44   for (int i = 0; i < output_length; ++i) {
45     TF_LITE_MICRO_EXPECT_NEAR(expected_output_data[i], output_data[i], 0.001f);
46   }
47 }
48 
49 template <typename T>
TestDequantizeToFloat(int * input_dims_data,const float * input_data,T * input_data_quantized,float scale,int zero_point,int * output_dims_data,const float * expected_output_data,float * output_data)50 void TestDequantizeToFloat(int* input_dims_data, const float* input_data,
51                            T* input_data_quantized, float scale, int zero_point,
52                            int* output_dims_data,
53                            const float* expected_output_data,
54                            float* output_data) {
55   TfLiteIntArray* input_dims = IntArrayFromInts(input_dims_data);
56   TfLiteIntArray* output_dims = IntArrayFromInts(output_dims_data);
57   const int output_length = ElementCount(*output_dims);
58 
59   // 1 input, 1 output.
60   const int tensors_size = 2;
61   TfLiteTensor tensors[tensors_size] = {
62       CreateQuantizedTensor(input_data, input_data_quantized, input_dims, scale,
63                             zero_point),
64       CreateTensor(output_data, output_dims),
65   };
66 
67   ValidateDequantizeGoldens(tensors, tensors_size, expected_output_data,
68                             output_data, output_length);
69 }
70 
71 template <typename T>
TestDequantizeToInt32(int * input_dims_data,const float * input_data,T * input_data_quantized,float input_scale,int input_zero_point,int * output_dims_data,const int32_t * expected_output_data,float output_scale,int output_zero_point,int32_t * output_data)72 void TestDequantizeToInt32(int* input_dims_data, const float* input_data,
73                            T* input_data_quantized, float input_scale,
74                            int input_zero_point, int* output_dims_data,
75                            const int32_t* expected_output_data,
76                            float output_scale, int output_zero_point,
77                            int32_t* output_data) {
78   TfLiteIntArray* input_dims = IntArrayFromInts(input_dims_data);
79   TfLiteIntArray* output_dims = IntArrayFromInts(output_dims_data);
80   const int output_length = ElementCount(*output_dims);
81 
82   // 1 input, 1 output.
83   const int tensors_size = 2;
84   TfLiteTensor tensors[tensors_size] = {
85       CreateQuantizedTensor(input_data, input_data_quantized, input_dims,
86                             input_scale, input_zero_point),
87       CreateTensor(output_data, output_dims),
88   };
89 
90   tensors[1].params.scale = output_scale;
91   tensors[1].params.zero_point = output_zero_point;
92 
93   ValidateDequantizeGoldens(tensors, tensors_size, expected_output_data,
94                             output_data, output_length);
95 }
96 
97 }  // namespace
98 }  // namespace testing
99 }  // namespace tflite
100 
101 TF_LITE_MICRO_TESTS_BEGIN
102 
TF_LITE_MICRO_TEST(DequantizeOpTestUint8)103 TF_LITE_MICRO_TEST(DequantizeOpTestUint8) {
104   const int length = 10;
105   int dims[] = {2, 5, 2};
106   const float values[] = {-63.5, -63,  -62.5, -62,  -61.5,
107                           62,    62.5, 63,    63.5, 64};
108   const float scale = 0.5;
109   const int zero_point = 127;
110   uint8_t input_quantized[length];
111   float output[length];
112   tflite::testing::TestDequantizeToFloat(dims, values, input_quantized, scale,
113                                          zero_point, dims, values, output);
114 }
115 
TF_LITE_MICRO_TEST(DequantizeOpTestInt8)116 TF_LITE_MICRO_TEST(DequantizeOpTestInt8) {
117   const int length = 10;
118   int dims[] = {2, 5, 2};
119   const float values[] = {-63.5, -63,  -62.5, -62,  -61.5,
120                           62,    62.5, 63,    63.5, 64};
121   const float scale = 0.5;
122   const int zero_point = -1;
123   int8_t input_quantized[length];
124   float output[length];
125   tflite::testing::TestDequantizeToFloat(dims, values, input_quantized, scale,
126                                          zero_point, dims, values, output);
127 }
128 
TF_LITE_MICRO_TEST(DequantizeOpTestInt16)129 TF_LITE_MICRO_TEST(DequantizeOpTestInt16) {
130   const int length = 10;
131   int dims[] = {2, 5, 2};
132   const float values[] = {-63.5, -63,  -62.5, -62,  -61.5,
133                           62,    62.5, 63,    63.5, 64};
134   const float scale = 0.5;
135   const int zero_point = -1;
136   int16_t input_quantized[length];
137   float output[length];
138   tflite::testing::TestDequantizeToFloat(dims, values, input_quantized, scale,
139                                          zero_point, dims, values, output);
140 }
141 
142 TF_LITE_MICRO_TESTS_END
143