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/mul_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 MulBuilderTest : public OpsTest {
28 public:
29     void SetUp() override;
30     void TearDown() override;
31 
32 protected:
33     void SaveParamsTensor(OH_NN_DataType dataType,
34         const std::vector<int32_t> &dim,  const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
35 
36 protected:
37     MulBuilder m_mul;
38     std::vector<uint32_t> m_inputs {0, 1};
39     std::vector<uint32_t> m_outputs {2};
40     std::vector<uint32_t> m_params {3};
41     std::vector<int32_t> m_inputDim {1, 2, 2, 1};
42     std::vector<int32_t> m_outputDim {1, 2, 2, 1};
43     std::vector<int32_t> m_paramDim {};
44 };
45 
SetUp()46 void MulBuilderTest::SetUp() {}
47 
TearDown()48 void MulBuilderTest::TearDown() {}
49 
SaveParamsTensor(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)50 void MulBuilderTest::SaveParamsTensor(OH_NN_DataType dataType,
51     const std::vector<int32_t> &dim, const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
52 {
53     std::shared_ptr<NNTensor> activationTensor = TransToNNTensor(dataType, dim, quantParam, type);
54     int8_t* activationValue = new (std::nothrow) int8_t(0);
55     EXPECT_NE(nullptr, activationValue);
56     activationTensor->SetBuffer(activationValue, sizeof(int8_t));
57     m_allTensors.emplace_back(activationTensor);
58 }
59 
60 /**
61  * @tc.name: mul_build_001
62  * @tc.desc: Verify that the build function returns a successful message.
63  * @tc.type: FUNC
64  */
65 HWTEST_F(MulBuilderTest, mul_build_001, TestSize.Level0)
66 {
67     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
68     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
69     SaveParamsTensor(OH_NN_INT8, m_paramDim, nullptr, OH_NN_MUL_ACTIVATION_TYPE);
70 
71     OH_NN_ReturnCode ret = m_mul.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
72     EXPECT_EQ(OH_NN_SUCCESS, ret);
73 }
74 
75 /**
76  * @tc.name: mul_build_002
77  * @tc.desc: Verify that the build function returns a failed message with true m_isBuild.
78  * @tc.type: FUNC
79  */
80 HWTEST_F(MulBuilderTest, mul_build_002, TestSize.Level0)
81 {
82     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
83     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
84     SaveParamsTensor(OH_NN_INT8, m_paramDim, nullptr, OH_NN_MUL_ACTIVATION_TYPE);
85 
86     EXPECT_EQ(OH_NN_SUCCESS, m_mul.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
87     OH_NN_ReturnCode ret = m_mul.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
88     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
89 }
90 
91 /**
92  * @tc.name: mul_build_003
93  * @tc.desc: Verify that the build function returns a failed message with invalided input.
94  * @tc.type: FUNC
95  */
96 HWTEST_F(MulBuilderTest, mul_build_003, TestSize.Level0)
97 {
98     m_inputs = {0, 1, 2};
99     m_outputs = {3};
100     m_params = {4};
101 
102     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
103     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
104     SaveParamsTensor(OH_NN_INT8, m_paramDim, nullptr, OH_NN_MUL_ACTIVATION_TYPE);
105 
106     OH_NN_ReturnCode ret = m_mul.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
107     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
108 }
109 
110 /**
111  * @tc.name: mul_build_004
112  * @tc.desc: Verify that the build function returns a failed message with invalided output.
113  * @tc.type: FUNC
114  */
115 HWTEST_F(MulBuilderTest, mul_build_004, TestSize.Level0)
116 {
117     m_outputs = {2, 3};
118     m_params = {4};
119 
120     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
121     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
122     SaveParamsTensor(OH_NN_INT8, m_paramDim, nullptr, OH_NN_MUL_ACTIVATION_TYPE);
123 
124     OH_NN_ReturnCode ret = m_mul.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
125     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
126 }
127 
128 /**
129  * @tc.name: mul_build_005
130  * @tc.desc: Verify that the build function returns a failed message with empty allTensor.
131  * @tc.type: FUNC
132  */
133 HWTEST_F(MulBuilderTest, mul_build_005, TestSize.Level0)
134 {
135     OH_NN_ReturnCode ret = m_mul.Build(m_params, m_inputs, m_outputs, m_allTensors);
136     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
137 }
138 
139 /**
140  * @tc.name: mul_build_006
141  * @tc.desc: Verify that the build function returns a failed message without output tensor.
142  * @tc.type: FUNC
143  */
144 HWTEST_F(MulBuilderTest, mul_build_006, TestSize.Level0)
145 {
146     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
147 
148     OH_NN_ReturnCode ret = m_mul.Build(m_params, m_inputsIndex, m_outputs, m_allTensors);
149     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
150 }
151 
152 /**
153  * @tc.name: mul_build_007
154  * @tc.desc: Verify that the build function returns a failed message with invalid activation's dataType.
155  * @tc.type: FUNC
156  */
157 HWTEST_F(MulBuilderTest, mul_build_007, TestSize.Level0)
158 {
159     m_params = {3};
160     std::vector<int32_t> m_paramDim = {};
161 
162     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
163     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
164 
165     std::shared_ptr<NNTensor> activationTensor = TransToNNTensor(OH_NN_FLOAT32, m_paramDim,
166         nullptr, OH_NN_MUL_ACTIVATION_TYPE);
167     float activationValue = 1e-7;
168     activationTensor->SetBuffer(&activationValue, sizeof(activationValue));
169     m_allTensors.emplace_back(activationTensor);
170 
171     OH_NN_ReturnCode ret = m_mul.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
172     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
173     activationTensor->SetBuffer(nullptr, 0);
174 }
175 
176 /**
177  * @tc.name: mul_build_008
178  * @tc.desc: Verify that the build function returns a failed message with invalid activation's dimension.
179  * @tc.type: FUNC
180  */
181 HWTEST_F(MulBuilderTest, mul_build_008, TestSize.Level0)
182 {
183     m_paramDim = {2};
184 
185     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
186     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
187 
188     std::shared_ptr<NNTensor> activationTensor = TransToNNTensor(OH_NN_INT8, m_paramDim,
189         nullptr, OH_NN_MUL_ACTIVATION_TYPE);
190     int8_t activationValue[2] = {0, 1};
191     activationTensor->SetBuffer(activationValue, 2 * sizeof(int8_t));
192     m_allTensors.emplace_back(activationTensor);
193 
194     OH_NN_ReturnCode ret = m_mul.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
195     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
196     activationTensor->SetBuffer(nullptr, 0);
197 }
198 
199 /**
200  * @tc.name: mul_build_009
201  * @tc.desc: Verify that the build function returns a failed message with invalid activation's data.
202  * @tc.type: FUNC
203  */
204 HWTEST_F(MulBuilderTest, mul_build_009, TestSize.Level0)
205 {
206     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
207     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
208 
209     std::shared_ptr<NNTensor> activationTensor = TransToNNTensor(OH_NN_INT8, m_paramDim,
210         nullptr, OH_NN_MUL_ACTIVATION_TYPE);
211     int8_t activationValue = -1;
212     activationTensor->SetBuffer(&activationValue, sizeof(activationValue));
213     m_allTensors.emplace_back(activationTensor);
214 
215     OH_NN_ReturnCode ret = m_mul.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
216     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
217     activationTensor->SetBuffer(nullptr, 0);
218 }
219 
220 /**
221  * @tc.name: mul_build_010
222  * @tc.desc: Verify that the build function returns a failed message with passing invalid param.
223  * @tc.type: FUNC
224  */
225 HWTEST_F(MulBuilderTest, mul_build_010, TestSize.Level0)
226 {
227     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
228     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
229     SaveParamsTensor(OH_NN_INT8, m_paramDim, nullptr, OH_NN_MATMUL_ACTIVATION_TYPE);
230 
231     OH_NN_ReturnCode ret = m_mul.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
232     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
233 }
234 
235 /**
236  * @tc.name: mul_build_011
237  * @tc.desc: Verify that the build function returns a failed message without set buffer for activation.
238  * @tc.type: FUNC
239  */
240 HWTEST_F(MulBuilderTest, mul_build_011, TestSize.Level0)
241 {
242     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
243     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
244 
245     std::shared_ptr<NNTensor> activationTensor = TransToNNTensor(OH_NN_INT8, m_paramDim,
246         nullptr, OH_NN_MUL_ACTIVATION_TYPE);
247     m_allTensors.emplace_back(activationTensor);
248 
249     OH_NN_ReturnCode ret = m_mul.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
250     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
251 }
252 
253 /**
254  * @tc.name: mul_getprimitive_001
255  * @tc.desc: Verify that the getPrimitive function returns a successful message
256  * @tc.type: FUNC
257  */
258 HWTEST_F(MulBuilderTest, mul_getprimitive_001, TestSize.Level0)
259 {
260     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
261     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
262     SaveParamsTensor(OH_NN_INT8, m_paramDim, nullptr, OH_NN_MUL_ACTIVATION_TYPE);
263 
264     int8_t activationValue = 0;
265     EXPECT_EQ(OH_NN_SUCCESS, m_mul.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
266     LiteGraphPrimitvePtr primitive = m_mul.GetPrimitive();
267     LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
268     EXPECT_NE(expectPrimitive, primitive);
269 
270     auto returnValue = mindspore::lite::MindIR_MulFusion_GetActivationType(primitive.get());
271     EXPECT_EQ(returnValue, activationValue);
272 }
273 
274 /**
275  * @tc.name: mul_getprimitive_002
276  * @tc.desc: Verify that the getPrimitive function returns a failed message without build.
277  * @tc.type: FUNC
278  */
279 HWTEST_F(MulBuilderTest, mul_getprimitive_002, TestSize.Level0)
280 {
281     MulBuilder mul;
282     LiteGraphPrimitvePtr primitive = m_mul.GetPrimitive();
283     LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
284     EXPECT_EQ(expectPrimitive, primitive);
285 }
286 }
287 }
288 }