From 4fad838cc97c4b382b2479bd90d5875f0616d56d Mon Sep 17 00:00:00 2001 From: fangzhou0329 Date: Mon, 12 Jun 2023 15:36:52 +0800 Subject: [PATCH] ohos info bugfix --- .../lite/src/runtime/js_api/common_napi.cc | 24 +++--- .../src/runtime/js_api/mslite_model_napi.cc | 75 +++++++++++-------- 2 files changed, 57 insertions(+), 42 deletions(-) diff --git a/mindspore/lite/src/runtime/js_api/common_napi.cc b/mindspore/lite/src/runtime/js_api/common_napi.cc index 5054a317..5f4970fc 100644 --- a/mindspore/lite/src/runtime/js_api/common_napi.cc +++ b/mindspore/lite/src/runtime/js_api/common_napi.cc @@ -64,17 +64,17 @@ int32_t CommonNapi::GetPropertyInt32(napi_env env, napi_value config_obj, const napi_status status = napi_has_named_property(env, config_obj, type.c_str(), &exist); if (status != napi_ok || !exist) { - MS_LOG(WARNING) << "can not find " << type.c_str() << " property"; + MS_LOG(WARNING) << "can not find " << type.c_str() << "will set default value"; return ERR_NOT_EXISTED_PARAM; } if (napi_get_named_property(env, config_obj, type.c_str(), &item) != napi_ok) { - MS_LOG(WARNING) << "get " << type.c_str() << " property fail"; + MS_LOG(WARNING) << "fail to get property: " << type.c_str(); return ERR_INVALID_PARAM; } if (napi_get_value_int32(env, item, &result) != napi_ok) { - MS_LOG(WARNING) << "get " << type.c_str() << " property value fail"; + MS_LOG(WARNING) << "fail to get property value " << type.c_str(); return ERR_INVALID_PARAM; } return SUCCESS; @@ -90,17 +90,17 @@ int32_t CommonNapi::GetPropertyString(napi_env env, napi_value config_obj, const napi_status status = napi_has_named_property(env, config_obj, type.c_str(), &exist); if (status != napi_ok || !exist) { - MS_LOG(WARNING) << "can not find " << type.c_str() << " property"; + MS_LOG(WARNING) << "can not find " << type.c_str() << "will set default value"; return ERR_NOT_EXISTED_PARAM; } if (napi_get_named_property(env, config_obj, type.c_str(), &item) != napi_ok) { - MS_LOG(WARNING) << "get " << type.c_str() << " property fail"; + MS_LOG(WARNING) << "fail to get property: " << type.c_str(); return ERR_INVALID_PARAM; } if (napi_get_value_string_utf8(env, item, buffer, SIZE, &length) != napi_ok) { - MS_LOG(WARNING) << "get " << type.c_str() << " property value fail"; + MS_LOG(WARNING) << "fail to get property value " << type.c_str(); return ERR_INVALID_PARAM; } result = std::string(buffer); @@ -113,19 +113,19 @@ int32_t CommonNapi::GetPropertyInt32Array(napi_env env, napi_value config_obj, c bool exist = false; napi_status status = napi_has_named_property(env, config_obj, type.c_str(), &exist); if (status != napi_ok || !exist) { - MS_LOG(WARNING) << "can not find " << type.c_str() << " property"; + MS_LOG(WARNING) << "can not find " << type.c_str() << "will set default value"; return ERR_NOT_EXISTED_PARAM; } if (napi_get_named_property(env, config_obj, type.c_str(), &item) != napi_ok) { - MS_LOG(WARNING) << "get " << type.c_str() << " property fail"; + MS_LOG(WARNING) << "fail to get property: " << type.c_str(); return ERR_INVALID_PARAM; } uint32_t array_length = 0; status = napi_get_array_length(env, item, &array_length); if (status != napi_ok || array_length <= 0) { - MS_LOG(WARNING) << "can not get array length"; + MS_LOG(WARNING) << "can not get array length."; return ERR_INVALID_PARAM; } MS_LOG(DEBUG) << "GetPropertyInt32Array array_length: " << array_length; @@ -156,12 +156,12 @@ int32_t CommonNapi::GetPropertyStringArray(napi_env env, napi_value config_obj, napi_status status = napi_has_named_property(env, config_obj, type.c_str(), &exist); if (status != napi_ok || !exist) { - MS_LOG(WARNING) << "can not find " << type.c_str() << " property"; + MS_LOG(WARNING) << "can not find " << type.c_str() << "will set default value"; return ERR_NOT_EXISTED_PARAM; } if (napi_get_named_property(env, config_obj, type.c_str(), &item) != napi_ok) { - MS_LOG(WARNING) << "get " << type.c_str() << " property fail"; + MS_LOG(WARNING) << "fail to get property: " << type.c_str(); return ERR_INVALID_PARAM; } @@ -184,7 +184,7 @@ int32_t CommonNapi::GetPropertyStringArray(napi_env env, napi_value config_obj, } if (napi_get_value_string_utf8(env, element, buffer, SIZE, &length) != napi_ok) { - MS_LOG(WARNING) << "get " << type.c_str() << " property value fail"; + MS_LOG(WARNING) << "fail to get property value " << type.c_str(); return ERR_INVALID_PARAM; } result.push_back(std::string(buffer)); diff --git a/mindspore/lite/src/runtime/js_api/mslite_model_napi.cc b/mindspore/lite/src/runtime/js_api/mslite_model_napi.cc index 7ee4b071..167c4a90 100644 --- a/mindspore/lite/src/runtime/js_api/mslite_model_napi.cc +++ b/mindspore/lite/src/runtime/js_api/mslite_model_napi.cc @@ -453,13 +453,12 @@ napi_value MSLiteModelNapi::LoadMSLiteModelFromFile(napi_env env, napi_callback_ napi_value result = nullptr; const int32_t refCount = 1; GET_PARAMS(env, info, ARGS_THREE); + napi_valuetype valueType = napi_undefined; std::unique_ptr asyncContext = std::make_unique(); int32_t ret; for (size_t i = PARAM0; i < argc; i++) { - napi_valuetype valueType = napi_undefined; - napi_typeof(env, argv[i], &valueType); if (i == PARAM0) { ret = ParseModelInfo(env, argv[i], asyncContext->model_info); if (ret != SUCCESS) { @@ -467,12 +466,18 @@ napi_value MSLiteModelNapi::LoadMSLiteModelFromFile(napi_env env, napi_callback_ return result; } } else if (i == PARAM1) { - ret = ParseContextInfo(env, argv[i], asyncContext->context); - if (ret != SUCCESS) { - MS_LOG(ERROR) << "Parsing context failed."; - return result; - } + napi_typeof(env, argv[i], &valueType); + if (valueType == napi_function) { + napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef); + } else { + ret = ParseContextInfo(env, argv[i], asyncContext->context); + if (ret != SUCCESS) { + MS_LOG(ERROR) << "Parsing context failed."; + return result; + } + } } else if (i == PARAM2) { + napi_typeof(env, argv[i], &valueType); if (valueType == napi_function) { napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef); } @@ -524,13 +529,12 @@ napi_value MSLiteModelNapi::LoadMSLiteModelFromBuffer(napi_env env, napi_callbac napi_value result = nullptr; const int32_t refCount = 1; GET_PARAMS(env, info, ARGS_THREE); + napi_valuetype valueType = napi_undefined; std::unique_ptr asyncContext = std::make_unique(); int32_t ret; for (size_t i = PARAM0; i < argc; i++) { - napi_valuetype valueType = napi_undefined; - napi_typeof(env, argv[i], &valueType); if (i == PARAM0) { ret = ParseModelInfo(env, argv[i], asyncContext->model_info); if (ret != SUCCESS) { @@ -538,12 +542,18 @@ napi_value MSLiteModelNapi::LoadMSLiteModelFromBuffer(napi_env env, napi_callbac return result; } } else if (i == PARAM1) { - ret = ParseContextInfo(env, argv[i], asyncContext->context); - if (ret != SUCCESS) { - MS_LOG(ERROR) << "Parsing context failed."; - return result; - } + napi_typeof(env, argv[i], &valueType); + if (valueType == napi_function) { + napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef); + } else { + ret = ParseContextInfo(env, argv[i], asyncContext->context); + if (ret != SUCCESS) { + MS_LOG(ERROR) << "Parsing context failed."; + return result; + } + } } else if (i == PARAM2) { + napi_typeof(env, argv[i], &valueType); if (valueType == napi_function) { napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef); } @@ -595,13 +605,12 @@ napi_value MSLiteModelNapi::LoadMSLiteModelFromFd(napi_env env, napi_callback_in napi_value result = nullptr; const int32_t refCount = 1; GET_PARAMS(env, info, ARGS_THREE); + napi_valuetype valueType = napi_undefined; std::unique_ptr asyncContext = std::make_unique(); int32_t ret; for (size_t i = PARAM0; i < argc; i++) { - napi_valuetype valueType = napi_undefined; - napi_typeof(env, argv[i], &valueType); if (i == PARAM0) { ret = ParseModelInfo(env, argv[i], asyncContext->model_info); if (ret != SUCCESS) { @@ -609,12 +618,18 @@ napi_value MSLiteModelNapi::LoadMSLiteModelFromFd(napi_env env, napi_callback_in return result; } } else if (i == PARAM1) { - ret = ParseContextInfo(env, argv[i], asyncContext->context); - if (ret != SUCCESS) { - MS_LOG(ERROR) << "Parsing context failed."; - return result; + napi_typeof(env, argv[i], &valueType); + if (valueType == napi_function) { + napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef); + } else { + ret = ParseContextInfo(env, argv[i], asyncContext->context); + if (ret != SUCCESS) { + MS_LOG(ERROR) << "Parsing context failed."; + return result; + } } } else if (i == PARAM2) { + napi_typeof(env, argv[i], &valueType); if (valueType == napi_function) { napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef); } @@ -692,8 +707,8 @@ int32_t MSLiteModelNapi::GetCpuDeviceInfo(napi_env env, napi_value args, Context if (CommonNapi::GetPropertyInt32(env, config_item, "threadAffinityMode", int_value) == SUCCESS) { MS_LOG(DEBUG) << "threadAffinityMode: " << int_value; - if (int_value > PARAM2 || int_value < PARAM0) { - MS_LOG(ERROR) << "threadAffinityMode value is set out of limitions"; + if (int_value > PARAM2 || int_value < PARAM0) { + MS_LOG(ERROR) << "threadAffinityMode value is set: " << int_value << ", is out of limition"; return ERR_INVALID_OPERATION; } context.cpu_device.thread_affinity_mode = int_value; @@ -728,13 +743,13 @@ napi_value MSLiteModelNapi::GetInputs(napi_env env, napi_callback_info info) { napi_status status = napi_get_cb_info(env, info, &argCount, nullptr, &jsThis, nullptr); if (status != napi_ok || jsThis == nullptr) { - MS_LOG(ERROR) << "Failed to retrieve details about the callback"; + MS_LOG(ERROR) << "failed to retrieve details about the callback"; return undefinedResult; } status = napi_unwrap(env, jsThis, reinterpret_cast(&modelNapi)); if (status != napi_ok || modelNapi == nullptr) { - MS_LOG(ERROR) << "Failed to get model"; + MS_LOG(ERROR) << "failed to get model"; return undefinedResult; } @@ -782,7 +797,7 @@ napi_value MSLiteModelNapi::Resize(napi_env env, napi_callback_info info) { size_t argCount = PARAM2; napi_status status = napi_get_cb_info(env, info, &argCount, argv, &jsThis, nullptr); if (status != napi_ok || jsThis == nullptr) { - MS_LOG(ERROR) << "Failed to retrieve details about the callback"; + MS_LOG(ERROR) << "failed to retrieve details about the callback"; return undefinedResult; } status = napi_unwrap(env, jsThis, reinterpret_cast(&modelNapi)); @@ -803,7 +818,7 @@ napi_value MSLiteModelNapi::Resize(napi_env env, napi_callback_info info) { uint32_t array_length = 0; status = napi_get_array_length(env, argv[PARAM0], &array_length); if (status != napi_ok || array_length <= 0) { - MS_LOG(ERROR) << "Get inputs tensor length failed."; + MS_LOG(ERROR) << "get inputs tensor length failed."; return undefinedResult; } if (inputs.size() != array_length) { @@ -849,7 +864,7 @@ napi_value MSLiteModelNapi::Resize(napi_env env, napi_callback_info info) { status = napi_get_arraybuffer_info(env, return_val, &js_data, &length); if (status != napi_ok || js_data == nullptr) { - MS_LOG(ERROR) << "Get js data error."; + MS_LOG(ERROR) << "get js data error."; return undefinedResult; } if (inputs[i].DataSize() != length) { @@ -941,7 +956,7 @@ int GenerateInputDataWithRandom(std::vector inputs) { for (auto tensor : inputs) { auto input_data = tensor.MutableData(); if (input_data == nullptr) { - std::cerr << "MallocData for inTensor failed." << std::endl; + std::cerr << "mallocData for inTensor failed." << std::endl; return -1; } GenerateRandomData(tensor.DataSize(), input_data, std::uniform_real_distribution(0.1f, 1.0f)); @@ -972,7 +987,7 @@ napi_value MSLiteModelNapi::PredictAsync(napi_env env, napi_callback_info info) } status = napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef); if (status != napi_ok) { - MS_LOG(ERROR) << "Failed to create reference of callback"; + MS_LOG(ERROR) << "failed to create reference of callback"; return result; } } @@ -1024,7 +1039,7 @@ int32_t MSLiteModelNapi::SetTensorData(napi_env env, napi_value thisVar, napi_va uint32_t array_length = 0; napi_status status = napi_get_array_length(env, argv, &array_length); if (status != napi_ok || array_length <= 0) { - MS_LOG(ERROR) << "Get inputs tensor length failed."; + MS_LOG(ERROR) << "get inputs tensor length failed."; return ERR_INVALID_PARAM; } -- 2.17.1