2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "kernels/LessEqual.h"
19 #include "kernels/TestUtils.h"
20 #include "luci_interpreter/TestMemoryManager.h"
22 namespace luci_interpreter
29 using namespace testing;
31 class LessEqualTest : public ::testing::Test
34 void SetUp() override { _memory_manager = std::make_unique<TestMemoryManager>(); }
36 std::unique_ptr<IMemoryManager> _memory_manager;
39 TEST_F(LessEqualTest, FloatSimple)
41 std::vector<float> x_data{
42 0.5, 0.7, 0.9, // Row 1
46 std::vector<float> y_data{
47 0.9, 0.7, 0.5, // Row 1
51 std::vector<bool> ref_output_data{
52 true, true, false, // Row 1
53 false, true, true, // Row 2
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);
60 LessEqual kernel(&x_tensor, &y_tensor, &output_tensor);
62 _memory_manager->allocate_memory(output_tensor);
65 EXPECT_THAT(extractTensorData<bool>(output_tensor), ::testing::ElementsAreArray(ref_output_data));
66 EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({2, 3}));
69 TEST_F(LessEqualTest, FloatBroardcast)
71 std::vector<float> x_data{
72 0.5, 0.7, 0.9, // Row 1
77 std::vector<float> y_data{
78 0.9, 0.7, 0.5, // Row 1
81 std::vector<bool> ref_output_data{
82 true, true, false, // Row 1
83 false, true, true, // Row 2
84 true, true, false, // Row 3
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);
91 LessEqual kernel(&x_tensor, &y_tensor, &output_tensor);
93 _memory_manager->allocate_memory(output_tensor);
96 EXPECT_THAT(extractTensorData<bool>(output_tensor), ::testing::ElementsAreArray(ref_output_data));
97 EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({3, 3}));
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;
104 TEST_F(LessEqualTest, Uint8Quantized)
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
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
116 std::vector<bool> ref_output_data{
117 true, true, false, false, // Row 1
118 false, true, false, true, // Row 2
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);
128 LessEqual kernel(&x_tensor, &y_tensor, &output_tensor);
130 _memory_manager->allocate_memory(output_tensor);
133 EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({1, 2, 4, 1}));
134 EXPECT_THAT(extractTensorData<bool>(output_tensor), ::testing::ElementsAreArray(ref_output_data));
137 TEST_F(LessEqualTest, Uint8QuantizedRescale)
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
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
149 std::vector<bool> ref_output_data{
150 true, true, false, false, // Row 1
151 false, true, false, true, // Row 2
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);
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);
163 LessEqual kernel(&x_tensor, &y_tensor, &output_tensor);
165 _memory_manager->allocate_memory(output_tensor);
168 EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({1, 2, 4, 1}));
169 EXPECT_THAT(extractTensorData<bool>(output_tensor), ::testing::ElementsAreArray(ref_output_data));
172 TEST_F(LessEqualTest, Uint8QuantizedBroadcast)
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
180 std::vector<float> y_data{
181 -1, 0.05, 0, 1, // Row 1
184 std::vector<bool> ref_output_data{
185 false, true, false, true, // Row 1
186 false, false, true, true, // Row 2
187 false, true, false, true, // Row 3
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);
197 LessEqual kernel(&x_tensor, &y_tensor, &output_tensor);
199 _memory_manager->allocate_memory(output_tensor);
202 EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({1, 3, 4, 1}));
203 EXPECT_THAT(extractTensorData<bool>(output_tensor), ::testing::ElementsAreArray(ref_output_data));
206 TEST_F(LessEqualTest, Input_Type_Mismatch_NEG)
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);
212 LessEqual kernel(&x_tensor, &y_tensor, &output_tensor);
213 EXPECT_ANY_THROW(kernel.configure());
216 TEST_F(LessEqualTest, Input_Output_Type_NEG)
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);
222 LessEqual kernel(&x_tensor, &y_tensor, &output_tensor);
223 EXPECT_ANY_THROW(kernel.configure());
227 } // namespace kernels
228 } // namespace luci_interpreter