2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "kernels/TestUtils.h"
18 #include "luci_interpreter/test_models/conv2d/FloatConv2DKernel.h"
19 #include "luci_interpreter/test_models/conv2d/U8Conv2DKernel.h"
20 #include "luci_interpreter/test_models/conv2d/NegConv2DKernel.h"
22 #include "loader/ModuleLoader.h"
24 namespace luci_interpreter
29 using namespace testing;
31 class Conv2DTest : public ::testing::Test
36 template <typename T> std::vector<T> checkConv2DKernel(test_kernel::TestDataBase<T> *test_data_base)
38 MemoryManager memory_manager{};
39 RuntimeModule runtime_module{};
40 bool dealloc_input = true;
42 // Load model with single op
43 auto *model_data_raw = reinterpret_cast<const char *>(test_data_base->get_model_ptr());
44 ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input);
46 auto *main_runtime_graph = runtime_module.getMainGraph();
47 assert(main_runtime_graph->getNumOfInputTensors() == 1);
51 auto *input_tensor_data = reinterpret_cast<T *>(main_runtime_graph->configureGraphInput(0));
52 std::copy(test_data_base->get_input_data_by_index(0).begin(),
53 test_data_base->get_input_data_by_index(0).end(), input_tensor_data);
56 runtime_module.execute();
58 assert(main_runtime_graph->getNumOfOutputTensors() == 1);
60 T *output_data = reinterpret_cast<T *>(main_runtime_graph->getOutputDataByIndex(0));
61 const size_t num_elements = (main_runtime_graph->getOutputDataSizeByIndex(0) / sizeof(T));
62 std::vector<T> output_data_vector(output_data, output_data + num_elements);
63 return output_data_vector;
66 TEST_F(Conv2DTest, Float_P)
68 test_kernel::TestDataFloatConv2D test_data_kernel;
69 std::vector<float> output_data_vector = checkConv2DKernel(&test_data_kernel);
70 EXPECT_THAT(output_data_vector, kernels::testing::FloatArrayNear(
71 test_data_kernel.get_output_data_by_index(0), 0.0001f));
74 TEST_F(Conv2DTest, U8_P)
76 test_kernel::TestDataU8Conv2D test_data_kernel;
77 std::vector<uint8_t> output_data_vector = checkConv2DKernel(&test_data_kernel);
78 EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
81 TEST_F(Conv2DTest, Input_type_mismatch_NEG)
83 test_kernel::NegTestDataInputMismatchConv2DKernel test_data_kernel;
85 MemoryManager memory_manager{};
86 RuntimeModule runtime_module{};
87 bool dealloc_input = true;
88 // Load model with single op
89 auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
90 EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
94 TEST_F(Conv2DTest, Wrong_bias_type_NEG)
96 test_kernel::NegTestDataWrongBiasTypeConv2DKernel test_data_kernel;
98 MemoryManager memory_manager{};
99 RuntimeModule runtime_module{};
100 bool dealloc_input = true;
101 // Load model with single op
102 auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
103 EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
107 TEST_F(Conv2DTest, Invalid_input_type_NEG)
109 test_kernel::NegTestDataInvalidInputTypeConv2DKernel test_data_kernel;
111 MemoryManager memory_manager{};
112 RuntimeModule runtime_module{};
113 bool dealloc_input = true;
114 // Load model with single op
115 auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
116 EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
121 } // namespace luci_interpreter