Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / ResizeBilinear.test.cpp
1 /*
2  * Copyright (c) 2023 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 "loader/ModuleLoader.h"
20 #include "luci_interpreter/test_models/resize_bilinear/FloatResizeBilinearKernel.h"
21 #include "luci_interpreter/test_models/resize_bilinear/U8ResizeBilinearKernel.h"
22 #include "luci_interpreter/test_models/resize_bilinear/NegResizeBilinearKernel.h"
23
24 namespace luci_interpreter
25 {
26 namespace kernels
27 {
28 namespace
29 {
30
31 using namespace testing;
32
33 class ResizeBilinearTest : public ::testing::Test
34 {
35   // Do nothing
36 };
37
38 template <typename T>
39 std::vector<T> checkResizeBilinearKernel(test_kernel::TestDataBase<T> *test_data_base)
40 {
41   MemoryManager memory_manager{};
42   RuntimeModule runtime_module{};
43   bool dealloc_input = true;
44
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);
48
49   auto *main_runtime_graph = runtime_module.getMainGraph();
50   assert(main_runtime_graph->getNumOfInputTensors() == 1);
51
52   // Set input data
53   {
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);
57   }
58
59   runtime_module.execute();
60
61   assert(main_runtime_graph->getNumOfOutputTensors() == 1);
62
63   T *output_data = reinterpret_cast<T *>(main_runtime_graph->getOutputDataByIndex(0));
64   const size_t num_elements = (main_runtime_graph->getOutputDataSizeByIndex(0) / sizeof(T));
65   std::vector<T> output_data_vector(output_data, output_data + num_elements);
66   return output_data_vector;
67 }
68
69 TEST_F(ResizeBilinearTest, Float_P)
70 {
71   test_kernel::TestDataFloatResizeBilinear test_data_kernel(false);
72   std::vector<float> output_data_vector = checkResizeBilinearKernel(&test_data_kernel);
73
74   EXPECT_THAT(output_data_vector,
75               FloatArrayNear(test_data_kernel.get_output_data_by_index(0), 0.0001f));
76 }
77
78 TEST_F(ResizeBilinearTest, HalfPixelCenter_Float_P)
79 {
80
81   test_kernel::TestDataFloatResizeBilinear test_data_kernel(true);
82   std::vector<float> output_data_vector = checkResizeBilinearKernel(&test_data_kernel);
83
84   EXPECT_THAT(output_data_vector,
85               FloatArrayNear(test_data_kernel.get_output_data_by_index(0), 0.0001f));
86 }
87
88 TEST_F(ResizeBilinearTest, Uint8_P)
89 {
90   test_kernel::TestDataUint8ResizeBilinear test_data_kernel(false);
91   std::vector<uint8_t> output_data_vector = checkResizeBilinearKernel<uint8_t>(&test_data_kernel);
92
93   EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
94 }
95
96 TEST_F(ResizeBilinearTest, HalfPixelCenter_Uint8_P)
97 {
98   test_kernel::TestDataUint8ResizeBilinear test_data_kernel(true);
99   std::vector<uint8_t> output_data_vector = checkResizeBilinearKernel<uint8_t>(&test_data_kernel);
100
101   EXPECT_THAT(output_data_vector, test_data_kernel.get_output_data_by_index(0));
102 }
103
104 TEST_F(ResizeBilinearTest, InvalidInputShape_Float_NEG)
105 {
106
107   test_kernel::NegTestDataInvalidInputShapeFloatResizeBilinearKernel test_data_kernel;
108
109   MemoryManager memory_manager{};
110   RuntimeModule runtime_module{};
111   bool dealloc_input = true;
112   // Load model with single op
113   auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
114   EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
115                "");
116 }
117
118 TEST_F(ResizeBilinearTest, InvalidParams_Float_NEG)
119 {
120
121   test_kernel::NegTestDataInvalidParamFloatResizeBilinearKernel test_data_kernel;
122
123   MemoryManager memory_manager{};
124   RuntimeModule runtime_module{};
125   bool dealloc_input = true;
126   // Load model with single op
127   auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
128   EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
129                "");
130 }
131
132 TEST_F(ResizeBilinearTest, InvalidSizeShape_Float_NEG)
133 {
134
135   test_kernel::NegTestDataInvalidSizeShapeDimensionsFloatResizeBilinearKernel test_data_kernel;
136
137   MemoryManager memory_manager{};
138   RuntimeModule runtime_module{};
139   bool dealloc_input = true;
140   // Load model with single op
141   auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
142   EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
143                "");
144 }
145
146 TEST_F(ResizeBilinearTest, InvalidInputShape_uint8_NEG)
147 {
148
149   test_kernel::NegTestDataInvalidInputShapeUint8ResizeBilinearKernel test_data_kernel;
150
151   MemoryManager memory_manager{};
152   RuntimeModule runtime_module{};
153   bool dealloc_input = true;
154   // Load model with single op
155   auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
156   EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
157                "");
158 }
159
160 TEST_F(ResizeBilinearTest, InvalidParams_uint8_NEG)
161 {
162
163   test_kernel::NegTestDataInvalidParamUint8ResizeBilinearKernel test_data_kernel;
164
165   MemoryManager memory_manager{};
166   RuntimeModule runtime_module{};
167   bool dealloc_input = true;
168   // Load model with single op
169   auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
170   EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
171                "");
172 }
173
174 TEST_F(ResizeBilinearTest, InvalidSizeShape_uint8_NEG)
175 {
176
177   test_kernel::NegTestDataInvalidSizeShapeDimensionsUint8ResizeBilinearKernel test_data_kernel;
178
179   MemoryManager memory_manager{};
180   RuntimeModule runtime_module{};
181   bool dealloc_input = true;
182   // Load model with single op
183   auto *model_data_raw = reinterpret_cast<const char *>(test_data_kernel.get_model_ptr());
184   EXPECT_DEATH(ModuleLoader::load(&runtime_module, &memory_manager, model_data_raw, dealloc_input),
185                "");
186 }
187
188 } // namespace
189 } // namespace kernels
190 } // namespace luci_interpreter