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/TestUtils.h"
19 #include "luci_interpreter/test_models/less/FloatLessKernel.h"
20 #include "luci_interpreter/test_models/less/IntLessKernel.h"
21 #include "luci_interpreter/test_models/less/QuantLessKernel.h"
22 #include "luci_interpreter/test_models/less/NegTestDataLessKernel.h"
24 #include "loader/ModuleLoader.h"
26 namespace luci_interpreter
31 using namespace testing;
33 class LessTest : public ::testing::Test
38 template <typename T, typename U>
39 std::vector<U> checkLessKernel(test_kernel::TestDataBase<T, U> *test_data_base)
41 MemoryManager memory_manager{};
42 RuntimeModule runtime_module{};
43 bool dealloc_input = true;
45 // Load model with single op
46 auto *model_data_raw = reinterpret_cast<const char *>(test_data_base->get_model_ptr());
47 ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input);
49 auto *main_runtime_graph = runtime_module.getMainGraph();
50 assert(main_runtime_graph->getNumOfInputTensors() == 2);
52 // set left input data
54 auto *input_tensor_data = reinterpret_cast<T *>(main_runtime_graph->configureGraphInput(0));
55 std::copy(test_data_base->get_input_data_by_index(0).begin(),
56 test_data_base->get_input_data_by_index(0).end(), input_tensor_data);
59 // set right input data
61 auto *input_tensor_data = reinterpret_cast<T *>(main_runtime_graph->configureGraphInput(1));
62 std::copy(test_data_base->get_input_data_by_index(1).begin(),
63 test_data_base->get_input_data_by_index(1).end(), input_tensor_data);
66 runtime_module.execute();
68 assert(main_runtime_graph->getNumOfOutputTensors() == 1);
70 U *output_data = reinterpret_cast<U *>(main_runtime_graph->getOutputDataByIndex(0));
71 const size_t num_elements = (main_runtime_graph->getOutputDataSizeByIndex(0) / sizeof(U));
72 std::vector<U> output_data_vector(output_data, output_data + num_elements);
73 return output_data_vector;
76 TEST_F(LessTest, FloatNoBroadcast_P)
78 const bool is_with_broadcast = false;
79 test_kernel::TestDataFloatLess test_data_kernel(is_with_broadcast, false);
80 std::vector<bool> output_data_vector = checkLessKernel<float, bool>(&test_data_kernel);
81 EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
84 TEST_F(LessTest, FloatWithBroadcast_P)
86 const bool is_with_broadcast = true;
87 test_kernel::TestDataFloatLess test_data_kernel(is_with_broadcast, false);
88 std::vector<bool> output_data_vector = checkLessKernel<float, bool>(&test_data_kernel);
89 EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
92 TEST_F(LessTest, FloatNoBroadcast_NEG)
94 const bool is_with_broadcast = false;
95 test_kernel::TestDataFloatLess test_data_kernel(is_with_broadcast, true);
96 EXPECT_DEATH(checkLessKernel(&test_data_kernel), "");
99 TEST_F(LessTest, FloatWithBroadcast_NEG)
101 const bool is_with_broadcast = true;
102 test_kernel::TestDataFloatLess test_data_kernel(is_with_broadcast, true);
103 EXPECT_DEATH(checkLessKernel(&test_data_kernel), "");
106 TEST_F(LessTest, IntWithBroadcast_P)
108 const bool is_with_broadcast = true;
109 test_kernel::TestDataIntLess test_data_kernel(is_with_broadcast, false);
110 std::vector<bool> output_data_vector = checkLessKernel<int32_t, bool>(&test_data_kernel);
111 EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
114 TEST_F(LessTest, IntNoBroadcast_P)
116 const bool is_with_broadcast = false;
117 test_kernel::TestDataIntLess test_data_kernel(is_with_broadcast, false);
118 std::vector<bool> output_data_vector = checkLessKernel<int32_t, bool>(&test_data_kernel);
119 EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
122 TEST_F(LessTest, IntWithBroadcast_NEG)
124 const bool is_with_broadcast = true;
125 test_kernel::TestDataIntLess test_data_kernel(is_with_broadcast, true);
126 EXPECT_DEATH(checkLessKernel(&test_data_kernel), "");
129 TEST_F(LessTest, IntNoBroadcast_NEG)
131 const bool is_with_broadcast = false;
132 test_kernel::TestDataIntLess test_data_kernel(is_with_broadcast, true);
133 EXPECT_DEATH(checkLessKernel(&test_data_kernel), "");
136 TEST_F(LessTest, Quant_P)
138 const bool is_with_broadcast = false;
139 test_kernel::TestDataQuantLess test_data_kernel(is_with_broadcast, false);
140 std::vector<bool> output_data_vector = checkLessKernel<uint8_t, bool>(&test_data_kernel);
141 EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
144 TEST_F(LessTest, Quant_NEG)
146 const bool is_with_broadcast = false;
147 test_kernel::TestDataQuantLess test_data_kernel(is_with_broadcast, true);
148 EXPECT_DEATH(checkLessKernel(&test_data_kernel), "");
151 TEST_F(LessTest, Wrong_Output_Type_NEG)
153 test_kernel::NegTestDataLessKernel test_data_kernel;
155 MemoryManager memory_manager{};
156 RuntimeModule runtime_module{};
157 bool dealloc_input = true;
158 // Load model with single op
159 auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
160 EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
165 } // namespace luci_interpreter