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 #include <cmath>
16 #include <cstdio>
17 #include <vector>
18 #include <thread>
19
20 #include "nnrt_utils.h"
21 #include "model.h"
22
23 using namespace testing::ext;
24 using namespace OHOS::NeuralNetworkRuntime;
25 using namespace OHOS::NeuralNetworkRuntime::Test;
26 using namespace OHOS::HDI::Nnrt::V1_0;
27
28 namespace {
29
30 class ExecutorTest : public testing::Test {
31 protected:
32 OHOS::sptr<V1_0::MockIDevice> device;
33 AddModel addModel;
34 OHNNGraphArgs graphArgs = addModel.graphArgs;
35 OHNNCompileParam compileParam;
36 };
37
ExecuteModel(OH_NNExecutor * executor,const OHNNGraphArgs & graphArgs,float * expect)38 void ExecuteModel(OH_NNExecutor *executor, const OHNNGraphArgs &graphArgs, float* expect)
39 {
40 ASSERT_EQ(OH_NN_SUCCESS, ExecuteGraphMock(executor, graphArgs, expect));
41 }
42
43 } // namespace
44
45 /**
46 * @tc.number : SUB_AI_NNRt_Func_North_Executor_Create_0100
47 * @tc.name : 创建执行实例,compilation为nullptr
48 * @tc.desc : [C- SOFTWARE -0200]
49 */
50 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_Create_0100, Function | MediumTest | Level3)
51 {
52 OH_NNExecutor *executor = OH_NNExecutor_Construct(nullptr);
53 ASSERT_EQ(nullptr, executor);
54 }
55
56 /**
57 * @tc.number : SUB_AI_NNRt_Func_North_Executor_Create_0200
58 * @tc.name : 创建执行实例,compilation未完成编译
59 * @tc.desc : [C- SOFTWARE -0200]
60 */
61 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_Create_0200, Function | MediumTest | Level3)
62 {
63 OH_NNModel *model = OH_NNModel_Construct();
64 ASSERT_NE(nullptr, model);
65 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
66
67 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
68 ASSERT_NE(nullptr, compilation);
69
70 const size_t *devicesID{nullptr};
71 uint32_t devicesCount{0};
72 ASSERT_EQ(OH_NN_SUCCESS, OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount));
73 size_t targetDevice = devicesID[0];
74
75 ASSERT_EQ(OH_NN_SUCCESS, OH_NNCompilation_SetDevice(compilation, targetDevice));
76 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
77 ASSERT_EQ(nullptr, executor);
78
79 Free(model, compilation, executor);
80 }
81
82 /**
83 * @tc.number : SUB_AI_NNRt_Func_North_Executor_SetInput_0100
84 * @tc.name : 设置输入,executor为nullptr
85 * @tc.desc : [C- SOFTWARE -0200]
86 */
87 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_SetInput_0100, Function | MediumTest | Level3)
88 {
89 OH_NNModel *model = OH_NNModel_Construct();
90 ASSERT_NE(nullptr, model);
91 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
92
93 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
94 ASSERT_NE(nullptr, compilation);
95 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
96 uint32_t inputIndex = 0;
97 const OHNNOperandTest &operandTem = graphArgs.operands[0];
98 auto quantParam = operandTem.quantParam;
99 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
100 quantParam, operandTem.type};
101 ASSERT_EQ(OH_NN_INVALID_PARAMETER,
102 OH_NNExecutor_SetInput(nullptr, inputIndex, &operand, operandTem.data, operandTem.length));
103
104 Free(model, compilation);
105 }
106
107 /**
108 * @tc.number : SUB_AI_NNRt_Func_North_Executor_SetInput_0200
109 * @tc.name : 设置输入,inputIndex不存在
110 * @tc.desc : [C- SOFTWARE -0200]
111 */
112 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_SetInput_0200, Function | MediumTest | Level3)
113 {
114 OH_NNModel *model = OH_NNModel_Construct();
115 ASSERT_NE(nullptr, model);
116 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
117
118 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
119 ASSERT_NE(nullptr, compilation);
120 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
121
122 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
123 ASSERT_NE(nullptr, executor);
124 uint32_t inputIndex = 100000;
125 const OHNNOperandTest &operandTem = graphArgs.operands[0];
126 auto quantParam = operandTem.quantParam;
127 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
128 quantParam, operandTem.type};
129 ASSERT_EQ(OH_NN_INVALID_PARAMETER,
130 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length));
131
132 Free(model, compilation, executor);
133 }
134
135 /**
136 * @tc.number : SUB_AI_NNRt_Func_North_Executor_SetInput_0300
137 * @tc.name : 设置输入,operand参数不一致
138 * @tc.desc : [C- SOFTWARE -0200]
139 */
140 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_SetInput_0300, Function | MediumTest | Level3)
141 {
142 OH_NNModel *model = OH_NNModel_Construct();
143 ASSERT_NE(nullptr, model);
144 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
145
146 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
147 ASSERT_NE(nullptr, compilation);
148 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
149
150 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
151 ASSERT_NE(nullptr, executor);
152 uint32_t inputIndex = 0;
153 const OHNNOperandTest &operandTem = graphArgs.operands[0];
154 auto quantParam = operandTem.quantParam;
155 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
156 quantParam, OH_NN_ADD_ACTIVATIONTYPE};
157 ASSERT_EQ(OH_NN_INVALID_PARAMETER,
158 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length));
159
160 Free(model, compilation, executor);
161 }
162
163 /**
164 * @tc.number : SUB_AI_NNRt_Func_North_Executor_SetInput_0400
165 * @tc.name : 设置输入,operand形状改变
166 * @tc.desc : [C- SOFTWARE -0200]
167 */
168 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_SetInput_0400, Function | MediumTest | Level3)
169 {
170 OH_NNModel *model = OH_NNModel_Construct();
171 ASSERT_NE(nullptr, model);
172 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
173
174 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
175 ASSERT_NE(nullptr, compilation);
176 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
177
178 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
179 ASSERT_NE(nullptr, executor);
180 uint32_t inputIndex = 0;
181 const OHNNOperandTest &operandTem = graphArgs.operands[0];
182 auto quantParam = operandTem.quantParam;
183 int32_t dimensions[3]{3, 3, 3};
184 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), dimensions, quantParam,
185 operandTem.type};
186 ASSERT_EQ(OH_NN_INVALID_PARAMETER,
187 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length));
188
189 Free(model, compilation, executor);
190 }
191
192 /**
193 * @tc.number : SUB_AI_NNRt_Func_North_Executor_SetInput_0500
194 * @tc.name : 设置输入,buffer为nullptr
195 * @tc.desc : [C- SOFTWARE -0200]
196 */
197 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_SetInput_0500, Function | MediumTest | Level3)
198 {
199 OH_NNModel *model = OH_NNModel_Construct();
200 ASSERT_NE(nullptr, model);
201 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
202
203 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
204 ASSERT_NE(nullptr, compilation);
205 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
206
207 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
208 ASSERT_NE(nullptr, executor);
209 uint32_t inputIndex = 0;
210 const OHNNOperandTest &operandTem = graphArgs.operands[0];
211 auto quantParam = operandTem.quantParam;
212 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
213 quantParam, operandTem.type};
214 ASSERT_EQ(OH_NN_INVALID_PARAMETER,
215 OH_NNExecutor_SetInput(executor, inputIndex, &operand, nullptr, operandTem.length));
216
217 Free(model, compilation, executor);
218 }
219
220 /**
221 * @tc.number : SUB_AI_NNRt_Func_North_Executor_SetInput_0600
222 * @tc.name : 设置输入,length小于输入长度
223 * @tc.desc : [C- SOFTWARE -0200]
224 */
225 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_SetInput_0600, Function | MediumTest | Level3)
226 {
227 OH_NNModel *model = OH_NNModel_Construct();
228 ASSERT_NE(nullptr, model);
229 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
230
231 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
232 ASSERT_NE(nullptr, compilation);
233 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
234
235 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
236 ASSERT_NE(nullptr, executor);
237 uint32_t inputIndex = 0;
238 const OHNNOperandTest &operandTem = graphArgs.operands[0];
239 auto quantParam = operandTem.quantParam;
240 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
241 quantParam, operandTem.type};
242 ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, 0));
243
244 Free(model, compilation, executor);
245 }
246
247 /**
248 * @tc.number : SUB_AI_NNRt_Func_North_Executor_SetInput_0700
249 * @tc.name : 设置输入,重复设置同一inputIndex
250 * @tc.desc : [C- SOFTWARE -0200]
251 */
252 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_SetInput_0700, Function | MediumTest | Level2)
253 {
254 OH_NNModel *model = OH_NNModel_Construct();
255 ASSERT_NE(nullptr, model);
256 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
257
258 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
259 ASSERT_NE(nullptr, compilation);
260 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
261
262 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
263 ASSERT_NE(nullptr, executor);
264 uint32_t inputIndex = 0;
265 const OHNNOperandTest &operandTem = graphArgs.operands[0];
266 auto quantParam = operandTem.quantParam;
267 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
268 quantParam, operandTem.type};
269 ASSERT_EQ(OH_NN_SUCCESS,
270 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length));
271 ASSERT_EQ(OH_NN_SUCCESS,
272 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length));
273
274 Free(model, compilation, executor);
275 }
276
277 /**
278 * @tc.number : SUB_AI_NNRt_Func_North_Executor_SetOutput_0100
279 * @tc.name : 设置输出,executor为nullptr
280 * @tc.desc : [C- SOFTWARE -0200]
281 */
282 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_SetOutput_0100, Function | MediumTest | Level3)
283 {
284 OH_NNModel *model = OH_NNModel_Construct();
285 ASSERT_NE(nullptr, model);
286 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
287
288 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
289 ASSERT_NE(nullptr, compilation);
290 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
291
292 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
293 ASSERT_NE(nullptr, executor);
294 uint32_t inputIndex = 0;
295 uint32_t outputIndex = 0;
296
297 for (int i = 0; i < graphArgs.operands.size(); i++) {
298 const OHNNOperandTest &operandTem = graphArgs.operands[i];
299 auto quantParam = operandTem.quantParam;
300 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
301 quantParam, operandTem.type};
302 if (std::find(graphArgs.inputIndices.begin(), graphArgs.inputIndices.end(), i) !=
303 graphArgs.inputIndices.end()) {
304 ASSERT_EQ(OH_NN_SUCCESS,
305 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length));
306 inputIndex += 1;
307 } else if (std::find(graphArgs.outputIndices.begin(), graphArgs.outputIndices.end(), i) !=
308 graphArgs.outputIndices.end()) {
309 ASSERT_EQ(OH_NN_INVALID_PARAMETER,
310 OH_NNExecutor_SetOutput(nullptr, outputIndex, operandTem.data, operandTem.length));
311 outputIndex += 1;
312 }
313 }
314 Free(model, compilation, executor);
315 }
316
317 /**
318 * @tc.number : SUB_AI_NNRt_Func_North_Executor_SetOutput_0200
319 * @tc.name : 设置输出,outputIndex不存在
320 * @tc.desc : [C- SOFTWARE -0200]
321 */
322 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_SetOutput_0200, Function | MediumTest | Level3)
323 {
324 OH_NNModel *model = OH_NNModel_Construct();
325 ASSERT_NE(nullptr, model);
326 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
327
328 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
329 ASSERT_NE(nullptr, compilation);
330 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
331
332 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
333 ASSERT_NE(nullptr, executor);
334 uint32_t inputIndex = 0;
335 uint32_t outputIndex = 10000;
336
337 for (int i = 0; i < graphArgs.operands.size(); i++) {
338 const OHNNOperandTest &operandTem = graphArgs.operands[i];
339 auto quantParam = operandTem.quantParam;
340 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
341 quantParam, operandTem.type};
342 if (std::find(graphArgs.inputIndices.begin(), graphArgs.inputIndices.end(), i) !=
343 graphArgs.inputIndices.end()) {
344 ASSERT_EQ(OH_NN_SUCCESS,
345 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length));
346 inputIndex += 1;
347 } else if (std::find(graphArgs.outputIndices.begin(), graphArgs.outputIndices.end(), i) !=
348 graphArgs.outputIndices.end()) {
349 ASSERT_EQ(OH_NN_INVALID_PARAMETER,
350 OH_NNExecutor_SetOutput(executor, outputIndex, operandTem.data, operandTem.length));
351 outputIndex += 1;
352 }
353 }
354 Free(model, compilation, executor);
355 }
356
357 /**
358 * @tc.number : SUB_AI_NNRt_Func_North_Executor_SetOutput_0300
359 * @tc.name : 设置输出,buffer为nullptr
360 * @tc.desc : [C- SOFTWARE -0200]
361 */
362 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_SetOutput_0300, Function | MediumTest | Level3)
363 {
364 OH_NNModel *model = OH_NNModel_Construct();
365 ASSERT_NE(nullptr, model);
366 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
367
368 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
369 ASSERT_NE(nullptr, compilation);
370 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
371
372 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
373 ASSERT_NE(nullptr, executor);
374 uint32_t inputIndex = 0;
375 uint32_t outputIndex = 0;
376
377 for (int i = 0; i < graphArgs.operands.size(); i++) {
378 const OHNNOperandTest &operandTem = graphArgs.operands[i];
379 auto quantParam = operandTem.quantParam;
380 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
381 quantParam, operandTem.type};
382 if (std::find(graphArgs.inputIndices.begin(), graphArgs.inputIndices.end(), i) !=
383 graphArgs.inputIndices.end()) {
384 ASSERT_EQ(OH_NN_SUCCESS,
385 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length));
386 inputIndex += 1;
387 } else if (std::find(graphArgs.outputIndices.begin(), graphArgs.outputIndices.end(), i) !=
388 graphArgs.outputIndices.end()) {
389 ASSERT_EQ(OH_NN_INVALID_PARAMETER,
390 OH_NNExecutor_SetOutput(executor, outputIndex, nullptr, operandTem.length));
391 outputIndex += 1;
392 }
393 }
394 Free(model, compilation, executor);
395 }
396
397 /**
398 * @tc.number : SUB_AI_NNRt_Func_North_Executor_SetOutput_0400
399 * @tc.name : 设置输出,length小于输出长度
400 * @tc.desc : [C- SOFTWARE -0200]
401 */
402 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_SetOutput_0400, Function | MediumTest | Level3)
403 {
404 OH_NNModel *model = OH_NNModel_Construct();
405 ASSERT_NE(nullptr, model);
406 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
407
408 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
409 ASSERT_NE(nullptr, compilation);
410 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
411
412 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
413 ASSERT_NE(nullptr, executor);
414 uint32_t inputIndex = 0;
415 uint32_t outputIndex = 0;
416
417 for (int i = 0; i < graphArgs.operands.size(); i++) {
418 const OHNNOperandTest &operandTem = graphArgs.operands[i];
419 auto quantParam = operandTem.quantParam;
420 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
421 quantParam, operandTem.type};
422 if (std::find(graphArgs.inputIndices.begin(), graphArgs.inputIndices.end(), i) !=
423 graphArgs.inputIndices.end()) {
424 ASSERT_EQ(OH_NN_SUCCESS,
425 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length));
426 inputIndex += 1;
427 } else if (std::find(graphArgs.outputIndices.begin(), graphArgs.outputIndices.end(), i) !=
428 graphArgs.outputIndices.end()) {
429 ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNExecutor_SetOutput(executor, outputIndex, operandTem.data, 0));
430 outputIndex += 1;
431 }
432 }
433 Free(model, compilation, executor);
434 }
435
436 /**
437 * @tc.number : SUB_AI_NNRt_Func_North_Executor_SetOutput_0500
438 * @tc.name : 设置输出,重复设置同一outputIndex
439 * @tc.desc : [C- SOFTWARE -0200]
440 */
441 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_SetOutput_0500, Function | MediumTest | Level2)
442 {
443 OH_NNModel *model = OH_NNModel_Construct();
444 ASSERT_NE(nullptr, model);
445 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
446
447 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
448 ASSERT_NE(nullptr, compilation);
449 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
450
451 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
452 ASSERT_NE(nullptr, executor);
453 uint32_t inputIndex = 0;
454 uint32_t outputIndex = 0;
455
456 for (int i = 0; i < graphArgs.operands.size(); i++) {
457 const OHNNOperandTest &operandTem = graphArgs.operands[i];
458 auto quantParam = operandTem.quantParam;
459 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
460 quantParam, operandTem.type};
461 if (std::find(graphArgs.inputIndices.begin(), graphArgs.inputIndices.end(), i) !=
462 graphArgs.inputIndices.end()) {
463 ASSERT_EQ(OH_NN_SUCCESS,
464 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length));
465 inputIndex += 1;
466 } else if (std::find(graphArgs.outputIndices.begin(), graphArgs.outputIndices.end(), i) !=
467 graphArgs.outputIndices.end()) {
468 ASSERT_EQ(OH_NN_SUCCESS,
469 OH_NNExecutor_SetOutput(executor, outputIndex, operandTem.data, operandTem.length));
470 ASSERT_EQ(OH_NN_SUCCESS,
471 OH_NNExecutor_SetOutput(executor, outputIndex, operandTem.data, operandTem.length));
472 outputIndex += 1;
473 }
474 }
475 Free(model, compilation, executor);
476 }
477
478 /**
479 * @tc.number : SUB_AI_NNRt_Func_North_Executor_Run_0100
480 * @tc.name : 模型推理,executor为nullptr
481 * @tc.desc : [C- SOFTWARE -0200]
482 */
483 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_Run_0100, Function | MediumTest | Level3)
484 {
485 ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNExecutor_Run(nullptr));
486 }
487 /**
488 * @tc.number : SUB_AI_NNRt_Func_North_Executor_Run_0200
489 * @tc.name : 模型推理,executor未设置输入
490 * @tc.desc : [C- SOFTWARE -0200]
491 */
492 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_Run_0200, Function | MediumTest | Level3)
493 {
494 OH_NNModel *model = OH_NNModel_Construct();
495 ASSERT_NE(nullptr, model);
496 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
497
498 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
499 ASSERT_NE(nullptr, compilation);
500 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
501
502 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
503 ASSERT_NE(nullptr, executor);
504 uint32_t outputIndex = 0;
505
506 for (int i = 0; i < graphArgs.operands.size(); i++) {
507 const OHNNOperandTest &operandTem = graphArgs.operands[i];
508 if (std::find(graphArgs.outputIndices.begin(), graphArgs.outputIndices.end(), i) !=
509 graphArgs.outputIndices.end()) {
510 ASSERT_EQ(OH_NN_SUCCESS,
511 OH_NNExecutor_SetOutput(executor, outputIndex, operandTem.data, operandTem.length));
512 }
513 }
514 ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNExecutor_Run(executor));
515
516 Free(model, compilation, executor);
517 }
518
519 /**
520 * @tc.number : SUB_AI_NNRt_Func_North_Executor_Run_0300
521 * @tc.name : 模型推理,executor未设置输出
522 * @tc.desc : [C- SOFTWARE -0200]
523 */
524 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_Run_0300, Function | MediumTest | Level3)
525 {
526 OH_NNModel *model = OH_NNModel_Construct();
527 ASSERT_NE(nullptr, model);
528 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
529
530 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
531 ASSERT_NE(nullptr, compilation);
532 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
533
534 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
535 ASSERT_NE(nullptr, executor);
536 uint32_t inputIndex = 0;
537
538 for (int i = 0; i < graphArgs.operands.size(); i++) {
539 const OHNNOperandTest &operandTem = graphArgs.operands[i];
540 auto quantParam = operandTem.quantParam;
541 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
542 quantParam, operandTem.type};
543 if (std::find(graphArgs.inputIndices.begin(), graphArgs.inputIndices.end(), i) !=
544 graphArgs.inputIndices.end()) {
545 ASSERT_EQ(OH_NN_SUCCESS,
546 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length));
547 inputIndex += 1;
548 }
549 }
550 ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNExecutor_Run(executor));
551
552 Free(model, compilation, executor);
553 }
554
555 /**
556 * @tc.number : SUB_AI_NNRt_Func_North_Executor_Run_0400
557 * @tc.name : 模型推理,executor设置输入个数不足
558 * @tc.desc : [C- SOFTWARE -0200]
559 */
560 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_Run_0400, Function | MediumTest | Level3)
561 {
562 OH_NNModel *model = OH_NNModel_Construct();
563 ASSERT_NE(nullptr, model);
564 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
565
566 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
567 ASSERT_NE(nullptr, compilation);
568 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
569
570 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
571 ASSERT_NE(nullptr, executor);
572 uint32_t inputIndex = 0;
573 uint32_t outputIndex = 0;
574
575 const OHNNOperandTest &operandTem = graphArgs.operands[0];
576 auto quantParam = operandTem.quantParam;
577 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
578 quantParam, operandTem.type};
579 ASSERT_EQ(OH_NN_SUCCESS,
580 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length));
581 inputIndex += 1;
582 const OHNNOperandTest &operandOut = graphArgs.operands[3];
583 ASSERT_EQ(OH_NN_SUCCESS, OH_NNExecutor_SetOutput(executor, outputIndex, operandOut.data, operandOut.length));
584 ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNExecutor_Run(executor));
585
586 Free(model, compilation, executor);
587 }
588
589 /**
590 * @tc.number : SUB_AI_NNRt_Func_North_Executor_Run_0500
591 * @tc.name : 模型推理,executor设置输出个数不足
592 * @tc.desc : [C- SOFTWARE -0200]
593 */
594 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_Run_0500, Function | MediumTest | Level3)
595 {
596 OH_NNModel *model = OH_NNModel_Construct();
597 ASSERT_NE(nullptr, model);
598 TopKModel topKModel;
599 graphArgs = topKModel.graphArgs;
600 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
601
602 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
603 ASSERT_NE(nullptr, compilation);
604 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
605
606 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
607 ASSERT_NE(nullptr, executor);
608
609 graphArgs.outputIndices = {3};
610 ASSERT_EQ(OH_NN_INVALID_PARAMETER, ExecuteGraphMock(executor, graphArgs, addModel.expectValue));
611
612 Free(model, compilation, executor);
613 }
614
615 /**
616 * @tc.number : SUB_AI_NNRt_Func_North_Executor_Run_0600
617 * @tc.name : 定长模型推理测试
618 * @tc.desc : [C- SOFTWARE -0200]
619 */
620 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_Run_0600, Function | MediumTest | Level1)
621 {
622 OH_NNModel *model = OH_NNModel_Construct();
623 ASSERT_NE(nullptr, model);
624 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
625
626 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
627 ASSERT_NE(nullptr, compilation);
628 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
629
630 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
631 ASSERT_NE(nullptr, executor);
632
633 ASSERT_EQ(OH_NN_SUCCESS, ExecuteGraphMock(executor, graphArgs, addModel.expectValue));
634
635 EXPECT_TRUE(CheckOutput(addModel.outputValue, addModel.expectValue));
636
637 Free(model, compilation, executor);
638 }
639
640 /**
641 * @tc.number : SUB_AI_NNRt_Func_North_Executor_Run_0700
642 * @tc.name : 变长模型推理测试
643 * @tc.desc : [C- SOFTWARE -0200]
644 */
645 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_Run_0700, Function | MediumTest | Level1)
646 {
647 OH_NNModel *model = OH_NNModel_Construct();
648 ASSERT_NE(nullptr, model);
649 AvgPoolDynamicModel avgModel;
650 graphArgs = avgModel.graphArgs;
651 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
652
653 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
654 ASSERT_NE(nullptr, compilation);
655
656 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
657
658 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
659 ASSERT_NE(nullptr, executor);
660 avgModel.dynamicInput.shape = {1, 3, 3, 1};
661 avgModel.output.shape = {1, 2, 2, 1};
662 graphArgs.operands = {avgModel.dynamicInput, avgModel.kernel, avgModel.strides,
663 avgModel.padMode, avgModel.activation, avgModel.output};
664 ASSERT_EQ(OH_NN_SUCCESS, ExecuteGraphMock(executor, graphArgs, avgModel.expectValue));
665 // check result
666 EXPECT_TRUE(CheckOutput(avgModel.outputValue, avgModel.expectValue));
667 Free(model, compilation, executor);
668 }
669
670 /**
671 * @tc.number : SUB_AI_NNRt_Func_North_Executor_GetOutputDimensions_0100
672 * @tc.name : 获取输出维度,executor为nullptr
673 * @tc.desc : [C- SOFTWARE -0200]
674 */
675 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_GetOutputDimensions_0100, Function | MediumTest | Level3)
676 {
677 OH_NNModel *model = OH_NNModel_Construct();
678 ASSERT_NE(nullptr, model);
679 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
680
681 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
682 ASSERT_NE(nullptr, compilation);
683 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
684
685 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
686 ASSERT_NE(nullptr, executor);
687
688 ASSERT_EQ(OH_NN_SUCCESS, ExecuteGraphMock(executor, graphArgs, addModel.expectValue));
689
690 EXPECT_TRUE(CheckOutput(addModel.outputValue, addModel.expectValue));
691 int32_t *outputDimensions = nullptr;
692 uint32_t outputDimensionCount{0};
693 uint32_t addOutputIndex = {0};
694 ASSERT_EQ(OH_NN_INVALID_PARAMETER,
695 OH_NNExecutor_GetOutputShape(nullptr, addOutputIndex, &outputDimensions, &outputDimensionCount));
696
697 Free(model, compilation, executor);
698 }
699
700 /**
701 * @tc.number : SUB_AI_NNRt_Func_North_Executor_GetOutputDimensions_0200
702 * @tc.name : 获取输出维度,outputIndex不存在
703 * @tc.desc : [C- SOFTWARE -0200]
704 */
705 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_GetOutputDimensions_0200, Function | MediumTest | Level3)
706 {
707 OH_NNModel *model = OH_NNModel_Construct();
708 ASSERT_NE(nullptr, model);
709 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
710
711 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
712 ASSERT_NE(nullptr, compilation);
713 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
714
715 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
716 ASSERT_NE(nullptr, executor);
717
718 ASSERT_EQ(OH_NN_SUCCESS, ExecuteGraphMock(executor, graphArgs, addModel.expectValue));
719
720 EXPECT_TRUE(CheckOutput(addModel.outputValue, addModel.expectValue));
721 int32_t *outputDimensions = nullptr;
722 uint32_t outputDimensionCount{0};
723 uint32_t addOutputIndex = {10000};
724 ASSERT_EQ(OH_NN_INVALID_PARAMETER,
725 OH_NNExecutor_GetOutputShape(executor, addOutputIndex, &outputDimensions, &outputDimensionCount));
726
727 Free(model, compilation, executor);
728 }
729
730 /**
731 * @tc.number : SUB_AI_NNRt_Func_North_Executor_GetOutputDimensions_0300
732 * @tc.name : 获取输出维度,*dimensions为nullptr
733 * @tc.desc : [C- SOFTWARE -0200]
734 */
735 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_GetOutputDimensions_0300, Function | MediumTest | Level3)
736 {
737 OH_NNModel *model = OH_NNModel_Construct();
738 ASSERT_NE(nullptr, model);
739 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
740
741 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
742 ASSERT_NE(nullptr, compilation);
743 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
744
745 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
746 ASSERT_NE(nullptr, executor);
747
748 ASSERT_EQ(OH_NN_SUCCESS, ExecuteGraphMock(executor, graphArgs, addModel.expectValue));
749
750 EXPECT_TRUE(CheckOutput(addModel.outputValue, addModel.expectValue));
751 uint32_t outputDimensionCount{0};
752 uint32_t addOutputIndex = {0};
753 ASSERT_EQ(OH_NN_INVALID_PARAMETER,
754 OH_NNExecutor_GetOutputShape(executor, addOutputIndex, nullptr, &outputDimensionCount));
755
756 Free(model, compilation, executor);
757 }
758
759 /**
760 * @tc.number : SUB_AI_NNRt_Func_North_Executor_GetOutputDimensions_0400
761 * @tc.name : 获取输出维度,**dimensions非nullptr
762 * @tc.desc : [C- SOFTWARE -0200]
763 */
764 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_GetOutputDimensions_0400, Function | MediumTest | Level3)
765 {
766 OH_NNModel *model = OH_NNModel_Construct();
767 ASSERT_NE(nullptr, model);
768 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
769
770 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
771 ASSERT_NE(nullptr, compilation);
772 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
773
774 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
775 ASSERT_NE(nullptr, executor);
776
777 ASSERT_EQ(OH_NN_SUCCESS, ExecuteGraphMock(executor, graphArgs, addModel.expectValue));
778
779 EXPECT_TRUE(CheckOutput(addModel.outputValue, addModel.expectValue));
780 int32_t outputDimensions{2};
781 int32_t *pOutputDimensions = &outputDimensions;
782 uint32_t outputDimensionCount{0};
783 uint32_t addOutputIndex = {0};
784 ASSERT_EQ(OH_NN_INVALID_PARAMETER,
785 OH_NNExecutor_GetOutputShape(executor, addOutputIndex, &pOutputDimensions, &outputDimensionCount));
786
787 Free(model, compilation, executor);
788 }
789
790 /**
791 * @tc.number : SUB_AI_NNRt_Func_North_Executor_GetOutputDimensions_0500
792 * @tc.name : 获取输出维度,*dimensionCount为nullptr
793 * @tc.desc : [C- SOFTWARE -0200]
794 */
795 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_GetOutputDimensions_0500, Function | MediumTest | Level3)
796 {
797 OH_NNModel *model = OH_NNModel_Construct();
798 ASSERT_NE(nullptr, model);
799 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
800
801 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
802 ASSERT_NE(nullptr, compilation);
803 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
804
805 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
806 ASSERT_NE(nullptr, executor);
807
808 ASSERT_EQ(OH_NN_SUCCESS, ExecuteGraphMock(executor, graphArgs, addModel.expectValue));
809
810 EXPECT_TRUE(CheckOutput(addModel.outputValue, addModel.expectValue));
811 int32_t *outputDimensions = nullptr;
812 uint32_t addOutputIndex = {0};
813 ASSERT_EQ(OH_NN_INVALID_PARAMETER,
814 OH_NNExecutor_GetOutputShape(executor, addOutputIndex, &outputDimensions, nullptr));
815
816 Free(model, compilation, executor);
817 }
818
819 /**
820 * @tc.number : SUB_AI_NNRt_Func_North_Executor_GetOutputDimensions_0600
821 * @tc.name : 未调用推理接口,获取输出维度
822 * @tc.desc : [C- SOFTWARE -0200]
823 */
824 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_GetOutputDimensions_0600, Function | MediumTest | Level3)
825 {
826 OH_NNModel *model = OH_NNModel_Construct();
827 ASSERT_NE(nullptr, model);
828 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
829
830 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
831 ASSERT_NE(nullptr, compilation);
832 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
833
834 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
835 ASSERT_NE(nullptr, executor);
836 uint32_t inputIndex = 0;
837 uint32_t outputIndex = 0;
838 for (int i = 0; i < graphArgs.operands.size(); i++) {
839 const OHNNOperandTest &operandTem = graphArgs.operands[i];
840 auto quantParam = operandTem.quantParam;
841 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
842 quantParam, operandTem.type};
843 if (std::find(graphArgs.inputIndices.begin(), graphArgs.inputIndices.end(), i) !=
844 graphArgs.inputIndices.end()) {
845 ASSERT_EQ(OH_NN_SUCCESS,
846 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length));
847 inputIndex += 1;
848 } else if (std::find(graphArgs.outputIndices.begin(), graphArgs.outputIndices.end(), i) !=
849 graphArgs.outputIndices.end()) {
850 ASSERT_EQ(OH_NN_SUCCESS,
851 OH_NNExecutor_SetOutput(executor, outputIndex, operandTem.data, operandTem.length));
852 outputIndex += 1;
853 }
854 }
855 int32_t *outputDimensions = nullptr;
856 uint32_t outputDimensionCount{0};
857 uint32_t addOutputIndex = {0};
858 ASSERT_EQ(OH_NN_OPERATION_FORBIDDEN,
859 OH_NNExecutor_GetOutputShape(executor, addOutputIndex, &outputDimensions, &outputDimensionCount));
860
861 Free(model, compilation, executor);
862 }
863
864 /**
865 * @tc.number : SUB_AI_NNRt_Func_North_Executor_GetOutputDimensions_0700
866 * @tc.name : 模型推理成功,获取输出维度
867 * @tc.desc : [C- SOFTWARE -0200]
868 */
869 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_GetOutputDimensions_0700, Function | MediumTest | Level2)
870 {
871 OH_NNModel *model = OH_NNModel_Construct();
872 ASSERT_NE(nullptr, model);
873 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
874
875 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
876 ASSERT_NE(nullptr, compilation);
877 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
878
879 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
880 ASSERT_NE(nullptr, executor);
881
882 ASSERT_EQ(OH_NN_SUCCESS, ExecuteGraphMock(executor, graphArgs, addModel.expectValue));
883
884 EXPECT_TRUE(CheckOutput(addModel.outputValue, addModel.expectValue));
885 int32_t *outputDimensions = nullptr;
886 uint32_t outputDimensionCount{0};
887 uint32_t addOutputIndex = {0};
888 ASSERT_EQ(OH_NN_SUCCESS,
889 OH_NNExecutor_GetOutputShape(executor, addOutputIndex, &outputDimensions, &outputDimensionCount));
890
891 Free(model, compilation, executor);
892 }
893
894 /**
895 * @tc.number : SUB_AI_NNRt_Func_North_Executor_GetOutputDimensions_0800
896 * @tc.name : 变长模型推理成功,获取输出维度
897 * @tc.desc : [C- SOFTWARE -0200]
898 */
899 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_GetOutputDimensions_0800, Function | MediumTest | Level1)
900 {
901 OH_NNModel *model = OH_NNModel_Construct();
902 ASSERT_NE(nullptr, model);
903 AvgPoolDynamicModel avgModel;
904 graphArgs = avgModel.graphArgs;
905 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
906
907 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
908 ASSERT_NE(nullptr, compilation);
909
910 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
911
912 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
913 ASSERT_NE(nullptr, executor);
914 avgModel.dynamicInput.shape = {1, 3, 3, 1};
915 avgModel.output.shape = {1, 2, 2, 1};
916 graphArgs.operands = {avgModel.dynamicInput, avgModel.kernel, avgModel.strides,
917 avgModel.padMode, avgModel.activation, avgModel.output};
918 ASSERT_EQ(OH_NN_SUCCESS, ExecuteGraphMock(executor, graphArgs, avgModel.expectValue));
919
920 // check result
921 EXPECT_TRUE(CheckOutput(avgModel.outputValue, avgModel.expectValue));
922 int32_t *outputDimensions = nullptr;
923 uint32_t outputDimensionCount{0};
924 uint32_t addOutputIndex = {0};
925 ASSERT_EQ(OH_NN_SUCCESS,
926 OH_NNExecutor_GetOutputShape(executor, addOutputIndex, &outputDimensions, &outputDimensionCount));
927
928 Free(model, compilation, executor);
929 }
930
931 /**
932 * @tc.number : SUB_AI_NNRt_Func_North_Executor_Destroy_0100
933 * @tc.name : 销毁执行器实例,*executor为nullptr
934 * @tc.desc : [C- SOFTWARE -0200]
935 */
936 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_Destroy_0100, Function | MediumTest | Level3)
937 {
938 OH_NNExecutor *executor = nullptr;
939 ASSERT_NO_THROW(OH_NNExecutor_Destroy(&executor));
940 }
941
942 /**
943 * @tc.number : SUB_AI_NNRt_Func_North_Executor_Destroy_0200
944 * @tc.name : 销毁执行器实例,executor释放
945 * @tc.desc : [C- SOFTWARE -0200]
946 */
947 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_Destroy_0200, Function | MediumTest | Level1)
948 {
949 OH_NNModel *model = OH_NNModel_Construct();
950 ASSERT_NE(nullptr, model);
951 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
952
953 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
954 ASSERT_NE(nullptr, compilation);
955 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
956
957 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
958 ASSERT_NE(nullptr, executor);
959
960 ASSERT_EQ(OH_NN_SUCCESS, ExecuteGraphMock(executor, graphArgs, addModel.expectValue));
961
962 EXPECT_TRUE(CheckOutput(addModel.outputValue, addModel.expectValue));
963
964 OH_NNExecutor_Destroy(&executor);
965 ASSERT_EQ(nullptr, executor);
966
967 Free(model, compilation);
968 }
969
970 /**
971 * @tc.number : SUB_AI_NNR_Func_North_Executor_Combine_0100
972 * @tc.name : 并发模型推理,推理成功
973 * @tc.desc : [C- SOFTWARE -0200]
974 */
975 HWTEST_F(ExecutorTest, SUB_AI_NNR_Func_North_Executor_Combine_0100, Function | MediumTest | Level2)
976 {
977 OH_NNModel *model1 = OH_NNModel_Construct();
978 ASSERT_NE(nullptr, model1);
979 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model1, graphArgs));
980
981 OH_NNModel *model2 = OH_NNModel_Construct();
982 ASSERT_NE(nullptr, model2);
983 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model2, graphArgs));
984
985 OH_NNCompilation *compilation1 = OH_NNCompilation_Construct(model1);
986 ASSERT_NE(nullptr, compilation1);
987 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation1, compileParam));
988
989 OH_NNCompilation *compilation2 = OH_NNCompilation_Construct(model2);
990 ASSERT_NE(nullptr, compilation2);
991 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation2, compileParam));
992
993 OH_NNExecutor *executor1 = OH_NNExecutor_Construct(compilation1);
994 ASSERT_NE(nullptr, executor1);
995
996 OH_NNExecutor *executor2 = OH_NNExecutor_Construct(compilation2);
997 ASSERT_NE(nullptr, executor2);
998
999 std::thread th1(ExecuteModel, executor1, graphArgs, addModel.expectValue);
1000 std::thread th2(ExecuteModel, executor2, graphArgs, addModel.expectValue);
1001 th1.join();
1002 th2.join();
1003 Free(model1, compilation1, executor1);
1004 Free(model2, compilation2, executor2);
1005 }
1006
1007 /**
1008 * @tc.number : SUB_AI_NNRt_Func_North_Executor_Combine_0200
1009 * @tc.name : 多次设置输入,仅首次成功,模型推理
1010 * @tc.desc : [C- SOFTWARE -0200]
1011 */
1012 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_Combine_0200, Function | MediumTest | Level1)
1013 {
1014 OH_NNModel *model = OH_NNModel_Construct();
1015 ASSERT_NE(nullptr, model);
1016 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
1017
1018 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
1019 ASSERT_NE(nullptr, compilation);
1020 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
1021
1022 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
1023 ASSERT_NE(nullptr, executor);
1024
1025 float valueX2[4] = {3, 2, 1, 0};
1026 uint32_t inputIndex = 0;
1027 uint32_t outputIndex = 0;
1028 for (auto i = 0; i < graphArgs.operands.size(); i++) {
1029 const OHNNOperandTest &operandTem = graphArgs.operands[i];
1030 auto quantParam = operandTem.quantParam;
1031 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
1032 quantParam, operandTem.type};
1033 if (std::find(graphArgs.inputIndices.begin(), graphArgs.inputIndices.end(), i) !=
1034 graphArgs.inputIndices.end()) {
1035 ASSERT_EQ(OH_NN_SUCCESS,
1036 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length));
1037 EXPECT_EQ(OH_NN_INVALID_PARAMETER,
1038 OH_NNExecutor_SetInput(executor, 3, &operand, valueX2, operandTem.length));
1039 inputIndex += 1;
1040 } else if (std::find(graphArgs.outputIndices.begin(), graphArgs.outputIndices.end(), i) !=
1041 graphArgs.outputIndices.end()) {
1042 ASSERT_EQ(OH_NN_SUCCESS,
1043 OH_NNExecutor_SetOutput(executor, outputIndex, operandTem.data, operandTem.length));
1044 OHOS::sptr<V1_0::MockIDevice> device = V1_0::MockIDevice::GetInstance();
1045 ASSERT_EQ(OH_NN_SUCCESS, device->MemoryCopy(addModel.expectValue, operandTem.length));
1046 outputIndex += 1;
1047 }
1048 }
1049 ASSERT_EQ(OH_NN_SUCCESS, OH_NNExecutor_Run(executor));
1050 // check result
1051 EXPECT_TRUE(CheckOutput(addModel.outputValue, addModel.expectValue));
1052 Free(model, compilation, executor);
1053 }
1054
1055 /**
1056 * @tc.number : SUB_AI_NNRt_Func_North_Executor_Combine_0300
1057 * @tc.name : 多次设置输出,仅首次生效,模型推理
1058 * @tc.desc : [C- SOFTWARE -0200]
1059 */
1060 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_Combine_0300, Function | MediumTest | Level1)
1061 {
1062 OH_NNModel *model = OH_NNModel_Construct();
1063 ASSERT_NE(nullptr, model);
1064 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
1065
1066 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
1067 ASSERT_NE(nullptr, compilation);
1068 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
1069
1070 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
1071 ASSERT_NE(nullptr, executor);
1072
1073 uint32_t inputIndex = 0;
1074 uint32_t outputIndex = 0;
1075
1076 for (auto i = 0; i < graphArgs.operands.size(); i++) {
1077 const OHNNOperandTest &operandTem = graphArgs.operands[i];
1078 auto quantParam = operandTem.quantParam;
1079 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
1080 quantParam, operandTem.type};
1081 if (std::find(graphArgs.inputIndices.begin(), graphArgs.inputIndices.end(), i) !=
1082 graphArgs.inputIndices.end()) {
1083 ASSERT_EQ(OH_NN_SUCCESS,
1084 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length));
1085 inputIndex += 1;
1086 } else if (std::find(graphArgs.outputIndices.begin(), graphArgs.outputIndices.end(), i) !=
1087 graphArgs.outputIndices.end()) {
1088 ASSERT_EQ(OH_NN_SUCCESS,
1089 OH_NNExecutor_SetOutput(executor, outputIndex, operandTem.data, operandTem.length));
1090 ASSERT_EQ(OH_NN_INVALID_PARAMETER,
1091 OH_NNExecutor_SetOutput(executor, outputIndex+10, operandTem.data, operandTem.length));
1092 OHOS::sptr<V1_0::MockIDevice> device = V1_0::MockIDevice::GetInstance();
1093 ASSERT_EQ(OH_NN_SUCCESS, device->MemoryCopy(addModel.expectValue, operandTem.length));
1094 outputIndex += 1;
1095 }
1096 }
1097 ASSERT_EQ(OH_NN_SUCCESS, OH_NNExecutor_Run(executor));
1098 // check result
1099 EXPECT_TRUE(CheckOutput(addModel.outputValue, addModel.expectValue));
1100 ASSERT_EQ(OH_NN_SUCCESS, OH_NNExecutor_Run(executor));
1101 Free(model, compilation, executor);
1102 }
1103
1104 /**
1105 * @tc.number : SUB_AI_NNRt_Func_North_Executor_Combine_0400
1106 * @tc.name : 模型推理,共享输入非共享输出
1107 * @tc.desc : [C- SOFTWARE -0200]
1108 */
1109 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_Combine_0400, Function | MediumTest | Level2)
1110 {
1111 OH_NNModel *model = OH_NNModel_Construct();
1112 ASSERT_NE(nullptr, model);
1113 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
1114
1115 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
1116 ASSERT_NE(nullptr, compilation);
1117 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
1118
1119 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
1120 ASSERT_NE(nullptr, executor);
1121 OH_NN_Memory *OHNNMemory[graphArgs.inputIndices.size()];
1122 uint32_t inputIndex = 0;
1123 uint32_t outputIndex = 0;
1124
1125 for (auto i = 0; i < graphArgs.operands.size(); i++) {
1126 const OHNNOperandTest &operandTem = graphArgs.operands[i];
1127 auto quantParam = operandTem.quantParam;
1128 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
1129 quantParam, operandTem.type};
1130 if (std::find(graphArgs.inputIndices.begin(), graphArgs.inputIndices.end(), i) !=
1131 graphArgs.inputIndices.end()) {
1132
1133 OH_NN_Memory *inputMemory = OH_NNExecutor_AllocateInputMemory(executor, inputIndex, operandTem.length);
1134 ASSERT_NE(nullptr, inputMemory);
1135
1136 ASSERT_EQ(OH_NN_SUCCESS, OH_NNExecutor_SetInputWithMemory(executor, inputIndex, &operand, inputMemory));
1137
1138 ASSERT_EQ(EOK, memcpy_s(inputMemory->data, operandTem.length, (void *)operandTem.data, operandTem.length));
1139 OHNNMemory[inputIndex] = inputMemory;
1140 inputIndex += 1;
1141 } else if (std::find(graphArgs.outputIndices.begin(), graphArgs.outputIndices.end(), i) !=
1142 graphArgs.outputIndices.end()) {
1143 ASSERT_EQ(OH_NN_SUCCESS,
1144 OH_NNExecutor_SetOutput(executor, outputIndex, operandTem.data, operandTem.length));
1145 OHOS::sptr<V1_0::MockIDevice> device = V1_0::MockIDevice::GetInstance();
1146 ASSERT_EQ(OH_NN_SUCCESS, device->MemoryCopy(addModel.expectValue, operandTem.length));
1147 outputIndex += 1;
1148 }
1149 }
1150 ASSERT_EQ(OH_NN_SUCCESS, OH_NNExecutor_Run(executor));
1151 // check result
1152 EXPECT_TRUE(CheckOutput(addModel.outputValue, addModel.expectValue));
1153
1154 for (auto i = 0; i < graphArgs.inputIndices.size(); i++) {
1155 OH_NNExecutor_DestroyInputMemory(executor, i, &OHNNMemory[i]);
1156 ASSERT_EQ(OHNNMemory[i], nullptr);
1157 }
1158 Free(model, compilation, executor);
1159 }
1160
1161 /**
1162 * @tc.number : SUB_AI_NNRt_Func_North_Executor_Combine_0500
1163 * @tc.name : 模型推理,非共享输入共享输出
1164 * @tc.desc : [C- SOFTWARE -0200]
1165 */
1166 HWTEST_F(ExecutorTest, SUB_AI_NNRt_Func_North_Executor_Combine_0500, Function | MediumTest | Level2)
1167 {
1168 OH_NNModel *model = OH_NNModel_Construct();
1169 ASSERT_NE(nullptr, model);
1170 ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
1171
1172 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
1173 ASSERT_NE(nullptr, compilation);
1174 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
1175
1176 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
1177 ASSERT_NE(nullptr, executor);
1178 uint32_t inputIndex = 0;
1179 uint32_t outputIndex = 0;
1180 OH_NN_Memory *outputMemory;
1181
1182 for (auto i = 0; i < graphArgs.operands.size(); i++) {
1183 const OHNNOperandTest &operandTem = graphArgs.operands[i];
1184 auto quantParam = operandTem.quantParam;
1185 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
1186 quantParam, operandTem.type};
1187 if (std::find(graphArgs.inputIndices.begin(), graphArgs.inputIndices.end(), i) !=
1188 graphArgs.inputIndices.end()) {
1189 ASSERT_EQ(OH_NN_SUCCESS,
1190 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length));
1191
1192 inputIndex += 1;
1193 } else if (std::find(graphArgs.outputIndices.begin(), graphArgs.outputIndices.end(), i) !=
1194 graphArgs.outputIndices.end()) {
1195
1196 outputMemory = OH_NNExecutor_AllocateOutputMemory(executor, outputIndex, operandTem.length);
1197 ASSERT_NE(nullptr, outputMemory);
1198 ASSERT_EQ(OH_NN_SUCCESS,
1199 OH_NNExecutor_SetOutputWithMemory(executor, outputIndex, outputMemory));
1200 OHOS::sptr<V1_0::MockIDevice> device = V1_0::MockIDevice::GetInstance();
1201 ASSERT_EQ(OH_NN_SUCCESS, device->MemoryCopy(addModel.expectValue, operandTem.length));
1202 outputIndex += 1;
1203 }
1204 }
1205 ASSERT_EQ(OH_NN_SUCCESS, OH_NNExecutor_Run(executor));
1206 // check result
1207 EXPECT_TRUE(CheckOutput(static_cast<float*>(const_cast<void*>(outputMemory->data)),
1208 (float*) addModel.expectValue));
1209
1210 OH_NNExecutor_DestroyOutputMemory(executor, 0, &outputMemory);
1211 ASSERT_EQ(outputMemory, nullptr);
1212 Free(model, compilation, executor);
1213 }