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