1 /*
2 * Copyright (c) 2024 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/leaky_relu_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 LeakyReluBuilderTest : public OpsTest {
28 public:
29 void SetUp() override;
30 void TearDown() override;
31
32 protected:
33 void SaveNegativeSlope(OH_NN_DataType dataType,
34 const std::vector<int32_t> &dim, const OH_NN_QuantParam* quantParam, OH_NN_TensorType type);
35
36 protected:
37 LeakyReluBuilder m_builder;
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_dim {1, 2, 2, 1};
42 std::vector<int32_t> m_paramDim {};
43 };
44
SetUp()45 void LeakyReluBuilderTest::SetUp() {}
46
TearDown()47 void LeakyReluBuilderTest::TearDown() {}
48
SaveNegativeSlope(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)49 void LeakyReluBuilderTest::SaveNegativeSlope(OH_NN_DataType dataType,
50 const std::vector<int32_t> &dim, const OH_NN_QuantParam* quantParam, OH_NN_TensorType type)
51 {
52 std::shared_ptr<NNTensor> negativeSlopeTensor = TransToNNTensor(dataType, dim, quantParam, type);
53 float* negativeSlopeValue = new (std::nothrow) float [1]{0.0f};
54 EXPECT_NE(nullptr, negativeSlopeValue);
55 negativeSlopeTensor->SetBuffer(negativeSlopeValue, sizeof(float));
56 m_allTensors.emplace_back(negativeSlopeTensor);
57 }
58
59 /**
60 * @tc.name: leaky_relu_build_001
61 * @tc.desc: Verify that the build function returns a successful message.
62 * @tc.type: FUNC
63 */
64 HWTEST_F(LeakyReluBuilderTest, leaky_relu_build_001, TestSize.Level1)
65 {
66 SaveInputTensor(m_inputs, OH_NN_INT32, m_dim, nullptr);
67 SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
68 SaveNegativeSlope(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_LEAKY_RELU_NEGATIVE_SLOPE);
69
70 OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
71 EXPECT_EQ(OH_NN_SUCCESS, ret);
72 }
73
74 /**
75 * @tc.name: leaky_relu_build_002
76 * @tc.desc: Verify that the build function returns a failed message with true m_isBuild.
77 * @tc.type: FUNC
78 */
79 HWTEST_F(LeakyReluBuilderTest, leaky_relu_build_002, TestSize.Level1)
80 {
81 SaveInputTensor(m_inputs, OH_NN_INT32, m_dim, nullptr);
82 SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
83 SaveNegativeSlope(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_LEAKY_RELU_NEGATIVE_SLOPE);
84
85 EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
86 OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
87 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
88 }
89
90 /**
91 * @tc.name: leaky_relu_build_003
92 * @tc.desc: Verify that the build function returns a failed message with invalided input.
93 * @tc.type: FUNC
94 */
95 HWTEST_F(LeakyReluBuilderTest, leaky_relu_build_003, TestSize.Level1)
96 {
97 m_inputs = {0, 1};
98 m_outputs = {2};
99 m_params = {3};
100
101 SaveInputTensor(m_inputs, OH_NN_INT32, m_dim, nullptr);
102 SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
103 SaveNegativeSlope(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_LEAKY_RELU_NEGATIVE_SLOPE);
104
105 OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
106 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
107 }
108
109 /**
110 * @tc.name: leaky_relu_build_004
111 * @tc.desc: Verify that the build function returns a failed message with invalided output.
112 * @tc.type: FUNC
113 */
114 HWTEST_F(LeakyReluBuilderTest, leaky_relu_build_004, TestSize.Level1)
115 {
116 m_outputs = {1, 2};
117 m_params = {3};
118
119 SaveInputTensor(m_inputs, OH_NN_INT32, m_dim, nullptr);
120 SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
121 SaveNegativeSlope(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_LEAKY_RELU_NEGATIVE_SLOPE);
122
123 OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
124 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
125 }
126
127 /**
128 * @tc.name: leaky_relu_build_005
129 * @tc.desc: Verify that the build function returns a failed message with empty allTensor.
130 * @tc.type: FUNC
131 */
132 HWTEST_F(LeakyReluBuilderTest, leaky_relu_build_005, TestSize.Level1)
133 {
134 OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputs, m_outputs, m_allTensors);
135 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
136 }
137
138 /**
139 * @tc.name: leaky_relu_build_006
140 * @tc.desc: Verify that the build function returns a failed message without output tensor.
141 * @tc.type: FUNC
142 */
143 HWTEST_F(LeakyReluBuilderTest, leaky_relu_build_006, TestSize.Level1)
144 {
145 SaveInputTensor(m_inputs, OH_NN_INT32, m_dim, nullptr);
146
147 OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputs, m_allTensors);
148 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
149 }
150
151 /**
152 * @tc.name: leaky_relu_build_007
153 * @tc.desc: Verify that the build function returns a failed message with invalid negative_slope's dataType.
154 * @tc.type: FUNC
155 */
156 HWTEST_F(LeakyReluBuilderTest, leaky_relu_build_007, TestSize.Level1)
157 {
158 SaveInputTensor(m_inputs, OH_NN_INT32, m_dim, nullptr);
159 SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
160
161 std::shared_ptr<NNTensor> negativeSlopeTensor = TransToNNTensor(OH_NN_INT64, m_paramDim,
162 nullptr, OH_NN_LEAKY_RELU_NEGATIVE_SLOPE);
163 int64_t* negativeSlopeValue = new (std::nothrow) int64_t [1]{0};
164 EXPECT_NE(nullptr, negativeSlopeValue);
165 negativeSlopeTensor->SetBuffer(negativeSlopeValue, sizeof(int64_t));
166 m_allTensors.emplace_back(negativeSlopeTensor);
167
168 OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
169 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
170 }
171
172 /**
173 * @tc.name: leaky_relu_build_008
174 * @tc.desc: Verify that the build function returns a failed message with passing invalid negative_slope param.
175 * @tc.type: FUNC
176 */
177 HWTEST_F(LeakyReluBuilderTest, leaky_relu_build_008, TestSize.Level1)
178 {
179 SaveInputTensor(m_inputs, OH_NN_INT32, m_dim, nullptr);
180 SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
181
182 SaveNegativeSlope(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_MUL_ACTIVATION_TYPE);
183
184 OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
185 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
186 }
187
188 /**
189 * @tc.name: leaky_relu_build_009
190 * @tc.desc: Verify that the build function returns a failed message without set buffer for negative_slope.
191 * @tc.type: FUNC
192 */
193 HWTEST_F(LeakyReluBuilderTest, leaky_relu_build_011, TestSize.Level1)
194 {
195 SaveInputTensor(m_inputs, OH_NN_INT32, m_dim, nullptr);
196 SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
197
198 std::shared_ptr<NNTensor> negativeSlopeTensor = TransToNNTensor(OH_NN_FLOAT32, m_paramDim,
199 nullptr, OH_NN_LEAKY_RELU_NEGATIVE_SLOPE);
200 m_allTensors.emplace_back(negativeSlopeTensor);
201
202 OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
203 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
204 }
205
206 /**
207 * @tc.name: leaky_relu_getprimitive_001
208 * @tc.desc: Verify that the getPrimitive function returns a successful message.
209 * @tc.type: FUNC
210 */
211 HWTEST_F(LeakyReluBuilderTest, leaky_relu_getprimitive_001, TestSize.Level1)
212 {
213 SaveInputTensor(m_inputs, OH_NN_INT32, m_dim, nullptr);
214 SaveOutputTensor(m_outputs, OH_NN_INT32, m_dim, nullptr);
215 SaveNegativeSlope(OH_NN_FLOAT32, m_paramDim, nullptr, OH_NN_LEAKY_RELU_NEGATIVE_SLOPE);
216
217 float negativeSlopeValue = 0.0f;
218 EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
219 LiteGraphPrimitvePtr primitive = m_builder.GetPrimitive();
220 LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
221 EXPECT_NE(expectPrimitive, primitive);
222
223 auto returnNegativeSlopeValue = mindspore::lite::MindIR_LeakyRelu_GetNegativeSlope(primitive.get());
224 EXPECT_EQ(returnNegativeSlopeValue, negativeSlopeValue);
225 }
226
227 /**
228 * @tc.name: leaky_relu_getprimitive_002
229 * @tc.desc: Verify that the getPrimitive function returns a failed message without build.
230 * @tc.type: FUNC
231 */
232 HWTEST_F(LeakyReluBuilderTest, leaky_relu_getprimitive_002, TestSize.Level1)
233 {
234 LiteGraphPrimitvePtr primitive = m_builder.GetPrimitive();
235 LiteGraphPrimitvePtr expectPrimitive(nullptr, DestroyLiteGraphPrimitive);
236 EXPECT_EQ(expectPrimitive, primitive);
237 }
238 }
239 }
240 }