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
16#include "gtest/gtest.h"
17#include <inttypes.h>
18#include <random>
19#include <securec.h>
20#include "include/c_api/context_c.h"
21#include "include/c_api/model_c.h"
22#include "include/c_api/types_c.h"
23#include "include/c_api/status_c.h"
24#include "include/c_api/data_type_c.h"
25#include "include/c_api/tensor_c.h"
26#include "include/c_api/format_c.h"
27#include "../utils/model_utils.h"
28#include "../utils/common.h"
29
30class MSLiteTest: public testing::Test {
31  protected:
32    static void SetUpTestCase(void) {}
33    static void TearDownTestCase(void) {}
34    virtual void SetUp() {}
35    virtual void TearDown() {}
36};
37
38std::string g_testFilesDir = "/data/test/resource/";
39
40/*
41 * @tc.name: Context_Create
42 * @tc.desc: Verify the return context of the OH_AI_ContextCreate function.
43 * @tc.type: FUNC
44 */
45HWTEST(MSLiteTest, Context_Create, testing::ext::TestSize.Level0) {
46    printf("==========Init Context==========\n");
47    OH_AI_ContextHandle context = OH_AI_ContextCreate();
48    ASSERT_NE(context, nullptr);
49}
50
51/*
52 * @tc.name: Context_Destroy
53 * @tc.desc: Verify the OH_AI_ContextDestroy function.
54 * @tc.type: FUNC
55 */
56HWTEST(MSLiteTest, Context_Destroy, testing::ext::TestSize.Level0) {
57    printf("==========Init Context==========\n");
58    OH_AI_ContextHandle context = OH_AI_ContextCreate();
59    ASSERT_NE(context, nullptr);
60    OH_AI_ContextDestroy(&context);
61    ASSERT_EQ(context, nullptr);
62}
63
64/*
65 * @tc.name: Context_Thread_Num
66 * @tc.desc: Verify the OH_AI_ContextSetThreadNum/OH_AI_ContextGetThreadNum function.
67 * @tc.type: FUNC
68 */
69HWTEST(MSLiteTest, Context_Thread_Num, testing::ext::TestSize.Level0) {
70    printf("==========Init Context==========\n");
71    OH_AI_ContextHandle context = OH_AI_ContextCreate();
72    ASSERT_NE(context, nullptr);
73    OH_AI_ContextSetThreadNum(context, 2);
74    auto thread_num = OH_AI_ContextGetThreadNum(context);
75    printf("==========thread_num: %d\n", thread_num);
76    ASSERT_EQ(thread_num, 2);
77}
78
79/*
80 * @tc.name: Context_Thread_Affinity
81 * @tc.desc: Verify the OH_AI_ContextSetThreadAffinityMode/OH_AI_ContextGetThreadAffinityMode function.
82 * @tc.type: FUNC
83 */
84HWTEST(MSLiteTest, Context_Thread_Affinity, testing::ext::TestSize.Level0) {
85    printf("==========Init Context==========\n");
86    OH_AI_ContextHandle context = OH_AI_ContextCreate();
87    ASSERT_NE(context, nullptr);
88    OH_AI_ContextSetThreadNum(context, 4);
89    auto thread_num = OH_AI_ContextGetThreadNum(context);
90    printf("==========thread_num: %d\n", thread_num);
91    ASSERT_EQ(thread_num, 4);
92
93    OH_AI_ContextSetThreadAffinityMode(context, 2);
94    int thread_affinity_mode = OH_AI_ContextGetThreadAffinityMode(context);
95    printf("==========thread_affinity_mode:%d\n", thread_affinity_mode);
96    ASSERT_EQ(thread_affinity_mode, 2);
97}
98
99/*
100 * @tc.name: Context_Thread_Affinity_Corelist
101 * @tc.desc: Verify the OH_AI_ContextSetThreadAffinityCoreList/OH_AI_ContextGetThreadAffinityCoreList function.
102 * @tc.type: FUNC
103 */
104HWTEST(MSLiteTest, Context_Thread_Affinity_Corelist, testing::ext::TestSize.Level0) {
105    printf("==========Init Context==========\n");
106    OH_AI_ContextHandle context = OH_AI_ContextCreate();
107    ASSERT_NE(context, nullptr);
108    OH_AI_ContextSetThreadNum(context, 4);
109    auto thread_num = OH_AI_ContextGetThreadNum(context);
110    printf("==========thread_num: %d\n", thread_num);
111    ASSERT_EQ(thread_num, 4);
112
113    constexpr size_t coreNum = 4;
114    int32_t coreList[coreNum] = {0, 1, 2, 3};
115    OH_AI_ContextSetThreadAffinityCoreList(context, coreList, coreNum);
116    size_t retCoreNum;
117    const int32_t *retCoreList = nullptr;
118    retCoreList = OH_AI_ContextGetThreadAffinityCoreList(context, &retCoreNum);
119    ASSERT_EQ(retCoreNum, coreNum);
120    for (size_t i = 0; i < retCoreNum; i++) {
121        printf("==========retCoreList:%d\n", retCoreList[i]);
122        ASSERT_EQ(retCoreList[i], coreList[i]);
123    }
124}
125
126/*
127 * @tc.name: Context_Enable_Parallel
128 * @tc.desc: Verify the OH_AI_ContextSetEnableParallel/OH_AI_ContextGetEnableParallel function.
129 * @tc.type: FUNC
130 */
131HWTEST(MSLiteTest, Context_Enable_Parallel, testing::ext::TestSize.Level0) {
132    printf("==========Init Context==========\n");
133    OH_AI_ContextHandle context = OH_AI_ContextCreate();
134    ASSERT_NE(context, nullptr);
135    OH_AI_ContextSetThreadNum(context, 4);
136    auto thread_num = OH_AI_ContextGetThreadNum(context);
137    printf("==========thread_num: %d\n", thread_num);
138    ASSERT_EQ(thread_num, 4);
139
140    OH_AI_ContextSetEnableParallel(context, true);
141    bool isParallel = OH_AI_ContextGetEnableParallel(context);
142    printf("==========isParallel:%d\n", isParallel);
143    ASSERT_EQ(isParallel, true);
144
145    AddContextDeviceCPU(context);
146    printf("==========Create model==========\n");
147    OH_AI_ModelHandle model = OH_AI_ModelCreate();
148    ASSERT_NE(model, nullptr);
149    ModelPredict(model, context, "ml_face_isface", {}, false, true, false);
150}
151
152/*
153 * @tc.name: Context_Enable_FP16
154 * @tc.desc: Verify the OH_AI_DeviceInfoSetEnableFP16/OH_AI_DeviceInfoGetEnableFP16 function.
155 * @tc.type: FUNC
156 */
157HWTEST(MSLiteTest, Context_Enable_FP16, testing::ext::TestSize.Level0) {
158    printf("==========Init Context==========\n");
159    OH_AI_ContextHandle context = OH_AI_ContextCreate();
160    ASSERT_NE(context, nullptr);
161
162    OH_AI_DeviceInfoHandle cpu_device_info = OH_AI_DeviceInfoCreate(OH_AI_DEVICETYPE_CPU);
163    ASSERT_NE(cpu_device_info, nullptr);
164    OH_AI_DeviceInfoSetEnableFP16(cpu_device_info, true);
165    bool isFp16 = OH_AI_DeviceInfoGetEnableFP16(cpu_device_info);
166    printf("==========isFp16:%d\n", isFp16);
167    ASSERT_EQ(isFp16, true);
168
169    OH_AI_ContextAddDeviceInfo(context, cpu_device_info);
170
171    printf("==========Create model==========\n");
172    OH_AI_ModelHandle model = OH_AI_ModelCreate();
173    ASSERT_NE(model, nullptr);
174    ModelPredict(model, context, "ml_face_isface", {}, false, true, false);
175}
176
177/*
178 * @tc.name: Context_Provider
179 * @tc.desc: Verify the OH_AI_DeviceInfoSetProvider/OH_AI_DeviceInfoGetProvider function.
180 * @tc.type: FUNC
181 */
182HWTEST(MSLiteTest, Context_Provider, testing::ext::TestSize.Level0) {
183    printf("==========Init Context==========\n");
184    OH_AI_ContextHandle context = OH_AI_ContextCreate();
185    ASSERT_NE(context, nullptr);
186
187    OH_AI_DeviceInfoHandle cpu_device_info = OH_AI_DeviceInfoCreate(OH_AI_DEVICETYPE_CPU);
188    ASSERT_NE(cpu_device_info, nullptr);
189    OH_AI_DeviceInfoSetProvider(cpu_device_info, "vendor_new");
190    ASSERT_EQ(strcmp(OH_AI_DeviceInfoGetProvider(cpu_device_info), "vendor_new"), 0);
191
192    OH_AI_ContextAddDeviceInfo(context, cpu_device_info);
193
194    printf("==========Create model==========\n");
195    OH_AI_ModelHandle model = OH_AI_ModelCreate();
196    ASSERT_NE(model, nullptr);
197    ModelPredict(model, context, "ml_face_isface", {}, false, true, false);
198}
199
200/*
201 * @tc.name: Context_Provider_Device
202 * @tc.desc: Verify the OH_AI_DeviceInfoSetProviderDevice/OH_AI_DeviceInfoGetProviderDevice function.
203 * @tc.type: FUNC
204 */
205HWTEST(MSLiteTest, Context_Provider_Device, testing::ext::TestSize.Level0) {
206    printf("==========Init Context==========\n");
207    OH_AI_ContextHandle context = OH_AI_ContextCreate();
208    ASSERT_NE(context, nullptr);
209
210    OH_AI_DeviceInfoHandle cpu_device_info = OH_AI_DeviceInfoCreate(OH_AI_DEVICETYPE_CPU);
211    ASSERT_NE(cpu_device_info, nullptr);
212    OH_AI_DeviceInfoSetProviderDevice(cpu_device_info, "cpu_new");
213    ASSERT_EQ(strcmp(OH_AI_DeviceInfoGetProviderDevice(cpu_device_info), "cpu_new"), 0);
214
215    OH_AI_ContextAddDeviceInfo(context, cpu_device_info);
216
217    printf("==========Create model==========\n");
218    OH_AI_ModelHandle model = OH_AI_ModelCreate();
219    ASSERT_NE(model, nullptr);
220    ModelPredict(model, context, "ml_face_isface", {}, false, true, false);
221}
222
223/*
224 * @tc.name: Context_Device
225 * @tc.desc: Verify the OH_AI_DeviceInfoCreate/OH_AI_DeviceInfoDestroy function.
226 * @tc.type: FUNC
227 */
228HWTEST(MSLiteTest, Context_Device, testing::ext::TestSize.Level0) {
229    OH_AI_DeviceInfoHandle cpu_device_info = OH_AI_DeviceInfoCreate(OH_AI_DEVICETYPE_CPU);
230    ASSERT_NE(cpu_device_info, nullptr);
231    OH_AI_DeviceType device_type = OH_AI_DeviceInfoGetDeviceType(cpu_device_info);
232    printf("==========device_type:%d\n", device_type);
233    ASSERT_EQ(device_type, OH_AI_DEVICETYPE_CPU);
234
235    OH_AI_DeviceInfoDestroy(&cpu_device_info);
236    ASSERT_EQ(cpu_device_info, nullptr);
237}
238
239/*
240 * @tc.name: Context_Kirin_Frequency
241 * @tc.desc: Verify the OH_AI_DeviceInfoSetFrequency/OH_AI_DeviceInfoGetFrequency function.
242 * @tc.type: FUNC
243 */
244HWTEST(MSLiteTest, Context_Kirin_Frequency, testing::ext::TestSize.Level0) {
245    printf("==========Init Context==========\n");
246    OH_AI_ContextHandle context = OH_AI_ContextCreate();
247    ASSERT_NE(context, nullptr);
248
249    OH_AI_DeviceInfoHandle npu_device_info = OH_AI_DeviceInfoCreate(OH_AI_DEVICETYPE_KIRIN_NPU);
250    ASSERT_NE(npu_device_info, nullptr);
251    OH_AI_DeviceInfoSetFrequency(npu_device_info, 1);
252    int frequency = OH_AI_DeviceInfoGetFrequency(npu_device_info);
253    printf("==========frequency:%d\n", frequency);
254    ASSERT_EQ(frequency, 1);
255    OH_AI_ContextAddDeviceInfo(context, npu_device_info);
256
257    OH_AI_ContextDestroy(&context);
258    ASSERT_EQ(context, nullptr);
259}
260
261/*
262 * @tc.name: Model_BuildByBuffer
263 * @tc.desc: Verify the OH_AI_ModelBuild function.
264 * @tc.type: FUNC
265 */
266HWTEST(MSLiteTest, Model_BuildByBuffer, testing::ext::TestSize.Level0) {
267    printf("==========Init Context==========\n");
268    OH_AI_ContextHandle context = OH_AI_ContextCreate();
269    ASSERT_NE(context, nullptr);
270
271    AddContextDeviceCPU(context);
272    printf("==========Create model==========\n");
273    OH_AI_ModelHandle model = OH_AI_ModelCreate();
274    ASSERT_NE(model, nullptr);
275    ModelPredict(model, context, "ml_face_isface", {}, true, true, false);
276}
277
278/*
279 * @tc.name: Model_GetOutputs
280 * @tc.desc: Verify the OH_AI_ModelGetOutputs/OH_AI_ModelGetInputs function.
281 * @tc.type: FUNC
282 */
283HWTEST(MSLiteTest, Model_GetOutputs, testing::ext::TestSize.Level0) {
284    printf("==========Init Context==========\n");
285    OH_AI_ContextHandle context = OH_AI_ContextCreate();
286    ASSERT_NE(context, nullptr);
287
288    AddContextDeviceCPU(context);
289    printf("==========Create model==========\n");
290    OH_AI_ModelHandle model = OH_AI_ModelCreate();
291    ASSERT_NE(model, nullptr);
292    printf("==========Model build==========\n");
293    OH_AI_ModelBuildFromFile(model, "/data/test/resource/ml_face_isface.ms", OH_AI_MODELTYPE_MINDIR, context);
294
295    printf("==========Model Predict==========\n");
296    OH_AI_TensorHandleArray inputs = OH_AI_ModelGetInputs(model);
297    OH_AI_TensorHandleArray output;
298    FillInputsData(inputs, "ml_face_isface", false);
299    OH_AI_Status ret = OH_AI_ModelPredict(model, inputs, &output, nullptr, nullptr);
300    ASSERT_EQ(ret, OH_AI_STATUS_SUCCESS);
301
302    printf("==========GetOutput==========\n");
303    OH_AI_TensorHandleArray outputs = OH_AI_ModelGetOutputs(model);
304    for (size_t i = 0; i < outputs.handle_num; ++i) {
305        OH_AI_TensorHandle tensor = outputs.handle_list[i];
306        int64_t elementNum = OH_AI_TensorGetElementNum(tensor);
307        printf("Tensor name: %s, elements num: %" PRId64 ".\n", OH_AI_TensorGetName(tensor), elementNum);
308        float *outputData = reinterpret_cast<float *>(OH_AI_TensorGetMutableData(tensor));
309        printf("output data is:");
310        constexpr int printNum = 20;
311        for (int j = 0; j < elementNum && j <= printNum; ++j) {
312            printf("%f ", outputData[j]);
313        }
314        printf("\n");
315        printf("==========compFp32WithTData==========\n");
316        std::string expectedDataFile = g_testFilesDir + "ml_face_isface" + std::to_string(i) + ".output";
317        bool result = compFp32WithTData(outputData, expectedDataFile, 0.01, 0.01, false);
318        EXPECT_EQ(result, true);
319    }
320}
321
322/*
323 * @tc.name: Model_Resize
324 * @tc.desc: Verify the OH_AI_ModelResize function.
325 * @tc.type: FUNC
326 */
327HWTEST(MSLiteTest, Model_Resize, testing::ext::TestSize.Level0) {
328    printf("==========Init Context==========\n");
329    OH_AI_ContextHandle context = OH_AI_ContextCreate();
330    ASSERT_NE(context, nullptr);
331
332    AddContextDeviceCPU(context);
333    printf("==========Create model==========\n");
334    OH_AI_ModelHandle model = OH_AI_ModelCreate();
335    ASSERT_NE(model, nullptr);
336    ModelPredict(model, context, "ml_ocr_cn", {4, {1, 32, 512, 1}}, false, true, false);
337}
338
339/*
340 * @tc.name: Model_GetInputByTensorName
341 * @tc.desc: Verify the OH_AI_ModelGetInputByTensorName function.
342 * @tc.type: FUNC
343 */
344HWTEST(MSLiteTest, Model_GetInputByTensorName, testing::ext::TestSize.Level0) {
345    printf("==========ReadFile==========\n");
346    size_t size1;
347    size_t *ptrSize1 = &size1;
348    const char *imagePath = "/data/test/resource/ml_face_isface.input";
349    char *imageBuf = ReadFile(imagePath, ptrSize1);
350    ASSERT_NE(imageBuf, nullptr);
351
352    printf("==========Init Context==========\n");
353    OH_AI_ContextHandle context = OH_AI_ContextCreate();
354    ASSERT_NE(context, nullptr);
355    AddContextDeviceCPU(context);
356    printf("==========Create model==========\n");
357    OH_AI_ModelHandle model = OH_AI_ModelCreate();
358    ASSERT_NE(model, nullptr);
359    printf("==========Build model==========\n");
360    OH_AI_Status ret = OH_AI_ModelBuildFromFile(model, "/data/test/resource/ml_face_isface.ms", OH_AI_MODELTYPE_MINDIR,
361                                                context);
362    printf("==========build model return code:%d\n", ret);
363    ASSERT_EQ(ret, OH_AI_STATUS_SUCCESS);
364
365    printf("==========GetInputs==========\n");
366    OH_AI_TensorHandle tensor = OH_AI_ModelGetInputByTensorName(model, "data");
367    ASSERT_NE(tensor, nullptr);
368    int64_t elementNum = OH_AI_TensorGetElementNum(tensor);
369    printf("Tensor name: %s, elements num: %" PRId64 ".\n", OH_AI_TensorGetName(tensor), elementNum);
370    float *inputData = reinterpret_cast<float *>(OH_AI_TensorGetMutableData(tensor));
371    ASSERT_NE(inputData, nullptr);
372    printf("==========Transpose==========\n");
373    size_t shapeNum;
374    const int64_t *shape = OH_AI_TensorGetShape(tensor, &shapeNum);
375    auto imageBufNhwc = new char[size1];
376    PackNCHWToNHWCFp32(imageBuf, imageBufNhwc, shape[0], shape[1] * shape[2], shape[3]);
377    errno_t mRet = memcpy_s(inputData, size1, imageBufNhwc, size1);
378    if (mRet != EOK) {
379        printf("memcpy_s failed, ret: %d\n", mRet);
380    }
381    printf("input data is:");
382    constexpr int printNum = 20;
383    for (int j = 0; j < elementNum && j <= printNum; ++j) {
384        printf("%f ", inputData[j]);
385    }
386    printf("\n");
387
388    printf("==========Model Predict==========\n");
389    OH_AI_TensorHandleArray inputs = OH_AI_ModelGetInputs(model);
390    ASSERT_NE(inputs.handle_list, nullptr);
391    OH_AI_TensorHandleArray outputs;
392    ret = OH_AI_ModelPredict(model, inputs, &outputs, nullptr, nullptr);
393    ASSERT_EQ(ret, OH_AI_STATUS_SUCCESS);
394    CompareResult(outputs, "ml_face_isface");
395    delete[] imageBuf;
396    OH_AI_ModelDestroy(&model);
397}
398
399/*
400 * @tc.name: Model_GetOutputByTensorName
401 * @tc.desc: Verify the OH_AI_ModelGetOutputByTensorName function.
402 * @tc.type: FUNC
403 */
404HWTEST(MSLiteTest, Model_GetOutputByTensorName, testing::ext::TestSize.Level0) {
405    printf("==========Init Context==========\n");
406    OH_AI_ContextHandle context = OH_AI_ContextCreate();
407    ASSERT_NE(context, nullptr);
408    AddContextDeviceCPU(context);
409    printf("==========Create model==========\n");
410    OH_AI_ModelHandle model = OH_AI_ModelCreate();
411    ASSERT_NE(model, nullptr);
412    printf("==========Build model==========\n");
413    OH_AI_Status ret = OH_AI_ModelBuildFromFile(model, "/data/test/resource/ml_face_isface.ms", OH_AI_MODELTYPE_MINDIR,
414                                                context);
415    printf("==========build model return code:%d\n", ret);
416    ASSERT_EQ(ret, OH_AI_STATUS_SUCCESS);
417
418    printf("==========GetInputs==========\n");
419    OH_AI_TensorHandleArray inputs = OH_AI_ModelGetInputs(model);
420    ASSERT_NE(inputs.handle_list, nullptr);
421    FillInputsData(inputs, "ml_face_isface", true);
422    printf("==========Model Predict==========\n");
423    OH_AI_TensorHandleArray outputs;
424    ret = OH_AI_ModelPredict(model, inputs, &outputs, nullptr, nullptr);
425    ASSERT_EQ(ret, OH_AI_STATUS_SUCCESS);
426
427    printf("==========GetOutput==========\n");
428    OH_AI_TensorHandle tensor = OH_AI_ModelGetOutputByTensorName(model, "prob");
429    ASSERT_NE(tensor, nullptr);
430    int64_t elementNum = OH_AI_TensorGetElementNum(tensor);
431    printf("Tensor name: %s, elements num: %" PRId64 ".\n", OH_AI_TensorGetName(tensor), elementNum);
432    float *outputData = reinterpret_cast<float *>(OH_AI_TensorGetMutableData(tensor));
433    printf("output data is:");
434    constexpr int printNum = 20;
435    for (int j = 0; j < elementNum && j <= printNum; ++j) {
436        printf("%f ", outputData[j]);
437    }
438    printf("\n");
439    printf("==========compFp32WithTData==========\n");
440    bool result = compFp32WithTData(outputData, g_testFilesDir + "ml_face_isface0.output", 0.01, 0.01, false);
441    EXPECT_EQ(result, true);
442    OH_AI_ModelDestroy(&model);
443}
444
445/*
446 * @tc.name: TrainCfg_CreateDestroy
447 * @tc.desc: Verify the OH_AI_TrainCfgCreate/OH_AI_TrainCfgDestroy function.
448 * @tc.type: FUNC
449 */
450HWTEST(MSLiteTest, TrainCfg_CreateDestroy, testing::ext::TestSize.Level0) {
451    OH_AI_TrainCfgHandle trainCfg = OH_AI_TrainCfgCreate();
452    ASSERT_NE(trainCfg, nullptr);
453
454    OH_AI_TrainCfgDestroy(&trainCfg);
455    ASSERT_EQ(trainCfg, nullptr);
456}
457
458/*
459 * @tc.name: TrainCfg_LossName
460 * @tc.desc: Verify the OH_AI_TrainCfgSetLossName/OH_AI_TrainCfgGetLossName function.
461 * @tc.type: FUNC
462 */
463HWTEST(MSLiteTest, TrainCfg_LossName, testing::ext::TestSize.Level0) {
464    OH_AI_TrainCfgHandle trainCfg = OH_AI_TrainCfgCreate();
465    ASSERT_NE(trainCfg, nullptr);
466    std::vector<std::string> set_train_cfg_loss_name = {"loss_fct"};
467    char **setLossName = TransStrVectorToCharArrays(set_train_cfg_loss_name);
468    OH_AI_TrainCfgSetLossName(trainCfg, const_cast<const char **>(setLossName), set_train_cfg_loss_name.size());
469
470    size_t getNum = 0;
471    char **getLossName = OH_AI_TrainCfgGetLossName(trainCfg, &getNum);
472    printf("trainCfg loss name: ");
473    for (size_t i = 0; i < getNum; i++) {
474        printf("%s ", getLossName[i]);
475    }
476    printf("\n");
477    ASSERT_EQ(strcmp(getLossName[0], "loss_fct"), 0);
478
479    for (size_t i = 0; i < getNum; i++) {
480        free(setLossName[i]);
481        free(getLossName[i]);
482    }
483    free(setLossName);
484    free(getLossName);
485    OH_AI_TrainCfgDestroy(&trainCfg);
486}
487
488/*
489 * @tc.name: TrainCfg_OptimizationLevel
490 * @tc.desc: Verify the OH_AI_TrainCfgSetOptimizationLevel/OH_AI_TrainCfgGetOptimizationLevel function.
491 * @tc.type: FUNC
492 */
493HWTEST(MSLiteTest, TrainCfg_OptimizationLevel, testing::ext::TestSize.Level0) {
494    OH_AI_TrainCfgHandle trainCfg = OH_AI_TrainCfgCreate();
495    ASSERT_NE(trainCfg, nullptr);
496
497    OH_AI_OptimizationLevel optim_level = OH_AI_KO2;
498    OH_AI_TrainCfgSetOptimizationLevel(trainCfg, optim_level);
499    OH_AI_OptimizationLevel get_optim_level = OH_AI_TrainCfgGetOptimizationLevel(trainCfg);
500    ASSERT_EQ(get_optim_level, OH_AI_KO2);
501
502    OH_AI_TrainCfgDestroy(&trainCfg);
503    ASSERT_EQ(trainCfg, nullptr);
504}
505
506/*
507 * @tc.name: Model_TrainModelBuild
508 * @tc.desc: Verify the OH_AI_TrainModelBuild function.
509 * @tc.type: FUNC
510 */
511HWTEST(MSLiteTest, Model_TrainModelBuild, testing::ext::TestSize.Level0) {
512    printf("==========OH_AI_ContextCreate==========\n");
513    OH_AI_ContextHandle context = OH_AI_ContextCreate();
514    ASSERT_NE(context, nullptr);
515    AddContextDeviceCPU(context);
516    printf("==========OH_AI_ModelCreate==========\n");
517    OH_AI_ModelHandle model = OH_AI_ModelCreate();
518    ASSERT_NE(model, nullptr);
519
520    printf("==========OH_AI_RunStep==========\n");
521    ModelTrain(model, context, "lenet_train", {}, true, false, false);
522    printf("==========OH_AI_ExportModel==========\n");
523    auto status = OH_AI_ExportModel(model, OH_AI_MODELTYPE_MINDIR, "/data/test/resource/lenet_train_infer.ms",
524                                    OH_AI_NO_QUANT, true, nullptr, 0);
525    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
526    OH_AI_ModelDestroy(&model);
527
528    printf("==========OH_AI_ModelCreate2==========\n");
529    context = OH_AI_ContextCreate();
530    ASSERT_NE(context, nullptr);
531    AddContextDeviceCPU(context);
532    OH_AI_ModelHandle model2 = OH_AI_ModelCreate();
533    ASSERT_NE(model2, nullptr);
534    printf("==========ModelPredict==========\n");
535    ModelPredict(model2, context, "lenet_train_infer", {}, true, false, true);
536}
537
538/*
539 * @tc.name: Model_LearningRate
540 * @tc.desc: Verify the OH_AI_ModelGetLearningRate/OH_AI_ModelSetLearningRate function.
541 * @tc.type: FUNC
542 */
543HWTEST(MSLiteTest, Model_LearningRate, testing::ext::TestSize.Level0) {
544    printf("==========OH_AI_ContextCreate==========\n");
545    OH_AI_ContextHandle context = OH_AI_ContextCreate();
546    ASSERT_NE(context, nullptr);
547    AddContextDeviceCPU(context);
548    printf("==========OH_AI_ModelCreate==========\n");
549    OH_AI_ModelHandle model = OH_AI_ModelCreate();
550    ASSERT_NE(model, nullptr);
551
552    printf("==========OH_AI_TrainCfgCreate==========\n");
553    OH_AI_TrainCfgHandle train_cfg = OH_AI_TrainCfgCreate();
554    ASSERT_NE(train_cfg, nullptr);
555    printf("==========OH_AI_TrainModelBuildFromFile==========\n");
556    auto status = OH_AI_TrainModelBuildFromFile(model, "/data/test/resource/lenet_train.ms", OH_AI_MODELTYPE_MINDIR,
557                                                context, train_cfg);
558    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
559
560    auto learing_rate = OH_AI_ModelGetLearningRate(model);
561    printf("learing_rate: %f\n", learing_rate);
562    status = OH_AI_ModelSetLearningRate(model, 0.01f);
563    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
564    learing_rate = OH_AI_ModelGetLearningRate(model);
565    printf("get_learing_rate: %f", learing_rate);
566    ASSERT_EQ(learing_rate, 0.01f);
567
568    printf("==========GetInputs==========\n");
569    OH_AI_TensorHandleArray inputs = OH_AI_ModelGetInputs(model);
570    ASSERT_NE(inputs.handle_list, nullptr);
571    FillInputsData(inputs, "lenet_train", false);
572    status = OH_AI_ModelSetTrainMode(model, true);
573    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
574    printf("==========Model RunStep==========\n");
575    status = OH_AI_RunStep(model, nullptr, nullptr);
576    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
577
578    OH_AI_ModelDestroy(&model);
579}
580
581/*
582 * @tc.name: Model_UpdateWeights
583 * @tc.desc: Verify the OH_AI_ModelUpdateWeights function.
584 * @tc.type: FUNC
585 */
586HWTEST(MSLiteTest, Model_UpdateWeights, testing::ext::TestSize.Level0) {
587    printf("==========OH_AI_ContextCreate==========\n");
588    OH_AI_ContextHandle context = OH_AI_ContextCreate();
589    ASSERT_NE(context, nullptr);
590    AddContextDeviceCPU(context);
591    printf("==========OH_AI_ModelCreate==========\n");
592    OH_AI_ModelHandle model = OH_AI_ModelCreate();
593    ASSERT_NE(model, nullptr);
594
595    printf("==========OH_AI_TrainCfgCreate==========\n");
596    OH_AI_TrainCfgHandle train_cfg = OH_AI_TrainCfgCreate();
597    ASSERT_NE(train_cfg, nullptr);
598    printf("==========OH_AI_TrainModelBuildFromFile==========\n");
599    auto status = OH_AI_TrainModelBuildFromFile(model, "/data/test/resource/lenet_train.ms", OH_AI_MODELTYPE_MINDIR,
600                                                context, train_cfg);
601    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
602
603    auto genRandomData = [](size_t size, void *data) {
604        auto generator = std::uniform_real_distribution<float>(0.0f, 1.0f);
605        std::mt19937 randomEngine;
606        size_t elementsNum = size / sizeof(float);
607        (void)std::generate_n(static_cast<float *>(data), elementsNum,
608                              [&]() { return static_cast<float>(generator(randomEngine)); });
609    };
610    std::vector<OH_AI_TensorHandle> vec_inputs;
611    constexpr size_t createShapeNum = 1;
612    int64_t createShape[createShapeNum] = {10};
613    OH_AI_TensorHandle tensor = OH_AI_TensorCreate("fc3.bias", OH_AI_DATATYPE_NUMBERTYPE_FLOAT32, createShape,
614                                                   createShapeNum, nullptr, 0);
615    ASSERT_NE(tensor, nullptr);
616    genRandomData(OH_AI_TensorGetDataSize(tensor), OH_AI_TensorGetMutableData(tensor));
617    vec_inputs.push_back(tensor);
618
619    OH_AI_TensorHandleArray update_weights = {1, vec_inputs.data()};
620    status = OH_AI_ModelUpdateWeights(model, update_weights);
621    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
622    printf("==========GetInputs==========\n");
623    OH_AI_TensorHandleArray inputs = OH_AI_ModelGetInputs(model);
624    ASSERT_NE(inputs.handle_list, nullptr);
625    FillInputsData(inputs, "lenet_train", false);
626    status = OH_AI_ModelSetTrainMode(model, true);
627    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
628    printf("==========Model RunStep==========\n");
629    status = OH_AI_RunStep(model, nullptr, nullptr);
630    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
631
632    OH_AI_ModelDestroy(&model);
633}
634
635/*
636 * @tc.name: Model_GetWeights
637 * @tc.desc: Verify the OH_AI_ModelGetWeights function.
638 * @tc.type: FUNC
639 */
640HWTEST(MSLiteTest, Model_GetWeights, testing::ext::TestSize.Level0) {
641    printf("==========OH_AI_ContextCreate==========\n");
642    OH_AI_ContextHandle context = OH_AI_ContextCreate();
643    ASSERT_NE(context, nullptr);
644    AddContextDeviceCPU(context);
645    printf("==========OH_AI_ModelCreate==========\n");
646    OH_AI_ModelHandle model = OH_AI_ModelCreate();
647    ASSERT_NE(model, nullptr);
648    printf("==========OH_AI_TrainCfgCreate==========\n");
649    OH_AI_TrainCfgHandle train_cfg = OH_AI_TrainCfgCreate();
650    ASSERT_NE(train_cfg, nullptr);
651    printf("==========OH_AI_TrainModelBuildFromFile==========\n");
652    auto status = OH_AI_TrainModelBuildFromFile(model, "/data/test/resource/lenet_train.ms", OH_AI_MODELTYPE_MINDIR,
653                                                context, train_cfg);
654    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
655    OH_AI_TensorHandleArray get_update_weights = OH_AI_ModelGetWeights(model);
656    for (size_t i = 0; i < get_update_weights.handle_num; ++i) {
657        OH_AI_TensorHandle weights_tensor = get_update_weights.handle_list[i];
658        if (strcmp(OH_AI_TensorGetName(weights_tensor), "fc3.bias") == 0) {
659            float *inputData = reinterpret_cast<float *>(OH_AI_TensorGetMutableData(weights_tensor));
660            printf("fc3.bias: %f", inputData[0]);
661        }
662    }
663    auto genRandomData = [](size_t size, void *data) {
664        auto generator = std::uniform_real_distribution<float>(0.0f, 1.0f);
665        std::mt19937 randomEngine;
666        size_t elementsNum = size / sizeof(float);
667        (void)std::generate_n(static_cast<float *>(data), elementsNum,
668                              [&]() { return static_cast<float>(generator(randomEngine)); });
669    };
670    std::vector<OH_AI_TensorHandle> vec_inputs;
671    constexpr size_t createShapeNum = 1;
672    int64_t createShape[createShapeNum] = {10};
673    OH_AI_TensorHandle tensor = OH_AI_TensorCreate("fc3.bias", OH_AI_DATATYPE_NUMBERTYPE_FLOAT32, createShape,
674                                                   createShapeNum, nullptr, 0);
675    ASSERT_NE(tensor, nullptr);
676    genRandomData(OH_AI_TensorGetDataSize(tensor), OH_AI_TensorGetMutableData(tensor));
677    vec_inputs.push_back(tensor);
678    OH_AI_TensorHandleArray update_weights = {1, vec_inputs.data()};
679    status = OH_AI_ModelUpdateWeights(model, update_weights);
680    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
681    printf("==========GetInputs==========\n");
682    OH_AI_TensorHandleArray inputs = OH_AI_ModelGetInputs(model);
683    ASSERT_NE(inputs.handle_list, nullptr);
684    FillInputsData(inputs, "lenet_train", false);
685    status = OH_AI_ModelSetTrainMode(model, true);
686    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
687    printf("==========Model RunStep==========\n");
688    status = OH_AI_RunStep(model, nullptr, nullptr);
689    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
690    printf("==========OH_AI_ExportModel==========\n");
691    status = OH_AI_ExportModel(model, OH_AI_MODELTYPE_MINDIR, "/data/test/resource/lenet_train_infer.ms",
692                               OH_AI_NO_QUANT, true, nullptr, 0);
693    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
694    OH_AI_TensorHandleArray export_update_weights = OH_AI_ModelGetWeights(model);
695    for (size_t i = 0; i < export_update_weights.handle_num; ++i) {
696        OH_AI_TensorHandle weights_tensor = export_update_weights.handle_list[i];
697        if (strcmp(OH_AI_TensorGetName(weights_tensor), "fc3.bias") == 0) {
698            float *inputData = reinterpret_cast<float *>(OH_AI_TensorGetMutableData(weights_tensor));
699            printf("fc3.bias: %f", inputData[0]);
700        }
701    }
702
703    OH_AI_ModelDestroy(&model);
704}
705
706/*
707 * @tc.name: Model_SetupVirtualBatch
708 * @tc.desc: Verify the OH_AI_ModelSetupVirtualBatch/OH_AI_ExportModelBuffer function.
709 * @tc.type: FUNC
710 */
711HWTEST(MSLiteTest, Model_SetupVirtualBatch, testing::ext::TestSize.Level0) {
712    printf("==========OH_AI_ContextCreate==========\n");
713    OH_AI_ContextHandle context = OH_AI_ContextCreate();
714    ASSERT_NE(context, nullptr);
715    AddContextDeviceCPU(context);
716    printf("==========OH_AI_ModelCreate==========\n");
717    OH_AI_ModelHandle model = OH_AI_ModelCreate();
718    ASSERT_NE(model, nullptr);
719    printf("==========OH_AI_TrainCfgCreate==========\n");
720    OH_AI_TrainCfgHandle train_cfg = OH_AI_TrainCfgCreate();
721    ASSERT_NE(train_cfg, nullptr);
722
723    printf("==========OH_AI_TrainModelBuildFromFile==========\n");
724    auto status = OH_AI_TrainModelBuildFromFile(model, "/data/test/resource/lenet_train.ms", OH_AI_MODELTYPE_MINDIR,
725                                                context, train_cfg);
726    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
727    status = OH_AI_ModelSetupVirtualBatch(model, 2, -1.0f, -1.0f);
728    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
729    printf("==========GetInputs==========\n");
730    OH_AI_TensorHandleArray inputs = OH_AI_ModelGetInputs(model);
731    ASSERT_NE(inputs.handle_list, nullptr);
732    FillInputsData(inputs, "lenet_train", false);
733    status = OH_AI_ModelSetTrainMode(model, true);
734    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
735    bool trainMode = OH_AI_ModelGetTrainMode(model);
736    printf("get train mode: %d\n", trainMode);
737    ASSERT_EQ(trainMode, true);
738
739    printf("==========Model RunStep==========\n");
740    status = OH_AI_RunStep(model, nullptr, nullptr);
741    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
742    printf("==========OH_AI_ExportModelBuffer==========\n");
743    char *modelBuffer;
744    size_t modelSize = 0;
745    status = OH_AI_ExportModelBuffer(model, OH_AI_MODELTYPE_MINDIR, &modelBuffer, &modelSize, OH_AI_NO_QUANT, true,
746                                     nullptr, 0);
747    printf("export model buffer size: %zu\n", modelSize);
748    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
749    ASSERT_NE(modelBuffer, nullptr);
750
751    OH_AI_ModelDestroy(&model);
752    free(modelBuffer);
753}
754
755/*
756 * @tc.name: Model_ExportWeights
757 * @tc.desc: Verify the OH_AI_ExportWeightsCollaborateWithMicro function.
758 * @tc.type: FUNC
759 */
760HWTEST(MSLiteTest, Model_ExportWeights, testing::ext::TestSize.Level0) {
761    printf("==========OH_AI_ContextCreate==========\n");
762    OH_AI_ContextHandle context = OH_AI_ContextCreate();
763    ASSERT_NE(context, nullptr);
764    AddContextDeviceCPU(context);
765    printf("==========OH_AI_ModelCreate==========\n");
766    OH_AI_ModelHandle model = OH_AI_ModelCreate();
767    ASSERT_NE(model, nullptr);
768    printf("==========OH_AI_TrainCfgCreate==========\n");
769    OH_AI_TrainCfgHandle train_cfg = OH_AI_TrainCfgCreate();
770    ASSERT_NE(train_cfg, nullptr);
771
772    printf("==========OH_AI_TrainModelBuildFromFile==========\n");
773    auto status = OH_AI_TrainModelBuildFromFile(model, "/data/test/resource/xiaoyi_train_codegen.ms",
774                                                OH_AI_MODELTYPE_MINDIR, context, train_cfg);
775    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
776    printf("==========OH_AI_ExportModel==========\n");
777    status = OH_AI_ExportModel(model, OH_AI_MODELTYPE_MINDIR, "/data/test/resource/xiaoyi_train_codegen_gru_model1.ms",
778                               OH_AI_NO_QUANT, true, nullptr, 0);
779    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
780    status = OH_AI_ExportWeightsCollaborateWithMicro(model, OH_AI_MODELTYPE_MINDIR,
781                                                     "/data/test/resource/xiaoyi_train_codegen_net1.bin", true, true,
782                                                     nullptr, 0);
783    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
784    status = OH_AI_ExportWeightsCollaborateWithMicro(model, OH_AI_MODELTYPE_MINDIR,
785                                                     "/data/test/resource/xiaoyi_train_codegen_net1_fp32.bin", true,
786                                                     false, nullptr, 0);
787    ASSERT_EQ(status, OH_AI_STATUS_SUCCESS);
788
789    OH_AI_ModelDestroy(&model);
790}
791
792/*
793 * @tc.name: Tensor_Create
794 * @tc.desc: Verify the OH_AI_TensorCreate function.
795 * @tc.type: FUNC
796 */
797HWTEST(MSLiteTest, Tensor_Create, testing::ext::TestSize.Level0) {
798    printf("==========Init Context==========\n");
799    OH_AI_ContextHandle context = OH_AI_ContextCreate();
800    ASSERT_NE(context, nullptr);
801    AddContextDeviceCPU(context);
802    printf("==========Create model==========\n");
803    OH_AI_ModelHandle model = OH_AI_ModelCreate();
804    ASSERT_NE(model, nullptr);
805    printf("==========Build model==========\n");
806    OH_AI_Status ret = OH_AI_ModelBuildFromFile(model, "/data/test/resource/ml_face_isface.ms", OH_AI_MODELTYPE_MINDIR,
807                                                context);
808    printf("==========build model return code:%d\n", ret);
809    ASSERT_EQ(ret, OH_AI_STATUS_SUCCESS);
810    printf("==========GetInputs==========\n");
811    constexpr size_t createShapeNum = 4;
812    int64_t createShape[createShapeNum] = {1, 48, 48, 3};
813    OH_AI_TensorHandle tensor = OH_AI_TensorCreate("data", OH_AI_DATATYPE_NUMBERTYPE_FLOAT32, createShape,
814                                                   createShapeNum, nullptr, 0);
815    ASSERT_NE(tensor, nullptr);
816    OH_AI_TensorHandleArray inputs = OH_AI_ModelGetInputs(model);
817    inputs.handle_list[0] = tensor;
818    FillInputsData(inputs, "ml_face_isface", true);
819    printf("==========Model Predict==========\n");
820    OH_AI_TensorHandleArray outputs;
821    ret = OH_AI_ModelPredict(model, inputs, &outputs, nullptr, nullptr);
822    ASSERT_EQ(ret, OH_AI_STATUS_SUCCESS);
823    CompareResult(outputs, "ml_face_isface");
824    OH_AI_ModelDestroy(&model);
825}
826
827/*
828 * @tc.name: Tensor_Destroy
829 * @tc.desc: Verify the OH_AI_TensorDestroy function.
830 * @tc.type: FUNC
831 */
832HWTEST(MSLiteTest, Tensor_Destroy, testing::ext::TestSize.Level0) {
833    printf("==========ReadFile==========\n");
834    size_t size1;
835    size_t *ptrSize1 = &size1;
836    const char *imagePath = "/data/test/resource/ml_face_isface.input";
837    char *imageBuf = ReadFile(imagePath, ptrSize1);
838    ASSERT_NE(imageBuf, nullptr);
839    printf("==========OH_AI_TensorCreate==========\n");
840    constexpr size_t createShapeNum = 4;
841    int64_t createShape[createShapeNum] = {1, 48, 48, 3};
842    OH_AI_TensorHandle tensor = OH_AI_TensorCreate("data", OH_AI_DATATYPE_NUMBERTYPE_FLOAT32, createShape,
843                                                   createShapeNum, imageBuf, size1);
844    ASSERT_NE(tensor, nullptr);
845    delete[] imageBuf;
846    OH_AI_TensorDestroy(&tensor);
847    ASSERT_EQ(tensor, nullptr);
848}
849
850/*
851 * @tc.name: Tensor_Clone
852 * @tc.desc: Verify the OH_AI_TensorClone function.
853 * @tc.type: FUNC
854 */
855HWTEST(MSLiteTest, Tensor_Clone, testing::ext::TestSize.Level0) {
856    printf("==========ReadFile==========\n");
857    size_t size1;
858    size_t *ptrSize1 = &size1;
859    const char *imagePath = "/data/test/resource/ml_face_isface.input";
860    char *imageBuf = ReadFile(imagePath, ptrSize1);
861    ASSERT_NE(imageBuf, nullptr);
862    printf("==========OH_AI_TensorCreate==========\n");
863    constexpr size_t createShapeNum = 4;
864    int64_t createShape[createShapeNum] = {1, 48, 48, 3};
865    OH_AI_TensorHandle tensor = OH_AI_TensorCreate("data", OH_AI_DATATYPE_NUMBERTYPE_FLOAT32, createShape,
866                                                   createShapeNum, imageBuf, size1);
867    ASSERT_NE(tensor, nullptr);
868    OH_AI_TensorHandle clone = OH_AI_TensorClone(tensor);
869    ASSERT_NE(clone, nullptr);
870    ASSERT_EQ(strcmp(OH_AI_TensorGetName(clone), "data_duplicate"), 0);
871    delete[] imageBuf;
872    OH_AI_TensorDestroy(&tensor);
873    OH_AI_TensorDestroy(&clone);
874}
875
876/*
877 * @tc.name: Tensor_GetName
878 * @tc.desc: Verify the OH_AI_TensorGetName function.
879 * @tc.type: FUNC
880 */
881HWTEST(MSLiteTest, Tensor_GetName, testing::ext::TestSize.Level0) {
882    printf("==========ReadFile==========\n");
883    size_t size1;
884    size_t *ptrSize1 = &size1;
885    const char *imagePath = "/data/test/resource/ml_face_isface.input";
886    char *imageBuf = ReadFile(imagePath, ptrSize1);
887    ASSERT_NE(imageBuf, nullptr);
888    printf("==========OH_AI_TensorCreate==========\n");
889    constexpr size_t createShapeNum = 4;
890    int64_t createShape[createShapeNum] = {1, 48, 48, 3};
891    OH_AI_TensorHandle tensor = OH_AI_TensorCreate("data", OH_AI_DATATYPE_NUMBERTYPE_FLOAT32, createShape,
892                                                   createShapeNum, imageBuf, size1);
893    ASSERT_NE(tensor, nullptr);
894    const char *tensorName = OH_AI_TensorGetName(tensor);
895    ASSERT_EQ(strcmp(tensorName, "data"), 0);
896    delete[] imageBuf;
897    OH_AI_TensorDestroy(&tensor);
898}
899
900/*
901 * @tc.name: Tensor_SetName
902 * @tc.desc: Verify the OH_AI_TensorSetName function.
903 * @tc.type: FUNC
904 */
905HWTEST(MSLiteTest, Tensor_SetName, testing::ext::TestSize.Level0) {
906    printf("==========ReadFile==========\n");
907    size_t size1;
908    size_t *ptrSize1 = &size1;
909    const char *imagePath = "/data/test/resource/ml_face_isface.input";
910    char *imageBuf = ReadFile(imagePath, ptrSize1);
911    ASSERT_NE(imageBuf, nullptr);
912    printf("==========OH_AI_TensorCreate==========\n");
913    constexpr size_t createShapeNum = 4;
914    int64_t createShape[createShapeNum] = {1, 48, 48, 3};
915    OH_AI_TensorHandle tensor = OH_AI_TensorCreate("data", OH_AI_DATATYPE_NUMBERTYPE_FLOAT32, createShape,
916                                                   createShapeNum, imageBuf, size1);
917    ASSERT_NE(tensor, nullptr);
918    OH_AI_TensorSetName(tensor, "new_data");
919    const char *tensorName = OH_AI_TensorGetName(tensor);
920    ASSERT_EQ(strcmp(tensorName, "new_data"), 0);
921    delete[] imageBuf;
922    OH_AI_TensorDestroy(&tensor);
923}
924
925/*
926 * @tc.name: Tensor_GetDataType
927 * @tc.desc: Verify the OH_AI_TensorGetDataType function.
928 * @tc.type: FUNC
929 */
930HWTEST(MSLiteTest, Tensor_GetDataType, testing::ext::TestSize.Level0) {
931    printf("==========ReadFile==========\n");
932    size_t size1;
933    size_t *ptrSize1 = &size1;
934    const char *imagePath = "/data/test/resource/ml_face_isface.input";
935    char *imageBuf = ReadFile(imagePath, ptrSize1);
936    ASSERT_NE(imageBuf, nullptr);
937    printf("==========OH_AI_TensorCreate==========\n");
938    constexpr size_t createShapeNum = 4;
939    int64_t createShape[createShapeNum] = {1, 48, 48, 3};
940    OH_AI_TensorHandle tensor = OH_AI_TensorCreate("data", OH_AI_DATATYPE_NUMBERTYPE_FLOAT32, createShape,
941                                                   createShapeNum, imageBuf, size1);
942    ASSERT_NE(tensor, nullptr);
943    OH_AI_DataType data_type = OH_AI_TensorGetDataType(tensor);
944    ASSERT_EQ(data_type, OH_AI_DATATYPE_NUMBERTYPE_FLOAT32);
945    delete[] imageBuf;
946    OH_AI_TensorDestroy(&tensor);
947}
948
949/*
950 * @tc.name: Tensor_SetDataType
951 * @tc.desc: Verify the OH_AI_TensorSetDataType function.
952 * @tc.type: FUNC
953 */
954HWTEST(MSLiteTest, Tensor_SetDataType, testing::ext::TestSize.Level0) {
955    printf("==========ReadFile==========\n");
956    size_t size1;
957    size_t *ptrSize1 = &size1;
958    const char *imagePath = "/data/test/resource/ml_face_isface.input";
959    char *imageBuf = ReadFile(imagePath, ptrSize1);
960    ASSERT_NE(imageBuf, nullptr);
961    printf("==========OH_AI_TensorCreate==========\n");
962    constexpr size_t createShapeNum = 4;
963    int64_t createShape[createShapeNum] = {1, 48, 48, 3};
964    OH_AI_TensorHandle tensor = OH_AI_TensorCreate("data", OH_AI_DATATYPE_NUMBERTYPE_FLOAT32, createShape,
965                                                   createShapeNum, imageBuf, size1);
966    ASSERT_NE(tensor, nullptr);
967    OH_AI_TensorSetDataType(tensor, OH_AI_DATATYPE_NUMBERTYPE_FLOAT16);
968    OH_AI_DataType data_type = OH_AI_TensorGetDataType(tensor);
969    ASSERT_EQ(data_type, OH_AI_DATATYPE_NUMBERTYPE_FLOAT16);
970    delete[] imageBuf;
971    OH_AI_TensorDestroy(&tensor);
972}
973
974/*
975 * @tc.name: Tensor_GetShape
976 * @tc.desc: Verify the OH_AI_TensorGetShape function.
977 * @tc.type: FUNC
978 */
979HWTEST(MSLiteTest, Tensor_GetShape, testing::ext::TestSize.Level0) {
980    printf("==========ReadFile==========\n");
981    size_t size1;
982    size_t *ptrSize1 = &size1;
983    const char *imagePath = "/data/test/resource/ml_face_isface.input";
984    char *imageBuf = ReadFile(imagePath, ptrSize1);
985    ASSERT_NE(imageBuf, nullptr);
986    printf("==========OH_AI_TensorCreate==========\n");
987    constexpr size_t createShapeNum = 4;
988    int64_t createShape[createShapeNum] = {1, 48, 48, 3};
989    OH_AI_TensorHandle tensor = OH_AI_TensorCreate("data", OH_AI_DATATYPE_NUMBERTYPE_FLOAT32, createShape,
990                                                   createShapeNum, imageBuf, size1);
991    ASSERT_NE(tensor, nullptr);
992    size_t retShapeNum;
993    const int64_t *retShape = OH_AI_TensorGetShape(tensor, &retShapeNum);
994    ASSERT_EQ(retShapeNum, createShapeNum);
995    for (size_t i = 0; i < retShapeNum; i++) {
996        ASSERT_EQ(retShape[i], createShape[i]);
997    }
998    delete[] imageBuf;
999    OH_AI_TensorDestroy(&tensor);
1000}
1001
1002/*
1003 * @tc.name: Tensor_SetShape
1004 * @tc.desc: Verify the OH_AI_TensorSetShape function.
1005 * @tc.type: FUNC
1006 */
1007HWTEST(MSLiteTest, Tensor_SetShape, testing::ext::TestSize.Level0) {
1008    printf("==========ReadFile==========\n");
1009    size_t size1;
1010    size_t *ptrSize1 = &size1;
1011    const char *imagePath = "/data/test/resource/ml_face_isface.input";
1012    char *imageBuf = ReadFile(imagePath, ptrSize1);
1013    ASSERT_NE(imageBuf, nullptr);
1014    printf("==========OH_AI_TensorCreate==========\n");
1015    constexpr size_t createShapeNum = 4;
1016    int64_t createShape[createShapeNum] = {1, 48, 48, 3};
1017    OH_AI_TensorHandle tensor = OH_AI_TensorCreate("data", OH_AI_DATATYPE_NUMBERTYPE_FLOAT32, createShape,
1018                                                   createShapeNum, imageBuf, size1);
1019    ASSERT_NE(tensor, nullptr);
1020    size_t retShapeNum;
1021    const int64_t *retShape = OH_AI_TensorGetShape(tensor, &retShapeNum);
1022    ASSERT_EQ(retShapeNum, createShapeNum);
1023    for (size_t i = 0; i < retShapeNum; i++) {
1024        ASSERT_EQ(retShape[i], createShape[i]);
1025    }
1026    constexpr size_t newShapeNum = 4;
1027    int64_t newShape[newShapeNum] = {1, 32, 32, 1};
1028    OH_AI_TensorSetShape(tensor, newShape, newShapeNum);
1029    size_t newRetShapeNum;
1030    const int64_t *newRetShape = OH_AI_TensorGetShape(tensor, &newRetShapeNum);
1031    ASSERT_EQ(newRetShapeNum, newShapeNum);
1032    for (size_t i = 0; i < newRetShapeNum; i++) {
1033        ASSERT_EQ(newRetShape[i], newShape[i]);
1034    }
1035    delete[] imageBuf;
1036    OH_AI_TensorDestroy(&tensor);
1037}
1038
1039/*
1040 * @tc.name: Tensor_GetFormat
1041 * @tc.desc: Verify the OH_AI_TensorGetFormat function.
1042 * @tc.type: FUNC
1043 */
1044HWTEST(MSLiteTest, Tensor_GetFormat, testing::ext::TestSize.Level0) {
1045    printf("==========ReadFile==========\n");
1046    size_t size1;
1047    size_t *ptrSize1 = &size1;
1048    const char *imagePath = "/data/test/resource/ml_face_isface.input";
1049    char *imageBuf = ReadFile(imagePath, ptrSize1);
1050    ASSERT_NE(imageBuf, nullptr);
1051    printf("==========OH_AI_TensorCreate==========\n");
1052    constexpr size_t createShapeNum = 4;
1053    int64_t createShape[createShapeNum] = {1, 48, 48, 3};
1054    OH_AI_TensorHandle tensor = OH_AI_TensorCreate("data", OH_AI_DATATYPE_NUMBERTYPE_FLOAT32, createShape,
1055                                                   createShapeNum, imageBuf, size1);
1056    ASSERT_NE(tensor, nullptr);
1057    OH_AI_Format data_format = OH_AI_TensorGetFormat(tensor);
1058    ASSERT_EQ(data_format, OH_AI_FORMAT_NHWC);
1059    delete[] imageBuf;
1060    OH_AI_TensorDestroy(&tensor);
1061}
1062
1063/*
1064 * @tc.name: Tensor_SetFormat
1065 * @tc.desc: Verify the OH_AI_TensorSetFormat function.
1066 * @tc.type: FUNC
1067 */
1068HWTEST(MSLiteTest, Tensor_SetFormat, testing::ext::TestSize.Level0) {
1069    printf("==========ReadFile==========\n");
1070    size_t size1;
1071    size_t *ptrSize1 = &size1;
1072    const char *imagePath = "/data/test/resource/ml_face_isface.input";
1073    char *imageBuf = ReadFile(imagePath, ptrSize1);
1074    ASSERT_NE(imageBuf, nullptr);
1075    printf("==========OH_AI_TensorCreate==========\n");
1076    constexpr size_t createShapeNum = 4;
1077    int64_t createShape[createShapeNum] = {1, 48, 48, 3};
1078    OH_AI_TensorHandle tensor = OH_AI_TensorCreate("data", OH_AI_DATATYPE_NUMBERTYPE_FLOAT32, createShape,
1079                                                   createShapeNum, imageBuf, size1);
1080    ASSERT_NE(tensor, nullptr);
1081    OH_AI_TensorSetFormat(tensor, OH_AI_FORMAT_NCHW);
1082    OH_AI_Format data_format = OH_AI_TensorGetFormat(tensor);
1083    ASSERT_EQ(data_format, OH_AI_FORMAT_NCHW);
1084    delete[] imageBuf;
1085    OH_AI_TensorDestroy(&tensor);
1086}
1087
1088/*
1089 * @tc.name: Tensor_GetData
1090 * @tc.desc: Verify the OH_AI_TensorGetData function.
1091 * @tc.type: FUNC
1092 */
1093HWTEST(MSLiteTest, Tensor_GetData, testing::ext::TestSize.Level0) {
1094    printf("==========ReadFile==========\n");
1095    size_t size1;
1096    size_t *ptrSize1 = &size1;
1097    const char *imagePath = "/data/test/resource/ml_face_isface.input";
1098    char *imageBuf = ReadFile(imagePath, ptrSize1);
1099    ASSERT_NE(imageBuf, nullptr);
1100    printf("==========OH_AI_TensorCreate==========\n");
1101    constexpr size_t createShapeNum = 4;
1102    int64_t createShape[createShapeNum] = {1, 48, 48, 3};
1103    OH_AI_TensorHandle tensor = OH_AI_TensorCreate("data", OH_AI_DATATYPE_NUMBERTYPE_FLOAT32, createShape,
1104                                                   createShapeNum, imageBuf, size1);
1105    ASSERT_NE(tensor, nullptr);
1106    const float *retData = static_cast<const float *>(OH_AI_TensorGetData(tensor));
1107    ASSERT_NE(retData, nullptr);
1108    printf("return data is: ");
1109    constexpr int printNum = 20;
1110    for (int i = 0; i < printNum; ++i) {
1111        printf("%f ", retData[i]);
1112    }
1113    printf("\n");
1114    delete[] imageBuf;
1115    OH_AI_TensorDestroy(&tensor);
1116}
1117
1118/*
1119 * @tc.name: Tensor_SetData
1120 * @tc.desc: Verify the OH_AI_TensorSetData function.
1121 * @tc.type: FUNC
1122 */
1123HWTEST(MSLiteTest, Tensor_SetData, testing::ext::TestSize.Level0) {
1124    printf("==========ReadFile==========\n");
1125    size_t size1;
1126    size_t *ptrSize1 = &size1;
1127    const char *imagePath = "/data/test/resource/ml_face_isface.input";
1128    char *imageBuf = ReadFile(imagePath, ptrSize1);
1129    ASSERT_NE(imageBuf, nullptr);
1130    printf("==========OH_AI_TensorCreate==========\n");
1131    constexpr size_t createShapeNum = 4;
1132    int64_t createShape[createShapeNum] = {1, 48, 48, 3};
1133    OH_AI_TensorHandle tensor = OH_AI_TensorCreate("data", OH_AI_DATATYPE_NUMBERTYPE_FLOAT32, createShape,
1134                                                   createShapeNum, imageBuf, size1);
1135    ASSERT_NE(tensor, nullptr);
1136    constexpr size_t dataLen = 6;
1137    float data[dataLen] = {1, 2, 3, 4, 5, 6};
1138    OH_AI_TensorSetData(tensor, data);
1139    const float *retData = static_cast<const float *>(OH_AI_TensorGetData(tensor));
1140    ASSERT_NE(retData, nullptr);
1141    printf("return data is:");
1142    for (size_t i = 0; i < dataLen; i++) {
1143        ASSERT_EQ(retData[i], data[i]);
1144        printf("%f ", retData[i]);
1145    }
1146    printf("\n");
1147    delete[] imageBuf;
1148    OH_AI_TensorDestroy(&tensor);
1149}
1150
1151/*
1152 * @tc.name: Tensor_SetUserData
1153 * @tc.desc: Verify the OH_AI_TensorSetData function.
1154 * @tc.type: FUNC
1155 */
1156HWTEST(MSLiteTest, Tensor_SetUserData, testing::ext::TestSize.Level0) {
1157    printf("==========ReadFile==========\n");
1158    size_t size1;
1159    size_t *ptrSize1 = &size1;
1160    const char *imagePath = "/data/test/resource/ml_face_isface.input";
1161    char *imageBuf = ReadFile(imagePath, ptrSize1);
1162    ASSERT_NE(imageBuf, nullptr);
1163    printf("==========OH_AI_TensorCreate==========\n");
1164    constexpr size_t createShapeNum = 4;
1165    int64_t createShape[createShapeNum] = {1, 48, 48, 3};
1166    OH_AI_TensorHandle tensor = OH_AI_TensorCreate("data", OH_AI_DATATYPE_NUMBERTYPE_FLOAT32, createShape,
1167                                                   createShapeNum, imageBuf, size1);
1168    ASSERT_NE(tensor, nullptr);
1169    float *inputData = reinterpret_cast<float *>(OH_AI_TensorGetMutableData(tensor));
1170    constexpr size_t dataLen = 6;
1171    float data[dataLen] = {1, 2, 3, 4, 5, 6};
1172    for (size_t i = 0; i < dataLen; i++) {
1173        inputData[i] = data[i];
1174    }
1175
1176    OH_AI_TensorSetUserData(tensor, inputData, size1);
1177    const float *retData = static_cast<const float *>(OH_AI_TensorGetData(tensor));
1178    ASSERT_NE(retData, nullptr);
1179    printf("return data is:");
1180    for (size_t i = 0; i < dataLen; i++) {
1181        ASSERT_EQ(retData[i], data[i]);
1182        printf("%f ", retData[i]);
1183    }
1184    printf("\n");
1185    delete[] imageBuf;
1186    OH_AI_TensorDestroy(&tensor);
1187}
1188
1189/*
1190 * @tc.name: Tensor_GetElementNum
1191 * @tc.desc: Verify the OH_AI_TensorGetElementNum function.
1192 * @tc.type: FUNC
1193 */
1194HWTEST(MSLiteTest, Tensor_GetElementNum, testing::ext::TestSize.Level0) {
1195    printf("==========ReadFile==========\n");
1196    size_t size1;
1197    size_t *ptrSize1 = &size1;
1198    const char *imagePath = "/data/test/resource/ml_face_isface.input";
1199    char *imageBuf = ReadFile(imagePath, ptrSize1);
1200    ASSERT_NE(imageBuf, nullptr);
1201    printf("==========OH_AI_TensorCreate==========\n");
1202    constexpr size_t createShapeNum = 4;
1203    int64_t createShape[createShapeNum] = {1, 48, 48, 3};
1204    OH_AI_TensorHandle tensor = OH_AI_TensorCreate("data", OH_AI_DATATYPE_NUMBERTYPE_FLOAT32, createShape,
1205                                                   createShapeNum, imageBuf, size1);
1206    ASSERT_NE(tensor, nullptr);
1207    int64_t elementNum = OH_AI_TensorGetElementNum(tensor);
1208    printf("Tensor name: %s, elements num: %" PRId64 ".\n", OH_AI_TensorGetName(tensor), elementNum);
1209    ASSERT_EQ(elementNum, 6912);
1210    delete[] imageBuf;
1211    OH_AI_TensorDestroy(&tensor);
1212}
1213
1214/*
1215 * @tc.name: Tensor_GetDataSize
1216 * @tc.desc: Verify the OH_AI_TensorGetDataSize function.
1217 * @tc.type: FUNC
1218 */
1219HWTEST(MSLiteTest, Tensor_GetDataSize, testing::ext::TestSize.Level0) {
1220    printf("==========ReadFile==========\n");
1221    size_t size1;
1222    size_t *ptrSize1 = &size1;
1223    const char *imagePath = "/data/test/resource/ml_face_isface.input";
1224    char *imageBuf = ReadFile(imagePath, ptrSize1);
1225    ASSERT_NE(imageBuf, nullptr);
1226    printf("==========OH_AI_TensorCreate==========\n");
1227    constexpr size_t createShapeNum = 4;
1228    int64_t createShape[createShapeNum] = {1, 48, 48, 3};
1229    OH_AI_TensorHandle tensor = OH_AI_TensorCreate("data", OH_AI_DATATYPE_NUMBERTYPE_FLOAT32, createShape,
1230                                                   createShapeNum, imageBuf, size1);
1231    ASSERT_NE(tensor, nullptr);
1232    size_t dataSize = OH_AI_TensorGetDataSize(tensor);
1233    printf("Tensor data size: %zu.\n", dataSize);
1234    ASSERT_EQ(dataSize, 6912 * sizeof(float));
1235    delete[] imageBuf;
1236    OH_AI_TensorDestroy(&tensor);
1237}
1238
1239/*
1240 * @tc.name: Tensor_GetMutableData
1241 * @tc.desc: Verify the OH_AI_TensorGetMutableData function.
1242 * @tc.type: FUNC
1243 */
1244HWTEST(MSLiteTest, Tensor_GetMutableData, testing::ext::TestSize.Level0) {
1245    printf("==========ReadFile==========\n");
1246    size_t size1;
1247    size_t *ptrSize1 = &size1;
1248    const char *imagePath = "/data/test/resource/ml_face_isface.input";
1249    char *imageBuf = ReadFile(imagePath, ptrSize1);
1250    ASSERT_NE(imageBuf, nullptr);
1251    printf("==========OH_AI_TensorCreate==========\n");
1252    constexpr size_t createShapeNum = 4;
1253    int64_t createShape[createShapeNum] = {1, 48, 48, 3};
1254    OH_AI_TensorHandle tensor = OH_AI_TensorCreate("data", OH_AI_DATATYPE_NUMBERTYPE_FLOAT32, createShape,
1255                                                   createShapeNum, imageBuf, size1);
1256    ASSERT_NE(tensor, nullptr);
1257    float *inputData = reinterpret_cast<float *>(OH_AI_TensorGetMutableData(tensor));
1258    ASSERT_NE(inputData, nullptr);
1259    delete[] imageBuf;
1260    OH_AI_TensorDestroy(&tensor);
1261}
1262