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