Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / Concatenation.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/Concatenation.h"
18 #include "kernels/TestUtils.h"
19 #include "luci_interpreter/TestMemoryManager.h"
20
21 namespace luci_interpreter
22 {
23 namespace kernels
24 {
25 namespace
26 {
27
28 using namespace testing;
29
30 class ConcatenationTest : public ::testing::Test
31 {
32 protected:
33   void SetUp() override { _memory_manager = std::make_unique<TestMemoryManager>(); }
34
35   std::unique_ptr<IMemoryManager> _memory_manager;
36 };
37
38 TEST_F(ConcatenationTest, Float)
39 {
40   std::vector<float> input1_data{1, 2, 3, 4, 5, 6};
41   std::vector<float> input2_data{7, 8, 9, 10, 11, 12};
42   Tensor input1_tensor =
43     makeInputTensor<DataType::FLOAT32>({2, 3}, input1_data, _memory_manager.get());
44   Tensor input2_tensor =
45     makeInputTensor<DataType::FLOAT32>({2, 3}, input2_data, _memory_manager.get());
46   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
47   ConcatenationParams params{};
48
49   // Try different 'axis' and expect different results.
50   {
51     params.axis = 0;
52     params.activation = luci::FusedActFunc::NONE;
53
54     Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
55     kernel.configure();
56     for (auto t : kernel.getOutputTensors())
57     {
58       _memory_manager->allocate_memory(*t);
59     }
60     kernel.execute();
61
62     EXPECT_THAT(extractTensorData<float>(output_tensor),
63                 FloatArrayNear({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}));
64   }
65   {
66     params.axis = -2; // Same as '0'.
67     params.activation = luci::FusedActFunc::NONE;
68
69     Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
70     kernel.configure();
71     _memory_manager->allocate_memory(output_tensor);
72     kernel.execute();
73
74     EXPECT_THAT(extractTensorData<float>(output_tensor),
75                 FloatArrayNear({1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}));
76   }
77   {
78     params.axis = 1;
79     params.activation = luci::FusedActFunc::NONE;
80
81     Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
82     kernel.configure();
83     _memory_manager->allocate_memory(output_tensor);
84     kernel.execute();
85
86     EXPECT_THAT(extractTensorData<float>(output_tensor),
87                 FloatArrayNear({1, 2, 3, 7, 8, 9, 4, 5, 6, 10, 11, 12}));
88   }
89   {
90     params.axis = -1; // Same as '1'.
91     params.activation = luci::FusedActFunc::NONE;
92
93     Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
94     kernel.configure();
95     _memory_manager->allocate_memory(output_tensor);
96     kernel.execute();
97
98     EXPECT_THAT(extractTensorData<float>(output_tensor),
99                 FloatArrayNear({1, 2, 3, 7, 8, 9, 4, 5, 6, 10, 11, 12}));
100   }
101 }
102
103 TEST_F(ConcatenationTest, Input_Number_Check_NEG)
104 {
105   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
106   ConcatenationParams params{};
107
108   params.axis = -1;
109   params.activation = luci::FusedActFunc::NONE;
110
111   Concatenation kernel({}, &output_tensor, params);
112   EXPECT_ANY_THROW(kernel.configure());
113 }
114
115 TEST_F(ConcatenationTest, Invalid_Axis_NEG)
116 {
117   std::vector<float> input1_data{1, 2, 3, 4, 5, 6};
118   std::vector<float> input2_data{7, 8, 9, 10, 11, 12};
119   Tensor input1_tensor =
120     makeInputTensor<DataType::FLOAT32>({2, 3}, input1_data, _memory_manager.get());
121   Tensor input2_tensor =
122     makeInputTensor<DataType::FLOAT32>({2, 3}, input2_data, _memory_manager.get());
123   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
124   ConcatenationParams params{};
125
126   params.axis = -3;
127   params.activation = luci::FusedActFunc::NONE;
128
129   Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
130   EXPECT_ANY_THROW(kernel.configure());
131 }
132
133 TEST_F(ConcatenationTest, Mismatching_Input_Type_NEG)
134 {
135   std::vector<float> input1_data{1, 2, 3, 4, 5, 6};
136   std::vector<uint8_t> input2_data{7, 8, 9, 10, 11, 12};
137   Tensor input1_tensor =
138     makeInputTensor<DataType::FLOAT32>({2, 3}, input1_data, _memory_manager.get());
139   Tensor input2_tensor = makeInputTensor<DataType::U8>({2, 3}, input2_data, _memory_manager.get());
140   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
141   ConcatenationParams params{};
142
143   params.axis = -1;
144   params.activation = luci::FusedActFunc::NONE;
145
146   Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
147   EXPECT_ANY_THROW(kernel.configure());
148 }
149
150 TEST_F(ConcatenationTest, Mismatching_Input_Dimension_Num_NEG)
151 {
152   std::vector<float> input1_data{1, 2, 3, 4, 5, 6};
153   std::vector<float> input2_data{7, 8, 9, 10, 11, 12};
154   Tensor input1_tensor =
155     makeInputTensor<DataType::FLOAT32>({2, 3}, input1_data, _memory_manager.get());
156   Tensor input2_tensor =
157     makeInputTensor<DataType::FLOAT32>({1, 2, 3}, input2_data, _memory_manager.get());
158   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
159   ConcatenationParams params{};
160
161   params.axis = -1;
162   params.activation = luci::FusedActFunc::NONE;
163
164   Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
165   EXPECT_ANY_THROW(kernel.configure());
166 }
167
168 TEST_F(ConcatenationTest, Mismatching_Input_Dimension_NEG)
169 {
170   std::vector<float> input1_data{1, 2, 3, 4, 5, 6};
171   std::vector<float> input2_data{7, 8, 9, 10, 11, 12, 13, 14, 15};
172   Tensor input1_tensor =
173     makeInputTensor<DataType::FLOAT32>({2, 3}, input1_data, _memory_manager.get());
174   Tensor input2_tensor =
175     makeInputTensor<DataType::FLOAT32>({3, 3}, input2_data, _memory_manager.get());
176   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
177   ConcatenationParams params{};
178
179   params.axis = -1;
180   params.activation = luci::FusedActFunc::NONE;
181
182   Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
183   EXPECT_ANY_THROW(kernel.configure());
184 }
185
186 TEST_F(ConcatenationTest, Unsupported_Configure_Type_NEG)
187 {
188   std::vector<int8_t> input1_data{1, 2, 3, 4, 5, 6};
189   std::vector<int8_t> input2_data{7, 8, 9, 10, 11, 12};
190   Tensor input1_tensor = makeInputTensor<DataType::S8>({2, 3}, input1_data, _memory_manager.get());
191   Tensor input2_tensor = makeInputTensor<DataType::S8>({2, 3}, input2_data, _memory_manager.get());
192   Tensor output_tensor = makeOutputTensor(DataType::S8);
193   ConcatenationParams params{};
194
195   params.axis = -1;
196   params.activation = luci::FusedActFunc::NONE;
197
198   Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
199   EXPECT_ANY_THROW(kernel.configure());
200 }
201
202 // TODO: Remove this test when concat w/ fused_activation is supported
203 TEST_F(ConcatenationTest, With_Fused_Activation_NEG)
204 {
205   std::vector<float> input1_data{1, 2, 3, 4, 5, 6};
206   std::vector<float> input2_data{7, 8, 9, 10, 11, 12};
207   Tensor input1_tensor =
208     makeInputTensor<DataType::FLOAT32>({2, 3}, input1_data, _memory_manager.get());
209   Tensor input2_tensor =
210     makeInputTensor<DataType::FLOAT32>({2, 3}, input2_data, _memory_manager.get());
211   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
212   ConcatenationParams params{};
213
214   params.axis = 1;
215   params.activation = luci::FusedActFunc::RELU;
216
217   Concatenation kernel({&input1_tensor, &input2_tensor}, &output_tensor, params);
218   EXPECT_ANY_THROW(kernel.configure());
219 }
220
221 } // namespace
222 } // namespace kernels
223 } // namespace luci_interpreter