• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <fstream>
20 
21 #include "nnrt_utils.h"
22 #include "model.h"
23 
24 using namespace testing::ext;
25 using namespace OHOS::NeuralNetworkRuntime;
26 using namespace OHOS::NeuralNetworkRuntime::Test;
27 using namespace OHOS::HDI::Nnrt::V1_0;
28 
29 namespace {
30 
31 class CompileTest : public testing::Test {
32 public:
SetUp()33     void SetUp()
34     {
35         CreateFolder(CACHE_DIR);
36     }
TearDown()37     void TearDown()
38     {
39         DeleteFolder(CACHE_DIR);
40     }
GenCacheFile()41     void GenCacheFile()
42     {
43         OH_NNModel *model = OH_NNModel_Construct();
44         ASSERT_NE(nullptr, model);
45         ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
46         OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
47         ASSERT_NE(nullptr, compilation);
48         OHNNCompileParam compileParam{
49             .cacheDir = "./cache",
50             .cacheVersion = 10,
51         };
52         ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
53         Free(model, compilation);
54         ASSERT_TRUE(CheckPath(CACHE_PATH) == PathType::FILE);
55         ASSERT_TRUE(CheckPath(CACHE_INFO_PATH) == PathType::FILE);
56     }
DestroyCache()57     void DestroyCache()
58     {
59         std::ifstream ifs(CACHE_PATH.c_str(), std::ios::in | std::ios::binary);
60         char* ptr{nullptr};
61         int cacheSize = ifs.tellg();
62         int invalidCacheSize = cacheSize * 0.9;
63         ifs.read(ptr, cacheSize);
64         ifs.close();
65         std::ofstream ofs(CACHE_PATH.c_str(), std::ios::out | std::ios::binary);
66         ofs.write(ptr, invalidCacheSize);
67         ofs.close();
68     }
69 
70 protected:
71     OHNNCompileParam compileParam;
72     AddModel addModel;
73     OHNNGraphArgs graphArgs = addModel.graphArgs;
74 };
75 
CompileModel(OH_NNCompilation * compilation,const OHNNCompileParam & compileParam)76 void CompileModel(OH_NNCompilation *compilation, const OHNNCompileParam &compileParam)
77 {
78     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
79 }
80 
81 } // namespace
82 
83 /**
84  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_Create_0100
85  * @tc.name   : 创建编译实例,model为nullptr
86  * @tc.desc   : [C- SOFTWARE -0200]
87  */
88 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_Create_0100, Function | MediumTest | Level3)
89 {
90     OH_NNCompilation *compilation = OH_NNCompilation_Construct(nullptr);
91     ASSERT_EQ(nullptr, compilation);
92 }
93 
94 /**
95  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_Create_0200
96  * @tc.name   : 创建编译实例,model未完成构图
97  * @tc.desc   : [C- SOFTWARE -0200]
98  */
99 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_Create_0200, Function | MediumTest | Level3)
100 {
101     OH_NNModel *model = OH_NNModel_Construct();
102     ASSERT_NE(nullptr, model);
103     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
104     ASSERT_EQ(nullptr, compilation);
105     Free(model, compilation);
106 }
107 
108 /**
109  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_Create_0300
110  * @tc.name   : 创建编译实例,model已完成构图,存在算子不支持
111  * @tc.desc   : [C- SOFTWARE -0200]
112  */
113 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_Create_0300, Function | MediumTest | Level2)
114 {
115     OHOS::sptr<V1_0::MockIDevice> device = V1_0::MockIDevice::GetInstance();
116     std::vector<bool> isSupported = {true, false};
117     device->SetOperationsSupported(isSupported);
118 
119     OH_NNModel *model = OH_NNModel_Construct();
120     ASSERT_NE(nullptr, model);
121 
122     AddTopKModel addTopKModel;
123     OHNNGraphArgsMulti graphArgsMulti = addTopKModel.graphArgs;
124     ASSERT_EQ(OH_NN_SUCCESS, BuildMultiOpGraph(model, graphArgsMulti));
125 
126     const size_t *devicesID{nullptr};
127     const bool *realSupported{nullptr};
128     uint32_t opCount;
129     uint32_t devicesCount;
130     ASSERT_EQ(OH_NN_SUCCESS, OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount));
131     size_t targetDevice = devicesID[0];
132 
133     OH_NN_ReturnCode ret = OH_NNModel_GetAvailableOperations(model, targetDevice, &realSupported, &opCount);
134     ASSERT_EQ(OH_NN_SUCCESS, ret);
135     for (int i = 0; i < opCount; i++) {
136         EXPECT_EQ(realSupported[i], isSupported[i]);
137     }
138     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
139     ASSERT_NE(nullptr, compilation);
140     ASSERT_EQ(OH_NN_FAILED, OH_NNCompilation_SetDevice(compilation, targetDevice));
141     Free(model, compilation);
142     device->SetOperationsSupported({true});
143 }
144 
145 /**
146  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetDevice_0100
147  * @tc.name   : 设置device,compilation为nullptr
148  * @tc.desc   : [C- SOFTWARE -0200]
149  */
150 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetDevice_0100, Function | MediumTest | Level3)
151 {
152     const size_t *devicesID{nullptr};
153     uint32_t devicesCount{0};
154     ASSERT_EQ(OH_NN_SUCCESS, OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount));
155 
156     size_t targetDevice = devicesID[0]; // Use the first device in system test.
157     ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNCompilation_SetDevice(nullptr, targetDevice));
158 }
159 
160 /**
161  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetDevice_0200
162  * @tc.name   : 设置device,deviceID不存在
163  * @tc.desc   : [C- SOFTWARE -0200]
164  */
165 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetDevice_0200, Function | MediumTest | Level3)
166 {
167     OH_NNModel *model = OH_NNModel_Construct();
168     ASSERT_NE(nullptr, model);
169     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
170     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
171     ASSERT_NE(nullptr, compilation);
172     ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNCompilation_SetDevice(compilation, 100000));
173     Free(model, compilation);
174 }
175 
176 /**
177  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetDevice_0300
178  * @tc.name   : 设置device,deviceID存在
179  * @tc.desc   : [C- SOFTWARE -0200]
180  */
181 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetDevice_0300, Function | MediumTest | Level3)
182 {
183     OH_NNModel *model = OH_NNModel_Construct();
184     ASSERT_NE(nullptr, model);
185     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
186     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
187     ASSERT_NE(nullptr, compilation);
188     OHNNCompileParam compileParam;
189     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
190     Free(model, compilation);
191 }
192 
193 /**
194  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetCache_0100
195  * @tc.name   : 设置cache路径及版本,compilation为nullptr
196  * @tc.desc   : [C- SOFTWARE -0200]
197  */
198 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetCache_0100, Function | MediumTest | Level3)
199 {
200     ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNCompilation_SetCache(nullptr, "./", 0));
201 }
202 
203 /**
204  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetCache_0200
205  * @tc.name   : 设置cache路径及版本,cacheDir为nullptr
206  * @tc.desc   : [C- SOFTWARE -0200]
207  */
208 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetCache_0200, Function | MediumTest | Level3)
209 {
210     OH_NNModel *model = OH_NNModel_Construct();
211     ASSERT_NE(nullptr, model);
212     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
213     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
214     ASSERT_NE(nullptr, compilation);
215     ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNCompilation_SetCache(compilation, nullptr, 0));
216     Free(model, compilation);
217 }
218 
219 /**
220  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetCache_0300
221  * @tc.name   : device不支持,设置cache路径及版本
222  * @tc.desc   : [C- SOFTWARE -0200]
223  */
224 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetCache_0300, Function | MediumTest | Level3)
225 {
226     OH_NNModel *model = OH_NNModel_Construct();
227     ASSERT_NE(nullptr, model);
228     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
229 
230     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
231     ASSERT_NE(nullptr, compilation);
232     // set model cache unavailabel
233     OHOS::sptr<V1_0::MockIDevice> device = V1_0::MockIDevice::GetInstance();
234     device->SetModelCacheSupported(false);
235 
236     ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
237     ASSERT_EQ(OH_NN_OPERATION_FORBIDDEN, OH_NNCompilation_SetCache(compilation, "./cache", 10));
238     Free(model, compilation);
239     device->SetModelCacheSupported(true);
240 }
241 
242 /**
243  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetCache_0400
244  * @tc.name   : 设置不存在cache路径
245  * @tc.desc   : [C- SOFTWARE -0200]
246  */
247 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetCache_0400, Function | MediumTest | Level3)
248 {
249     OH_NNModel *model = OH_NNModel_Construct();
250     ASSERT_NE(nullptr, model);
251     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
252     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
253     ASSERT_NE(nullptr, compilation);
254     OHNNCompileParam compileParam{.cacheDir = "./test"};
255     ASSERT_EQ(OH_NN_INVALID_PARAMETER, CompileGraphMock(compilation, compileParam));
256     Free(model, compilation);
257 }
258 
259 /**
260  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetCache_0500
261  * @tc.name   : 设置cache路径,cache不完整
262  * @tc.desc   : [C- SOFTWARE -0200]
263  */
264 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetCache_0500, Function | MediumTest | Level2)
265 {
266     // generate cache file in cache diretory
267     GenCacheFile();
268     // destroy cache file to invalid size
269     DestroyCache();
270     OH_NNModel *model = OH_NNModel_Construct();
271     ASSERT_NE(nullptr, model);
272     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
273     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
274     ASSERT_NE(nullptr, compilation);
275     OHNNCompileParam compileParam{
276         .cacheDir = "./cache",
277         .cacheVersion = 10,
278     };
279     ASSERT_EQ(OH_NN_INVALID_FILE, CompileGraphMock(compilation, compileParam));
280     Free(model, compilation);
281 }
282 
283 /**
284  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetCache_0600
285  * @tc.name   : 设置version,小于cache版本号
286  * @tc.desc   : [C- SOFTWARE -0200]
287  */
288 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetCache_0600, Function | MediumTest | Level2)
289 {
290     GenCacheFile();
291     OH_NNModel *model = OH_NNModel_Construct();
292     ASSERT_NE(nullptr, model);
293     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
294     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
295     ASSERT_NE(nullptr, compilation);
296     OHNNCompileParam compileParam{
297         .cacheDir = "./cache",
298         .cacheVersion = 9,
299     };
300     ASSERT_EQ(OH_NN_OPERATION_FORBIDDEN, CompileGraphMock(compilation, compileParam));
301     Free(model, compilation);
302 }
303 
304 /**
305  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetCache_0700
306  * @tc.name   : 设置version,等于cache版本号
307  * @tc.desc   : [C- SOFTWARE -0200]
308  */
309 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetCache_0700, Function | MediumTest | Level2)
310 {
311     GenCacheFile();
312     OH_NNModel *model = OH_NNModel_Construct();
313     ASSERT_NE(nullptr, model);
314     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
315     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
316     ASSERT_NE(nullptr, compilation);
317     OHNNCompileParam compileParam{
318         .cacheDir = "./cache",
319         .cacheVersion = 10,
320     };
321     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
322     Free(model, compilation);
323 }
324 
325 /**
326  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetCache_0800
327  * @tc.name   : 设置version,大于cache版本号
328  * @tc.desc   : [C- SOFTWARE -0200]
329  */
330 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetCache_0800, Function | MediumTest | Level2)
331 {
332     GenCacheFile();
333     OH_NNModel *model = OH_NNModel_Construct();
334     ASSERT_NE(nullptr, model);
335     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
336     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
337     ASSERT_NE(nullptr, compilation);
338     OHNNCompileParam compileParam{
339         .cacheDir = "./cache",
340         .cacheVersion = 11,
341     };
342     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
343     Free(model, compilation);
344 }
345 
346 /**
347  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetPerformanceMode_0100
348  * @tc.name   : 设置priority,compilation为nullptr
349  * @tc.desc   : [C- SOFTWARE -0200]
350  */
351 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetPerformanceMode_0100, Function | MediumTest | Level3)
352 {
353     ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNCompilation_SetPerformanceMode(nullptr, OH_NN_PERFORMANCE_MEDIUM));
354 }
355 
356 /**
357  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetPerformanceMode_0200
358  * @tc.name   : device不支持,设置performance
359  * @tc.desc   : [C- SOFTWARE -0200]
360  */
361 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetPerformanceMode_Mock_0200, Function | MediumTest | Level3)
362 {
363     OH_NNModel *model = OH_NNModel_Construct();
364     ASSERT_NE(nullptr, model);
365     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
366 
367     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
368     ASSERT_NE(nullptr, compilation);
369     OHNNCompileParam compileParam{
370         .performanceMode = OH_NN_PERFORMANCE_LOW,
371     };
372     OHOS::sptr<V1_0::MockIDevice> device = V1_0::MockIDevice::GetInstance();
373     device->SetPerformanceSupported(false);
374     ASSERT_EQ(OH_NN_OPERATION_FORBIDDEN, CompileGraphMock(compilation, compileParam));
375     Free(model, compilation);
376     device->SetPerformanceSupported(true);
377 }
378 
379 /**
380  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetPerformanceMode_0300
381  * @tc.name   : 设置performanceMode为NONE
382  * @tc.desc   : [C- SOFTWARE -0200]
383  */
384 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetPerformanceMode_0300, Function | MediumTest | Level2)
385 {
386     OH_NNModel *model = OH_NNModel_Construct();
387     ASSERT_NE(nullptr, model);
388     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
389     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
390     ASSERT_NE(nullptr, compilation);
391     ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
392     ASSERT_EQ(OH_NN_SUCCESS, OH_NNCompilation_SetPerformanceMode(compilation, OH_NN_PERFORMANCE_NONE));
393     Free(model, compilation);
394 }
395 
396 /**
397  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetPerformanceMode_0400
398  * @tc.name   : 设置performanceMode为LOW
399  * @tc.desc   : [C- SOFTWARE -0200]
400  */
401 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetPerformanceMode_0400, Function | MediumTest | Level2)
402 {
403     OH_NNModel *model = OH_NNModel_Construct();
404     ASSERT_NE(nullptr, model);
405     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
406     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
407     ASSERT_NE(nullptr, compilation);
408     ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
409     ASSERT_EQ(OH_NN_SUCCESS, OH_NNCompilation_SetPerformanceMode(compilation, OH_NN_PERFORMANCE_LOW));
410     Free(model, compilation);
411 }
412 
413 /**
414  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetPerformanceMode_0500
415  * @tc.name   : 设置performanceMode为MEDIUM
416  * @tc.desc   : [C- SOFTWARE -0200]
417  */
418 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetPerformanceMode_0500, Function | MediumTest | Level2)
419 {
420     OH_NNModel *model = OH_NNModel_Construct();
421     ASSERT_NE(nullptr, model);
422     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
423     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
424     ASSERT_NE(nullptr, compilation);
425     ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
426     ASSERT_EQ(OH_NN_SUCCESS, OH_NNCompilation_SetPerformanceMode(compilation, OH_NN_PERFORMANCE_MEDIUM));
427     Free(model, compilation);
428 }
429 
430 /**
431  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetPerformanceMode_0600
432  * @tc.name   : 设置performanceMode为HIGH
433  * @tc.desc   : [C- SOFTWARE -0200]
434  */
435 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetPerformanceMode_0600, Function | MediumTest | Level2)
436 {
437     OH_NNModel *model = OH_NNModel_Construct();
438     ASSERT_NE(nullptr, model);
439     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
440     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
441     ASSERT_NE(nullptr, compilation);
442     ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
443     ASSERT_EQ(OH_NN_SUCCESS, OH_NNCompilation_SetPerformanceMode(compilation, OH_NN_PERFORMANCE_HIGH));
444     Free(model, compilation);
445 }
446 
447 /**
448  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetPerformanceMode_0700
449  * @tc.name   : 设置performanceMode为EXTREME
450  * @tc.desc   : [C- SOFTWARE -0200]
451  */
452 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetPerformanceMode_0700, Function | MediumTest | Level2)
453 {
454     OH_NNModel *model = OH_NNModel_Construct();
455     ASSERT_NE(nullptr, model);
456     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
457     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
458     ASSERT_NE(nullptr, compilation);
459     ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
460     ASSERT_EQ(OH_NN_SUCCESS, OH_NNCompilation_SetPerformanceMode(compilation, OH_NN_PERFORMANCE_EXTREME));
461     Free(model, compilation);
462 }
463 
464 /**
465  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetPerformanceMode_0800
466  * @tc.name   : 设置performanceMode为NONE-1
467  * @tc.desc   : [C- SOFTWARE -0200]
468  */
469 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetPerformanceMode_0800, Function | MediumTest | Level3)
470 {
471     OH_NNModel *model = OH_NNModel_Construct();
472     ASSERT_NE(nullptr, model);
473     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
474     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
475     ASSERT_NE(nullptr, compilation);
476     ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
477     ASSERT_EQ(OH_NN_INVALID_PARAMETER,
478               OH_NNCompilation_SetPerformanceMode(compilation,
479                                                   static_cast<OH_NN_PerformanceMode>(OH_NN_PERFORMANCE_NONE - 1)));
480     Free(model, compilation);
481 }
482 
483 /**
484  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetPerformanceMode_0900
485  * @tc.name   : 设置performanceMode为EXTREME+1
486  * @tc.desc   : [C- SOFTWARE -0200]
487  */
488 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetPerformanceMode_0900, Function | MediumTest | Level3)
489 {
490     OH_NNModel *model = OH_NNModel_Construct();
491     ASSERT_NE(nullptr, model);
492     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
493     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
494     ASSERT_NE(nullptr, compilation);
495     ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
496     ASSERT_EQ(OH_NN_INVALID_PARAMETER,
497               OH_NNCompilation_SetPerformanceMode(compilation,
498                                                   static_cast<OH_NN_PerformanceMode>(OH_NN_PERFORMANCE_EXTREME + 1)));
499     Free(model, compilation);
500 }
501 
502 /**
503  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetPriority_0100
504  * @tc.name   : 设置priority,compilation为nullptr
505  * @tc.desc   : [C- SOFTWARE -0200]
506  */
507 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetPriority_0100, Function | MediumTest | Level3)
508 {
509     ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNCompilation_SetPriority(nullptr, OH_NN_PRIORITY_MEDIUM));
510 }
511 
512 /**
513  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetPriority_0200
514  * @tc.name   : device不支持,设置priority
515  * @tc.desc   : [C- SOFTWARE -0200]
516  */
517 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetPriority_0200, Function | MediumTest | Level3)
518 {
519     OH_NNModel *model = OH_NNModel_Construct();
520     ASSERT_NE(nullptr, model);
521     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
522     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
523     ASSERT_NE(nullptr, compilation);
524     // set device not supported
525     OHOS::sptr<V1_0::MockIDevice> device = V1_0::MockIDevice::GetInstance();
526     device->SetPrioritySupported(false);
527     ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
528     ASSERT_EQ(OH_NN_OPERATION_FORBIDDEN, OH_NNCompilation_SetPriority(compilation, OH_NN_PRIORITY_NONE));
529     Free(model, compilation);
530     device->SetPrioritySupported(true);
531 }
532 
533 /**
534  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetPriority_0300
535  * @tc.name   : 设置priority为NONE
536  * @tc.desc   : [C- SOFTWARE -0200]
537  */
538 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetPriority_0300, Function | MediumTest | Level2)
539 {
540     OH_NNModel *model = OH_NNModel_Construct();
541     ASSERT_NE(nullptr, model);
542     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
543     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
544     ASSERT_NE(nullptr, compilation);
545     ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
546     ASSERT_EQ(OH_NN_SUCCESS, OH_NNCompilation_SetPriority(compilation, OH_NN_PRIORITY_NONE));
547     Free(model, compilation);
548 }
549 
550 /**
551  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetPriority_0400
552  * @tc.name   : 设置priority为LOW
553  * @tc.desc   : [C- SOFTWARE -0200]
554  */
555 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetPriority_0400, Function | MediumTest | Level2)
556 {
557     OH_NNModel *model = OH_NNModel_Construct();
558     ASSERT_NE(nullptr, model);
559     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
560     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
561     ASSERT_NE(nullptr, compilation);
562     ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
563     ASSERT_EQ(OH_NN_SUCCESS, OH_NNCompilation_SetPriority(compilation, OH_NN_PRIORITY_LOW));
564     Free(model, compilation);
565 }
566 
567 /**
568  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetPriority_0500
569  * @tc.name   : 设置priority为MEDIUM
570  * @tc.desc   : [C- SOFTWARE -0200]
571  */
572 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetPriority_0500, Function | MediumTest | Level2)
573 {
574     OH_NNModel *model = OH_NNModel_Construct();
575     ASSERT_NE(nullptr, model);
576     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
577     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
578     ASSERT_NE(nullptr, compilation);
579     ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
580     ASSERT_EQ(OH_NN_SUCCESS, OH_NNCompilation_SetPriority(compilation, OH_NN_PRIORITY_MEDIUM));
581     Free(model, compilation);
582 }
583 
584 /**
585  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetPriority_0600
586  * @tc.name   : 设置priority为LOW
587  * @tc.desc   : [C- SOFTWARE -0200]
588  */
589 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetPriority_0600, Function | MediumTest | Level2)
590 {
591     OH_NNModel *model = OH_NNModel_Construct();
592     ASSERT_NE(nullptr, model);
593     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
594     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
595     ASSERT_NE(nullptr, compilation);
596     ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
597     ASSERT_EQ(OH_NN_SUCCESS, OH_NNCompilation_SetPriority(compilation, OH_NN_PRIORITY_HIGH));
598     Free(model, compilation);
599 }
600 
601 /**
602  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetPriority_0700
603  * @tc.name   : 设置priority为NONE-1
604  * @tc.desc   : [C- SOFTWARE -0200]
605  */
606 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetPriority_0700, Function | MediumTest | Level3)
607 {
608     OH_NNModel *model = OH_NNModel_Construct();
609     ASSERT_NE(nullptr, model);
610     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
611     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
612     ASSERT_NE(nullptr, compilation);
613     ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
614     ASSERT_EQ(OH_NN_INVALID_PARAMETER,
615               OH_NNCompilation_SetPriority(compilation, static_cast<OH_NN_Priority>(OH_NN_PRIORITY_NONE - 1)));
616     Free(model, compilation);
617 }
618 
619 /**
620  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_SetPriority_0800
621  * @tc.name   : 设置priority为HIGH+1
622  * @tc.desc   : [C- SOFTWARE -0200]
623  */
624 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_SetPriority_0800, Function | MediumTest | Level3)
625 {
626     OH_NNModel *model = OH_NNModel_Construct();
627     ASSERT_NE(nullptr, model);
628     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
629 
630     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
631     ASSERT_NE(nullptr, compilation);
632     ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
633     ASSERT_EQ(OH_NN_INVALID_PARAMETER,
634               OH_NNCompilation_SetPriority(compilation, static_cast<OH_NN_Priority>(OH_NN_PRIORITY_HIGH + 1)));
635     Free(model, compilation);
636 }
637 
638 /**
639  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_EnableFloat16_0100
640  * @tc.name   : 设置enableFloat16,compilation为nullptr
641  * @tc.desc   : [C- SOFTWARE -0200]
642  */
643 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_EnableFloat16_0100, Function | MediumTest | Level3)
644 {
645     ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNCompilation_EnableFloat16(nullptr, OH_NN_PERFORMANCE_MEDIUM));
646 }
647 
648 /**
649  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_EnableFloat16_0200
650  * @tc.name   : device支持,设置fp16推理为false
651  * @tc.desc   : [C- SOFTWARE -0200]
652  */
653 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_EnableFloat16_0200, Function | MediumTest | Level3)
654 {
655     OH_NNModel *model = OH_NNModel_Construct();
656     ASSERT_NE(nullptr, model);
657     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
658     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
659     ASSERT_NE(nullptr, compilation);
660     ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
661     ASSERT_EQ(OH_NN_SUCCESS, OH_NNCompilation_EnableFloat16(compilation, false));
662     Free(model, compilation);
663 }
664 
665 /**
666  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_EnableFloat16_0300
667  * @tc.name   : device不支持,设置fp16推理为false
668  * @tc.desc   : [C- SOFTWARE -0200]
669  */
670 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_EnableFloat16_0300, Function | MediumTest | Level2)
671 {
672     OH_NNModel *model = OH_NNModel_Construct();
673     ASSERT_NE(nullptr, model);
674     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
675     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
676     ASSERT_NE(nullptr, compilation);
677     // set fp16 unavailable
678     OHOS::sptr<V1_0::MockIDevice> device = V1_0::MockIDevice::GetInstance();
679     device->SetFP16Supported(false);
680 
681     ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
682     ASSERT_EQ(OH_NN_OPERATION_FORBIDDEN, OH_NNCompilation_EnableFloat16(compilation, false));
683     Free(model, compilation);
684     device->SetFP16Supported(true);
685 }
686 
687 /**
688  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_EnableFloat16_0400
689  * @tc.name   : device不支持,设置fp16推理为true
690  * @tc.desc   : [C- SOFTWARE -0200]
691  */
692 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_EnableFloat16_0400, Function | MediumTest | Level3)
693 {
694     OH_NNModel *model = OH_NNModel_Construct();
695     ASSERT_NE(nullptr, model);
696     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
697     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
698     ASSERT_NE(nullptr, compilation);
699     // set fp16 unavailable
700     OHOS::sptr<V1_0::MockIDevice> device = V1_0::MockIDevice::GetInstance();
701     device->SetFP16Supported(false);
702 
703     ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
704     ASSERT_EQ(OH_NN_OPERATION_FORBIDDEN, OH_NNCompilation_EnableFloat16(compilation, true));
705     Free(model, compilation);
706     device->SetFP16Supported(true);
707 }
708 
709 /**
710  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_Build_0100
711  * @tc.name   : 编译模型,compilation为空指针
712  * @tc.desc   : [C- SOFTWARE -0200]
713  */
714 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_Build_0100, Function | MediumTest | Level3)
715 {
716     ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNCompilation_Build(nullptr));
717 }
718 
719 /**
720  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_Build_0200
721  * @tc.name   : 编译模型,未设置device
722  * @tc.desc   : [C- SOFTWARE -0200]
723  */
724 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_Build_0200, Function | MediumTest | Level2)
725 {
726     OH_NNModel *model = OH_NNModel_Construct();
727     ASSERT_NE(nullptr, model);
728     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
729     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
730     ASSERT_NE(nullptr, compilation);
731     ASSERT_EQ(OH_NN_OPERATION_FORBIDDEN, OH_NNCompilation_Build(compilation));
732     Free(model, compilation);
733 }
734 
735 /**
736  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_Build_0300
737  * @tc.name   : 编译模型,仅设置device,默认配置测试
738  * @tc.desc   : [C- SOFTWARE -0200]
739  */
740 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_Build_0300, Function | MediumTest | Level1)
741 {
742     OH_NNModel *model = OH_NNModel_Construct();
743     ASSERT_NE(nullptr, model);
744     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
745     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
746     ASSERT_NE(nullptr, compilation);
747     OHNNCompileParam compileParam;
748     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
749     Free(model, compilation);
750 }
751 
752 /**
753  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_Build_0400
754  * @tc.name   : 设置缓存路径及版本,编译模型导出缓存
755  * @tc.desc   : [C- SOFTWARE -0200]
756  */
757 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_Build_0400, Function | MediumTest | Level2)
758 {
759     OH_NNModel *model = OH_NNModel_Construct();
760     ASSERT_NE(nullptr, model);
761     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
762     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
763     ASSERT_NE(nullptr, compilation);
764     OHNNCompileParam compileParam{
765         .cacheDir = "./cache",
766         .cacheVersion = 10,
767     };
768     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
769     Free(model, compilation);
770 }
771 
772 /**
773  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_Destroy_0100
774  * @tc.name   : 释放编译实例,compilation为nullptr
775  * @tc.desc   : [C- SOFTWARE -0200]
776  */
777 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_Destroy_0100, Function | MediumTest | Level3)
778 {
779     OH_NNCompilation *compilation = nullptr;
780     OH_NNCompilation_Destroy(&compilation);
781     ASSERT_EQ(nullptr, compilation);
782 }
783 
784 /**
785  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_Destroy_0200
786  * @tc.name   : 释放编译实例,未调用模型编译
787  * @tc.desc   : [C- SOFTWARE -0200]
788  */
789 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_Destroy_0200, Function | MediumTest | Level2)
790 {
791     OH_NNModel *model = OH_NNModel_Construct();
792     ASSERT_NE(nullptr, model);
793     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
794     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
795     ASSERT_NE(nullptr, compilation);
796     ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
797     OH_NNCompilation_Destroy(&compilation);
798     ASSERT_EQ(nullptr, compilation);
799 }
800 
801 /**
802  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_Destroy_0300
803  * @tc.name   : 模型已编译,释放编译实例
804  * @tc.desc   : [C- SOFTWARE -0200]
805  */
806 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_Destroy_0300, Function | MediumTest | Level0)
807 {
808     OH_NNModel *model = OH_NNModel_Construct();
809     ASSERT_NE(nullptr, model);
810     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
811     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
812     ASSERT_NE(nullptr, compilation);
813     OHNNCompileParam compileParam;
814     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
815     Free(model, compilation);
816 }
817 
818 /**
819  * @tc.number : SUB_AI_NNR_Func_North_Compilation_Combine_0100
820  * @tc.name   : 多线程并发模型编译,编译成功
821  * @tc.desc   : [C- SOFTWARE -0200]
822  */
823 HWTEST_F(CompileTest, SUB_AI_NNR_Func_North_Compilation_Combine_0100, Function | MediumTest | Level2)
824 {
825     OH_NNModel *model1 = OH_NNModel_Construct();
826     ASSERT_NE(nullptr, model1);
827     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model1, graphArgs));
828 
829     OH_NNModel *model2 = OH_NNModel_Construct();
830     ASSERT_NE(nullptr, model2);
831     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model2, graphArgs));
832 
833     OH_NNCompilation *compilation1 = OH_NNCompilation_Construct(model1);
834     ASSERT_NE(nullptr, compilation1);
835     OH_NNCompilation *compilation2 = OH_NNCompilation_Construct(model2);
836     ASSERT_NE(nullptr, compilation2);
837 
838     std::thread th1(CompileModel, compilation1, compileParam);
839     std::thread th2(CompileModel, compilation2, compileParam);
840     th1.join();
841     th2.join();
842     Free(model1, compilation1);
843     Free(model2, compilation2);
844 }
845 
846 /**
847  * @tc.number : SUB_AI_NNRt_Func_North_Compilation_Combine_0200
848  * @tc.name   : 已编译模型,重复编译
849  * @tc.desc   : [C- SOFTWARE -0200]
850  */
851 HWTEST_F(CompileTest, SUB_AI_NNRt_Func_North_Compilation_Combine_0200, Function | MediumTest | Level2)
852 {
853     OH_NNModel *model = OH_NNModel_Construct();
854     ASSERT_NE(nullptr, model);
855     ASSERT_EQ(OH_NN_SUCCESS, BuildSingleOpGraph(model, graphArgs));
856     OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
857     ASSERT_NE(nullptr, compilation);
858     OHNNCompileParam compileParam;
859     ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
860     ASSERT_EQ(OH_NN_OPERATION_FORBIDDEN, OH_NNCompilation_Build(compilation));
861     Free(model, compilation);
862 }