1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ops/stack_builder.h"
17
18 #include <gtest/gtest.h>
19 #include "nn_tensor.h"
20 #include "ops_test.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24 using namespace OHOS::NeuralNetworkRuntime::Ops;
25
26 namespace OHOS {
27 namespace NeuralNetworkRuntime {
28 namespace UnitTest {
29 class StackBuilderTest : public OpsTest {
30 protected:
31 void InitTensor(const std::vector<uint32_t>& inputsIndex,
32 const std::vector<uint32_t>& outputsIndex) override;
33 void SaveAxisTensor(OH_NN_DataType dataType, const std::vector<int32_t> &dim,
34 const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
35
36 protected:
37 StackBuilder m_builder;
38 int64_t m_expectAxisValue {0};
39 };
40
InitTensor(const std::vector<uint32_t> & inputsIndex,const std::vector<uint32_t> & outputsIndex)41 void StackBuilderTest::InitTensor(const std::vector<uint32_t>& inputsIndex,
42 const std::vector<uint32_t>& outputsIndex)
43 {
44 std::vector<uint32_t> paramsIndex = { 3 };
45 std::vector<int32_t> inputDim = {2};
46 std::vector<int32_t> OutputDim = {2, 2};
47
48 m_paramsIndex = paramsIndex;
49 SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
50 SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
51 }
52
SaveAxisTensor(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)53 void StackBuilderTest::SaveAxisTensor(OH_NN_DataType dataType, const std::vector<int32_t> &dim,
54 const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
55 {
56 std::shared_ptr<NNTensor> axisTensor = TransToNNTensor(dataType, dim, quantParam, type);
57 int64_t* axisValue = new (std::nothrow) int64_t[1]{1};
58 EXPECT_NE(nullptr, axisValue);
59 axisTensor->SetBuffer(axisValue, sizeof(int64_t));
60 m_allTensors.emplace_back(axisTensor);
61 m_expectAxisValue = *axisValue;
62 }
63
64 /**
65 * @tc.name: stack_build_001
66 * @tc.desc: Provide normal input, output, and parameters to verify the normal behavior of the Build function
67 * @tc.type: FUNC
68 */
69 HWTEST_F(StackBuilderTest, stack_build_001, TestSize.Level0)
70 {
71 std::vector<uint32_t> inputsIndex = { 0, 1 };
72 std::vector<uint32_t> outputsIndex = { 2 };
73 std::vector<int32_t> paramDim = {};
74
75 InitTensor(inputsIndex, outputsIndex);
76 SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STACK_AXIS);
77
78 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
79 EXPECT_EQ(OH_NN_SUCCESS, ret);
80 }
81
82 /**
83 * @tc.name: stack_build_002
84 * @tc.desc: Call Build func twice to verify the abnormal behavior of the Build function
85 * @tc.type: FUNC
86 */
87 HWTEST_F(StackBuilderTest, stack_build_002, TestSize.Level0)
88 {
89 std::vector<uint32_t> inputsIndex = { 0, 1 };
90 std::vector<uint32_t> outputsIndex = { 2 };
91 std::vector<int32_t> paramDim = {};
92
93 InitTensor(inputsIndex, outputsIndex);
94 SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STACK_AXIS);
95
96 EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
97 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
98 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
99 }
100
101 /**
102 * @tc.name: stack_build_003
103 * @tc.desc: Provide one more than normal input to verify the normal behavior of the Build function
104 * @tc.type: FUNC
105 */
106 HWTEST_F(StackBuilderTest, stack_build_003, TestSize.Level0)
107 {
108 std::vector<uint32_t> inputsIndex = { 0, 1, 2 };
109 std::vector<uint32_t> outputsIndex = { 3 };
110 std::vector<int32_t> paramDim = {};
111
112 InitTensor(inputsIndex, outputsIndex);
113 SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STACK_AXIS);
114
115 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
116 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
117 }
118
119 /**
120 * @tc.name: stack_build_004
121 * @tc.desc: Provide one more than normal output to verify the abnormal behavior of the Build function
122 * @tc.type: FUNC
123 */
124 HWTEST_F(StackBuilderTest, stack_build_004, TestSize.Level0)
125 {
126 std::vector<uint32_t> inputsIndex = { 0, 1 };
127 std::vector<uint32_t> outputsIndex = { 2, 3 };
128 std::vector<int32_t> paramDim = {};
129
130 InitTensor(inputsIndex, outputsIndex);
131 SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STACK_AXIS);
132
133 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
134 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
135 }
136
137 /**
138 * @tc.name: stack_build_005
139 * @tc.desc: Provide empty input, output, and parameters to verify the abnormal behavior of the Build function
140 * @tc.type: FUNC
141 */
142 HWTEST_F(StackBuilderTest, stack_build_005, TestSize.Level0)
143 {
144 std::vector<uint32_t> inputsIndex = { 0, 1 };
145 std::vector<uint32_t> outputsIndex = {};
146 std::vector<uint32_t> paramsIndex = {};
147
148 OH_NN_ReturnCode ret = m_builder.Build(paramsIndex, inputsIndex, outputsIndex, m_allTensors);
149 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
150 }
151
152 /**
153 * @tc.name: stack_build_006
154 * @tc.desc: Provide empty output to verify the abnormal behavior of the Build function
155 * @tc.type: FUNC
156 */
157 HWTEST_F(StackBuilderTest, stack_build_006, TestSize.Level0)
158 {
159 std::vector<uint32_t> inputsIndex = { 0, 1 };
160 std::vector<uint32_t> outputsIndex = {};
161 std::vector<int32_t> paramDim = {};
162
163 InitTensor(inputsIndex, outputsIndex);
164 SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STACK_AXIS);
165
166 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
167 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
168 }
169
170 /**
171 * @tc.name: stack_build_007
172 * @tc.desc: Provide param type error to verify the abnormal behavior of the Build function
173 * @tc.type: FUNC
174 */
175 HWTEST_F(StackBuilderTest, stack_build_007, TestSize.Level0)
176 {
177 std::vector<uint32_t> inputsIndex = { 0, 1 };
178 std::vector<uint32_t> outputsIndex = { 2 };
179 std::vector<int32_t> paramDim = {};
180
181 InitTensor(inputsIndex, outputsIndex);
182 SaveAxisTensor(OH_NN_INT32, paramDim, nullptr, OH_NN_STACK_AXIS);
183
184 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
185 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
186 }
187
188 /**
189 * @tc.name: stack_build_008
190 * @tc.desc: Provide one less than normal input to verify the abnormal behavior of the Build function
191 * @tc.type: FUNC
192 */
193 HWTEST_F(StackBuilderTest, stack_build_008, TestSize.Level0)
194 {
195 std::vector<uint32_t> inputsIndex = { 0 };
196 std::vector<uint32_t> outputsIndex = { 1 };
197 std::vector<int32_t> paramDim = {};
198
199 InitTensor(inputsIndex, outputsIndex);
200 SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STACK_AXIS);
201
202 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
203 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
204 }
205
206 /**
207 * @tc.name: stack_build_009
208 * @tc.desc: Provide axis parameter buffer is nullptr to verify the abnormal behavior of the Build function
209 * @tc.type: FUNC
210 */
211 HWTEST_F(StackBuilderTest, stack_build_009, TestSize.Level0)
212 {
213 std::vector<uint32_t> inputsIndex = { 0, 1 };
214 std::vector<uint32_t> outputsIndex = { 2 };
215 std::vector<int32_t> paramDim = {};
216
217 InitTensor(inputsIndex, outputsIndex);
218
219 std::shared_ptr<NNTensor> axisTensor =TransToNNTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STACK_AXIS);
220 axisTensor->SetBuffer(nullptr, 0);
221 m_allTensors.emplace_back(axisTensor);
222
223 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
224 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
225 }
226
227 /**
228 * @tc.name: stack_build_010
229 * @tc.desc: Provide axis not scaler to verify the normal behavior of the Build function
230 * @tc.type: FUNC
231 */
232 HWTEST_F(StackBuilderTest, stack_build_010, TestSize.Level0)
233 {
234 std::vector<uint32_t> inputsIndex = { 0, 1 };
235 std::vector<uint32_t> outputsIndex = { 2 };
236 std::vector<int32_t> paramDim = {1, 2};
237
238 InitTensor(inputsIndex, outputsIndex);
239 SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STACK_AXIS);
240
241 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
242 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
243 }
244
245 /**
246 * @tc.name: stack_build_011
247 * @tc.desc: Provide invalid parameter type to verify the abnormal behavior of the Build function
248 * @tc.type: FUNC
249 */
250 HWTEST_F(StackBuilderTest, stack_build_011, TestSize.Level0)
251 {
252 std::vector<uint32_t> inputsIndex = { 0, 1 };
253 std::vector<uint32_t> outputsIndex = { 2 };
254 std::vector<int32_t> paramDim = {};
255
256 InitTensor(inputsIndex, outputsIndex);
257 SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_SCALE_AXIS);
258
259 OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
260 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
261 }
262
263 /**
264 * @tc.name: stack_get_primitive_001
265 * @tc.desc: Verify the GetPrimitive function return nullptr
266 * @tc.type: FUNC
267 */
268 HWTEST_F(StackBuilderTest, stack_get_primitive_001, TestSize.Level0)
269 {
270 LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
271 LiteGraphTensorPtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
272 EXPECT_EQ(primitive, expectPrimitive);
273 }
274
275 /**
276 * @tc.name: stack_get_primitive_002
277 * @tc.desc: Verify the normal return behavior of the getprimitive function
278 * @tc.type: FUNC
279 */
280 HWTEST_F(StackBuilderTest, stack_get_primitive_002, TestSize.Level0)
281 {
282 std::vector<uint32_t> inputsIndex = { 0, 1 };
283 std::vector<uint32_t> outputsIndex = { 2 };
284 std::vector<int32_t> paramDim = {};
285
286 InitTensor(inputsIndex, outputsIndex);
287 SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_STACK_AXIS);
288
289 EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
290 LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
291 LiteGraphTensorPtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
292 EXPECT_NE(primitive, expectPrimitive);
293
294 auto returnValue = mindspore::lite::MindIR_Stack_GetAxis(primitive.get());
295 EXPECT_EQ(returnValue, m_expectAxisValue);
296 }
297 } // namespace UnitTest
298 } // namespace NeuralNetworkRuntime
299 } // namespace OHOS