1 /*
2 * Copyright (c) 2023 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 <fstream>
16
17 #include "nncore_utils.h"
18
19 using namespace testing::ext;
20 using namespace OHOS::NeuralNetworkRuntime::Test;
21 namespace OHOS::NeuralNetworkCore {
22 class CompilationTest : public testing::Test {
23 public:
SetUp()24 void SetUp()
25 {
26 CreateFolder(CACHE_DIR);
27 }
TearDown()28 void TearDown()
29 {
30 DeleteFolder(CACHE_DIR);
31 }
GenCacheFile()32 void GenCacheFile()
33 {
34 OH_NNCompilation *compilation = nullptr;
35 OH_NNModel *model = nullptr;
36 ConstructCompilation(&compilation, &model);
37 OHNNCompileParam compileParam{
38 .cacheDir = CACHE_DIR,
39 .cacheVersion = CACHEVERSION,
40 };
41 ASSERT_EQ(OH_NN_SUCCESS, CompileGraphMock(compilation, compileParam));
42 ASSERT_TRUE(CheckPath(CACHE_PATH) == PathType::FILE);
43 ASSERT_TRUE(CheckPath(CACHE_INFO_PATH) == PathType::FILE);
44 OH_NNModel_Destroy(&model);
45 OH_NNCompilation_Destroy(&compilation);
46 }
SaveSupportModel()47 void SaveSupportModel()
48 {
49 OH_NNModel *model = nullptr;
50 ConstructAddModel(&model);
51 std::ofstream ofs(SUPPORTMODELPATH, std::ios::out | std::ios::binary);
52 if (ofs) {
53 ofs.write(reinterpret_cast<char*>(model), sizeof(reinterpret_cast<char*>(model)));
54 ofs.close();
55 }
56 OH_NNModel_Destroy(&model);
57 }
58
59 protected:
60 OHNNCompileParam m_compileParam;
61 AddModel addModel;
62 OHNNGraphArgs graphArgs = addModel.graphArgs;
63 };
64
65 /**
66 * @tc.name: SUB_AI_NNRt_Core_Func_North_Construct_Compilation_For_Cache_0100
67 * @tc.desc: 创建compilation,检查返回值为空,设置正确的cache路径,build成功,推理成功
68 * @tc.type: FUNC
69 */
HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Construct_Compilation_For_Cache_0100, Function | MediumTest | Level1)70 HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Construct_Compilation_For_Cache_0100,
71 Function | MediumTest | Level1)
72 {
73 GenCacheFile();
74 OH_NNCompilation *compilation = OH_NNCompilation_ConstructForCache();
75 ASSERT_NE(nullptr, compilation);
76
77 ASSERT_EQ(OH_NN_SUCCESS, OH_NNCompilation_SetCache(compilation, CACHE_DIR.c_str(), CACHEVERSION));
78 ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
79 ASSERT_EQ(OH_NN_SUCCESS, OH_NNCompilation_Build(compilation));
80 OH_NNCompilation_Destroy(&compilation);
81 }
82
83 /**
84 * @tc.name: SUB_AI_NNRt_Core_Func_North_Construct_Compilation_For_Cache_0200
85 * @tc.desc: 创建compilation,检查返回值非空,不设置cache,build失败
86 * @tc.type: FUNC
87 */
HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Construct_Compilation_For_Cache_0200, Function | MediumTest | Level1)88 HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Construct_Compilation_For_Cache_0200,
89 Function | MediumTest | Level1)
90 {
91 OH_NNCompilation *compilation = OH_NNCompilation_ConstructForCache();
92 ASSERT_NE(nullptr, compilation);
93 ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNCompilation_Build(compilation));
94 OH_NNCompilation_Destroy(&compilation);
95 }
96
97 /**
98 * @tc.name: SUB_AI_NNRt_Core_Func_North_AddExtension_Config_To_Compilation_0100
99 * @tc.desc: 创建compilation,增加config,传入compilation为空,返回错误
100 * @tc.type: FUNC
101 */
HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_AddExtension_Config_To_Compilation_0100, Function | MediumTest | Level1)102 HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_AddExtension_Config_To_Compilation_0100,
103 Function | MediumTest | Level1)
104 {
105 const char *configName = "test";
106 const void *configValue = reinterpret_cast<const void*>(10);
107 const size_t configValueSize = 1;
108 OH_NN_ReturnCode ret = OH_NNCompilation_AddExtensionConfig(nullptr, configName, configValue, configValueSize);
109 ASSERT_EQ(OH_NN_INVALID_PARAMETER, ret);
110 }
111
112 /**
113 * @tc.name: SUB_AI_NNRt_Core_Func_North_AddExtension_Config_To_Compilation_0200
114 * @tc.desc: 创建compilation,增加config,传入configNames为空指针,返回错误
115 * @tc.type: FUNC
116 */
HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_AddExtension_Config_To_Compilation_0200, Function | MediumTest | Level1)117 HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_AddExtension_Config_To_Compilation_0200,
118 Function | MediumTest | Level1)
119 {
120 OH_NNCompilation *compilation = nullptr;
121 OH_NNModel *model = nullptr;
122 ConstructCompilation(&compilation, &model);
123
124 const void *configValue = reinterpret_cast<const void*>(10);
125 const size_t configValueSize = 1;
126 OH_NN_ReturnCode ret = OH_NNCompilation_AddExtensionConfig(compilation, nullptr, configValue, configValueSize);
127 ASSERT_EQ(OH_NN_INVALID_PARAMETER, ret);
128 OH_NNModel_Destroy(&model);
129 OH_NNCompilation_Destroy(&compilation);
130 }
131
132 /**
133 * @tc.name: SUB_AI_NNRt_Core_Func_North_AddExtension_Config_To_Compilation_0300
134 * @tc.desc: 创建compilation,增加config,传入configNames为空字符串,报错
135 * @tc.type: FUNC
136 */
HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_AddExtension_Config_To_Compilation_0300, Function | MediumTest | Level1)137 HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_AddExtension_Config_To_Compilation_0300,
138 Function | MediumTest | Level1)
139 {
140 OH_NNCompilation *compilation = nullptr;
141 OH_NNModel *model = nullptr;
142 ConstructCompilation(&compilation, &model);
143
144 const char *configName = "";
145 int num = 10;
146 const void *configValue = #
147 const size_t configValueSize = sizeof(num);
148
149 OH_NN_ReturnCode ret = OH_NNCompilation_AddExtensionConfig(compilation, configName, configValue, configValueSize);
150 ASSERT_EQ(OH_NN_INVALID_PARAMETER, ret);
151 OH_NNModel_Destroy(&model);
152 OH_NNCompilation_Destroy(&compilation);
153 }
154
155 /**
156 * @tc.name: SUB_AI_NNRt_Core_Func_North_AddExtension_Config_To_Compilation_0400
157 * @tc.desc: 创建compilation,增加config,传入configValues为空,报错
158 * @tc.type: FUNC
159 */
HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_AddExtension_Config_To_Compilation_0400, Function | MediumTest | Level1)160 HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_AddExtension_Config_To_Compilation_0400,
161 Function | MediumTest | Level1)
162 {
163 OH_NNCompilation *compilation = nullptr;
164 OH_NNModel *model = nullptr;
165 ConstructCompilation(&compilation, &model);
166
167 const char *configName = "test";
168 const size_t configValueSize = 1;
169 OH_NN_ReturnCode ret = OH_NNCompilation_AddExtensionConfig(compilation, configName, nullptr, configValueSize);
170 ASSERT_EQ(OH_NN_INVALID_PARAMETER, ret);
171 OH_NNModel_Destroy(&model);
172 OH_NNCompilation_Destroy(&compilation);
173 }
174
175 /**
176 * @tc.name: SUB_AI_NNRt_Core_Func_North_AddExtension_Config_To_Compilation_0500
177 * @tc.desc: 创建compilation,增加config,传入configValueSize为0
178 * @tc.type: FUNC
179 */
HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_AddExtension_Config_To_Compilation_0500, Function | MediumTest | Level1)180 HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_AddExtension_Config_To_Compilation_0500,
181 Function | MediumTest | Level1)
182 {
183 OH_NNCompilation *compilation = nullptr;
184 OH_NNModel *model = nullptr;
185 ConstructCompilation(&compilation, &model);
186
187 const char *configName = "test";
188 const void *configValue = reinterpret_cast<const void*>(10);
189 const size_t configValueSize = 0;
190 OH_NN_ReturnCode ret = OH_NNCompilation_AddExtensionConfig(compilation, configName, configValue, configValueSize);
191 ASSERT_EQ(OH_NN_INVALID_PARAMETER, ret);
192 OH_NNModel_Destroy(&model);
193 OH_NNCompilation_Destroy(&compilation);
194 }
195
196 /**
197 * @tc.name: SUB_AI_NNRt_Core_Func_North_Construct_Compilation_With_OfflineModel_File_0100
198 * @tc.desc: 传入filepath为空指针,返回不支持
199 * @tc.type: FUNC
200 */
HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Construct_Compilation_With_OfflineModel_File_0100, Function | MediumTest | Level1)201 HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Construct_Compilation_With_OfflineModel_File_0100,
202 Function | MediumTest | Level1)
203 {
204 OH_NNCompilation *compilation = OH_NNCompilation_ConstructWithOfflineModelFile(nullptr);
205 ASSERT_EQ(nullptr, compilation);
206 }
207
208 /**
209 * @tc.name: SUB_AI_NNRt_Core_Func_North_Construct_Compilation_With_OfflineModel_File_0200
210 * @tc.desc: 传入合法文件,返回不支持
211 * @tc.type: FUNC
212 */
HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Construct_Compilation_With_OfflineModel_File_0200, Function | MediumTest | Level1)213 HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Construct_Compilation_With_OfflineModel_File_0200,
214 Function | MediumTest | Level1)
215 {
216 SaveSupportModel();
217 OH_NNCompilation *compilation = OH_NNCompilation_ConstructWithOfflineModelFile(SUPPORTMODELPATH.c_str());
218 ASSERT_NE(nullptr, compilation);
219
220 ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
221 ASSERT_EQ(OH_NN_FAILED, OH_NNCompilation_Build(compilation));
222 DeleteFile(SUPPORTMODELPATH);
223 OH_NNCompilation_Destroy(&compilation);
224 }
225
226 /**
227 * @tc.name: SUB_AI_NNRt_Core_Func_North_Construct_Compilation_With_Offline_ModelBuffer_0100
228 * @tc.desc: 传入modelData为空指针,返回错误
229 * @tc.type: FUNC
230 */
HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Construct_Compilation_With_Offline_ModelBuffer_0100, Function | MediumTest | Level1)231 HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Construct_Compilation_With_Offline_ModelBuffer_0100,
232 Function | MediumTest | Level1)
233 {
234 int modelSize = 0;
235 const void *buffer = nullptr;
236 OH_NNCompilation *compilation = OH_NNCompilation_ConstructWithOfflineModelBuffer(buffer, modelSize);
237 ASSERT_EQ(nullptr, compilation);
238 }
239
240 /**
241 * @tc.name: SUB_AI_NNRt_Core_Func_North_Construct_Compilation_With_Offline_ModelBuffer_0200
242 * @tc.desc: 传入modelData为合法离线模型buffer,返回不支持
243 * @tc.type: FUNC
244 */
HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Construct_Compilation_With_Offline_ModelBuffer_0200, Function | MediumTest | Level1)245 HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Construct_Compilation_With_Offline_ModelBuffer_0200,
246 Function | MediumTest | Level1)
247 {
248 OH_NNCompilation *compilation =
249 OH_NNCompilation_ConstructWithOfflineModelBuffer(reinterpret_cast<const void*>(TEST_BUFFER), 28);
250 ASSERT_NE(nullptr, compilation);
251 ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
252 ASSERT_EQ(OH_NN_FAILED, OH_NNCompilation_Build(compilation));
253 OH_NNCompilation_Destroy(&compilation);
254 }
255
256 /**
257 * @tc.name: SUB_AI_NNRt_Core_Func_North_Export_Compilation_Cache_To_Buffer_0100
258 * @tc.desc: 传入空指针返回失败
259 * @tc.type: FUNC
260 */
HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Export_Compilation_Cache_To_Buffer_0100, Function | MediumTest | Level1)261 HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Export_Compilation_Cache_To_Buffer_0100,
262 Function | MediumTest | Level1)
263 {
264 const char *any = "123456789";
265 const void *buffer = reinterpret_cast<const void*>(any);
266 size_t length = 10;
267 size_t *modelSize = &length;
268 OH_NN_ReturnCode ret = OH_NNCompilation_ExportCacheToBuffer(nullptr, buffer, length, modelSize);
269 ASSERT_EQ(OH_NN_INVALID_PARAMETER, ret);
270 }
271
272 /**
273 * @tc.name: SUB_AI_NNRt_Core_Func_North_Export_Compilation_Cache_To_Buffer_0200
274 * @tc.desc: 参数正确,nnrt模型返回不支持
275 * @tc.type: FUNC
276 */
HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Export_Compilation_Cache_To_Buffer_0200, Function | MediumTest | Level1)277 HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Export_Compilation_Cache_To_Buffer_0200,
278 Function | MediumTest | Level1)
279 {
280 OH_NNCompilation *compilation = nullptr;
281 OH_NNModel *model = nullptr;
282 ConstructCompilation(&compilation, &model);
283 ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
284 ASSERT_EQ(OH_NN_SUCCESS, OH_NNCompilation_Build(compilation));
285
286 const char *any = "123456789";
287 const void *buffer = reinterpret_cast<const void*>(any);
288 size_t length = 10;
289 size_t *modelSize = &length;
290 OH_NN_ReturnCode ret = OH_NNCompilation_ExportCacheToBuffer(compilation, buffer, length, modelSize);
291 ASSERT_EQ(OH_NN_UNSUPPORTED, ret);
292 OH_NNModel_Destroy(&model);
293 OH_NNCompilation_Destroy(&compilation);
294 }
295
296 /**
297 * @tc.name: SUB_AI_NNRt_Core_Func_North_Import_Compilation_Cache_From_Buffer_0100
298 * @tc.desc: buffer为空,返回错误
299 * @tc.type: FUNC
300 */
HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Import_Compilation_Cache_From_Buffer_0100, Function | MediumTest | Level1)301 HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Import_Compilation_Cache_From_Buffer_0100,
302 Function | MediumTest | Level1)
303 {
304 OH_NNCompilation *compilation = nullptr;
305 OH_NNModel *model = nullptr;
306 ConstructCompilation(&compilation, &model);
307
308 const void *buffer = nullptr;
309 size_t modelSize = MODEL_SIZE;
310 OH_NN_ReturnCode ret = OH_NNCompilation_ImportCacheFromBuffer(compilation, buffer, modelSize);
311 ASSERT_EQ(OH_NN_INVALID_PARAMETER, ret);
312 OH_NNModel_Destroy(&model);
313 OH_NNCompilation_Destroy(&compilation);
314 }
315
316 /**
317 * @tc.name: SUB_AI_NNRt_Core_Func_North_Import_Compilation_Cache_From_Buffer_0200
318 * @tc.desc: modelSize为0,返回错误
319 * @tc.type: FUNC
320 */
HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Import_Compilation_Cache_From_Buffer_0200, Function | MediumTest | Level1)321 HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Import_Compilation_Cache_From_Buffer_0200,
322 Function | MediumTest | Level1)
323 {
324 OH_NNCompilation *compilation = nullptr;
325 OH_NNModel *model = nullptr;
326 ConstructCompilation(&compilation, &model);
327 const char *any = "123456789";
328 const void *buffer = reinterpret_cast<const void*>(any);
329 size_t modelSize = ZERO;
330 OH_NN_ReturnCode ret = OH_NNCompilation_ImportCacheFromBuffer(compilation, buffer, modelSize);
331 ASSERT_EQ(OH_NN_INVALID_PARAMETER, ret);
332 OH_NNModel_Destroy(&model);
333 OH_NNCompilation_Destroy(&compilation);
334 }
335
336 /**
337 * @tc.name: SUB_AI_NNRt_Core_Func_North_Import_Compilation_Cache_From_Buffer_0300
338 * @tc.desc: 参数正确,返回不支持
339 * @tc.type: FUNC
340 */
HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Import_Compilation_Cache_From_Buffer_0300, Function | MediumTest | Level1)341 HWTEST_F(CompilationTest, SUB_AI_NNRt_Core_Func_North_Import_Compilation_Cache_From_Buffer_0300,
342 Function | MediumTest | Level1)
343 {
344 OH_NNCompilation *compilation = nullptr;
345 OH_NNModel *model = nullptr;
346 ConstructCompilation(&compilation, &model);
347
348 ASSERT_EQ(OH_NN_SUCCESS, OH_NNCompilation_SetCache(compilation, CACHE_DIR.c_str(), CACHEVERSION));
349 ASSERT_EQ(OH_NN_SUCCESS, SetDevice(compilation));
350 ASSERT_EQ(OH_NN_SUCCESS, OH_NNCompilation_SetPerformanceMode(compilation, OH_NN_PERFORMANCE_EXTREME));
351 ASSERT_EQ(OH_NN_SUCCESS, OH_NNCompilation_SetPriority(compilation, OH_NN_PRIORITY_HIGH));
352 ASSERT_EQ(OH_NN_SUCCESS, OH_NNCompilation_EnableFloat16(compilation, false));
353
354 const char *any = "123456789";
355 const void *buffer = reinterpret_cast<const void*>(any);
356 size_t modelSize = MODEL_SIZE;
357 ASSERT_EQ(OH_NN_SUCCESS, OH_NNCompilation_ImportCacheFromBuffer(compilation, buffer, modelSize));
358 ASSERT_EQ(OH_NN_INVALID_PARAMETER, OH_NNCompilation_Build(compilation));
359 OH_NNModel_Destroy(&model);
360 OH_NNCompilation_Destroy(&compilation);
361 }
362 }