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/conv2d_builder.h"
17 
18 #include "ops_test.h"
19 
20 using namespace testing;
21 using namespace testing::ext;
22 using namespace OHOS::NeuralNetworkRuntime::Ops;
23 
24 namespace OHOS {
25 namespace NeuralNetworkRuntime {
26 namespace UnitTest {
27 class Conv2DBuilderPadmodeTest : public OpsTest {
28 public:
29     void SetUp() override;
30     void TearDown() override;
31 
32     void SetConv2dInput();
33     void SetPadMode(OH_NN_DataType dataType,
34         const std::vector<int32_t> &dim,  const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
35     void SetParam();
36 
37 public:
38     Conv2DBuilder m_builder;
39     std::vector<uint32_t> m_inputs{0, 1, 2};
40     std::vector<uint32_t> m_outputs{3};
41     std::vector<uint32_t> m_params{4, 5, 6, 7, 8};
42     std::vector<int32_t> m_output_dim{1, 3, 3, 1};
43     std::vector<int32_t> m_stride_dim{2};
44     std::vector<int32_t> m_dilation_dim{2};
45     std::vector<int32_t> m_param_dim{};
46 };
47 
SetUp()48 void Conv2DBuilderPadmodeTest::SetUp() {}
49 
TearDown()50 void Conv2DBuilderPadmodeTest::TearDown() {}
51 
SetPadMode(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)52 void Conv2DBuilderPadmodeTest::SetPadMode(OH_NN_DataType dataType,
53     const std::vector<int32_t> &dim,  const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
54 {
55     std::shared_ptr<NNTensor> tensor = TransToNNTensor(dataType, dim, quantParam, type);
56     int8_t* padModeValue = new (std::nothrow) int8_t(0);
57     EXPECT_NE(nullptr, padModeValue);
58 
59     tensor->SetBuffer(padModeValue, sizeof(int8_t));
60     m_allTensors.emplace_back(tensor);
61 }
62 
SetParam()63 void Conv2DBuilderPadmodeTest::SetParam()
64 {
65     SetStride(OH_NN_INT64, m_stride_dim, nullptr, OH_NN_CONV2D_STRIDES);
66     SetDilation(OH_NN_INT64, m_dilation_dim, nullptr, OH_NN_CONV2D_DILATION);
67     SetPadMode(OH_NN_INT8, m_param_dim, nullptr, OH_NN_CONV2D_PAD_MODE);
68     SetGroup(OH_NN_INT64, m_param_dim, nullptr, OH_NN_CONV2D_GROUP);
69     SetActivation(OH_NN_INT8, m_param_dim, nullptr, OH_NN_CONV2D_ACTIVATION_TYPE);
70 }
71 
SetConv2dInput()72 void Conv2DBuilderPadmodeTest::SetConv2dInput()
73 {
74     int32_t weightNum = 4;
75     std::vector<int32_t> m_input_dim{1, 4, 4, 1};
76     std::vector<int32_t> weightDim = {1, 2, 2, 1};
77     std::vector<int32_t> biasDim = {1};
78 
79     std::shared_ptr<NNTensor> inputTensor;
80     inputTensor = TransToNNTensor(OH_NN_FLOAT32, m_input_dim, nullptr, OH_NN_TENSOR);
81     m_allTensors.emplace_back(inputTensor);
82 
83     inputTensor = TransToNNTensor(OH_NN_FLOAT32, weightDim, nullptr, OH_NN_TENSOR);
84     float* weightValue = new (std::nothrow) float[4]{1, 1, 1, 1};
85     EXPECT_NE(nullptr, weightValue);
86     inputTensor->SetBuffer(weightValue, weightNum * sizeof(weightValue));
87     m_allTensors.emplace_back(inputTensor);
88 
89     inputTensor = TransToNNTensor(OH_NN_FLOAT32, biasDim, nullptr, OH_NN_TENSOR);
90     float* biasValue = new (std::nothrow) float[1]{0};
91     EXPECT_NE(nullptr, biasValue);
92 
93     inputTensor->SetBuffer(biasValue, sizeof(float));
94     m_allTensors.emplace_back(inputTensor);
95 }
96 
97 
98 /**
99  * @tc.name: conv2d_build_padmode_001
100  * @tc.desc: Verify the success of the build function
101  * @tc.type: FUNC
102  */
103 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_build_padmode_001, TestSize.Level1)
104 {
105     m_paramsIndex = m_params;
106     m_inputsIndex = m_inputs;
107 
108     SetConv2dInput();
109     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
110     SetParam();
111     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
112 }
113 
114 /**
115  * @tc.name: conv2d_build_padmode_002
116  * @tc.desc: Verify the forbidden of the build function
117  * @tc.type: FUNC
118  */
119 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_build_padmode_002, TestSize.Level1)
120 {
121     m_paramsIndex = m_params;
122     m_inputsIndex = m_inputs;
123 
124     SetConv2dInput();
125     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
126     SetParam();
127 
128     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
129     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
130 }
131 
132 /**
133  * @tc.name: conv2d_build_padmode_003
134  * @tc.desc: Verify the missing input of the build function
135  * @tc.type: FUNC
136  */
137 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_build_padmode_003, TestSize.Level1)
138 {
139     m_inputs = {0};
140     m_outputs = {1};
141     m_params = {2, 3, 4, 5, 6};
142     m_paramsIndex = m_params;
143     m_inputsIndex = m_inputs;
144 
145     SetConv2dInput();
146     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
147     SetParam();
148     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
149 }
150 
151 /**
152  * @tc.name: conv2d_build_padmode_004
153  * @tc.desc: Verify the missing output of the build function
154  * @tc.type: FUNC
155  */
156 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_build_padmode_004, TestSize.Level1)
157 {
158     m_inputs = {0, 1, 2};
159     m_outputs = {};
160     m_params = {3, 4, 5, 6, 7};
161     m_paramsIndex = m_params;
162     m_inputsIndex = m_inputs;
163 
164     SetConv2dInput();
165     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
166     SetParam();
167     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
168 }
169 /**
170  * @tc.name: conv2d_build_padmode_005
171  * @tc.desc: Verify the inputIndex out of bounds of the build function
172  * @tc.type: FUNC
173  */
174 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_build_padmode_005, TestSize.Level1)
175 {
176     m_inputs = {0, 1, 9};
177     m_outputs = {3};
178     m_params = {4, 5, 6, 7, 8};
179     m_paramsIndex = m_params;
180     m_inputsIndex = m_inputs;
181 
182     SetConv2dInput();
183     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
184     SetParam();
185     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
186 }
187 
188 /**
189  * @tc.name: conv2d_build_padmode_006
190  * @tc.desc: Verify the outputIndex out of bounds of the build function
191  * @tc.type: FUNC
192  */
193 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_build_padmode_006, TestSize.Level1)
194 {
195     m_inputs = {0, 1, 2};
196     m_outputs = {9};
197     m_params = {4, 5, 6, 7, 8};
198     m_paramsIndex = m_params;
199     m_inputsIndex = m_inputs;
200 
201     SetConv2dInput();
202     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
203     SetParam();
204     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
205 }
206 
207 /**
208  * @tc.name: conv2d_build_padmode_007
209  * @tc.desc: Verify the invalid stride of the build function
210  * @tc.type: FUNC
211  */
212 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_build_padmode_007, TestSize.Level1)
213 {
214     SetConv2dInput();
215     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
216     std::shared_ptr<NNTensor> tensor = TransToNNTensor(OH_NN_INT32, m_stride_dim, nullptr, OH_NN_CONV2D_STRIDES);
217     int32_t* strideValue = new (std::nothrow) int32_t[2]{1, 1};
218     EXPECT_NE(nullptr, strideValue);
219 
220     tensor->SetBuffer(strideValue, 2 * sizeof(int32_t));
221     m_allTensors.emplace_back(tensor);
222 
223     SetDilation(OH_NN_INT64, m_dilation_dim, nullptr, OH_NN_CONV2D_DILATION);
224     SetPadMode(OH_NN_INT8, m_param_dim, nullptr, OH_NN_CONV2D_PAD_MODE);
225     SetGroup(OH_NN_INT64, m_param_dim, nullptr, OH_NN_CONV2D_GROUP);
226     SetActivation(OH_NN_INT8, m_param_dim, nullptr, OH_NN_CONV2D_ACTIVATION_TYPE);
227 
228     m_paramsIndex = m_params;
229     m_inputsIndex = m_inputs;
230     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
231 }
232 
233 /**
234  * @tc.name: conv2d_build_padmode_008
235  * @tc.desc: Verify the invalid dilation of the build function
236  * @tc.type: FUNC
237  */
238 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_build_padmode_008, TestSize.Level1)
239 {
240     SetConv2dInput();
241     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
242     SetStride(OH_NN_INT64, m_stride_dim, nullptr, OH_NN_CONV2D_STRIDES);
243 
244     std::shared_ptr<NNTensor> tensor = TransToNNTensor(OH_NN_INT32, m_dilation_dim, nullptr, OH_NN_CONV2D_DILATION);
245     int32_t* dilationValue = new (std::nothrow) int32_t[2]{1, 1};
246     EXPECT_NE(nullptr, dilationValue);
247 
248     tensor->SetBuffer(dilationValue, 2 * sizeof(int32_t));
249     m_allTensors.emplace_back(tensor);
250 
251     SetPadMode(OH_NN_INT8, m_param_dim, nullptr, OH_NN_CONV2D_PAD_MODE);
252     SetGroup(OH_NN_INT64, m_param_dim, nullptr, OH_NN_CONV2D_GROUP);
253     SetActivation(OH_NN_INT8, m_param_dim, nullptr, OH_NN_CONV2D_ACTIVATION_TYPE);
254 
255     m_paramsIndex = m_params;
256     m_inputsIndex = m_inputs;
257 
258     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
259 }
260 
261 /**
262  * @tc.name: conv2d_build_padmode_009
263  * @tc.desc: Verify the invalid padMode of the build function
264  * @tc.type: FUNC
265  */
266 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_build_padmode_009, TestSize.Level1)
267 {
268     m_paramsIndex = m_params;
269     m_inputsIndex = m_inputs;
270 
271     SetConv2dInput();
272     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
273     SetStride(OH_NN_INT64, m_stride_dim, nullptr, OH_NN_CONV2D_STRIDES);
274     SetDilation(OH_NN_INT64, m_dilation_dim, nullptr, OH_NN_CONV2D_DILATION);
275 
276     std::shared_ptr<NNTensor> tensor = TransToNNTensor(OH_NN_INT32, m_param_dim, nullptr, OH_NN_CONV2D_PAD_MODE);
277     int32_t* padModeValue = new (std::nothrow) int32_t(0);
278     EXPECT_NE(nullptr, padModeValue);
279     tensor->SetBuffer(padModeValue, sizeof(int32_t));
280     m_allTensors.emplace_back(tensor);
281 
282     SetGroup(OH_NN_INT64, m_param_dim, nullptr, OH_NN_CONV2D_GROUP);
283     SetActivation(OH_NN_INT8, m_param_dim, nullptr, OH_NN_CONV2D_ACTIVATION_TYPE);
284     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
285 }
286 
287 /**
288  * @tc.name: conv2d_build_padmode_010
289  * @tc.desc: Verify the invalid group of the build function
290  * @tc.type: FUNC
291  */
292 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_build_padmode_010, TestSize.Level1)
293 {
294     m_paramsIndex = m_params;
295     m_inputsIndex = m_inputs;
296 
297     SetConv2dInput();
298     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
299     SetStride(OH_NN_INT64, m_stride_dim, nullptr, OH_NN_CONV2D_STRIDES);
300     SetDilation(OH_NN_INT64, m_dilation_dim, nullptr, OH_NN_CONV2D_DILATION);
301     SetPadMode(OH_NN_INT8, m_param_dim, nullptr, OH_NN_CONV2D_PAD_MODE);
302 
303     std::shared_ptr<NNTensor> tensor = TransToNNTensor(OH_NN_INT32, m_param_dim, nullptr, OH_NN_CONV2D_GROUP);
304     int32_t* groupValue = new (std::nothrow) int32_t(0);
305     EXPECT_NE(nullptr, groupValue);
306 
307     tensor->SetBuffer(groupValue, sizeof(int32_t));
308     m_allTensors.emplace_back(tensor);
309     SetActivation(OH_NN_INT8, m_param_dim, nullptr, OH_NN_CONV2D_ACTIVATION_TYPE);
310     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
311 }
312 
313 
314 /**
315  * @tc.name: conv2d_build_padmode_011
316  * @tc.desc: Verify the invalid activation of the build function
317  * @tc.type: FUNC
318  */
319 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_build_padmode_011, TestSize.Level1)
320 {
321     m_paramsIndex = m_params;
322     m_inputsIndex = m_inputs;
323 
324     SetConv2dInput();
325     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
326     SetStride(OH_NN_INT64, m_stride_dim, nullptr, OH_NN_CONV2D_STRIDES);
327     SetDilation(OH_NN_INT64, m_dilation_dim, nullptr, OH_NN_CONV2D_DILATION);
328     SetPadMode(OH_NN_INT8, m_param_dim, nullptr, OH_NN_CONV2D_PAD_MODE);
329     SetGroup(OH_NN_INT64, m_param_dim, nullptr, OH_NN_CONV2D_GROUP);
330 
331     std::shared_ptr<NNTensor> tensor = TransToNNTensor(OH_NN_INT32, m_param_dim, nullptr, OH_NN_CONV2D_ACTIVATION_TYPE);
332     int32_t* activationValue = new (std::nothrow) int32_t(0);
333     EXPECT_NE(nullptr, activationValue);
334     tensor->SetBuffer(activationValue, sizeof(int32_t));
335 
336     m_allTensors.emplace_back(tensor);
337     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
338 }
339 
340 /**
341  * @tc.name: conv2d_build_padmode_012
342  * @tc.desc: Verify the group scalar length of the build function
343  * @tc.type: FUNC
344  */
345 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_build_padmode_012, TestSize.Level1)
346 {
347     std::vector<int32_t> groupDim = {2};
348     m_paramsIndex = m_params;
349     m_inputsIndex = m_inputs;
350 
351     SetConv2dInput();
352     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
353     SetStride(OH_NN_INT64, m_stride_dim, nullptr, OH_NN_CONV2D_STRIDES);
354     SetDilation(OH_NN_INT64, m_dilation_dim, nullptr, OH_NN_CONV2D_DILATION);
355     SetPadMode(OH_NN_INT8, m_param_dim, nullptr, OH_NN_CONV2D_PAD_MODE);
356 
357     std::shared_ptr<NNTensor> tensor = TransToNNTensor(OH_NN_INT64, groupDim, nullptr, OH_NN_CONV2D_GROUP);
358     int64_t* groupValue = new (std::nothrow) int64_t[2]{0, 0};
359     EXPECT_NE(nullptr, groupValue);
360     tensor->SetBuffer(groupValue, 2 * sizeof(int64_t));
361     m_allTensors.emplace_back(tensor);
362 
363     SetActivation(OH_NN_INT8, m_param_dim, nullptr, OH_NN_CONV2D_ACTIVATION_TYPE);
364     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
365 }
366 
367 /**
368  * @tc.name: conv2d_build_padmode_013
369  * @tc.desc: Verify the activation scalar length of the build function
370  * @tc.type: FUNC
371  */
372 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_build_padmode_013, TestSize.Level1)
373 {
374     m_paramsIndex = m_params;
375     m_inputsIndex = m_inputs;
376 
377     SetConv2dInput();
378     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
379     SetStride(OH_NN_INT64, m_stride_dim, nullptr, OH_NN_CONV2D_STRIDES);
380     SetDilation(OH_NN_INT64, m_dilation_dim, nullptr, OH_NN_CONV2D_DILATION);
381     SetPadMode(OH_NN_INT8, m_param_dim, nullptr, OH_NN_CONV2D_PAD_MODE);
382     SetGroup(OH_NN_INT64, m_param_dim, nullptr, OH_NN_CONV2D_GROUP);
383 
384     std::vector<int32_t> activationDim = {2};
385     std::shared_ptr<NNTensor> tensor = TransToNNTensor(OH_NN_INT8, activationDim, nullptr,
386         OH_NN_CONV2D_ACTIVATION_TYPE);
387     int8_t* activationValue = new (std::nothrow) int8_t[2]{0, 0};
388     EXPECT_NE(nullptr, activationValue);
389 
390     tensor->SetBuffer(activationValue, 2 * sizeof(int8_t));
391     m_allTensors.emplace_back(tensor);
392     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
393 }
394 
395 /**
396  * @tc.name: conv2d_build_padmode_014
397  * @tc.desc: Verify the param invalid to conv2d of the build function
398  * @tc.type: FUNC
399  */
400 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_build_padmode_014, TestSize.Level1)
401 {
402     m_paramsIndex = m_params;
403     m_inputsIndex = m_inputs;
404     std::vector<int32_t> activationDim = {2};
405 
406     SetConv2dInput();
407     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
408     SetStride(OH_NN_INT64, m_stride_dim, nullptr, OH_NN_CONV2D_STRIDES);
409     SetDilation(OH_NN_INT64, m_dilation_dim, nullptr, OH_NN_CONV2D_DILATION);
410     SetPadMode(OH_NN_INT8, m_param_dim, nullptr, OH_NN_CONV2D_PAD_MODE);
411     SetGroup(OH_NN_INT64, m_param_dim, nullptr, OH_NN_CONV2D_GROUP);
412 
413     std::shared_ptr<NNTensor> tensor = TransToNNTensor(OH_NN_INT8, activationDim, nullptr,
414         OH_NN_DIV_ACTIVATIONTYPE);
415     int8_t* activationValue = new (std::nothrow) int8_t(0);
416     EXPECT_NE(nullptr, activationValue);
417     tensor->SetBuffer(activationValue, sizeof(int8_t));
418     m_allTensors.emplace_back(tensor);
419     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
420 }
421 
422 /**
423  * @tc.name: conv2d_build_padmode_015
424  * @tc.desc: Verify the pad value of the build function
425  * @tc.type: FUNC
426  */
427 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_build_padmode_015, TestSize.Level1)
428 {
429     std::vector<int32_t> activationDim = {2};
430     m_paramsIndex = m_params;
431     m_inputsIndex = m_inputs;
432 
433     SetConv2dInput();
434     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
435     SetStride(OH_NN_INT64, m_stride_dim, nullptr, OH_NN_CONV2D_STRIDES);
436     SetDilation(OH_NN_INT64, m_dilation_dim, nullptr, OH_NN_CONV2D_DILATION);
437     std::shared_ptr<NNTensor> tensor = TransToNNTensor(OH_NN_INT8, m_param_dim, nullptr, OH_NN_CONV2D_PAD_MODE);
438     int8_t* padModeValue = new (std::nothrow) int8_t(10);
439     EXPECT_NE(nullptr, padModeValue);
440 
441     tensor->SetBuffer(padModeValue, sizeof(int8_t));
442     m_allTensors.emplace_back(tensor);
443     SetGroup(OH_NN_INT64, m_param_dim, nullptr, OH_NN_CONV2D_GROUP);
444     SetActivation(OH_NN_INT8, m_param_dim, nullptr, OH_NN_CONV2D_ACTIVATION_TYPE);
445     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
446 }
447 
448 /**
449  * @tc.name: conv2d_build_padmode_016
450  * @tc.desc: Verify the activation scalar length of the build function
451  * @tc.type: FUNC
452  */
453 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_build_padmode_016, TestSize.Level1)
454 {
455     std::vector<int32_t> activationDim = {2};
456     m_paramsIndex = m_params;
457     m_inputsIndex = m_inputs;
458     int32_t weightNum = 3;
459     std::vector<int32_t> m_input_dim{1, 4, 4, 1};
460     std::vector<int32_t> weightDim = {1, 3, 1};
461     std::vector<int32_t> biasDim = {1};
462 
463     std::shared_ptr<NNTensor> inputTensor;
464     inputTensor = TransToNNTensor(OH_NN_FLOAT32, m_input_dim, nullptr, OH_NN_TENSOR);
465     m_allTensors.emplace_back(inputTensor);
466     inputTensor = TransToNNTensor(OH_NN_FLOAT32, weightDim, nullptr, OH_NN_TENSOR);
__anon58a7d2960302null467     float* weightValue = new (std::nothrow) float[3]{1, 1, 1};
468     EXPECT_NE(nullptr, weightValue);
469 
470     inputTensor->SetBuffer(weightValue, weightNum * sizeof(weightValue));
471     m_allTensors.emplace_back(inputTensor);
472     inputTensor = TransToNNTensor(OH_NN_FLOAT32, biasDim, nullptr, OH_NN_TENSOR);
__anon58a7d2960402null473     float* biasValue = new (std::nothrow) float[1]{0};
474     EXPECT_NE(nullptr, biasValue);
475 
476     inputTensor->SetBuffer(biasValue, sizeof(float));
477     m_allTensors.emplace_back(inputTensor);
478     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
479     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
480 }
481 
482 /**
483  * @tc.name: conv2d_build_padmode_017
484  * @tc.desc: Verify the activation value of the build function
485  * @tc.type: FUNC
486  */
487 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_build_padmode_017, TestSize.Level1)
488 {
489     m_paramsIndex = m_params;
490     m_inputsIndex = m_inputs;
491 
492     SetConv2dInput();
493     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
494     SetStride(OH_NN_INT64, m_stride_dim, nullptr, OH_NN_CONV2D_STRIDES);
495     SetDilation(OH_NN_INT64, m_dilation_dim, nullptr, OH_NN_CONV2D_DILATION);
496     SetPadMode(OH_NN_INT8, m_param_dim, nullptr, OH_NN_CONV2D_PAD_MODE);
497     SetGroup(OH_NN_INT64, m_param_dim, nullptr, OH_NN_CONV2D_GROUP);
498 
499     std::shared_ptr<NNTensor> tensor = TransToNNTensor(OH_NN_INT8, m_param_dim, nullptr,
500         OH_NN_CONV2D_ACTIVATION_TYPE);
501     int8_t* activationValue = new (std::nothrow) int8_t(10);
502     EXPECT_NE(nullptr, activationValue);
503 
504     tensor->SetBuffer(activationValue, sizeof(int8_t));
505     m_allTensors.emplace_back(tensor);
506     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
507 }
508 
509 /**
510  * @tc.name: conv2d_build_padmode_018
511  * @tc.desc: Verify the activation value of the build function
512  * @tc.type: FUNC
513  */
514 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_build_padmode_018, TestSize.Level1)
515 {
516     std::vector<int32_t> m_pad_dim = {3};
517     m_paramsIndex = m_params;
518     m_inputsIndex = m_inputs;
519 
520     SetConv2dInput();
521     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
522     SetStride(OH_NN_INT64, m_stride_dim, nullptr, OH_NN_CONV2D_STRIDES);
523     SetDilation(OH_NN_INT64, m_dilation_dim, nullptr, OH_NN_CONV2D_DILATION);
524 
525     int32_t padNum = 3;
526     std::shared_ptr<NNTensor> tensor = TransToNNTensor(OH_NN_INT64, m_pad_dim, nullptr, OH_NN_CONV2D_PAD);
527     int64_t* padValue = new (std::nothrow) int64_t[3]{1, 1, 1};
528     EXPECT_NE(nullptr, padValue);
529 
530     tensor->SetBuffer(padValue, padNum * sizeof(int64_t));
531     m_allTensors.emplace_back(tensor);
532     EXPECT_EQ(OH_NN_INVALID_PARAMETER, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
533 }
534 
535 /**
536  * @tc.name: conv2d_getprimitive_padmode_001
537  * @tc.desc: Verify the success of the GetPrimitive function
538  * @tc.type: FUNC
539  */
540 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_getprimitive_padmode_001, TestSize.Level1)
541 {
542     m_paramsIndex = m_params;
543     m_inputsIndex = m_inputs;
544 
545     SetConv2dInput();
546     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
547 
548     SetParam();
549     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
550     LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
551     LiteGraphTensorPtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
552     EXPECT_NE(expectPrimitive, primitive);
553 
554     std::vector<int64_t> expectStrides = mindspore::lite::MindIR_Conv2DFusion_GetStride(primitive.get());
555     std::vector<int64_t> strideValueTest{1, 1};
556     EXPECT_EQ(strideValueTest, expectStrides);
557 
558     std::vector<int64_t> expectDliation = mindspore::lite::MindIR_Conv2DFusion_GetDilation(primitive.get());
559     std::vector<int64_t> dilationValueTest{1, 1};
560     EXPECT_EQ(dilationValueTest, expectDliation);
561     int expectpadMode = mindspore::lite::MindIR_Conv2DFusion_GetPadMode(primitive.get());
562     EXPECT_EQ(1, expectpadMode);
563 
564     int expectGroup = mindspore::lite::MindIR_Conv2DFusion_GetGroup(primitive.get());
565     EXPECT_EQ(0, expectGroup);
566 
567     int expectActivation = mindspore::lite::MindIR_Conv2DFusion_GetActivationType(primitive.get());
568     EXPECT_EQ(0, expectActivation);
569 }
570 
571 /**
572  * @tc.name: conv2d_getprimitive_padmode_002
573  * @tc.desc: Verify the nullptr return of the GetPrimitive function
574  * @tc.type: FUNC
575  */
576 HWTEST_F(Conv2DBuilderPadmodeTest, conv2d_getprimitive_padmode_002, TestSize.Level1)
577 {
578     m_paramsIndex = m_params;
579     m_inputsIndex = m_inputs;
580 
581     SetConv2dInput();
582     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_output_dim, nullptr);
583     SetParam();
584 
585     LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
586     LiteGraphTensorPtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
587     EXPECT_EQ(expectPrimitive, primitive);
588 }
589 } // namespace UnitTest
590 } // namespace NeuralNetworkRuntime
591 } // namespace OHOS