1 /*
2  * Copyright (c) 2023 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/constant_of_shape_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 ConstantOfShapeBuilderTest : public OpsTest {
28 public:
29     void SetUp() override;
30     void TearDown() override;
31 
32 protected:
33     void SaveDataType(OH_NN_DataType dataType,
34         const std::vector<int32_t> &dim,  const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
35     void SaveValue(OH_NN_DataType dataType,
36         const std::vector<int32_t> &dim,  const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
37 
38 protected:
39     ConstantOfShapeBuilder m_builder;
40     std::vector<uint32_t> m_inputs {0};
41     std::vector<uint32_t> m_outputs {1};
42     std::vector<uint32_t> m_params {2, 3};
43     std::vector<int32_t> m_inputDim {3};
44     std::vector<int32_t> m_outputDim {3};
45     std::vector<int32_t> m_dataTypeDim {};
46     std::vector<int32_t> m_valueDim {1};
47 };
48 
SetUp()49 void ConstantOfShapeBuilderTest::SetUp() {}
50 
TearDown()51 void ConstantOfShapeBuilderTest::TearDown() {}
52 
SaveDataType(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)53 void ConstantOfShapeBuilderTest::SaveDataType(OH_NN_DataType dataType,
54     const std::vector<int32_t> &dim, const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
55 {
56     std::shared_ptr<NNTensor> dataTypeTensor = TransToNNTensor(dataType, dim, quantParam, type);
57     int64_t* dataTypeValue = new (std::nothrow) int64_t[1] {0};
58     dataTypeTensor->SetBuffer(dataTypeValue, sizeof(int64_t));
59     m_allTensors.emplace_back(dataTypeTensor);
60 }
61 
SaveValue(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)62 void ConstantOfShapeBuilderTest::SaveValue(OH_NN_DataType dataType,
63     const std::vector<int32_t> &dim, const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
64 {
65     std::shared_ptr<NNTensor> valueTensor = TransToNNTensor(dataType, dim, quantParam, type);
66     float* valueValue = new (std::nothrow) float[1] {1.0f};
67     int32_t valueSize = 1;
68     EXPECT_NE(nullptr, valueValue);
69     valueTensor->SetBuffer(valueValue, sizeof(float) * valueSize);
70     m_allTensors.emplace_back(valueTensor);
71 }
72 
73 /**
74  * @tc.name: constant_of_shape_build_001
75  * @tc.desc: Verify that the build function returns a successful message.
76  * @tc.type: FUNC
77  */
78 HWTEST_F(ConstantOfShapeBuilderTest, constant_of_shape_build_001, TestSize.Level1)
79 {
80     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
81     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
82     SaveDataType(OH_NN_INT64, m_dataTypeDim, nullptr, OH_NN_CONSTANT_OF_SHAPE_DATA_TYPE);
83     SaveValue(OH_NN_FLOAT32, m_valueDim, nullptr, OH_NN_CONSTANT_OF_SHAPE_VALUE);
84 
85     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
86     EXPECT_EQ(OH_NN_SUCCESS, ret);
87 }
88 
89 /**
90  * @tc.name: constant_of_shape_build_002
91  * @tc.desc: Verify that the build function returns a failed message with true m_isBuild.
92  * @tc.type: FUNC
93  */
94 HWTEST_F(ConstantOfShapeBuilderTest, constant_of_shape_build_002, TestSize.Level1)
95 {
96     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
97     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
98     SaveDataType(OH_NN_INT64, m_dataTypeDim, nullptr, OH_NN_CONSTANT_OF_SHAPE_DATA_TYPE);
99     SaveValue(OH_NN_FLOAT32, m_valueDim, nullptr, OH_NN_CONSTANT_OF_SHAPE_VALUE);
100 
101     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
102     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
103     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
104 }
105 
106 /**
107  * @tc.name: constant_of_shape_build_003
108  * @tc.desc: Verify that the build function returns a failed message with invalided input.
109  * @tc.type: FUNC
110  */
111 HWTEST_F(ConstantOfShapeBuilderTest, constant_of_shape_build_003, TestSize.Level1)
112 {
113     m_inputs = {0, 1};
114     m_outputs = {2};
115     m_params = {3, 4};
116 
117     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
118     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
119     SaveDataType(OH_NN_INT64, m_dataTypeDim, nullptr, OH_NN_CONSTANT_OF_SHAPE_DATA_TYPE);
120     SaveValue(OH_NN_FLOAT32, m_valueDim, nullptr, OH_NN_CONSTANT_OF_SHAPE_VALUE);
121 
122     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
123     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
124 }
125 
126 /**
127  * @tc.name: constant_of_shape_build_004
128  * @tc.desc: Verify that the build function returns a failed message with invalided output.
129  * @tc.type: FUNC
130  */
131 HWTEST_F(ConstantOfShapeBuilderTest, constant_of_shape_build_004, TestSize.Level1)
132 {
133     m_outputs = {1, 2};
134     m_params = {3, 4};
135 
136     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
137     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
138     SaveDataType(OH_NN_INT64, m_dataTypeDim, nullptr, OH_NN_CONSTANT_OF_SHAPE_DATA_TYPE);
139     SaveValue(OH_NN_FLOAT32, m_valueDim, nullptr, OH_NN_CONSTANT_OF_SHAPE_VALUE);
140 
141     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
142     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
143 }
144 
145 /**
146  * @tc.name: constant_of_shape_build_005
147  * @tc.desc: Verify that the build function returns a failed message with empty allTensor.
148  * @tc.type: FUNC
149  */
150 HWTEST_F(ConstantOfShapeBuilderTest, constant_of_shape_build_005, TestSize.Level1)
151 {
152     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputs, m_outputs, m_allTensors);
153     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
154 }
155 
156 /**
157  * @tc.name: constant_of_shape_build_006
158  * @tc.desc: Verify that the build function returns a failed message without output tensor.
159  * @tc.type: FUNC
160  */
161 HWTEST_F(ConstantOfShapeBuilderTest, constant_of_shape_build_006, TestSize.Level1)
162 {
163     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
164 
165     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputs, m_allTensors);
166     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
167 }
168 
169 /**
170  * @tc.name: constant_of_shape_build_007
171  * @tc.desc: Verify that the build function returns a failed message with invalid dataType's dataType.
172  * @tc.type: FUNC
173  */
174 HWTEST_F(ConstantOfShapeBuilderTest, constant_of_shape_build_007, TestSize.Level1)
175 {
176     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
177     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
178 
179     std::shared_ptr<NNTensor> dataTypeTensor = TransToNNTensor(OH_NN_FLOAT32, m_dataTypeDim,
180         nullptr, OH_NN_CONSTANT_OF_SHAPE_DATA_TYPE);
__anon8f362cff0202null181     float* dataTypeValue = new (std::nothrow) float [1]{0.0f};
182     dataTypeTensor->SetBuffer(dataTypeValue, sizeof(float));
183     m_allTensors.emplace_back(dataTypeTensor);
184     SaveValue(OH_NN_FLOAT32, m_valueDim, nullptr, OH_NN_CONSTANT_OF_SHAPE_VALUE);
185 
186     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
187     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
188 }
189 
190 /**
191  * @tc.name: constant_of_shape_build_008
192  * @tc.desc: Verify that the build function returns a failed message with invalid value's dataType.
193  * @tc.type: FUNC
194  */
195 HWTEST_F(ConstantOfShapeBuilderTest, constant_of_shape_build_008, TestSize.Level1)
196 {
197     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
198     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
199 
200     SaveDataType(OH_NN_INT64, m_dataTypeDim, nullptr, OH_NN_CONSTANT_OF_SHAPE_DATA_TYPE);
201     std::shared_ptr<NNTensor> valueTensor = TransToNNTensor(OH_NN_INT64, m_valueDim,
202         nullptr, OH_NN_CONSTANT_OF_SHAPE_VALUE);
203     int64_t* valueValue = new (std::nothrow) int64_t[1] {1};
204     int32_t valueSize = 1;
205     EXPECT_NE(nullptr, valueValue);
206     valueTensor->SetBuffer(valueValue, sizeof(float) * valueSize);
207     m_allTensors.emplace_back(valueTensor);
208 
209     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
210     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
211 }
212 
213 /**
214  * @tc.name: constant_of_shape_build_009
215  * @tc.desc: Verify that the build function returns a failed message with passing invalid dataType param.
216  * @tc.type: FUNC
217  */
218 HWTEST_F(ConstantOfShapeBuilderTest, constant_of_shape_build_009, TestSize.Level1)
219 {
220     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
221     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
222     SaveDataType(OH_NN_INT64, m_dataTypeDim, nullptr, OH_NN_MUL_ACTIVATION_TYPE);
223     SaveValue(OH_NN_FLOAT32, m_valueDim, nullptr, OH_NN_CONSTANT_OF_SHAPE_VALUE);
224 
225     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
226     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
227 }
228 
229 /**
230  * @tc.name: constant_of_shape_build_010
231  * @tc.desc: Verify that the build function returns a failed message with passing invalid value param.
232  * @tc.type: FUNC
233  */
234 HWTEST_F(ConstantOfShapeBuilderTest, constant_of_shape_build_010, TestSize.Level1)
235 {
236     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
237     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
238     SaveDataType(OH_NN_INT64, m_dataTypeDim, nullptr, OH_NN_CONSTANT_OF_SHAPE_DATA_TYPE);
239     SaveValue(OH_NN_FLOAT32, m_valueDim, nullptr, OH_NN_MUL_ACTIVATION_TYPE);
240 
241     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
242     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
243 }
244 
245 /**
246  * @tc.name: constant_of_shape_build_011
247  * @tc.desc: Verify that the build function returns a failed message without set buffer for dataType.
248  * @tc.type: FUNC
249  */
250 HWTEST_F(ConstantOfShapeBuilderTest, constant_of_shape_build_011, TestSize.Level1)
251 {
252     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
253     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
254 
255     std::shared_ptr<NNTensor> dataTypeTensor = TransToNNTensor(OH_NN_INT64, m_dataTypeDim,
256         nullptr, OH_NN_CONSTANT_OF_SHAPE_DATA_TYPE);
257     m_allTensors.emplace_back(dataTypeTensor);
258     SaveValue(OH_NN_FLOAT32, m_valueDim, nullptr, OH_NN_CONSTANT_OF_SHAPE_VALUE);
259 
260     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
261     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
262 }
263 
264 /**
265  * @tc.name: constant_of_shape_build_012
266  * @tc.desc: Verify that the build function returns a failed message without set buffer for value.
267  * @tc.type: FUNC
268  */
269 HWTEST_F(ConstantOfShapeBuilderTest, constant_of_shape_build_012, TestSize.Level1)
270 {
271     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
272     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
273 
274     SaveDataType(OH_NN_INT64, m_dataTypeDim, nullptr, OH_NN_CONSTANT_OF_SHAPE_DATA_TYPE);
275     std::shared_ptr<NNTensor> valueTensor = TransToNNTensor(OH_NN_FLOAT32, m_valueDim,
276         nullptr, OH_NN_CONSTANT_OF_SHAPE_VALUE);
277     m_allTensors.emplace_back(valueTensor);
278 
279     OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
280     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
281 }
282 
283 /**
284  * @tc.name: constant_of_shape_getprimitive_001
285  * @tc.desc: Verify that the getPrimitive function returns a successful message
286  * @tc.type: FUNC
287  */
288 HWTEST_F(ConstantOfShapeBuilderTest, constant_of_shape_getprimitive_001, TestSize.Level1)
289 {
290     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
291     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
292     SaveDataType(OH_NN_INT64, m_dataTypeDim, nullptr, OH_NN_CONSTANT_OF_SHAPE_DATA_TYPE);
293     SaveValue(OH_NN_FLOAT32, m_valueDim, nullptr, OH_NN_CONSTANT_OF_SHAPE_VALUE);
294 
295     int64_t dataTypeValue = 0;
296     std::vector<float> valueValue = {1.0f};
297     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
298     LiteGraphPrimitvePtr primitive = m_builder.GetPrimitive();
299     LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
300     EXPECT_NE(expectPrimitive, primitive);
301 
302     auto returnDataTypeValue = mindspore::lite::MindIR_ConstantOfShape_GetDataType(primitive.get());
303     EXPECT_EQ(returnDataTypeValue, dataTypeValue);
304     auto returnValue = mindspore::lite::MindIR_ConstantOfShape_GetValue(primitive.get());
305     auto returnValueSize = returnValue.size();
306     for (size_t i = 0; i < returnValueSize; ++i) {
307         EXPECT_EQ(returnValue[i], valueValue[i]);
308     }
309 }
310 
311 /**
312  * @tc.name: constant_of_shape_getprimitive_002
313  * @tc.desc: Verify that the getPrimitive function returns a failed message without build.
314  * @tc.type: FUNC
315  */
316 HWTEST_F(ConstantOfShapeBuilderTest, constant_of_shape_getprimitive_002, TestSize.Level1)
317 {
318     LiteGraphPrimitvePtr primitive = m_builder.GetPrimitive();
319     LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
320     EXPECT_EQ(expectPrimitive, primitive);
321 }
322 }
323 }
324 }