• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1From 4fad838cc97c4b382b2479bd90d5875f0616d56d Mon Sep 17 00:00:00 2001
2From: fangzhou0329 <fangzhou12@huawei.com>
3Date: Mon, 12 Jun 2023 15:36:52 +0800
4Subject: [PATCH] ohos info bugfix
5
6---
7 .../lite/src/runtime/js_api/common_napi.cc    | 24 +++---
8 .../src/runtime/js_api/mslite_model_napi.cc   | 75 +++++++++++--------
9 2 files changed, 57 insertions(+), 42 deletions(-)
10
11diff --git a/mindspore/lite/src/runtime/js_api/common_napi.cc b/mindspore/lite/src/runtime/js_api/common_napi.cc
12index 5054a317..5f4970fc 100644
13--- a/mindspore/lite/src/runtime/js_api/common_napi.cc
14+++ b/mindspore/lite/src/runtime/js_api/common_napi.cc
15@@ -64,17 +64,17 @@ int32_t CommonNapi::GetPropertyInt32(napi_env env, napi_value config_obj, const
16   napi_status status = napi_has_named_property(env, config_obj, type.c_str(), &exist);
17
18   if (status != napi_ok || !exist) {
19-    MS_LOG(WARNING) << "can not find " << type.c_str() << " property";
20+    MS_LOG(WARNING) << "can not find " << type.c_str() << "will set default value";
21     return ERR_NOT_EXISTED_PARAM;
22   }
23
24   if (napi_get_named_property(env, config_obj, type.c_str(), &item) != napi_ok) {
25-    MS_LOG(WARNING) << "get " << type.c_str() << " property fail";
26+    MS_LOG(WARNING) << "fail to get property: " << type.c_str();
27     return ERR_INVALID_PARAM;
28   }
29
30   if (napi_get_value_int32(env, item, &result) != napi_ok) {
31-    MS_LOG(WARNING) << "get " << type.c_str() << " property value fail";
32+    MS_LOG(WARNING) << "fail to get property value " << type.c_str();
33     return ERR_INVALID_PARAM;
34   }
35   return SUCCESS;
36@@ -90,17 +90,17 @@ int32_t CommonNapi::GetPropertyString(napi_env env, napi_value config_obj, const
37   napi_status status = napi_has_named_property(env, config_obj, type.c_str(), &exist);
38
39   if (status != napi_ok || !exist) {
40-    MS_LOG(WARNING) << "can not find " << type.c_str() << " property";
41+    MS_LOG(WARNING) << "can not find " << type.c_str() << "will set default value";
42     return ERR_NOT_EXISTED_PARAM;
43   }
44
45   if (napi_get_named_property(env, config_obj, type.c_str(), &item) != napi_ok) {
46-    MS_LOG(WARNING) << "get " << type.c_str() << " property fail";
47+    MS_LOG(WARNING) << "fail to get property: " << type.c_str();
48     return ERR_INVALID_PARAM;
49   }
50
51   if (napi_get_value_string_utf8(env, item, buffer, SIZE, &length) != napi_ok) {
52-    MS_LOG(WARNING) << "get " << type.c_str() << " property value fail";
53+    MS_LOG(WARNING) << "fail to get property value " << type.c_str();
54     return ERR_INVALID_PARAM;
55   }
56   result = std::string(buffer);
57@@ -113,19 +113,19 @@ int32_t CommonNapi::GetPropertyInt32Array(napi_env env, napi_value config_obj, c
58   bool exist = false;
59   napi_status status = napi_has_named_property(env, config_obj, type.c_str(), &exist);
60   if (status != napi_ok || !exist) {
61-    MS_LOG(WARNING) << "can not find " << type.c_str() << " property";
62+    MS_LOG(WARNING) << "can not find " << type.c_str() << "will set default value";
63     return ERR_NOT_EXISTED_PARAM;
64   }
65
66   if (napi_get_named_property(env, config_obj, type.c_str(), &item) != napi_ok) {
67-    MS_LOG(WARNING) << "get " << type.c_str() << " property fail";
68+    MS_LOG(WARNING) << "fail to get property: " << type.c_str();
69     return ERR_INVALID_PARAM;
70   }
71
72   uint32_t array_length = 0;
73   status = napi_get_array_length(env, item, &array_length);
74   if (status != napi_ok || array_length <= 0) {
75-    MS_LOG(WARNING) << "can not get array length";
76+    MS_LOG(WARNING) << "can not get array length.";
77     return ERR_INVALID_PARAM;
78   }
79   MS_LOG(DEBUG) << "GetPropertyInt32Array array_length: " << array_length;
80@@ -156,12 +156,12 @@ int32_t CommonNapi::GetPropertyStringArray(napi_env env, napi_value config_obj,
81   napi_status status = napi_has_named_property(env, config_obj, type.c_str(), &exist);
82
83   if (status != napi_ok || !exist) {
84-    MS_LOG(WARNING) << "can not find " << type.c_str() << " property";
85+    MS_LOG(WARNING) << "can not find " << type.c_str() << "will set default value";
86     return ERR_NOT_EXISTED_PARAM;
87   }
88
89   if (napi_get_named_property(env, config_obj, type.c_str(), &item) != napi_ok) {
90-    MS_LOG(WARNING) << "get " << type.c_str() << " property fail";
91+    MS_LOG(WARNING) << "fail to get property: " << type.c_str();
92     return ERR_INVALID_PARAM;
93   }
94
95@@ -184,7 +184,7 @@ int32_t CommonNapi::GetPropertyStringArray(napi_env env, napi_value config_obj,
96     }
97
98     if (napi_get_value_string_utf8(env, element, buffer, SIZE, &length) != napi_ok) {
99-      MS_LOG(WARNING) << "get " << type.c_str() << " property value fail";
100+      MS_LOG(WARNING) << "fail to get property value " << type.c_str();
101       return ERR_INVALID_PARAM;
102     }
103     result.push_back(std::string(buffer));
104diff --git a/mindspore/lite/src/runtime/js_api/mslite_model_napi.cc b/mindspore/lite/src/runtime/js_api/mslite_model_napi.cc
105index 7ee4b071..167c4a90 100644
106--- a/mindspore/lite/src/runtime/js_api/mslite_model_napi.cc
107+++ b/mindspore/lite/src/runtime/js_api/mslite_model_napi.cc
108@@ -453,13 +453,12 @@ napi_value MSLiteModelNapi::LoadMSLiteModelFromFile(napi_env env, napi_callback_
109   napi_value result = nullptr;
110   const int32_t refCount = 1;
111   GET_PARAMS(env, info, ARGS_THREE);
112+  napi_valuetype valueType = napi_undefined;
113
114   std::unique_ptr<MSLiteModelAsyncContext> asyncContext = std::make_unique<MSLiteModelAsyncContext>();
115
116   int32_t ret;
117   for (size_t i = PARAM0; i < argc; i++) {
118-    napi_valuetype valueType = napi_undefined;
119-    napi_typeof(env, argv[i], &valueType);
120     if (i == PARAM0) {
121       ret = ParseModelInfo(env, argv[i], asyncContext->model_info);
122       if (ret != SUCCESS) {
123@@ -467,12 +466,18 @@ napi_value MSLiteModelNapi::LoadMSLiteModelFromFile(napi_env env, napi_callback_
124         return result;
125       }
126     } else if (i == PARAM1) {
127-      ret = ParseContextInfo(env, argv[i], asyncContext->context);
128-      if (ret != SUCCESS) {
129-        MS_LOG(ERROR) << "Parsing context failed.";
130-        return result;
131-      }
132+      napi_typeof(env, argv[i], &valueType);
133+      if (valueType == napi_function) {
134+        napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
135+      } else {
136+        ret = ParseContextInfo(env, argv[i], asyncContext->context);
137+        if (ret != SUCCESS) {
138+          MS_LOG(ERROR) << "Parsing context failed.";
139+          return result;
140+        }
141+      }
142     } else if (i == PARAM2) {
143+      napi_typeof(env, argv[i], &valueType);
144       if (valueType == napi_function) {
145         napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
146       }
147@@ -524,13 +529,12 @@ napi_value MSLiteModelNapi::LoadMSLiteModelFromBuffer(napi_env env, napi_callbac
148   napi_value result = nullptr;
149   const int32_t refCount = 1;
150   GET_PARAMS(env, info, ARGS_THREE);
151+  napi_valuetype valueType = napi_undefined;
152
153   std::unique_ptr<MSLiteModelAsyncContext> asyncContext = std::make_unique<MSLiteModelAsyncContext>();
154
155   int32_t ret;
156   for (size_t i = PARAM0; i < argc; i++) {
157-    napi_valuetype valueType = napi_undefined;
158-    napi_typeof(env, argv[i], &valueType);
159     if (i == PARAM0) {
160       ret = ParseModelInfo(env, argv[i], asyncContext->model_info);
161       if (ret != SUCCESS) {
162@@ -538,12 +542,18 @@ napi_value MSLiteModelNapi::LoadMSLiteModelFromBuffer(napi_env env, napi_callbac
163         return result;
164       }
165     } else if (i == PARAM1) {
166-      ret = ParseContextInfo(env, argv[i], asyncContext->context);
167-      if (ret != SUCCESS) {
168-        MS_LOG(ERROR) << "Parsing context failed.";
169-        return result;
170-      }
171+      napi_typeof(env, argv[i], &valueType);
172+      if (valueType == napi_function) {
173+        napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
174+      } else {
175+        ret = ParseContextInfo(env, argv[i], asyncContext->context);
176+        if (ret != SUCCESS) {
177+          MS_LOG(ERROR) << "Parsing context failed.";
178+          return result;
179+        }
180+      }
181     } else if (i == PARAM2) {
182+      napi_typeof(env, argv[i], &valueType);
183       if (valueType == napi_function) {
184         napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
185       }
186@@ -595,13 +605,12 @@ napi_value MSLiteModelNapi::LoadMSLiteModelFromFd(napi_env env, napi_callback_in
187   napi_value result = nullptr;
188   const int32_t refCount = 1;
189   GET_PARAMS(env, info, ARGS_THREE);
190+  napi_valuetype valueType = napi_undefined;
191
192   std::unique_ptr<MSLiteModelAsyncContext> asyncContext = std::make_unique<MSLiteModelAsyncContext>();
193
194   int32_t ret;
195   for (size_t i = PARAM0; i < argc; i++) {
196-    napi_valuetype valueType = napi_undefined;
197-    napi_typeof(env, argv[i], &valueType);
198     if (i == PARAM0) {
199       ret = ParseModelInfo(env, argv[i], asyncContext->model_info);
200       if (ret != SUCCESS) {
201@@ -609,12 +618,18 @@ napi_value MSLiteModelNapi::LoadMSLiteModelFromFd(napi_env env, napi_callback_in
202         return result;
203       }
204     } else if (i == PARAM1) {
205-      ret = ParseContextInfo(env, argv[i], asyncContext->context);
206-      if (ret != SUCCESS) {
207-        MS_LOG(ERROR) << "Parsing context failed.";
208-        return result;
209+      napi_typeof(env, argv[i], &valueType);
210+      if (valueType == napi_function) {
211+        napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
212+      } else {
213+        ret = ParseContextInfo(env, argv[i], asyncContext->context);
214+        if (ret != SUCCESS) {
215+          MS_LOG(ERROR) << "Parsing context failed.";
216+          return result;
217+        }
218       }
219     } else if (i == PARAM2) {
220+      napi_typeof(env, argv[i], &valueType);
221       if (valueType == napi_function) {
222         napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
223       }
224@@ -692,8 +707,8 @@ int32_t MSLiteModelNapi::GetCpuDeviceInfo(napi_env env, napi_value args, Context
225
226   if (CommonNapi::GetPropertyInt32(env, config_item, "threadAffinityMode", int_value) == SUCCESS) {
227     MS_LOG(DEBUG) << "threadAffinityMode: " << int_value;
228-    if (int_value > PARAM2 || int_value < PARAM0) {
229-        MS_LOG(ERROR) << "threadAffinityMode value is set out of limitions";
230+	if (int_value > PARAM2 || int_value < PARAM0) {
231+        MS_LOG(ERROR) << "threadAffinityMode value is set: " << int_value << ", is out of limition";
232         return ERR_INVALID_OPERATION;
233     }
234     context.cpu_device.thread_affinity_mode = int_value;
235@@ -728,13 +743,13 @@ napi_value MSLiteModelNapi::GetInputs(napi_env env, napi_callback_info info) {
236
237   napi_status status = napi_get_cb_info(env, info, &argCount, nullptr, &jsThis, nullptr);
238   if (status != napi_ok || jsThis == nullptr) {
239-    MS_LOG(ERROR) << "Failed to retrieve details about the callback";
240+    MS_LOG(ERROR) << "failed to retrieve details about the callback";
241     return undefinedResult;
242   }
243
244   status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&modelNapi));
245   if (status != napi_ok || modelNapi == nullptr) {
246-    MS_LOG(ERROR) << "Failed to get model";
247+    MS_LOG(ERROR) << "failed to get model";
248     return undefinedResult;
249   }
250
251@@ -782,7 +797,7 @@ napi_value MSLiteModelNapi::Resize(napi_env env, napi_callback_info info) {
252   size_t argCount = PARAM2;
253   napi_status status = napi_get_cb_info(env, info, &argCount, argv, &jsThis, nullptr);
254   if (status != napi_ok || jsThis == nullptr) {
255-    MS_LOG(ERROR) << "Failed to retrieve details about the callback";
256+    MS_LOG(ERROR) << "failed to retrieve details about the callback";
257     return undefinedResult;
258   }
259   status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&modelNapi));
260@@ -803,7 +818,7 @@ napi_value MSLiteModelNapi::Resize(napi_env env, napi_callback_info info) {
261   uint32_t array_length = 0;
262   status = napi_get_array_length(env, argv[PARAM0], &array_length);
263   if (status != napi_ok || array_length <= 0) {
264-    MS_LOG(ERROR) << "Get inputs tensor length failed.";
265+    MS_LOG(ERROR) << "get inputs tensor length failed.";
266     return undefinedResult;
267   }
268   if (inputs.size() != array_length) {
269@@ -849,7 +864,7 @@ napi_value MSLiteModelNapi::Resize(napi_env env, napi_callback_info info) {
270
271     status = napi_get_arraybuffer_info(env, return_val, &js_data, &length);
272     if (status != napi_ok || js_data == nullptr) {
273-      MS_LOG(ERROR) << "Get js data error.";
274+      MS_LOG(ERROR) << "get js data error.";
275       return undefinedResult;
276     }
277     if (inputs[i].DataSize() != length) {
278@@ -941,7 +956,7 @@ int GenerateInputDataWithRandom(std::vector<mindspore::MSTensor> inputs) {
279   for (auto tensor : inputs) {
280     auto input_data = tensor.MutableData();
281     if (input_data == nullptr) {
282-      std::cerr << "MallocData for inTensor failed." << std::endl;
283+      std::cerr << "mallocData for inTensor failed." << std::endl;
284       return -1;
285     }
286     GenerateRandomData<float>(tensor.DataSize(), input_data, std::uniform_real_distribution<float>(0.1f, 1.0f));
287@@ -972,7 +987,7 @@ napi_value MSLiteModelNapi::PredictAsync(napi_env env, napi_callback_info info)
288       }
289       status = napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
290       if (status != napi_ok) {
291-        MS_LOG(ERROR) << "Failed to create reference of callback";
292+        MS_LOG(ERROR) << "failed to create reference of callback";
293         return result;
294       }
295     }
296@@ -1024,7 +1039,7 @@ int32_t MSLiteModelNapi::SetTensorData(napi_env env, napi_value thisVar, napi_va
297   uint32_t array_length = 0;
298   napi_status status = napi_get_array_length(env, argv, &array_length);
299   if (status != napi_ok || array_length <= 0) {
300-    MS_LOG(ERROR) << "Get inputs tensor length failed.";
301+    MS_LOG(ERROR) << "get inputs tensor length failed.";
302     return ERR_INVALID_PARAM;
303   }
304
305--
3062.17.1
307
308