From cbb8daa4018e2dbbe78bcfb1587d487bf40db81b Mon Sep 17 00:00:00 2001 From: Zhu Guodong Date: Fri, 12 May 2023 14:12:06 +0800 Subject: [PATCH] add more NNRT device info tests and remove one unspported uint8 feature test Change-Id: If8c561b0cd20f4713e630be0fe72a5a9131ab5cc Signed-off-by: Zhu Guodong --- ai/mindspore/src/ohos_c_api_test_mslite.cpp | 145 +++++++++++++------- 1 file changed, 99 insertions(+), 46 deletions(-) diff --git a/ai/mindspore/src/ohos_c_api_test_mslite.cpp b/ai/mindspore/src/ohos_c_api_test_mslite.cpp index 79487694e..93b459cef 100644 --- a/ai/mindspore/src/ohos_c_api_test_mslite.cpp +++ b/ai/mindspore/src/ohos_c_api_test_mslite.cpp @@ -62,30 +62,84 @@ void AddContextDeviceCPU(OH_AI_ContextHandle context) { // add nnrt device info void AddContextDeviceNNRT(OH_AI_ContextHandle context) { + size_t num = 0; + auto desc = OH_AI_NNRTGetAllDeviceDescs(&num); + if (desc == nullptr) { + return; + } + + std::cout << "found " << num << " nnrt devices" << std::endl; + auto id = OH_AI_NNRTDeviceDescGetId(desc); + auto name = OH_AI_NNRTDeviceDescGetName(desc); + auto type = OH_AI_NNRTDeviceDescGetType(desc); + std::cout << "NNRT device: id = " << id << ", name: " << name << ", type:" << type << std::endl; + OH_AI_DeviceInfoHandle nnrt_device_info = OH_AI_DeviceInfoCreate(OH_AI_DEVICETYPE_NNRT); ASSERT_NE(nnrt_device_info, nullptr); + OH_AI_DeviceInfoSetDeviceId(nnrt_device_info, id); + OH_AI_NNRTDestroyAllDeviceDescs(&desc); + + OH_AI_DeviceType device_type = OH_AI_DeviceInfoGetDeviceType(nnrt_device_info); + printf("==========device_type:%d\n", device_type); + ASSERT_EQ(device_type, OH_AI_DEVICETYPE_NNRT); + OH_AI_DeviceInfoSetPerformanceMode(nnrt_device_info, OH_AI_PERFORMANCE_MEDIUM); + ASSERT_EQ(OH_AI_DeviceInfoGetPerformanceMode(nnrt_device_info), OH_AI_PERFORMANCE_MEDIUM); + OH_AI_DeviceInfoSetPriority(nnrt_device_info, OH_AI_PRIORITY_MEDIUM); + ASSERT_EQ(OH_AI_DeviceInfoGetPriority(nnrt_device_info), OH_AI_PRIORITY_MEDIUM); + + OH_AI_ContextAddDeviceInfo(context, nnrt_device_info); +} + +// add nnrt device info by type +void AddContextDeviceNNRTByType(OH_AI_ContextHandle context) { size_t num = 0; auto desc = OH_AI_NNRTGetAllDeviceDescs(&num); if (desc == nullptr) { return; } - std::cout << "found " << num << " nnrt devices" << std::endl; - for (size_t i = 0; i < num; i++) { - std::cout << "nnrt device " << i << ": {device_id: " << desc[i].device_id - << ", type:" << desc[i].device_type << ", name:" << desc[i].device_name << std::endl; - } + auto id = OH_AI_NNRTDeviceDescGetId(desc); + auto name = OH_AI_NNRTDeviceDescGetName(desc); + auto type = OH_AI_NNRTDeviceDescGetType(desc); + std::cout << "NNRT device: id = " << id << ", name: " << name << ", type:" << type << std::endl; - OH_AI_DeviceInfoSetDeviceId(nnrt_device_info, desc[0].device_id); + auto nnrt_device_info = OH_AI_CreateNNRTDeviceInfoByType(type); OH_AI_NNRTDestroyAllDeviceDescs(&desc); + ASSERT_NE(nnrt_device_info, nullptr); OH_AI_DeviceType device_type = OH_AI_DeviceInfoGetDeviceType(nnrt_device_info); printf("==========device_type:%d\n", device_type); ASSERT_EQ(device_type, OH_AI_DEVICETYPE_NNRT); + ASSERT_EQ(OH_AI_DeviceInfoGetDeviceId(nnrt_device_info), id); + OH_AI_ContextAddDeviceInfo(context, nnrt_device_info); } +// add nnrt device info by name +void AddContextDeviceNNRTByName(OH_AI_ContextHandle context) { + size_t num = 0; + auto desc = OH_AI_NNRTGetAllDeviceDescs(&num); + if (desc == nullptr) { + return; + } + std::cout << "found " << num << " nnrt devices" << std::endl; + auto id = OH_AI_NNRTDeviceDescGetId(desc); + auto name = OH_AI_NNRTDeviceDescGetName(desc); + auto type = OH_AI_NNRTDeviceDescGetType(desc); + std::cout << "NNRT device: id = " << id << ", name: " << name << ", type:" << type << std::endl; + + auto nnrt_device_info = OH_AI_CreateNNRTDeviceInfoByName(name); + OH_AI_NNRTDestroyAllDeviceDescs(&desc); + ASSERT_NE(nnrt_device_info, nullptr); + + OH_AI_DeviceType device_type = OH_AI_DeviceInfoGetDeviceType(nnrt_device_info); + printf("==========device_type:%d\n", device_type); + ASSERT_EQ(device_type, OH_AI_DEVICETYPE_NNRT); + ASSERT_EQ(OH_AI_DeviceInfoGetDeviceId(nnrt_device_info), id); + + OH_AI_ContextAddDeviceInfo(context, nnrt_device_info); +} // fill data to inputs tensor void FillInputsData(OH_AI_TensorHandleArray inputs, string model_name, bool is_transpose) { @@ -1559,7 +1613,7 @@ HWTEST(MSLiteTest, OHOS_Compatible_0001, Function | MediumTest | Level1) { OH_AI_ModelDestroy(&model); } -// delegate异构 +// delegate异构:选取第一个NNRT设备 HWTEST(MSLiteTest, OHOS_NNRT_0001, Function | MediumTest | Level1) { printf("==========Init Context==========\n"); OH_AI_ContextHandle context = OH_AI_ContextCreate(); @@ -1586,41 +1640,62 @@ HWTEST(MSLiteTest, OHOS_NNRT_0001, Function | MediumTest | Level1) { OH_AI_ModelDestroy(&model); } -// delegate异构:多输入单输出 +// delegate异构:根据类型确定NNRT设备 HWTEST(MSLiteTest, OHOS_NNRT_0002, Function | MediumTest | Level1) { printf("==========Init Context==========\n"); OH_AI_ContextHandle context = OH_AI_ContextCreate(); ASSERT_NE(context, nullptr); - AddContextDeviceNNRT(context); + AddContextDeviceNNRTByType(context); AddContextDeviceCPU(context); printf("==========Create model==========\n"); OH_AI_ModelHandle model = OH_AI_ModelCreate(); ASSERT_NE(model, nullptr); printf("==========Build model==========\n"); - OH_AI_Status ret = OH_AI_ModelBuildFromFile(model, "/data/test/ml_headpose_pb2tflite.ms", + OH_AI_Status ret = OH_AI_ModelBuildFromFile(model, "/data/test/ml_face_isface.ms", OH_AI_MODELTYPE_MINDIR, context); printf("==========build model return code:%d\n", ret); ASSERT_EQ(ret, OH_AI_STATUS_SUCCESS); printf("==========GetInputs==========\n"); OH_AI_TensorHandleArray inputs = OH_AI_ModelGetInputs(model); ASSERT_NE(inputs.handle_list, nullptr); - FillInputsData(inputs, "ml_headpose_pb2tflite", false); + FillInputsData(inputs, "ml_face_isface", true); printf("==========Model Predict==========\n"); OH_AI_TensorHandleArray outputs; ret = OH_AI_ModelPredict(model, inputs, &outputs, nullptr, nullptr); ASSERT_EQ(ret, OH_AI_STATUS_SUCCESS); - CompareResult(outputs, "ml_headpose_pb2tflite", 0.02, 0.02); + CompareResult(outputs, "ml_face_isface"); OH_AI_ModelDestroy(&model); } -// delegate异构:输入为uint8模型 +// delegate异构:根据名称确定NNRT设备 HWTEST(MSLiteTest, OHOS_NNRT_0003, Function | MediumTest | Level1) { - printf("==========ReadFile==========\n"); - size_t size1; - size_t *ptr_size1 = &size1; - const char *imagePath = "/data/test/aiy_vision_classifier_plants_V1_3.input"; - char *imageBuf = ReadFile(imagePath, ptr_size1); - ASSERT_NE(imageBuf, nullptr); + printf("==========Init Context==========\n"); + OH_AI_ContextHandle context = OH_AI_ContextCreate(); + ASSERT_NE(context, nullptr); + AddContextDeviceNNRTByName(context); + AddContextDeviceCPU(context); + printf("==========Create model==========\n"); + OH_AI_ModelHandle model = OH_AI_ModelCreate(); + ASSERT_NE(model, nullptr); + printf("==========Build model==========\n"); + OH_AI_Status ret = OH_AI_ModelBuildFromFile(model, "/data/test/ml_face_isface.ms", + OH_AI_MODELTYPE_MINDIR, context); + printf("==========build model return code:%d\n", ret); + ASSERT_EQ(ret, OH_AI_STATUS_SUCCESS); + printf("==========GetInputs==========\n"); + OH_AI_TensorHandleArray inputs = OH_AI_ModelGetInputs(model); + ASSERT_NE(inputs.handle_list, nullptr); + FillInputsData(inputs, "ml_face_isface", true); + printf("==========Model Predict==========\n"); + OH_AI_TensorHandleArray outputs; + ret = OH_AI_ModelPredict(model, inputs, &outputs, nullptr, nullptr); + ASSERT_EQ(ret, OH_AI_STATUS_SUCCESS); + CompareResult(outputs, "ml_face_isface"); + OH_AI_ModelDestroy(&model); +} + +// delegate异构:多输入单输出 +HWTEST(MSLiteTest, OHOS_NNRT_0004, Function | MediumTest | Level1) { printf("==========Init Context==========\n"); OH_AI_ContextHandle context = OH_AI_ContextCreate(); ASSERT_NE(context, nullptr); @@ -1630,41 +1705,19 @@ HWTEST(MSLiteTest, OHOS_NNRT_0003, Function | MediumTest | Level1) { OH_AI_ModelHandle model = OH_AI_ModelCreate(); ASSERT_NE(model, nullptr); printf("==========Build model==========\n"); - OH_AI_Status ret = OH_AI_ModelBuildFromFile(model, "/data/test/aiy_vision_classifier_plants_V1_3.ms", OH_AI_MODELTYPE_MINDIR, - context); + OH_AI_Status ret = OH_AI_ModelBuildFromFile(model, "/data/test/ml_headpose_pb2tflite.ms", + OH_AI_MODELTYPE_MINDIR, context); printf("==========build model return code:%d\n", ret); ASSERT_EQ(ret, OH_AI_STATUS_SUCCESS); printf("==========GetInputs==========\n"); OH_AI_TensorHandleArray inputs = OH_AI_ModelGetInputs(model); ASSERT_NE(inputs.handle_list, nullptr); - for (size_t i = 0; i < inputs.handle_num; ++i) { - OH_AI_TensorHandle tensor = inputs.handle_list[i]; - int64_t element_num = OH_AI_TensorGetElementNum(tensor); - printf("Tensor name: %s, elements num: %" PRId64 ".\n", OH_AI_TensorGetName(tensor), element_num); - void *input_data = OH_AI_TensorGetMutableData(inputs.handle_list[i]); - ASSERT_NE(input_data, nullptr); - memcpy_s(input_data, size1, imageBuf, size1); - } + FillInputsData(inputs, "ml_headpose_pb2tflite", false); printf("==========Model Predict==========\n"); OH_AI_TensorHandleArray outputs; ret = OH_AI_ModelPredict(model, inputs, &outputs, nullptr, nullptr); ASSERT_EQ(ret, OH_AI_STATUS_SUCCESS); - printf("==========GetOutput==========\n"); - for (size_t i = 0; i < outputs.handle_num; ++i) { - OH_AI_TensorHandle tensor = outputs.handle_list[i]; - int64_t element_num = OH_AI_TensorGetElementNum(tensor); - printf("Tensor name: %s, elements num: %" PRId64 ".\n", OH_AI_TensorGetName(tensor), element_num); - uint8_t *output_data = reinterpret_cast(OH_AI_TensorGetMutableData(tensor)); - printf("output data is:"); - for (int j = 0; j < element_num && j <= 20; ++j) { - printf("%d ", output_data[j]); - } - printf("\n"); - printf("==========compFp32WithTData==========\n"); - string expectedDataFile = "/data/test/aiy_vision_classifier_plants_V1_3" + std::to_string(i) + ".output"; - bool result = compUint8WithTData(output_data, expectedDataFile, 0.01, 0.01, false); - EXPECT_EQ(result, true); - } - delete[] imageBuf; + CompareResult(outputs, "ml_headpose_pb2tflite", 0.02, 0.02); OH_AI_ModelDestroy(&model); } + -- GitLab