Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / FullyConnected.test.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
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
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "kernels/TestUtils.h"
18 #include "luci_interpreter/test_models/fully_connected/FloatFullyConnectedKernel.h"
19 #include "luci_interpreter/test_models/fully_connected/U8FullyConnectedKernel.h"
20 #include "luci_interpreter/test_models/fully_connected/NegFullyConnectedKernel.h"
21
22 #include "loader/ModuleLoader.h"
23
24 namespace luci_interpreter
25 {
26 namespace
27 {
28
29 using namespace testing;
30
31 class FullyConnectedTest : public ::testing::Test
32 {
33   // Do nothing
34 };
35
36 template <typename T>
37 std::vector<T> checkFullyConnectedKernel(test_kernel::TestDataBase<T> *test_data_base)
38 {
39   MemoryManager memory_manager{};
40   RuntimeModule runtime_module{};
41   bool dealloc_input = true;
42
43   // Load model with single op
44   auto *model_data_raw = reinterpret_cast<const char *>(test_data_base->get_model_ptr());
45   ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input);
46
47   auto *main_runtime_graph = runtime_module.getMainGraph();
48   assert(main_runtime_graph->getNumOfInputTensors() == 1);
49
50   // set input data
51   {
52     auto *input_tensor_data = reinterpret_cast<T *>(main_runtime_graph->configureGraphInput(0));
53     std::copy(test_data_base->get_input_data_by_index(0).begin(),
54               test_data_base->get_input_data_by_index(0).end(), input_tensor_data);
55   }
56
57   runtime_module.execute();
58
59   assert(main_runtime_graph->getNumOfOutputTensors() == 1);
60
61   T *output_data = reinterpret_cast<T *>(main_runtime_graph->getOutputDataByIndex(0));
62   const size_t num_elements = (main_runtime_graph->getOutputDataSizeByIndex(0) / sizeof(T));
63   std::vector<T> output_data_vector(output_data, output_data + num_elements);
64   return output_data_vector;
65 }
66
67 TEST_F(FullyConnectedTest, Float_P)
68 {
69   test_kernel::TestDataFloatFullyConnected test_data_kernel;
70   std::vector<float> output_data_vector = checkFullyConnectedKernel(&test_data_kernel);
71   EXPECT_THAT(output_data_vector, kernels::testing::FloatArrayNear(
72                                     test_data_kernel.get_output_data_by_index(0), 0.0001f));
73 }
74
75 TEST_F(FullyConnectedTest, U8_P)
76 {
77   test_kernel::TestDataU8FullyConnected test_data_kernel;
78   std::vector<uint8_t> output_data_vector = checkFullyConnectedKernel(&test_data_kernel);
79   EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
80 }
81
82 TEST_F(FullyConnectedTest, Wrong_weight_type_NEG)
83 {
84   test_kernel::NegTestDataWrongWeightTypeFullyConnectedKernel test_data_kernel;
85
86   MemoryManager memory_manager{};
87   RuntimeModule runtime_module{};
88   bool dealloc_input = true;
89   // Load model with single op
90   auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
91   EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
92                "");
93 }
94
95 TEST_F(FullyConnectedTest, Wrong_weight_shape_NEG)
96 {
97   test_kernel::NegTestDataWrongWeightShapeFullyConnectedKernel test_data_kernel;
98
99   MemoryManager memory_manager{};
100   RuntimeModule runtime_module{};
101   bool dealloc_input = true;
102   // Load model with single op
103   auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
104   EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
105                "");
106 }
107
108 TEST_F(FullyConnectedTest, Wrong_bias_shape_NEG)
109 {
110   test_kernel::NegTestDataWrongBiasShapeFullyConnectedKernel test_data_kernel;
111
112   MemoryManager memory_manager{};
113   RuntimeModule runtime_module{};
114   bool dealloc_input = true;
115   // Load model with single op
116   auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
117   EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
118                "");
119 }
120
121 // TODO: add tests for S8
122
123 } // namespace
124 } // namespace luci_interpreter