• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1From b76ed2aef929564040297d967eb0f5fa402eacc5 Mon Sep 17 00:00:00 2001
2From: fangzhou12 <fangzhou12@huawei.com>
3Date: Fri, 7 Jul 2023 16:27:31 +0800
4Subject: [PATCH] enable enum
5
6---
7 include/js_api/common_napi.h                  |  31 +++++
8 include/js_api/mslite_model_napi.h            |  36 ++++++
9 .../src/runtime/js_api/mslite_model_napi.cc   | 109 ++++++++++++++++++
10 3 files changed, 176 insertions(+)
11
12diff --git a/include/js_api/common_napi.h b/include/js_api/common_napi.h
13index 415f813e..d6e161dd 100644
14--- a/include/js_api/common_napi.h
15+++ b/include/js_api/common_napi.h
16@@ -48,6 +48,37 @@ struct MSLiteAsyncContext {
17   std::string errMessage = "";
18 };
19
20+enum ContextThreadAffinityMode : int32_t {
21+    CONTEXT_AFFINITY_MODE = 0,
22+    CONTEXT_BIG_CORES_FIRST,
23+    CONTEXT_LITTLE_CORES_FIRST
24+};
25+
26+enum TensorFormat : int32_t {
27+    TENSOR_DEFAULT_FORMAT = -1,
28+    TENSOR_NCHW,
29+    TENSOR_NHWC,
30+    TENSOR_NHWC4,
31+    TENSOR_HWKC,
32+    TENSOR_HWCK,
33+    TENSOR_KCHW
34+};
35+
36+enum TensorDataType : int32_t {
37+    TENSOR_UNKNOWN = 0,
38+    TENSOR_INT8 = 32,
39+    TENSOR_INT16 = 33,
40+    TENSOR_INT32 = 34,
41+    TENSOR_INT64 = 35,
42+    TENSOR_UINT8 = 37,
43+    TENSOR_UINT16 = 38,
44+    TENSOR_UINT32 = 39,
45+    TENSOR_UINT64 = 40,
46+    TENSOR_FLOAT16 = 42,
47+    TENSOR_FLOAT32 = 43,
48+    TENSOR_FLOAT64 = 44
49+};
50+
51 enum ModelMode : int32_t {
52   kBuffer = 0,
53   kPath,
54diff --git a/include/js_api/mslite_model_napi.h b/include/js_api/mslite_model_napi.h
55index 717b595a..e85fc2f4 100644
56--- a/include/js_api/mslite_model_napi.h
57+++ b/include/js_api/mslite_model_napi.h
58@@ -24,8 +24,37 @@
59 #include "mslite_model_callback_napi.h"
60 #include "napi/native_api.h"
61 #include "napi/native_node_api.h"
62+#include "include/js_api/common_napi.h"
63
64 namespace mindspore {
65+static const std::map<std::string, TensorFormat> tensorFormatMap = {
66+    {"DEFAULT_FORMAT", TENSOR_DEFAULT_FORMAT},
67+    {"NCHW", TENSOR_NCHW},
68+    {"NHWC", TENSOR_NHWC},
69+    {"NHWC4", TENSOR_NHWC4},
70+    {"HWKC", TENSOR_HWKC},
71+    {"HWCK", TENSOR_HWCK},
72+    {"KCHW", TENSOR_KCHW}
73+};
74+static const std::map<std::string, TensorDataType> tensorDataTypeMap = {
75+    {"TYPE_UNKNOWN", TENSOR_UNKNOWN},
76+    {"NUMBER_TYPE_INT8", TENSOR_INT8},
77+    {"NUMBER_TYPE_INT16", TENSOR_INT16},
78+    {"NUMBER_TYPE_INT32", TENSOR_INT32},
79+    {"NUMBER_TYPE_INT64", TENSOR_INT64},
80+    {"NUMBER_TYPE_UINT8", TENSOR_UINT8},
81+    {"NUMBER_TYPE_UINT16", TENSOR_UINT16},
82+    {"NUMBER_TYPE_UINT32", TENSOR_UINT32},
83+    {"NUMBER_TYPE_UINT64", TENSOR_UINT64},
84+    {"NUMBER_TYPE_FLOAT16", TENSOR_FLOAT16},
85+    {"NUMBER_TYPE_FLOAT32", TENSOR_FLOAT32},
86+    {"NUMBER_TYPE_FLOAT64", TENSOR_FLOAT64}
87+};
88+static const std::map<std::string, ContextThreadAffinityMode> contextThreadAffinityModeMap = {
89+    {"NO_AFFINITIES", CONTEXT_AFFINITY_MODE},
90+    {"BIG_CORES_FIRST", CONTEXT_BIG_CORES_FIRST},
91+    {"LITTLE_CORES_FIRST", CONTEXT_LITTLE_CORES_FIRST},
92+};
93 class MSLiteModelNapi {
94  public:
95   MSLiteModelNapi();
96@@ -72,9 +101,16 @@ class MSLiteModelNapi {
97                                       std::vector<std::shared_ptr<DeviceInfoContext>> &device_infos);
98   static int32_t SetTensorData(napi_env env, napi_value thisVar, napi_value argv,
99                                MSLiteModelAsyncContext *async_context);
100+  static napi_status AddNamedProperty(napi_env env, napi_value object, const std::string name, int32_t enumValue);
101+  static napi_value CreateFormatObject(napi_env env);
102+  static napi_value CreateDataTypeObject(napi_env env);
103+  static napi_value CreateThreadAffinityModeObject(napi_env env);
104
105   static thread_local napi_ref constructor_;
106   napi_env env_ = nullptr;
107+  static napi_ref tensorFormat_;
108+  static napi_ref tensorDataType_;
109+  static napi_ref contextThreadAffinityMode_;
110
111   static ModelInfo *model_info_;
112   static ContextInfo *context_;
113diff --git a/mindspore/lite/src/runtime/js_api/mslite_model_napi.cc b/mindspore/lite/src/runtime/js_api/mslite_model_napi.cc
114index 0f1934a7..2ecc478d 100644
115--- a/mindspore/lite/src/runtime/js_api/mslite_model_napi.cc
116+++ b/mindspore/lite/src/runtime/js_api/mslite_model_napi.cc
117@@ -37,6 +37,9 @@ thread_local napi_ref MSLiteModelNapi::constructor_ = nullptr;
118 ModelInfo *MSLiteModelNapi::model_info_ = nullptr;
119 ContextInfo *MSLiteModelNapi::context_ = nullptr;
120 std::mutex MSLiteModelNapi::create_mutex_;
121+napi_ref MSLiteModelNapi::tensorFormat_ = nullptr;
122+napi_ref MSLiteModelNapi::tensorDataType_ = nullptr;
123+napi_ref MSLiteModelNapi::contextThreadAffinityMode_ = nullptr;
124
125 #define GET_PARAMS(env, info, num) \
126   size_t argc = num;               \
127@@ -101,6 +104,9 @@ napi_value MSLiteModelNapi::Init(napi_env env, napi_value exports) {
128     DECLARE_NAPI_STATIC_FUNCTION("loadModelFromFile", LoadMSLiteModelFromFile),
129     DECLARE_NAPI_STATIC_FUNCTION("loadModelFromBuffer", LoadMSLiteModelFromBuffer),
130     DECLARE_NAPI_STATIC_FUNCTION("loadModelFromFd", LoadMSLiteModelFromFd),
131+    DECLARE_NAPI_PROPERTY("Format", CreateFormatObject(env)),
132+    DECLARE_NAPI_PROPERTY("DataType", CreateDataTypeObject(env)),
133+    DECLARE_NAPI_PROPERTY("ThreadAffinityMode", CreateThreadAffinityModeObject(env)),
134   };
135
136   napi_value constructor = nullptr;
137@@ -133,6 +139,109 @@ napi_value MSLiteModelNapi::Init(napi_env env, napi_value exports) {
138   return exports;
139 }
140
141+napi_value MSLiteModelNapi::CreateFormatObject(napi_env env)
142+{
143+  napi_value result = nullptr;
144+  napi_status status;
145+  std::string propName;
146+  int32_t refCount = 1;
147+
148+  status = napi_create_object(env, &result);
149+  if (status == napi_ok) {
150+      for (auto &iter: tensorFormatMap) {
151+          propName = iter.first;
152+          status = AddNamedProperty(env, result, propName, iter.second);
153+          if (status != napi_ok) {
154+              MS_LOG(ERROR) << "Failed to add named prop in CreateFormatObject.";
155+              break;
156+          }
157+          propName.clear();
158+      }
159+      if (status == napi_ok) {
160+          status = napi_create_reference(env, result, refCount, &tensorFormat_);
161+          if (status == napi_ok) {
162+              return result;
163+          }
164+      }
165+  }
166+  MS_LOG(ERROR) << "CreateFormatObject is Failed!";
167+  napi_get_undefined(env, &result);
168+  return result;
169+}
170+
171+napi_value MSLiteModelNapi::CreateDataTypeObject(napi_env env)
172+{
173+  napi_value result = nullptr;
174+  napi_status status;
175+  std::string propName;
176+  int32_t refCount = 1;
177+
178+  status = napi_create_object(env, &result);
179+    if (status == napi_ok) {
180+        for (auto &iter: tensorDataTypeMap) {
181+             propName = iter.first;
182+             status = AddNamedProperty(env, result, propName, iter.second);
183+             if (status != napi_ok) {
184+                 MS_LOG(ERROR) << "Failed to add named prop in CreateDataTypeObject.";
185+                 break;
186+             }
187+             propName.clear();
188+        }
189+        if (status == napi_ok) {
190+            status = napi_create_reference(env, result, refCount, &tensorDataType_);
191+            if (status == napi_ok) {
192+                return result;
193+            }
194+        }
195+    }
196+    MS_LOG(ERROR) << "CreateDataTypeObject is Failed!";
197+    napi_get_undefined(env, &result);
198+    return result;
199+}
200+
201+napi_value MSLiteModelNapi::CreateThreadAffinityModeObject(napi_env env)
202+{
203+    napi_value result = nullptr;
204+    napi_status status;
205+    std::string propName;
206+    int32_t refCount = 1;
207+
208+    status = napi_create_object(env, &result);
209+    if (status == napi_ok) {
210+        for (auto &iter: contextThreadAffinityModeMap) {
211+            propName = iter.first;
212+            status = AddNamedProperty(env, result, propName, iter.second);
213+            if (status != napi_ok) {
214+                MS_LOG(ERROR) << "Failed to add named prop in CreateThreadAffinityModeObject.";
215+                break;
216+            }
217+            propName.clear();
218+        }
219+        if (status == napi_ok) {
220+            status = napi_create_reference(env, result, refCount, &contextThreadAffinityMode_);
221+            if (status == napi_ok) {
222+                return result;
223+            }
224+        }
225+    }
226+    MS_LOG(ERROR) << "CreateThreadAffinityModeObject is Failed!";
227+    napi_get_undefined(env, &result);
228+    return result;
229+}
230+
231+napi_status MSLiteModelNapi::AddNamedProperty(napi_env env, napi_value object,
232+                                              const std::string name, int32_t enumValue)
233+{
234+    napi_status status;
235+    napi_value enumNapiValue;
236+
237+    status = napi_create_int32(env, enumValue, &enumNapiValue);
238+    if (status == napi_ok) {
239+        status = napi_set_named_property(env, object, name.c_str(), enumNapiValue);
240+    }
241+    return status;
242+}
243+
244 std::shared_ptr<mindspore::Model> MSLiteModelNapi::CreateModel(ModelInfo *model_info_ptr,
245                                                                ContextInfo *context_info_ptr) {
246   // create and init context
247--
2482.17.1
249
250