Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / Less.test.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *    http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "kernels/Less.h"
19 #include "kernels/TestUtils.h"
20 #include "luci_interpreter/TestMemoryManager.h"
21
22 namespace luci_interpreter
23 {
24 namespace kernels
25 {
26 namespace
27 {
28
29 using namespace testing;
30
31 class LessTest : public ::testing::Test
32 {
33 protected:
34   void SetUp() override { _memory_manager = std::make_unique<TestMemoryManager>(); }
35
36   std::unique_ptr<IMemoryManager> _memory_manager;
37 };
38
39 TEST_F(LessTest, FloatSimple)
40 {
41   std::vector<float> x_data{
42     0.5, 0.7, 0.9, // Row 1
43     1,   0,   -1,  // Row 2
44   };
45
46   std::vector<float> y_data{
47     0.9, 0.7, 0.5, // Row 1
48     -1,  0,   1,   // Row 2
49   };
50
51   std::vector<bool> ref_output_data{
52     true,  false, false, // Row 1
53     false, false, true,  // Row 2
54   };
55
56   Tensor x_tensor = makeInputTensor<DataType::FLOAT32>({2, 3}, x_data, _memory_manager.get());
57   Tensor y_tensor = makeInputTensor<DataType::FLOAT32>({2, 3}, y_data, _memory_manager.get());
58   Tensor output_tensor = makeOutputTensor(DataType::BOOL);
59
60   Less kernel(&x_tensor, &y_tensor, &output_tensor);
61   kernel.configure();
62   _memory_manager->allocate_memory(output_tensor);
63   kernel.execute();
64
65   EXPECT_THAT(extractTensorData<bool>(output_tensor), ::testing::ElementsAreArray(ref_output_data));
66   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({2, 3}));
67 }
68
69 TEST_F(LessTest, FloatBroardcast)
70 {
71   std::vector<float> x_data{
72     0.5, 0.7, 0.9, // Row 1
73     1,   0,   -1,  // Row 2
74     -1,  0,   1,   // Row 3
75   };
76
77   std::vector<float> y_data{
78     0.9, 0.7, 0.5, // Row 1
79   };
80
81   std::vector<bool> ref_output_data{
82     true,  false, false, // Row 1
83     false, true,  true,  // Row 2
84     true,  true,  false, // Row 3
85   };
86
87   Tensor x_tensor = makeInputTensor<DataType::FLOAT32>({3, 3}, x_data, _memory_manager.get());
88   Tensor y_tensor = makeInputTensor<DataType::FLOAT32>({1, 3}, y_data, _memory_manager.get());
89   Tensor output_tensor = makeOutputTensor(DataType::BOOL);
90
91   Less kernel(&x_tensor, &y_tensor, &output_tensor);
92   kernel.configure();
93   _memory_manager->allocate_memory(output_tensor);
94   kernel.execute();
95
96   EXPECT_THAT(extractTensorData<bool>(output_tensor), ::testing::ElementsAreArray(ref_output_data));
97   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({3, 3}));
98 }
99
100 // Choose min / max in such a way that there are exactly 256 units to avoid rounding errors.
101 const float F_MIN = -128.0 / 128.0;
102 const float F_MAX = 127.0 / 128.0;
103
104 TEST_F(LessTest, Uint8Quantized)
105 {
106   std::vector<float> x_data{
107     0.5, 0.6, 0.7,  0.9, // Row 1
108     1,   0,   0.05, -1,  // Row 2
109   };
110
111   std::vector<float> y_data{
112     0.9, 0.6,  0.55, 0.5, // Row 1
113     -1,  0.05, 0,    1,   // Row 2
114   };
115
116   std::vector<bool> ref_output_data{
117     true,  false, false, false, // Row 1
118     false, true,  false, true,  // Row 2
119   };
120
121   std::pair<float, int32_t> quant_param = quantizationParams<uint8_t>(F_MIN, F_MAX);
122   Tensor x_tensor = makeInputTensor<DataType::U8>(
123     {1, 2, 4, 1}, quant_param.first, quant_param.second, x_data, _memory_manager.get());
124   Tensor y_tensor = makeInputTensor<DataType::U8>(
125     {1, 2, 4, 1}, quant_param.first, quant_param.second, y_data, _memory_manager.get());
126   Tensor output_tensor = makeOutputTensor(DataType::BOOL);
127
128   Less kernel(&x_tensor, &y_tensor, &output_tensor);
129   kernel.configure();
130   _memory_manager->allocate_memory(output_tensor);
131   kernel.execute();
132
133   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({1, 2, 4, 1}));
134   EXPECT_THAT(extractTensorData<bool>(output_tensor), ::testing::ElementsAreArray(ref_output_data));
135 }
136
137 TEST_F(LessTest, Uint8QuantizedRescale)
138 {
139   std::vector<float> x_data{
140     0.5, 0.6, 0.7,  0.9, // Row 1
141     1,   0,   0.05, -1,  // Row 2
142   };
143
144   std::vector<float> y_data{
145     0.9, 0.6,  0.6, 0.5, // Row 1
146     -1,  0.05, 0,   1,   // Row 2
147   };
148
149   std::vector<bool> ref_output_data{
150     true,  false, false, false, // Row 1
151     false, true,  false, true,  // Row 2
152   };
153
154   std::pair<float, int32_t> x_quant_param = quantizationParams<uint8_t>(F_MIN, F_MAX);
155   std::pair<float, int32_t> y_quant_param = quantizationParams<uint8_t>(F_MIN * 1.2, F_MAX * 1.5);
156
157   Tensor x_tensor = makeInputTensor<DataType::U8>(
158     {1, 2, 4, 1}, x_quant_param.first, x_quant_param.second, x_data, _memory_manager.get());
159   Tensor y_tensor = makeInputTensor<DataType::U8>(
160     {1, 2, 4, 1}, y_quant_param.first, y_quant_param.second, y_data, _memory_manager.get());
161   Tensor output_tensor = makeOutputTensor(DataType::BOOL);
162
163   Less kernel(&x_tensor, &y_tensor, &output_tensor);
164   kernel.configure();
165   _memory_manager->allocate_memory(output_tensor);
166   kernel.execute();
167
168   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({1, 2, 4, 1}));
169   EXPECT_THAT(extractTensorData<bool>(output_tensor), ::testing::ElementsAreArray(ref_output_data));
170 }
171
172 TEST_F(LessTest, Uint8QuantizedBroadcast)
173 {
174   std::vector<float> x_data{
175     0.4,  -0.8, 0.7,  0.3, // Row 1
176     -0.5, 0.1,  0,    0.5, // Row 2
177     1,    0,    0.05, -1,  // Row 3
178   };
179
180   std::vector<float> y_data{
181     -1, 0.05, 0, 1, // Row 1
182   };
183
184   std::vector<bool> ref_output_data{
185     false, true,  false, true, // Row 1
186     false, false, false, true, // Row 2
187     false, true,  false, true, // Row 3
188   };
189
190   std::pair<float, int32_t> quant_param = quantizationParams<uint8_t>(F_MIN, F_MAX);
191   Tensor x_tensor = makeInputTensor<DataType::U8>(
192     {1, 3, 4, 1}, quant_param.first, quant_param.second, x_data, _memory_manager.get());
193   Tensor y_tensor = makeInputTensor<DataType::U8>(
194     {1, 1, 4, 1}, quant_param.first, quant_param.second, y_data, _memory_manager.get());
195   Tensor output_tensor = makeOutputTensor(DataType::BOOL);
196
197   Less kernel(&x_tensor, &y_tensor, &output_tensor);
198   kernel.configure();
199   _memory_manager->allocate_memory(output_tensor);
200   kernel.execute();
201
202   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({1, 3, 4, 1}));
203   EXPECT_THAT(extractTensorData<bool>(output_tensor), ::testing::ElementsAreArray(ref_output_data));
204 }
205
206 TEST_F(LessTest, Input_Type_Mismatch_NEG)
207 {
208   Tensor x_tensor = makeInputTensor<DataType::FLOAT32>({1}, {1.f}, _memory_manager.get());
209   Tensor y_tensor = makeInputTensor<DataType::U8>({1}, {1}, _memory_manager.get());
210   Tensor output_tensor = makeOutputTensor(DataType::BOOL);
211
212   Less kernel(&x_tensor, &y_tensor, &output_tensor);
213   EXPECT_ANY_THROW(kernel.configure());
214 }
215
216 TEST_F(LessTest, Input_Output_Type_NEG)
217 {
218   Tensor x_tensor = makeInputTensor<DataType::FLOAT32>({1}, {1.f}, _memory_manager.get());
219   Tensor y_tensor = makeInputTensor<DataType::FLOAT32>({1}, {1.f}, _memory_manager.get());
220   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
221
222   Less kernel(&x_tensor, &y_tensor, &output_tensor);
223   EXPECT_ANY_THROW(kernel.configure());
224 }
225
226 } // namespace
227 } // namespace kernels
228 } // namespace luci_interpreter