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/softmax_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 SoftmaxBuilderTest : 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     SoftmaxBuilder m_builder;
38     std::vector<int64_t> m_expectAxisValue;
39 };
40 
SaveAxisTensor(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)41 void SoftmaxBuilderTest::SaveAxisTensor(OH_NN_DataType dataType, const std::vector<int32_t> &dim,
42     const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
43 {
44     std::shared_ptr<NNTensor> axisTensor = TransToNNTensor(dataType, dim, quantParam, type);
45     int64_t* axisValue = new (std::nothrow) int64_t[1]{1};
46     EXPECT_NE(nullptr, axisValue);
47     axisTensor->SetBuffer(axisValue, sizeof(int64_t));
48     m_allTensors.emplace_back(axisTensor);
49     m_expectAxisValue.emplace_back(*axisValue);
50 }
51 
InitTensor(const std::vector<uint32_t> & inputsIndex,const std::vector<uint32_t> & outputsIndex)52 void SoftmaxBuilderTest::InitTensor(const std::vector<uint32_t>& inputsIndex,
53     const std::vector<uint32_t>& outputsIndex)
54 {
55     std::vector<uint32_t> paramsIndex = { 2 };
56     std::vector<int32_t> inputDim = {1, 5, 1, 1};
57     std::vector<int32_t> OutputDim = {1, 5, 1, 1};
58 
59     m_paramsIndex = paramsIndex;
60     SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
61     SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
62 }
63 
64 /**
65  * @tc.name: softmax_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(SoftmaxBuilderTest, softmax_build_001, TestSize.Level0)
70 {
71     std::vector<uint32_t> inputsIndex = { 0 };
72     std::vector<uint32_t> outputsIndex = { 1 };
73     std::vector<int32_t> paramDim = {};
74 
75     InitTensor(inputsIndex, outputsIndex);
76     SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_SOFTMAX_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: softmax_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(SoftmaxBuilderTest, softmax_build_002, TestSize.Level0)
88 {
89     std::vector<uint32_t> inputsIndex = { 0 };
90     std::vector<uint32_t> outputsIndex = { 1 };
91     std::vector<int32_t> paramDim = {};
92 
93     InitTensor(inputsIndex, outputsIndex);
94     SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_SOFTMAX_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: softmax_build_003
103  * @tc.desc: Provide two more than normal input to verify the abnormal behavior of the Build function
104  * @tc.type: FUNC
105  */
106 HWTEST_F(SoftmaxBuilderTest, softmax_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_SOFTMAX_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: softmax_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(SoftmaxBuilderTest, softmax_build_004, TestSize.Level0)
125 {
126     std::vector<uint32_t> inputsIndex = { 0 };
127     std::vector<uint32_t> outputsIndex = { 1, 2 };
128     std::vector<int32_t> paramDim = {};
129 
130     InitTensor(inputsIndex, outputsIndex);
131     SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_SOFTMAX_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: softmax_build_005
139  * @tc.desc: Provide empty input, output, and parameters to verify the normal behavior of the Build function
140  * @tc.type: FUNC
141  */
142 HWTEST_F(SoftmaxBuilderTest, softmax_build_005, TestSize.Level0)
143 {
144     std::vector<uint32_t> inputsIndex = { 0, 1 };
145     std::vector<uint32_t> outputsIndex = { 2 };
146     std::vector<uint32_t> paramsIndex = { 3 };
147     OH_NN_ReturnCode ret = m_builder.Build(paramsIndex, inputsIndex, outputsIndex, m_allTensors);
148     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
149 }
150 
151 /**
152  * @tc.name: softmax_build_006
153  * @tc.desc: Provide empty output to verify the normal behavior of the Build function
154  * @tc.type: FUNC
155  */
156 HWTEST_F(SoftmaxBuilderTest, softmax_build_006, TestSize.Level0)
157 {
158     std::vector<uint32_t> inputsIndex = { 0 };
159     std::vector<uint32_t> outputsIndex = {};
160     std::vector<int32_t> paramDim = {};
161 
162     InitTensor(inputsIndex, outputsIndex);
163     SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_SOFTMAX_AXIS);
164 
165     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
166     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
167 }
168 
169 /**
170  * @tc.name: softmax_build_007
171  * @tc.desc: Provide no param error to verify the abnormal behavior of the Build function
172  * @tc.type: FUNC
173  */
174 HWTEST_F(SoftmaxBuilderTest, softmax_build_007, TestSize.Level0)
175 {
176     std::vector<uint32_t> inputsIndex = { 0, 1 };
177     std::vector<uint32_t> outputsIndex = { 2 };
178     std::vector<uint32_t> paramsIndex = {};
179     std::vector<int32_t> inputDim = {1, 5, 1, 1};
180     std::vector<int32_t> OutputDim = {1, 5, 1, 1};
181 
182     m_paramsIndex = paramsIndex;
183     SaveInputTensor(inputsIndex, OH_NN_FLOAT32, inputDim, nullptr);
184     SaveOutputTensor(outputsIndex, OH_NN_FLOAT32, OutputDim, nullptr);
185 
186     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
187     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
188 }
189 
190 /**
191  * @tc.name: softmax_build_008
192  * @tc.desc: Provide param type error to verify the abnormal behavior of the Build function
193  * @tc.type: FUNC
194  */
195 HWTEST_F(SoftmaxBuilderTest, softmax_build_008, TestSize.Level0)
196 {
197     std::vector<uint32_t> inputsIndex = { 0 };
198     std::vector<uint32_t> outputsIndex = { 1 };
199     std::vector<int32_t> paramDim = {};
200 
201     InitTensor(inputsIndex, outputsIndex);
202     SaveAxisTensor(OH_NN_INT32, paramDim, nullptr, OH_NN_SOFTMAX_AXIS);
203 
204     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
205     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
206 }
207 
208 /**
209  * @tc.name: softmax_build_009
210  * @tc.desc: Provide param dimension error to verify the abnormal behavior of the Build function
211  * @tc.type: FUNC
212  */
213 HWTEST_F(SoftmaxBuilderTest, softmax_build_009, TestSize.Level0)
214 {
215     std::vector<uint32_t> inputsIndex = { 0 };
216     std::vector<uint32_t> outputsIndex = { 1 };
217     std::vector<int32_t> paramDim = {1, 5, 1, 1};
218 
219     InitTensor(inputsIndex, outputsIndex);
220     SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_SOFTMAX_AXIS);
221 
222     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
223     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
224 }
225 
226 /**
227  * @tc.name: softmax_build_010
228  * @tc.desc: Provide parameter buffer is nullptr to verify the abnormal behavior of the Build function
229  * @tc.type: FUNC
230  */
231 HWTEST_F(SoftmaxBuilderTest, softmax_build_010, TestSize.Level0)
232 {
233     std::vector<uint32_t> inputsIndex = { 0 };
234     std::vector<uint32_t> outputsIndex = { 1 };
235     std::vector<int32_t> paramDim = {};
236 
237     InitTensor(inputsIndex, outputsIndex);
238     std::shared_ptr<NNTensor> axisTensor = TransToNNTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_SOFTMAX_AXIS);
239     axisTensor->SetBuffer(nullptr, 0);
240     m_allTensors.emplace_back(axisTensor);
241 
242     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
243     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
244 }
245 
246 /**
247  * @tc.name: softmax_build_011
248  * @tc.desc: Provide invalid parameter type to verify the abnormal behavior of the Build function
249  * @tc.type: FUNC
250  */
251 HWTEST_F(SoftmaxBuilderTest, softmax_build_011, TestSize.Level0)
252 {
253     std::vector<uint32_t> inputsIndex = { 0 };
254     std::vector<uint32_t> outputsIndex = { 1 };
255     std::vector<int32_t> paramDim = {};
256 
257     InitTensor(inputsIndex, outputsIndex);
258     SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_SCALE_AXIS);
259 
260     OH_NN_ReturnCode ret = m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors);
261     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
262 }
263 
264 /**
265  * @tc.name: softmax_getprimitive_001
266  * @tc.desc: Verify the GetPrimitive function return nullptr
267  * @tc.type: FUNC
268  */
269 HWTEST_F(SoftmaxBuilderTest, softmax_getprimitive_001, TestSize.Level0)
270 {
271     LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
272     LiteGraphTensorPtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
273     EXPECT_EQ(primitive, expectPrimitive);
274 }
275 
276 /**
277  * @tc.name: softmax_getprimitive_002
278  * @tc.desc: Verify the normal params return behavior of the getprimitive function
279  * @tc.type: FUNC
280  */
281 HWTEST_F(SoftmaxBuilderTest, softmax_getprimitive_002, TestSize.Level0)
282 {
283     std::vector<uint32_t> inputsIndex = { 0 };
284     std::vector<uint32_t> outputsIndex = { 1 };
285     std::vector<int32_t> paramDim = {};
286 
287     InitTensor(inputsIndex, outputsIndex);
288     SaveAxisTensor(OH_NN_INT64, paramDim, nullptr, OH_NN_SOFTMAX_AXIS);
289 
290     EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_paramsIndex, m_inputsIndex, m_outputsIndex, m_allTensors));
291     LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
292     LiteGraphTensorPtr nullPrimitive(nullptr, DestroyLiteGraphPrimitive);
293     EXPECT_NE(nullPrimitive, primitive);
294 
295     auto returnValue = mindspore::lite::MindIR_Softmax_GetAxis(primitive.get());
296     auto returnValueSize = returnValue.size();
297     for (size_t i = 0; i < returnValueSize; ++i) {
298         EXPECT_EQ(returnValue[i], m_expectAxisValue[i]);
299     }
300 }
301 } // namespace UnitTest
302 } // namespace NeuralNetworkRuntime
303 } // namespace OHOS
304