From a88010b8286a1f50e29374f4bb27bf8ffe223001 Mon Sep 17 00:00:00 2001 From: principal87 Date: Fri, 11 Aug 2023 15:34:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=A6=86=E7=9B=96=E7=94=A8?= =?UTF-8?q?=E4=BE=8B=EF=BC=8C=E4=BF=AE=E5=A4=8DNNRT=E7=A6=BB=E7=BA=BF?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: principal87 --- ai/mindspore/mindsporectest/Test.json | 3 +- .../src/ohos_c_api_test_mslite.cpp | 62 ++++++++++++++++++- .../mindsporectest/src/ohos_common.cpp | 16 +++-- 3 files changed, 75 insertions(+), 6 deletions(-) diff --git a/ai/mindspore/mindsporectest/Test.json b/ai/mindspore/mindsporectest/Test.json index 3ef532f71..9e5287d4a 100644 --- a/ai/mindspore/mindsporectest/Test.json +++ b/ai/mindspore/mindsporectest/Test.json @@ -41,7 +41,8 @@ "resource/ai/mindspore/ml_headpose_pb2tflite/ml_headpose_pb2tflite_0.input -> /data/test", "resource/ai/mindspore/ml_headpose_pb2tflite/ml_headpose_pb2tflite_1.input -> /data/test", "resource/ai/mindspore/ml_headpose_pb2tflite/ml_headpose_pb2tflite_2.input -> /data/test", - "resource/ai/mindspore/ml_headpose_pb2tflite/ml_headpose_pb2tflite0.output -> /data/test" + "resource/ai/mindspore/ml_headpose_pb2tflite/ml_headpose_pb2tflite0.output -> /data/test", + "resource/ai/mindspore/Kirin_model/tinynet.om.ms -> /data/test" ] }, { diff --git a/ai/mindspore/mindsporectest/src/ohos_c_api_test_mslite.cpp b/ai/mindspore/mindsporectest/src/ohos_c_api_test_mslite.cpp index 305db7639..09eab4cc3 100644 --- a/ai/mindspore/mindsporectest/src/ohos_c_api_test_mslite.cpp +++ b/ai/mindspore/mindsporectest/src/ohos_c_api_test_mslite.cpp @@ -14,6 +14,7 @@ */ #include +#include #include #include #include "ohos_common.h" @@ -66,11 +67,28 @@ bool IsNNRTAvailable() { if (desc == nullptr) { return false; } - + auto type = OH_AI_GetTypeFromNNRTDeviceDesc(desc); + if (type != 1) { + return false; + } OH_AI_DestroyAllNNRTDeviceDescs(&desc); return true; } +bool IsNPU() { + size_t num = 0; + auto desc = OH_AI_GetAllNNRTDeviceDescs(&num); + if (desc == nullptr) { + return false; + } + auto name = OH_AI_GetNameFromNNRTDeviceDesc(desc); + const std::string npu_name_prefix = "NPU_"; + if (strncmp(npu_name_prefix.c_str(), name, npu_name_prefix.size()) != 0) { + return false; + } + return true; +} + // add nnrt device info void AddContextDeviceNNRT(OH_AI_ContextHandle context) { size_t num = 0; @@ -178,6 +196,7 @@ void FillInputsData(OH_AI_TensorHandleArray inputs, string model_name, bool is_t auto imageBuf_nhwc = new char[size1]; PackNCHWToNHWCFp32(imageBuf, imageBuf_nhwc, shape[0], shape[1] * shape[2], shape[3]); memcpy_s(input_data, size1, imageBuf_nhwc, size1); + delete[] imageBuf_nhwc; } else { memcpy_s(input_data, size1, imageBuf, size1); } @@ -223,6 +242,7 @@ void ModelPredict(OH_AI_ModelHandle model, OH_AI_ContextHandle context, string m char *graphBuf = ReadFile(graphPath, ptr_size); ASSERT_NE(graphBuf, nullptr); ret = OH_AI_ModelBuild(model, graphBuf, size, OH_AI_MODELTYPE_MINDIR, context); + delete[] graphBuf; } else { printf("==========Build model==========\n"); ret = OH_AI_ModelBuildFromFile(model, graphPath, OH_AI_MODELTYPE_MINDIR, context); @@ -1933,6 +1953,46 @@ HWTEST(MSLiteTest, OHOS_OfflineModel_0008, Function | MediumTest | Level1) { OH_AI_ModelDestroy(&model); } +// 正常场景:离线模型覆盖NPU +HWTEST(MSLiteTest, OHOS_OfflineModel_0009, Function | MediumTest | Level1) { + if (!IsNPU()) { + printf("NNRt is not NPU, skip this test"); + return; + } + + printf("==========Init Context==========\n"); + OH_AI_ContextHandle context = OH_AI_ContextCreate(); + ASSERT_NE(context, nullptr); + AddContextDeviceNNRT(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/tinynet.om.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]; + float *input_data = reinterpret_cast(OH_AI_TensorGetMutableData(tensor)); + size_t element_num = OH_AI_TensorGetElementNum(tensor); + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_real_distribution dis(0.0f,1.0f); + for (int z=0;z