Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / Add.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/TestUtils.h"
19 #include "luci_interpreter/test_models/add/FloatAddKernel.h"
20 #include "luci_interpreter/test_models/add/IntAddKernel.h"
21 #include "luci_interpreter/test_models/add/NegAddKernel.h"
22
23 #include "loader/ModuleLoader.h"
24
25 namespace luci_interpreter
26 {
27 namespace
28 {
29
30 using namespace testing;
31
32 class AddTest : public ::testing::Test
33 {
34   // Do nothing
35 };
36
37 template <typename T> std::vector<T> checkAddKernel(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() == 2);
49
50   // set left 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   // set right input data
58   {
59     auto *input_tensor_data = reinterpret_cast<T *>(main_runtime_graph->configureGraphInput(1));
60     std::copy(test_data_base->get_input_data_by_index(1).begin(),
61               test_data_base->get_input_data_by_index(1).end(), input_tensor_data);
62   }
63
64   runtime_module.execute();
65
66   assert(main_runtime_graph->getNumOfOutputTensors() == 1);
67
68   T *output_data = reinterpret_cast<T *>(main_runtime_graph->getOutputDataByIndex(0));
69   const size_t num_elements = (main_runtime_graph->getOutputDataSizeByIndex(0) / sizeof(T));
70   std::vector<T> output_data_vector(output_data, output_data + num_elements);
71   return output_data_vector;
72 }
73
74 TEST_F(AddTest, Float_P)
75 {
76   // No broadcast
77   {
78     const bool is_with_broadcast = false;
79     test_kernel::TestDataFloatAdd test_data_float_add_no_broadcasting(is_with_broadcast);
80     std::vector<float> output_data_vector = checkAddKernel(&test_data_float_add_no_broadcasting);
81     EXPECT_THAT(output_data_vector,
82                 kernels::testing::FloatArrayNear(
83                   test_data_float_add_no_broadcasting.get_output_data_by_index(0), 0.0001f));
84   }
85   // With broadcast
86   {
87     const bool is_with_broadcast = true;
88     test_kernel::TestDataFloatAdd test_data_float_add_with_broadcasting(is_with_broadcast);
89     std::vector<float> output_data_vector = checkAddKernel(&test_data_float_add_with_broadcasting);
90     EXPECT_THAT(output_data_vector,
91                 kernels::testing::FloatArrayNear(
92                   test_data_float_add_with_broadcasting.get_output_data_by_index(0), 0.0001f));
93   }
94 }
95
96 TEST_F(AddTest, INT64_P)
97 {
98   // No broadcast
99   {
100     const bool is_with_broadcast = false;
101     test_kernel::TestData64IntAdd test_data_int64_add_no_broadcasting(is_with_broadcast);
102     const auto output_data_vector = checkAddKernel(&test_data_int64_add_no_broadcasting);
103     EXPECT_THAT(output_data_vector,
104                 test_data_int64_add_no_broadcasting.get_output_data_by_index(0));
105   }
106   // With broadcast
107   {
108     const bool is_with_broadcast = true;
109     test_kernel::TestData64IntAdd test_data_int64_add_with_broadcasting(is_with_broadcast);
110     const auto output_data_vector = checkAddKernel(&test_data_int64_add_with_broadcasting);
111     EXPECT_THAT(output_data_vector,
112                 test_data_int64_add_with_broadcasting.get_output_data_by_index(0));
113   }
114 }
115
116 TEST_F(AddTest, INT32_P)
117 {
118   // No broadcast
119   {
120     const bool is_with_broadcast = false;
121     test_kernel::TestData32IntAdd test_data_int32_add_no_broadcasting(is_with_broadcast);
122     const auto output_data_vector = checkAddKernel<int32_t>(&test_data_int32_add_no_broadcasting);
123     EXPECT_THAT(output_data_vector,
124                 test_data_int32_add_no_broadcasting.get_output_data_by_index(0));
125   }
126   // With broadcast
127   {
128     const bool is_with_broadcast = true;
129     test_kernel::TestData32IntAdd test_data_int32_add_with_broadcasting(is_with_broadcast);
130     const auto output_data_vector = checkAddKernel<int32_t>(&test_data_int32_add_with_broadcasting);
131     EXPECT_THAT(output_data_vector,
132                 test_data_int32_add_with_broadcasting.get_output_data_by_index(0));
133   }
134 }
135
136 TEST_F(AddTest, Input_type_mismatch_NEG)
137 {
138   test_kernel::NegTestDataInputMismatchAddKernel test_data_kernel;
139
140   MemoryManager memory_manager{};
141   RuntimeModule runtime_module{};
142   bool dealloc_input = true;
143   // Load model with single op
144   auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
145   EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
146                "");
147 }
148
149 TEST_F(AddTest, No_quant_params_NEG)
150 {
151   test_kernel::NegTestDataNoQuantParamsS16AddKernel test_data_kernel;
152
153   MemoryManager memory_manager{};
154   RuntimeModule runtime_module{};
155   bool dealloc_input = true;
156   // Load model with single op
157   auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
158   EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
159                "");
160 }
161
162 // TODO: add tests for U8 and S16
163 // TODO: add tests for inplace optimizations for all types
164
165 } // namespace
166 } // namespace luci_interpreter