Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / Sub.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/sub/FloatSubKernel.h"
20 #include "luci_interpreter/test_models/sub/IntSubKernel.h"
21 #include "luci_interpreter/test_models/sub/NegSubKernel.h"
22
23 #include "loader/ModuleLoader.h"
24
25 namespace luci_interpreter
26 {
27 namespace
28 {
29
30 using namespace testing;
31
32 class SubTest : public ::testing::Test
33 {
34   // Do nothing
35 };
36
37 template <typename T> std::vector<T> checkSubKernel(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(SubTest, Float_P)
75 {
76   // No broadcast
77   {
78     const bool is_with_broadcast = false;
79     test_kernel::TestDataFloatSub test_data_float_kernel(is_with_broadcast);
80     std::vector<float> output_data_vector = checkSubKernel(&test_data_float_kernel);
81     EXPECT_THAT(output_data_vector, kernels::testing::FloatArrayNear(
82                                       test_data_float_kernel.get_output_data_by_index(0), 0.0001f));
83   }
84   // With broadcast
85   {
86     const bool is_with_broadcast = true;
87     test_kernel::TestDataFloatSub test_data_float_kernel(is_with_broadcast);
88     std::vector<float> output_data_vector = checkSubKernel(&test_data_float_kernel);
89     EXPECT_THAT(output_data_vector, kernels::testing::FloatArrayNear(
90                                       test_data_float_kernel.get_output_data_by_index(0), 0.0001f));
91   }
92 }
93
94 TEST_F(SubTest, INT_P)
95 {
96   // No broadcast
97   {
98     const bool is_with_broadcast = false;
99     test_kernel::TestDataIntSub test_data_kernel(is_with_broadcast);
100     const auto output_data_vector = checkSubKernel<int32_t>(&test_data_kernel);
101     EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
102   }
103   // With broadcast
104   {
105     const bool is_with_broadcast = true;
106     test_kernel::TestDataIntSub test_data_kernel(is_with_broadcast);
107     const auto output_data_vector = checkSubKernel<int32_t>(&test_data_kernel);
108     EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
109   }
110 }
111
112 TEST_F(SubTest, Inputs_type_mismatch_NEG)
113 {
114   test_kernel::NegTestDataInputsTypeMismatchSubKernel test_data_kernel;
115   MemoryManager memory_manager{};
116   RuntimeModule runtime_module{};
117   bool dealloc_input = true;
118   // Load model with single op
119   auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
120   EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
121                "");
122 }
123
124 TEST_F(SubTest, Input_output_type_mismatch_NEG)
125 {
126   test_kernel::NegTestDataInputOutputTypeMismatchSubKernel test_data_kernel;
127   MemoryManager memory_manager{};
128   RuntimeModule runtime_module{};
129   bool dealloc_input = true;
130   // Load model with single op
131   auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
132   EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
133                "");
134 }
135
136 TEST_F(SubTest, No_quant_params_NEG)
137 {
138   test_kernel::NegTestDataNoQuantParamsSubKernel test_data_kernel;
139   MemoryManager memory_manager{};
140   RuntimeModule runtime_module{};
141   bool dealloc_input = true;
142   // Load model with single op
143   auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
144   EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
145                "");
146 }
147
148 // TODO: add tests for U8 and S16
149 // TODO: add tests for inplace optimizations for all types
150
151 } // namespace
152 } // namespace luci_interpreter