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/gelu_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 GeluBuilderTest : public OpsTest {
28 public:
29     void SetUp() override;
30     void TearDown() override;
31 
32 protected:
33     void SetApproximate(OH_NN_DataType dataType,
34         const std::vector<int32_t> &dim,  const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
35 
36 protected:
37     GeluBuilder m_gelu;
38     std::vector<uint32_t> m_inputs {0};
39     std::vector<uint32_t> m_outputs {1};
40     std::vector<uint32_t> m_params {2};
41     std::vector<int32_t> m_inputDim {1, 5, 1, 1};
42     std::vector<int32_t> m_outputDim {1, 5, 1, 1};
43     std::vector<int32_t> m_paramsDim {};
44 };
45 
SetUp()46 void GeluBuilderTest::SetUp() {}
47 
TearDown()48 void GeluBuilderTest::TearDown() {}
49 
SetApproximate(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)50 void GeluBuilderTest::SetApproximate(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> outQuantizedTensor = TransToNNTensor(dataType, dim, quantParam, type);
54     bool* outQuantizedValue = new (std::nothrow) bool(false);
55     EXPECT_NE(nullptr, outQuantizedValue);
56     outQuantizedTensor->SetBuffer(outQuantizedValue, sizeof(bool));
57     m_allTensors.emplace_back(outQuantizedTensor);
58 }
59 
60 /**
61  * @tc.name: gelu_build_001
62  * @tc.desc: Verify that the build function returns a successful message.
63  * @tc.type: FUNC
64  */
65 HWTEST_F(GeluBuilderTest, gelu_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     SetApproximate(OH_NN_BOOL, m_paramsDim, nullptr, OH_NN_GELU_APPROXIMATE);
70 
71     OH_NN_ReturnCode ret = m_gelu.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
72     EXPECT_EQ(OH_NN_SUCCESS, ret);
73 }
74 
75 /**
76  * @tc.name: gelu_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(GeluBuilderTest, gelu_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     SetApproximate(OH_NN_BOOL, m_paramsDim, nullptr, OH_NN_GELU_APPROXIMATE);
85 
86     EXPECT_EQ(OH_NN_SUCCESS, m_gelu.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
87     OH_NN_ReturnCode ret = m_gelu.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
88     EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
89 }
90 
91 /**
92  * @tc.name: gelu_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(GeluBuilderTest, gelu_build_003, TestSize.Level0)
97 {
98     m_inputs = {0, 1};
99     m_outputs = {2};
100     m_params = {3};
101 
102     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
103     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
104     SetApproximate(OH_NN_BOOL, m_paramsDim, nullptr, OH_NN_GELU_APPROXIMATE);
105 
106     OH_NN_ReturnCode ret = m_gelu.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
107     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
108 }
109 
110 /**
111  * @tc.name: gelu_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(GeluBuilderTest, gelu_build_004, TestSize.Level0)
116 {
117     std::vector<uint32_t> m_outputs = {1, 2};
118     m_params = {3};
119 
120     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
121     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
122     SetApproximate(OH_NN_BOOL, m_paramsDim, nullptr, OH_NN_GELU_APPROXIMATE);
123 
124     OH_NN_ReturnCode ret = m_gelu.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
125     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
126 }
127 
128 /**
129  * @tc.name: gelu_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(GeluBuilderTest, gelu_build_005, TestSize.Level0)
134 {
135     OH_NN_ReturnCode ret = m_gelu.Build(m_params, m_inputs, m_outputs, m_allTensors);
136     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
137 }
138 
139 /**
140  * @tc.name: gelu_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(GeluBuilderTest, gelu_build_006, TestSize.Level0)
145 {
146     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
147 
148     OH_NN_ReturnCode ret = m_gelu.Build(m_params, m_inputsIndex, m_outputs, m_allTensors);
149     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
150 }
151 
152 /**
153  * @tc.name: gelu_build_007
154  * @tc.desc: Verify that the build function returns a failed message with a virtual parameter.
155  * @tc.type: FUNC
156  */
157 HWTEST_F(GeluBuilderTest, gelu_build_007, TestSize.Level0)
158 {
159     m_params = {2, 3};
160     std::vector<int32_t> paramDim = {};
161 
162     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
163     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
164     std::shared_ptr<NNTensor> paramTensor;
165     paramTensor = TransToNNTensor(OH_NN_INT32, paramDim, nullptr, OH_NN_TENSOR);
166     m_allTensors.emplace_back(paramTensor);
167     SetApproximate(OH_NN_BOOL, m_paramsDim, nullptr, OH_NN_GELU_APPROXIMATE);
168 
169     OH_NN_ReturnCode ret = m_gelu.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
170     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
171 }
172 
173 /**
174  * @tc.name: gelu_build_008
175  * @tc.desc: Verify that the build function returns a failed message with invalid approximate's dataType.
176  * @tc.type: FUNC
177  */
178 HWTEST_F(GeluBuilderTest, gelu_build_008, TestSize.Level0)
179 {
180     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
181     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
182 
183     std::shared_ptr<NNTensor> approximateTensor = TransToNNTensor(OH_NN_INT64, m_paramsDim,
184         nullptr, OH_NN_GELU_APPROXIMATE);
185     int64_t* approximateValue = new (std::nothrow) int64_t[1] {0};
186     EXPECT_NE(nullptr, approximateValue);
187     approximateTensor->SetBuffer(approximateValue, sizeof(int64_t));
188     m_allTensors.emplace_back(approximateTensor);
189 
190     OH_NN_ReturnCode ret = m_gelu.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
191     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
192 }
193 
194 /**
195  * @tc.name: gelu_build_009
196  * @tc.desc: Verify that the build function returns a failed message with passing invalid approximate param.
197  * @tc.type: FUNC
198  */
199 HWTEST_F(GeluBuilderTest, gelu_build_009, TestSize.Level0)
200 {
201     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
202     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
203     SetApproximate(OH_NN_BOOL, m_paramsDim, nullptr, OH_NN_MUL_ACTIVATION_TYPE);
204 
205     OH_NN_ReturnCode ret = m_gelu.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
206     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
207 }
208 
209 /**
210  * @tc.name: Gelu_build_011
211  * @tc.desc: Verify that the build function returns a failed message without set buffer for approximate.
212  * @tc.type: FUNC
213  */
214 HWTEST_F(GeluBuilderTest, gelu_build_011, TestSize.Level0)
215 {
216     SaveInputTensor(m_inputs, OH_NN_INT32, m_inputDim, nullptr);
217     SaveOutputTensor(m_outputs, OH_NN_INT32, m_outputDim, nullptr);
218 
219     std::shared_ptr<NNTensor> approximateTensor = TransToNNTensor(OH_NN_BOOL, m_paramsDim,
220         nullptr, OH_NN_GELU_APPROXIMATE);
221     m_allTensors.emplace_back(approximateTensor);
222 
223     OH_NN_ReturnCode ret = m_gelu.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
224     EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
225 }
226 
227 /**
228  * @tc.name: gelu_getprimitive_001
229  * @tc.desc: Verify that the getPrimitive function returns a successful message
230  * @tc.type: FUNC
231  */
232 HWTEST_F(GeluBuilderTest, gelu_getprimitive_001, TestSize.Level0)
233 {
234     SaveInputTensor(m_inputs, OH_NN_FLOAT32, m_inputDim, nullptr);
235     SaveOutputTensor(m_outputs, OH_NN_FLOAT32, m_outputDim, nullptr);
236     SetApproximate(OH_NN_BOOL, m_paramsDim, nullptr, OH_NN_GELU_APPROXIMATE);
237 
238     bool approximateValue = false;
239     EXPECT_EQ(OH_NN_SUCCESS, m_gelu.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
240     LiteGraphPrimitvePtr primitive = m_gelu.GetPrimitive();
241     LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
242     EXPECT_NE(expectPrimitive, primitive);
243 
244     mindspore::lite::ActivationType activationType = mindspore::lite::ACTIVATION_TYPE_GELU;
245     auto returnValue = mindspore::lite::MindIR_Activation_GetActivationType(primitive.get());
246     EXPECT_EQ(returnValue, activationType);
247     auto returnApproximateValue = mindspore::lite::MindIR_Activation_GetApproximate(primitive.get());
248     EXPECT_EQ(returnApproximateValue, approximateValue);
249 }
250 
251 /**
252  * @tc.name: gelu_getprimitive_002
253  * @tc.desc: Verify that the getPrimitive function returns a failed message without build.
254  * @tc.type: FUNC
255  */
256 HWTEST_F(GeluBuilderTest, gelu_getprimitive_002, TestSize.Level0)
257 {
258     GeluBuilder gelu;
259     LiteGraphPrimitvePtr primitive = m_gelu.GetPrimitive();
260     LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
261     EXPECT_EQ(expectPrimitive, primitive);
262 }
263 }
264 }
265 }