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 "frameworks/native/ops/reduceall_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 ReduceAllBuilderTest : 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 ReduceAllBuilder m_builder;
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, 1, 2, 2};
42 std::vector<int32_t> m_outputDim {1, 1, 1, 2};
43 std::vector<int32_t> m_paramDim {1};
44 };
45
SetUp()46 void ReduceAllBuilderTest::SetUp() {}
47
TearDown()48 void ReduceAllBuilderTest::TearDown() {}
49
SaveParamsTensor(OH_NN_DataType dataType,const std::vector<int32_t> & dim,const OH_NN_QuantParam * quantParam,OH_NN_TensorType type)50 void ReduceAllBuilderTest::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> keepDimsTensor = TransToNNTensor(dataType, dim, quantParam, type);
54 bool *keepDimsValue = new (std::nothrow) bool(true);
55 EXPECT_NE(nullptr, keepDimsValue);
56 keepDimsTensor->SetBuffer(keepDimsValue, sizeof(bool));
57 m_allTensors.emplace_back(keepDimsTensor);
58 }
59
60 /**
61 * @tc.name: reduceall_build_001
62 * @tc.desc: Provide normal input, output, and parameters to verify the normal behavior of the Build function
63 * @tc.type: FUNC
64 */
65 HWTEST_F(ReduceAllBuilderTest, reduceall_build_001, TestSize.Level0)
66 {
67 SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
68 SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
69 SaveParamsTensor(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_ALL_KEEP_DIMS);
70
71 OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
72 EXPECT_EQ(OH_NN_SUCCESS, ret);
73 }
74
75 /**
76 * @tc.name: reduceall_build_002
77 * @tc.desc: Call Build func twice to verify the abnormal behavior of the Build function
78 * @tc.type: FUNC
79 */
80 HWTEST_F(ReduceAllBuilderTest, reduceall_build_002, TestSize.Level0)
81 {
82 SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
83 SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
84 SaveParamsTensor(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_ALL_KEEP_DIMS);
85
86 EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
87 OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
88 EXPECT_EQ(OH_NN_OPERATION_FORBIDDEN, ret);
89 }
90
91 /**
92 * @tc.name: reduceall_build_003
93 * @tc.desc: Provide one more than normal input to verify the abnormal behavior of the Build function
94 * @tc.type: FUNC
95 */
96 HWTEST_F(ReduceAllBuilderTest, reduceall_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_BOOL, m_inputDim, nullptr);
103 SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
104 SaveParamsTensor(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_ALL_KEEP_DIMS);
105
106 OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
107 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
108 }
109
110 /**
111 * @tc.name: reduceall_build_004
112 * @tc.desc: Provide one more than normal output to verify the abnormal behavior of the Build function
113 * @tc.type: FUNC
114 */
115 HWTEST_F(ReduceAllBuilderTest, reduceall_build_004, TestSize.Level0)
116 {
117 m_outputs = {2, 3};
118 m_params = {4};
119
120 SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
121 SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
122 SaveParamsTensor(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_ALL_KEEP_DIMS);
123
124 OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
125 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
126 }
127
128 /**
129 * @tc.name: reduceall_build_005
130 * @tc.desc: Verify that the build function return a failed message with null allTensor
131 * @tc.type: FUNC
132 */
133 HWTEST_F(ReduceAllBuilderTest, reduceall_build_005, TestSize.Level0)
134 {
135 OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputs, m_outputs, m_allTensors);
136 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
137 }
138
139 /**
140 * @tc.name: reduceall_build_006
141 * @tc.desc: Verify that the build function return a failed message without output tensor
142 * @tc.type: FUNC
143 */
144 HWTEST_F(ReduceAllBuilderTest, reduceall_build_006, TestSize.Level0)
145 {
146 SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
147
148 OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputs, m_allTensors);
149 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
150 }
151
152 /**
153 * @tc.name: reduceall_build_007
154 * @tc.desc: Verify that the build function return a failed message with invalided keepdims's dataType
155 * @tc.type: FUNC
156 */
157 HWTEST_F(ReduceAllBuilderTest, reduceall_build_007, TestSize.Level0)
158 {
159 SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
160 SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
161
162 std::shared_ptr<NNTensor> keepDimsTensor = TransToNNTensor(OH_NN_INT64,
163 m_paramDim, nullptr, OH_NN_REDUCE_ALL_KEEP_DIMS);
164 int64_t keepDimsValue = 1;
165 keepDimsTensor->SetBuffer(&keepDimsValue, sizeof(keepDimsValue));
166 m_allTensors.emplace_back(keepDimsTensor);
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 keepDimsTensor->SetBuffer(nullptr, 0);
171 }
172
173 /**
174 * @tc.name: reduceall_build_008
175 * @tc.desc: Verify that the build function return a failed message with invalided keepdims's dimension
176 * @tc.type: FUNC
177 */
178 HWTEST_F(ReduceAllBuilderTest, reduceall_build_008, TestSize.Level0)
179 {
180 m_paramDim = {1, 2};
181
182 SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
183 SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
184
185 std::shared_ptr<NNTensor> keepDimsTensor = TransToNNTensor(OH_NN_BOOL, m_paramDim,
186 nullptr, OH_NN_REDUCE_ALL_KEEP_DIMS);
187 bool keepDimsValue[2] = {true, true};
188 keepDimsTensor->SetBuffer(keepDimsValue, 2 * sizeof(bool));
189 m_allTensors.emplace_back(keepDimsTensor);
190
191 OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
192 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
193 keepDimsTensor->SetBuffer(nullptr, 0);
194 }
195
196 /**
197 * @tc.name: reduceall_build_009
198 * @tc.desc: Verify that the build function return a failed message with invalided parameter
199 * @tc.type: FUNC
200 */
201 HWTEST_F(ReduceAllBuilderTest, reduceall_build_009, TestSize.Level0)
202 {
203 SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
204 SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
205 SaveParamsTensor(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_QUANT_DTYPE_CAST_SRC_T);
206
207 OH_NN_ReturnCode ret = m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors);
208 EXPECT_EQ(OH_NN_INVALID_PARAMETER, ret);
209 }
210
211 /**
212 * @tc.name: reduceall_build_010
213 * @tc.desc: Verify that the build function return a failed message with empty keepdims's buffer
214 * @tc.type: FUNC
215 */
216 HWTEST_F(ReduceAllBuilderTest, reduceall_build_010, TestSize.Level0)
217 {
218 SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
219 SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
220
221 std::shared_ptr<NNTensor> keepDimsTensor = TransToNNTensor(OH_NN_BOOL,
222 m_paramDim, nullptr, OH_NN_REDUCE_ALL_KEEP_DIMS);
223 m_allTensors.emplace_back(keepDimsTensor);
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 keepDimsTensor->SetBuffer(nullptr, 0);
228 }
229
230 /**
231 * @tc.name: reduceall_get_primitive_001
232 * @tc.desc: Verify the GetPrimitive function return nullptr
233 * @tc.type: FUNC
234 */
235 HWTEST_F(ReduceAllBuilderTest, reduceall_get_primitive_001, TestSize.Level0)
236 {
237 LiteGraphTensorPtr primitive = m_builder.GetPrimitive();
238 LiteGraphTensorPtr expectPrimitive = {nullptr, DestroyLiteGraphPrimitive};
239 EXPECT_EQ(primitive, expectPrimitive);
240 }
241
242 /**
243 * @tc.name: reduceall_get_primitive_002
244 * @tc.desc: Verify the normal params return behavior of the getprimitive function
245 * @tc.type: FUNC
246 */
247 HWTEST_F(ReduceAllBuilderTest, reduceall_get_primitive_002, TestSize.Level0)
248 {
249 SaveInputTensor(m_inputs, OH_NN_BOOL, m_inputDim, nullptr);
250 SaveOutputTensor(m_outputs, OH_NN_BOOL, m_outputDim, nullptr);
251 SaveParamsTensor(OH_NN_BOOL, m_paramDim, nullptr, OH_NN_REDUCE_ALL_KEEP_DIMS);
252
253 bool keepDimsValue = true;
254 EXPECT_EQ(OH_NN_SUCCESS, m_builder.Build(m_params, m_inputsIndex, m_outputsIndex, m_allTensors));
255 LiteGraphTensorPtr reduceallPrimitive = m_builder.GetPrimitive();
256 LiteGraphTensorPtr expectPrimitive = {nullptr, DestroyLiteGraphPrimitive};
257 EXPECT_NE(reduceallPrimitive, expectPrimitive);
258 auto returnValue = mindspore::lite::MindIR_ReduceFusion_GetKeepDims(reduceallPrimitive.get());
259 EXPECT_EQ(returnValue, keepDimsValue);
260 }
261 } // namespace UnitTest
262 } // namespace NeuralNetworkRuntime
263 } // namespace OHOS