1be168c0dSopenharmony_ciFrom 318fa79ab19e529b32b73a976f852a7428a71109 Mon Sep 17 00:00:00 2001
2be168c0dSopenharmony_ciFrom: zhangyanhui <zhangyanhui17@huawei.com>
3be168c0dSopenharmony_ciDate: Sat, 25 May 2024 10:19:16 +0800
4be168c0dSopenharmony_ciSubject: [PATCH] auto-apply 0003-add-js-api.patch
5be168c0dSopenharmony_ci
6be168c0dSopenharmony_ci---
7be168c0dSopenharmony_ci include/js_api/@ohos.ai.mindSporeLite.d.ts    |  867 ++++++
8be168c0dSopenharmony_ci include/js_api/common_napi.h                  |  196 ++
9be168c0dSopenharmony_ci include/js_api/ms_errors.h                    |   39 +
10be168c0dSopenharmony_ci include/js_api/ms_info.h                      |   69 +
11be168c0dSopenharmony_ci include/js_api/ms_parameters_napi.h           |   24 +
12be168c0dSopenharmony_ci include/js_api/mslite_model_callback_napi.h   |   38 +
13be168c0dSopenharmony_ci include/js_api/mslite_model_napi.h            |  186 ++
14be168c0dSopenharmony_ci include/js_api/mstensor_napi.h                |   49 +
15be168c0dSopenharmony_ci include/js_api/native_module_ohos_ms.h        |   22 +
16be168c0dSopenharmony_ci include/js_api/nnrt_device_desc_napi.h        |   45 +
17be168c0dSopenharmony_ci mindspore/lite/BUILD.gn                       |    1 +
18be168c0dSopenharmony_ci mindspore/lite/src/litert/js_api/BUILD.gn     |   56 +
19be168c0dSopenharmony_ci .../lite/src/litert/js_api/common_napi.cc     |  303 ++
20be168c0dSopenharmony_ci .../src/litert/js_api/mslite_model_napi.cc    | 2653 +++++++++++++++++
21be168c0dSopenharmony_ci .../lite/src/litert/js_api/mstensor_napi.cc   |  416 +++
22be168c0dSopenharmony_ci .../litert/js_api/native_module_ohos_ms.cc    |   48 +
23be168c0dSopenharmony_ci .../src/litert/js_api/nnrt_device_desc.cc     |  216 ++
24be168c0dSopenharmony_ci 17 files changed, 5228 insertions(+)
25be168c0dSopenharmony_ci create mode 100644 include/js_api/@ohos.ai.mindSporeLite.d.ts
26be168c0dSopenharmony_ci create mode 100644 include/js_api/common_napi.h
27be168c0dSopenharmony_ci create mode 100644 include/js_api/ms_errors.h
28be168c0dSopenharmony_ci create mode 100644 include/js_api/ms_info.h
29be168c0dSopenharmony_ci create mode 100644 include/js_api/ms_parameters_napi.h
30be168c0dSopenharmony_ci create mode 100644 include/js_api/mslite_model_callback_napi.h
31be168c0dSopenharmony_ci create mode 100644 include/js_api/mslite_model_napi.h
32be168c0dSopenharmony_ci create mode 100644 include/js_api/mstensor_napi.h
33be168c0dSopenharmony_ci create mode 100644 include/js_api/native_module_ohos_ms.h
34be168c0dSopenharmony_ci create mode 100644 include/js_api/nnrt_device_desc_napi.h
35be168c0dSopenharmony_ci create mode 100644 mindspore/lite/src/litert/js_api/BUILD.gn
36be168c0dSopenharmony_ci create mode 100644 mindspore/lite/src/litert/js_api/common_napi.cc
37be168c0dSopenharmony_ci create mode 100644 mindspore/lite/src/litert/js_api/mslite_model_napi.cc
38be168c0dSopenharmony_ci create mode 100644 mindspore/lite/src/litert/js_api/mstensor_napi.cc
39be168c0dSopenharmony_ci create mode 100644 mindspore/lite/src/litert/js_api/native_module_ohos_ms.cc
40be168c0dSopenharmony_ci create mode 100644 mindspore/lite/src/litert/js_api/nnrt_device_desc.cc
41be168c0dSopenharmony_ci
42be168c0dSopenharmony_cidiff --git a/include/js_api/@ohos.ai.mindSporeLite.d.ts b/include/js_api/@ohos.ai.mindSporeLite.d.ts
43be168c0dSopenharmony_cinew file mode 100644
44be168c0dSopenharmony_ciindex 00000000..6b9aa822
45be168c0dSopenharmony_ci--- /dev/null
46be168c0dSopenharmony_ci+++ b/include/js_api/@ohos.ai.mindSporeLite.d.ts
47be168c0dSopenharmony_ci@@ -0,0 +1,867 @@
48be168c0dSopenharmony_ci+/*
49be168c0dSopenharmony_ci+ * Copyright (c) 2023 Huawei Device Co., Ltd.
50be168c0dSopenharmony_ci+ * Licensed under the Apache License, Version 2.0 (the "License");
51be168c0dSopenharmony_ci+ * you may not use this file except in compliance with the License.
52be168c0dSopenharmony_ci+ * You may obtain a copy of the License at
53be168c0dSopenharmony_ci+ *
54be168c0dSopenharmony_ci+ * http://www.apache.org/licenses/LICENSE-2.0
55be168c0dSopenharmony_ci+ *
56be168c0dSopenharmony_ci+ * Unless required by applicable law or agreed to in writing, software
57be168c0dSopenharmony_ci+ * distributed under the License is distributed on an "AS IS" BASIS,
58be168c0dSopenharmony_ci+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
59be168c0dSopenharmony_ci+ * See the License for the specific language governing permissions and
60be168c0dSopenharmony_ci+ * limitations under the License.
61be168c0dSopenharmony_ci+ */
62be168c0dSopenharmony_ci+
63be168c0dSopenharmony_ci+import { Callback } from './@ohos.base';
64be168c0dSopenharmony_ci+
65be168c0dSopenharmony_ci+/**
66be168c0dSopenharmony_ci+ * @namespace mindSporeLite
67be168c0dSopenharmony_ci+ * @syscap SystemCapability.AI.MindSporeLite
68be168c0dSopenharmony_ci+ * @stagemodelonly
69be168c0dSopenharmony_ci+ * @since 10
70be168c0dSopenharmony_ci+ */
71be168c0dSopenharmony_ci+declare namespace mindSporeLite {
72be168c0dSopenharmony_ci+  /**
73be168c0dSopenharmony_ci+   * Create a Model instance from file path
74be168c0dSopenharmony_ci+   * @param { string } model - model indicates model path to be loaded
75be168c0dSopenharmony_ci+   * @param { Context } context - context indicates model context information
76be168c0dSopenharmony_ci+   * @returns { Promise<Model> } the promise returned by the function.
77be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
78be168c0dSopenharmony_ci+   * @stagemodelonly
79be168c0dSopenharmony_ci+   * @since 10
80be168c0dSopenharmony_ci+   */
81be168c0dSopenharmony_ci+  function loadModelFromFile(
82be168c0dSopenharmony_ci+    model: string,
83be168c0dSopenharmony_ci+    context?: Context): Promise<Model>;
84be168c0dSopenharmony_ci+
85be168c0dSopenharmony_ci+  /**
86be168c0dSopenharmony_ci+   * Create a Model instance from file path.
87be168c0dSopenharmony_ci+   * @param { string } model - model indicates model path to be loaded
88be168c0dSopenharmony_ci+   * @param { callback: Callback<Model> } callback - the callback of model
89be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
90be168c0dSopenharmony_ci+   * @stagemodelonly
91be168c0dSopenharmony_ci+   * @since 10
92be168c0dSopenharmony_ci+   */
93be168c0dSopenharmony_ci+  function loadModelFromFile(
94be168c0dSopenharmony_ci+    model: string, callback: Callback<Model>): void;
95be168c0dSopenharmony_ci+
96be168c0dSopenharmony_ci+  /**
97be168c0dSopenharmony_ci+   * Create a Model instance from file path.
98be168c0dSopenharmony_ci+   * @param { string } model - model indicates model path to be loaded
99be168c0dSopenharmony_ci+   * @param { Context } [context] - context indicates model context information
100be168c0dSopenharmony_ci+   * @param { callback: Callback<Model> } callback - the callback of model
101be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
102be168c0dSopenharmony_ci+   * @stagemodelonly
103be168c0dSopenharmony_ci+   * @since 10
104be168c0dSopenharmony_ci+   */
105be168c0dSopenharmony_ci+  function loadModelFromFile(
106be168c0dSopenharmony_ci+    model: string,
107be168c0dSopenharmony_ci+    context: Context, callback: Callback<Model>): void;
108be168c0dSopenharmony_ci+
109be168c0dSopenharmony_ci+  /**
110be168c0dSopenharmony_ci+   * Create a Model instance from buffer
111be168c0dSopenharmony_ci+   * @param { ArrayBuffer } model - model indicates model buffer to be loaded
112be168c0dSopenharmony_ci+   * @param { Context } context - context indicates model context information
113be168c0dSopenharmony_ci+   * @returns { Promise<Model> } the promise returned by the function.
114be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
115be168c0dSopenharmony_ci+   * @stagemodelonly
116be168c0dSopenharmony_ci+   * @since 10
117be168c0dSopenharmony_ci+   */
118be168c0dSopenharmony_ci+  function loadModelFromBuffer(
119be168c0dSopenharmony_ci+    model: ArrayBuffer,
120be168c0dSopenharmony_ci+    context?: Context): Promise<Model>;
121be168c0dSopenharmony_ci+
122be168c0dSopenharmony_ci+  /**
123be168c0dSopenharmony_ci+   * Create a Model instance from buffer
124be168c0dSopenharmony_ci+   * @param { ArrayBuffer } model - model indicates model buffer to be loaded
125be168c0dSopenharmony_ci+   * @param { callback: Callback<Model> } callback - the callback of model
126be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
127be168c0dSopenharmony_ci+   * @stagemodelonly
128be168c0dSopenharmony_ci+   * @since 10
129be168c0dSopenharmony_ci+   */
130be168c0dSopenharmony_ci+  function loadModelFromBuffer(
131be168c0dSopenharmony_ci+    model: ArrayBuffer, callback: Callback<Model>): void;
132be168c0dSopenharmony_ci+
133be168c0dSopenharmony_ci+  /**
134be168c0dSopenharmony_ci+   * Create a Model instance from buffer
135be168c0dSopenharmony_ci+   * @param { ArrayBuffer } model - model indicates model buffer to be loaded
136be168c0dSopenharmony_ci+   * @param { Context } [context] - context indicates model context information
137be168c0dSopenharmony_ci+   * @param { callback: Callback<Model> } callback - the callback of model
138be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
139be168c0dSopenharmony_ci+   * @stagemodelonly
140be168c0dSopenharmony_ci+   * @since 10
141be168c0dSopenharmony_ci+   */
142be168c0dSopenharmony_ci+  function loadModelFromBuffer(
143be168c0dSopenharmony_ci+    model: ArrayBuffer,
144be168c0dSopenharmony_ci+    context: Context, callback: Callback<Model>): void;
145be168c0dSopenharmony_ci+
146be168c0dSopenharmony_ci+  /**
147be168c0dSopenharmony_ci+   * Creates a Model instance file description
148be168c0dSopenharmony_ci+   * @param { number } model - model indicates model file description to be loaded
149be168c0dSopenharmony_ci+   * @param { Context } context - context indicates model context information
150be168c0dSopenharmony_ci+   * @returns { Promise<Model> } the promise returned by the function.
151be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
152be168c0dSopenharmony_ci+   * @stagemodelonly
153be168c0dSopenharmony_ci+   * @since 10
154be168c0dSopenharmony_ci+   */
155be168c0dSopenharmony_ci+  function loadModelFromFd(
156be168c0dSopenharmony_ci+    model: number,
157be168c0dSopenharmony_ci+    context?: Context): Promise<Model>;
158be168c0dSopenharmony_ci+
159be168c0dSopenharmony_ci+  /**
160be168c0dSopenharmony_ci+   * Create a Model instance from file description
161be168c0dSopenharmony_ci+   * @param { number } model - model indicates model file description to be loaded
162be168c0dSopenharmony_ci+   * @param { callback: Callback<Model> } callback - the callback of model
163be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
164be168c0dSopenharmony_ci+   * @stagemodelonly
165be168c0dSopenharmony_ci+   * @since 10
166be168c0dSopenharmony_ci+   */
167be168c0dSopenharmony_ci+  function loadModelFromFd(
168be168c0dSopenharmony_ci+    model: number, callback: Callback<Model>): void;
169be168c0dSopenharmony_ci+
170be168c0dSopenharmony_ci+  /**
171be168c0dSopenharmony_ci+   * Create a Model instance from file description
172be168c0dSopenharmony_ci+   * @param { number } model - model indicates model file description to be loaded
173be168c0dSopenharmony_ci+   * @param { Context } [context] - context indicates model context information
174be168c0dSopenharmony_ci+   * @param { callback: Callback<Model> } callback - the callback of model
175be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
176be168c0dSopenharmony_ci+   * @stagemodelonly
177be168c0dSopenharmony_ci+   * @since 10
178be168c0dSopenharmony_ci+   */
179be168c0dSopenharmony_ci+  function loadModelFromFd(
180be168c0dSopenharmony_ci+    model: number,
181be168c0dSopenharmony_ci+    context: Context, callback: Callback<Model>): void;
182be168c0dSopenharmony_ci+
183be168c0dSopenharmony_ci+  /**
184be168c0dSopenharmony_ci+   * Load train model from file
185be168c0dSopenharmony_ci+   * @param { string } model - model file path
186be168c0dSopenharmony_ci+   * @param { ?TrainCfg } trainCfg - model train configuration
187be168c0dSopenharmony_ci+   * @param { ?Context } context - model build context
188be168c0dSopenharmony_ci+   * @returns { Promise<Model> } the promise of the built model
189be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
190be168c0dSopenharmony_ci+   * @stagemodelonly
191be168c0dSopenharmony_ci+   * @since 11
192be168c0dSopenharmony_ci+   */
193be168c0dSopenharmony_ci+  function loadTrainModelFromFile(
194be168c0dSopenharmony_ci+    model: string,
195be168c0dSopenharmony_ci+    trainCfg?: TrainCfg,
196be168c0dSopenharmony_ci+    context?: Context): Promise<Model>;
197be168c0dSopenharmony_ci+
198be168c0dSopenharmony_ci+  /**
199be168c0dSopenharmony_ci+   * Load train model from buffer
200be168c0dSopenharmony_ci+   * @param { ArrayBuffer } model - model buffer
201be168c0dSopenharmony_ci+   * @param { ?TrainCfg } trainCfg - model train configuration
202be168c0dSopenharmony_ci+   * @param { ?Context } context - model build context
203be168c0dSopenharmony_ci+   * @returns { Promise<Model> } the promise of the built model
204be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
205be168c0dSopenharmony_ci+   * @stagemodelonly
206be168c0dSopenharmony_ci+   * @since 11
207be168c0dSopenharmony_ci+   */
208be168c0dSopenharmony_ci+  function loadTrainModelFromBuffer(
209be168c0dSopenharmony_ci+    model: ArrayBuffer,
210be168c0dSopenharmony_ci+    trainCfg?: TrainCfg,
211be168c0dSopenharmony_ci+    context?: Context): Promise<Model>;
212be168c0dSopenharmony_ci+
213be168c0dSopenharmony_ci+  /**
214be168c0dSopenharmony_ci+   * Load train model from file description
215be168c0dSopenharmony_ci+   * @param { number } model - model file description
216be168c0dSopenharmony_ci+   * @param { ?TrainCfg } trainCfg - model train configuration
217be168c0dSopenharmony_ci+   * @param { ?Context } context - model build context
218be168c0dSopenharmony_ci+   * @returns { Promise<Model> } the promise of the built model
219be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
220be168c0dSopenharmony_ci+   * @stagemodelonly
221be168c0dSopenharmony_ci+   * @since 11
222be168c0dSopenharmony_ci+   */
223be168c0dSopenharmony_ci+  function loadTrainModelFromFd(
224be168c0dSopenharmony_ci+    model: number,
225be168c0dSopenharmony_ci+    trainCfg?: TrainCfg,
226be168c0dSopenharmony_ci+    context?: Context): Promise<Model>;
227be168c0dSopenharmony_ci+
228be168c0dSopenharmony_ci+  /**
229be168c0dSopenharmony_ci+   * Provides manages model function. Including get inputs, predict ,resize.
230be168c0dSopenharmony_ci+   * @typedef Model
231be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
232be168c0dSopenharmony_ci+   * @stagemodelonly
233be168c0dSopenharmony_ci+   * @since 10
234be168c0dSopenharmony_ci+   */
235be168c0dSopenharmony_ci+  interface Model {
236be168c0dSopenharmony_ci+    /**
237be168c0dSopenharmony_ci+     * The learning rate of the training model
238be168c0dSopenharmony_ci+     * @type {?number}
239be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
240be168c0dSopenharmony_ci+     * @since 11
241be168c0dSopenharmony_ci+     */
242be168c0dSopenharmony_ci+    learningRate?: number,
243be168c0dSopenharmony_ci+
244be168c0dSopenharmony_ci+    /**
245be168c0dSopenharmony_ci+     * The running mode of the model
246be168c0dSopenharmony_ci+     * @type {?boolean}
247be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
248be168c0dSopenharmony_ci+     * @since 11
249be168c0dSopenharmony_ci+     */
250be168c0dSopenharmony_ci+    trainMode?: boolean,
251be168c0dSopenharmony_ci+
252be168c0dSopenharmony_ci+    /**
253be168c0dSopenharmony_ci+     * Get model input tensors.
254be168c0dSopenharmony_ci+     * @returns { MSTensor[] } the MSTensor array of the inputs.
255be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
256be168c0dSopenharmony_ci+     * @stagemodelonly
257be168c0dSopenharmony_ci+     * @since 10
258be168c0dSopenharmony_ci+     */
259be168c0dSopenharmony_ci+    getInputs(): MSTensor[];
260be168c0dSopenharmony_ci+
261be168c0dSopenharmony_ci+    /**
262be168c0dSopenharmony_ci+     * Infer model
263be168c0dSopenharmony_ci+     * @param { MSTensor[] } inputs - indicates the MSTensor array of the inputs.
264be168c0dSopenharmony_ci+     * @param { callback: Callback<MSTensor[]> }  callback - the callback of MSTensor array.
265be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
266be168c0dSopenharmony_ci+     * @stagemodelonly
267be168c0dSopenharmony_ci+     * @since 10
268be168c0dSopenharmony_ci+     */
269be168c0dSopenharmony_ci+    predict(inputs: MSTensor[], callback: Callback<MSTensor[]>): void;
270be168c0dSopenharmony_ci+
271be168c0dSopenharmony_ci+    /**
272be168c0dSopenharmony_ci+     * Infer model
273be168c0dSopenharmony_ci+     * @param { MSTensor[] } inputs - indicates the MSTensor array of the inputs.
274be168c0dSopenharmony_ci+     * @returns { Promise<MSTensor[]> } the promise returned by the function.
275be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
276be168c0dSopenharmony_ci+     * @stagemodelonly
277be168c0dSopenharmony_ci+     * @since 10
278be168c0dSopenharmony_ci+     */
279be168c0dSopenharmony_ci+    predict(inputs: MSTensor[]): Promise<MSTensor[]>;
280be168c0dSopenharmony_ci+
281be168c0dSopenharmony_ci+    /**
282be168c0dSopenharmony_ci+     * resize model input
283be168c0dSopenharmony_ci+     * @param { MSTensor[] } inputs - indicates the MSTensor array of the inputs.
284be168c0dSopenharmony_ci+     * @param { Array<Array<number>> } dims - indicates the target new shape array
285be168c0dSopenharmony_ci+     * @returns { boolean } the boolean result if the resize operation is successful
286be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
287be168c0dSopenharmony_ci+     * @stagemodelonly
288be168c0dSopenharmony_ci+     * @since 10
289be168c0dSopenharmony_ci+     */
290be168c0dSopenharmony_ci+    resize(inputs: MSTensor[], dims: Array<Array<number>>): boolean;
291be168c0dSopenharmony_ci+
292be168c0dSopenharmony_ci+    /**
293be168c0dSopenharmony_ci+     * Train model by step
294be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
295be168c0dSopenharmony_ci+     * @returns { boolean } the boolean result if the runStep operation is successful
296be168c0dSopenharmony_ci+     * @stagemodelonly
297be168c0dSopenharmony_ci+     * @since 11
298be168c0dSopenharmony_ci+     */
299be168c0dSopenharmony_ci+    runStep(): boolean;
300be168c0dSopenharmony_ci+
301be168c0dSopenharmony_ci+    /**
302be168c0dSopenharmony_ci+     * Obtain all weights of the model
303be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
304be168c0dSopenharmony_ci+     * @returns { MSTensor[] } the weight tensors of the model
305be168c0dSopenharmony_ci+     * @stagemodelonly
306be168c0dSopenharmony_ci+     * @since 11
307be168c0dSopenharmony_ci+     */
308be168c0dSopenharmony_ci+    getWeights(): MSTensor[];
309be168c0dSopenharmony_ci+
310be168c0dSopenharmony_ci+    /**
311be168c0dSopenharmony_ci+     * Update weights of the model
312be168c0dSopenharmony_ci+     * @param { MSTensor[] } weights - indicates the MSTensor array of the inputs
313be168c0dSopenharmony_ci+     * @returns { boolean } the boolean result if updating weights operation is successful
314be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
315be168c0dSopenharmony_ci+     * @stagemodelonly
316be168c0dSopenharmony_ci+     * @since 11
317be168c0dSopenharmony_ci+     */
318be168c0dSopenharmony_ci+    updateWeights(weights: MSTensor[]): boolean;
319be168c0dSopenharmony_ci+
320be168c0dSopenharmony_ci+    /**
321be168c0dSopenharmony_ci+     * Setup training with virtual batches
322be168c0dSopenharmony_ci+     * @param { number } virtualBatchMultiplier - virtual batch multiplier, use any number < 1 to disable
323be168c0dSopenharmony_ci+     * @param { number } lr - learning rate to use for virtual batch, -1 for internal configuration
324be168c0dSopenharmony_ci+     * @param { number } momentum - batch norm momentum to use for virtual batch, -1 for internal configuration
325be168c0dSopenharmony_ci+     * @returns { boolean } the boolean result if the operation is successful
326be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
327be168c0dSopenharmony_ci+     * @stagemodelonly
328be168c0dSopenharmony_ci+     * @since 11
329be168c0dSopenharmony_ci+     */
330be168c0dSopenharmony_ci+    setupVirtualBatch(virtualBatchMultiplier: number, lr: number, momentum: number): boolean;
331be168c0dSopenharmony_ci+
332be168c0dSopenharmony_ci+    /**
333be168c0dSopenharmony_ci+     * Export train model to file
334be168c0dSopenharmony_ci+     * @param { string } modelFile - model file path.
335be168c0dSopenharmony_ci+     * @param { ?QuantizationType } quantizationType - the quantization type, default NO_QUANT.
336be168c0dSopenharmony_ci+     * @param { ?boolean } exportInferenceOnly - whether to export a inference only model, default true.
337be168c0dSopenharmony_ci+     * @param { ?string[] } outputTensorName - the set of name of output tensor the exported inference model,
338be168c0dSopenharmony_ci+     * @returns { boolean } - the boolean result if the operation is successful
339be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
340be168c0dSopenharmony_ci+     * @stagemodelonly
341be168c0dSopenharmony_ci+     * @since 11
342be168c0dSopenharmony_ci+     */
343be168c0dSopenharmony_ci+    exportModel(
344be168c0dSopenharmony_ci+      modelFile: string,
345be168c0dSopenharmony_ci+      quantizationType?: QuantizationType,
346be168c0dSopenharmony_ci+      exportInferenceOnly?: boolean,
347be168c0dSopenharmony_ci+      outputTensorName?: string[]): boolean;
348be168c0dSopenharmony_ci+
349be168c0dSopenharmony_ci+    /**
350be168c0dSopenharmony_ci+     * Export model's weights, which can be used in micro only. Only valid for Lite Train
351be168c0dSopenharmony_ci+     * @param { string } weightFile - weight file path
352be168c0dSopenharmony_ci+     * @param { ?boolean } isInference - whether to export weights from inference model, only support this is `true` for now, default true
353be168c0dSopenharmony_ci+     * @param { ?boolean } enableFp16 - float-weight is whether to be saved in float16 format, default false
354be168c0dSopenharmony_ci+     * @param { ?string[] } changeableWeightsName - changeable weights name
355be168c0dSopenharmony_ci+     * @returns { boolean } the boolean result if the operation is successful
356be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
357be168c0dSopenharmony_ci+     * @stagemodelonly
358be168c0dSopenharmony_ci+     * @since 11
359be168c0dSopenharmony_ci+     */
360be168c0dSopenharmony_ci+    exportWeightsCollaborateWithMicro(
361be168c0dSopenharmony_ci+      weightFile: string,
362be168c0dSopenharmony_ci+      isInference?: boolean,
363be168c0dSopenharmony_ci+      enableFp16?: boolean,
364be168c0dSopenharmony_ci+      changeableWeightsName?: string[]): boolean;
365be168c0dSopenharmony_ci+  }
366be168c0dSopenharmony_ci+
367be168c0dSopenharmony_ci+  export enum QuantizationType {
368be168c0dSopenharmony_ci+    /**
369be168c0dSopenharmony_ci+     * No quantization.
370be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
371be168c0dSopenharmony_ci+     * @since 11
372be168c0dSopenharmony_ci+     */
373be168c0dSopenharmony_ci+    NO_QUANT = 0,
374be168c0dSopenharmony_ci+    /**
375be168c0dSopenharmony_ci+     * Weight quantization.
376be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
377be168c0dSopenharmony_ci+     * @since 11
378be168c0dSopenharmony_ci+     */
379be168c0dSopenharmony_ci+    WEIGHT_QUANT = 1,
380be168c0dSopenharmony_ci+    /**
381be168c0dSopenharmony_ci+     * Full quantization.
382be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
383be168c0dSopenharmony_ci+     * @since 11
384be168c0dSopenharmony_ci+     */
385be168c0dSopenharmony_ci+    FULL_QUANT = 2,
386be168c0dSopenharmony_ci+  }
387be168c0dSopenharmony_ci+  export enum OptimizationLevel {
388be168c0dSopenharmony_ci+    /**
389be168c0dSopenharmony_ci+     * Do not change
390be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
391be168c0dSopenharmony_ci+     * @since 11
392be168c0dSopenharmony_ci+     */
393be168c0dSopenharmony_ci+    O0 = 0,
394be168c0dSopenharmony_ci+    /**
395be168c0dSopenharmony_ci+     * Cast network to float16, keep batchnorm and loss in float32
396be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
397be168c0dSopenharmony_ci+     * @since 11
398be168c0dSopenharmony_ci+     */
399be168c0dSopenharmony_ci+    O2 = 2,
400be168c0dSopenharmony_ci+    /**
401be168c0dSopenharmony_ci+     * Cast network to float16, including bacthnorm
402be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
403be168c0dSopenharmony_ci+     * @since 11
404be168c0dSopenharmony_ci+     */
405be168c0dSopenharmony_ci+    O3 = 3,
406be168c0dSopenharmony_ci+    /**
407be168c0dSopenharmony_ci+     * Choose optimization based on device
408be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
409be168c0dSopenharmony_ci+     * @since 11
410be168c0dSopenharmony_ci+     */
411be168c0dSopenharmony_ci+    AUTO = 4,
412be168c0dSopenharmony_ci+  }
413be168c0dSopenharmony_ci+
414be168c0dSopenharmony_ci+  interface TrainCfg {
415be168c0dSopenharmony_ci+    /**
416be168c0dSopenharmony_ci+     * Array of loss name
417be168c0dSopenharmony_ci+     * @type {?string[]}
418be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
419be168c0dSopenharmony_ci+     * @since 11
420be168c0dSopenharmony_ci+     */
421be168c0dSopenharmony_ci+    lossName?: string[],
422be168c0dSopenharmony_ci+    /**
423be168c0dSopenharmony_ci+     * Train optimization level
424be168c0dSopenharmony_ci+     * @type {?OptimizationLevel}
425be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
426be168c0dSopenharmony_ci+     * @since 11
427be168c0dSopenharmony_ci+     */
428be168c0dSopenharmony_ci+    optimizationLevel?: OptimizationLevel,
429be168c0dSopenharmony_ci+  }
430be168c0dSopenharmony_ci+
431be168c0dSopenharmony_ci+  /**
432be168c0dSopenharmony_ci+   * Provides the device configurations
433be168c0dSopenharmony_ci+   * @typedef Context
434be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
435be168c0dSopenharmony_ci+   * @stagemodelonly
436be168c0dSopenharmony_ci+   * @since 10
437be168c0dSopenharmony_ci+   */
438be168c0dSopenharmony_ci+  interface Context {
439be168c0dSopenharmony_ci+    /**
440be168c0dSopenharmony_ci+     * The target device
441be168c0dSopenharmony_ci+     * @type {?string[]}
442be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
443be168c0dSopenharmony_ci+     * @since 10
444be168c0dSopenharmony_ci+     */
445be168c0dSopenharmony_ci+    target?: string[];
446be168c0dSopenharmony_ci+    /**
447be168c0dSopenharmony_ci+     * The cpu device information
448be168c0dSopenharmony_ci+     * @type {?CpuDevice}
449be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
450be168c0dSopenharmony_ci+     * @since 10
451be168c0dSopenharmony_ci+     */
452be168c0dSopenharmony_ci+    cpu?: CpuDevice;
453be168c0dSopenharmony_ci+    /**
454be168c0dSopenharmony_ci+     * The NNRT device information
455be168c0dSopenharmony_ci+     * @type {?NNRTDevice}
456be168c0dSopenharmony_ci+     * @since 10
457be168c0dSopenharmony_ci+     */
458be168c0dSopenharmony_ci+    nnrt?: NNRTDevice;
459be168c0dSopenharmony_ci+  }
460be168c0dSopenharmony_ci+
461be168c0dSopenharmony_ci+  /**
462be168c0dSopenharmony_ci+   * Provides the CPU device info
463be168c0dSopenharmony_ci+   * @typedef CpuDevice
464be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
465be168c0dSopenharmony_ci+   * @stagemodelonly
466be168c0dSopenharmony_ci+   * @since 10
467be168c0dSopenharmony_ci+   */
468be168c0dSopenharmony_ci+  interface CpuDevice {
469be168c0dSopenharmony_ci+    /**
470be168c0dSopenharmony_ci+     * The thread num
471be168c0dSopenharmony_ci+     * @type {?number}
472be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
473be168c0dSopenharmony_ci+     * @since 10
474be168c0dSopenharmony_ci+     */
475be168c0dSopenharmony_ci+    threadNum?: number;
476be168c0dSopenharmony_ci+    /**
477be168c0dSopenharmony_ci+     * The thread affinity mode
478be168c0dSopenharmony_ci+     * @type {?ThreadAffinityMode}
479be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
480be168c0dSopenharmony_ci+     * @since 10
481be168c0dSopenharmony_ci+     */
482be168c0dSopenharmony_ci+    threadAffinityMode?: ThreadAffinityMode;
483be168c0dSopenharmony_ci+    /**
484be168c0dSopenharmony_ci+     * The thread affinity core list
485be168c0dSopenharmony_ci+     * @type {?number[]}
486be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
487be168c0dSopenharmony_ci+     * @since 10
488be168c0dSopenharmony_ci+     */
489be168c0dSopenharmony_ci+    threadAffinityCoreList?: number[];
490be168c0dSopenharmony_ci+    /**
491be168c0dSopenharmony_ci+     * The precision mode
492be168c0dSopenharmony_ci+     * @type {?string}
493be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
494be168c0dSopenharmony_ci+     * @since 10
495be168c0dSopenharmony_ci+     */
496be168c0dSopenharmony_ci+    precisionMode?: string;
497be168c0dSopenharmony_ci+  }
498be168c0dSopenharmony_ci+
499be168c0dSopenharmony_ci+  export enum PerformanceMode {
500be168c0dSopenharmony_ci+    /**
501be168c0dSopenharmony_ci+     * No performance mode preference
502be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
503be168c0dSopenharmony_ci+     * @since 11
504be168c0dSopenharmony_ci+     */
505be168c0dSopenharmony_ci+    PERFORMANCE_NONE = 0,
506be168c0dSopenharmony_ci+    /**
507be168c0dSopenharmony_ci+     * Low power consumption mode
508be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
509be168c0dSopenharmony_ci+     * @since 11
510be168c0dSopenharmony_ci+     */
511be168c0dSopenharmony_ci+    PERFORMANCE_LOW = 1,
512be168c0dSopenharmony_ci+    /**
513be168c0dSopenharmony_ci+     * Medium performance mode
514be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
515be168c0dSopenharmony_ci+     * @since 11
516be168c0dSopenharmony_ci+     */
517be168c0dSopenharmony_ci+    PERFORMANCE_MEDIUM = 2,
518be168c0dSopenharmony_ci+    /**
519be168c0dSopenharmony_ci+     * High performance mode
520be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
521be168c0dSopenharmony_ci+     * @since 11
522be168c0dSopenharmony_ci+     */
523be168c0dSopenharmony_ci+    PERFORMANCE_HIGH = 3,
524be168c0dSopenharmony_ci+    /**
525be168c0dSopenharmony_ci+     * Ultimate performance mode
526be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
527be168c0dSopenharmony_ci+     * @since 11
528be168c0dSopenharmony_ci+     */
529be168c0dSopenharmony_ci+    PERFORMANCE_EXTREME = 4,
530be168c0dSopenharmony_ci+  }
531be168c0dSopenharmony_ci+
532be168c0dSopenharmony_ci+  export enum Priority {
533be168c0dSopenharmony_ci+    /**
534be168c0dSopenharmony_ci+     * No priority preference
535be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
536be168c0dSopenharmony_ci+     * @since 11
537be168c0dSopenharmony_ci+     */
538be168c0dSopenharmony_ci+    PRIORITY_NONE = 0,
539be168c0dSopenharmony_ci+    /**
540be168c0dSopenharmony_ci+     * Low priority
541be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
542be168c0dSopenharmony_ci+     * @since 11
543be168c0dSopenharmony_ci+     */
544be168c0dSopenharmony_ci+    PRIORITY_LOW = 1,
545be168c0dSopenharmony_ci+    /**
546be168c0dSopenharmony_ci+     * Medium priority
547be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
548be168c0dSopenharmony_ci+     * @since 11
549be168c0dSopenharmony_ci+     */
550be168c0dSopenharmony_ci+    PRIORITY_MEDIUM = 2,
551be168c0dSopenharmony_ci+    /**
552be168c0dSopenharmony_ci+     * High priority
553be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
554be168c0dSopenharmony_ci+     * @since 11
555be168c0dSopenharmony_ci+     */
556be168c0dSopenharmony_ci+    PRIORITY_HIGH = 3,
557be168c0dSopenharmony_ci+  }
558be168c0dSopenharmony_ci+
559be168c0dSopenharmony_ci+  interface Extension {
560be168c0dSopenharmony_ci+    /**
561be168c0dSopenharmony_ci+     * Extension name
562be168c0dSopenharmony_ci+     * @type {?string}
563be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
564be168c0dSopenharmony_ci+     * @since 11
565be168c0dSopenharmony_ci+     */
566be168c0dSopenharmony_ci+    name: String,
567be168c0dSopenharmony_ci+    /**
568be168c0dSopenharmony_ci+     * Extension array buffer
569be168c0dSopenharmony_ci+     * @type {?ArrayBuffer}
570be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
571be168c0dSopenharmony_ci+     * @since 11
572be168c0dSopenharmony_ci+     */
573be168c0dSopenharmony_ci+    value: ArrayBuffer
574be168c0dSopenharmony_ci+  }
575be168c0dSopenharmony_ci+
576be168c0dSopenharmony_ci+  export enum NNRTDeviceType {
577be168c0dSopenharmony_ci+    /**
578be168c0dSopenharmony_ci+     * Devices that are not CPU, GPU, or dedicated accelerator
579be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
580be168c0dSopenharmony_ci+     * @since 11
581be168c0dSopenharmony_ci+     */
582be168c0dSopenharmony_ci+    NNRTDEVICE_OTHERS = 0,
583be168c0dSopenharmony_ci+
584be168c0dSopenharmony_ci+    /**
585be168c0dSopenharmony_ci+     * CPU device
586be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
587be168c0dSopenharmony_ci+     * @since 11
588be168c0dSopenharmony_ci+     */
589be168c0dSopenharmony_ci+    NNRTDEVICE_CPU = 1,
590be168c0dSopenharmony_ci+
591be168c0dSopenharmony_ci+    /**
592be168c0dSopenharmony_ci+     * GPU device
593be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
594be168c0dSopenharmony_ci+     * @since 11
595be168c0dSopenharmony_ci+     */
596be168c0dSopenharmony_ci+    NNRTDEVICE_GPU = 2,
597be168c0dSopenharmony_ci+
598be168c0dSopenharmony_ci+    /**
599be168c0dSopenharmony_ci+     * Dedicated hardware accelerator
600be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
601be168c0dSopenharmony_ci+     * @since 11
602be168c0dSopenharmony_ci+     */
603be168c0dSopenharmony_ci+    NNRTDEVICE_ACCELERATOR = 3,
604be168c0dSopenharmony_ci+  }
605be168c0dSopenharmony_ci+
606be168c0dSopenharmony_ci+  interface NNRTDeviceDesc {
607be168c0dSopenharmony_ci+    /**
608be168c0dSopenharmony_ci+     * Get device id
609be168c0dSopenharmony_ci+     * @returns { number } the number of device id
610be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
611be168c0dSopenharmony_ci+     * @stagemodelonly
612be168c0dSopenharmony_ci+     * @since 11
613be168c0dSopenharmony_ci+     */
614be168c0dSopenharmony_ci+    deviceID() : number;
615be168c0dSopenharmony_ci+    /**
616be168c0dSopenharmony_ci+     * Get device type.
617be168c0dSopenharmony_ci+     * @returns { NNRTDeviceType } the device type
618be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
619be168c0dSopenharmony_ci+     * @stagemodelonly
620be168c0dSopenharmony_ci+     * @since 11
621be168c0dSopenharmony_ci+     */
622be168c0dSopenharmony_ci+    deviceType() : NNRTDeviceType;
623be168c0dSopenharmony_ci+    /**
624be168c0dSopenharmony_ci+     * Get device name.
625be168c0dSopenharmony_ci+     * @returns { string } device name
626be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
627be168c0dSopenharmony_ci+     * @stagemodelonly
628be168c0dSopenharmony_ci+     * @since 11
629be168c0dSopenharmony_ci+     */
630be168c0dSopenharmony_ci+    deviceName() : string;
631be168c0dSopenharmony_ci+  }
632be168c0dSopenharmony_ci+
633be168c0dSopenharmony_ci+  /**
634be168c0dSopenharmony_ci+   * Obtain the all device descriptions in NNRT.
635be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
636be168c0dSopenharmony_ci+   * @returns { NNRTDeviceDesc[] } the array of NNRTDeviceDecs
637be168c0dSopenharmony_ci+   * @since 11
638be168c0dSopenharmony_ci+   */
639be168c0dSopenharmony_ci+  function getAllNNRTDeviceDescs() : NNRTDeviceDesc[];
640be168c0dSopenharmony_ci+
641be168c0dSopenharmony_ci+  /**
642be168c0dSopenharmony_ci+   * Provides the NNRT device info
643be168c0dSopenharmony_ci+   * @typedef NNRTDevice
644be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
645be168c0dSopenharmony_ci+   * @stagemodelonly
646be168c0dSopenharmony_ci+   * @since 10
647be168c0dSopenharmony_ci+   */
648be168c0dSopenharmony_ci+
649be168c0dSopenharmony_ci+  interface NNRTDevice {
650be168c0dSopenharmony_ci+    /**
651be168c0dSopenharmony_ci+     * NNRT device id.
652be168c0dSopenharmony_ci+     * @type {?number}
653be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
654be168c0dSopenharmony_ci+     * @since 11
655be168c0dSopenharmony_ci+     */
656be168c0dSopenharmony_ci+    deviceID?: number,
657be168c0dSopenharmony_ci+    /**
658be168c0dSopenharmony_ci+     * NNRT device performance mode.
659be168c0dSopenharmony_ci+     * @type {?PerformanceMode}
660be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
661be168c0dSopenharmony_ci+     * @since 11
662be168c0dSopenharmony_ci+     */
663be168c0dSopenharmony_ci+    performanceMode?: PerformanceMode,
664be168c0dSopenharmony_ci+    /**
665be168c0dSopenharmony_ci+     * NNRT device priority.
666be168c0dSopenharmony_ci+     * @type {?Priority}
667be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
668be168c0dSopenharmony_ci+     * @since 11
669be168c0dSopenharmony_ci+     */
670be168c0dSopenharmony_ci+    priority?: Priority,
671be168c0dSopenharmony_ci+    /**
672be168c0dSopenharmony_ci+     * NNRT device extension array.
673be168c0dSopenharmony_ci+     * @type {?Extension[]}
674be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
675be168c0dSopenharmony_ci+     * @since 11
676be168c0dSopenharmony_ci+     */
677be168c0dSopenharmony_ci+    extensions?: Extension[],
678be168c0dSopenharmony_ci+  }
679be168c0dSopenharmony_ci+
680be168c0dSopenharmony_ci+  /**
681be168c0dSopenharmony_ci+   * Enum for provides CPU thread affinity mode
682be168c0dSopenharmony_ci+   * @enum {number}
683be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
684be168c0dSopenharmony_ci+   * @stagemodelonly
685be168c0dSopenharmony_ci+   * @since 10
686be168c0dSopenharmony_ci+   */
687be168c0dSopenharmony_ci+  export enum ThreadAffinityMode {
688be168c0dSopenharmony_ci+    /**
689be168c0dSopenharmony_ci+     * Thread affinity mode is no bind.
690be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
691be168c0dSopenharmony_ci+     * @since 10
692be168c0dSopenharmony_ci+     */
693be168c0dSopenharmony_ci+    NO_AFFINITIES = 0,
694be168c0dSopenharmony_ci+
695be168c0dSopenharmony_ci+    /**
696be168c0dSopenharmony_ci+     * Thread affinity mode is big cores first
697be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
698be168c0dSopenharmony_ci+     * @since 10
699be168c0dSopenharmony_ci+     */
700be168c0dSopenharmony_ci+    BIG_CORES_FIRST = 1,
701be168c0dSopenharmony_ci+
702be168c0dSopenharmony_ci+    /**
703be168c0dSopenharmony_ci+     * Thread affinity mode is little cores first
704be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
705be168c0dSopenharmony_ci+     * @since 10
706be168c0dSopenharmony_ci+     */
707be168c0dSopenharmony_ci+    LITTLE_CORES_FIRST = 2,
708be168c0dSopenharmony_ci+  }
709be168c0dSopenharmony_ci+
710be168c0dSopenharmony_ci+  /**
711be168c0dSopenharmony_ci+   * Provides MSTensor definition
712be168c0dSopenharmony_ci+   * @typedef MSTensor
713be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
714be168c0dSopenharmony_ci+   * @stagemodelonly
715be168c0dSopenharmony_ci+   * @since 10
716be168c0dSopenharmony_ci+   */
717be168c0dSopenharmony_ci+  interface MSTensor {
718be168c0dSopenharmony_ci+    /**
719be168c0dSopenharmony_ci+     * The name of the tensor.
720be168c0dSopenharmony_ci+     * @type {string}
721be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
722be168c0dSopenharmony_ci+     * @since 10
723be168c0dSopenharmony_ci+     */
724be168c0dSopenharmony_ci+    name: string;
725be168c0dSopenharmony_ci+    /**
726be168c0dSopenharmony_ci+     * The shape of the tensor.
727be168c0dSopenharmony_ci+     * @type {number[]}
728be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
729be168c0dSopenharmony_ci+     * @since 10
730be168c0dSopenharmony_ci+     */
731be168c0dSopenharmony_ci+    shape: number[];
732be168c0dSopenharmony_ci+    /**
733be168c0dSopenharmony_ci+     * The number of elements in the tensor.
734be168c0dSopenharmony_ci+     * @type {number}
735be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
736be168c0dSopenharmony_ci+     * @since 10
737be168c0dSopenharmony_ci+     */
738be168c0dSopenharmony_ci+    elementNum: number;
739be168c0dSopenharmony_ci+    /**
740be168c0dSopenharmony_ci+     * The data size of the tensor.
741be168c0dSopenharmony_ci+     * @type {number}
742be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
743be168c0dSopenharmony_ci+     * @since 10
744be168c0dSopenharmony_ci+     */
745be168c0dSopenharmony_ci+    dataSize: number;
746be168c0dSopenharmony_ci+    /**
747be168c0dSopenharmony_ci+     * The data type of the tensor.
748be168c0dSopenharmony_ci+     * @type {DataType}
749be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
750be168c0dSopenharmony_ci+     * @since 10
751be168c0dSopenharmony_ci+     */
752be168c0dSopenharmony_ci+    dtype: DataType;
753be168c0dSopenharmony_ci+    /**
754be168c0dSopenharmony_ci+     * The format of the tensor.
755be168c0dSopenharmony_ci+     * @type {Format}
756be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
757be168c0dSopenharmony_ci+     * @since 10
758be168c0dSopenharmony_ci+     */
759be168c0dSopenharmony_ci+    format: Format;
760be168c0dSopenharmony_ci+
761be168c0dSopenharmony_ci+    /**
762be168c0dSopenharmony_ci+     * Get MSTensor data
763be168c0dSopenharmony_ci+     * @returns { ArrayBuffer } the data of tensor
764be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
765be168c0dSopenharmony_ci+     * @stagemodelonly
766be168c0dSopenharmony_ci+     * @since 10
767be168c0dSopenharmony_ci+     */
768be168c0dSopenharmony_ci+    getData(): ArrayBuffer;
769be168c0dSopenharmony_ci+
770be168c0dSopenharmony_ci+    /**
771be168c0dSopenharmony_ci+     * Set MSTensor data
772be168c0dSopenharmony_ci+     * @param { ArrayBuffer } inputArray - indicates the buffer of tensor
773be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
774be168c0dSopenharmony_ci+     * @stagemodelonly
775be168c0dSopenharmony_ci+     * @since 10
776be168c0dSopenharmony_ci+     */
777be168c0dSopenharmony_ci+    setData(inputArray: ArrayBuffer): void;
778be168c0dSopenharmony_ci+  }
779be168c0dSopenharmony_ci+
780be168c0dSopenharmony_ci+  /**
781be168c0dSopenharmony_ci+   * Enum for provides MSTensor data type
782be168c0dSopenharmony_ci+   * @enum {number}
783be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
784be168c0dSopenharmony_ci+   * @stagemodelonly
785be168c0dSopenharmony_ci+   * @since 10
786be168c0dSopenharmony_ci+   */
787be168c0dSopenharmony_ci+  export enum DataType {
788be168c0dSopenharmony_ci+    /**
789be168c0dSopenharmony_ci+     * data type is unknown
790be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
791be168c0dSopenharmony_ci+     * @since 10
792be168c0dSopenharmony_ci+     */
793be168c0dSopenharmony_ci+    TYPE_UNKNOWN = 0,
794be168c0dSopenharmony_ci+    /**
795be168c0dSopenharmony_ci+     * data type is int8
796be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
797be168c0dSopenharmony_ci+     * @since 10
798be168c0dSopenharmony_ci+     */
799be168c0dSopenharmony_ci+    NUMBER_TYPE_INT8 = 32,
800be168c0dSopenharmony_ci+    /**
801be168c0dSopenharmony_ci+     * data type is int16
802be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
803be168c0dSopenharmony_ci+     * @since 10
804be168c0dSopenharmony_ci+     */
805be168c0dSopenharmony_ci+    NUMBER_TYPE_INT16 = 33,
806be168c0dSopenharmony_ci+    /**
807be168c0dSopenharmony_ci+     * data type is int32
808be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
809be168c0dSopenharmony_ci+     * @since 10
810be168c0dSopenharmony_ci+     */
811be168c0dSopenharmony_ci+    NUMBER_TYPE_INT32 = 34,
812be168c0dSopenharmony_ci+    /**
813be168c0dSopenharmony_ci+     * data type is int64
814be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
815be168c0dSopenharmony_ci+     * @since 10
816be168c0dSopenharmony_ci+     */
817be168c0dSopenharmony_ci+    NUMBER_TYPE_INT64 = 35,
818be168c0dSopenharmony_ci+    /**
819be168c0dSopenharmony_ci+     * data type is uint8
820be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
821be168c0dSopenharmony_ci+     * @since 10
822be168c0dSopenharmony_ci+     */
823be168c0dSopenharmony_ci+    NUMBER_TYPE_UINT8 = 37,
824be168c0dSopenharmony_ci+    /**
825be168c0dSopenharmony_ci+     * data type is uint16
826be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
827be168c0dSopenharmony_ci+     * @since 10
828be168c0dSopenharmony_ci+     */
829be168c0dSopenharmony_ci+    NUMBER_TYPE_UINT16 = 38,
830be168c0dSopenharmony_ci+    /**
831be168c0dSopenharmony_ci+     * data type is uint32
832be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
833be168c0dSopenharmony_ci+     * @since 10
834be168c0dSopenharmony_ci+     */
835be168c0dSopenharmony_ci+    NUMBER_TYPE_UINT32 = 39,
836be168c0dSopenharmony_ci+    /**
837be168c0dSopenharmony_ci+     * data type is uint64
838be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
839be168c0dSopenharmony_ci+     * @since 10
840be168c0dSopenharmony_ci+     */
841be168c0dSopenharmony_ci+    NUMBER_TYPE_UINT64 = 40,
842be168c0dSopenharmony_ci+    /**
843be168c0dSopenharmony_ci+     * data type is float16
844be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
845be168c0dSopenharmony_ci+     * @since 10
846be168c0dSopenharmony_ci+     */
847be168c0dSopenharmony_ci+    NUMBER_TYPE_FLOAT16 = 42,
848be168c0dSopenharmony_ci+    /**
849be168c0dSopenharmony_ci+     * data type is float32
850be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
851be168c0dSopenharmony_ci+     * @since 10
852be168c0dSopenharmony_ci+     */
853be168c0dSopenharmony_ci+    NUMBER_TYPE_FLOAT32 = 43,
854be168c0dSopenharmony_ci+    /**
855be168c0dSopenharmony_ci+     * data type is float64
856be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
857be168c0dSopenharmony_ci+     * @since 10
858be168c0dSopenharmony_ci+     */
859be168c0dSopenharmony_ci+    NUMBER_TYPE_FLOAT64 = 44,
860be168c0dSopenharmony_ci+  }
861be168c0dSopenharmony_ci+
862be168c0dSopenharmony_ci+  /**
863be168c0dSopenharmony_ci+   * Enum for provides MSTensor format
864be168c0dSopenharmony_ci+   * @enum {number}
865be168c0dSopenharmony_ci+   * @syscap SystemCapability.AI.MindSporeLite
866be168c0dSopenharmony_ci+   * @stagemodelonly
867be168c0dSopenharmony_ci+   * @since 10
868be168c0dSopenharmony_ci+   */
869be168c0dSopenharmony_ci+  export enum Format {
870be168c0dSopenharmony_ci+    /**
871be168c0dSopenharmony_ci+     * data format is default
872be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
873be168c0dSopenharmony_ci+     * @since 10
874be168c0dSopenharmony_ci+     */
875be168c0dSopenharmony_ci+    DEFAULT_FORMAT = -1,
876be168c0dSopenharmony_ci+    /**
877be168c0dSopenharmony_ci+     * data format is NCHW
878be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
879be168c0dSopenharmony_ci+     * @since 10
880be168c0dSopenharmony_ci+     */
881be168c0dSopenharmony_ci+    NCHW = 0,
882be168c0dSopenharmony_ci+    /**
883be168c0dSopenharmony_ci+     * data format is NHWC
884be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
885be168c0dSopenharmony_ci+     * @since 10
886be168c0dSopenharmony_ci+     */
887be168c0dSopenharmony_ci+    NHWC = 1,
888be168c0dSopenharmony_ci+    /**
889be168c0dSopenharmony_ci+     * data format is NHWC4
890be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
891be168c0dSopenharmony_ci+     * @since 10
892be168c0dSopenharmony_ci+     */
893be168c0dSopenharmony_ci+    NHWC4 = 2,
894be168c0dSopenharmony_ci+    /**
895be168c0dSopenharmony_ci+     * data format is HWKC
896be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
897be168c0dSopenharmony_ci+     * @since 10
898be168c0dSopenharmony_ci+     */
899be168c0dSopenharmony_ci+    HWKC = 3,
900be168c0dSopenharmony_ci+    /**
901be168c0dSopenharmony_ci+     * data format is HWCK
902be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
903be168c0dSopenharmony_ci+     * @since 10
904be168c0dSopenharmony_ci+     */
905be168c0dSopenharmony_ci+    HWCK = 4,
906be168c0dSopenharmony_ci+    /**
907be168c0dSopenharmony_ci+     * data format is KCHW
908be168c0dSopenharmony_ci+     * @syscap SystemCapability.AI.MindSporeLite
909be168c0dSopenharmony_ci+     * @since 10
910be168c0dSopenharmony_ci+     */
911be168c0dSopenharmony_ci+    KCHW = 5,
912be168c0dSopenharmony_ci+  }
913be168c0dSopenharmony_ci+}
914be168c0dSopenharmony_ci+export default mindSporeLite;
915be168c0dSopenharmony_cidiff --git a/include/js_api/common_napi.h b/include/js_api/common_napi.h
916be168c0dSopenharmony_cinew file mode 100644
917be168c0dSopenharmony_ciindex 00000000..b857ac85
918be168c0dSopenharmony_ci--- /dev/null
919be168c0dSopenharmony_ci+++ b/include/js_api/common_napi.h
920be168c0dSopenharmony_ci@@ -0,0 +1,196 @@
921be168c0dSopenharmony_ci+/**
922be168c0dSopenharmony_ci+ * Copyright (C) 2023 Huawei Device Co., Ltd.
923be168c0dSopenharmony_ci+ * Licensed under the Apache License, Version 2.0 (the "License");
924be168c0dSopenharmony_ci+ * you may not use this file except in compliance with the License.
925be168c0dSopenharmony_ci+ * You may obtain a copy of the License at
926be168c0dSopenharmony_ci+ *
927be168c0dSopenharmony_ci+ *     http://www.apache.org/licenses/LICENSE-2.0
928be168c0dSopenharmony_ci+ *
929be168c0dSopenharmony_ci+ * Unless required by applicable law or agreed to in writing, software
930be168c0dSopenharmony_ci+ * distributed under the License is distributed on an "AS IS" BASIS,
931be168c0dSopenharmony_ci+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
932be168c0dSopenharmony_ci+ * See the License for the specific language governing permissions and
933be168c0dSopenharmony_ci+ * limitations under the License.
934be168c0dSopenharmony_ci+ */
935be168c0dSopenharmony_ci+
936be168c0dSopenharmony_ci+#ifndef MINDSPORE_INCLUDE_JS_API_COMMON_NAPI_H
937be168c0dSopenharmony_ci+#define MINDSPORE_INCLUDE_JS_API_COMMON_NAPI_H
938be168c0dSopenharmony_ci+
939be168c0dSopenharmony_ci+#include <string>
940be168c0dSopenharmony_ci+#include <fstream>
941be168c0dSopenharmony_ci+#include "napi/native_api.h"
942be168c0dSopenharmony_ci+#include "napi/native_node_api.h"
943be168c0dSopenharmony_ci+#include "ms_errors.h"
944be168c0dSopenharmony_ci+#include "include/api/types.h"
945be168c0dSopenharmony_ci+
946be168c0dSopenharmony_ci+namespace mindspore {
947be168c0dSopenharmony_ci+
948be168c0dSopenharmony_ci+class CommonNapi {
949be168c0dSopenharmony_ci+ public:
950be168c0dSopenharmony_ci+  CommonNapi() = delete;
951be168c0dSopenharmony_ci+  ~CommonNapi() = delete;
952be168c0dSopenharmony_ci+
953be168c0dSopenharmony_ci+  static std::string getMessageByCode(int32_t &code);
954be168c0dSopenharmony_ci+  static int32_t GetPropertyInt32(napi_env env, napi_value config_obj, const std::string &type, int32_t &result);
955be168c0dSopenharmony_ci+  static int32_t GetPropertyString(napi_env env, napi_value config_obj, const std::string &type, std::string &result);
956be168c0dSopenharmony_ci+  static int32_t GetPropertyInt32Array(napi_env env, napi_value config_obj, const std::string &type,
957be168c0dSopenharmony_ci+                                       std::vector<int32_t> &result);
958be168c0dSopenharmony_ci+  static int32_t GetPropertyBigIntUint64(napi_env env, napi_value config_obj, const std::string &type,
959be168c0dSopenharmony_ci+                                         uint64_t &result);
960be168c0dSopenharmony_ci+  static int32_t GetPropertyStringArray(napi_env env, napi_value config_obj, const std::string &type,
961be168c0dSopenharmony_ci+                                        std::vector<std::string> &result);
962be168c0dSopenharmony_ci+  static int32_t GetStringArray(napi_env env, napi_value value, std::vector<std::string> &result);
963be168c0dSopenharmony_ci+  static void WriteTensorData(MSTensor tensor, std::string file_path);
964be168c0dSopenharmony_ci+  static void WriteOutputsData(const std::vector<MSTensor> outputs, std::string file_path);
965be168c0dSopenharmony_ci+};
966be168c0dSopenharmony_ci+
967be168c0dSopenharmony_ci+struct MSLiteAsyncContext {
968be168c0dSopenharmony_ci+  explicit MSLiteAsyncContext(napi_env env);
969be168c0dSopenharmony_ci+  virtual ~MSLiteAsyncContext();
970be168c0dSopenharmony_ci+  int status = SUCCESS;
971be168c0dSopenharmony_ci+  std::string errMessage = "";
972be168c0dSopenharmony_ci+};
973be168c0dSopenharmony_ci+
974be168c0dSopenharmony_ci+enum ContextThreadAffinityMode : int32_t {
975be168c0dSopenharmony_ci+  CONTEXT_AFFINITY_MODE = 0,
976be168c0dSopenharmony_ci+  CONTEXT_BIG_CORES_FIRST,
977be168c0dSopenharmony_ci+  CONTEXT_LITTLE_CORES_FIRST
978be168c0dSopenharmony_ci+};
979be168c0dSopenharmony_ci+
980be168c0dSopenharmony_ci+enum TensorFormat : int32_t {
981be168c0dSopenharmony_ci+  TENSOR_DEFAULT_FORMAT = -1,
982be168c0dSopenharmony_ci+  TENSOR_NCHW,
983be168c0dSopenharmony_ci+  TENSOR_NHWC,
984be168c0dSopenharmony_ci+  TENSOR_NHWC4,
985be168c0dSopenharmony_ci+  TENSOR_HWKC,
986be168c0dSopenharmony_ci+  TENSOR_HWCK,
987be168c0dSopenharmony_ci+  TENSOR_KCHW
988be168c0dSopenharmony_ci+};
989be168c0dSopenharmony_ci+
990be168c0dSopenharmony_ci+enum TensorDataType : int32_t {
991be168c0dSopenharmony_ci+  TENSOR_UNKNOWN = 0,
992be168c0dSopenharmony_ci+  TENSOR_INT8 = 32,
993be168c0dSopenharmony_ci+  TENSOR_INT16 = 33,
994be168c0dSopenharmony_ci+  TENSOR_INT32 = 34,
995be168c0dSopenharmony_ci+  TENSOR_INT64 = 35,
996be168c0dSopenharmony_ci+  TENSOR_UINT8 = 37,
997be168c0dSopenharmony_ci+  TENSOR_UINT16 = 38,
998be168c0dSopenharmony_ci+  TENSOR_UINT32 = 39,
999be168c0dSopenharmony_ci+  TENSOR_UINT64 = 40,
1000be168c0dSopenharmony_ci+  TENSOR_FLOAT16 = 42,
1001be168c0dSopenharmony_ci+  TENSOR_FLOAT32 = 43,
1002be168c0dSopenharmony_ci+  TENSOR_FLOAT64 = 44
1003be168c0dSopenharmony_ci+};
1004be168c0dSopenharmony_ci+
1005be168c0dSopenharmony_ci+enum ModelMode : int32_t {
1006be168c0dSopenharmony_ci+  kBuffer = 0,
1007be168c0dSopenharmony_ci+  kPath,
1008be168c0dSopenharmony_ci+  kFD,
1009be168c0dSopenharmony_ci+  // add new type here
1010be168c0dSopenharmony_ci+  kInvalidModelMode = 10,
1011be168c0dSopenharmony_ci+};
1012be168c0dSopenharmony_ci+
1013be168c0dSopenharmony_ci+enum ContextQuantizationType : int32_t {
1014be168c0dSopenharmony_ci+  NO_QUANT = 0,
1015be168c0dSopenharmony_ci+  WEIGHT_QUANT = 1,
1016be168c0dSopenharmony_ci+  FULL_QUANT = 2,
1017be168c0dSopenharmony_ci+};
1018be168c0dSopenharmony_ci+
1019be168c0dSopenharmony_ci+enum ContextOptimizationLevel : int32_t {
1020be168c0dSopenharmony_ci+  O0 = 0,
1021be168c0dSopenharmony_ci+  O2 = 2,
1022be168c0dSopenharmony_ci+  O3 = 3,
1023be168c0dSopenharmony_ci+  AUTO = 4,
1024be168c0dSopenharmony_ci+};
1025be168c0dSopenharmony_ci+
1026be168c0dSopenharmony_ci+enum ContextPerformanceMode : int32_t {
1027be168c0dSopenharmony_ci+  PERFORMANCE_NONE = 0,
1028be168c0dSopenharmony_ci+  PERFORMANCE_LOW = 1,
1029be168c0dSopenharmony_ci+  PERFORMANCE_MEDIUM = 2,
1030be168c0dSopenharmony_ci+  PERFORMANCE_HIGH = 3,
1031be168c0dSopenharmony_ci+  PERFORMANCE_EXTREME = 4,
1032be168c0dSopenharmony_ci+};
1033be168c0dSopenharmony_ci+
1034be168c0dSopenharmony_ci+enum ContextPriority : int32_t {
1035be168c0dSopenharmony_ci+  PRIORITY_NONE = 0,
1036be168c0dSopenharmony_ci+  PRIORITY_LOW = 1,
1037be168c0dSopenharmony_ci+  PRIORITY_MEDIUM = 2,
1038be168c0dSopenharmony_ci+  PRIORITY_HIGH = 3,
1039be168c0dSopenharmony_ci+};
1040be168c0dSopenharmony_ci+
1041be168c0dSopenharmony_ci+enum ContextNnrtDeviceType : int32_t {
1042be168c0dSopenharmony_ci+  NNRTDEVICE_OTHERS = 0,
1043be168c0dSopenharmony_ci+  NNRTDEVICE_CPU = 1,
1044be168c0dSopenharmony_ci+  NNRTDEVICE_GPU = 2,
1045be168c0dSopenharmony_ci+  NNRTDEVICE_ACCELERATOR = 3,
1046be168c0dSopenharmony_ci+};
1047be168c0dSopenharmony_ci+
1048be168c0dSopenharmony_ci+struct ModelInfo {
1049be168c0dSopenharmony_ci+  std::string model_path = "";
1050be168c0dSopenharmony_ci+  char *model_buffer_data = nullptr;
1051be168c0dSopenharmony_ci+  size_t model_buffer_total = 0;
1052be168c0dSopenharmony_ci+  int32_t model_fd = 0;
1053be168c0dSopenharmony_ci+  ModelMode mode = kBuffer;
1054be168c0dSopenharmony_ci+  bool train_model = false;
1055be168c0dSopenharmony_ci+};
1056be168c0dSopenharmony_ci+
1057be168c0dSopenharmony_ci+struct CpuDevice {
1058be168c0dSopenharmony_ci+  int thread_num;
1059be168c0dSopenharmony_ci+  int thread_affinity_mode;
1060be168c0dSopenharmony_ci+  std::vector<int32_t> thread_affinity_cores;
1061be168c0dSopenharmony_ci+  std::string precision_mode;
1062be168c0dSopenharmony_ci+  CpuDevice(){};
1063be168c0dSopenharmony_ci+  CpuDevice(int thread_num, int affinity_mode, std::vector<int32_t> affinity_cores, std::string precision)
1064be168c0dSopenharmony_ci+      : thread_num(thread_num),
1065be168c0dSopenharmony_ci+        thread_affinity_mode(affinity_mode),
1066be168c0dSopenharmony_ci+        thread_affinity_cores(affinity_cores),
1067be168c0dSopenharmony_ci+        precision_mode(precision){};
1068be168c0dSopenharmony_ci+};
1069be168c0dSopenharmony_ci+
1070be168c0dSopenharmony_ci+struct NnrtDeviceDesc {
1071be168c0dSopenharmony_ci+  std::string name;
1072be168c0dSopenharmony_ci+  ContextNnrtDeviceType type;
1073be168c0dSopenharmony_ci+  size_t id;
1074be168c0dSopenharmony_ci+};
1075be168c0dSopenharmony_ci+
1076be168c0dSopenharmony_ci+struct NNRTDevice {
1077be168c0dSopenharmony_ci+  size_t device_id;
1078be168c0dSopenharmony_ci+  int performance_mode{-1};
1079be168c0dSopenharmony_ci+  int priority{-1};
1080be168c0dSopenharmony_ci+  NNRTDevice(){};
1081be168c0dSopenharmony_ci+  NNRTDevice(int device_id, int performance_mode, int priority)
1082be168c0dSopenharmony_ci+      : device_id(device_id), performance_mode(performance_mode), priority(priority){};
1083be168c0dSopenharmony_ci+};
1084be168c0dSopenharmony_ci+
1085be168c0dSopenharmony_ci+struct TrainConfig {
1086be168c0dSopenharmony_ci+  std::vector<std::string> loss_names;
1087be168c0dSopenharmony_ci+  int optimization_level = kO0; // kAUTO
1088be168c0dSopenharmony_ci+};
1089be168c0dSopenharmony_ci+
1090be168c0dSopenharmony_ci+struct ContextInfo {
1091be168c0dSopenharmony_ci+  std::vector<std::string> target;
1092be168c0dSopenharmony_ci+  CpuDevice cpu_device;
1093be168c0dSopenharmony_ci+  NNRTDevice nnrt_device;
1094be168c0dSopenharmony_ci+  TrainConfig train_cfg;
1095be168c0dSopenharmony_ci+};
1096be168c0dSopenharmony_ci+
1097be168c0dSopenharmony_ci+const int32_t NAPI_ERR_INPUT_INVALID = 401;
1098be168c0dSopenharmony_ci+const int32_t NAPI_ERR_INVALID_PARAM = 1000101;
1099be168c0dSopenharmony_ci+const int32_t NAPI_ERR_NO_MEMORY = 1000102;
1100be168c0dSopenharmony_ci+const int32_t NAPI_ERR_ILLEGAL_STATE = 1000103;
1101be168c0dSopenharmony_ci+const int32_t NAPI_ERR_UNSUPPORTED = 1000104;
1102be168c0dSopenharmony_ci+const int32_t NAPI_ERR_TIMEOUT = 1000105;
1103be168c0dSopenharmony_ci+const int32_t NAPI_ERR_STREAM_LIMIT = 1000201;
1104be168c0dSopenharmony_ci+const int32_t NAPI_ERR_SYSTEM = 1000301;
1105be168c0dSopenharmony_ci+
1106be168c0dSopenharmony_ci+const std::string NAPI_ERROR_INVALID_PARAM_INFO = "input parameter value error";
1107be168c0dSopenharmony_ci+const std::string NAPI_ERR_INPUT_INVALID_INFO = "input parameter type or number mismatch";
1108be168c0dSopenharmony_ci+const std::string NAPI_ERR_INVALID_PARAM_INFO = "invalid parameter";
1109be168c0dSopenharmony_ci+const std::string NAPI_ERR_NO_MEMORY_INFO = "allocate memory failed";
1110be168c0dSopenharmony_ci+const std::string NAPI_ERR_ILLEGAL_STATE_INFO = "Operation not permit at current state";
1111be168c0dSopenharmony_ci+const std::string NAPI_ERR_UNSUPPORTED_INFO = "unsupported option";
1112be168c0dSopenharmony_ci+const std::string NAPI_ERR_TIMEOUT_INFO = "time out";
1113be168c0dSopenharmony_ci+const std::string NAPI_ERR_STREAM_LIMIT_INFO = "stream number limited";
1114be168c0dSopenharmony_ci+const std::string NAPI_ERR_SYSTEM_INFO = "system error";
1115be168c0dSopenharmony_ci+}  // namespace mindspore
1116be168c0dSopenharmony_ci+#endif  // COMMON_NAPI_H
1117be168c0dSopenharmony_ci\ No newline at end of file
1118be168c0dSopenharmony_cidiff --git a/include/js_api/ms_errors.h b/include/js_api/ms_errors.h
1119be168c0dSopenharmony_cinew file mode 100644
1120be168c0dSopenharmony_ciindex 00000000..0d030241
1121be168c0dSopenharmony_ci--- /dev/null
1122be168c0dSopenharmony_ci+++ b/include/js_api/ms_errors.h
1123be168c0dSopenharmony_ci@@ -0,0 +1,39 @@
1124be168c0dSopenharmony_ci+/**
1125be168c0dSopenharmony_ci+ * Copyright (C) 2023 Huawei Device Co., Ltd.
1126be168c0dSopenharmony_ci+ * Licensed under the Apache License, Version 2.0 (the "License");
1127be168c0dSopenharmony_ci+ * you may not use this file except in compliance with the License.
1128be168c0dSopenharmony_ci+ * You may obtain a copy of the License at
1129be168c0dSopenharmony_ci+ *
1130be168c0dSopenharmony_ci+ *     http://www.apache.org/licenses/LICENSE-2.0
1131be168c0dSopenharmony_ci+ *
1132be168c0dSopenharmony_ci+ * Unless required by applicable law or agreed to in writing, software
1133be168c0dSopenharmony_ci+ * distributed under the License is distributed on an "AS IS" BASIS,
1134be168c0dSopenharmony_ci+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1135be168c0dSopenharmony_ci+ * See the License for the specific language governing permissions and
1136be168c0dSopenharmony_ci+ * limitations under the License.
1137be168c0dSopenharmony_ci+ */
1138be168c0dSopenharmony_ci+#ifndef MINDSPORE_INCLUDE_JS_API_MS_ERRORS_H
1139be168c0dSopenharmony_ci+#define MINDSPORE_INCLUDE_JS_API_MS_ERRORS_H
1140be168c0dSopenharmony_ci+
1141be168c0dSopenharmony_ci+namespace mindspore {
1142be168c0dSopenharmony_ci+const int32_t BASE_MSLITE_ERR_OFFSET = 1000199;
1143be168c0dSopenharmony_ci+
1144be168c0dSopenharmony_ci+/** Success */
1145be168c0dSopenharmony_ci+const int32_t SUCCESS = 0;
1146be168c0dSopenharmony_ci+
1147be168c0dSopenharmony_ci+/** Fail */
1148be168c0dSopenharmony_ci+const int32_t ERROR = BASE_MSLITE_ERR_OFFSET;
1149be168c0dSopenharmony_ci+
1150be168c0dSopenharmony_ci+/** Status error */
1151be168c0dSopenharmony_ci+const int32_t ERR_ILLEGAL_STATE = BASE_MSLITE_ERR_OFFSET - 1;
1152be168c0dSopenharmony_ci+
1153be168c0dSopenharmony_ci+/** Invalid parameter */
1154be168c0dSopenharmony_ci+const int32_t ERR_INVALID_PARAM = BASE_MSLITE_ERR_OFFSET - 2;
1155be168c0dSopenharmony_ci+
1156be168c0dSopenharmony_ci+/** Not existed parameter */
1157be168c0dSopenharmony_ci+const int32_t ERR_NOT_EXISTED_PARAM = BASE_MSLITE_ERR_OFFSET - 3;
1158be168c0dSopenharmony_ci+
1159be168c0dSopenharmony_ci+/** Invalid operation */
1160be168c0dSopenharmony_ci+const int32_t ERR_INVALID_OPERATION = BASE_MSLITE_ERR_OFFSET - 4;
1161be168c0dSopenharmony_ci+}  // namespace mindspore
1162be168c0dSopenharmony_ci+#endif  // MS_ERRORS_H
1163be168c0dSopenharmony_ci\ No newline at end of file
1164be168c0dSopenharmony_cidiff --git a/include/js_api/ms_info.h b/include/js_api/ms_info.h
1165be168c0dSopenharmony_cinew file mode 100644
1166be168c0dSopenharmony_ciindex 00000000..6f563231
1167be168c0dSopenharmony_ci--- /dev/null
1168be168c0dSopenharmony_ci+++ b/include/js_api/ms_info.h
1169be168c0dSopenharmony_ci@@ -0,0 +1,69 @@
1170be168c0dSopenharmony_ci+/**
1171be168c0dSopenharmony_ci+ * Copyright (C) 2023 Huawei Device Co., Ltd.
1172be168c0dSopenharmony_ci+ * Licensed under the Apache License, Version 2.0 (the "License");
1173be168c0dSopenharmony_ci+ * you may not use this file except in compliance with the License.
1174be168c0dSopenharmony_ci+ * You may obtain a copy of the License at
1175be168c0dSopenharmony_ci+ *
1176be168c0dSopenharmony_ci+ *     http://www.apache.org/licenses/LICENSE-2.0
1177be168c0dSopenharmony_ci+ *
1178be168c0dSopenharmony_ci+ * Unless required by applicable law or agreed to in writing, software
1179be168c0dSopenharmony_ci+ * distributed under the License is distributed on an "AS IS" BASIS,
1180be168c0dSopenharmony_ci+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1181be168c0dSopenharmony_ci+ * See the License for the specific language governing permissions and
1182be168c0dSopenharmony_ci+ * limitations under the License.
1183be168c0dSopenharmony_ci+ */
1184be168c0dSopenharmony_ci+#ifndef MINDSPORE_INCLUDE_JS_API_MS_INFO_H
1185be168c0dSopenharmony_ci+#define MINDSPORE_INCLUDE_JS_API_MS_INFO_H
1186be168c0dSopenharmony_ci+
1187be168c0dSopenharmony_ci+namespace mindspore {
1188be168c0dSopenharmony_ci+enum InterruptType {
1189be168c0dSopenharmony_ci+  INTERRUPT_TYPE_BEGIN = 1,
1190be168c0dSopenharmony_ci+  INTERRUPT_TYPE_END = 2,
1191be168c0dSopenharmony_ci+};
1192be168c0dSopenharmony_ci+
1193be168c0dSopenharmony_ci+enum InterruptHint {
1194be168c0dSopenharmony_ci+  INTERRUPT_HINT_NONE = 0,
1195be168c0dSopenharmony_ci+  INTERRUPT_HINT_RESUME,
1196be168c0dSopenharmony_ci+  INTERRUPT_HINT_PAUSE,
1197be168c0dSopenharmony_ci+  INTERRUPT_HINT_STOP,
1198be168c0dSopenharmony_ci+  INTERRUPT_HINT_DUCK,
1199be168c0dSopenharmony_ci+  INTERRUPT_HINT_UNDUCK
1200be168c0dSopenharmony_ci+};
1201be168c0dSopenharmony_ci+
1202be168c0dSopenharmony_ci+enum InterruptForceType {
1203be168c0dSopenharmony_ci+  /**
1204be168c0dSopenharmony_ci+   * Force type, system change audio state.
1205be168c0dSopenharmony_ci+   */
1206be168c0dSopenharmony_ci+  INTERRUPT_FORCE = 0,
1207be168c0dSopenharmony_ci+  /**
1208be168c0dSopenharmony_ci+   * Share type, application change audio state.
1209be168c0dSopenharmony_ci+   */
1210be168c0dSopenharmony_ci+  INTERRUPT_SHARE
1211be168c0dSopenharmony_ci+};
1212be168c0dSopenharmony_ci+
1213be168c0dSopenharmony_ci+struct InterruptEvent {
1214be168c0dSopenharmony_ci+  /**
1215be168c0dSopenharmony_ci+   * Interrupt event type, begin or end
1216be168c0dSopenharmony_ci+   */
1217be168c0dSopenharmony_ci+  InterruptType eventType;
1218be168c0dSopenharmony_ci+  /**
1219be168c0dSopenharmony_ci+   * Interrupt force type, force or share
1220be168c0dSopenharmony_ci+   */
1221be168c0dSopenharmony_ci+  InterruptForceType forceType;
1222be168c0dSopenharmony_ci+  /**
1223be168c0dSopenharmony_ci+   * Interrupt hint type. In force type, the audio state already changed,
1224be168c0dSopenharmony_ci+   * but in share mode, only provide a hint for application to decide.
1225be168c0dSopenharmony_ci+   */
1226be168c0dSopenharmony_ci+  InterruptHint hintType;
1227be168c0dSopenharmony_ci+};
1228be168c0dSopenharmony_ci+
1229be168c0dSopenharmony_ci+// Used internally only by AudioFramework
1230be168c0dSopenharmony_ci+struct InterruptEventInternal {
1231be168c0dSopenharmony_ci+  InterruptType eventType;
1232be168c0dSopenharmony_ci+  InterruptForceType forceType;
1233be168c0dSopenharmony_ci+  InterruptHint hintType;
1234be168c0dSopenharmony_ci+  float duckVolume;
1235be168c0dSopenharmony_ci+};
1236be168c0dSopenharmony_ci+
1237be168c0dSopenharmony_ci+}  // namespace mindspore
1238be168c0dSopenharmony_ci+#endif  // MS_INFO_H
1239be168c0dSopenharmony_ci\ No newline at end of file
1240be168c0dSopenharmony_cidiff --git a/include/js_api/ms_parameters_napi.h b/include/js_api/ms_parameters_napi.h
1241be168c0dSopenharmony_cinew file mode 100644
1242be168c0dSopenharmony_ciindex 00000000..9585255f
1243be168c0dSopenharmony_ci--- /dev/null
1244be168c0dSopenharmony_ci+++ b/include/js_api/ms_parameters_napi.h
1245be168c0dSopenharmony_ci@@ -0,0 +1,24 @@
1246be168c0dSopenharmony_ci+/**
1247be168c0dSopenharmony_ci+ * Copyright (C) 2023 Huawei Device Co., Ltd.
1248be168c0dSopenharmony_ci+ * Licensed under the Apache License, Version 2.0 (the "License");
1249be168c0dSopenharmony_ci+ * you may not use this file except in compliance with the License.
1250be168c0dSopenharmony_ci+ * You may obtain a copy of the License at
1251be168c0dSopenharmony_ci+ *
1252be168c0dSopenharmony_ci+ *     http://www.apache.org/licenses/LICENSE-2.0
1253be168c0dSopenharmony_ci+ *
1254be168c0dSopenharmony_ci+ * Unless required by applicable law or agreed to in writing, software
1255be168c0dSopenharmony_ci+ * distributed under the License is distributed on an "AS IS" BASIS,
1256be168c0dSopenharmony_ci+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1257be168c0dSopenharmony_ci+ * See the License for the specific language governing permissions and
1258be168c0dSopenharmony_ci+ * limitations under the License.
1259be168c0dSopenharmony_ci+ */
1260be168c0dSopenharmony_ci+#ifndef MINDSPORE_INCLUDE_JS_API_MS_PARAMETERS_NAPI_H
1261be168c0dSopenharmony_ci+#define MINDSPORE_INCLUDE_JS_API_MS_PARAMETERS_NAPI_H
1262be168c0dSopenharmony_ci+
1263be168c0dSopenharmony_ci+#include <iostream>
1264be168c0dSopenharmony_ci+
1265be168c0dSopenharmony_ci+namespace mindspore {
1266be168c0dSopenharmony_ci+
1267be168c0dSopenharmony_ci+static const std::int32_t REFERENCE_CREATION_COUNT = 1;
1268be168c0dSopenharmony_ci+}
1269be168c0dSopenharmony_ci+#endif  // MS_PARAMETERS_NAPI
1270be168c0dSopenharmony_ci\ No newline at end of file
1271be168c0dSopenharmony_cidiff --git a/include/js_api/mslite_model_callback_napi.h b/include/js_api/mslite_model_callback_napi.h
1272be168c0dSopenharmony_cinew file mode 100644
1273be168c0dSopenharmony_ciindex 00000000..3b3ee595
1274be168c0dSopenharmony_ci--- /dev/null
1275be168c0dSopenharmony_ci+++ b/include/js_api/mslite_model_callback_napi.h
1276be168c0dSopenharmony_ci@@ -0,0 +1,38 @@
1277be168c0dSopenharmony_ci+/**
1278be168c0dSopenharmony_ci+ * Copyright (C) 2023 Huawei Device Co., Ltd.
1279be168c0dSopenharmony_ci+ * Licensed under the Apache License, Version 2.0 (the "License");
1280be168c0dSopenharmony_ci+ * you may not use this file except in compliance with the License.
1281be168c0dSopenharmony_ci+ * You may obtain a copy of the License at
1282be168c0dSopenharmony_ci+ *
1283be168c0dSopenharmony_ci+ *     http://www.apache.org/licenses/LICENSE-2.0
1284be168c0dSopenharmony_ci+ *
1285be168c0dSopenharmony_ci+ * Unless required by applicable law or agreed to in writing, software
1286be168c0dSopenharmony_ci+ * distributed under the License is distributed on an "AS IS" BASIS,
1287be168c0dSopenharmony_ci+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1288be168c0dSopenharmony_ci+ * See the License for the specific language governing permissions and
1289be168c0dSopenharmony_ci+ * limitations under the License.
1290be168c0dSopenharmony_ci+ */
1291be168c0dSopenharmony_ci+#ifndef MINDSPORE_INCLUDE_JS_API_MSLITE_MODEL_CALLBACK_NAPI_H
1292be168c0dSopenharmony_ci+#define MINDSPORE_INCLUDE_JS_API_MSLITE_MODEL_CALLBACK_NAPI_H
1293be168c0dSopenharmony_ci+
1294be168c0dSopenharmony_ci+#include <queue>
1295be168c0dSopenharmony_ci+#include <uv.h>
1296be168c0dSopenharmony_ci+#include "mslite_model_napi.h"
1297be168c0dSopenharmony_ci+#include "ms_info.h"
1298be168c0dSopenharmony_ci+#include "common_napi.h"
1299be168c0dSopenharmony_ci+
1300be168c0dSopenharmony_ci+namespace mindspore {
1301be168c0dSopenharmony_ci+enum class AsyncWorkType : int32_t {
1302be168c0dSopenharmony_ci+  ASYNC_WORK_PREPARE = 0,
1303be168c0dSopenharmony_ci+  ASYNC_WORK_PLAY,
1304be168c0dSopenharmony_ci+  ASYNC_WORK_PAUSE,
1305be168c0dSopenharmony_ci+  ASYNC_WORK_STOP,
1306be168c0dSopenharmony_ci+  ASYNC_WORK_RESET,
1307be168c0dSopenharmony_ci+  ASYNC_WORK_SEEK,
1308be168c0dSopenharmony_ci+  ASYNC_WORK_SPEED,
1309be168c0dSopenharmony_ci+  ASYNC_WORK_VOLUME,
1310be168c0dSopenharmony_ci+  ASYNC_WORK_BITRATE,
1311be168c0dSopenharmony_ci+  ASYNC_WORK_INVALID,
1312be168c0dSopenharmony_ci+};
1313be168c0dSopenharmony_ci+}  // namespace mindspore
1314be168c0dSopenharmony_ci+#endif  // COMMON_NAPI_H
1315be168c0dSopenharmony_ci\ No newline at end of file
1316be168c0dSopenharmony_cidiff --git a/include/js_api/mslite_model_napi.h b/include/js_api/mslite_model_napi.h
1317be168c0dSopenharmony_cinew file mode 100644
1318be168c0dSopenharmony_ciindex 00000000..1f075ba4
1319be168c0dSopenharmony_ci--- /dev/null
1320be168c0dSopenharmony_ci+++ b/include/js_api/mslite_model_napi.h
1321be168c0dSopenharmony_ci@@ -0,0 +1,186 @@
1322be168c0dSopenharmony_ci+/**
1323be168c0dSopenharmony_ci+ * Copyright 2023 Huawei Technologies Co., Ltd
1324be168c0dSopenharmony_ci+ *
1325be168c0dSopenharmony_ci+ * Licensed under the Apache License, Version 2.0 (the "License");
1326be168c0dSopenharmony_ci+ * you may not use this file except in compliance with the License.
1327be168c0dSopenharmony_ci+ * You may obtain a copy of the License at
1328be168c0dSopenharmony_ci+ *
1329be168c0dSopenharmony_ci+ * http://www.apache.org/licenses/LICENSE-2.0
1330be168c0dSopenharmony_ci+ *
1331be168c0dSopenharmony_ci+ * Unless required by applicable law or agreed to in writing, software
1332be168c0dSopenharmony_ci+ * distributed under the License is distributed on an "AS IS" BASIS,
1333be168c0dSopenharmony_ci+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1334be168c0dSopenharmony_ci+ * See the License for the specific language governing permissions and
1335be168c0dSopenharmony_ci+ * limitations under the License.
1336be168c0dSopenharmony_ci+ */
1337be168c0dSopenharmony_ci+#ifndef MINDSPORE_INCLUDE_JS_API_MSLITE_MODEL_NAPI_H
1338be168c0dSopenharmony_ci+#define MINDSPORE_INCLUDE_JS_API_MSLITE_MODEL_NAPI_H
1339be168c0dSopenharmony_ci+
1340be168c0dSopenharmony_ci+#include <memory>
1341be168c0dSopenharmony_ci+#include <mutex>
1342be168c0dSopenharmony_ci+#include "include/api/model.h"
1343be168c0dSopenharmony_ci+#include "include/api/context.h"
1344be168c0dSopenharmony_ci+#include "include/api/serialization.h"
1345be168c0dSopenharmony_ci+#include "include/api/cell.h"
1346be168c0dSopenharmony_ci+#include "common_napi.h"
1347be168c0dSopenharmony_ci+#include "mslite_model_callback_napi.h"
1348be168c0dSopenharmony_ci+#include "napi/native_api.h"
1349be168c0dSopenharmony_ci+#include "napi/native_node_api.h"
1350be168c0dSopenharmony_ci+#include "include/js_api/common_napi.h"
1351be168c0dSopenharmony_ci+
1352be168c0dSopenharmony_ci+namespace mindspore {
1353be168c0dSopenharmony_ci+static const std::map<std::string, TensorFormat> tensorFormatMap = {
1354be168c0dSopenharmony_ci+    {"DEFAULT_FORMAT", TENSOR_DEFAULT_FORMAT},
1355be168c0dSopenharmony_ci+    {"NCHW", TENSOR_NCHW},
1356be168c0dSopenharmony_ci+    {"NHWC", TENSOR_NHWC},
1357be168c0dSopenharmony_ci+    {"NHWC4", TENSOR_NHWC4},
1358be168c0dSopenharmony_ci+    {"HWKC", TENSOR_HWKC},
1359be168c0dSopenharmony_ci+    {"HWCK", TENSOR_HWCK},
1360be168c0dSopenharmony_ci+    {"KCHW", TENSOR_KCHW}
1361be168c0dSopenharmony_ci+};
1362be168c0dSopenharmony_ci+static const std::map<std::string, TensorDataType> tensorDataTypeMap = {
1363be168c0dSopenharmony_ci+    {"TYPE_UNKNOWN", TENSOR_UNKNOWN},
1364be168c0dSopenharmony_ci+    {"NUMBER_TYPE_INT8", TENSOR_INT8},
1365be168c0dSopenharmony_ci+    {"NUMBER_TYPE_INT16", TENSOR_INT16},
1366be168c0dSopenharmony_ci+    {"NUMBER_TYPE_INT32", TENSOR_INT32},
1367be168c0dSopenharmony_ci+    {"NUMBER_TYPE_INT64", TENSOR_INT64},
1368be168c0dSopenharmony_ci+    {"NUMBER_TYPE_UINT8", TENSOR_UINT8},
1369be168c0dSopenharmony_ci+    {"NUMBER_TYPE_UINT16", TENSOR_UINT16},
1370be168c0dSopenharmony_ci+    {"NUMBER_TYPE_UINT32", TENSOR_UINT32},
1371be168c0dSopenharmony_ci+    {"NUMBER_TYPE_UINT64", TENSOR_UINT64},
1372be168c0dSopenharmony_ci+    {"NUMBER_TYPE_FLOAT16", TENSOR_FLOAT16},
1373be168c0dSopenharmony_ci+    {"NUMBER_TYPE_FLOAT32", TENSOR_FLOAT32},
1374be168c0dSopenharmony_ci+    {"NUMBER_TYPE_FLOAT64", TENSOR_FLOAT64}
1375be168c0dSopenharmony_ci+};
1376be168c0dSopenharmony_ci+static const std::map<std::string, ContextThreadAffinityMode> contextThreadAffinityModeMap = {
1377be168c0dSopenharmony_ci+    {"NO_AFFINITIES", CONTEXT_AFFINITY_MODE},
1378be168c0dSopenharmony_ci+    {"BIG_CORES_FIRST", CONTEXT_BIG_CORES_FIRST},
1379be168c0dSopenharmony_ci+    {"LITTLE_CORES_FIRST", CONTEXT_LITTLE_CORES_FIRST},
1380be168c0dSopenharmony_ci+};
1381be168c0dSopenharmony_ci+
1382be168c0dSopenharmony_ci+static const std::map<std::string, ContextQuantizationType> contextQuantizationTypeMap = {
1383be168c0dSopenharmony_ci+  {"NO_QUANT", NO_QUANT},
1384be168c0dSopenharmony_ci+  {"WEIGHT_QUANT", WEIGHT_QUANT},
1385be168c0dSopenharmony_ci+  {"FULL_QUANT", FULL_QUANT},
1386be168c0dSopenharmony_ci+};
1387be168c0dSopenharmony_ci+
1388be168c0dSopenharmony_ci+static const std::map<std::string, ContextOptimizationLevel> contextOptimizationLevelTypeMap = {
1389be168c0dSopenharmony_ci+  {"O0", O0},
1390be168c0dSopenharmony_ci+  {"O2", O2},
1391be168c0dSopenharmony_ci+  {"O3", O3},
1392be168c0dSopenharmony_ci+  {"AUTO", AUTO},
1393be168c0dSopenharmony_ci+};
1394be168c0dSopenharmony_ci+
1395be168c0dSopenharmony_ci+static const std::map<std::string, ContextPerformanceMode> contextPerformanceModeTypeMap = {
1396be168c0dSopenharmony_ci+  {"PERFORMANCE_NONE", PERFORMANCE_NONE},
1397be168c0dSopenharmony_ci+  {"PERFORMANCE_LOW", PERFORMANCE_LOW},
1398be168c0dSopenharmony_ci+  {"PERFORMANCE_MEDIUM", PERFORMANCE_MEDIUM},
1399be168c0dSopenharmony_ci+  {"PERFORMANCE_HIGH", PERFORMANCE_HIGH},
1400be168c0dSopenharmony_ci+  {"PERFORMANCE_EXTREME", PERFORMANCE_EXTREME}
1401be168c0dSopenharmony_ci+};
1402be168c0dSopenharmony_ci+
1403be168c0dSopenharmony_ci+static const std::map<std::string, ContextPriority> contextPriorityTypeMap = {
1404be168c0dSopenharmony_ci+  {"PRIORITY_NONE", PRIORITY_NONE},
1405be168c0dSopenharmony_ci+  {"PRIORITY_LOW", PRIORITY_LOW},
1406be168c0dSopenharmony_ci+  {"PRIORITY_MEDIUM", PRIORITY_MEDIUM},
1407be168c0dSopenharmony_ci+  {"PRIORITY_HIGH", PRIORITY_HIGH},
1408be168c0dSopenharmony_ci+};
1409be168c0dSopenharmony_ci+
1410be168c0dSopenharmony_ci+static const std::map<std::string, ContextNnrtDeviceType> contextNnrtDeviceTypeTypeMap = {
1411be168c0dSopenharmony_ci+  {"NNRTDEVICE_OTHERS", NNRTDEVICE_OTHERS},
1412be168c0dSopenharmony_ci+  {"NNRTDEVICE_CPU", NNRTDEVICE_CPU},
1413be168c0dSopenharmony_ci+  {"NNRTDEVICE_GPU", NNRTDEVICE_GPU},
1414be168c0dSopenharmony_ci+  {"NNRTDEVICE_ACCELERATOR", NNRTDEVICE_ACCELERATOR},
1415be168c0dSopenharmony_ci+};
1416be168c0dSopenharmony_ci+
1417be168c0dSopenharmony_ci+class MSLiteModelNapi {
1418be168c0dSopenharmony_ci+ public:
1419be168c0dSopenharmony_ci+  MSLiteModelNapi();
1420be168c0dSopenharmony_ci+  ~MSLiteModelNapi();
1421be168c0dSopenharmony_ci+
1422be168c0dSopenharmony_ci+  static napi_value Init(napi_env env, napi_value exports);
1423be168c0dSopenharmony_ci+  std::shared_ptr<mindspore::Model> native_model_ = nullptr;
1424be168c0dSopenharmony_ci+
1425be168c0dSopenharmony_ci+ private:
1426be168c0dSopenharmony_ci+  struct MSLiteModelAsyncContext {
1427be168c0dSopenharmony_ci+    napi_async_work work;
1428be168c0dSopenharmony_ci+    napi_deferred deferred = nullptr;
1429be168c0dSopenharmony_ci+    napi_ref callbackRef = nullptr;
1430be168c0dSopenharmony_ci+    int32_t status = SUCCESS;
1431be168c0dSopenharmony_ci+    MSLiteModelNapi *lite_model = nullptr;
1432be168c0dSopenharmony_ci+    ModelInfo model_info;
1433be168c0dSopenharmony_ci+    ContextInfo context;
1434be168c0dSopenharmony_ci+
1435be168c0dSopenharmony_ci+    MSLiteModelAsyncContext() {
1436be168c0dSopenharmony_ci+      // setting context default value
1437be168c0dSopenharmony_ci+      context.target.push_back("cpu");
1438be168c0dSopenharmony_ci+      context.cpu_device.thread_num = 2;
1439be168c0dSopenharmony_ci+      context.cpu_device.thread_affinity_mode = 0;
1440be168c0dSopenharmony_ci+      context.cpu_device.precision_mode = "enforce_fp32";
1441be168c0dSopenharmony_ci+    }
1442be168c0dSopenharmony_ci+  };
1443be168c0dSopenharmony_ci+  static napi_value Constructor(napi_env env, napi_callback_info info);
1444be168c0dSopenharmony_ci+  static void Finalize(napi_env env, void *nativeObject, void *finalize);
1445be168c0dSopenharmony_ci+  static napi_value LoadMSLiteModelFromFile(napi_env env, napi_callback_info info);
1446be168c0dSopenharmony_ci+  static napi_value LoadMSLiteModelFromBuffer(napi_env env, napi_callback_info info);
1447be168c0dSopenharmony_ci+  static napi_value LoadMSLiteModelFromFd(napi_env env, napi_callback_info info);
1448be168c0dSopenharmony_ci+  static napi_value LoadMSLiteTrainModelFromFile(napi_env env, napi_callback_info info);
1449be168c0dSopenharmony_ci+  static napi_value LoadMSLiteTrainModelFromBuffer(napi_env env, napi_callback_info info);
1450be168c0dSopenharmony_ci+  static napi_value LoadMSLiteTrainModelFromFd(napi_env env, napi_callback_info info);
1451be168c0dSopenharmony_ci+  static napi_value GetInputs(napi_env env, napi_callback_info info);
1452be168c0dSopenharmony_ci+  static napi_value Resize(napi_env env, napi_callback_info info);
1453be168c0dSopenharmony_ci+  static napi_value PredictAsync(napi_env env, napi_callback_info info);
1454be168c0dSopenharmony_ci+  static napi_value RunStep(napi_env env, napi_callback_info info);
1455be168c0dSopenharmony_ci+  static napi_value GetWeights(napi_env env, napi_callback_info info);
1456be168c0dSopenharmony_ci+  static napi_value UpdateWeights(napi_env env, napi_callback_info info);
1457be168c0dSopenharmony_ci+  static napi_value SetupVirtualBatch(napi_env env, napi_callback_info info);
1458be168c0dSopenharmony_ci+  static napi_value ExportModel(napi_env env, napi_callback_info info);
1459be168c0dSopenharmony_ci+  static napi_value ExportWeightsCollaborateWithMicro(napi_env env, napi_callback_info info);
1460be168c0dSopenharmony_ci+  static napi_value GetTrainMode(napi_env env, napi_callback_info info);
1461be168c0dSopenharmony_ci+  static napi_value SetTrainMode(napi_env env, napi_callback_info info);
1462be168c0dSopenharmony_ci+  static napi_value GetLearningRate(napi_env env, napi_callback_info info);
1463be168c0dSopenharmony_ci+  static napi_value SetLearningRate(napi_env env, napi_callback_info info);
1464be168c0dSopenharmony_ci+  static int32_t ParseModelInfo(napi_env env, napi_value root, ModelInfo &model_info);
1465be168c0dSopenharmony_ci+  static int32_t ParseContextInfo(napi_env env, napi_value root, ContextInfo &info);
1466be168c0dSopenharmony_ci+  static int32_t ParseTrainCfgInfo(napi_env env, napi_value root, TrainConfig &cfg);
1467be168c0dSopenharmony_ci+  static void GetMSLiteModelAsyncCallbackComplete(napi_env env, napi_status status, void *data);
1468be168c0dSopenharmony_ci+  static void PredictAsyncCallbackComplete(napi_env env, napi_status status, void *data);
1469be168c0dSopenharmony_ci+  static napi_value CreateMSLiteModelWrapper(napi_env env, MSLiteModelAsyncContext *async_context);
1470be168c0dSopenharmony_ci+  static void CommonCallbackRoutine(napi_env env, MSLiteModelAsyncContext *&asyncContext, const napi_value &valueParam);
1471be168c0dSopenharmony_ci+  static std::shared_ptr<mindspore::Model> CreateModel(ModelInfo *model_info_ptr, ContextInfo *contex_ptr);
1472be168c0dSopenharmony_ci+  static std::shared_ptr<mindspore::Model> CreateTrainModel(ModelInfo *model_info_ptr, ContextInfo *contex_ptr);
1473be168c0dSopenharmony_ci+  static int32_t GetCpuDeviceInfo(napi_env env, napi_value args, ContextInfo &context);
1474be168c0dSopenharmony_ci+  static int32_t GetNNRTDeviceInfo(napi_env env, napi_value args, ContextInfo &context);
1475be168c0dSopenharmony_ci+  static int32_t GetDeviceInfoContext(ContextInfo *context_info_ptr,
1476be168c0dSopenharmony_ci+                                      std::vector<std::shared_ptr<DeviceInfoContext>> &device_infos);
1477be168c0dSopenharmony_ci+  static int32_t SetTensorData(napi_env env, napi_value thisVar, napi_value argv,
1478be168c0dSopenharmony_ci+                               MSLiteModelAsyncContext *async_context);
1479be168c0dSopenharmony_ci+  static napi_status AddNamedProperty(napi_env env, napi_value object, const std::string name, int32_t enumValue);
1480be168c0dSopenharmony_ci+  static napi_value GetAllNnrtDeviceDescs(napi_env env, napi_callback_info info);
1481be168c0dSopenharmony_ci+  static napi_value CreateFormatObject(napi_env env);
1482be168c0dSopenharmony_ci+  static napi_value CreateDataTypeObject(napi_env env);
1483be168c0dSopenharmony_ci+  static napi_value CreateThreadAffinityModeObject(napi_env env);
1484be168c0dSopenharmony_ci+  static napi_value CreateQuantizationTypeObject(napi_env env);
1485be168c0dSopenharmony_ci+  static napi_value CreateOptimizationLevelObject(napi_env env);
1486be168c0dSopenharmony_ci+  static napi_value CreatePerformanceModeObject(napi_env env);
1487be168c0dSopenharmony_ci+  static napi_value CreatePriorityObject(napi_env env);
1488be168c0dSopenharmony_ci+  static napi_value CreateNnrtDeviceTypeObject(napi_env env);
1489be168c0dSopenharmony_ci+
1490be168c0dSopenharmony_ci+
1491be168c0dSopenharmony_ci+  static thread_local napi_ref constructor_;
1492be168c0dSopenharmony_ci+  napi_env env_ = nullptr;
1493be168c0dSopenharmony_ci+  static napi_ref tensorFormat_;
1494be168c0dSopenharmony_ci+  static napi_ref tensorDataType_;
1495be168c0dSopenharmony_ci+  static napi_ref contextThreadAffinityMode_;
1496be168c0dSopenharmony_ci+  static napi_ref contextQuantizationType_;
1497be168c0dSopenharmony_ci+  static napi_ref contextOptimizationLevel_;
1498be168c0dSopenharmony_ci+  static napi_ref contextPerformanceMode_;
1499be168c0dSopenharmony_ci+  static napi_ref contextPriority_;
1500be168c0dSopenharmony_ci+  static napi_ref contextNnrtDeviceType_;
1501be168c0dSopenharmony_ci+
1502be168c0dSopenharmony_ci+  static ModelInfo *model_info_;
1503be168c0dSopenharmony_ci+  static ContextInfo *context_;
1504be168c0dSopenharmony_ci+  static std::mutex create_mutex_;
1505be168c0dSopenharmony_ci+};
1506be168c0dSopenharmony_ci+}  // namespace mindspore
1507be168c0dSopenharmony_ci+#endif  // MINDSPORE_INCLUDE_JS_API_MSLITE_MODEL_NAPI_H
1508be168c0dSopenharmony_ci\ No newline at end of file
1509be168c0dSopenharmony_cidiff --git a/include/js_api/mstensor_napi.h b/include/js_api/mstensor_napi.h
1510be168c0dSopenharmony_cinew file mode 100644
1511be168c0dSopenharmony_ciindex 00000000..e2b181b8
1512be168c0dSopenharmony_ci--- /dev/null
1513be168c0dSopenharmony_ci+++ b/include/js_api/mstensor_napi.h
1514be168c0dSopenharmony_ci@@ -0,0 +1,49 @@
1515be168c0dSopenharmony_ci+/**
1516be168c0dSopenharmony_ci+ * Copyright 2022 Huawei Technologies Co., Ltd
1517be168c0dSopenharmony_ci+ *
1518be168c0dSopenharmony_ci+ * Licensed under the Apache License, Version 2.0 (the "License");
1519be168c0dSopenharmony_ci+ * you may not use this file except in compliance with the License.
1520be168c0dSopenharmony_ci+ * You may obtain a copy of the License at
1521be168c0dSopenharmony_ci+ *
1522be168c0dSopenharmony_ci+ * http://www.apache.org/licenses/LICENSE-2.0
1523be168c0dSopenharmony_ci+ *
1524be168c0dSopenharmony_ci+ * Unless required by applicable law or agreed to in writing, software
1525be168c0dSopenharmony_ci+ * distributed under the License is distributed on an "AS IS" BASIS,
1526be168c0dSopenharmony_ci+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1527be168c0dSopenharmony_ci+ * See the License for the specific language governing permissions and
1528be168c0dSopenharmony_ci+ * limitations under the License.
1529be168c0dSopenharmony_ci+ */
1530be168c0dSopenharmony_ci+#ifndef MINDSPORE_INCLUDE_JS_API_MSTENSOR_NAPI_H
1531be168c0dSopenharmony_ci+#define MINDSPORE_INCLUDE_JS_API_MSTENSOR_NAPI_H
1532be168c0dSopenharmony_ci+
1533be168c0dSopenharmony_ci+#include "include/api/types.h"
1534be168c0dSopenharmony_ci+#include "napi/native_api.h"
1535be168c0dSopenharmony_ci+#include "napi/native_node_api.h"
1536be168c0dSopenharmony_ci+
1537be168c0dSopenharmony_ci+namespace mindspore {
1538be168c0dSopenharmony_ci+class MSTensorNapi {
1539be168c0dSopenharmony_ci+ public:
1540be168c0dSopenharmony_ci+  static napi_value NewInstance(napi_env env, mindspore::MSTensor tensor);
1541be168c0dSopenharmony_ci+  MSTensorNapi();
1542be168c0dSopenharmony_ci+  ~MSTensorNapi();
1543be168c0dSopenharmony_ci+ private:
1544be168c0dSopenharmony_ci+  static napi_value Constructor(napi_env env, napi_callback_info info);
1545be168c0dSopenharmony_ci+  static void Finalize(napi_env env, void *nativeObject, void *finalize);
1546be168c0dSopenharmony_ci+  static napi_value GetConstructor(napi_env env);
1547be168c0dSopenharmony_ci+
1548be168c0dSopenharmony_ci+  static napi_value GetName(napi_env env, napi_callback_info info);
1549be168c0dSopenharmony_ci+  static napi_value GetShape(napi_env env, napi_callback_info info);
1550be168c0dSopenharmony_ci+  static napi_value GetElementNum(napi_env env, napi_callback_info info);
1551be168c0dSopenharmony_ci+  static napi_value GetDtype(napi_env env, napi_callback_info info);
1552be168c0dSopenharmony_ci+  static napi_value GetFormat(napi_env env, napi_callback_info info);
1553be168c0dSopenharmony_ci+  static napi_value GetDataSize(napi_env env, napi_callback_info info);
1554be168c0dSopenharmony_ci+  static napi_value GetDataBuffer(napi_env env, napi_callback_info info);
1555be168c0dSopenharmony_ci+  static napi_value SetData(napi_env env, napi_callback_info info);
1556be168c0dSopenharmony_ci+
1557be168c0dSopenharmony_ci+  static thread_local napi_ref constructor_;
1558be168c0dSopenharmony_ci+  napi_env env_ = nullptr;
1559be168c0dSopenharmony_ci+
1560be168c0dSopenharmony_ci+  std::unique_ptr<MSTensor> nativeMSTensor_ = nullptr;
1561be168c0dSopenharmony_ci+};
1562be168c0dSopenharmony_ci+}  // namespace mindspore
1563be168c0dSopenharmony_ci+#endif  // MINDSPORE_INCLUDE_JS_API_MSTENSOR_NAPI_H
1564be168c0dSopenharmony_ci\ No newline at end of file
1565be168c0dSopenharmony_cidiff --git a/include/js_api/native_module_ohos_ms.h b/include/js_api/native_module_ohos_ms.h
1566be168c0dSopenharmony_cinew file mode 100644
1567be168c0dSopenharmony_ciindex 00000000..202e8384
1568be168c0dSopenharmony_ci--- /dev/null
1569be168c0dSopenharmony_ci+++ b/include/js_api/native_module_ohos_ms.h
1570be168c0dSopenharmony_ci@@ -0,0 +1,22 @@
1571be168c0dSopenharmony_ci+/**
1572be168c0dSopenharmony_ci+ * Copyright 2023 Huawei Technologies Co., Ltd
1573be168c0dSopenharmony_ci+ *
1574be168c0dSopenharmony_ci+ * Licensed under the Apache License, Version 2.0 (the "License");
1575be168c0dSopenharmony_ci+ * you may not use this file except in compliance with the License.
1576be168c0dSopenharmony_ci+ * You may obtain a copy of the License at
1577be168c0dSopenharmony_ci+ *
1578be168c0dSopenharmony_ci+ * http://www.apache.org/licenses/LICENSE-2.0
1579be168c0dSopenharmony_ci+ *
1580be168c0dSopenharmony_ci+ * Unless required by applicable law or agreed to in writing, software
1581be168c0dSopenharmony_ci+ * distributed under the License is distributed on an "AS IS" BASIS,
1582be168c0dSopenharmony_ci+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1583be168c0dSopenharmony_ci+ * See the License for the specific language governing permissions and
1584be168c0dSopenharmony_ci+ * limitations under the License.
1585be168c0dSopenharmony_ci+ */
1586be168c0dSopenharmony_ci+#ifndef MINDSPORE_INCLUDE_JS_API_NATIVE_MODULE_OHOS_MS_H
1587be168c0dSopenharmony_ci+#define MINDSPORE_INCLUDE_JS_API_NATIVE_MODULE_OHOS_MS_H
1588be168c0dSopenharmony_ci+
1589be168c0dSopenharmony_ci+#include "mslite_model_napi.h"
1590be168c0dSopenharmony_ci+#include "mstensor_napi.h"
1591be168c0dSopenharmony_ci+
1592be168c0dSopenharmony_ci+#endif  // MINDSPORE_INCLUDE_JS_API_NATIVE_MODULE_OHOS_MS_H
1593be168c0dSopenharmony_ci\ No newline at end of file
1594be168c0dSopenharmony_cidiff --git a/include/js_api/nnrt_device_desc_napi.h b/include/js_api/nnrt_device_desc_napi.h
1595be168c0dSopenharmony_cinew file mode 100644
1596be168c0dSopenharmony_ciindex 00000000..a1c92de5
1597be168c0dSopenharmony_ci--- /dev/null
1598be168c0dSopenharmony_ci+++ b/include/js_api/nnrt_device_desc_napi.h
1599be168c0dSopenharmony_ci@@ -0,0 +1,45 @@
1600be168c0dSopenharmony_ci+/**
1601be168c0dSopenharmony_ci+* Copyright 2022 Huawei Technologies Co., Ltd
1602be168c0dSopenharmony_ci+*
1603be168c0dSopenharmony_ci+* Licensed under the Apache License, Version 2.0 (the "License");
1604be168c0dSopenharmony_ci+* you may not use this file except in compliance with the License.
1605be168c0dSopenharmony_ci+* You may obtain a copy of the License at
1606be168c0dSopenharmony_ci+*
1607be168c0dSopenharmony_ci+* http://www.apache.org/licenses/LICENSE-2.0
1608be168c0dSopenharmony_ci+*
1609be168c0dSopenharmony_ci+* Unless required by applicable law or agreed to in writing, software
1610be168c0dSopenharmony_ci+* distributed under the License is distributed on an "AS IS" BASIS,
1611be168c0dSopenharmony_ci+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1612be168c0dSopenharmony_ci+* See the License for the specific language governing permissions and
1613be168c0dSopenharmony_ci+* limitations under the License.
1614be168c0dSopenharmony_ci+*/
1615be168c0dSopenharmony_ci+#ifndef MINDSPORE_INCLUDE_JS_API_NNRT_DEVICE_DESC_NAPI_H
1616be168c0dSopenharmony_ci+#define MINDSPORE_INCLUDE_JS_API_NNRT_DEVICE_DESC_NAPI_H
1617be168c0dSopenharmony_ci+
1618be168c0dSopenharmony_ci+#include "include/api/types.h"
1619be168c0dSopenharmony_ci+#include "napi/native_api.h"
1620be168c0dSopenharmony_ci+#include "napi/native_node_api.h"
1621be168c0dSopenharmony_ci+#include "include/js_api/common_napi.h"
1622be168c0dSopenharmony_ci+
1623be168c0dSopenharmony_ci+namespace mindspore {
1624be168c0dSopenharmony_ci+class NnrtDeviceDescNapi {
1625be168c0dSopenharmony_ci+public:
1626be168c0dSopenharmony_ci+ static napi_value NewInstance(napi_env env, NnrtDeviceDesc decs);
1627be168c0dSopenharmony_ci+ NnrtDeviceDescNapi();
1628be168c0dSopenharmony_ci+ ~NnrtDeviceDescNapi();
1629be168c0dSopenharmony_ci+private:
1630be168c0dSopenharmony_ci+ static napi_value Constructor(napi_env env, napi_callback_info info);
1631be168c0dSopenharmony_ci+ static void Finalize(napi_env env, void *nativeObject, void *finalize);
1632be168c0dSopenharmony_ci+ static napi_value GetConstructor(napi_env env);
1633be168c0dSopenharmony_ci+
1634be168c0dSopenharmony_ci+ static napi_value GetDeviceName(napi_env env, napi_callback_info info);
1635be168c0dSopenharmony_ci+ static napi_value GetDeviceType(napi_env env, napi_callback_info info);
1636be168c0dSopenharmony_ci+ static napi_value GetDeviceID(napi_env env, napi_callback_info info);
1637be168c0dSopenharmony_ci+
1638be168c0dSopenharmony_ci+ static thread_local napi_ref constructor_;
1639be168c0dSopenharmony_ci+ napi_env env_ = nullptr;
1640be168c0dSopenharmony_ci+
1641be168c0dSopenharmony_ci+ std::unique_ptr<NnrtDeviceDesc> nativeNnrtDeviceDesc_ = nullptr;
1642be168c0dSopenharmony_ci+};
1643be168c0dSopenharmony_ci+}  // namespace mindspore
1644be168c0dSopenharmony_ci+#endif  // MINDSPORE_INCLUDE_JS_API_NNRT_DEVICE_DESC_NAPI_H
1645be168c0dSopenharmony_ci\ No newline at end of file
1646be168c0dSopenharmony_cidiff --git a/mindspore/lite/BUILD.gn b/mindspore/lite/BUILD.gn
1647be168c0dSopenharmony_ciindex 8a492bf9..7e824c01 100644
1648be168c0dSopenharmony_ci--- a/mindspore/lite/BUILD.gn
1649be168c0dSopenharmony_ci+++ b/mindspore/lite/BUILD.gn
1650be168c0dSopenharmony_ci@@ -76,6 +76,7 @@ ohos_group("mindspore") {
1651be168c0dSopenharmony_ci     ":mindspore_lib",
1652be168c0dSopenharmony_ci     ":mindspore_train_lib",
1653be168c0dSopenharmony_ci     "mindir:mindir_lib",
1654be168c0dSopenharmony_ci+    "src/litert/js_api:mindsporelite_napi"
1655be168c0dSopenharmony_ci   ]
1656be168c0dSopenharmony_ci }
1657be168c0dSopenharmony_ci 
1658be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/litert/js_api/BUILD.gn b/mindspore/lite/src/litert/js_api/BUILD.gn
1659be168c0dSopenharmony_cinew file mode 100644
1660be168c0dSopenharmony_ciindex 00000000..f12e4db4
1661be168c0dSopenharmony_ci--- /dev/null
1662be168c0dSopenharmony_ci+++ b/mindspore/lite/src/litert/js_api/BUILD.gn
1663be168c0dSopenharmony_ci@@ -0,0 +1,56 @@
1664be168c0dSopenharmony_ci+# Copyright (c) 2023 Huawei Device Co., Ltd.
1665be168c0dSopenharmony_ci+# Licensed under the Apache License, Version 2.0 (the "License");
1666be168c0dSopenharmony_ci+# you may not use this file except in compliance with the License.
1667be168c0dSopenharmony_ci+# You may obtain a copy of the License at
1668be168c0dSopenharmony_ci+#
1669be168c0dSopenharmony_ci+#     http://www.apache.org/licenses/LICENSE-2.0
1670be168c0dSopenharmony_ci+#
1671be168c0dSopenharmony_ci+# Unless required by applicable law or agreed to in writing, software
1672be168c0dSopenharmony_ci+# distributed under the License is distributed on an "AS IS" BASIS,
1673be168c0dSopenharmony_ci+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1674be168c0dSopenharmony_ci+# See the License for the specific language governing permissions and
1675be168c0dSopenharmony_ci+# limitations under the License.
1676be168c0dSopenharmony_ci+
1677be168c0dSopenharmony_ci+import("//build/ohos.gni")
1678be168c0dSopenharmony_ci+import("//build/ohos/ace/ace.gni")
1679be168c0dSopenharmony_ci+
1680be168c0dSopenharmony_ci+ohos_shared_library("mindsporelite_napi") {
1681be168c0dSopenharmony_ci+  include_dirs = [
1682be168c0dSopenharmony_ci+    "//third_party/mindspore/mindspore-src/source/",
1683be168c0dSopenharmony_ci+    "//third_party/mindspore/mindspore-src/source/include/api",
1684be168c0dSopenharmony_ci+    "//third_party/mindspore/mindspore-src/source/mindspore/core",
1685be168c0dSopenharmony_ci+    "//third_party//mindspore/mindspore-src/source/mindspore/lite",
1686be168c0dSopenharmony_ci+    "//third_party/libuv/include",
1687be168c0dSopenharmony_ci+
1688be168c0dSopenharmony_ci+    "//foundation/arkui/napi",
1689be168c0dSopenharmony_ci+    "//foundation/arkui/napi/interfaces/inner_api",
1690be168c0dSopenharmony_ci+    "//foundation/arkui/napi/interfaces/kits",
1691be168c0dSopenharmony_ci+    "//third_party/libuv/include",
1692be168c0dSopenharmony_ci+    "//third_party/node/src",
1693be168c0dSopenharmony_ci+  ]
1694be168c0dSopenharmony_ci+
1695be168c0dSopenharmony_ci+  sources = [
1696be168c0dSopenharmony_ci+    "mslite_model_napi.cc",
1697be168c0dSopenharmony_ci+    "mstensor_napi.cc",
1698be168c0dSopenharmony_ci+    "native_module_ohos_ms.cc",
1699be168c0dSopenharmony_ci+    "common_napi.cc",
1700be168c0dSopenharmony_ci+    "nnrt_device_desc.cc"
1701be168c0dSopenharmony_ci+  ]
1702be168c0dSopenharmony_ci+
1703be168c0dSopenharmony_ci+  deps = [
1704be168c0dSopenharmony_ci+    "../../../:mindspore_lib",
1705be168c0dSopenharmony_ci+    "../../../:mindspore_ndk"
1706be168c0dSopenharmony_ci+  ]
1707be168c0dSopenharmony_ci+  external_deps = [
1708be168c0dSopenharmony_ci+    "ability_runtime:abilitykit_native",
1709be168c0dSopenharmony_ci+    "ability_runtime:napi_base_context",
1710be168c0dSopenharmony_ci+    "c_utils:utils",
1711be168c0dSopenharmony_ci+    "hilog:libhilog",
1712be168c0dSopenharmony_ci+    "napi:ace_napi",
1713be168c0dSopenharmony_ci+    "resource_management:global_resmgr",
1714be168c0dSopenharmony_ci+  ]
1715be168c0dSopenharmony_ci+
1716be168c0dSopenharmony_ci+  relative_install_dir = "module/ai"
1717be168c0dSopenharmony_ci+  part_name = "mindspore"
1718be168c0dSopenharmony_ci+  subsystem_name = "thirdparty"
1719be168c0dSopenharmony_ci+}
1720be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/litert/js_api/common_napi.cc b/mindspore/lite/src/litert/js_api/common_napi.cc
1721be168c0dSopenharmony_cinew file mode 100644
1722be168c0dSopenharmony_ciindex 00000000..85d69ff6
1723be168c0dSopenharmony_ci--- /dev/null
1724be168c0dSopenharmony_ci+++ b/mindspore/lite/src/litert/js_api/common_napi.cc
1725be168c0dSopenharmony_ci@@ -0,0 +1,303 @@
1726be168c0dSopenharmony_ci+/*
1727be168c0dSopenharmony_ci+ * Copyright (C) 2023 Huawei Device Co., Ltd.
1728be168c0dSopenharmony_ci+ * Licensed under the Apache License, Version 2.0 (the "License");
1729be168c0dSopenharmony_ci+ * you may not use this file except in compliance with the License.
1730be168c0dSopenharmony_ci+ * You may obtain a copy of the License at
1731be168c0dSopenharmony_ci+ *
1732be168c0dSopenharmony_ci+ *     http://www.apache.org/licenses/LICENSE-2.0
1733be168c0dSopenharmony_ci+ *
1734be168c0dSopenharmony_ci+ * Unless required by applicable law or agreed to in writing, software
1735be168c0dSopenharmony_ci+ * distributed under the License is distributed on an "AS IS" BASIS,
1736be168c0dSopenharmony_ci+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1737be168c0dSopenharmony_ci+ * See the License for the specific language governing permissions and
1738be168c0dSopenharmony_ci+ * limitations under the License.
1739be168c0dSopenharmony_ci+ */
1740be168c0dSopenharmony_ci+
1741be168c0dSopenharmony_ci+#include "include/js_api/common_napi.h"
1742be168c0dSopenharmony_ci+#include <climits>
1743be168c0dSopenharmony_ci+#include "src/common/log.h"
1744be168c0dSopenharmony_ci+
1745be168c0dSopenharmony_ci+namespace mindspore {
1746be168c0dSopenharmony_ci+
1747be168c0dSopenharmony_ci+namespace {
1748be168c0dSopenharmony_ci+const int SIZE = 100;
1749be168c0dSopenharmony_ci+}
1750be168c0dSopenharmony_ci+
1751be168c0dSopenharmony_ci+std::string CommonNapi::getMessageByCode(int32_t &code) {
1752be168c0dSopenharmony_ci+  std::string err_message;
1753be168c0dSopenharmony_ci+  switch (code) {
1754be168c0dSopenharmony_ci+    case NAPI_ERR_INVALID_PARAM:
1755be168c0dSopenharmony_ci+      err_message = NAPI_ERR_INVALID_PARAM_INFO;
1756be168c0dSopenharmony_ci+      break;
1757be168c0dSopenharmony_ci+    case NAPI_ERR_NO_MEMORY:
1758be168c0dSopenharmony_ci+      err_message = NAPI_ERR_NO_MEMORY_INFO;
1759be168c0dSopenharmony_ci+      break;
1760be168c0dSopenharmony_ci+    case NAPI_ERR_ILLEGAL_STATE:
1761be168c0dSopenharmony_ci+      err_message = NAPI_ERR_ILLEGAL_STATE_INFO;
1762be168c0dSopenharmony_ci+      break;
1763be168c0dSopenharmony_ci+    case NAPI_ERR_UNSUPPORTED:
1764be168c0dSopenharmony_ci+      err_message = NAPI_ERR_UNSUPPORTED_INFO;
1765be168c0dSopenharmony_ci+      break;
1766be168c0dSopenharmony_ci+    case NAPI_ERR_TIMEOUT:
1767be168c0dSopenharmony_ci+      err_message = NAPI_ERR_TIMEOUT_INFO;
1768be168c0dSopenharmony_ci+      break;
1769be168c0dSopenharmony_ci+    case NAPI_ERR_STREAM_LIMIT:
1770be168c0dSopenharmony_ci+      err_message = NAPI_ERR_STREAM_LIMIT_INFO;
1771be168c0dSopenharmony_ci+      break;
1772be168c0dSopenharmony_ci+    case NAPI_ERR_SYSTEM:
1773be168c0dSopenharmony_ci+      err_message = NAPI_ERR_SYSTEM_INFO;
1774be168c0dSopenharmony_ci+      break;
1775be168c0dSopenharmony_ci+    case NAPI_ERR_INPUT_INVALID:
1776be168c0dSopenharmony_ci+      err_message = NAPI_ERR_INPUT_INVALID_INFO;
1777be168c0dSopenharmony_ci+      break;
1778be168c0dSopenharmony_ci+    default:
1779be168c0dSopenharmony_ci+      err_message = NAPI_ERR_SYSTEM_INFO;
1780be168c0dSopenharmony_ci+      code = NAPI_ERR_SYSTEM;
1781be168c0dSopenharmony_ci+      break;
1782be168c0dSopenharmony_ci+  }
1783be168c0dSopenharmony_ci+  return err_message;
1784be168c0dSopenharmony_ci+}
1785be168c0dSopenharmony_ci+
1786be168c0dSopenharmony_ci+int32_t CommonNapi::GetPropertyInt32(napi_env env, napi_value config_obj, const std::string &type, int32_t &result) {
1787be168c0dSopenharmony_ci+  napi_value item = nullptr;
1788be168c0dSopenharmony_ci+  bool exist = false;
1789be168c0dSopenharmony_ci+  napi_status status = napi_has_named_property(env, config_obj, type.c_str(), &exist);
1790be168c0dSopenharmony_ci+
1791be168c0dSopenharmony_ci+  if (status != napi_ok || !exist) {
1792be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "can not find " << type.c_str() << " will set default value";
1793be168c0dSopenharmony_ci+    return ERR_NOT_EXISTED_PARAM;
1794be168c0dSopenharmony_ci+  }
1795be168c0dSopenharmony_ci+
1796be168c0dSopenharmony_ci+  if (napi_get_named_property(env, config_obj, type.c_str(), &item) != napi_ok) {
1797be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "fail to get property: " << type.c_str();
1798be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
1799be168c0dSopenharmony_ci+  }
1800be168c0dSopenharmony_ci+
1801be168c0dSopenharmony_ci+  if (napi_get_value_int32(env, item, &result) != napi_ok) {
1802be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "fail to get property value " << type.c_str();
1803be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
1804be168c0dSopenharmony_ci+  }
1805be168c0dSopenharmony_ci+  return SUCCESS;
1806be168c0dSopenharmony_ci+}
1807be168c0dSopenharmony_ci+
1808be168c0dSopenharmony_ci+int32_t CommonNapi::GetPropertyString(napi_env env, napi_value config_obj, const std::string &type,
1809be168c0dSopenharmony_ci+                                      std::string &result) {
1810be168c0dSopenharmony_ci+  napi_value item = nullptr;
1811be168c0dSopenharmony_ci+  bool exist = false;
1812be168c0dSopenharmony_ci+  char buffer[SIZE];
1813be168c0dSopenharmony_ci+  size_t length = 0;
1814be168c0dSopenharmony_ci+
1815be168c0dSopenharmony_ci+  napi_status status = napi_has_named_property(env, config_obj, type.c_str(), &exist);
1816be168c0dSopenharmony_ci+
1817be168c0dSopenharmony_ci+  if (status != napi_ok || !exist) {
1818be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "can not find " << type.c_str() << "will set default value";
1819be168c0dSopenharmony_ci+    return ERR_NOT_EXISTED_PARAM;
1820be168c0dSopenharmony_ci+  }
1821be168c0dSopenharmony_ci+
1822be168c0dSopenharmony_ci+  if (napi_get_named_property(env, config_obj, type.c_str(), &item) != napi_ok) {
1823be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "fail to get property: " << type.c_str();
1824be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
1825be168c0dSopenharmony_ci+  }
1826be168c0dSopenharmony_ci+
1827be168c0dSopenharmony_ci+  if (napi_get_value_string_utf8(env, item, buffer, SIZE, &length) != napi_ok) {
1828be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "fail to get property value " << type.c_str();
1829be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
1830be168c0dSopenharmony_ci+  }
1831be168c0dSopenharmony_ci+  result = std::string(buffer);
1832be168c0dSopenharmony_ci+  return SUCCESS;
1833be168c0dSopenharmony_ci+}
1834be168c0dSopenharmony_ci+
1835be168c0dSopenharmony_ci+int32_t CommonNapi::GetPropertyBigIntUint64(napi_env env, napi_value config_obj, const std::string &type,
1836be168c0dSopenharmony_ci+                                uint64_t &result) {
1837be168c0dSopenharmony_ci+  napi_value item = nullptr;
1838be168c0dSopenharmony_ci+  bool exist = false;
1839be168c0dSopenharmony_ci+  napi_status status = napi_has_named_property(env, config_obj, type.c_str(), &exist);
1840be168c0dSopenharmony_ci+
1841be168c0dSopenharmony_ci+  if (status != napi_ok || !exist) {
1842be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "can not find " << type.c_str() << " will set default value";
1843be168c0dSopenharmony_ci+    return ERR_NOT_EXISTED_PARAM;
1844be168c0dSopenharmony_ci+  }
1845be168c0dSopenharmony_ci+
1846be168c0dSopenharmony_ci+  if (napi_get_named_property(env, config_obj, type.c_str(), &item) != napi_ok) {
1847be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "fail to get property: " << type.c_str();
1848be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
1849be168c0dSopenharmony_ci+  }
1850be168c0dSopenharmony_ci+
1851be168c0dSopenharmony_ci+  bool lossless = false;
1852be168c0dSopenharmony_ci+  if (napi_get_value_bigint_uint64(env, item, &result, &lossless) != napi_ok) {
1853be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "fail to get property value " << type.c_str();
1854be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
1855be168c0dSopenharmony_ci+  }
1856be168c0dSopenharmony_ci+
1857be168c0dSopenharmony_ci+  if (!lossless) {
1858be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "get uint64_t loss precision !";
1859be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
1860be168c0dSopenharmony_ci+  }
1861be168c0dSopenharmony_ci+  return SUCCESS;
1862be168c0dSopenharmony_ci+}
1863be168c0dSopenharmony_ci+
1864be168c0dSopenharmony_ci+int32_t CommonNapi::GetPropertyInt32Array(napi_env env, napi_value config_obj, const std::string &type,
1865be168c0dSopenharmony_ci+                                          std::vector<int32_t> &result) {
1866be168c0dSopenharmony_ci+  napi_value item = nullptr;
1867be168c0dSopenharmony_ci+  bool exist = false;
1868be168c0dSopenharmony_ci+  napi_status status = napi_has_named_property(env, config_obj, type.c_str(), &exist);
1869be168c0dSopenharmony_ci+  if (status != napi_ok || !exist) {
1870be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "can not find " << type.c_str() << "will set default value";
1871be168c0dSopenharmony_ci+    return ERR_NOT_EXISTED_PARAM;
1872be168c0dSopenharmony_ci+  }
1873be168c0dSopenharmony_ci+
1874be168c0dSopenharmony_ci+  if (napi_get_named_property(env, config_obj, type.c_str(), &item) != napi_ok) {
1875be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "fail to get property: " << type.c_str();
1876be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
1877be168c0dSopenharmony_ci+  }
1878be168c0dSopenharmony_ci+
1879be168c0dSopenharmony_ci+  uint32_t array_length = 0;
1880be168c0dSopenharmony_ci+  status = napi_get_array_length(env, item, &array_length);
1881be168c0dSopenharmony_ci+  if (status != napi_ok || array_length < 0) {
1882be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "can not get array length.";
1883be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
1884be168c0dSopenharmony_ci+  }
1885be168c0dSopenharmony_ci+
1886be168c0dSopenharmony_ci+  if (array_length == 0) {
1887be168c0dSopenharmony_ci+    return SUCCESS;
1888be168c0dSopenharmony_ci+  }
1889be168c0dSopenharmony_ci+
1890be168c0dSopenharmony_ci+  for (size_t i = 0; i < array_length; i++) {
1891be168c0dSopenharmony_ci+    int32_t int_value = {0};
1892be168c0dSopenharmony_ci+    napi_value element = nullptr;
1893be168c0dSopenharmony_ci+    status = napi_get_element(env, item, i, &element);
1894be168c0dSopenharmony_ci+    if (status != napi_ok) {
1895be168c0dSopenharmony_ci+      MS_LOG(WARNING) << "can not get element";
1896be168c0dSopenharmony_ci+      return ERR_INVALID_PARAM;
1897be168c0dSopenharmony_ci+    }
1898be168c0dSopenharmony_ci+
1899be168c0dSopenharmony_ci+    if (napi_get_value_int32(env, element, &int_value) != napi_ok) {
1900be168c0dSopenharmony_ci+      MS_LOG(WARNING) << "get " << type.c_str() << " property value fail";
1901be168c0dSopenharmony_ci+      return ERR_INVALID_PARAM;
1902be168c0dSopenharmony_ci+    }
1903be168c0dSopenharmony_ci+    result.push_back(int_value);
1904be168c0dSopenharmony_ci+  }
1905be168c0dSopenharmony_ci+
1906be168c0dSopenharmony_ci+  return SUCCESS;
1907be168c0dSopenharmony_ci+}
1908be168c0dSopenharmony_ci+
1909be168c0dSopenharmony_ci+int32_t CommonNapi::GetPropertyStringArray(napi_env env, napi_value config_obj, const std::string &type,
1910be168c0dSopenharmony_ci+                                           std::vector<std::string> &result) {
1911be168c0dSopenharmony_ci+  napi_value item = nullptr;
1912be168c0dSopenharmony_ci+  bool exist = false;
1913be168c0dSopenharmony_ci+  napi_status status = napi_has_named_property(env, config_obj, type.c_str(), &exist);
1914be168c0dSopenharmony_ci+
1915be168c0dSopenharmony_ci+  if (status != napi_ok || !exist) {
1916be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "can not find " << type.c_str() << "will set default value";
1917be168c0dSopenharmony_ci+    return ERR_NOT_EXISTED_PARAM;
1918be168c0dSopenharmony_ci+  }
1919be168c0dSopenharmony_ci+
1920be168c0dSopenharmony_ci+  if (napi_get_named_property(env, config_obj, type.c_str(), &item) != napi_ok) {
1921be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "fail to get property: " << type.c_str();
1922be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
1923be168c0dSopenharmony_ci+  }
1924be168c0dSopenharmony_ci+
1925be168c0dSopenharmony_ci+  uint32_t array_length = 0;
1926be168c0dSopenharmony_ci+  status = napi_get_array_length(env, item, &array_length);
1927be168c0dSopenharmony_ci+  if (status != napi_ok || array_length <= 0) {
1928be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "can not get array length";
1929be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
1930be168c0dSopenharmony_ci+  }
1931be168c0dSopenharmony_ci+
1932be168c0dSopenharmony_ci+  for (size_t i = 0; i < array_length; i++) {
1933be168c0dSopenharmony_ci+    char buffer[SIZE];
1934be168c0dSopenharmony_ci+    size_t length = 0;
1935be168c0dSopenharmony_ci+
1936be168c0dSopenharmony_ci+    napi_value element = nullptr;
1937be168c0dSopenharmony_ci+    status = napi_get_element(env, item, i, &element);
1938be168c0dSopenharmony_ci+    if (status != napi_ok) {
1939be168c0dSopenharmony_ci+      MS_LOG(WARNING) << "can not get element";
1940be168c0dSopenharmony_ci+      return ERR_INVALID_PARAM;
1941be168c0dSopenharmony_ci+    }
1942be168c0dSopenharmony_ci+
1943be168c0dSopenharmony_ci+    if (napi_get_value_string_utf8(env, element, buffer, SIZE, &length) != napi_ok) {
1944be168c0dSopenharmony_ci+      MS_LOG(WARNING) << "fail to get property value " << type.c_str();
1945be168c0dSopenharmony_ci+      return ERR_INVALID_PARAM;
1946be168c0dSopenharmony_ci+    }
1947be168c0dSopenharmony_ci+    result.push_back(std::string(buffer));
1948be168c0dSopenharmony_ci+  }
1949be168c0dSopenharmony_ci+
1950be168c0dSopenharmony_ci+  return SUCCESS;
1951be168c0dSopenharmony_ci+}
1952be168c0dSopenharmony_ci+
1953be168c0dSopenharmony_ci+int32_t CommonNapi::GetStringArray(napi_env env, napi_value value, std::vector<std::string> &result) {
1954be168c0dSopenharmony_ci+  uint32_t array_length = 0;
1955be168c0dSopenharmony_ci+  auto status = napi_get_array_length(env, value, &array_length);
1956be168c0dSopenharmony_ci+  if (status != napi_ok || array_length <= 0) {
1957be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "can not get array length";
1958be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
1959be168c0dSopenharmony_ci+  }
1960be168c0dSopenharmony_ci+
1961be168c0dSopenharmony_ci+  for (size_t i = 0; i < array_length; i++) {
1962be168c0dSopenharmony_ci+    char buffer[SIZE];
1963be168c0dSopenharmony_ci+    size_t length = 0;
1964be168c0dSopenharmony_ci+
1965be168c0dSopenharmony_ci+    napi_value element = nullptr;
1966be168c0dSopenharmony_ci+    status = napi_get_element(env, value, i, &element);
1967be168c0dSopenharmony_ci+    if (status != napi_ok) {
1968be168c0dSopenharmony_ci+      MS_LOG(WARNING) << "can not get element";
1969be168c0dSopenharmony_ci+      return ERR_INVALID_PARAM;
1970be168c0dSopenharmony_ci+    }
1971be168c0dSopenharmony_ci+
1972be168c0dSopenharmony_ci+    if (napi_get_value_string_utf8(env, element, buffer, SIZE, &length) != napi_ok) {
1973be168c0dSopenharmony_ci+      MS_LOG(WARNING) << "fail to get string_utf8 value";
1974be168c0dSopenharmony_ci+      return ERR_INVALID_PARAM;
1975be168c0dSopenharmony_ci+    }
1976be168c0dSopenharmony_ci+    result.push_back(std::string(buffer));
1977be168c0dSopenharmony_ci+  }
1978be168c0dSopenharmony_ci+
1979be168c0dSopenharmony_ci+  return SUCCESS;
1980be168c0dSopenharmony_ci+}
1981be168c0dSopenharmony_ci+
1982be168c0dSopenharmony_ci+void CommonNapi::WriteTensorData(MSTensor tensor, std::string file_path) {
1983be168c0dSopenharmony_ci+  std::ofstream out_file;
1984be168c0dSopenharmony_ci+  out_file.open(file_path, std::ios::out | std::ios::app);
1985be168c0dSopenharmony_ci+  if (!out_file.is_open()) {
1986be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "output file open failed";
1987be168c0dSopenharmony_ci+    return;
1988be168c0dSopenharmony_ci+  }
1989be168c0dSopenharmony_ci+  auto out_data = reinterpret_cast<const float *>(tensor.Data().get());
1990be168c0dSopenharmony_ci+  out_file << tensor.Name() << " ";
1991be168c0dSopenharmony_ci+  for (auto dim : tensor.Shape()) {
1992be168c0dSopenharmony_ci+    out_file << dim << " ";
1993be168c0dSopenharmony_ci+  }
1994be168c0dSopenharmony_ci+  out_file << std::endl;
1995be168c0dSopenharmony_ci+  for (int i = 0; i < tensor.ElementNum(); i++) {
1996be168c0dSopenharmony_ci+    out_file << out_data[i] << " ";
1997be168c0dSopenharmony_ci+  }
1998be168c0dSopenharmony_ci+  out_file << std::endl;
1999be168c0dSopenharmony_ci+  out_file.close();
2000be168c0dSopenharmony_ci+}
2001be168c0dSopenharmony_ci+
2002be168c0dSopenharmony_ci+void CommonNapi::WriteOutputsData(const std::vector<MSTensor> outputs, std::string file_path) {
2003be168c0dSopenharmony_ci+  std::ofstream out_file;
2004be168c0dSopenharmony_ci+  out_file.open(file_path, std::ios::out | std::ios::app);
2005be168c0dSopenharmony_ci+  if (!out_file.is_open()) {
2006be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "output file open failed";
2007be168c0dSopenharmony_ci+    return;
2008be168c0dSopenharmony_ci+  }
2009be168c0dSopenharmony_ci+  for (auto tensor : outputs) {
2010be168c0dSopenharmony_ci+    MS_LOG(INFO) << "tensor name is: " << tensor.Name().c_str()
2011be168c0dSopenharmony_ci+                 << "tensor size is: " << static_cast<int>(tensor.DataSize())
2012be168c0dSopenharmony_ci+                 << "tensor elements num is: " << static_cast<int>(tensor.ElementNum());
2013be168c0dSopenharmony_ci+    // dtype float
2014be168c0dSopenharmony_ci+    auto out_data = reinterpret_cast<const float *>(tensor.Data().get());
2015be168c0dSopenharmony_ci+    out_file << tensor.Name() << " ";
2016be168c0dSopenharmony_ci+    for (auto dim : tensor.Shape()) {
2017be168c0dSopenharmony_ci+      out_file << dim << " ";
2018be168c0dSopenharmony_ci+    }
2019be168c0dSopenharmony_ci+    out_file << std::endl;
2020be168c0dSopenharmony_ci+    for (int i = 0; i < tensor.ElementNum(); i++) {
2021be168c0dSopenharmony_ci+      out_file << out_data[i] << " ";
2022be168c0dSopenharmony_ci+    }
2023be168c0dSopenharmony_ci+    out_file << std::endl;
2024be168c0dSopenharmony_ci+  }
2025be168c0dSopenharmony_ci+  out_file.close();
2026be168c0dSopenharmony_ci+}
2027be168c0dSopenharmony_ci+
2028be168c0dSopenharmony_ci+}  // namespace mindspore
2029be168c0dSopenharmony_ci\ No newline at end of file
2030be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/litert/js_api/mslite_model_napi.cc b/mindspore/lite/src/litert/js_api/mslite_model_napi.cc
2031be168c0dSopenharmony_cinew file mode 100644
2032be168c0dSopenharmony_ciindex 00000000..cfc71762
2033be168c0dSopenharmony_ci--- /dev/null
2034be168c0dSopenharmony_ci+++ b/mindspore/lite/src/litert/js_api/mslite_model_napi.cc
2035be168c0dSopenharmony_ci@@ -0,0 +1,2653 @@
2036be168c0dSopenharmony_ci+/**
2037be168c0dSopenharmony_ci+ * Copyright 2023 Huawei Technologies Co., Ltd
2038be168c0dSopenharmony_ci+ *
2039be168c0dSopenharmony_ci+ * Licensed under the Apache License, Version 2.0 (the "License");
2040be168c0dSopenharmony_ci+ * you may not use this file except in compliance with the License.
2041be168c0dSopenharmony_ci+ * You may obtain a copy of the License at
2042be168c0dSopenharmony_ci+ *
2043be168c0dSopenharmony_ci+ * http://www.apache.org/licenses/LICENSE-2.0
2044be168c0dSopenharmony_ci+ *
2045be168c0dSopenharmony_ci+ * Unless required by applicable law or agreed to in writing, software
2046be168c0dSopenharmony_ci+ * distributed under the License is distributed on an "AS IS" BASIS,
2047be168c0dSopenharmony_ci+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2048be168c0dSopenharmony_ci+ * See the License for the specific language governing permissions and
2049be168c0dSopenharmony_ci+ * limitations under the License.
2050be168c0dSopenharmony_ci+ */
2051be168c0dSopenharmony_ci+#include "include/js_api/mslite_model_napi.h"
2052be168c0dSopenharmony_ci+#include <climits>
2053be168c0dSopenharmony_ci+#include <algorithm>
2054be168c0dSopenharmony_ci+#include <random>
2055be168c0dSopenharmony_ci+#include <cstring>
2056be168c0dSopenharmony_ci+#include <memory>
2057be168c0dSopenharmony_ci+#include <map>
2058be168c0dSopenharmony_ci+#include <vector>
2059be168c0dSopenharmony_ci+#include <unistd.h>
2060be168c0dSopenharmony_ci+#include <fcntl.h>
2061be168c0dSopenharmony_ci+#include <sys/mman.h>
2062be168c0dSopenharmony_ci+#include <sys/stat.h>
2063be168c0dSopenharmony_ci+#include "include/js_api/mstensor_napi.h"
2064be168c0dSopenharmony_ci+#include "include/js_api/common_napi.h"
2065be168c0dSopenharmony_ci+#include "include/js_api/ms_parameters_napi.h"
2066be168c0dSopenharmony_ci+#include "include/js_api/ms_errors.h"
2067be168c0dSopenharmony_ci+#include "include/js_api/mslite_model_callback_napi.h"
2068be168c0dSopenharmony_ci+#include "src/common/log.h"
2069be168c0dSopenharmony_ci+#include "mindspore/lite/src/common/log.h"
2070be168c0dSopenharmony_ci+#include "include/c_api/model_c.h"
2071be168c0dSopenharmony_ci+#include "include/c_api/context_c.h"
2072be168c0dSopenharmony_ci+#include "include/c_api/types_c.h"
2073be168c0dSopenharmony_ci+#include "include/js_api/nnrt_device_desc_napi.h"
2074be168c0dSopenharmony_ci+
2075be168c0dSopenharmony_ci+namespace mindspore {
2076be168c0dSopenharmony_ci+thread_local napi_ref MSLiteModelNapi::constructor_ = nullptr;
2077be168c0dSopenharmony_ci+ModelInfo *MSLiteModelNapi::model_info_ = nullptr;
2078be168c0dSopenharmony_ci+ContextInfo *MSLiteModelNapi::context_ = nullptr;
2079be168c0dSopenharmony_ci+std::mutex MSLiteModelNapi::create_mutex_;
2080be168c0dSopenharmony_ci+napi_ref MSLiteModelNapi::tensorFormat_ = nullptr;
2081be168c0dSopenharmony_ci+napi_ref MSLiteModelNapi::tensorDataType_ = nullptr;
2082be168c0dSopenharmony_ci+napi_ref MSLiteModelNapi::contextThreadAffinityMode_ = nullptr;
2083be168c0dSopenharmony_ci+napi_ref MSLiteModelNapi::contextQuantizationType_ = nullptr;
2084be168c0dSopenharmony_ci+napi_ref MSLiteModelNapi::contextOptimizationLevel_ = nullptr;
2085be168c0dSopenharmony_ci+napi_ref MSLiteModelNapi::contextPerformanceMode_ = nullptr;
2086be168c0dSopenharmony_ci+napi_ref MSLiteModelNapi::contextPriority_ = nullptr;
2087be168c0dSopenharmony_ci+napi_ref MSLiteModelNapi::contextNnrtDeviceType_ = nullptr;
2088be168c0dSopenharmony_ci+
2089be168c0dSopenharmony_ci+#define GET_PARAMS(env, info, num) \
2090be168c0dSopenharmony_ci+  size_t argc = num;               \
2091be168c0dSopenharmony_ci+  napi_value argv[num] = {0};      \
2092be168c0dSopenharmony_ci+  napi_value thisVar = nullptr;    \
2093be168c0dSopenharmony_ci+  void *data;                      \
2094be168c0dSopenharmony_ci+  napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)
2095be168c0dSopenharmony_ci+
2096be168c0dSopenharmony_ci+namespace {
2097be168c0dSopenharmony_ci+const int ARGS_ONE = 1;
2098be168c0dSopenharmony_ci+const int ARGS_TWO = 2;
2099be168c0dSopenharmony_ci+const int ARGS_THREE = 3;
2100be168c0dSopenharmony_ci+const int ARGS_FOUR = 4;
2101be168c0dSopenharmony_ci+
2102be168c0dSopenharmony_ci+const int PARAM0 = 0;
2103be168c0dSopenharmony_ci+const int PARAM1 = 1;
2104be168c0dSopenharmony_ci+const int PARAM2 = 2;
2105be168c0dSopenharmony_ci+const int PARAM3 = 3;
2106be168c0dSopenharmony_ci+const int PARAM4 = 4;
2107be168c0dSopenharmony_ci+const int UNSET_VALUE = -1;
2108be168c0dSopenharmony_ci+
2109be168c0dSopenharmony_ci+const int SIZE = 100;
2110be168c0dSopenharmony_ci+
2111be168c0dSopenharmony_ci+const std::string CLASS_NAME = "Model";
2112be168c0dSopenharmony_ci+
2113be168c0dSopenharmony_ci+const std::unordered_map<std::string, DeviceType> kDeviceTypes{
2114be168c0dSopenharmony_ci+  {"cpu", kCPU},
2115be168c0dSopenharmony_ci+  {"nnrt", kNNRt},
2116be168c0dSopenharmony_ci+  {"gpu", kGPU},
2117be168c0dSopenharmony_ci+};
2118be168c0dSopenharmony_ci+}  // namespace
2119be168c0dSopenharmony_ci+
2120be168c0dSopenharmony_ci+MSLiteModelNapi::MSLiteModelNapi() : native_model_(nullptr), env_(nullptr) {
2121be168c0dSopenharmony_ci+  MS_LOG(INFO) << "MSLiteModelNapi Instances create.";
2122be168c0dSopenharmony_ci+}
2123be168c0dSopenharmony_ci+
2124be168c0dSopenharmony_ci+MSLiteModelNapi::~MSLiteModelNapi() {
2125be168c0dSopenharmony_ci+  native_model_ = nullptr;
2126be168c0dSopenharmony_ci+  env_ = nullptr;
2127be168c0dSopenharmony_ci+  MS_LOG(INFO) << "MSLiteModelNapi Instances destroy.";
2128be168c0dSopenharmony_ci+}
2129be168c0dSopenharmony_ci+
2130be168c0dSopenharmony_ci+void MSLiteModelNapi::Finalize(napi_env env, void *nativeObject, void *finalize) {
2131be168c0dSopenharmony_ci+  (void)env;
2132be168c0dSopenharmony_ci+  (void)finalize;
2133be168c0dSopenharmony_ci+  if (nativeObject != nullptr) {
2134be168c0dSopenharmony_ci+    // delete nativeObject
2135be168c0dSopenharmony_ci+    auto obj = static_cast<MSLiteModelNapi *>(nativeObject);
2136be168c0dSopenharmony_ci+    delete obj;
2137be168c0dSopenharmony_ci+    obj = nullptr;
2138be168c0dSopenharmony_ci+  }
2139be168c0dSopenharmony_ci+  MS_LOG(INFO) << "Finalize success";
2140be168c0dSopenharmony_ci+}
2141be168c0dSopenharmony_ci+
2142be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::Init(napi_env env, napi_value exports) {
2143be168c0dSopenharmony_ci+  napi_property_descriptor properties[] = {
2144be168c0dSopenharmony_ci+    DECLARE_NAPI_FUNCTION("getInputs", GetInputs),
2145be168c0dSopenharmony_ci+    DECLARE_NAPI_FUNCTION("resize", Resize),
2146be168c0dSopenharmony_ci+    DECLARE_NAPI_FUNCTION("predict", PredictAsync),
2147be168c0dSopenharmony_ci+    DECLARE_NAPI_FUNCTION("runStep", RunStep),
2148be168c0dSopenharmony_ci+    DECLARE_NAPI_FUNCTION("getWeights", GetWeights),
2149be168c0dSopenharmony_ci+    DECLARE_NAPI_FUNCTION("updateWeights", UpdateWeights),
2150be168c0dSopenharmony_ci+    DECLARE_NAPI_FUNCTION("setupVirtualBatch", SetupVirtualBatch),
2151be168c0dSopenharmony_ci+    DECLARE_NAPI_FUNCTION("exportModel", ExportModel),
2152be168c0dSopenharmony_ci+    DECLARE_NAPI_FUNCTION("exportWeightsCollaborateWithMicro", ExportWeightsCollaborateWithMicro),
2153be168c0dSopenharmony_ci+    DECLARE_NAPI_GETTER_SETTER("trainMode", GetTrainMode, SetTrainMode),
2154be168c0dSopenharmony_ci+    DECLARE_NAPI_GETTER_SETTER("learningRate", GetLearningRate, SetLearningRate),
2155be168c0dSopenharmony_ci+    };
2156be168c0dSopenharmony_ci+
2157be168c0dSopenharmony_ci+  napi_property_descriptor staticProperty[] = {
2158be168c0dSopenharmony_ci+    DECLARE_NAPI_STATIC_FUNCTION("loadModelFromFile", LoadMSLiteModelFromFile),
2159be168c0dSopenharmony_ci+    DECLARE_NAPI_STATIC_FUNCTION("loadModelFromBuffer", LoadMSLiteModelFromBuffer),
2160be168c0dSopenharmony_ci+    DECLARE_NAPI_STATIC_FUNCTION("loadModelFromFd", LoadMSLiteModelFromFd),
2161be168c0dSopenharmony_ci+    DECLARE_NAPI_STATIC_FUNCTION("loadTrainModelFromFile", LoadMSLiteTrainModelFromFile),
2162be168c0dSopenharmony_ci+    DECLARE_NAPI_STATIC_FUNCTION("loadTrainModelFromBuffer", LoadMSLiteTrainModelFromBuffer),
2163be168c0dSopenharmony_ci+    DECLARE_NAPI_STATIC_FUNCTION("loadTrainModelFromFd", LoadMSLiteTrainModelFromFd),
2164be168c0dSopenharmony_ci+    DECLARE_NAPI_STATIC_FUNCTION("getAllNNRTDeviceDescriptions", GetAllNnrtDeviceDescs),
2165be168c0dSopenharmony_ci+    DECLARE_NAPI_PROPERTY("Format", CreateFormatObject(env)),
2166be168c0dSopenharmony_ci+    DECLARE_NAPI_PROPERTY("DataType", CreateDataTypeObject(env)),
2167be168c0dSopenharmony_ci+    DECLARE_NAPI_PROPERTY("ThreadAffinityMode", CreateThreadAffinityModeObject(env)),
2168be168c0dSopenharmony_ci+    DECLARE_NAPI_PROPERTY("QuantizationType", CreateQuantizationTypeObject(env)),
2169be168c0dSopenharmony_ci+    DECLARE_NAPI_PROPERTY("OptimizationLevel", CreateOptimizationLevelObject(env)),
2170be168c0dSopenharmony_ci+    DECLARE_NAPI_PROPERTY("PerformanceMode", CreatePerformanceModeObject(env)),
2171be168c0dSopenharmony_ci+    DECLARE_NAPI_PROPERTY("Priority", CreatePriorityObject(env)),
2172be168c0dSopenharmony_ci+    DECLARE_NAPI_PROPERTY("NNRTDeviceType", CreateNnrtDeviceTypeObject(env)),
2173be168c0dSopenharmony_ci+  };
2174be168c0dSopenharmony_ci+
2175be168c0dSopenharmony_ci+  napi_value constructor = nullptr;
2176be168c0dSopenharmony_ci+  napi_status status = napi_define_class(env, CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Constructor, nullptr,
2177be168c0dSopenharmony_ci+                                         sizeof(properties) / sizeof(properties[0]), properties, &constructor);
2178be168c0dSopenharmony_ci+  if (status != napi_ok) {
2179be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Failed to define MSLiteModel class";
2180be168c0dSopenharmony_ci+    return nullptr;
2181be168c0dSopenharmony_ci+  }
2182be168c0dSopenharmony_ci+
2183be168c0dSopenharmony_ci+  status = napi_create_reference(env, constructor, REFERENCE_CREATION_COUNT, &constructor_);
2184be168c0dSopenharmony_ci+  if (status != napi_ok) {
2185be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Failed to create reference of constructor";
2186be168c0dSopenharmony_ci+    return nullptr;
2187be168c0dSopenharmony_ci+  }
2188be168c0dSopenharmony_ci+
2189be168c0dSopenharmony_ci+  status = napi_set_named_property(env, exports, CLASS_NAME.c_str(), constructor);
2190be168c0dSopenharmony_ci+  if (status != napi_ok) {
2191be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Failed to set constructor";
2192be168c0dSopenharmony_ci+    return nullptr;
2193be168c0dSopenharmony_ci+  }
2194be168c0dSopenharmony_ci+
2195be168c0dSopenharmony_ci+  status = napi_define_properties(env, exports, sizeof(staticProperty) / sizeof(staticProperty[0]), staticProperty);
2196be168c0dSopenharmony_ci+  if (status != napi_ok) {
2197be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Failed to define static function";
2198be168c0dSopenharmony_ci+    return nullptr;
2199be168c0dSopenharmony_ci+  }
2200be168c0dSopenharmony_ci+
2201be168c0dSopenharmony_ci+  MS_LOG(INFO) << "init success";
2202be168c0dSopenharmony_ci+  return exports;
2203be168c0dSopenharmony_ci+}
2204be168c0dSopenharmony_ci+
2205be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::CreateFormatObject(napi_env env)
2206be168c0dSopenharmony_ci+{
2207be168c0dSopenharmony_ci+  napi_value result = nullptr;
2208be168c0dSopenharmony_ci+  napi_status status;
2209be168c0dSopenharmony_ci+  std::string propName;
2210be168c0dSopenharmony_ci+  int32_t refCount = 1;
2211be168c0dSopenharmony_ci+
2212be168c0dSopenharmony_ci+  status = napi_create_object(env, &result);
2213be168c0dSopenharmony_ci+  if (status == napi_ok) {
2214be168c0dSopenharmony_ci+    for (auto &iter : tensorFormatMap) {
2215be168c0dSopenharmony_ci+      propName = iter.first;
2216be168c0dSopenharmony_ci+      status = AddNamedProperty(env, result, propName, iter.second);
2217be168c0dSopenharmony_ci+      if (status != napi_ok) {
2218be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Failed to add named prop in CreateFormatObject.";
2219be168c0dSopenharmony_ci+        break;
2220be168c0dSopenharmony_ci+      }
2221be168c0dSopenharmony_ci+      propName.clear();
2222be168c0dSopenharmony_ci+    }
2223be168c0dSopenharmony_ci+    if (status == napi_ok) {
2224be168c0dSopenharmony_ci+      status = napi_create_reference(env, result, refCount, &tensorFormat_);
2225be168c0dSopenharmony_ci+      if (status == napi_ok) {
2226be168c0dSopenharmony_ci+        return result;
2227be168c0dSopenharmony_ci+      }
2228be168c0dSopenharmony_ci+    }
2229be168c0dSopenharmony_ci+  }
2230be168c0dSopenharmony_ci+  MS_LOG(ERROR) << "CreateFormatObject is Failed!";
2231be168c0dSopenharmony_ci+  napi_get_undefined(env, &result);
2232be168c0dSopenharmony_ci+  return result;
2233be168c0dSopenharmony_ci+}
2234be168c0dSopenharmony_ci+
2235be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::CreateDataTypeObject(napi_env env) {
2236be168c0dSopenharmony_ci+  napi_value result = nullptr;
2237be168c0dSopenharmony_ci+  napi_status status;
2238be168c0dSopenharmony_ci+  std::string propName;
2239be168c0dSopenharmony_ci+  int32_t refCount = 1;
2240be168c0dSopenharmony_ci+
2241be168c0dSopenharmony_ci+  status = napi_create_object(env, &result);
2242be168c0dSopenharmony_ci+  if (status == napi_ok) {
2243be168c0dSopenharmony_ci+    for (auto &iter : tensorDataTypeMap) {
2244be168c0dSopenharmony_ci+      propName = iter.first;
2245be168c0dSopenharmony_ci+      status = AddNamedProperty(env, result, propName, iter.second);
2246be168c0dSopenharmony_ci+      if (status != napi_ok) {
2247be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Failed to add named prop in CreateDataTypeObject.";
2248be168c0dSopenharmony_ci+        break;
2249be168c0dSopenharmony_ci+      }
2250be168c0dSopenharmony_ci+      propName.clear();
2251be168c0dSopenharmony_ci+    }
2252be168c0dSopenharmony_ci+    if (status == napi_ok) {
2253be168c0dSopenharmony_ci+      status = napi_create_reference(env, result, refCount, &tensorDataType_);
2254be168c0dSopenharmony_ci+      if (status == napi_ok) {
2255be168c0dSopenharmony_ci+        return result;
2256be168c0dSopenharmony_ci+      }
2257be168c0dSopenharmony_ci+    }
2258be168c0dSopenharmony_ci+  }
2259be168c0dSopenharmony_ci+  MS_LOG(ERROR) << "CreateDataTypeObject is Failed!";
2260be168c0dSopenharmony_ci+  napi_get_undefined(env, &result);
2261be168c0dSopenharmony_ci+  return result;
2262be168c0dSopenharmony_ci+}
2263be168c0dSopenharmony_ci+
2264be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::CreateThreadAffinityModeObject(napi_env env) {
2265be168c0dSopenharmony_ci+  napi_value result = nullptr;
2266be168c0dSopenharmony_ci+  napi_status status;
2267be168c0dSopenharmony_ci+  std::string propName;
2268be168c0dSopenharmony_ci+  int32_t refCount = 1;
2269be168c0dSopenharmony_ci+
2270be168c0dSopenharmony_ci+  status = napi_create_object(env, &result);
2271be168c0dSopenharmony_ci+  if (status == napi_ok) {
2272be168c0dSopenharmony_ci+    for (auto &iter : contextThreadAffinityModeMap) {
2273be168c0dSopenharmony_ci+      propName = iter.first;
2274be168c0dSopenharmony_ci+      status = AddNamedProperty(env, result, propName, iter.second);
2275be168c0dSopenharmony_ci+      if (status != napi_ok) {
2276be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Failed to add named prop in CreateThreadAffinityModeObject.";
2277be168c0dSopenharmony_ci+        break;
2278be168c0dSopenharmony_ci+      }
2279be168c0dSopenharmony_ci+      propName.clear();
2280be168c0dSopenharmony_ci+    }
2281be168c0dSopenharmony_ci+    if (status == napi_ok) {
2282be168c0dSopenharmony_ci+      status = napi_create_reference(env, result, refCount, &contextThreadAffinityMode_);
2283be168c0dSopenharmony_ci+      if (status == napi_ok) {
2284be168c0dSopenharmony_ci+        return result;
2285be168c0dSopenharmony_ci+      }
2286be168c0dSopenharmony_ci+    }
2287be168c0dSopenharmony_ci+  }
2288be168c0dSopenharmony_ci+  MS_LOG(ERROR) << "CreateThreadAffinityModeObject is Failed!";
2289be168c0dSopenharmony_ci+  napi_get_undefined(env, &result);
2290be168c0dSopenharmony_ci+  return result;
2291be168c0dSopenharmony_ci+}
2292be168c0dSopenharmony_ci+
2293be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::CreateQuantizationTypeObject(napi_env env) {
2294be168c0dSopenharmony_ci+  napi_value result = nullptr;
2295be168c0dSopenharmony_ci+  napi_status status;
2296be168c0dSopenharmony_ci+  std::string propName;
2297be168c0dSopenharmony_ci+  int32_t refCount = 1;
2298be168c0dSopenharmony_ci+
2299be168c0dSopenharmony_ci+  status = napi_create_object(env, &result);
2300be168c0dSopenharmony_ci+  if (status == napi_ok) {
2301be168c0dSopenharmony_ci+    for (auto &iter : contextQuantizationTypeMap) {
2302be168c0dSopenharmony_ci+      propName = iter.first;
2303be168c0dSopenharmony_ci+      status = AddNamedProperty(env, result, propName, iter.second);
2304be168c0dSopenharmony_ci+      if (status != napi_ok) {
2305be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Failed to add named prop in CreateQuantizationTypeObject.";
2306be168c0dSopenharmony_ci+        break;
2307be168c0dSopenharmony_ci+      }
2308be168c0dSopenharmony_ci+      propName.clear();
2309be168c0dSopenharmony_ci+    }
2310be168c0dSopenharmony_ci+    if (status == napi_ok) {
2311be168c0dSopenharmony_ci+      status = napi_create_reference(env, result, refCount, &contextQuantizationType_);
2312be168c0dSopenharmony_ci+      if (status == napi_ok) {
2313be168c0dSopenharmony_ci+        return result;
2314be168c0dSopenharmony_ci+      }
2315be168c0dSopenharmony_ci+    }
2316be168c0dSopenharmony_ci+  }
2317be168c0dSopenharmony_ci+  MS_LOG(ERROR) << "CreateQuantizationTypeObject is Failed!";
2318be168c0dSopenharmony_ci+  napi_get_undefined(env, &result);
2319be168c0dSopenharmony_ci+  return result;
2320be168c0dSopenharmony_ci+}
2321be168c0dSopenharmony_ci+
2322be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::CreateOptimizationLevelObject(napi_env env) {
2323be168c0dSopenharmony_ci+  napi_value result = nullptr;
2324be168c0dSopenharmony_ci+  napi_status status;
2325be168c0dSopenharmony_ci+  std::string propName;
2326be168c0dSopenharmony_ci+  int32_t refCount = 1;
2327be168c0dSopenharmony_ci+
2328be168c0dSopenharmony_ci+  status = napi_create_object(env, &result);
2329be168c0dSopenharmony_ci+  if (status == napi_ok) {
2330be168c0dSopenharmony_ci+    for (auto &iter : contextOptimizationLevelTypeMap) {
2331be168c0dSopenharmony_ci+      propName = iter.first;
2332be168c0dSopenharmony_ci+      status = AddNamedProperty(env, result, propName, iter.second);
2333be168c0dSopenharmony_ci+      if (status != napi_ok) {
2334be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Failed to add named prop in CreateOptimizationLevelObject.";
2335be168c0dSopenharmony_ci+        break;
2336be168c0dSopenharmony_ci+      }
2337be168c0dSopenharmony_ci+      propName.clear();
2338be168c0dSopenharmony_ci+    }
2339be168c0dSopenharmony_ci+    if (status == napi_ok) {
2340be168c0dSopenharmony_ci+      status = napi_create_reference(env, result, refCount, &contextOptimizationLevel_);
2341be168c0dSopenharmony_ci+      if (status == napi_ok) {
2342be168c0dSopenharmony_ci+        return result;
2343be168c0dSopenharmony_ci+      }
2344be168c0dSopenharmony_ci+    }
2345be168c0dSopenharmony_ci+  }
2346be168c0dSopenharmony_ci+  MS_LOG(ERROR) << "CreateOptimizationLevelObject is Failed!";
2347be168c0dSopenharmony_ci+  napi_get_undefined(env, &result);
2348be168c0dSopenharmony_ci+  return result;
2349be168c0dSopenharmony_ci+}
2350be168c0dSopenharmony_ci+
2351be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::CreatePerformanceModeObject(napi_env env) {
2352be168c0dSopenharmony_ci+  napi_value result = nullptr;
2353be168c0dSopenharmony_ci+  napi_status status;
2354be168c0dSopenharmony_ci+  std::string propName;
2355be168c0dSopenharmony_ci+  int32_t refCount = 1;
2356be168c0dSopenharmony_ci+
2357be168c0dSopenharmony_ci+  status = napi_create_object(env, &result);
2358be168c0dSopenharmony_ci+  if (status == napi_ok) {
2359be168c0dSopenharmony_ci+    for (auto &iter : contextPerformanceModeTypeMap) {
2360be168c0dSopenharmony_ci+      propName = iter.first;
2361be168c0dSopenharmony_ci+      status = AddNamedProperty(env, result, propName, iter.second);
2362be168c0dSopenharmony_ci+      if (status != napi_ok) {
2363be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Failed to add named prop in CreatePerformanceModeObject.";
2364be168c0dSopenharmony_ci+        break;
2365be168c0dSopenharmony_ci+      }
2366be168c0dSopenharmony_ci+      propName.clear();
2367be168c0dSopenharmony_ci+    }
2368be168c0dSopenharmony_ci+    if (status == napi_ok) {
2369be168c0dSopenharmony_ci+      status = napi_create_reference(env, result, refCount, &contextPerformanceMode_);
2370be168c0dSopenharmony_ci+      if (status == napi_ok) {
2371be168c0dSopenharmony_ci+        return result;
2372be168c0dSopenharmony_ci+      }
2373be168c0dSopenharmony_ci+    }
2374be168c0dSopenharmony_ci+  }
2375be168c0dSopenharmony_ci+  MS_LOG(ERROR) << "CreatePerformanceModeObject is Failed!";
2376be168c0dSopenharmony_ci+  napi_get_undefined(env, &result);
2377be168c0dSopenharmony_ci+  return result;
2378be168c0dSopenharmony_ci+}
2379be168c0dSopenharmony_ci+
2380be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::CreatePriorityObject(napi_env env) {
2381be168c0dSopenharmony_ci+  napi_value result = nullptr;
2382be168c0dSopenharmony_ci+  napi_status status;
2383be168c0dSopenharmony_ci+  std::string propName;
2384be168c0dSopenharmony_ci+  int32_t refCount = 1;
2385be168c0dSopenharmony_ci+
2386be168c0dSopenharmony_ci+  status = napi_create_object(env, &result);
2387be168c0dSopenharmony_ci+  if (status == napi_ok) {
2388be168c0dSopenharmony_ci+    for (auto &iter : contextPriorityTypeMap) {
2389be168c0dSopenharmony_ci+      propName = iter.first;
2390be168c0dSopenharmony_ci+      status = AddNamedProperty(env, result, propName, iter.second);
2391be168c0dSopenharmony_ci+      if (status != napi_ok) {
2392be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Failed to add named prop in CreatePriorityObject.";
2393be168c0dSopenharmony_ci+        break;
2394be168c0dSopenharmony_ci+      }
2395be168c0dSopenharmony_ci+      propName.clear();
2396be168c0dSopenharmony_ci+    }
2397be168c0dSopenharmony_ci+    if (status == napi_ok) {
2398be168c0dSopenharmony_ci+      status = napi_create_reference(env, result, refCount, &contextPriority_);
2399be168c0dSopenharmony_ci+      if (status == napi_ok) {
2400be168c0dSopenharmony_ci+        return result;
2401be168c0dSopenharmony_ci+      }
2402be168c0dSopenharmony_ci+    }
2403be168c0dSopenharmony_ci+  }
2404be168c0dSopenharmony_ci+  MS_LOG(ERROR) << "CreatePriorityObject is Failed!";
2405be168c0dSopenharmony_ci+  napi_get_undefined(env, &result);
2406be168c0dSopenharmony_ci+  return result;
2407be168c0dSopenharmony_ci+}
2408be168c0dSopenharmony_ci+
2409be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::CreateNnrtDeviceTypeObject(napi_env env) {
2410be168c0dSopenharmony_ci+  napi_value result = nullptr;
2411be168c0dSopenharmony_ci+  napi_status status;
2412be168c0dSopenharmony_ci+  std::string propName;
2413be168c0dSopenharmony_ci+  int32_t refCount = 1;
2414be168c0dSopenharmony_ci+
2415be168c0dSopenharmony_ci+  status = napi_create_object(env, &result);
2416be168c0dSopenharmony_ci+  if (status == napi_ok) {
2417be168c0dSopenharmony_ci+    for (auto &iter : contextNnrtDeviceTypeTypeMap) {
2418be168c0dSopenharmony_ci+      propName = iter.first;
2419be168c0dSopenharmony_ci+      status = AddNamedProperty(env, result, propName, iter.second);
2420be168c0dSopenharmony_ci+      if (status != napi_ok) {
2421be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Failed to add named prop in CreateNnrtDeviceTypeObject.";
2422be168c0dSopenharmony_ci+        break;
2423be168c0dSopenharmony_ci+      }
2424be168c0dSopenharmony_ci+      propName.clear();
2425be168c0dSopenharmony_ci+    }
2426be168c0dSopenharmony_ci+    if (status == napi_ok) {
2427be168c0dSopenharmony_ci+      status = napi_create_reference(env, result, refCount, &contextNnrtDeviceType_);
2428be168c0dSopenharmony_ci+      if (status == napi_ok) {
2429be168c0dSopenharmony_ci+        return result;
2430be168c0dSopenharmony_ci+      }
2431be168c0dSopenharmony_ci+    }
2432be168c0dSopenharmony_ci+  }
2433be168c0dSopenharmony_ci+  MS_LOG(ERROR) << "CreateNnrtDeviceTypeObject is Failed!";
2434be168c0dSopenharmony_ci+  napi_get_undefined(env, &result);
2435be168c0dSopenharmony_ci+  return result;
2436be168c0dSopenharmony_ci+}
2437be168c0dSopenharmony_ci+
2438be168c0dSopenharmony_ci+napi_status MSLiteModelNapi::AddNamedProperty(napi_env env, napi_value object, const std::string name,
2439be168c0dSopenharmony_ci+                                              int32_t enumValue) {
2440be168c0dSopenharmony_ci+  napi_status status;
2441be168c0dSopenharmony_ci+  napi_value enumNapiValue;
2442be168c0dSopenharmony_ci+
2443be168c0dSopenharmony_ci+  status = napi_create_int32(env, enumValue, &enumNapiValue);
2444be168c0dSopenharmony_ci+  if (status == napi_ok) {
2445be168c0dSopenharmony_ci+    status = napi_set_named_property(env, object, name.c_str(), enumNapiValue);
2446be168c0dSopenharmony_ci+  }
2447be168c0dSopenharmony_ci+  return status;
2448be168c0dSopenharmony_ci+}
2449be168c0dSopenharmony_ci+
2450be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::GetAllNnrtDeviceDescs(napi_env env, napi_callback_info info) {
2451be168c0dSopenharmony_ci+  size_t num;
2452be168c0dSopenharmony_ci+  napi_value jsResult = nullptr;
2453be168c0dSopenharmony_ci+  NNRTDeviceDesc *devices = OH_AI_GetAllNNRTDeviceDescs(&num);
2454be168c0dSopenharmony_ci+  if (devices == nullptr) {
2455be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Get all nnrt devices error, may nnrt is not supported.";
2456be168c0dSopenharmony_ci+    OH_AI_DestroyAllNNRTDeviceDescs(&devices);
2457be168c0dSopenharmony_ci+    return jsResult;
2458be168c0dSopenharmony_ci+  }
2459be168c0dSopenharmony_ci+
2460be168c0dSopenharmony_ci+  MS_LOG(INFO) << "all nnrt devices size: " << num;
2461be168c0dSopenharmony_ci+  napi_create_array_with_length(env, num, &jsResult);
2462be168c0dSopenharmony_ci+  for (size_t i = 0; i < num; i++) {
2463be168c0dSopenharmony_ci+    NnrtDeviceDesc nnrt_device;
2464be168c0dSopenharmony_ci+    NNRTDeviceDesc *nnrt_device_desc = OH_AI_GetElementOfNNRTDeviceDescs(devices, i);
2465be168c0dSopenharmony_ci+    nnrt_device.name.assign(OH_AI_GetNameFromNNRTDeviceDesc(nnrt_device_desc));
2466be168c0dSopenharmony_ci+    size_t id = OH_AI_GetDeviceIdFromNNRTDeviceDesc(nnrt_device_desc);
2467be168c0dSopenharmony_ci+    nnrt_device.id = id;
2468be168c0dSopenharmony_ci+    nnrt_device.type = static_cast<ContextNnrtDeviceType>(OH_AI_GetTypeFromNNRTDeviceDesc(nnrt_device_desc));
2469be168c0dSopenharmony_ci+    auto status = napi_set_element(env, jsResult, i, NnrtDeviceDescNapi::NewInstance(env, nnrt_device));
2470be168c0dSopenharmony_ci+    if (status != napi_ok) {
2471be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "napi_set_element failed! code: " << status;
2472be168c0dSopenharmony_ci+      OH_AI_DestroyAllNNRTDeviceDescs(&devices);
2473be168c0dSopenharmony_ci+      return jsResult;
2474be168c0dSopenharmony_ci+    }
2475be168c0dSopenharmony_ci+  }
2476be168c0dSopenharmony_ci+  MS_LOG(INFO) << "get All nnrt devices success!";
2477be168c0dSopenharmony_ci+  OH_AI_DestroyAllNNRTDeviceDescs(&devices);
2478be168c0dSopenharmony_ci+  return jsResult;
2479be168c0dSopenharmony_ci+}
2480be168c0dSopenharmony_ci+
2481be168c0dSopenharmony_ci+std::shared_ptr<mindspore::Model> MSLiteModelNapi::CreateModel(ModelInfo *model_info_ptr,
2482be168c0dSopenharmony_ci+                                                               ContextInfo *context_info_ptr) {
2483be168c0dSopenharmony_ci+  if (context_info_ptr == nullptr) {
2484be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "context_info_ptr is nullptr.";
2485be168c0dSopenharmony_ci+    return nullptr;
2486be168c0dSopenharmony_ci+  }
2487be168c0dSopenharmony_ci+  // create and init context
2488be168c0dSopenharmony_ci+  std::string s;
2489be168c0dSopenharmony_ci+  for (const auto &device_name : context_info_ptr->target) {
2490be168c0dSopenharmony_ci+    s += device_name + " ";
2491be168c0dSopenharmony_ci+  }
2492be168c0dSopenharmony_ci+  MS_LOG(DEBUG) << "target device: " << s.c_str();
2493be168c0dSopenharmony_ci+
2494be168c0dSopenharmony_ci+  auto context = std::make_shared<mindspore::Context>();
2495be168c0dSopenharmony_ci+  if (context == nullptr) {
2496be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Failed to new context.";
2497be168c0dSopenharmony_ci+    return nullptr;
2498be168c0dSopenharmony_ci+  }
2499be168c0dSopenharmony_ci+
2500be168c0dSopenharmony_ci+  auto &device_infos = context->MutableDeviceInfo();
2501be168c0dSopenharmony_ci+  if (context_info_ptr->target.empty()) {
2502be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "context is empty.";
2503be168c0dSopenharmony_ci+    return nullptr;
2504be168c0dSopenharmony_ci+  }
2505be168c0dSopenharmony_ci+  if (GetDeviceInfoContext(context_info_ptr, device_infos) != SUCCESS) {
2506be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Create context failed.";
2507be168c0dSopenharmony_ci+    return nullptr;
2508be168c0dSopenharmony_ci+  }
2509be168c0dSopenharmony_ci+  context->SetThreadNum(context_info_ptr->cpu_device.thread_num);
2510be168c0dSopenharmony_ci+  MS_LOG(DEBUG) << "current thread num is : " << context->GetThreadNum();
2511be168c0dSopenharmony_ci+
2512be168c0dSopenharmony_ci+  switch (model_info_ptr->mode) {
2513be168c0dSopenharmony_ci+    case kBuffer: {
2514be168c0dSopenharmony_ci+      MS_LOG(DEBUG) << "input model buffer, model_buffer_total: " << model_info_ptr->model_buffer_total;
2515be168c0dSopenharmony_ci+      if (model_info_ptr->model_buffer_data == nullptr || model_info_ptr->model_buffer_total <= 0) {
2516be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Failed to build model.";
2517be168c0dSopenharmony_ci+        return nullptr;
2518be168c0dSopenharmony_ci+      }
2519be168c0dSopenharmony_ci+      std::shared_ptr<mindspore::Model> model_ptr = std::make_shared<mindspore::Model>();
2520be168c0dSopenharmony_ci+      if (model_ptr == nullptr) {
2521be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Failed to new mindspore::model.";
2522be168c0dSopenharmony_ci+        return nullptr;
2523be168c0dSopenharmony_ci+      }
2524be168c0dSopenharmony_ci+      auto ret = model_ptr->Build(model_info_ptr->model_buffer_data, model_info_ptr->model_buffer_total,
2525be168c0dSopenharmony_ci+                                  mindspore::kMindIR, context);
2526be168c0dSopenharmony_ci+      if (ret == mindspore::kSuccess) {
2527be168c0dSopenharmony_ci+        MS_LOG(INFO) << "Build model from buffer success.";
2528be168c0dSopenharmony_ci+        return model_ptr;
2529be168c0dSopenharmony_ci+      }
2530be168c0dSopenharmony_ci+      break;
2531be168c0dSopenharmony_ci+    }
2532be168c0dSopenharmony_ci+    case kPath: {
2533be168c0dSopenharmony_ci+      MS_LOG(DEBUG) << "input model path, model_buffer_total: " << model_info_ptr->model_path.c_str();
2534be168c0dSopenharmony_ci+      std::shared_ptr<mindspore::Model> model_ptr = std::make_shared<mindspore::Model>();
2535be168c0dSopenharmony_ci+      if (model_ptr == nullptr) {
2536be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Failed to new mindspore::model.";
2537be168c0dSopenharmony_ci+        return nullptr;
2538be168c0dSopenharmony_ci+      }
2539be168c0dSopenharmony_ci+      auto ret = model_ptr->Build(model_info_ptr->model_path, mindspore::kMindIR, context);
2540be168c0dSopenharmony_ci+      if (ret == mindspore::kSuccess) {
2541be168c0dSopenharmony_ci+        MS_LOG(INFO) << "Build model from path success.";
2542be168c0dSopenharmony_ci+        return model_ptr;
2543be168c0dSopenharmony_ci+      }
2544be168c0dSopenharmony_ci+      return nullptr;
2545be168c0dSopenharmony_ci+    }
2546be168c0dSopenharmony_ci+    case kFD: {
2547be168c0dSopenharmony_ci+      MS_LOG(DEBUG) << "input model fd:" << model_info_ptr->model_fd
2548be168c0dSopenharmony_ci+                    << ", model_buffer_total: " << model_info_ptr->model_buffer_total;
2549be168c0dSopenharmony_ci+      std::shared_ptr<mindspore::Model> model_ptr = std::make_shared<mindspore::Model>();
2550be168c0dSopenharmony_ci+      if (model_ptr == nullptr) {
2551be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Failed to new mindspore::model.";
2552be168c0dSopenharmony_ci+        return nullptr;
2553be168c0dSopenharmony_ci+      }
2554be168c0dSopenharmony_ci+      auto ret = model_ptr->Build(model_info_ptr->model_buffer_data, model_info_ptr->model_buffer_total,
2555be168c0dSopenharmony_ci+                                  mindspore::kMindIR, context);
2556be168c0dSopenharmony_ci+
2557be168c0dSopenharmony_ci+      (void)munmap(model_info_ptr->model_buffer_data, model_info_ptr->model_buffer_total);
2558be168c0dSopenharmony_ci+      if (ret == mindspore::kSuccess) {
2559be168c0dSopenharmony_ci+        MS_LOG(INFO) << "Build model from fd success.";
2560be168c0dSopenharmony_ci+        return model_ptr;
2561be168c0dSopenharmony_ci+      }
2562be168c0dSopenharmony_ci+
2563be168c0dSopenharmony_ci+      break;
2564be168c0dSopenharmony_ci+    }
2565be168c0dSopenharmony_ci+    default: {
2566be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "Invalid model mode.";
2567be168c0dSopenharmony_ci+    }
2568be168c0dSopenharmony_ci+  }
2569be168c0dSopenharmony_ci+  MS_LOG(ERROR) << "Build model failed.";
2570be168c0dSopenharmony_ci+  return nullptr;
2571be168c0dSopenharmony_ci+}
2572be168c0dSopenharmony_ci+
2573be168c0dSopenharmony_ci+std::shared_ptr<mindspore::Model> MSLiteModelNapi::CreateTrainModel(ModelInfo *model_info_ptr,
2574be168c0dSopenharmony_ci+                                                                    ContextInfo *context_info_ptr) {
2575be168c0dSopenharmony_ci+  // create and init context
2576be168c0dSopenharmony_ci+  std::string s;
2577be168c0dSopenharmony_ci+  for (const auto &device_name : context_info_ptr->target) {
2578be168c0dSopenharmony_ci+    s += device_name + " ";
2579be168c0dSopenharmony_ci+  }
2580be168c0dSopenharmony_ci+  MS_LOG(DEBUG) << "target device: " << s.c_str();
2581be168c0dSopenharmony_ci+
2582be168c0dSopenharmony_ci+  auto context = std::make_shared<mindspore::Context>();
2583be168c0dSopenharmony_ci+  if (context == nullptr) {
2584be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Failed to new context.";
2585be168c0dSopenharmony_ci+    return nullptr;
2586be168c0dSopenharmony_ci+  }
2587be168c0dSopenharmony_ci+
2588be168c0dSopenharmony_ci+  auto &device_infos = context->MutableDeviceInfo();
2589be168c0dSopenharmony_ci+  if (context_info_ptr->target.empty()) {
2590be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "context is empty.";
2591be168c0dSopenharmony_ci+    return nullptr;
2592be168c0dSopenharmony_ci+  }
2593be168c0dSopenharmony_ci+  if (GetDeviceInfoContext(context_info_ptr, device_infos) != SUCCESS) {
2594be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Create context failed.";
2595be168c0dSopenharmony_ci+    return nullptr;
2596be168c0dSopenharmony_ci+  }
2597be168c0dSopenharmony_ci+
2598be168c0dSopenharmony_ci+  auto train_cfg = std::make_shared<TrainCfg>();
2599be168c0dSopenharmony_ci+  std::vector<std::string> loss_names;
2600be168c0dSopenharmony_ci+  for (const auto &name : train_cfg->GetLossName()) {
2601be168c0dSopenharmony_ci+      loss_names.push_back(name);
2602be168c0dSopenharmony_ci+  }
2603be168c0dSopenharmony_ci+  for (const auto &name : context_info_ptr->train_cfg.loss_names) {
2604be168c0dSopenharmony_ci+      loss_names.push_back(name);
2605be168c0dSopenharmony_ci+  }
2606be168c0dSopenharmony_ci+  train_cfg->SetLossName(loss_names);
2607be168c0dSopenharmony_ci+  train_cfg->optimization_level_ = static_cast<OptimizationLevel>(context_info_ptr->train_cfg.optimization_level);
2608be168c0dSopenharmony_ci+
2609be168c0dSopenharmony_ci+  switch (model_info_ptr->mode) {
2610be168c0dSopenharmony_ci+    case kBuffer: {
2611be168c0dSopenharmony_ci+      MS_LOG(DEBUG) << "input model buffer, model_buffer_total: " << model_info_ptr->model_buffer_total;
2612be168c0dSopenharmony_ci+      if (model_info_ptr->model_buffer_data == nullptr || model_info_ptr->model_buffer_total <= 0) {
2613be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Failed to build model.";
2614be168c0dSopenharmony_ci+        return nullptr;
2615be168c0dSopenharmony_ci+      }
2616be168c0dSopenharmony_ci+      std::shared_ptr<mindspore::Model> model_ptr = std::make_shared<mindspore::Model>();
2617be168c0dSopenharmony_ci+      if (model_ptr == nullptr) {
2618be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Failed to new mindspore::model.";
2619be168c0dSopenharmony_ci+        return nullptr;
2620be168c0dSopenharmony_ci+      }
2621be168c0dSopenharmony_ci+      mindspore::Graph graph;
2622be168c0dSopenharmony_ci+      auto status = mindspore::Serialization::Load(model_info_ptr->model_buffer_data,
2623be168c0dSopenharmony_ci+                                                   model_info_ptr->model_buffer_total, mindspore::kMindIR, &graph);
2624be168c0dSopenharmony_ci+      if (status != mindspore::kSuccess) {
2625be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "load ms file failed.";
2626be168c0dSopenharmony_ci+        return nullptr;
2627be168c0dSopenharmony_ci+      }
2628be168c0dSopenharmony_ci+      auto ret = model_ptr->Build(static_cast<mindspore::GraphCell>(graph), context, train_cfg);
2629be168c0dSopenharmony_ci+      if (ret == mindspore::kSuccess) {
2630be168c0dSopenharmony_ci+        MS_LOG(INFO) << "Build model from buffer success.";
2631be168c0dSopenharmony_ci+        return model_ptr;
2632be168c0dSopenharmony_ci+      }
2633be168c0dSopenharmony_ci+      break;
2634be168c0dSopenharmony_ci+    }
2635be168c0dSopenharmony_ci+    case kPath: {
2636be168c0dSopenharmony_ci+      MS_LOG(DEBUG) << "input model path, model_buffer_total: " << model_info_ptr->model_path.c_str();
2637be168c0dSopenharmony_ci+      std::shared_ptr<mindspore::Model> model_ptr = std::make_shared<mindspore::Model>();
2638be168c0dSopenharmony_ci+      if (model_ptr == nullptr) {
2639be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Failed to new mindspore::model.";
2640be168c0dSopenharmony_ci+        return nullptr;
2641be168c0dSopenharmony_ci+      }
2642be168c0dSopenharmony_ci+
2643be168c0dSopenharmony_ci+      mindspore::Graph graph;
2644be168c0dSopenharmony_ci+      auto status = mindspore::Serialization::Load(model_info_ptr->model_path, mindspore::kMindIR, &graph);
2645be168c0dSopenharmony_ci+      if (status != mindspore::kSuccess) {
2646be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "load ms file failed.";
2647be168c0dSopenharmony_ci+        return nullptr;
2648be168c0dSopenharmony_ci+      }
2649be168c0dSopenharmony_ci+      auto ret = model_ptr->Build(static_cast<mindspore::GraphCell>(graph), context, train_cfg);
2650be168c0dSopenharmony_ci+      if (ret == mindspore::kSuccess) {
2651be168c0dSopenharmony_ci+        MS_LOG(INFO) << "Build model from path success.";
2652be168c0dSopenharmony_ci+        return model_ptr;
2653be168c0dSopenharmony_ci+      }
2654be168c0dSopenharmony_ci+      return nullptr;
2655be168c0dSopenharmony_ci+    }
2656be168c0dSopenharmony_ci+    case kFD: {
2657be168c0dSopenharmony_ci+      MS_LOG(DEBUG) << "input model fd:" << model_info_ptr->model_fd
2658be168c0dSopenharmony_ci+                    << ", model_buffer_total: " << model_info_ptr->model_buffer_total;
2659be168c0dSopenharmony_ci+      std::shared_ptr<mindspore::Model> model_ptr = std::make_shared<mindspore::Model>();
2660be168c0dSopenharmony_ci+      if (model_ptr == nullptr) {
2661be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Failed to new mindspore::model.";
2662be168c0dSopenharmony_ci+        return nullptr;
2663be168c0dSopenharmony_ci+      }
2664be168c0dSopenharmony_ci+
2665be168c0dSopenharmony_ci+      mindspore::Graph graph;
2666be168c0dSopenharmony_ci+      auto status = mindspore::Serialization::Load(model_info_ptr->model_buffer_data,
2667be168c0dSopenharmony_ci+                                                   model_info_ptr->model_buffer_total, mindspore::kMindIR, &graph);
2668be168c0dSopenharmony_ci+      if (status != mindspore::kSuccess) {
2669be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "load ms file failed.";
2670be168c0dSopenharmony_ci+        return nullptr;
2671be168c0dSopenharmony_ci+      }
2672be168c0dSopenharmony_ci+      auto ret = model_ptr->Build(static_cast<mindspore::GraphCell>(graph), context, train_cfg);
2673be168c0dSopenharmony_ci+      (void)munmap(model_info_ptr->model_buffer_data, model_info_ptr->model_buffer_total);
2674be168c0dSopenharmony_ci+      if (ret == mindspore::kSuccess) {
2675be168c0dSopenharmony_ci+        MS_LOG(INFO) << "Build model from fd success.";
2676be168c0dSopenharmony_ci+        return model_ptr;
2677be168c0dSopenharmony_ci+      }
2678be168c0dSopenharmony_ci+
2679be168c0dSopenharmony_ci+      break;
2680be168c0dSopenharmony_ci+    }
2681be168c0dSopenharmony_ci+    default: {
2682be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "Invalid model mode.";
2683be168c0dSopenharmony_ci+    }
2684be168c0dSopenharmony_ci+  }
2685be168c0dSopenharmony_ci+  MS_LOG(ERROR) << "Build model failed.";
2686be168c0dSopenharmony_ci+  return nullptr;
2687be168c0dSopenharmony_ci+}
2688be168c0dSopenharmony_ci+
2689be168c0dSopenharmony_ci+int32_t MSLiteModelNapi::GetDeviceInfoContext(ContextInfo *context_ptr,
2690be168c0dSopenharmony_ci+                                              std::vector<std::shared_ptr<DeviceInfoContext>> &device_infos) {
2691be168c0dSopenharmony_ci+  for (auto device_name : context_ptr->target) {
2692be168c0dSopenharmony_ci+    if (kDeviceTypes.find(device_name) == kDeviceTypes.end()) {
2693be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "Invalid device: " << device_name.c_str();
2694be168c0dSopenharmony_ci+      return ERR_INVALID_OPERATION;
2695be168c0dSopenharmony_ci+    }
2696be168c0dSopenharmony_ci+
2697be168c0dSopenharmony_ci+    auto device_type = kDeviceTypes.at(device_name);
2698be168c0dSopenharmony_ci+    switch (device_type) {
2699be168c0dSopenharmony_ci+      case kCPU: {
2700be168c0dSopenharmony_ci+        auto cpu_device = std::make_shared<mindspore::CPUDeviceInfo>();
2701be168c0dSopenharmony_ci+        if (cpu_device == nullptr) {
2702be168c0dSopenharmony_ci+          MS_LOG(ERROR) << "Failed to new CPU deviceInfo.";
2703be168c0dSopenharmony_ci+          return ERR_INVALID_OPERATION;
2704be168c0dSopenharmony_ci+        }
2705be168c0dSopenharmony_ci+        bool is_fp16 = (context_ptr->cpu_device.precision_mode.compare("preferred_fp16") == 0) ? true : false;
2706be168c0dSopenharmony_ci+        cpu_device->SetEnableFP16(is_fp16);
2707be168c0dSopenharmony_ci+        device_infos.push_back(cpu_device);
2708be168c0dSopenharmony_ci+        break;
2709be168c0dSopenharmony_ci+      }
2710be168c0dSopenharmony_ci+      case kNNRt: {
2711be168c0dSopenharmony_ci+        auto nnrt_device = std::make_shared<mindspore::NNRTDeviceInfo>();
2712be168c0dSopenharmony_ci+        if (nnrt_device == nullptr) {
2713be168c0dSopenharmony_ci+          MS_LOG(ERROR) << "Failed to new NNRT deviceInfo.";
2714be168c0dSopenharmony_ci+          return ERR_INVALID_OPERATION;
2715be168c0dSopenharmony_ci+        }
2716be168c0dSopenharmony_ci+        nnrt_device->SetDeviceID(context_ptr->nnrt_device.device_id);
2717be168c0dSopenharmony_ci+        if (context_ptr->nnrt_device.performance_mode != UNSET_VALUE) {
2718be168c0dSopenharmony_ci+          nnrt_device->SetPerformanceMode(context_ptr->nnrt_device.performance_mode);
2719be168c0dSopenharmony_ci+        }
2720be168c0dSopenharmony_ci+        if (context_ptr->nnrt_device.priority != UNSET_VALUE) {
2721be168c0dSopenharmony_ci+          nnrt_device->SetPriority(context_ptr->nnrt_device.priority);
2722be168c0dSopenharmony_ci+        }
2723be168c0dSopenharmony_ci+        // ignore extensions
2724be168c0dSopenharmony_ci+        device_infos.push_back(nnrt_device);
2725be168c0dSopenharmony_ci+        break;
2726be168c0dSopenharmony_ci+      }
2727be168c0dSopenharmony_ci+      default: {
2728be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "invalid device.";
2729be168c0dSopenharmony_ci+        return ERR_INVALID_OPERATION;
2730be168c0dSopenharmony_ci+      }
2731be168c0dSopenharmony_ci+    }
2732be168c0dSopenharmony_ci+  }
2733be168c0dSopenharmony_ci+  return SUCCESS;
2734be168c0dSopenharmony_ci+}
2735be168c0dSopenharmony_ci+
2736be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::Constructor(napi_env env, napi_callback_info info) {
2737be168c0dSopenharmony_ci+  napi_status status;
2738be168c0dSopenharmony_ci+  napi_value result = nullptr;
2739be168c0dSopenharmony_ci+  napi_get_undefined(env, &result);
2740be168c0dSopenharmony_ci+  GET_PARAMS(env, info, ARGS_TWO);
2741be168c0dSopenharmony_ci+
2742be168c0dSopenharmony_ci+  std::unique_ptr<MSLiteModelNapi> model_napi = std::make_unique<MSLiteModelNapi>();
2743be168c0dSopenharmony_ci+  if (model_napi == nullptr) {
2744be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "No memory";
2745be168c0dSopenharmony_ci+    return result;
2746be168c0dSopenharmony_ci+  }
2747be168c0dSopenharmony_ci+
2748be168c0dSopenharmony_ci+  model_napi->env_ = env;
2749be168c0dSopenharmony_ci+  if (model_info_->train_model) {
2750be168c0dSopenharmony_ci+    model_napi->native_model_ = CreateTrainModel(model_info_, context_);
2751be168c0dSopenharmony_ci+  } else {
2752be168c0dSopenharmony_ci+    model_napi->native_model_ = CreateModel(model_info_, context_);
2753be168c0dSopenharmony_ci+  }
2754be168c0dSopenharmony_ci+  if (model_napi->native_model_ == nullptr) {
2755be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Failed to create model.";
2756be168c0dSopenharmony_ci+    return result;
2757be168c0dSopenharmony_ci+  }
2758be168c0dSopenharmony_ci+
2759be168c0dSopenharmony_ci+  status =
2760be168c0dSopenharmony_ci+    napi_wrap(env, thisVar, reinterpret_cast<void *>(model_napi.get()), MSLiteModelNapi::Finalize, nullptr, nullptr);
2761be168c0dSopenharmony_ci+  if (status == napi_ok) {
2762be168c0dSopenharmony_ci+    model_napi.release();
2763be168c0dSopenharmony_ci+    return thisVar;
2764be168c0dSopenharmony_ci+  }
2765be168c0dSopenharmony_ci+  return result;
2766be168c0dSopenharmony_ci+}
2767be168c0dSopenharmony_ci+
2768be168c0dSopenharmony_ci+int32_t MSLiteModelNapi::ParseModelInfo(napi_env env, napi_value root, ModelInfo &model_info) {
2769be168c0dSopenharmony_ci+  napi_valuetype valueType;
2770be168c0dSopenharmony_ci+  napi_status status = napi_typeof(env, root, &valueType);
2771be168c0dSopenharmony_ci+  if (status != napi_ok) {
2772be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "napi_typeof error.";
2773be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
2774be168c0dSopenharmony_ci+  }
2775be168c0dSopenharmony_ci+  if ((valueType != napi_object) && (valueType != napi_string) && (valueType != napi_number)) {
2776be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "model is invaild.";
2777be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
2778be168c0dSopenharmony_ci+  }
2779be168c0dSopenharmony_ci+
2780be168c0dSopenharmony_ci+  bool is_model_buffer = false;
2781be168c0dSopenharmony_ci+  napi_is_arraybuffer(env, root, &is_model_buffer);
2782be168c0dSopenharmony_ci+  if (is_model_buffer) {
2783be168c0dSopenharmony_ci+    // copy buffer
2784be168c0dSopenharmony_ci+    char *array_buffer_data;
2785be168c0dSopenharmony_ci+    size_t array_buffer_total;
2786be168c0dSopenharmony_ci+    status = napi_get_arraybuffer_info(env, root, reinterpret_cast<void **>(&array_buffer_data), &array_buffer_total);
2787be168c0dSopenharmony_ci+    if ((status != napi_ok) || (array_buffer_total <= 0)) {
2788be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "Parse model buffer failed.";
2789be168c0dSopenharmony_ci+      return ERR_INVALID_PARAM;
2790be168c0dSopenharmony_ci+    }
2791be168c0dSopenharmony_ci+
2792be168c0dSopenharmony_ci+    // shallow copy
2793be168c0dSopenharmony_ci+    model_info.model_buffer_data = array_buffer_data;
2794be168c0dSopenharmony_ci+    model_info.model_buffer_total = array_buffer_total;
2795be168c0dSopenharmony_ci+    model_info.mode = kBuffer;
2796be168c0dSopenharmony_ci+  } else if (valueType == napi_number) {
2797be168c0dSopenharmony_ci+    int32_t fd;
2798be168c0dSopenharmony_ci+    status = napi_get_value_int32(env, root, &fd);
2799be168c0dSopenharmony_ci+    if ((status != napi_ok) || (fd <= 0)) {
2800be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "Parse model FD failed.";
2801be168c0dSopenharmony_ci+      return ERR_INVALID_PARAM;
2802be168c0dSopenharmony_ci+    }
2803be168c0dSopenharmony_ci+
2804be168c0dSopenharmony_ci+    int size = lseek(fd, 0, SEEK_END);
2805be168c0dSopenharmony_ci+    (void)lseek(fd, 0, SEEK_SET);
2806be168c0dSopenharmony_ci+    auto mmap_buffers = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
2807be168c0dSopenharmony_ci+    if (mmap_buffers == NULL) {
2808be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "mmap_buffers is NULL.";
2809be168c0dSopenharmony_ci+      return ERR_INVALID_PARAM;
2810be168c0dSopenharmony_ci+    }
2811be168c0dSopenharmony_ci+    model_info.model_fd = fd;
2812be168c0dSopenharmony_ci+    model_info.model_buffer_data = static_cast<char *>(mmap_buffers);
2813be168c0dSopenharmony_ci+    model_info.model_buffer_total = size;
2814be168c0dSopenharmony_ci+    model_info.mode = kFD;
2815be168c0dSopenharmony_ci+  } else {
2816be168c0dSopenharmony_ci+    char char_buf[SIZE];
2817be168c0dSopenharmony_ci+    size_t buf_length = 0;
2818be168c0dSopenharmony_ci+    status = napi_get_value_string_utf8(env, root, char_buf, SIZE, &buf_length);
2819be168c0dSopenharmony_ci+    if ((status != napi_ok) || (buf_length <= 0)) {
2820be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "Parse model file failed.";
2821be168c0dSopenharmony_ci+      return ERR_INVALID_PARAM;
2822be168c0dSopenharmony_ci+    }
2823be168c0dSopenharmony_ci+    model_info.model_path.assign(char_buf, char_buf + buf_length);
2824be168c0dSopenharmony_ci+    model_info.mode = kPath;
2825be168c0dSopenharmony_ci+    MS_LOG(DEBUG) << "model_path: " << model_info.model_path.c_str();
2826be168c0dSopenharmony_ci+  }
2827be168c0dSopenharmony_ci+  return SUCCESS;
2828be168c0dSopenharmony_ci+}
2829be168c0dSopenharmony_ci+
2830be168c0dSopenharmony_ci+int32_t MSLiteModelNapi::ParseContextInfo(napi_env env, napi_value args, ContextInfo &context) {
2831be168c0dSopenharmony_ci+  napi_valuetype valueType;
2832be168c0dSopenharmony_ci+  napi_status status = napi_typeof(env, args, &valueType);
2833be168c0dSopenharmony_ci+  if ((status != napi_ok) || (valueType != napi_object)) {
2834be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "context is invaild.";
2835be168c0dSopenharmony_ci+    return ERR_NOT_EXISTED_PARAM;
2836be168c0dSopenharmony_ci+  }
2837be168c0dSopenharmony_ci+
2838be168c0dSopenharmony_ci+  std::vector<std::string> str_values;
2839be168c0dSopenharmony_ci+  auto ret = CommonNapi::GetPropertyStringArray(env, args, "target", str_values);
2840be168c0dSopenharmony_ci+  if (ret != SUCCESS) {
2841be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Get context target failed.";
2842be168c0dSopenharmony_ci+    return ret;
2843be168c0dSopenharmony_ci+  }
2844be168c0dSopenharmony_ci+  context.target.assign(str_values.begin(), str_values.end());
2845be168c0dSopenharmony_ci+
2846be168c0dSopenharmony_ci+  ret = GetCpuDeviceInfo(env, args, context);
2847be168c0dSopenharmony_ci+  if (ret != ERR_NOT_EXISTED_PARAM && ret != SUCCESS) {
2848be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Get context CpuDeviceInfo failed.";
2849be168c0dSopenharmony_ci+    return ret;
2850be168c0dSopenharmony_ci+  }
2851be168c0dSopenharmony_ci+
2852be168c0dSopenharmony_ci+  ret = GetNNRTDeviceInfo(env, args, context);
2853be168c0dSopenharmony_ci+  if (ret != ERR_NOT_EXISTED_PARAM && ret != SUCCESS) {
2854be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Get context NnrtDeviceInfo failed.";
2855be168c0dSopenharmony_ci+    return ret;
2856be168c0dSopenharmony_ci+  }
2857be168c0dSopenharmony_ci+  return SUCCESS;
2858be168c0dSopenharmony_ci+}
2859be168c0dSopenharmony_ci+
2860be168c0dSopenharmony_ci+int32_t MSLiteModelNapi::ParseTrainCfgInfo(napi_env env, napi_value root, TrainConfig &cfg) {
2861be168c0dSopenharmony_ci+  napi_valuetype valueType;
2862be168c0dSopenharmony_ci+  napi_status status = napi_typeof(env, root, &valueType);
2863be168c0dSopenharmony_ci+  if ((status != napi_ok) || (valueType != napi_object)) {
2864be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "TrainCfg is invaild.";
2865be168c0dSopenharmony_ci+    return ERR_NOT_EXISTED_PARAM;
2866be168c0dSopenharmony_ci+  }
2867be168c0dSopenharmony_ci+  std::vector<std::string> str_values;
2868be168c0dSopenharmony_ci+  auto ret = CommonNapi::GetPropertyStringArray(env, root, "lossName", str_values);
2869be168c0dSopenharmony_ci+  if (ret != SUCCESS && ret != ERR_NOT_EXISTED_PARAM) {
2870be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Get lossName failed.";
2871be168c0dSopenharmony_ci+    return ret;
2872be168c0dSopenharmony_ci+  }
2873be168c0dSopenharmony_ci+  cfg.loss_names.assign(str_values.begin(), str_values.end());
2874be168c0dSopenharmony_ci+
2875be168c0dSopenharmony_ci+  int32_t int_value = 0;
2876be168c0dSopenharmony_ci+  ret = CommonNapi::GetPropertyInt32(env, root, "optimizationLevel", int_value);
2877be168c0dSopenharmony_ci+  if (ret != SUCCESS && ret != ERR_NOT_EXISTED_PARAM) {
2878be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Get optimization level failed";
2879be168c0dSopenharmony_ci+    return ret;
2880be168c0dSopenharmony_ci+  } else {
2881be168c0dSopenharmony_ci+    cfg.optimization_level = int_value;
2882be168c0dSopenharmony_ci+  }
2883be168c0dSopenharmony_ci+  return SUCCESS;
2884be168c0dSopenharmony_ci+}
2885be168c0dSopenharmony_ci+
2886be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::CreateMSLiteModelWrapper(napi_env env, MSLiteModelAsyncContext *async_context) {
2887be168c0dSopenharmony_ci+  std::lock_guard<std::mutex> lock(create_mutex_);
2888be168c0dSopenharmony_ci+  napi_status status;
2889be168c0dSopenharmony_ci+  napi_value result = nullptr;
2890be168c0dSopenharmony_ci+  napi_value constructor;
2891be168c0dSopenharmony_ci+  napi_get_undefined(env, &result);
2892be168c0dSopenharmony_ci+
2893be168c0dSopenharmony_ci+  status = napi_get_reference_value(env, constructor_, &constructor);
2894be168c0dSopenharmony_ci+  if (status != napi_ok) {
2895be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get reference failed.";
2896be168c0dSopenharmony_ci+    return result;
2897be168c0dSopenharmony_ci+  }
2898be168c0dSopenharmony_ci+  model_info_ = &(async_context->model_info);
2899be168c0dSopenharmony_ci+  context_ = &(async_context->context);
2900be168c0dSopenharmony_ci+  status = napi_new_instance(env, constructor, 0, nullptr, &result);
2901be168c0dSopenharmony_ci+  if (status == napi_ok) {
2902be168c0dSopenharmony_ci+    return result;
2903be168c0dSopenharmony_ci+  }
2904be168c0dSopenharmony_ci+
2905be168c0dSopenharmony_ci+  return result;
2906be168c0dSopenharmony_ci+}
2907be168c0dSopenharmony_ci+
2908be168c0dSopenharmony_ci+void MSLiteModelNapi::GetMSLiteModelAsyncCallbackComplete(napi_env env, napi_status status, void *data) {
2909be168c0dSopenharmony_ci+  napi_value valueParam = nullptr;
2910be168c0dSopenharmony_ci+  auto async_context = static_cast<MSLiteModelAsyncContext *>(data);
2911be168c0dSopenharmony_ci+
2912be168c0dSopenharmony_ci+  if (async_context != nullptr) {
2913be168c0dSopenharmony_ci+    if (!async_context->status) {
2914be168c0dSopenharmony_ci+      valueParam = CreateMSLiteModelWrapper(env, async_context);
2915be168c0dSopenharmony_ci+    }
2916be168c0dSopenharmony_ci+    CommonCallbackRoutine(env, async_context, valueParam);
2917be168c0dSopenharmony_ci+  } else {
2918be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "GetMSLiteModelAsyncCallbackComplete asyncContext is Null!";
2919be168c0dSopenharmony_ci+  }
2920be168c0dSopenharmony_ci+}
2921be168c0dSopenharmony_ci+
2922be168c0dSopenharmony_ci+void MSLiteModelNapi::CommonCallbackRoutine(napi_env env, MSLiteModelAsyncContext *&asyncContext,
2923be168c0dSopenharmony_ci+                                            const napi_value &valueParam) {
2924be168c0dSopenharmony_ci+  napi_value result[ARGS_ONE] = {0};
2925be168c0dSopenharmony_ci+  napi_value retVal;
2926be168c0dSopenharmony_ci+  napi_value error = nullptr;
2927be168c0dSopenharmony_ci+
2928be168c0dSopenharmony_ci+  if (!asyncContext->status) {
2929be168c0dSopenharmony_ci+    result[PARAM0] = valueParam;
2930be168c0dSopenharmony_ci+  } else {
2931be168c0dSopenharmony_ci+    napi_value message = nullptr;
2932be168c0dSopenharmony_ci+    std::string messageValue = CommonNapi::getMessageByCode(asyncContext->status);
2933be168c0dSopenharmony_ci+    napi_create_string_utf8(env, messageValue.c_str(), NAPI_AUTO_LENGTH, &message);
2934be168c0dSopenharmony_ci+
2935be168c0dSopenharmony_ci+    napi_value code = nullptr;
2936be168c0dSopenharmony_ci+    napi_create_string_utf8(env, (std::to_string(asyncContext->status)).c_str(), NAPI_AUTO_LENGTH, &code);
2937be168c0dSopenharmony_ci+
2938be168c0dSopenharmony_ci+    napi_create_error(env, code, message, &error);
2939be168c0dSopenharmony_ci+    napi_get_undefined(env, &result[PARAM0]);
2940be168c0dSopenharmony_ci+  }
2941be168c0dSopenharmony_ci+
2942be168c0dSopenharmony_ci+  if (asyncContext->deferred != nullptr) {
2943be168c0dSopenharmony_ci+    if (!asyncContext->status) {
2944be168c0dSopenharmony_ci+      napi_resolve_deferred(env, asyncContext->deferred, result[PARAM0]);
2945be168c0dSopenharmony_ci+    } else {
2946be168c0dSopenharmony_ci+      napi_reject_deferred(env, asyncContext->deferred, error);
2947be168c0dSopenharmony_ci+    }
2948be168c0dSopenharmony_ci+  } else {
2949be168c0dSopenharmony_ci+    napi_value callback = nullptr;
2950be168c0dSopenharmony_ci+    napi_get_reference_value(env, asyncContext->callbackRef, &callback);
2951be168c0dSopenharmony_ci+    napi_call_function(env, nullptr, callback, ARGS_ONE, result, &retVal);
2952be168c0dSopenharmony_ci+    napi_delete_reference(env, asyncContext->callbackRef);
2953be168c0dSopenharmony_ci+  }
2954be168c0dSopenharmony_ci+  napi_delete_async_work(env, asyncContext->work);
2955be168c0dSopenharmony_ci+
2956be168c0dSopenharmony_ci+  delete asyncContext;
2957be168c0dSopenharmony_ci+  asyncContext = nullptr;
2958be168c0dSopenharmony_ci+}
2959be168c0dSopenharmony_ci+
2960be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::LoadMSLiteModelFromFile(napi_env env, napi_callback_info info) {
2961be168c0dSopenharmony_ci+  napi_status status;
2962be168c0dSopenharmony_ci+  napi_value result = nullptr;
2963be168c0dSopenharmony_ci+  const int32_t refCount = 1;
2964be168c0dSopenharmony_ci+  GET_PARAMS(env, info, ARGS_THREE);
2965be168c0dSopenharmony_ci+  napi_valuetype valueType = napi_undefined;
2966be168c0dSopenharmony_ci+
2967be168c0dSopenharmony_ci+  std::unique_ptr<MSLiteModelAsyncContext> asyncContext = std::make_unique<MSLiteModelAsyncContext>();
2968be168c0dSopenharmony_ci+
2969be168c0dSopenharmony_ci+  int32_t ret;
2970be168c0dSopenharmony_ci+  for (size_t i = PARAM0; i < argc; i++) {
2971be168c0dSopenharmony_ci+    if (i == PARAM0) {
2972be168c0dSopenharmony_ci+      ret = ParseModelInfo(env, argv[i], asyncContext->model_info);
2973be168c0dSopenharmony_ci+      if (ret != SUCCESS) {
2974be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Parsing model failed.";
2975be168c0dSopenharmony_ci+        return result;
2976be168c0dSopenharmony_ci+      }
2977be168c0dSopenharmony_ci+    } else if (i == PARAM1) {
2978be168c0dSopenharmony_ci+      napi_typeof(env, argv[i], &valueType);
2979be168c0dSopenharmony_ci+      if (valueType == napi_function) {
2980be168c0dSopenharmony_ci+        napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
2981be168c0dSopenharmony_ci+      } else {
2982be168c0dSopenharmony_ci+        ret = ParseContextInfo(env, argv[i], asyncContext->context);
2983be168c0dSopenharmony_ci+        if (ret != SUCCESS) {
2984be168c0dSopenharmony_ci+          MS_LOG(ERROR) << "Parsing context failed.";
2985be168c0dSopenharmony_ci+          return result;
2986be168c0dSopenharmony_ci+        }
2987be168c0dSopenharmony_ci+      }
2988be168c0dSopenharmony_ci+    } else if (i == PARAM2) {
2989be168c0dSopenharmony_ci+      napi_typeof(env, argv[i], &valueType);
2990be168c0dSopenharmony_ci+      if (valueType == napi_function) {
2991be168c0dSopenharmony_ci+        napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
2992be168c0dSopenharmony_ci+      }
2993be168c0dSopenharmony_ci+      break;
2994be168c0dSopenharmony_ci+    } else {
2995be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "Invalid input params.";
2996be168c0dSopenharmony_ci+      return result;
2997be168c0dSopenharmony_ci+    }
2998be168c0dSopenharmony_ci+  }
2999be168c0dSopenharmony_ci+
3000be168c0dSopenharmony_ci+  if (asyncContext->callbackRef == nullptr) {
3001be168c0dSopenharmony_ci+    status = napi_create_promise(env, &asyncContext->deferred, &result);
3002be168c0dSopenharmony_ci+    if (status != napi_ok) {
3003be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "create promise failed.";
3004be168c0dSopenharmony_ci+      return result;
3005be168c0dSopenharmony_ci+    }
3006be168c0dSopenharmony_ci+  } else {
3007be168c0dSopenharmony_ci+    status = napi_get_undefined(env, &result);
3008be168c0dSopenharmony_ci+    if (status != napi_ok) {
3009be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "create callback failed.";
3010be168c0dSopenharmony_ci+      return result;
3011be168c0dSopenharmony_ci+    }
3012be168c0dSopenharmony_ci+  }
3013be168c0dSopenharmony_ci+
3014be168c0dSopenharmony_ci+  napi_value resource = nullptr;
3015be168c0dSopenharmony_ci+  napi_create_string_utf8(env, "LoadMSLiteModelFromFile", NAPI_AUTO_LENGTH, &resource);
3016be168c0dSopenharmony_ci+  status = napi_create_async_work(
3017be168c0dSopenharmony_ci+    env, nullptr, resource,
3018be168c0dSopenharmony_ci+    [](napi_env env, void *data) {
3019be168c0dSopenharmony_ci+      auto context = static_cast<MSLiteModelAsyncContext *>(data);
3020be168c0dSopenharmony_ci+      context->status = SUCCESS;
3021be168c0dSopenharmony_ci+    },
3022be168c0dSopenharmony_ci+    GetMSLiteModelAsyncCallbackComplete, static_cast<void *>(asyncContext.get()), &asyncContext->work);
3023be168c0dSopenharmony_ci+  if (status != napi_ok) {
3024be168c0dSopenharmony_ci+    result = nullptr;
3025be168c0dSopenharmony_ci+  } else {
3026be168c0dSopenharmony_ci+    status = napi_queue_async_work(env, asyncContext->work);
3027be168c0dSopenharmony_ci+    if (status == napi_ok) {
3028be168c0dSopenharmony_ci+      asyncContext.release();
3029be168c0dSopenharmony_ci+    } else {
3030be168c0dSopenharmony_ci+      result = nullptr;
3031be168c0dSopenharmony_ci+    }
3032be168c0dSopenharmony_ci+  }
3033be168c0dSopenharmony_ci+  return result;
3034be168c0dSopenharmony_ci+}
3035be168c0dSopenharmony_ci+
3036be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::LoadMSLiteTrainModelFromFile(napi_env env, napi_callback_info info) {
3037be168c0dSopenharmony_ci+  napi_status status;
3038be168c0dSopenharmony_ci+  napi_value result = nullptr;
3039be168c0dSopenharmony_ci+  GET_PARAMS(env, info, ARGS_THREE);
3040be168c0dSopenharmony_ci+
3041be168c0dSopenharmony_ci+  std::unique_ptr<MSLiteModelAsyncContext> asyncContext = std::make_unique<MSLiteModelAsyncContext>();
3042be168c0dSopenharmony_ci+
3043be168c0dSopenharmony_ci+  asyncContext->model_info.train_model = true;
3044be168c0dSopenharmony_ci+  int32_t ret;
3045be168c0dSopenharmony_ci+  for (size_t i = PARAM0; i < argc; i++) {
3046be168c0dSopenharmony_ci+    if (i == PARAM0) {
3047be168c0dSopenharmony_ci+      ret = ParseModelInfo(env, argv[i], asyncContext->model_info);
3048be168c0dSopenharmony_ci+      if (ret != SUCCESS) {
3049be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Parsing model failed.";
3050be168c0dSopenharmony_ci+        return result;
3051be168c0dSopenharmony_ci+      }
3052be168c0dSopenharmony_ci+    } else if (i == PARAM1) {
3053be168c0dSopenharmony_ci+      ret = ParseTrainCfgInfo(env, argv[i], asyncContext->context.train_cfg);
3054be168c0dSopenharmony_ci+      if (ret != SUCCESS) {
3055be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Parsing TrainCfg failed.";
3056be168c0dSopenharmony_ci+        return result;
3057be168c0dSopenharmony_ci+      }
3058be168c0dSopenharmony_ci+    } else if (i == PARAM2) {
3059be168c0dSopenharmony_ci+      ret = ParseContextInfo(env, argv[i], asyncContext->context);
3060be168c0dSopenharmony_ci+      if (ret != SUCCESS) {
3061be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Parsing context failed.";
3062be168c0dSopenharmony_ci+        return result;
3063be168c0dSopenharmony_ci+      }
3064be168c0dSopenharmony_ci+    } else {
3065be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "Invalid input params.";
3066be168c0dSopenharmony_ci+      return result;
3067be168c0dSopenharmony_ci+    }
3068be168c0dSopenharmony_ci+  }
3069be168c0dSopenharmony_ci+
3070be168c0dSopenharmony_ci+  if (asyncContext->callbackRef == nullptr) {
3071be168c0dSopenharmony_ci+    status = napi_create_promise(env, &asyncContext->deferred, &result);
3072be168c0dSopenharmony_ci+    if (status != napi_ok) {
3073be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "create promise failed.";
3074be168c0dSopenharmony_ci+      return result;
3075be168c0dSopenharmony_ci+    }
3076be168c0dSopenharmony_ci+  } else {
3077be168c0dSopenharmony_ci+    status = napi_get_undefined(env, &result);
3078be168c0dSopenharmony_ci+    if (status != napi_ok) {
3079be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "create callback failed.";
3080be168c0dSopenharmony_ci+      return result;
3081be168c0dSopenharmony_ci+    }
3082be168c0dSopenharmony_ci+  }
3083be168c0dSopenharmony_ci+
3084be168c0dSopenharmony_ci+  napi_value resource = nullptr;
3085be168c0dSopenharmony_ci+  napi_create_string_utf8(env, "LoadMSLiteTrainModelFromFile", NAPI_AUTO_LENGTH, &resource);
3086be168c0dSopenharmony_ci+  status = napi_create_async_work(
3087be168c0dSopenharmony_ci+    env, nullptr, resource,
3088be168c0dSopenharmony_ci+    [](napi_env env, void *data) {
3089be168c0dSopenharmony_ci+      auto context = static_cast<MSLiteModelAsyncContext *>(data);
3090be168c0dSopenharmony_ci+      context->status = SUCCESS;
3091be168c0dSopenharmony_ci+    },
3092be168c0dSopenharmony_ci+    GetMSLiteModelAsyncCallbackComplete, static_cast<void *>(asyncContext.get()), &asyncContext->work);
3093be168c0dSopenharmony_ci+  if (status != napi_ok) {
3094be168c0dSopenharmony_ci+    result = nullptr;
3095be168c0dSopenharmony_ci+  } else {
3096be168c0dSopenharmony_ci+    status = napi_queue_async_work(env, asyncContext->work);
3097be168c0dSopenharmony_ci+    if (status == napi_ok) {
3098be168c0dSopenharmony_ci+      asyncContext.release();
3099be168c0dSopenharmony_ci+    } else {
3100be168c0dSopenharmony_ci+      result = nullptr;
3101be168c0dSopenharmony_ci+    }
3102be168c0dSopenharmony_ci+  }
3103be168c0dSopenharmony_ci+  return result;
3104be168c0dSopenharmony_ci+}
3105be168c0dSopenharmony_ci+
3106be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::LoadMSLiteTrainModelFromBuffer(napi_env env, napi_callback_info info) {
3107be168c0dSopenharmony_ci+  napi_status status;
3108be168c0dSopenharmony_ci+  napi_value result = nullptr;
3109be168c0dSopenharmony_ci+  GET_PARAMS(env, info, ARGS_THREE);
3110be168c0dSopenharmony_ci+
3111be168c0dSopenharmony_ci+  std::unique_ptr<MSLiteModelAsyncContext> asyncContext = std::make_unique<MSLiteModelAsyncContext>();
3112be168c0dSopenharmony_ci+
3113be168c0dSopenharmony_ci+  asyncContext->model_info.train_model = true;
3114be168c0dSopenharmony_ci+  int32_t ret;
3115be168c0dSopenharmony_ci+  for (size_t i = PARAM0; i < argc; i++) {
3116be168c0dSopenharmony_ci+    if (i == PARAM0) {
3117be168c0dSopenharmony_ci+      ret = ParseModelInfo(env, argv[i], asyncContext->model_info);
3118be168c0dSopenharmony_ci+      if (ret != SUCCESS) {
3119be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Parsing model failed.";
3120be168c0dSopenharmony_ci+        return result;
3121be168c0dSopenharmony_ci+      }
3122be168c0dSopenharmony_ci+    } else if (i == PARAM1) {
3123be168c0dSopenharmony_ci+      ret = ParseTrainCfgInfo(env, argv[i], asyncContext->context.train_cfg);
3124be168c0dSopenharmony_ci+      if (ret != SUCCESS) {
3125be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Parsing TrainCfg failed.";
3126be168c0dSopenharmony_ci+        return result;
3127be168c0dSopenharmony_ci+      }
3128be168c0dSopenharmony_ci+    } else if (i == PARAM2) {
3129be168c0dSopenharmony_ci+      ret = ParseContextInfo(env, argv[i], asyncContext->context);
3130be168c0dSopenharmony_ci+      if (ret != SUCCESS) {
3131be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Parsing context failed.";
3132be168c0dSopenharmony_ci+        return result;
3133be168c0dSopenharmony_ci+      }
3134be168c0dSopenharmony_ci+    } else {
3135be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "Invalid input params.";
3136be168c0dSopenharmony_ci+      return result;
3137be168c0dSopenharmony_ci+    }
3138be168c0dSopenharmony_ci+  }
3139be168c0dSopenharmony_ci+
3140be168c0dSopenharmony_ci+  if (asyncContext->callbackRef == nullptr) {
3141be168c0dSopenharmony_ci+    status = napi_create_promise(env, &asyncContext->deferred, &result);
3142be168c0dSopenharmony_ci+    if (status != napi_ok) {
3143be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "create promise failed.";
3144be168c0dSopenharmony_ci+      return result;
3145be168c0dSopenharmony_ci+    }
3146be168c0dSopenharmony_ci+  } else {
3147be168c0dSopenharmony_ci+    status = napi_get_undefined(env, &result);
3148be168c0dSopenharmony_ci+    if (status != napi_ok) {
3149be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "create callback failed.";
3150be168c0dSopenharmony_ci+      return result;
3151be168c0dSopenharmony_ci+    }
3152be168c0dSopenharmony_ci+  }
3153be168c0dSopenharmony_ci+
3154be168c0dSopenharmony_ci+  napi_value resource = nullptr;
3155be168c0dSopenharmony_ci+  napi_create_string_utf8(env, "LoadMSLiteTrainModelFromBuffer", NAPI_AUTO_LENGTH, &resource);
3156be168c0dSopenharmony_ci+  status = napi_create_async_work(
3157be168c0dSopenharmony_ci+    env, nullptr, resource,
3158be168c0dSopenharmony_ci+    [](napi_env env, void *data) {
3159be168c0dSopenharmony_ci+      auto context = static_cast<MSLiteModelAsyncContext *>(data);
3160be168c0dSopenharmony_ci+      context->status = SUCCESS;
3161be168c0dSopenharmony_ci+    },
3162be168c0dSopenharmony_ci+    GetMSLiteModelAsyncCallbackComplete, static_cast<void *>(asyncContext.get()), &asyncContext->work);
3163be168c0dSopenharmony_ci+  if (status != napi_ok) {
3164be168c0dSopenharmony_ci+    result = nullptr;
3165be168c0dSopenharmony_ci+  } else {
3166be168c0dSopenharmony_ci+    status = napi_queue_async_work(env, asyncContext->work);
3167be168c0dSopenharmony_ci+    if (status == napi_ok) {
3168be168c0dSopenharmony_ci+      asyncContext.release();
3169be168c0dSopenharmony_ci+    } else {
3170be168c0dSopenharmony_ci+      result = nullptr;
3171be168c0dSopenharmony_ci+    }
3172be168c0dSopenharmony_ci+  }
3173be168c0dSopenharmony_ci+  return result;
3174be168c0dSopenharmony_ci+}
3175be168c0dSopenharmony_ci+
3176be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::LoadMSLiteTrainModelFromFd(napi_env env, napi_callback_info info) {
3177be168c0dSopenharmony_ci+  napi_status status;
3178be168c0dSopenharmony_ci+  napi_value result = nullptr;
3179be168c0dSopenharmony_ci+  GET_PARAMS(env, info, ARGS_THREE);
3180be168c0dSopenharmony_ci+
3181be168c0dSopenharmony_ci+  std::unique_ptr<MSLiteModelAsyncContext> asyncContext = std::make_unique<MSLiteModelAsyncContext>();
3182be168c0dSopenharmony_ci+
3183be168c0dSopenharmony_ci+  asyncContext->model_info.train_model = true;
3184be168c0dSopenharmony_ci+  int32_t ret;
3185be168c0dSopenharmony_ci+  for (size_t i = PARAM0; i < argc; i++) {
3186be168c0dSopenharmony_ci+    if (i == PARAM0) {
3187be168c0dSopenharmony_ci+      ret = ParseModelInfo(env, argv[i], asyncContext->model_info);
3188be168c0dSopenharmony_ci+      if (ret != SUCCESS) {
3189be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Parsing model failed.";
3190be168c0dSopenharmony_ci+        return result;
3191be168c0dSopenharmony_ci+      }
3192be168c0dSopenharmony_ci+    } else if (i == PARAM1) {
3193be168c0dSopenharmony_ci+      ret = ParseTrainCfgInfo(env, argv[i], asyncContext->context.train_cfg);
3194be168c0dSopenharmony_ci+      if (ret != SUCCESS) {
3195be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Parsing TrainCfg failed.";
3196be168c0dSopenharmony_ci+        return result;
3197be168c0dSopenharmony_ci+      }
3198be168c0dSopenharmony_ci+    } else if (i == PARAM2) {
3199be168c0dSopenharmony_ci+      ret = ParseContextInfo(env, argv[i], asyncContext->context);
3200be168c0dSopenharmony_ci+      if (ret != SUCCESS) {
3201be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Parsing context failed.";
3202be168c0dSopenharmony_ci+        return result;
3203be168c0dSopenharmony_ci+      }
3204be168c0dSopenharmony_ci+    } else {
3205be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "Invalid input params.";
3206be168c0dSopenharmony_ci+      return result;
3207be168c0dSopenharmony_ci+    }
3208be168c0dSopenharmony_ci+  }
3209be168c0dSopenharmony_ci+
3210be168c0dSopenharmony_ci+  if (asyncContext->callbackRef == nullptr) {
3211be168c0dSopenharmony_ci+    status = napi_create_promise(env, &asyncContext->deferred, &result);
3212be168c0dSopenharmony_ci+    if (status != napi_ok) {
3213be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "create promise failed.";
3214be168c0dSopenharmony_ci+      return result;
3215be168c0dSopenharmony_ci+    }
3216be168c0dSopenharmony_ci+  } else {
3217be168c0dSopenharmony_ci+    status = napi_get_undefined(env, &result);
3218be168c0dSopenharmony_ci+    if (status != napi_ok) {
3219be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "create callback failed.";
3220be168c0dSopenharmony_ci+      return result;
3221be168c0dSopenharmony_ci+    }
3222be168c0dSopenharmony_ci+  }
3223be168c0dSopenharmony_ci+
3224be168c0dSopenharmony_ci+  napi_value resource = nullptr;
3225be168c0dSopenharmony_ci+  napi_create_string_utf8(env, "LoadMSLiteTrainModelFromFd", NAPI_AUTO_LENGTH, &resource);
3226be168c0dSopenharmony_ci+  status = napi_create_async_work(
3227be168c0dSopenharmony_ci+    env, nullptr, resource,
3228be168c0dSopenharmony_ci+    [](napi_env env, void *data) {
3229be168c0dSopenharmony_ci+      auto context = static_cast<MSLiteModelAsyncContext *>(data);
3230be168c0dSopenharmony_ci+      context->status = SUCCESS;
3231be168c0dSopenharmony_ci+    },
3232be168c0dSopenharmony_ci+    GetMSLiteModelAsyncCallbackComplete, static_cast<void *>(asyncContext.get()), &asyncContext->work);
3233be168c0dSopenharmony_ci+  if (status != napi_ok) {
3234be168c0dSopenharmony_ci+    result = nullptr;
3235be168c0dSopenharmony_ci+  } else {
3236be168c0dSopenharmony_ci+    status = napi_queue_async_work(env, asyncContext->work);
3237be168c0dSopenharmony_ci+    if (status == napi_ok) {
3238be168c0dSopenharmony_ci+      asyncContext.release();
3239be168c0dSopenharmony_ci+    } else {
3240be168c0dSopenharmony_ci+      result = nullptr;
3241be168c0dSopenharmony_ci+    }
3242be168c0dSopenharmony_ci+  }
3243be168c0dSopenharmony_ci+  return result;
3244be168c0dSopenharmony_ci+}
3245be168c0dSopenharmony_ci+
3246be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::LoadMSLiteModelFromBuffer(napi_env env, napi_callback_info info) {
3247be168c0dSopenharmony_ci+  napi_status status;
3248be168c0dSopenharmony_ci+  napi_value result = nullptr;
3249be168c0dSopenharmony_ci+  const int32_t refCount = 1;
3250be168c0dSopenharmony_ci+  GET_PARAMS(env, info, ARGS_THREE);
3251be168c0dSopenharmony_ci+  napi_valuetype valueType = napi_undefined;
3252be168c0dSopenharmony_ci+
3253be168c0dSopenharmony_ci+  std::unique_ptr<MSLiteModelAsyncContext> asyncContext = std::make_unique<MSLiteModelAsyncContext>();
3254be168c0dSopenharmony_ci+
3255be168c0dSopenharmony_ci+  int32_t ret;
3256be168c0dSopenharmony_ci+  for (size_t i = PARAM0; i < argc; i++) {
3257be168c0dSopenharmony_ci+    if (i == PARAM0) {
3258be168c0dSopenharmony_ci+      ret = ParseModelInfo(env, argv[i], asyncContext->model_info);
3259be168c0dSopenharmony_ci+      if (ret != SUCCESS) {
3260be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Parsing model failed.";
3261be168c0dSopenharmony_ci+        return result;
3262be168c0dSopenharmony_ci+      }
3263be168c0dSopenharmony_ci+    } else if (i == PARAM1) {
3264be168c0dSopenharmony_ci+      napi_typeof(env, argv[i], &valueType);
3265be168c0dSopenharmony_ci+      if (valueType == napi_function) {
3266be168c0dSopenharmony_ci+        napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
3267be168c0dSopenharmony_ci+      } else {
3268be168c0dSopenharmony_ci+        ret = ParseContextInfo(env, argv[i], asyncContext->context);
3269be168c0dSopenharmony_ci+        if (ret != SUCCESS) {
3270be168c0dSopenharmony_ci+          MS_LOG(ERROR) << "Parsing context failed.";
3271be168c0dSopenharmony_ci+          return result;
3272be168c0dSopenharmony_ci+        }
3273be168c0dSopenharmony_ci+      }
3274be168c0dSopenharmony_ci+    } else if (i == PARAM2) {
3275be168c0dSopenharmony_ci+      napi_typeof(env, argv[i], &valueType);
3276be168c0dSopenharmony_ci+      if (valueType == napi_function) {
3277be168c0dSopenharmony_ci+        napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
3278be168c0dSopenharmony_ci+      }
3279be168c0dSopenharmony_ci+      break;
3280be168c0dSopenharmony_ci+    } else {
3281be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "Invalid input params.";
3282be168c0dSopenharmony_ci+      return result;
3283be168c0dSopenharmony_ci+    }
3284be168c0dSopenharmony_ci+  }
3285be168c0dSopenharmony_ci+
3286be168c0dSopenharmony_ci+  if (asyncContext->callbackRef == nullptr) {
3287be168c0dSopenharmony_ci+    status = napi_create_promise(env, &asyncContext->deferred, &result);
3288be168c0dSopenharmony_ci+    if (status != napi_ok) {
3289be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "create promise failed.";
3290be168c0dSopenharmony_ci+      return result;
3291be168c0dSopenharmony_ci+    }
3292be168c0dSopenharmony_ci+  } else {
3293be168c0dSopenharmony_ci+    status = napi_get_undefined(env, &result);
3294be168c0dSopenharmony_ci+    if (status != napi_ok) {
3295be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "create callback failed.";
3296be168c0dSopenharmony_ci+      return result;
3297be168c0dSopenharmony_ci+    }
3298be168c0dSopenharmony_ci+  }
3299be168c0dSopenharmony_ci+
3300be168c0dSopenharmony_ci+  napi_value resource = nullptr;
3301be168c0dSopenharmony_ci+  napi_create_string_utf8(env, "LoadMSLiteModelFromBuffer", NAPI_AUTO_LENGTH, &resource);
3302be168c0dSopenharmony_ci+  status = napi_create_async_work(
3303be168c0dSopenharmony_ci+    env, nullptr, resource,
3304be168c0dSopenharmony_ci+    [](napi_env env, void *data) {
3305be168c0dSopenharmony_ci+      auto context = static_cast<MSLiteModelAsyncContext *>(data);
3306be168c0dSopenharmony_ci+      context->status = SUCCESS;
3307be168c0dSopenharmony_ci+    },
3308be168c0dSopenharmony_ci+    GetMSLiteModelAsyncCallbackComplete, static_cast<void *>(asyncContext.get()), &asyncContext->work);
3309be168c0dSopenharmony_ci+  if (status != napi_ok) {
3310be168c0dSopenharmony_ci+    result = nullptr;
3311be168c0dSopenharmony_ci+  } else {
3312be168c0dSopenharmony_ci+    status = napi_queue_async_work(env, asyncContext->work);
3313be168c0dSopenharmony_ci+    if (status == napi_ok) {
3314be168c0dSopenharmony_ci+      asyncContext.release();
3315be168c0dSopenharmony_ci+    } else {
3316be168c0dSopenharmony_ci+      result = nullptr;
3317be168c0dSopenharmony_ci+    }
3318be168c0dSopenharmony_ci+  }
3319be168c0dSopenharmony_ci+  return result;
3320be168c0dSopenharmony_ci+}
3321be168c0dSopenharmony_ci+
3322be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::LoadMSLiteModelFromFd(napi_env env, napi_callback_info info) {
3323be168c0dSopenharmony_ci+  napi_status status;
3324be168c0dSopenharmony_ci+  napi_value result = nullptr;
3325be168c0dSopenharmony_ci+  const int32_t refCount = 1;
3326be168c0dSopenharmony_ci+  GET_PARAMS(env, info, ARGS_THREE);
3327be168c0dSopenharmony_ci+  napi_valuetype valueType = napi_undefined;
3328be168c0dSopenharmony_ci+
3329be168c0dSopenharmony_ci+  std::unique_ptr<MSLiteModelAsyncContext> asyncContext = std::make_unique<MSLiteModelAsyncContext>();
3330be168c0dSopenharmony_ci+
3331be168c0dSopenharmony_ci+  int32_t ret;
3332be168c0dSopenharmony_ci+  for (size_t i = PARAM0; i < argc; i++) {
3333be168c0dSopenharmony_ci+    if (i == PARAM0) {
3334be168c0dSopenharmony_ci+      ret = ParseModelInfo(env, argv[i], asyncContext->model_info);
3335be168c0dSopenharmony_ci+      if (ret != SUCCESS) {
3336be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "Parsing model failed.";
3337be168c0dSopenharmony_ci+        return result;
3338be168c0dSopenharmony_ci+      }
3339be168c0dSopenharmony_ci+    } else if (i == PARAM1) {
3340be168c0dSopenharmony_ci+      napi_typeof(env, argv[i], &valueType);
3341be168c0dSopenharmony_ci+      if (valueType == napi_function) {
3342be168c0dSopenharmony_ci+        napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
3343be168c0dSopenharmony_ci+      } else {
3344be168c0dSopenharmony_ci+        ret = ParseContextInfo(env, argv[i], asyncContext->context);
3345be168c0dSopenharmony_ci+        if (ret != SUCCESS) {
3346be168c0dSopenharmony_ci+          MS_LOG(ERROR) << "Parsing context failed.";
3347be168c0dSopenharmony_ci+          return result;
3348be168c0dSopenharmony_ci+        }
3349be168c0dSopenharmony_ci+      }
3350be168c0dSopenharmony_ci+    } else if (i == PARAM2) {
3351be168c0dSopenharmony_ci+      napi_typeof(env, argv[i], &valueType);
3352be168c0dSopenharmony_ci+      if (valueType == napi_function) {
3353be168c0dSopenharmony_ci+        napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
3354be168c0dSopenharmony_ci+      }
3355be168c0dSopenharmony_ci+      break;
3356be168c0dSopenharmony_ci+    } else {
3357be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "Invalid input params.";
3358be168c0dSopenharmony_ci+      return result;
3359be168c0dSopenharmony_ci+    }
3360be168c0dSopenharmony_ci+  }
3361be168c0dSopenharmony_ci+
3362be168c0dSopenharmony_ci+  if (asyncContext->callbackRef == nullptr) {
3363be168c0dSopenharmony_ci+    status = napi_create_promise(env, &asyncContext->deferred, &result);
3364be168c0dSopenharmony_ci+    if (status != napi_ok) {
3365be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "create promise failed.";
3366be168c0dSopenharmony_ci+      return result;
3367be168c0dSopenharmony_ci+    }
3368be168c0dSopenharmony_ci+  } else {
3369be168c0dSopenharmony_ci+    status = napi_get_undefined(env, &result);
3370be168c0dSopenharmony_ci+    if (status != napi_ok) {
3371be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "create callback failed.";
3372be168c0dSopenharmony_ci+      return result;
3373be168c0dSopenharmony_ci+    }
3374be168c0dSopenharmony_ci+  }
3375be168c0dSopenharmony_ci+
3376be168c0dSopenharmony_ci+  napi_value resource = nullptr;
3377be168c0dSopenharmony_ci+  napi_create_string_utf8(env, "LoadMSLiteModelFromFd", NAPI_AUTO_LENGTH, &resource);
3378be168c0dSopenharmony_ci+  status = napi_create_async_work(
3379be168c0dSopenharmony_ci+    env, nullptr, resource,
3380be168c0dSopenharmony_ci+    [](napi_env env, void *data) {
3381be168c0dSopenharmony_ci+      auto context = static_cast<MSLiteModelAsyncContext *>(data);
3382be168c0dSopenharmony_ci+      context->status = SUCCESS;
3383be168c0dSopenharmony_ci+    },
3384be168c0dSopenharmony_ci+    GetMSLiteModelAsyncCallbackComplete, static_cast<void *>(asyncContext.get()), &asyncContext->work);
3385be168c0dSopenharmony_ci+  if (status != napi_ok) {
3386be168c0dSopenharmony_ci+    result = nullptr;
3387be168c0dSopenharmony_ci+  } else {
3388be168c0dSopenharmony_ci+    status = napi_queue_async_work(env, asyncContext->work);
3389be168c0dSopenharmony_ci+    if (status == napi_ok) {
3390be168c0dSopenharmony_ci+      asyncContext.release();
3391be168c0dSopenharmony_ci+    } else {
3392be168c0dSopenharmony_ci+      result = nullptr;
3393be168c0dSopenharmony_ci+    }
3394be168c0dSopenharmony_ci+  }
3395be168c0dSopenharmony_ci+  return result;
3396be168c0dSopenharmony_ci+}
3397be168c0dSopenharmony_ci+
3398be168c0dSopenharmony_ci+int32_t MSLiteModelNapi::GetCpuDeviceInfo(napi_env env, napi_value args, ContextInfo &context) {
3399be168c0dSopenharmony_ci+  bool has_cpu_property = false;
3400be168c0dSopenharmony_ci+  napi_status status = napi_has_named_property(env, args, "cpu", &has_cpu_property);
3401be168c0dSopenharmony_ci+  if (status != napi_ok) {
3402be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "can not find cpu property";
3403be168c0dSopenharmony_ci+    return ERR_INVALID_OPERATION;
3404be168c0dSopenharmony_ci+  }
3405be168c0dSopenharmony_ci+  if (!has_cpu_property) {
3406be168c0dSopenharmony_ci+    return ERR_NOT_EXISTED_PARAM;
3407be168c0dSopenharmony_ci+  }
3408be168c0dSopenharmony_ci+
3409be168c0dSopenharmony_ci+  napi_value config_item = nullptr;
3410be168c0dSopenharmony_ci+  status = napi_get_named_property(env, args, "cpu", &config_item);
3411be168c0dSopenharmony_ci+  if (status != napi_ok) {
3412be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "can not get cpu property";
3413be168c0dSopenharmony_ci+    return ERR_INVALID_OPERATION;
3414be168c0dSopenharmony_ci+  }
3415be168c0dSopenharmony_ci+
3416be168c0dSopenharmony_ci+  int32_t int_value = 0;
3417be168c0dSopenharmony_ci+  std::string str_value = "";
3418be168c0dSopenharmony_ci+  std::vector<int32_t> affinity_cores;
3419be168c0dSopenharmony_ci+
3420be168c0dSopenharmony_ci+  if (CommonNapi::GetPropertyInt32(env, config_item, "threadNum", int_value) == SUCCESS) {
3421be168c0dSopenharmony_ci+    MS_LOG(DEBUG) << "threadNum: " << int_value;
3422be168c0dSopenharmony_ci+    context.cpu_device.thread_num = int_value;
3423be168c0dSopenharmony_ci+  } else {
3424be168c0dSopenharmony_ci+    context.cpu_device.thread_num = PARAM2;
3425be168c0dSopenharmony_ci+  }
3426be168c0dSopenharmony_ci+
3427be168c0dSopenharmony_ci+  if (CommonNapi::GetPropertyInt32(env, config_item, "threadAffinityMode", int_value) == SUCCESS) {
3428be168c0dSopenharmony_ci+    MS_LOG(DEBUG) << "threadAffinityMode: " << int_value;
3429be168c0dSopenharmony_ci+    if (int_value > PARAM2 || int_value < PARAM0) {
3430be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "threadAffinityMode value is set: " << int_value << ", is out of limition";
3431be168c0dSopenharmony_ci+      return ERR_INVALID_OPERATION;
3432be168c0dSopenharmony_ci+    }
3433be168c0dSopenharmony_ci+    context.cpu_device.thread_affinity_mode = int_value;
3434be168c0dSopenharmony_ci+  } else {
3435be168c0dSopenharmony_ci+    context.cpu_device.thread_affinity_mode = PARAM0;
3436be168c0dSopenharmony_ci+  }
3437be168c0dSopenharmony_ci+
3438be168c0dSopenharmony_ci+  if (CommonNapi::GetPropertyInt32Array(env, config_item, "threadAffinityCoreList", affinity_cores) == SUCCESS) {
3439be168c0dSopenharmony_ci+    MS_LOG(DEBUG) << "affinityCores size: " << affinity_cores.size();
3440be168c0dSopenharmony_ci+    context.cpu_device.thread_affinity_cores.assign(affinity_cores.begin(), affinity_cores.end());
3441be168c0dSopenharmony_ci+  } else {
3442be168c0dSopenharmony_ci+    context.cpu_device.thread_affinity_cores = {};
3443be168c0dSopenharmony_ci+  }
3444be168c0dSopenharmony_ci+
3445be168c0dSopenharmony_ci+  if (CommonNapi::GetPropertyString(env, config_item, "precisionMode", str_value) == SUCCESS) {
3446be168c0dSopenharmony_ci+    MS_LOG(DEBUG) << "precisionMode: " << str_value.c_str();
3447be168c0dSopenharmony_ci+    context.cpu_device.precision_mode = str_value;
3448be168c0dSopenharmony_ci+  } else {
3449be168c0dSopenharmony_ci+    context.cpu_device.precision_mode = "enforce_fp32";
3450be168c0dSopenharmony_ci+  }
3451be168c0dSopenharmony_ci+  return SUCCESS;
3452be168c0dSopenharmony_ci+}
3453be168c0dSopenharmony_ci+
3454be168c0dSopenharmony_ci+int32_t MSLiteModelNapi::GetNNRTDeviceInfo(napi_env env, napi_value args, ContextInfo &context) {
3455be168c0dSopenharmony_ci+  bool has_nnrt_property = false;
3456be168c0dSopenharmony_ci+  napi_status status = napi_has_named_property(env, args, "nnrt", &has_nnrt_property);
3457be168c0dSopenharmony_ci+  if (status != napi_ok) {
3458be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "can not find nnrt property";
3459be168c0dSopenharmony_ci+    return ERR_ILLEGAL_STATE;
3460be168c0dSopenharmony_ci+  }
3461be168c0dSopenharmony_ci+  if (!has_nnrt_property) {
3462be168c0dSopenharmony_ci+    return ERR_NOT_EXISTED_PARAM;
3463be168c0dSopenharmony_ci+  }
3464be168c0dSopenharmony_ci+
3465be168c0dSopenharmony_ci+  napi_value config_item = nullptr;
3466be168c0dSopenharmony_ci+  status = napi_get_named_property(env, args, "nnrt", &config_item);
3467be168c0dSopenharmony_ci+  if (status != napi_ok) {
3468be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "can not get nnrt property";
3469be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
3470be168c0dSopenharmony_ci+  }
3471be168c0dSopenharmony_ci+
3472be168c0dSopenharmony_ci+  int32_t int_value = 0;
3473be168c0dSopenharmony_ci+  std::string str_value = "";
3474be168c0dSopenharmony_ci+  std::vector<int32_t> affinity_cores;
3475be168c0dSopenharmony_ci+
3476be168c0dSopenharmony_ci+  uint64_t device_id;
3477be168c0dSopenharmony_ci+  auto ret = CommonNapi::GetPropertyBigIntUint64(env, config_item, "deviceID", device_id);
3478be168c0dSopenharmony_ci+  if (ret == SUCCESS) {
3479be168c0dSopenharmony_ci+    MS_LOG(DEBUG) << "deviceID: " << device_id;
3480be168c0dSopenharmony_ci+    context.nnrt_device.device_id = static_cast<size_t>(device_id);
3481be168c0dSopenharmony_ci+  } else if (ret == ERR_NOT_EXISTED_PARAM) {
3482be168c0dSopenharmony_ci+    size_t num = 0;
3483be168c0dSopenharmony_ci+    auto *desc = OH_AI_GetAllNNRTDeviceDescs(&num);
3484be168c0dSopenharmony_ci+    if (desc == nullptr || num == 0) {
3485be168c0dSopenharmony_ci+      MS_LOG(WARNING) << "Failed to get nnrt device id, skip adding nnrt device info.";
3486be168c0dSopenharmony_ci+      return ERR_NOT_EXISTED_PARAM;
3487be168c0dSopenharmony_ci+    }
3488be168c0dSopenharmony_ci+    auto id = OH_AI_GetDeviceIdFromNNRTDeviceDesc(desc);
3489be168c0dSopenharmony_ci+    OH_AI_DestroyAllNNRTDeviceDescs(&desc);
3490be168c0dSopenharmony_ci+    MS_LOG(INFO) << "set nnrt device id to " << id;
3491be168c0dSopenharmony_ci+    context.nnrt_device.device_id = id;
3492be168c0dSopenharmony_ci+  } else {
3493be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
3494be168c0dSopenharmony_ci+  }
3495be168c0dSopenharmony_ci+
3496be168c0dSopenharmony_ci+  ret = CommonNapi::GetPropertyInt32(env, config_item, "performanceMode", int_value);
3497be168c0dSopenharmony_ci+  if (ret == SUCCESS) {
3498be168c0dSopenharmony_ci+    MS_LOG(DEBUG) << "performanceMode: " << int_value;
3499be168c0dSopenharmony_ci+    if (int_value > PARAM4 || int_value < PARAM0) {
3500be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "performanceMode value is set to: " << int_value << ", which is out of range";
3501be168c0dSopenharmony_ci+      return ERR_INVALID_PARAM;
3502be168c0dSopenharmony_ci+    }
3503be168c0dSopenharmony_ci+    context.nnrt_device.performance_mode = int_value;
3504be168c0dSopenharmony_ci+  } else if (ret == ERR_NOT_EXISTED_PARAM) {
3505be168c0dSopenharmony_ci+    context.nnrt_device.performance_mode = UNSET_VALUE;
3506be168c0dSopenharmony_ci+  } else {
3507be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
3508be168c0dSopenharmony_ci+  }
3509be168c0dSopenharmony_ci+
3510be168c0dSopenharmony_ci+  ret = CommonNapi::GetPropertyInt32(env, config_item, "priority", int_value);
3511be168c0dSopenharmony_ci+  if (ret == SUCCESS) {
3512be168c0dSopenharmony_ci+    MS_LOG(DEBUG) << "priority: " << int_value;
3513be168c0dSopenharmony_ci+    if (int_value > PARAM3 || int_value < PARAM0) {
3514be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "priority value is set to: " << int_value << ", which is out of range";
3515be168c0dSopenharmony_ci+      return ERR_INVALID_PARAM;
3516be168c0dSopenharmony_ci+    }
3517be168c0dSopenharmony_ci+    context.nnrt_device.priority = int_value;
3518be168c0dSopenharmony_ci+  } else if (ret == ERR_NOT_EXISTED_PARAM) {
3519be168c0dSopenharmony_ci+    context.nnrt_device.priority = UNSET_VALUE;
3520be168c0dSopenharmony_ci+  } else {
3521be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
3522be168c0dSopenharmony_ci+  }
3523be168c0dSopenharmony_ci+
3524be168c0dSopenharmony_ci+  // ignore extensions for now
3525be168c0dSopenharmony_ci+  return SUCCESS;
3526be168c0dSopenharmony_ci+}
3527be168c0dSopenharmony_ci+
3528be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::GetInputs(napi_env env, napi_callback_info info) {
3529be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
3530be168c0dSopenharmony_ci+  napi_get_undefined(env, &undefinedResult);
3531be168c0dSopenharmony_ci+
3532be168c0dSopenharmony_ci+  size_t argCount = 0;
3533be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
3534be168c0dSopenharmony_ci+  napi_value jsResult = nullptr;
3535be168c0dSopenharmony_ci+  MSLiteModelNapi *modelNapi = nullptr;
3536be168c0dSopenharmony_ci+
3537be168c0dSopenharmony_ci+  napi_status status = napi_get_cb_info(env, info, &argCount, nullptr, &jsThis, nullptr);
3538be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
3539be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "failed to retrieve details about the callback";
3540be168c0dSopenharmony_ci+    return undefinedResult;
3541be168c0dSopenharmony_ci+  }
3542be168c0dSopenharmony_ci+
3543be168c0dSopenharmony_ci+  status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&modelNapi));
3544be168c0dSopenharmony_ci+  if (status != napi_ok || modelNapi == nullptr) {
3545be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "failed to get model";
3546be168c0dSopenharmony_ci+    return undefinedResult;
3547be168c0dSopenharmony_ci+  }
3548be168c0dSopenharmony_ci+
3549be168c0dSopenharmony_ci+  if (modelNapi->native_model_ == nullptr) {
3550be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "model is released(null), please create model again";
3551be168c0dSopenharmony_ci+    return undefinedResult;
3552be168c0dSopenharmony_ci+  }
3553be168c0dSopenharmony_ci+  std::vector<MSTensor> inputs = modelNapi->native_model_->GetInputs();
3554be168c0dSopenharmony_ci+  std::vector<MSTensor> tensor_inputs;
3555be168c0dSopenharmony_ci+  for (size_t i = 0; i < inputs.size(); i++) {
3556be168c0dSopenharmony_ci+    auto tensor = mindspore::MSTensor::CreateTensor(inputs.at(i).Name(), inputs.at(i).DataType(), {}, nullptr, 0);
3557be168c0dSopenharmony_ci+    if (tensor == nullptr) {
3558be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "create tensor failed.";
3559be168c0dSopenharmony_ci+      return undefinedResult;
3560be168c0dSopenharmony_ci+    }
3561be168c0dSopenharmony_ci+    tensor->SetShape(inputs.at(i).Shape());
3562be168c0dSopenharmony_ci+    tensor->SetFormat(inputs.at(i).format());
3563be168c0dSopenharmony_ci+    tensor->SetDataType(inputs.at(i).DataType());
3564be168c0dSopenharmony_ci+    tensor_inputs.push_back(*tensor);
3565be168c0dSopenharmony_ci+    delete tensor;
3566be168c0dSopenharmony_ci+  }
3567be168c0dSopenharmony_ci+
3568be168c0dSopenharmony_ci+  size_t size = inputs.size();
3569be168c0dSopenharmony_ci+  MS_LOG(INFO) << "inputs size: " << size;
3570be168c0dSopenharmony_ci+  napi_create_array_with_length(env, size, &jsResult);
3571be168c0dSopenharmony_ci+  for (size_t i = 0; i < size; i++) {
3572be168c0dSopenharmony_ci+    status = napi_set_element(env, jsResult, i, MSTensorNapi::NewInstance(env, tensor_inputs[i]));
3573be168c0dSopenharmony_ci+    if (status != napi_ok) {
3574be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "napi_set_element failed! code: " << status;
3575be168c0dSopenharmony_ci+    } 
3576be168c0dSopenharmony_ci+  }
3577be168c0dSopenharmony_ci+  MS_LOG(INFO) << "get model inputs success: " << inputs[0].Name().c_str();
3578be168c0dSopenharmony_ci+  return jsResult;
3579be168c0dSopenharmony_ci+}
3580be168c0dSopenharmony_ci+
3581be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::Resize(napi_env env, napi_callback_info info) {
3582be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
3583be168c0dSopenharmony_ci+  bool result = false;
3584be168c0dSopenharmony_ci+  napi_status status = napi_get_boolean(env, result, &undefinedResult);
3585be168c0dSopenharmony_ci+  if (status != napi_ok) {
3586be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get bool error";
3587be168c0dSopenharmony_ci+    return undefinedResult;
3588be168c0dSopenharmony_ci+  }
3589be168c0dSopenharmony_ci+
3590be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
3591be168c0dSopenharmony_ci+  napi_value jsResult = nullptr;
3592be168c0dSopenharmony_ci+  MSLiteModelNapi *modelNapi = nullptr;
3593be168c0dSopenharmony_ci+  napi_value argv[ARGS_TWO] = {0};
3594be168c0dSopenharmony_ci+  size_t argCount = PARAM2;
3595be168c0dSopenharmony_ci+  status = napi_get_cb_info(env, info, &argCount, argv, &jsThis, nullptr);
3596be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
3597be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "failed to retrieve details about the callback";
3598be168c0dSopenharmony_ci+    return undefinedResult;
3599be168c0dSopenharmony_ci+  }
3600be168c0dSopenharmony_ci+  status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&modelNapi));
3601be168c0dSopenharmony_ci+  if (status != napi_ok || modelNapi == nullptr) {
3602be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get model napi error";
3603be168c0dSopenharmony_ci+    return undefinedResult;
3604be168c0dSopenharmony_ci+  }
3605be168c0dSopenharmony_ci+
3606be168c0dSopenharmony_ci+  if (modelNapi->native_model_ == nullptr) {
3607be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "model is released(null), please create model again";
3608be168c0dSopenharmony_ci+    return undefinedResult;
3609be168c0dSopenharmony_ci+  }
3610be168c0dSopenharmony_ci+  std::vector<MSTensor> inputs = modelNapi->native_model_->GetInputs();
3611be168c0dSopenharmony_ci+  std::vector<MSTensor> tensor_inputs;
3612be168c0dSopenharmony_ci+  std::vector<std::vector<int64_t>> dims;
3613be168c0dSopenharmony_ci+
3614be168c0dSopenharmony_ci+  // set inputs data
3615be168c0dSopenharmony_ci+  uint32_t array_length = 0;
3616be168c0dSopenharmony_ci+  status = napi_get_array_length(env, argv[PARAM0], &array_length);
3617be168c0dSopenharmony_ci+  if (status != napi_ok || array_length <= 0) {
3618be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get inputs tensor length failed.";
3619be168c0dSopenharmony_ci+    return undefinedResult;
3620be168c0dSopenharmony_ci+  }
3621be168c0dSopenharmony_ci+  if (inputs.size() != array_length) {
3622be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "array length not equal to model inputs size.";
3623be168c0dSopenharmony_ci+    return undefinedResult;
3624be168c0dSopenharmony_ci+  }
3625be168c0dSopenharmony_ci+  for (size_t i = 0; i < array_length; i++) {
3626be168c0dSopenharmony_ci+    napi_value element = nullptr;
3627be168c0dSopenharmony_ci+    status = napi_get_element(env, argv[PARAM0], i, &element);
3628be168c0dSopenharmony_ci+    if (status != napi_ok) {
3629be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "can not get element";
3630be168c0dSopenharmony_ci+      return undefinedResult;
3631be168c0dSopenharmony_ci+    }
3632be168c0dSopenharmony_ci+
3633be168c0dSopenharmony_ci+    std::string property_name = "getData";
3634be168c0dSopenharmony_ci+    bool exist = false;
3635be168c0dSopenharmony_ci+    napi_value data_func = nullptr;
3636be168c0dSopenharmony_ci+
3637be168c0dSopenharmony_ci+    status = napi_has_named_property(env, element, property_name.c_str(), &exist);
3638be168c0dSopenharmony_ci+    if (status != napi_ok || !exist) {
3639be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "can not find target property";
3640be168c0dSopenharmony_ci+      return undefinedResult;
3641be168c0dSopenharmony_ci+    }
3642be168c0dSopenharmony_ci+
3643be168c0dSopenharmony_ci+    if (status != napi_ok || !exist) {
3644be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "can not find " << property_name.c_str() << " property.";
3645be168c0dSopenharmony_ci+      return undefinedResult;
3646be168c0dSopenharmony_ci+    }
3647be168c0dSopenharmony_ci+
3648be168c0dSopenharmony_ci+    if (napi_get_named_property(env, element, property_name.c_str(), &data_func) != napi_ok) {
3649be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "get " << property_name.c_str() << " property fail.";
3650be168c0dSopenharmony_ci+      return undefinedResult;
3651be168c0dSopenharmony_ci+    }
3652be168c0dSopenharmony_ci+    void *js_data = nullptr;
3653be168c0dSopenharmony_ci+    size_t length = 0;
3654be168c0dSopenharmony_ci+    napi_value return_val;
3655be168c0dSopenharmony_ci+
3656be168c0dSopenharmony_ci+    status = napi_call_function(env, element, data_func, 0, nullptr, &return_val);
3657be168c0dSopenharmony_ci+    if (status != napi_ok || return_val == nullptr) {
3658be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "napi call function error.";
3659be168c0dSopenharmony_ci+      return undefinedResult;
3660be168c0dSopenharmony_ci+    }
3661be168c0dSopenharmony_ci+
3662be168c0dSopenharmony_ci+    status = napi_get_arraybuffer_info(env, return_val, &js_data, &length);
3663be168c0dSopenharmony_ci+    if (status != napi_ok || js_data == nullptr) {
3664be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "get js data error.";
3665be168c0dSopenharmony_ci+      return undefinedResult;
3666be168c0dSopenharmony_ci+    }
3667be168c0dSopenharmony_ci+    if (inputs[i].DataSize() != length) {
3668be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "tensor size is: " << static_cast<int>(inputs[i].DataSize()) << ", but data length got "
3669be168c0dSopenharmony_ci+                    << static_cast<int>(length);
3670be168c0dSopenharmony_ci+      return undefinedResult;
3671be168c0dSopenharmony_ci+    }
3672be168c0dSopenharmony_ci+
3673be168c0dSopenharmony_ci+    auto tensor_data = inputs[i].MutableData();
3674be168c0dSopenharmony_ci+    if (tensor_data == nullptr) {
3675be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "malloc data for tensor failed.";
3676be168c0dSopenharmony_ci+      return undefinedResult;
3677be168c0dSopenharmony_ci+    }
3678be168c0dSopenharmony_ci+    memcpy(tensor_data, js_data, length);
3679be168c0dSopenharmony_ci+  }
3680be168c0dSopenharmony_ci+
3681be168c0dSopenharmony_ci+  napi_value dim_num = nullptr;
3682be168c0dSopenharmony_ci+  int64_t dim_ele = 0;
3683be168c0dSopenharmony_ci+  uint32_t dims_size = 0;
3684be168c0dSopenharmony_ci+  uint32_t dim_size = 0;
3685be168c0dSopenharmony_ci+
3686be168c0dSopenharmony_ci+  status = napi_is_array(env, argv[PARAM1], &result);
3687be168c0dSopenharmony_ci+  if (status != napi_ok || result == false) {
3688be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "new dim is not a array";
3689be168c0dSopenharmony_ci+    return undefinedResult;
3690be168c0dSopenharmony_ci+  }
3691be168c0dSopenharmony_ci+
3692be168c0dSopenharmony_ci+  status = napi_get_array_length(env, argv[PARAM1], &dims_size);
3693be168c0dSopenharmony_ci+  if (status != napi_ok) {
3694be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get new dims size error";
3695be168c0dSopenharmony_ci+    return undefinedResult;
3696be168c0dSopenharmony_ci+  }
3697be168c0dSopenharmony_ci+  for (size_t i = 0; i < dims_size; i++) {
3698be168c0dSopenharmony_ci+    napi_value dim_element = nullptr;
3699be168c0dSopenharmony_ci+    status = napi_get_element(env, argv[PARAM1], i, &dim_element);
3700be168c0dSopenharmony_ci+    if (status != napi_ok) {
3701be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "can not get element";
3702be168c0dSopenharmony_ci+      return undefinedResult;
3703be168c0dSopenharmony_ci+    }
3704be168c0dSopenharmony_ci+
3705be168c0dSopenharmony_ci+    status = napi_is_array(env, dim_element, &result);
3706be168c0dSopenharmony_ci+    if (status != napi_ok || result == false) {
3707be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "new dim's element is not a array";
3708be168c0dSopenharmony_ci+      return undefinedResult;
3709be168c0dSopenharmony_ci+    }
3710be168c0dSopenharmony_ci+
3711be168c0dSopenharmony_ci+    status = napi_get_array_length(env, dim_element, &dim_size);
3712be168c0dSopenharmony_ci+    if (status != napi_ok) {
3713be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "get new dim size error";
3714be168c0dSopenharmony_ci+      return undefinedResult;
3715be168c0dSopenharmony_ci+    }
3716be168c0dSopenharmony_ci+    std::vector<int64_t> dim(dim_size);
3717be168c0dSopenharmony_ci+    for (size_t j = 0; j < dim_size; j++) {
3718be168c0dSopenharmony_ci+      status = napi_get_element(env, dim_element, j, &dim_num);
3719be168c0dSopenharmony_ci+      if (status != napi_ok) {
3720be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "get dim num error";
3721be168c0dSopenharmony_ci+        return undefinedResult;
3722be168c0dSopenharmony_ci+      }
3723be168c0dSopenharmony_ci+      status = napi_get_value_int64(env, dim_num, &dim_ele);
3724be168c0dSopenharmony_ci+      if (status != napi_ok) {
3725be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "get dim element error";
3726be168c0dSopenharmony_ci+        return undefinedResult;
3727be168c0dSopenharmony_ci+      }
3728be168c0dSopenharmony_ci+      dim[j] = dim_ele;
3729be168c0dSopenharmony_ci+    }
3730be168c0dSopenharmony_ci+    dims.push_back(dim);
3731be168c0dSopenharmony_ci+  }
3732be168c0dSopenharmony_ci+  if (modelNapi->native_model_->Resize(inputs, dims) != mindspore::kSuccess) {
3733be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "resize failed";
3734be168c0dSopenharmony_ci+    return undefinedResult;
3735be168c0dSopenharmony_ci+  }
3736be168c0dSopenharmony_ci+  status = napi_get_boolean(env, result, &jsResult);
3737be168c0dSopenharmony_ci+  if (status != napi_ok) {
3738be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get bool error";
3739be168c0dSopenharmony_ci+    return undefinedResult;
3740be168c0dSopenharmony_ci+  }
3741be168c0dSopenharmony_ci+  return jsResult;
3742be168c0dSopenharmony_ci+}
3743be168c0dSopenharmony_ci+
3744be168c0dSopenharmony_ci+template <typename T, typename Distribution>
3745be168c0dSopenharmony_ci+void GenerateRandomData(int size, void *data, Distribution distribution) {
3746be168c0dSopenharmony_ci+  std::mt19937 random_engine;
3747be168c0dSopenharmony_ci+  int elements_num = size / sizeof(T);
3748be168c0dSopenharmony_ci+  (void)std::generate_n(static_cast<T *>(data), elements_num,
3749be168c0dSopenharmony_ci+                        [&distribution, &random_engine]() { return static_cast<T>(distribution(random_engine)); });
3750be168c0dSopenharmony_ci+}
3751be168c0dSopenharmony_ci+
3752be168c0dSopenharmony_ci+int GenerateInputDataWithRandom(std::vector<mindspore::MSTensor> inputs) {
3753be168c0dSopenharmony_ci+  for (auto tensor : inputs) {
3754be168c0dSopenharmony_ci+    auto input_data = tensor.MutableData();
3755be168c0dSopenharmony_ci+    if (input_data == nullptr) {
3756be168c0dSopenharmony_ci+      std::cerr << "mallocData for inTensor failed." << std::endl;
3757be168c0dSopenharmony_ci+      return -1;
3758be168c0dSopenharmony_ci+    }
3759be168c0dSopenharmony_ci+    GenerateRandomData<float>(tensor.DataSize(), input_data, std::uniform_real_distribution<float>(0.1f, 1.0f));
3760be168c0dSopenharmony_ci+  }
3761be168c0dSopenharmony_ci+  return mindspore::kSuccess;
3762be168c0dSopenharmony_ci+}
3763be168c0dSopenharmony_ci+
3764be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::PredictAsync(napi_env env, napi_callback_info info) {
3765be168c0dSopenharmony_ci+  napi_status status = napi_ok;
3766be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
3767be168c0dSopenharmony_ci+  napi_value result = nullptr;
3768be168c0dSopenharmony_ci+  const int32_t refCount = 1;
3769be168c0dSopenharmony_ci+  napi_valuetype valueType;
3770be168c0dSopenharmony_ci+
3771be168c0dSopenharmony_ci+  std::unique_ptr<MSLiteModelAsyncContext> asyncContext = std::make_unique<MSLiteModelAsyncContext>();
3772be168c0dSopenharmony_ci+  if (asyncContext == nullptr) {
3773be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "MSLiteModelAsyncContext object create failed.";
3774be168c0dSopenharmony_ci+    return undefinedResult;
3775be168c0dSopenharmony_ci+  }
3776be168c0dSopenharmony_ci+
3777be168c0dSopenharmony_ci+  GET_PARAMS(env, info, ARGS_TWO);
3778be168c0dSopenharmony_ci+  for (size_t i = PARAM0; i < argc; i++) {
3779be168c0dSopenharmony_ci+    if (i == PARAM1) {
3780be168c0dSopenharmony_ci+      status = napi_typeof(env, argv[i], &valueType);
3781be168c0dSopenharmony_ci+      if ((status != napi_ok) || (valueType != napi_function)) {
3782be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "napi_typeof check callback failed.";
3783be168c0dSopenharmony_ci+        return result;
3784be168c0dSopenharmony_ci+      }
3785be168c0dSopenharmony_ci+      status = napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
3786be168c0dSopenharmony_ci+      if (status != napi_ok) {
3787be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "failed to create reference of callback";
3788be168c0dSopenharmony_ci+        return result;
3789be168c0dSopenharmony_ci+      }
3790be168c0dSopenharmony_ci+    }
3791be168c0dSopenharmony_ci+  }
3792be168c0dSopenharmony_ci+
3793be168c0dSopenharmony_ci+  if (SetTensorData(env, thisVar, argv[PARAM0], asyncContext.get()) != SUCCESS) {
3794be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Set tensor data failed.";
3795be168c0dSopenharmony_ci+    return undefinedResult;
3796be168c0dSopenharmony_ci+  }
3797be168c0dSopenharmony_ci+
3798be168c0dSopenharmony_ci+  if (asyncContext->callbackRef == nullptr) {
3799be168c0dSopenharmony_ci+    status = napi_create_promise(env, &asyncContext->deferred, &result);
3800be168c0dSopenharmony_ci+    if (status != napi_ok) {
3801be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "create promise failed.";
3802be168c0dSopenharmony_ci+      return result;
3803be168c0dSopenharmony_ci+    }
3804be168c0dSopenharmony_ci+  } else {
3805be168c0dSopenharmony_ci+    status = napi_get_undefined(env, &result);
3806be168c0dSopenharmony_ci+    if (status != napi_ok) {
3807be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "create callback failed.";
3808be168c0dSopenharmony_ci+      return result;
3809be168c0dSopenharmony_ci+    }
3810be168c0dSopenharmony_ci+  }
3811be168c0dSopenharmony_ci+
3812be168c0dSopenharmony_ci+  napi_value resource = nullptr;
3813be168c0dSopenharmony_ci+  napi_create_string_utf8(env, "Predict", NAPI_AUTO_LENGTH, &resource);
3814be168c0dSopenharmony_ci+  status = napi_create_async_work(
3815be168c0dSopenharmony_ci+    env, nullptr, resource,
3816be168c0dSopenharmony_ci+    [](napi_env env, void *data) {
3817be168c0dSopenharmony_ci+      auto context = static_cast<MSLiteModelAsyncContext *>(data);
3818be168c0dSopenharmony_ci+      context->status = SUCCESS;
3819be168c0dSopenharmony_ci+    },
3820be168c0dSopenharmony_ci+    PredictAsyncCallbackComplete, static_cast<void *>(asyncContext.get()), &asyncContext->work);
3821be168c0dSopenharmony_ci+  if (status != napi_ok) {
3822be168c0dSopenharmony_ci+    result = nullptr;
3823be168c0dSopenharmony_ci+  } else {
3824be168c0dSopenharmony_ci+    status = napi_queue_async_work(env, asyncContext->work);
3825be168c0dSopenharmony_ci+    if (status == napi_ok) {
3826be168c0dSopenharmony_ci+      asyncContext.release();
3827be168c0dSopenharmony_ci+    } else {
3828be168c0dSopenharmony_ci+      result = nullptr;
3829be168c0dSopenharmony_ci+    }
3830be168c0dSopenharmony_ci+  }
3831be168c0dSopenharmony_ci+  return result;
3832be168c0dSopenharmony_ci+}
3833be168c0dSopenharmony_ci+
3834be168c0dSopenharmony_ci+int32_t MSLiteModelNapi::SetTensorData(napi_env env, napi_value thisVar, napi_value argv,
3835be168c0dSopenharmony_ci+                                       MSLiteModelAsyncContext *async_context) {
3836be168c0dSopenharmony_ci+  uint32_t array_length = 0;
3837be168c0dSopenharmony_ci+  napi_status status = napi_get_array_length(env, argv, &array_length);
3838be168c0dSopenharmony_ci+  if (status != napi_ok || array_length <= 0) {
3839be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get inputs tensor length failed.";
3840be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
3841be168c0dSopenharmony_ci+  }
3842be168c0dSopenharmony_ci+
3843be168c0dSopenharmony_ci+  status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&(async_context->lite_model)));
3844be168c0dSopenharmony_ci+  if (status != napi_ok || async_context->lite_model == nullptr) {
3845be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get model napi error";
3846be168c0dSopenharmony_ci+    return ERROR;
3847be168c0dSopenharmony_ci+  }
3848be168c0dSopenharmony_ci+  auto modelNapi = async_context->lite_model;
3849be168c0dSopenharmony_ci+  if (modelNapi->native_model_ == nullptr) {
3850be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "model is released(null), please create model again";
3851be168c0dSopenharmony_ci+    return ERROR;
3852be168c0dSopenharmony_ci+  }
3853be168c0dSopenharmony_ci+
3854be168c0dSopenharmony_ci+  auto inputs = modelNapi->native_model_->GetInputs();
3855be168c0dSopenharmony_ci+  if (inputs.size() != array_length) {
3856be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "array length not equal to model inputs size.";
3857be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
3858be168c0dSopenharmony_ci+  }
3859be168c0dSopenharmony_ci+
3860be168c0dSopenharmony_ci+  for (size_t i = 0; i < array_length; i++) {
3861be168c0dSopenharmony_ci+    napi_value element = nullptr;
3862be168c0dSopenharmony_ci+    status = napi_get_element(env, argv, i, &element);
3863be168c0dSopenharmony_ci+    if (status != napi_ok) {
3864be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "can not get element";
3865be168c0dSopenharmony_ci+      return ERROR;
3866be168c0dSopenharmony_ci+    }
3867be168c0dSopenharmony_ci+
3868be168c0dSopenharmony_ci+    std::string property_name = "getData";
3869be168c0dSopenharmony_ci+    bool exist = false;
3870be168c0dSopenharmony_ci+    napi_value data_func = nullptr;
3871be168c0dSopenharmony_ci+
3872be168c0dSopenharmony_ci+    napi_status status = napi_has_named_property(env, element, property_name.c_str(), &exist);
3873be168c0dSopenharmony_ci+
3874be168c0dSopenharmony_ci+    if (status != napi_ok || !exist) {
3875be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "can not find " << property_name.c_str() << " property.";
3876be168c0dSopenharmony_ci+      return ERROR;
3877be168c0dSopenharmony_ci+    }
3878be168c0dSopenharmony_ci+
3879be168c0dSopenharmony_ci+    if (napi_get_named_property(env, element, property_name.c_str(), &data_func) != napi_ok) {
3880be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "get " << property_name.c_str() << " property fail.";
3881be168c0dSopenharmony_ci+      return ERROR;
3882be168c0dSopenharmony_ci+    }
3883be168c0dSopenharmony_ci+    void *js_data = nullptr;
3884be168c0dSopenharmony_ci+    size_t length = 0;
3885be168c0dSopenharmony_ci+    napi_value return_val;
3886be168c0dSopenharmony_ci+
3887be168c0dSopenharmony_ci+    status = napi_call_function(env, element, data_func, 0, nullptr, &return_val);
3888be168c0dSopenharmony_ci+    if (status != napi_ok || return_val == nullptr) {
3889be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "napi call function error.";
3890be168c0dSopenharmony_ci+      return ERROR;
3891be168c0dSopenharmony_ci+    }
3892be168c0dSopenharmony_ci+    status = napi_get_arraybuffer_info(env, return_val, &js_data, &length);
3893be168c0dSopenharmony_ci+    if (status != napi_ok || js_data == nullptr) {
3894be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "Get js data error.";
3895be168c0dSopenharmony_ci+      return ERROR;
3896be168c0dSopenharmony_ci+    }
3897be168c0dSopenharmony_ci+    if (inputs[i].DataSize() != length) {
3898be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "tensor size is: " << static_cast<int>(inputs[i].DataSize()) << ", but data length got "
3899be168c0dSopenharmony_ci+                    << static_cast<int>(length);
3900be168c0dSopenharmony_ci+      return ERROR;
3901be168c0dSopenharmony_ci+    }
3902be168c0dSopenharmony_ci+
3903be168c0dSopenharmony_ci+    auto tensor_data = inputs[i].MutableData();
3904be168c0dSopenharmony_ci+    if (tensor_data == nullptr) {
3905be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "malloc data for tensor failed.";
3906be168c0dSopenharmony_ci+      return ERROR;
3907be168c0dSopenharmony_ci+    }
3908be168c0dSopenharmony_ci+    memcpy(tensor_data, js_data, length);
3909be168c0dSopenharmony_ci+  }
3910be168c0dSopenharmony_ci+  return SUCCESS;
3911be168c0dSopenharmony_ci+}
3912be168c0dSopenharmony_ci+
3913be168c0dSopenharmony_ci+void MSLiteModelNapi::PredictAsyncCallbackComplete(napi_env env, napi_status status, void *data) {
3914be168c0dSopenharmony_ci+  napi_value valueParam = nullptr;
3915be168c0dSopenharmony_ci+  auto asyncContext = static_cast<MSLiteModelAsyncContext *>(data);
3916be168c0dSopenharmony_ci+
3917be168c0dSopenharmony_ci+  if (asyncContext != nullptr) {
3918be168c0dSopenharmony_ci+    if (!asyncContext->status) {
3919be168c0dSopenharmony_ci+      auto modelNapi = asyncContext->lite_model;
3920be168c0dSopenharmony_ci+      if (modelNapi->native_model_ == nullptr) {
3921be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "model is released(null), please create model again";
3922be168c0dSopenharmony_ci+        return;
3923be168c0dSopenharmony_ci+      }
3924be168c0dSopenharmony_ci+      auto inputs = modelNapi->native_model_->GetInputs();
3925be168c0dSopenharmony_ci+      std::vector<MSTensor> outputs;
3926be168c0dSopenharmony_ci+
3927be168c0dSopenharmony_ci+      auto predict_ret = modelNapi->native_model_->Predict(inputs, &outputs);
3928be168c0dSopenharmony_ci+      if (predict_ret != mindspore::kSuccess) {
3929be168c0dSopenharmony_ci+        MS_LOG(ERROR) << "model predict failed.";
3930be168c0dSopenharmony_ci+        return;
3931be168c0dSopenharmony_ci+      }
3932be168c0dSopenharmony_ci+
3933be168c0dSopenharmony_ci+      napi_create_array_with_length(env, outputs.size(), &valueParam);
3934be168c0dSopenharmony_ci+      for (size_t i = 0; i < outputs.size(); i++) {
3935be168c0dSopenharmony_ci+        status = napi_set_element(env, valueParam, i, MSTensorNapi::NewInstance(env, outputs[i]));
3936be168c0dSopenharmony_ci+        if (status != napi_ok) {
3937be168c0dSopenharmony_ci+          MS_LOG(ERROR) << "napi_set_element failed! code: " << status;
3938be168c0dSopenharmony_ci+        }
3939be168c0dSopenharmony_ci+      }
3940be168c0dSopenharmony_ci+      MS_LOG(INFO) << "predict model success.";
3941be168c0dSopenharmony_ci+    }
3942be168c0dSopenharmony_ci+    CommonCallbackRoutine(env, asyncContext, valueParam);
3943be168c0dSopenharmony_ci+  } else {
3944be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "ERROR: PredictAsyncCallbackComplete asyncContext is Null!";
3945be168c0dSopenharmony_ci+  }
3946be168c0dSopenharmony_ci+}
3947be168c0dSopenharmony_ci+
3948be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::GetWeights(napi_env env, napi_callback_info info) {
3949be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
3950be168c0dSopenharmony_ci+  napi_get_undefined(env, &undefinedResult);
3951be168c0dSopenharmony_ci+
3952be168c0dSopenharmony_ci+  size_t argCount = 0;
3953be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
3954be168c0dSopenharmony_ci+  napi_value jsResult = nullptr;
3955be168c0dSopenharmony_ci+  MSLiteModelNapi *modelNapi = nullptr;
3956be168c0dSopenharmony_ci+
3957be168c0dSopenharmony_ci+  napi_status status = napi_get_cb_info(env, info, &argCount, nullptr, &jsThis, nullptr);
3958be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
3959be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "failed to retrieve details about the callback";
3960be168c0dSopenharmony_ci+    return undefinedResult;
3961be168c0dSopenharmony_ci+  }
3962be168c0dSopenharmony_ci+
3963be168c0dSopenharmony_ci+  status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&modelNapi));
3964be168c0dSopenharmony_ci+  if (status != napi_ok || modelNapi == nullptr) {
3965be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "failed to get model";
3966be168c0dSopenharmony_ci+    return undefinedResult;
3967be168c0dSopenharmony_ci+  }
3968be168c0dSopenharmony_ci+
3969be168c0dSopenharmony_ci+  if (modelNapi->native_model_ == nullptr) {
3970be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "model is released(null), please create model again";
3971be168c0dSopenharmony_ci+    return undefinedResult;
3972be168c0dSopenharmony_ci+  }
3973be168c0dSopenharmony_ci+  std::vector<MSTensor> weights = modelNapi->native_model_->GetFeatureMaps();
3974be168c0dSopenharmony_ci+  std::vector<MSTensor> feature_maps;
3975be168c0dSopenharmony_ci+  for (size_t i = 0; i < weights.size(); i++) {
3976be168c0dSopenharmony_ci+    auto tensor = mindspore::MSTensor::CreateTensor(weights.at(i).Name(), weights.at(i).DataType(), {}, nullptr, 0);
3977be168c0dSopenharmony_ci+    if (tensor == nullptr) {
3978be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "create tensor failed.";
3979be168c0dSopenharmony_ci+      return undefinedResult;
3980be168c0dSopenharmony_ci+    }
3981be168c0dSopenharmony_ci+    tensor->SetShape(weights.at(i).Shape());
3982be168c0dSopenharmony_ci+    tensor->SetFormat(weights.at(i).format());
3983be168c0dSopenharmony_ci+    tensor->SetDataType(weights.at(i).DataType());
3984be168c0dSopenharmony_ci+    tensor->SetData(weights.at(i).MutableData(), false);
3985be168c0dSopenharmony_ci+    feature_maps.push_back(*tensor);
3986be168c0dSopenharmony_ci+    delete tensor;
3987be168c0dSopenharmony_ci+  }
3988be168c0dSopenharmony_ci+
3989be168c0dSopenharmony_ci+  size_t size = weights.size();
3990be168c0dSopenharmony_ci+  MS_LOG(INFO) << "weights size: " << size;
3991be168c0dSopenharmony_ci+  napi_create_array_with_length(env, size, &jsResult);
3992be168c0dSopenharmony_ci+  for (size_t i = 0; i < size; i++) {
3993be168c0dSopenharmony_ci+    status = napi_set_element(env, jsResult, i, MSTensorNapi::NewInstance(env, feature_maps[i]));
3994be168c0dSopenharmony_ci+    if (status != napi_ok) {
3995be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "napi_set_element failed! code: " << status;
3996be168c0dSopenharmony_ci+    }
3997be168c0dSopenharmony_ci+  }
3998be168c0dSopenharmony_ci+  MS_LOG(INFO) << "get model weights success";
3999be168c0dSopenharmony_ci+  return jsResult;
4000be168c0dSopenharmony_ci+}
4001be168c0dSopenharmony_ci+
4002be168c0dSopenharmony_ci+int32_t SetModelInputs(napi_env env, napi_value argv, std::shared_ptr<Model> model) {
4003be168c0dSopenharmony_ci+  uint32_t array_length = 0;
4004be168c0dSopenharmony_ci+  napi_status status = napi_get_array_length(env, argv, &array_length);
4005be168c0dSopenharmony_ci+  if (status != napi_ok || array_length <= 0) {
4006be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get inputs tensor length failed.";
4007be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
4008be168c0dSopenharmony_ci+  }
4009be168c0dSopenharmony_ci+
4010be168c0dSopenharmony_ci+  if (model == nullptr) {
4011be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "model is nullptr";
4012be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
4013be168c0dSopenharmony_ci+  }
4014be168c0dSopenharmony_ci+
4015be168c0dSopenharmony_ci+  auto inputs = model->GetInputs();
4016be168c0dSopenharmony_ci+  if (inputs.size() != array_length) {
4017be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "array length not equal to model inputs size.";
4018be168c0dSopenharmony_ci+    return ERR_INVALID_PARAM;
4019be168c0dSopenharmony_ci+  }
4020be168c0dSopenharmony_ci+
4021be168c0dSopenharmony_ci+  for (size_t i = 0; i < array_length; i++) {
4022be168c0dSopenharmony_ci+    napi_value element = nullptr;
4023be168c0dSopenharmony_ci+    status = napi_get_element(env, argv, i, &element);
4024be168c0dSopenharmony_ci+    if (status != napi_ok) {
4025be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "can not get element";
4026be168c0dSopenharmony_ci+      return ERROR;
4027be168c0dSopenharmony_ci+    }
4028be168c0dSopenharmony_ci+
4029be168c0dSopenharmony_ci+    std::string property_name = "getData";
4030be168c0dSopenharmony_ci+    bool exist = false;
4031be168c0dSopenharmony_ci+    napi_value data_func = nullptr;
4032be168c0dSopenharmony_ci+
4033be168c0dSopenharmony_ci+    napi_status status = napi_has_named_property(env, element, property_name.c_str(), &exist);
4034be168c0dSopenharmony_ci+
4035be168c0dSopenharmony_ci+    if (status != napi_ok || !exist) {
4036be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "can not find " << property_name.c_str() << " property.";
4037be168c0dSopenharmony_ci+      return ERROR;
4038be168c0dSopenharmony_ci+    }
4039be168c0dSopenharmony_ci+
4040be168c0dSopenharmony_ci+    if (napi_get_named_property(env, element, property_name.c_str(), &data_func) != napi_ok) {
4041be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "get " << property_name.c_str() << " property fail.";
4042be168c0dSopenharmony_ci+      return ERROR;
4043be168c0dSopenharmony_ci+    }
4044be168c0dSopenharmony_ci+    void *js_data = nullptr;
4045be168c0dSopenharmony_ci+    size_t length = 0;
4046be168c0dSopenharmony_ci+    napi_value return_val;
4047be168c0dSopenharmony_ci+
4048be168c0dSopenharmony_ci+    status = napi_call_function(env, element, data_func, 0, nullptr, &return_val);
4049be168c0dSopenharmony_ci+    if (status != napi_ok || return_val == nullptr) {
4050be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "napi call function error.";
4051be168c0dSopenharmony_ci+      return ERROR;
4052be168c0dSopenharmony_ci+    }
4053be168c0dSopenharmony_ci+    status = napi_get_arraybuffer_info(env, return_val, &js_data, &length);
4054be168c0dSopenharmony_ci+    if (status != napi_ok || js_data == nullptr) {
4055be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "Get js data error.";
4056be168c0dSopenharmony_ci+      return ERROR;
4057be168c0dSopenharmony_ci+    }
4058be168c0dSopenharmony_ci+    if (inputs[i].DataSize() != length) {
4059be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "tensor size is: " << static_cast<int>(inputs[i].DataSize()) << ", but data length got "
4060be168c0dSopenharmony_ci+                    << static_cast<int>(length);
4061be168c0dSopenharmony_ci+      return ERROR;
4062be168c0dSopenharmony_ci+    }
4063be168c0dSopenharmony_ci+
4064be168c0dSopenharmony_ci+    auto tensor_data = inputs[i].MutableData();
4065be168c0dSopenharmony_ci+    if (tensor_data == nullptr) {
4066be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "malloc data for tensor failed.";
4067be168c0dSopenharmony_ci+      return ERROR;
4068be168c0dSopenharmony_ci+    }
4069be168c0dSopenharmony_ci+    memcpy(tensor_data, js_data, length);
4070be168c0dSopenharmony_ci+  }
4071be168c0dSopenharmony_ci+  return SUCCESS;
4072be168c0dSopenharmony_ci+}
4073be168c0dSopenharmony_ci+
4074be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::RunStep(napi_env env, napi_callback_info info) {
4075be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
4076be168c0dSopenharmony_ci+  bool result = false;
4077be168c0dSopenharmony_ci+  napi_status status = napi_get_boolean(env, result, &undefinedResult);
4078be168c0dSopenharmony_ci+  if (status != napi_ok) {
4079be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get bool error";
4080be168c0dSopenharmony_ci+    return undefinedResult;
4081be168c0dSopenharmony_ci+  }
4082be168c0dSopenharmony_ci+
4083be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
4084be168c0dSopenharmony_ci+  MSLiteModelNapi *modelNapi = nullptr;
4085be168c0dSopenharmony_ci+  size_t argCount = PARAM1;
4086be168c0dSopenharmony_ci+  napi_value argv[ARGS_ONE] = {0};
4087be168c0dSopenharmony_ci+
4088be168c0dSopenharmony_ci+  status = napi_get_cb_info(env, info, &argCount, argv, &jsThis, nullptr);
4089be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
4090be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "failed to retrieve details about the callback";
4091be168c0dSopenharmony_ci+    return undefinedResult;
4092be168c0dSopenharmony_ci+  }
4093be168c0dSopenharmony_ci+
4094be168c0dSopenharmony_ci+  if (argCount < ARGS_ONE) {
4095be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "argument num is less than one, please give input tensors";
4096be168c0dSopenharmony_ci+    return undefinedResult;
4097be168c0dSopenharmony_ci+  }
4098be168c0dSopenharmony_ci+
4099be168c0dSopenharmony_ci+  status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&modelNapi));
4100be168c0dSopenharmony_ci+  if (status != napi_ok || modelNapi == nullptr) {
4101be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get model napi error";
4102be168c0dSopenharmony_ci+    return undefinedResult;
4103be168c0dSopenharmony_ci+  }
4104be168c0dSopenharmony_ci+
4105be168c0dSopenharmony_ci+  if (SetModelInputs(env, argv[PARAM0], modelNapi->native_model_) != SUCCESS) {
4106be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "set tensor data failed";
4107be168c0dSopenharmony_ci+    return undefinedResult;
4108be168c0dSopenharmony_ci+  }
4109be168c0dSopenharmony_ci+
4110be168c0dSopenharmony_ci+  if (modelNapi->native_model_ == nullptr) {
4111be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "model is released(null), please create model again";
4112be168c0dSopenharmony_ci+    return undefinedResult;
4113be168c0dSopenharmony_ci+  }
4114be168c0dSopenharmony_ci+
4115be168c0dSopenharmony_ci+  auto ret = modelNapi->native_model_->RunStep();
4116be168c0dSopenharmony_ci+  if (ret != kSuccess) {
4117be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Model run step failed";
4118be168c0dSopenharmony_ci+    return undefinedResult;
4119be168c0dSopenharmony_ci+  }
4120be168c0dSopenharmony_ci+  status = napi_get_boolean(env, true, &undefinedResult);
4121be168c0dSopenharmony_ci+  if (status != napi_ok) {
4122be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "create bool true value failed";
4123be168c0dSopenharmony_ci+    return undefinedResult;
4124be168c0dSopenharmony_ci+  }
4125be168c0dSopenharmony_ci+  return undefinedResult;
4126be168c0dSopenharmony_ci+}
4127be168c0dSopenharmony_ci+
4128be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::UpdateWeights(napi_env env, napi_callback_info info) {
4129be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
4130be168c0dSopenharmony_ci+  bool result = false;
4131be168c0dSopenharmony_ci+  napi_status status = napi_get_boolean(env, result, &undefinedResult);
4132be168c0dSopenharmony_ci+  if (status != napi_ok) {
4133be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get bool error";
4134be168c0dSopenharmony_ci+    return undefinedResult;
4135be168c0dSopenharmony_ci+  }
4136be168c0dSopenharmony_ci+
4137be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
4138be168c0dSopenharmony_ci+  napi_value jsResult = nullptr;
4139be168c0dSopenharmony_ci+  MSLiteModelNapi *modelNapi = nullptr;
4140be168c0dSopenharmony_ci+  napi_value argv[ARGS_ONE] = {0};
4141be168c0dSopenharmony_ci+  size_t argCount = PARAM1;
4142be168c0dSopenharmony_ci+  status = napi_get_cb_info(env, info, &argCount, argv, &jsThis, nullptr);
4143be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
4144be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "failed to retrieve details about the callback";
4145be168c0dSopenharmony_ci+    return undefinedResult;
4146be168c0dSopenharmony_ci+  }
4147be168c0dSopenharmony_ci+  status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&modelNapi));
4148be168c0dSopenharmony_ci+  if (status != napi_ok || modelNapi == nullptr) {
4149be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get model napi error";
4150be168c0dSopenharmony_ci+    return undefinedResult;
4151be168c0dSopenharmony_ci+  }
4152be168c0dSopenharmony_ci+
4153be168c0dSopenharmony_ci+  if (modelNapi->native_model_ == nullptr) {
4154be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "model is released(null), please create model again";
4155be168c0dSopenharmony_ci+    return undefinedResult;
4156be168c0dSopenharmony_ci+  }
4157be168c0dSopenharmony_ci+
4158be168c0dSopenharmony_ci+  // set inputs data
4159be168c0dSopenharmony_ci+  uint32_t array_length = 0;
4160be168c0dSopenharmony_ci+  status = napi_get_array_length(env, argv[PARAM0], &array_length);
4161be168c0dSopenharmony_ci+  if (status != napi_ok || array_length <= 0) {
4162be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get inputs tensor length failed.";
4163be168c0dSopenharmony_ci+    return undefinedResult;
4164be168c0dSopenharmony_ci+  }
4165be168c0dSopenharmony_ci+
4166be168c0dSopenharmony_ci+  std::vector<MSTensor> weights;
4167be168c0dSopenharmony_ci+  for (size_t i = 0; i < array_length; i++) {
4168be168c0dSopenharmony_ci+    napi_value element = nullptr;
4169be168c0dSopenharmony_ci+    status = napi_get_element(env, argv[PARAM0], i, &element);
4170be168c0dSopenharmony_ci+    if (status != napi_ok) {
4171be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "can not get element";
4172be168c0dSopenharmony_ci+      return undefinedResult;
4173be168c0dSopenharmony_ci+    }
4174be168c0dSopenharmony_ci+
4175be168c0dSopenharmony_ci+    // get tensor name
4176be168c0dSopenharmony_ci+    std::string tensor_name;
4177be168c0dSopenharmony_ci+    auto ret = CommonNapi::GetPropertyString(env, element, "name", tensor_name);
4178be168c0dSopenharmony_ci+    if (ret != SUCCESS) {
4179be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "get tensor name property failed";
4180be168c0dSopenharmony_ci+      return undefinedResult;
4181be168c0dSopenharmony_ci+    }
4182be168c0dSopenharmony_ci+
4183be168c0dSopenharmony_ci+    // get tensor format
4184be168c0dSopenharmony_ci+    int format;
4185be168c0dSopenharmony_ci+    ret = CommonNapi::GetPropertyInt32(env, element, "format", format);
4186be168c0dSopenharmony_ci+    if (ret != SUCCESS) {
4187be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "get format property failed";
4188be168c0dSopenharmony_ci+      return undefinedResult;
4189be168c0dSopenharmony_ci+    }
4190be168c0dSopenharmony_ci+
4191be168c0dSopenharmony_ci+    // get dtype
4192be168c0dSopenharmony_ci+    int dtype;
4193be168c0dSopenharmony_ci+    ret = CommonNapi::GetPropertyInt32(env, element, "dtype", dtype);
4194be168c0dSopenharmony_ci+    if (ret != SUCCESS) {
4195be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "get format property failed";
4196be168c0dSopenharmony_ci+      return undefinedResult;
4197be168c0dSopenharmony_ci+    }
4198be168c0dSopenharmony_ci+
4199be168c0dSopenharmony_ci+    // get data size
4200be168c0dSopenharmony_ci+    int data_size;
4201be168c0dSopenharmony_ci+    ret = CommonNapi::GetPropertyInt32(env, element, "dataSize", data_size);
4202be168c0dSopenharmony_ci+    if (ret != SUCCESS) {
4203be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "get dataSize property failed";
4204be168c0dSopenharmony_ci+      return undefinedResult;
4205be168c0dSopenharmony_ci+    }
4206be168c0dSopenharmony_ci+
4207be168c0dSopenharmony_ci+    // get shape
4208be168c0dSopenharmony_ci+    std::vector<int32_t> shape;
4209be168c0dSopenharmony_ci+    ret = CommonNapi::GetPropertyInt32Array(env, element, "shape", shape);
4210be168c0dSopenharmony_ci+    if (ret != SUCCESS) {
4211be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "get shape property failed";
4212be168c0dSopenharmony_ci+      return undefinedResult;
4213be168c0dSopenharmony_ci+    }
4214be168c0dSopenharmony_ci+
4215be168c0dSopenharmony_ci+    // get data
4216be168c0dSopenharmony_ci+    std::string property_name = "getData";
4217be168c0dSopenharmony_ci+    bool exist = false;
4218be168c0dSopenharmony_ci+    napi_value data_func = nullptr;
4219be168c0dSopenharmony_ci+
4220be168c0dSopenharmony_ci+    status = napi_has_named_property(env, element, property_name.c_str(), &exist);
4221be168c0dSopenharmony_ci+    if (status != napi_ok || !exist) {
4222be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "can not find target property";
4223be168c0dSopenharmony_ci+      return undefinedResult;
4224be168c0dSopenharmony_ci+    }
4225be168c0dSopenharmony_ci+
4226be168c0dSopenharmony_ci+    if (napi_get_named_property(env, element, property_name.c_str(), &data_func) != napi_ok) {
4227be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "get " << property_name.c_str() << " property fail.";
4228be168c0dSopenharmony_ci+      return undefinedResult;
4229be168c0dSopenharmony_ci+    }
4230be168c0dSopenharmony_ci+    void *js_data = nullptr;
4231be168c0dSopenharmony_ci+    size_t length = 0;
4232be168c0dSopenharmony_ci+
4233be168c0dSopenharmony_ci+    napi_value return_val;
4234be168c0dSopenharmony_ci+    status = napi_call_function(env, element, data_func, 0, nullptr, &return_val);
4235be168c0dSopenharmony_ci+    if (status != napi_ok || return_val == nullptr) {
4236be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "napi call function error.";
4237be168c0dSopenharmony_ci+      return undefinedResult;
4238be168c0dSopenharmony_ci+    }
4239be168c0dSopenharmony_ci+
4240be168c0dSopenharmony_ci+    status = napi_get_arraybuffer_info(env, return_val, &js_data, &length);
4241be168c0dSopenharmony_ci+    if (status != napi_ok || js_data == nullptr) {
4242be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "get js data error.";
4243be168c0dSopenharmony_ci+      return undefinedResult;
4244be168c0dSopenharmony_ci+    }
4245be168c0dSopenharmony_ci+
4246be168c0dSopenharmony_ci+    std::vector<int64_t> int64_shape;
4247be168c0dSopenharmony_ci+    int64_shape.reserve(shape.size());
4248be168c0dSopenharmony_ci+    std::transform(shape.begin(), shape.end(), std::back_inserter(int64_shape), [](int32_t value) {
4249be168c0dSopenharmony_ci+      return static_cast<int64_t>(value);
4250be168c0dSopenharmony_ci+    });
4251be168c0dSopenharmony_ci+    auto tensor = mindspore::MSTensor::CreateTensor(tensor_name, static_cast<mindspore::DataType>(dtype), int64_shape, nullptr, 0);
4252be168c0dSopenharmony_ci+    if (tensor == nullptr) {
4253be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "create tensor failed.";
4254be168c0dSopenharmony_ci+      return undefinedResult;
4255be168c0dSopenharmony_ci+    }
4256be168c0dSopenharmony_ci+    tensor->SetFormat(static_cast<mindspore::Format>(format));
4257be168c0dSopenharmony_ci+    auto tensor_data = tensor->MutableData();
4258be168c0dSopenharmony_ci+    if (tensor_data == nullptr) {
4259be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "mutable tensor data failed, get nullptr";
4260be168c0dSopenharmony_ci+      return undefinedResult;
4261be168c0dSopenharmony_ci+    }
4262be168c0dSopenharmony_ci+
4263be168c0dSopenharmony_ci+    if (tensor->DataSize() != length) {
4264be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "tensor size is: " << static_cast<int>(tensor->DataSize()) << ", but data length got "
4265be168c0dSopenharmony_ci+                    << static_cast<int>(length);
4266be168c0dSopenharmony_ci+      return undefinedResult;
4267be168c0dSopenharmony_ci+    }
4268be168c0dSopenharmony_ci+
4269be168c0dSopenharmony_ci+    memcpy(tensor_data, js_data, length);
4270be168c0dSopenharmony_ci+    weights.push_back(*tensor);
4271be168c0dSopenharmony_ci+    delete tensor;
4272be168c0dSopenharmony_ci+  }
4273be168c0dSopenharmony_ci+
4274be168c0dSopenharmony_ci+  if (modelNapi->native_model_->UpdateFeatureMaps(weights) != mindspore::kSuccess) {
4275be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "UpdateFeatureMaps failed";
4276be168c0dSopenharmony_ci+    return undefinedResult;
4277be168c0dSopenharmony_ci+  }
4278be168c0dSopenharmony_ci+  status = napi_get_boolean(env, true, &jsResult);
4279be168c0dSopenharmony_ci+  if (status != napi_ok) {
4280be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get bool error";
4281be168c0dSopenharmony_ci+    return undefinedResult;
4282be168c0dSopenharmony_ci+  }
4283be168c0dSopenharmony_ci+  return jsResult;
4284be168c0dSopenharmony_ci+}
4285be168c0dSopenharmony_ci+
4286be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::ExportModel(napi_env env, napi_callback_info info) {
4287be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
4288be168c0dSopenharmony_ci+  bool result = false;
4289be168c0dSopenharmony_ci+  napi_status status = napi_get_boolean(env, result, &undefinedResult);
4290be168c0dSopenharmony_ci+  if (status != napi_ok) {
4291be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get bool error";
4292be168c0dSopenharmony_ci+    return undefinedResult;
4293be168c0dSopenharmony_ci+  }
4294be168c0dSopenharmony_ci+
4295be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
4296be168c0dSopenharmony_ci+  napi_value jsResult = nullptr;
4297be168c0dSopenharmony_ci+  MSLiteModelNapi *modelNapi = nullptr;
4298be168c0dSopenharmony_ci+  napi_value argv[ARGS_FOUR] = {0};
4299be168c0dSopenharmony_ci+  size_t argCount = PARAM4;
4300be168c0dSopenharmony_ci+  status = napi_get_cb_info(env, info, &argCount, argv, &jsThis, nullptr);
4301be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
4302be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "failed to retrieve details about the callback";
4303be168c0dSopenharmony_ci+    return undefinedResult;
4304be168c0dSopenharmony_ci+  }
4305be168c0dSopenharmony_ci+  status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&modelNapi));
4306be168c0dSopenharmony_ci+  if (status != napi_ok || modelNapi == nullptr) {
4307be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get model napi error";
4308be168c0dSopenharmony_ci+    return undefinedResult;
4309be168c0dSopenharmony_ci+  }
4310be168c0dSopenharmony_ci+
4311be168c0dSopenharmony_ci+  if (modelNapi->native_model_ == nullptr) {
4312be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "model is released(null), please create model again";
4313be168c0dSopenharmony_ci+    return undefinedResult;
4314be168c0dSopenharmony_ci+  }
4315be168c0dSopenharmony_ci+
4316be168c0dSopenharmony_ci+  // get modelfile
4317be168c0dSopenharmony_ci+  char char_buf[SIZE];
4318be168c0dSopenharmony_ci+  size_t buf_length = 0;
4319be168c0dSopenharmony_ci+  status = napi_get_value_string_utf8(env, argv[PARAM0], char_buf, SIZE, &buf_length);
4320be168c0dSopenharmony_ci+  if ((status != napi_ok) || (buf_length <= 0)) {
4321be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Parse model file failed.";
4322be168c0dSopenharmony_ci+    return undefinedResult;
4323be168c0dSopenharmony_ci+  }
4324be168c0dSopenharmony_ci+
4325be168c0dSopenharmony_ci+  std::string model_path;
4326be168c0dSopenharmony_ci+  model_path.assign(char_buf, char_buf + buf_length);
4327be168c0dSopenharmony_ci+  MS_LOG(DEBUG) << "model_path: " << model_path.c_str();
4328be168c0dSopenharmony_ci+
4329be168c0dSopenharmony_ci+  mindspore::QuantizationType quantization_type = kNoQuant;
4330be168c0dSopenharmony_ci+  int32_t quantization_type_value;
4331be168c0dSopenharmony_ci+  // get quantization
4332be168c0dSopenharmony_ci+  if (argCount >= ARGS_TWO) {
4333be168c0dSopenharmony_ci+    if (napi_get_value_int32(env, argv[PARAM1], &quantization_type_value) != napi_ok) {
4334be168c0dSopenharmony_ci+      MS_LOG(WARNING) << "fail to get int32_t value from quantizationType";
4335be168c0dSopenharmony_ci+      return undefinedResult;
4336be168c0dSopenharmony_ci+    }
4337be168c0dSopenharmony_ci+    quantization_type = static_cast<mindspore::QuantizationType>(quantization_type_value);
4338be168c0dSopenharmony_ci+  }
4339be168c0dSopenharmony_ci+
4340be168c0dSopenharmony_ci+  // get inference mode
4341be168c0dSopenharmony_ci+  bool export_inference_only = true;
4342be168c0dSopenharmony_ci+  if (argCount >= ARGS_THREE) {
4343be168c0dSopenharmony_ci+    if (napi_get_value_bool(env, argv[PARAM2], &export_inference_only) != napi_ok) {
4344be168c0dSopenharmony_ci+      MS_LOG(WARNING) << "fail to get bool value from exportInferenceOnly";
4345be168c0dSopenharmony_ci+      return undefinedResult;
4346be168c0dSopenharmony_ci+    }
4347be168c0dSopenharmony_ci+  }
4348be168c0dSopenharmony_ci+
4349be168c0dSopenharmony_ci+  // get output names
4350be168c0dSopenharmony_ci+  std::vector<std::string> output_tensor_name;
4351be168c0dSopenharmony_ci+  if (argCount >= ARGS_FOUR) {
4352be168c0dSopenharmony_ci+    auto ret = CommonNapi::GetStringArray(env, argv[PARAM3], output_tensor_name);
4353be168c0dSopenharmony_ci+    if (ret != SUCCESS) {
4354be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "Get context target failed.";
4355be168c0dSopenharmony_ci+      return undefinedResult;
4356be168c0dSopenharmony_ci+    }
4357be168c0dSopenharmony_ci+  }
4358be168c0dSopenharmony_ci+
4359be168c0dSopenharmony_ci+  auto ret = mindspore::Serialization::ExportModel(*(modelNapi->native_model_.get()), static_cast<mindspore::ModelType>(kMindIR),
4360be168c0dSopenharmony_ci+                                        model_path, static_cast<mindspore::QuantizationType>(quantization_type),
4361be168c0dSopenharmony_ci+                                        export_inference_only, output_tensor_name);
4362be168c0dSopenharmony_ci+  if (ret != mindspore::kSuccess) {
4363be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Export model failed";
4364be168c0dSopenharmony_ci+    return undefinedResult;
4365be168c0dSopenharmony_ci+  }
4366be168c0dSopenharmony_ci+
4367be168c0dSopenharmony_ci+  status = napi_get_boolean(env, true, &jsResult);
4368be168c0dSopenharmony_ci+  if (status != napi_ok) {
4369be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get bool error";
4370be168c0dSopenharmony_ci+    return undefinedResult;
4371be168c0dSopenharmony_ci+  }
4372be168c0dSopenharmony_ci+  MS_LOG(DEBUG) << "Export Model Success";
4373be168c0dSopenharmony_ci+  return jsResult;
4374be168c0dSopenharmony_ci+}
4375be168c0dSopenharmony_ci+
4376be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::ExportWeightsCollaborateWithMicro(napi_env env, napi_callback_info info) {
4377be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
4378be168c0dSopenharmony_ci+  bool result = false;
4379be168c0dSopenharmony_ci+  napi_status status = napi_get_boolean(env, result, &undefinedResult);
4380be168c0dSopenharmony_ci+  if (status != napi_ok) {
4381be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get bool error";
4382be168c0dSopenharmony_ci+    return undefinedResult;
4383be168c0dSopenharmony_ci+  }
4384be168c0dSopenharmony_ci+
4385be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
4386be168c0dSopenharmony_ci+  napi_value jsResult = nullptr;
4387be168c0dSopenharmony_ci+  MSLiteModelNapi *modelNapi = nullptr;
4388be168c0dSopenharmony_ci+  napi_value argv[ARGS_FOUR] = {0};
4389be168c0dSopenharmony_ci+  size_t argCount = PARAM4;
4390be168c0dSopenharmony_ci+  status = napi_get_cb_info(env, info, &argCount, argv, &jsThis, nullptr);
4391be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
4392be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "failed to retrieve details about the callback";
4393be168c0dSopenharmony_ci+    return undefinedResult;
4394be168c0dSopenharmony_ci+  }
4395be168c0dSopenharmony_ci+  status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&modelNapi));
4396be168c0dSopenharmony_ci+  if (status != napi_ok || modelNapi == nullptr) {
4397be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get model napi error";
4398be168c0dSopenharmony_ci+    return undefinedResult;
4399be168c0dSopenharmony_ci+  }
4400be168c0dSopenharmony_ci+
4401be168c0dSopenharmony_ci+  if (modelNapi->native_model_ == nullptr) {
4402be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "model is released(null), please create model again";
4403be168c0dSopenharmony_ci+    return undefinedResult;
4404be168c0dSopenharmony_ci+  }
4405be168c0dSopenharmony_ci+
4406be168c0dSopenharmony_ci+  // get weight file
4407be168c0dSopenharmony_ci+  char char_buf[SIZE];
4408be168c0dSopenharmony_ci+  size_t buf_length = 0;
4409be168c0dSopenharmony_ci+  status = napi_get_value_string_utf8(env, argv[PARAM0], char_buf, SIZE, &buf_length);
4410be168c0dSopenharmony_ci+  if ((status != napi_ok) || (buf_length <= 0)) {
4411be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Parse model file failed.";
4412be168c0dSopenharmony_ci+    return undefinedResult;
4413be168c0dSopenharmony_ci+  }
4414be168c0dSopenharmony_ci+
4415be168c0dSopenharmony_ci+  std::string weight_file;
4416be168c0dSopenharmony_ci+  weight_file.assign(char_buf, char_buf + buf_length);
4417be168c0dSopenharmony_ci+  MS_LOG(DEBUG) << "weight_file: " << weight_file.c_str();
4418be168c0dSopenharmony_ci+
4419be168c0dSopenharmony_ci+  // get is inference
4420be168c0dSopenharmony_ci+  bool is_inference = true;
4421be168c0dSopenharmony_ci+  if (argCount >= ARGS_TWO) {
4422be168c0dSopenharmony_ci+    if (napi_get_value_bool(env, argv[PARAM1], &is_inference) != napi_ok) {
4423be168c0dSopenharmony_ci+      MS_LOG(WARNING) << "fail to get bool value from isInference";
4424be168c0dSopenharmony_ci+      return undefinedResult;
4425be168c0dSopenharmony_ci+    }
4426be168c0dSopenharmony_ci+  }
4427be168c0dSopenharmony_ci+
4428be168c0dSopenharmony_ci+  // get inference mode
4429be168c0dSopenharmony_ci+  bool enable_fp16 = false;
4430be168c0dSopenharmony_ci+  if (argCount >= ARGS_THREE) {
4431be168c0dSopenharmony_ci+    if (napi_get_value_bool(env, argv[PARAM2], &enable_fp16) != napi_ok) {
4432be168c0dSopenharmony_ci+      MS_LOG(WARNING) << "fail to get bool value from enableFp16";
4433be168c0dSopenharmony_ci+      return undefinedResult;
4434be168c0dSopenharmony_ci+    }
4435be168c0dSopenharmony_ci+  }
4436be168c0dSopenharmony_ci+
4437be168c0dSopenharmony_ci+  // get output names
4438be168c0dSopenharmony_ci+  std::vector<std::string> changeable_weights_name;
4439be168c0dSopenharmony_ci+  if (argCount >= ARGS_FOUR) {
4440be168c0dSopenharmony_ci+    auto ret = CommonNapi::GetStringArray(env, argv[PARAM3], changeable_weights_name);
4441be168c0dSopenharmony_ci+    if (ret != SUCCESS) {
4442be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "failed to get string array from changeableWeightsName";
4443be168c0dSopenharmony_ci+      return undefinedResult;
4444be168c0dSopenharmony_ci+    }
4445be168c0dSopenharmony_ci+  }
4446be168c0dSopenharmony_ci+
4447be168c0dSopenharmony_ci+  auto ret = mindspore::Serialization::ExportWeightsCollaborateWithMicro(*(modelNapi->native_model_.get()), static_cast<mindspore::ModelType>(kMindIR),
4448be168c0dSopenharmony_ci+                                                              weight_file, is_inference, enable_fp16, changeable_weights_name);
4449be168c0dSopenharmony_ci+
4450be168c0dSopenharmony_ci+  if (ret != mindspore::kSuccess) {
4451be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "ExportWeightsCollaborateWithMicro failed";
4452be168c0dSopenharmony_ci+    return undefinedResult;
4453be168c0dSopenharmony_ci+  }
4454be168c0dSopenharmony_ci+
4455be168c0dSopenharmony_ci+  status = napi_get_boolean(env, true, &jsResult);
4456be168c0dSopenharmony_ci+  if (status != napi_ok) {
4457be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get bool error";
4458be168c0dSopenharmony_ci+    return undefinedResult;
4459be168c0dSopenharmony_ci+  }
4460be168c0dSopenharmony_ci+  MS_LOG(DEBUG) << "ExportWeightsCollaborateWithMicro Success";
4461be168c0dSopenharmony_ci+  return jsResult;
4462be168c0dSopenharmony_ci+}
4463be168c0dSopenharmony_ci+
4464be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::SetupVirtualBatch(napi_env env, napi_callback_info info) {
4465be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
4466be168c0dSopenharmony_ci+  bool result = false;
4467be168c0dSopenharmony_ci+  napi_status status = napi_get_boolean(env, result, &undefinedResult);
4468be168c0dSopenharmony_ci+  if (status != napi_ok) {
4469be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get bool error";
4470be168c0dSopenharmony_ci+    return undefinedResult;
4471be168c0dSopenharmony_ci+  }
4472be168c0dSopenharmony_ci+
4473be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
4474be168c0dSopenharmony_ci+  napi_value jsResult = nullptr;
4475be168c0dSopenharmony_ci+  MSLiteModelNapi *modelNapi = nullptr;
4476be168c0dSopenharmony_ci+  napi_value argv[ARGS_THREE] = {0};
4477be168c0dSopenharmony_ci+  size_t argCount = ARGS_THREE;
4478be168c0dSopenharmony_ci+  status = napi_get_cb_info(env, info, &argCount, argv, &jsThis, nullptr);
4479be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
4480be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "failed to retrieve details about the callback";
4481be168c0dSopenharmony_ci+    return undefinedResult;
4482be168c0dSopenharmony_ci+  }
4483be168c0dSopenharmony_ci+  status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&modelNapi));
4484be168c0dSopenharmony_ci+  if (status != napi_ok || modelNapi == nullptr) {
4485be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get model napi error";
4486be168c0dSopenharmony_ci+    return undefinedResult;
4487be168c0dSopenharmony_ci+  }
4488be168c0dSopenharmony_ci+
4489be168c0dSopenharmony_ci+  if (modelNapi->native_model_ == nullptr) {
4490be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "model is released(null), please create model again";
4491be168c0dSopenharmony_ci+    return undefinedResult;
4492be168c0dSopenharmony_ci+  }
4493be168c0dSopenharmony_ci+
4494be168c0dSopenharmony_ci+  // get virtual batch
4495be168c0dSopenharmony_ci+  int virtual_batch_multiplier;
4496be168c0dSopenharmony_ci+  if (napi_get_value_int32(env, argv[PARAM0], &virtual_batch_multiplier) != napi_ok) {
4497be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "fail to get int32 value from virtualBatchMultiplier";
4498be168c0dSopenharmony_ci+    return undefinedResult;
4499be168c0dSopenharmony_ci+  }
4500be168c0dSopenharmony_ci+
4501be168c0dSopenharmony_ci+  // get lr
4502be168c0dSopenharmony_ci+  double lr = -1.0f;
4503be168c0dSopenharmony_ci+  if (argCount >= ARGS_TWO) {
4504be168c0dSopenharmony_ci+    if (napi_get_value_double(env, argv[PARAM1], &lr) != napi_ok) {
4505be168c0dSopenharmony_ci+      MS_LOG(WARNING) << "fail to get double value from lr";
4506be168c0dSopenharmony_ci+      return undefinedResult;
4507be168c0dSopenharmony_ci+    }
4508be168c0dSopenharmony_ci+  }
4509be168c0dSopenharmony_ci+
4510be168c0dSopenharmony_ci+  // get lr
4511be168c0dSopenharmony_ci+  double momentum = -1.0f;
4512be168c0dSopenharmony_ci+  if (argCount >= ARGS_THREE) {
4513be168c0dSopenharmony_ci+    if (napi_get_value_double(env, argv[PARAM2], &momentum) != napi_ok) {
4514be168c0dSopenharmony_ci+      MS_LOG(WARNING) << "fail to get double value from momentum";
4515be168c0dSopenharmony_ci+      return undefinedResult;
4516be168c0dSopenharmony_ci+    }
4517be168c0dSopenharmony_ci+  }
4518be168c0dSopenharmony_ci+
4519be168c0dSopenharmony_ci+
4520be168c0dSopenharmony_ci+  auto ret = modelNapi->native_model_->SetupVirtualBatch(virtual_batch_multiplier, static_cast<float>(lr), static_cast<float>(momentum));
4521be168c0dSopenharmony_ci+
4522be168c0dSopenharmony_ci+  if (ret != mindspore::kSuccess) {
4523be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "SetupVirtualBatch failed";
4524be168c0dSopenharmony_ci+    return undefinedResult;
4525be168c0dSopenharmony_ci+  }
4526be168c0dSopenharmony_ci+
4527be168c0dSopenharmony_ci+  status = napi_get_boolean(env, true, &jsResult);
4528be168c0dSopenharmony_ci+  if (status != napi_ok) {
4529be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get bool error";
4530be168c0dSopenharmony_ci+    return undefinedResult;
4531be168c0dSopenharmony_ci+  }
4532be168c0dSopenharmony_ci+  return jsResult;
4533be168c0dSopenharmony_ci+}
4534be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::GetTrainMode(napi_env env, napi_callback_info info) {
4535be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
4536be168c0dSopenharmony_ci+
4537be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
4538be168c0dSopenharmony_ci+  napi_value jsResult = nullptr;
4539be168c0dSopenharmony_ci+  MSLiteModelNapi *modelNapi = nullptr;
4540be168c0dSopenharmony_ci+  napi_value argv[ARGS_ONE] = {0};
4541be168c0dSopenharmony_ci+  size_t argCount = ARGS_ONE;
4542be168c0dSopenharmony_ci+  auto status = napi_get_cb_info(env, info, &argCount, argv, &jsThis, nullptr);
4543be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
4544be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "failed to retrieve details about the callback";
4545be168c0dSopenharmony_ci+    return undefinedResult;
4546be168c0dSopenharmony_ci+  }
4547be168c0dSopenharmony_ci+  status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&modelNapi));
4548be168c0dSopenharmony_ci+  if (status != napi_ok || modelNapi == nullptr) {
4549be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get model napi error";
4550be168c0dSopenharmony_ci+    return undefinedResult;
4551be168c0dSopenharmony_ci+  }
4552be168c0dSopenharmony_ci+  if (modelNapi->native_model_ == nullptr) {
4553be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "model is released(null), please create model again";
4554be168c0dSopenharmony_ci+    return undefinedResult;
4555be168c0dSopenharmony_ci+  }
4556be168c0dSopenharmony_ci+
4557be168c0dSopenharmony_ci+  auto train_mode = modelNapi->native_model_->GetTrainMode();
4558be168c0dSopenharmony_ci+
4559be168c0dSopenharmony_ci+  status = napi_get_boolean(env, train_mode, &jsResult);
4560be168c0dSopenharmony_ci+  if (status != napi_ok) {
4561be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "create bool value error";
4562be168c0dSopenharmony_ci+    return undefinedResult;
4563be168c0dSopenharmony_ci+  }
4564be168c0dSopenharmony_ci+  return jsResult;
4565be168c0dSopenharmony_ci+}
4566be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::SetTrainMode(napi_env env, napi_callback_info info) {
4567be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
4568be168c0dSopenharmony_ci+
4569be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
4570be168c0dSopenharmony_ci+  napi_value jsResult = nullptr;
4571be168c0dSopenharmony_ci+  MSLiteModelNapi *modelNapi = nullptr;
4572be168c0dSopenharmony_ci+  napi_value argv[ARGS_ONE] = {0};
4573be168c0dSopenharmony_ci+  size_t argCount = ARGS_ONE;
4574be168c0dSopenharmony_ci+  auto status = napi_get_cb_info(env, info, &argCount, argv, &jsThis, nullptr);
4575be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
4576be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "failed to retrieve details about the callback";
4577be168c0dSopenharmony_ci+    return undefinedResult;
4578be168c0dSopenharmony_ci+  }
4579be168c0dSopenharmony_ci+  status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&modelNapi));
4580be168c0dSopenharmony_ci+  if (status != napi_ok || modelNapi == nullptr) {
4581be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get model napi error";
4582be168c0dSopenharmony_ci+    return undefinedResult;
4583be168c0dSopenharmony_ci+  }
4584be168c0dSopenharmony_ci+  if (modelNapi->native_model_ == nullptr) {
4585be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "model is released(null), please create model again";
4586be168c0dSopenharmony_ci+    return undefinedResult;
4587be168c0dSopenharmony_ci+  }
4588be168c0dSopenharmony_ci+
4589be168c0dSopenharmony_ci+  bool train_mode;
4590be168c0dSopenharmony_ci+  if (napi_get_value_bool(env, argv[PARAM0], &train_mode) != napi_ok) {
4591be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "failed to get bool value from input train mode.";
4592be168c0dSopenharmony_ci+    return undefinedResult;
4593be168c0dSopenharmony_ci+  }
4594be168c0dSopenharmony_ci+  if (!model_info_->train_model) {
4595be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "current model is not train model, unable to set train or eval mode";
4596be168c0dSopenharmony_ci+    return undefinedResult;
4597be168c0dSopenharmony_ci+  }
4598be168c0dSopenharmony_ci+  if (modelNapi->native_model_->SetTrainMode(train_mode) != kSuccess) {
4599be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "set train mode failed";
4600be168c0dSopenharmony_ci+    return undefinedResult;
4601be168c0dSopenharmony_ci+  }
4602be168c0dSopenharmony_ci+
4603be168c0dSopenharmony_ci+  status = napi_get_boolean(env, true, &jsResult);
4604be168c0dSopenharmony_ci+  if (status != napi_ok) {
4605be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "create bool value error";
4606be168c0dSopenharmony_ci+    return undefinedResult;
4607be168c0dSopenharmony_ci+  }
4608be168c0dSopenharmony_ci+  return jsResult;
4609be168c0dSopenharmony_ci+}
4610be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::GetLearningRate(napi_env env, napi_callback_info info) {
4611be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
4612be168c0dSopenharmony_ci+
4613be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
4614be168c0dSopenharmony_ci+  napi_value jsResult = nullptr;
4615be168c0dSopenharmony_ci+  MSLiteModelNapi *modelNapi = nullptr;
4616be168c0dSopenharmony_ci+  napi_value argv[ARGS_ONE] = {0};
4617be168c0dSopenharmony_ci+  size_t argCount = ARGS_ONE;
4618be168c0dSopenharmony_ci+  auto status = napi_get_cb_info(env, info, &argCount, argv, &jsThis, nullptr);
4619be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
4620be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "failed to retrieve details about the callback";
4621be168c0dSopenharmony_ci+    return undefinedResult;
4622be168c0dSopenharmony_ci+  }
4623be168c0dSopenharmony_ci+  status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&modelNapi));
4624be168c0dSopenharmony_ci+  if (status != napi_ok || modelNapi == nullptr) {
4625be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get model napi error";
4626be168c0dSopenharmony_ci+    return undefinedResult;
4627be168c0dSopenharmony_ci+  }
4628be168c0dSopenharmony_ci+  if (modelNapi->native_model_ == nullptr) {
4629be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "model is released(null), please create model again";
4630be168c0dSopenharmony_ci+    return undefinedResult;
4631be168c0dSopenharmony_ci+  }
4632be168c0dSopenharmony_ci+
4633be168c0dSopenharmony_ci+  auto lr = modelNapi->native_model_->GetLearningRate();
4634be168c0dSopenharmony_ci+
4635be168c0dSopenharmony_ci+  status = napi_create_double(env, lr, &jsResult);
4636be168c0dSopenharmony_ci+  if (status != napi_ok) {
4637be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "create double value error";
4638be168c0dSopenharmony_ci+    return undefinedResult;
4639be168c0dSopenharmony_ci+  }
4640be168c0dSopenharmony_ci+  return jsResult;
4641be168c0dSopenharmony_ci+}
4642be168c0dSopenharmony_ci+napi_value MSLiteModelNapi::SetLearningRate(napi_env env, napi_callback_info info) {
4643be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
4644be168c0dSopenharmony_ci+
4645be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
4646be168c0dSopenharmony_ci+  napi_value jsResult = nullptr;
4647be168c0dSopenharmony_ci+  MSLiteModelNapi *modelNapi = nullptr;
4648be168c0dSopenharmony_ci+  napi_value argv[ARGS_ONE] = {0};
4649be168c0dSopenharmony_ci+  size_t argCount = ARGS_ONE;
4650be168c0dSopenharmony_ci+  auto status = napi_get_cb_info(env, info, &argCount, argv, &jsThis, nullptr);
4651be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
4652be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "failed to retrieve details about the callback";
4653be168c0dSopenharmony_ci+    return undefinedResult;
4654be168c0dSopenharmony_ci+  }
4655be168c0dSopenharmony_ci+  status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&modelNapi));
4656be168c0dSopenharmony_ci+  if (status != napi_ok || modelNapi == nullptr) {
4657be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get model napi error";
4658be168c0dSopenharmony_ci+    return undefinedResult;
4659be168c0dSopenharmony_ci+  }
4660be168c0dSopenharmony_ci+  if (modelNapi->native_model_ == nullptr) {
4661be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "model is released(null), please create model again";
4662be168c0dSopenharmony_ci+    return undefinedResult;
4663be168c0dSopenharmony_ci+  }
4664be168c0dSopenharmony_ci+
4665be168c0dSopenharmony_ci+  if (!model_info_->train_model) {
4666be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "current model is not train model, unable to set learning rate";
4667be168c0dSopenharmony_ci+    return undefinedResult;
4668be168c0dSopenharmony_ci+  }
4669be168c0dSopenharmony_ci+
4670be168c0dSopenharmony_ci+  double lr;
4671be168c0dSopenharmony_ci+  if (napi_get_value_double(env, argv[PARAM0], &lr) != napi_ok) {
4672be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "failed to get double value.";
4673be168c0dSopenharmony_ci+    return undefinedResult;
4674be168c0dSopenharmony_ci+  }
4675be168c0dSopenharmony_ci+
4676be168c0dSopenharmony_ci+  if (modelNapi->native_model_->SetLearningRate(static_cast<float>(lr)) != kSuccess) {
4677be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "set learning rate failed";
4678be168c0dSopenharmony_ci+    return undefinedResult;
4679be168c0dSopenharmony_ci+  }
4680be168c0dSopenharmony_ci+
4681be168c0dSopenharmony_ci+  status = napi_get_boolean(env, true, &jsResult);
4682be168c0dSopenharmony_ci+  if (status != napi_ok) {
4683be168c0dSopenharmony_ci+    MS_LOG(WARNING) << "create bool value error";
4684be168c0dSopenharmony_ci+    return undefinedResult;
4685be168c0dSopenharmony_ci+  }
4686be168c0dSopenharmony_ci+  return jsResult;
4687be168c0dSopenharmony_ci+}
4688be168c0dSopenharmony_ci+}  // namespace mindspore
4689be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/litert/js_api/mstensor_napi.cc b/mindspore/lite/src/litert/js_api/mstensor_napi.cc
4690be168c0dSopenharmony_cinew file mode 100644
4691be168c0dSopenharmony_ciindex 00000000..975d3d94
4692be168c0dSopenharmony_ci--- /dev/null
4693be168c0dSopenharmony_ci+++ b/mindspore/lite/src/litert/js_api/mstensor_napi.cc
4694be168c0dSopenharmony_ci@@ -0,0 +1,416 @@
4695be168c0dSopenharmony_ci+/**
4696be168c0dSopenharmony_ci+ * Copyright 2023 Huawei Technologies Co., Ltd
4697be168c0dSopenharmony_ci+ *
4698be168c0dSopenharmony_ci+ * Licensed under the Apache License, Version 2.0 (the "License");
4699be168c0dSopenharmony_ci+ * you may not use this file except in compliance with the License.
4700be168c0dSopenharmony_ci+ * You may obtain a copy of the License at
4701be168c0dSopenharmony_ci+ *
4702be168c0dSopenharmony_ci+ * http://www.apache.org/licenses/LICENSE-2.0
4703be168c0dSopenharmony_ci+ *
4704be168c0dSopenharmony_ci+ * Unless required by applicable law or agreed to in writing, software
4705be168c0dSopenharmony_ci+ * distributed under the License is distributed on an "AS IS" BASIS,
4706be168c0dSopenharmony_ci+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4707be168c0dSopenharmony_ci+ * See the License for the specific language governing permissions and
4708be168c0dSopenharmony_ci+ * limitations under the License.
4709be168c0dSopenharmony_ci+ */
4710be168c0dSopenharmony_ci+
4711be168c0dSopenharmony_ci+#include "include/js_api/mstensor_napi.h"
4712be168c0dSopenharmony_ci+#include <climits>
4713be168c0dSopenharmony_ci+#include <string.h>
4714be168c0dSopenharmony_ci+#include <map>
4715be168c0dSopenharmony_ci+#include "src/common/log.h"
4716be168c0dSopenharmony_ci+
4717be168c0dSopenharmony_ci+namespace mindspore {
4718be168c0dSopenharmony_ci+thread_local napi_ref MSTensorNapi::constructor_ = nullptr;
4719be168c0dSopenharmony_ci+const std::string CLASS_NAME = "MSTensor";
4720be168c0dSopenharmony_ci+
4721be168c0dSopenharmony_ci+#define GET_PARAMS(env, info, num) \
4722be168c0dSopenharmony_ci+  size_t argc = num;               \
4723be168c0dSopenharmony_ci+  napi_value argv[num] = {0};      \
4724be168c0dSopenharmony_ci+  napi_value thisVar = nullptr;    \
4725be168c0dSopenharmony_ci+  void *data;                      \
4726be168c0dSopenharmony_ci+  napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)
4727be168c0dSopenharmony_ci+
4728be168c0dSopenharmony_ci+const std::unordered_map<std::string, napi_typedarray_type> kDTypeMap{
4729be168c0dSopenharmony_ci+  {"int32", napi_int32_array},
4730be168c0dSopenharmony_ci+  {"float32", napi_float32_array},
4731be168c0dSopenharmony_ci+  {"int8", napi_int8_array},
4732be168c0dSopenharmony_ci+  {"uint8", napi_uint8_array},
4733be168c0dSopenharmony_ci+};
4734be168c0dSopenharmony_ci+
4735be168c0dSopenharmony_ci+namespace {
4736be168c0dSopenharmony_ci+const int ARGS_TWO = 2;
4737be168c0dSopenharmony_ci+}
4738be168c0dSopenharmony_ci+
4739be168c0dSopenharmony_ci+MSTensorNapi::MSTensorNapi() { MS_LOG(DEBUG) << "MSLITE MSTensorNapi Instances create."; }
4740be168c0dSopenharmony_ci+
4741be168c0dSopenharmony_ci+MSTensorNapi::~MSTensorNapi() {
4742be168c0dSopenharmony_ci+  if (nativeMSTensor_ != nullptr) {
4743be168c0dSopenharmony_ci+    nativeMSTensor_ = nullptr;
4744be168c0dSopenharmony_ci+  }
4745be168c0dSopenharmony_ci+  MS_LOG(INFO) << "MSLITE MSTensorNapi Instances destroy.";
4746be168c0dSopenharmony_ci+}
4747be168c0dSopenharmony_ci+
4748be168c0dSopenharmony_ci+napi_value MSTensorNapi::Constructor(napi_env env, napi_callback_info info) {
4749be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
4750be168c0dSopenharmony_ci+  napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
4751be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
4752be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Failed to retrieve details about the callback";
4753be168c0dSopenharmony_ci+    return nullptr;
4754be168c0dSopenharmony_ci+  }
4755be168c0dSopenharmony_ci+
4756be168c0dSopenharmony_ci+  std::unique_ptr<MSTensorNapi> tensorNapi = std::make_unique<MSTensorNapi>();
4757be168c0dSopenharmony_ci+  if (tensorNapi == nullptr) {
4758be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "No memory";
4759be168c0dSopenharmony_ci+    return nullptr;
4760be168c0dSopenharmony_ci+  }
4761be168c0dSopenharmony_ci+
4762be168c0dSopenharmony_ci+  tensorNapi->env_ = env;
4763be168c0dSopenharmony_ci+  status = napi_wrap(env, jsThis, reinterpret_cast<void *>(tensorNapi.get()), MSTensorNapi::Finalize, nullptr, nullptr);
4764be168c0dSopenharmony_ci+  if (status == napi_ok) {
4765be168c0dSopenharmony_ci+    tensorNapi.release();
4766be168c0dSopenharmony_ci+    return jsThis;
4767be168c0dSopenharmony_ci+  }
4768be168c0dSopenharmony_ci+
4769be168c0dSopenharmony_ci+  MS_LOG(ERROR) << "Constructor fail.";
4770be168c0dSopenharmony_ci+  return nullptr;
4771be168c0dSopenharmony_ci+}
4772be168c0dSopenharmony_ci+
4773be168c0dSopenharmony_ci+void MSTensorNapi::Finalize(napi_env env, void *nativeObject, void *finalize) {
4774be168c0dSopenharmony_ci+  (void)env;
4775be168c0dSopenharmony_ci+  (void)finalize;
4776be168c0dSopenharmony_ci+  if (nativeObject != nullptr) {
4777be168c0dSopenharmony_ci+    delete reinterpret_cast<MSTensorNapi *>(nativeObject);
4778be168c0dSopenharmony_ci+  }
4779be168c0dSopenharmony_ci+  MS_LOG(INFO) << "Finalize success.";
4780be168c0dSopenharmony_ci+}
4781be168c0dSopenharmony_ci+
4782be168c0dSopenharmony_ci+napi_value MSTensorNapi::NewInstance(napi_env env, mindspore::MSTensor tensor) {
4783be168c0dSopenharmony_ci+  napi_value cons = GetConstructor(env);
4784be168c0dSopenharmony_ci+  if (cons == nullptr) {
4785be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "NewInstance GetConstructor is nullptr!";
4786be168c0dSopenharmony_ci+    return nullptr;
4787be168c0dSopenharmony_ci+  }
4788be168c0dSopenharmony_ci+  napi_value instance;
4789be168c0dSopenharmony_ci+  napi_status status = napi_new_instance(env, cons, 0, nullptr, &instance);
4790be168c0dSopenharmony_ci+  if (status != napi_ok) {
4791be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "NewInstance napi_new_instance failed! code: " << status;
4792be168c0dSopenharmony_ci+    return nullptr;
4793be168c0dSopenharmony_ci+  }
4794be168c0dSopenharmony_ci+
4795be168c0dSopenharmony_ci+  MSTensorNapi *proxy = nullptr;
4796be168c0dSopenharmony_ci+  status = napi_unwrap(env, instance, reinterpret_cast<void **>(&proxy));
4797be168c0dSopenharmony_ci+  if (proxy == nullptr) {
4798be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "NewInstance native instance is nullptr! code: " << status;
4799be168c0dSopenharmony_ci+    return instance;
4800be168c0dSopenharmony_ci+  }
4801be168c0dSopenharmony_ci+  // MSTensor 不需要new 内存,直接获取Model.getInputs()
4802be168c0dSopenharmony_ci+  proxy->nativeMSTensor_ = std::make_unique<mindspore::MSTensor>(tensor);
4803be168c0dSopenharmony_ci+  if (proxy->nativeMSTensor_ == nullptr) {
4804be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "NewInstance native tensor unique ptr is nullptr!";
4805be168c0dSopenharmony_ci+    return instance;
4806be168c0dSopenharmony_ci+  }
4807be168c0dSopenharmony_ci+  return instance;
4808be168c0dSopenharmony_ci+}
4809be168c0dSopenharmony_ci+
4810be168c0dSopenharmony_ci+napi_value MSTensorNapi::GetConstructor(napi_env env) {
4811be168c0dSopenharmony_ci+  napi_value cons;
4812be168c0dSopenharmony_ci+  if (constructor_ != nullptr) {
4813be168c0dSopenharmony_ci+    napi_get_reference_value(env, constructor_, &cons);
4814be168c0dSopenharmony_ci+    return cons;
4815be168c0dSopenharmony_ci+  }
4816be168c0dSopenharmony_ci+
4817be168c0dSopenharmony_ci+  MS_LOG(INFO) << "Get msTensorNapi constructor.";
4818be168c0dSopenharmony_ci+  napi_property_descriptor properties[] = {
4819be168c0dSopenharmony_ci+    DECLARE_NAPI_GETTER("name", GetName),
4820be168c0dSopenharmony_ci+    DECLARE_NAPI_GETTER("shape", GetShape),
4821be168c0dSopenharmony_ci+    DECLARE_NAPI_GETTER("elementNum", GetElementNum),
4822be168c0dSopenharmony_ci+    DECLARE_NAPI_GETTER("dtype", GetDtype),
4823be168c0dSopenharmony_ci+    DECLARE_NAPI_GETTER("format", GetFormat),
4824be168c0dSopenharmony_ci+    DECLARE_NAPI_GETTER("dataSize", GetDataSize),
4825be168c0dSopenharmony_ci+
4826be168c0dSopenharmony_ci+    DECLARE_NAPI_FUNCTION("getData", GetDataBuffer),
4827be168c0dSopenharmony_ci+    DECLARE_NAPI_FUNCTION("setData", SetData),
4828be168c0dSopenharmony_ci+  };
4829be168c0dSopenharmony_ci+
4830be168c0dSopenharmony_ci+  napi_status status = napi_define_class(env, CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Constructor, nullptr,
4831be168c0dSopenharmony_ci+                                         sizeof(properties) / sizeof(napi_property_descriptor), properties, &cons);
4832be168c0dSopenharmony_ci+  if (status != napi_ok) {
4833be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "MSLITE Failed to define MSTensor class";
4834be168c0dSopenharmony_ci+    return nullptr;
4835be168c0dSopenharmony_ci+  }
4836be168c0dSopenharmony_ci+
4837be168c0dSopenharmony_ci+  status = napi_create_reference(env, cons, 1, &constructor_);
4838be168c0dSopenharmony_ci+  if (status != napi_ok) {
4839be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "MSLITE Failed to create reference of constructor";
4840be168c0dSopenharmony_ci+    return nullptr;
4841be168c0dSopenharmony_ci+  }
4842be168c0dSopenharmony_ci+
4843be168c0dSopenharmony_ci+  return cons;
4844be168c0dSopenharmony_ci+}
4845be168c0dSopenharmony_ci+
4846be168c0dSopenharmony_ci+napi_value MSTensorNapi::GetName(napi_env env, napi_callback_info info) {
4847be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
4848be168c0dSopenharmony_ci+  napi_get_undefined(env, &undefinedResult);
4849be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
4850be168c0dSopenharmony_ci+  napi_value jsResult = nullptr;
4851be168c0dSopenharmony_ci+  MSTensorNapi *tensor = nullptr;
4852be168c0dSopenharmony_ci+
4853be168c0dSopenharmony_ci+  napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
4854be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
4855be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Failed to retrieve details about the callback";
4856be168c0dSopenharmony_ci+    return undefinedResult;
4857be168c0dSopenharmony_ci+  }
4858be168c0dSopenharmony_ci+
4859be168c0dSopenharmony_ci+  status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&tensor));
4860be168c0dSopenharmony_ci+  if (status != napi_ok || tensor == nullptr) {
4861be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get tensor napi error";
4862be168c0dSopenharmony_ci+    return undefinedResult;
4863be168c0dSopenharmony_ci+  }
4864be168c0dSopenharmony_ci+
4865be168c0dSopenharmony_ci+  status = napi_create_string_utf8(env, tensor->nativeMSTensor_->Name().c_str(), NAPI_AUTO_LENGTH, &jsResult);
4866be168c0dSopenharmony_ci+  if (status != napi_ok) {
4867be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "napi_create_string_utf8 error";
4868be168c0dSopenharmony_ci+    return undefinedResult;
4869be168c0dSopenharmony_ci+  }
4870be168c0dSopenharmony_ci+
4871be168c0dSopenharmony_ci+  MS_LOG(INFO) << "GetName success.";
4872be168c0dSopenharmony_ci+  return jsResult;
4873be168c0dSopenharmony_ci+}
4874be168c0dSopenharmony_ci+
4875be168c0dSopenharmony_ci+napi_value MSTensorNapi::GetShape(napi_env env, napi_callback_info info) {
4876be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
4877be168c0dSopenharmony_ci+  napi_get_undefined(env, &undefinedResult);
4878be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
4879be168c0dSopenharmony_ci+  napi_value jsResult = nullptr;
4880be168c0dSopenharmony_ci+  MSTensorNapi *tensor = nullptr;
4881be168c0dSopenharmony_ci+
4882be168c0dSopenharmony_ci+  napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
4883be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
4884be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Failed to retrieve details about the callback";
4885be168c0dSopenharmony_ci+    return undefinedResult;
4886be168c0dSopenharmony_ci+  }
4887be168c0dSopenharmony_ci+
4888be168c0dSopenharmony_ci+  status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&tensor));
4889be168c0dSopenharmony_ci+  if (status != napi_ok || tensor == nullptr) {
4890be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get tensor napi error";
4891be168c0dSopenharmony_ci+    return undefinedResult;
4892be168c0dSopenharmony_ci+  }
4893be168c0dSopenharmony_ci+
4894be168c0dSopenharmony_ci+  // return array
4895be168c0dSopenharmony_ci+  auto shape = tensor->nativeMSTensor_->Shape();
4896be168c0dSopenharmony_ci+  size_t size = shape.size();
4897be168c0dSopenharmony_ci+  napi_create_array_with_length(env, size, &jsResult);
4898be168c0dSopenharmony_ci+  for (size_t i = 0; i < size; i++) {
4899be168c0dSopenharmony_ci+    napi_value num;
4900be168c0dSopenharmony_ci+    status = napi_create_int32(env, shape.at(i), &num);
4901be168c0dSopenharmony_ci+    if (status != napi_ok) {
4902be168c0dSopenharmony_ci+      MS_LOG(ERROR) << "napi_create_int32 error";
4903be168c0dSopenharmony_ci+      return undefinedResult;
4904be168c0dSopenharmony_ci+    }
4905be168c0dSopenharmony_ci+    napi_set_element(env, jsResult, i, num);
4906be168c0dSopenharmony_ci+  }
4907be168c0dSopenharmony_ci+
4908be168c0dSopenharmony_ci+  MS_LOG(INFO) << "GetShape success.";
4909be168c0dSopenharmony_ci+  return jsResult;
4910be168c0dSopenharmony_ci+}
4911be168c0dSopenharmony_ci+
4912be168c0dSopenharmony_ci+napi_value MSTensorNapi::GetElementNum(napi_env env, napi_callback_info info) {
4913be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
4914be168c0dSopenharmony_ci+  napi_get_undefined(env, &undefinedResult);
4915be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
4916be168c0dSopenharmony_ci+  napi_value jsResult = nullptr;
4917be168c0dSopenharmony_ci+  MSTensorNapi *tensor = nullptr;
4918be168c0dSopenharmony_ci+
4919be168c0dSopenharmony_ci+  napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
4920be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
4921be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Failed to retrieve details about the callback";
4922be168c0dSopenharmony_ci+    return undefinedResult;
4923be168c0dSopenharmony_ci+  }
4924be168c0dSopenharmony_ci+
4925be168c0dSopenharmony_ci+  status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&tensor));
4926be168c0dSopenharmony_ci+  if (status != napi_ok || tensor == nullptr) {
4927be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get tensor napi error";
4928be168c0dSopenharmony_ci+    return undefinedResult;
4929be168c0dSopenharmony_ci+  }
4930be168c0dSopenharmony_ci+
4931be168c0dSopenharmony_ci+  status = napi_create_int32(env, tensor->nativeMSTensor_->ElementNum(), &jsResult);
4932be168c0dSopenharmony_ci+  if (status != napi_ok) {
4933be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "napi_create_int32 error";
4934be168c0dSopenharmony_ci+    return undefinedResult;
4935be168c0dSopenharmony_ci+  }
4936be168c0dSopenharmony_ci+
4937be168c0dSopenharmony_ci+  MS_LOG(INFO) << "GetElementNum success.";
4938be168c0dSopenharmony_ci+  return jsResult;
4939be168c0dSopenharmony_ci+}
4940be168c0dSopenharmony_ci+
4941be168c0dSopenharmony_ci+napi_value MSTensorNapi::GetDtype(napi_env env, napi_callback_info info) {
4942be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
4943be168c0dSopenharmony_ci+  napi_get_undefined(env, &undefinedResult);
4944be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
4945be168c0dSopenharmony_ci+  napi_value jsResult = nullptr;
4946be168c0dSopenharmony_ci+  MSTensorNapi *tensor = nullptr;
4947be168c0dSopenharmony_ci+
4948be168c0dSopenharmony_ci+  napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
4949be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
4950be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Failed to retrieve details about the callback";
4951be168c0dSopenharmony_ci+    return undefinedResult;
4952be168c0dSopenharmony_ci+  }
4953be168c0dSopenharmony_ci+
4954be168c0dSopenharmony_ci+  status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&tensor));
4955be168c0dSopenharmony_ci+  if (status != napi_ok || tensor == nullptr) {
4956be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get tensor napi error";
4957be168c0dSopenharmony_ci+    return undefinedResult;
4958be168c0dSopenharmony_ci+  }
4959be168c0dSopenharmony_ci+
4960be168c0dSopenharmony_ci+  status = napi_create_int32(env, static_cast<int32_t>(tensor->nativeMSTensor_->DataType()), &jsResult);
4961be168c0dSopenharmony_ci+  if (status != napi_ok) {
4962be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "napi_create_int32 error";
4963be168c0dSopenharmony_ci+    return undefinedResult;
4964be168c0dSopenharmony_ci+  }
4965be168c0dSopenharmony_ci+
4966be168c0dSopenharmony_ci+  MS_LOG(INFO) << "GetDtype success.";
4967be168c0dSopenharmony_ci+  return jsResult;
4968be168c0dSopenharmony_ci+}
4969be168c0dSopenharmony_ci+
4970be168c0dSopenharmony_ci+napi_value MSTensorNapi::GetFormat(napi_env env, napi_callback_info info) {
4971be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
4972be168c0dSopenharmony_ci+  napi_get_undefined(env, &undefinedResult);
4973be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
4974be168c0dSopenharmony_ci+  napi_value jsResult = nullptr;
4975be168c0dSopenharmony_ci+  MSTensorNapi *tensor = nullptr;
4976be168c0dSopenharmony_ci+
4977be168c0dSopenharmony_ci+  napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
4978be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
4979be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Failed to retrieve details about the callback";
4980be168c0dSopenharmony_ci+    return undefinedResult;
4981be168c0dSopenharmony_ci+  }
4982be168c0dSopenharmony_ci+
4983be168c0dSopenharmony_ci+  status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&tensor));
4984be168c0dSopenharmony_ci+  if (status != napi_ok || tensor == nullptr) {
4985be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get tensor napi error";
4986be168c0dSopenharmony_ci+    return undefinedResult;
4987be168c0dSopenharmony_ci+  }
4988be168c0dSopenharmony_ci+
4989be168c0dSopenharmony_ci+  status = napi_create_int32(env, static_cast<int32_t>(tensor->nativeMSTensor_->format()), &jsResult);
4990be168c0dSopenharmony_ci+  if (status != napi_ok) {
4991be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "napi_create_int32 error";
4992be168c0dSopenharmony_ci+    return undefinedResult;
4993be168c0dSopenharmony_ci+  }
4994be168c0dSopenharmony_ci+
4995be168c0dSopenharmony_ci+  MS_LOG(INFO) << "GetFormat success.";
4996be168c0dSopenharmony_ci+  return jsResult;
4997be168c0dSopenharmony_ci+}
4998be168c0dSopenharmony_ci+
4999be168c0dSopenharmony_ci+napi_value MSTensorNapi::GetDataSize(napi_env env, napi_callback_info info) {
5000be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
5001be168c0dSopenharmony_ci+  napi_get_undefined(env, &undefinedResult);
5002be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
5003be168c0dSopenharmony_ci+  napi_value jsResult = nullptr;
5004be168c0dSopenharmony_ci+  MSTensorNapi *tensor = nullptr;
5005be168c0dSopenharmony_ci+
5006be168c0dSopenharmony_ci+  napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
5007be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
5008be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Failed to retrieve details about the callback";
5009be168c0dSopenharmony_ci+    return undefinedResult;
5010be168c0dSopenharmony_ci+  }
5011be168c0dSopenharmony_ci+
5012be168c0dSopenharmony_ci+  status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&tensor));
5013be168c0dSopenharmony_ci+  if (status != napi_ok || tensor == nullptr) {
5014be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get tensor napi error";
5015be168c0dSopenharmony_ci+    return undefinedResult;
5016be168c0dSopenharmony_ci+  }
5017be168c0dSopenharmony_ci+
5018be168c0dSopenharmony_ci+  status = napi_create_int32(env, tensor->nativeMSTensor_->DataSize(), &jsResult);
5019be168c0dSopenharmony_ci+  if (status != napi_ok) {
5020be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "napi_create_int32 error";
5021be168c0dSopenharmony_ci+    return undefinedResult;
5022be168c0dSopenharmony_ci+  }
5023be168c0dSopenharmony_ci+
5024be168c0dSopenharmony_ci+  MS_LOG(INFO) << "GetDataSize success.";
5025be168c0dSopenharmony_ci+  return jsResult;
5026be168c0dSopenharmony_ci+}
5027be168c0dSopenharmony_ci+
5028be168c0dSopenharmony_ci+napi_value MSTensorNapi::GetDataBuffer(napi_env env, napi_callback_info info) {
5029be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
5030be168c0dSopenharmony_ci+  napi_get_undefined(env, &undefinedResult);
5031be168c0dSopenharmony_ci+
5032be168c0dSopenharmony_ci+  napi_value jsThis = nullptr;
5033be168c0dSopenharmony_ci+  napi_value jsResult = nullptr;
5034be168c0dSopenharmony_ci+  MSTensorNapi *tensor = nullptr;
5035be168c0dSopenharmony_ci+
5036be168c0dSopenharmony_ci+  napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
5037be168c0dSopenharmony_ci+  if (status != napi_ok || jsThis == nullptr) {
5038be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Failed to retrieve details about the callback";
5039be168c0dSopenharmony_ci+    return undefinedResult;
5040be168c0dSopenharmony_ci+  }
5041be168c0dSopenharmony_ci+
5042be168c0dSopenharmony_ci+  status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&tensor));
5043be168c0dSopenharmony_ci+  if (status != napi_ok || tensor == nullptr) {
5044be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get tensor napi error";
5045be168c0dSopenharmony_ci+    return undefinedResult;
5046be168c0dSopenharmony_ci+  }
5047be168c0dSopenharmony_ci+
5048be168c0dSopenharmony_ci+  size_t byte_length = tensor->nativeMSTensor_->DataSize();
5049be168c0dSopenharmony_ci+  auto tensor_data = tensor->nativeMSTensor_->MutableData();
5050be168c0dSopenharmony_ci+  if (tensor_data == nullptr) {
5051be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "tensor_data is null.";
5052be168c0dSopenharmony_ci+    return undefinedResult;
5053be168c0dSopenharmony_ci+  }
5054be168c0dSopenharmony_ci+
5055be168c0dSopenharmony_ci+  void *data = nullptr;
5056be168c0dSopenharmony_ci+  status = napi_create_arraybuffer(env, byte_length, &data, &jsResult);
5057be168c0dSopenharmony_ci+  if (status != napi_ok) {
5058be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "napi_create_arraybuffer error";
5059be168c0dSopenharmony_ci+    return undefinedResult;
5060be168c0dSopenharmony_ci+  }
5061be168c0dSopenharmony_ci+  if (data == nullptr || jsResult == nullptr) {
5062be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "napi_create_arraybuffer error";
5063be168c0dSopenharmony_ci+    return undefinedResult;
5064be168c0dSopenharmony_ci+  }
5065be168c0dSopenharmony_ci+
5066be168c0dSopenharmony_ci+  memcpy(data, tensor_data, byte_length);
5067be168c0dSopenharmony_ci+  MS_LOG(INFO) << "GetDataBuffer success.";
5068be168c0dSopenharmony_ci+  return jsResult;
5069be168c0dSopenharmony_ci+}
5070be168c0dSopenharmony_ci+
5071be168c0dSopenharmony_ci+napi_value MSTensorNapi::SetData(napi_env env, napi_callback_info info) {
5072be168c0dSopenharmony_ci+  napi_value undefinedResult = nullptr;
5073be168c0dSopenharmony_ci+  napi_get_undefined(env, &undefinedResult);
5074be168c0dSopenharmony_ci+  MSTensorNapi *tensor = nullptr;
5075be168c0dSopenharmony_ci+
5076be168c0dSopenharmony_ci+  GET_PARAMS(env, info, ARGS_TWO);
5077be168c0dSopenharmony_ci+
5078be168c0dSopenharmony_ci+  napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&tensor));
5079be168c0dSopenharmony_ci+  if (status != napi_ok || tensor == nullptr) {
5080be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "get tensor napi error";
5081be168c0dSopenharmony_ci+    return undefinedResult;
5082be168c0dSopenharmony_ci+  }
5083be168c0dSopenharmony_ci+
5084be168c0dSopenharmony_ci+  // convert napi_value to c++ type data
5085be168c0dSopenharmony_ci+  void *js_data = nullptr;
5086be168c0dSopenharmony_ci+  size_t length = 0;
5087be168c0dSopenharmony_ci+  status = napi_get_arraybuffer_info(env, argv[0], &js_data, &length);
5088be168c0dSopenharmony_ci+  if (status != napi_ok || js_data == nullptr) {
5089be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "Get js data error.";
5090be168c0dSopenharmony_ci+    return undefinedResult;
5091be168c0dSopenharmony_ci+  }
5092be168c0dSopenharmony_ci+
5093be168c0dSopenharmony_ci+  if (tensor->nativeMSTensor_->DataSize() != length) {
5094be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "tensor size is: " << static_cast<int>(tensor->nativeMSTensor_->DataSize())
5095be168c0dSopenharmony_ci+                  << "but data length got " << length;
5096be168c0dSopenharmony_ci+    return undefinedResult;
5097be168c0dSopenharmony_ci+  }
5098be168c0dSopenharmony_ci+
5099be168c0dSopenharmony_ci+  // memcpy
5100be168c0dSopenharmony_ci+  auto tensor_data = tensor->nativeMSTensor_->MutableData();
5101be168c0dSopenharmony_ci+  if (tensor_data == nullptr) {
5102be168c0dSopenharmony_ci+    MS_LOG(ERROR) << "malloc data for tensor failed.";
5103be168c0dSopenharmony_ci+    return undefinedResult;
5104be168c0dSopenharmony_ci+  }
5105be168c0dSopenharmony_ci+  memcpy(tensor_data, js_data, length);
5106be168c0dSopenharmony_ci+
5107be168c0dSopenharmony_ci+  MS_LOG(INFO) << "SetFloatData success.";
5108be168c0dSopenharmony_ci+  return undefinedResult;
5109be168c0dSopenharmony_ci+}
5110be168c0dSopenharmony_ci+}  // namespace mindspore
5111be168c0dSopenharmony_ci\ No newline at end of file
5112be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/litert/js_api/native_module_ohos_ms.cc b/mindspore/lite/src/litert/js_api/native_module_ohos_ms.cc
5113be168c0dSopenharmony_cinew file mode 100644
5114be168c0dSopenharmony_ciindex 00000000..7a381c1e
5115be168c0dSopenharmony_ci--- /dev/null
5116be168c0dSopenharmony_ci+++ b/mindspore/lite/src/litert/js_api/native_module_ohos_ms.cc
5117be168c0dSopenharmony_ci@@ -0,0 +1,48 @@
5118be168c0dSopenharmony_ci+/**
5119be168c0dSopenharmony_ci+ * Copyright 2023 Huawei Technologies Co., Ltd
5120be168c0dSopenharmony_ci+ *
5121be168c0dSopenharmony_ci+ * Licensed under the Apache License, Version 2.0 (the "License");
5122be168c0dSopenharmony_ci+ * you may not use this file except in compliance with the License.
5123be168c0dSopenharmony_ci+ * You may obtain a copy of the License at
5124be168c0dSopenharmony_ci+ *
5125be168c0dSopenharmony_ci+ * http://www.apache.org/licenses/LICENSE-2.0
5126be168c0dSopenharmony_ci+ *
5127be168c0dSopenharmony_ci+ * Unless required by applicable law or agreed to in writing, software
5128be168c0dSopenharmony_ci+ * distributed under the License is distributed on an "AS IS" BASIS,
5129be168c0dSopenharmony_ci+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5130be168c0dSopenharmony_ci+ * See the License for the specific language governing permissions and
5131be168c0dSopenharmony_ci+ * limitations under the License.
5132be168c0dSopenharmony_ci+ */
5133be168c0dSopenharmony_ci+
5134be168c0dSopenharmony_ci+#include "include/js_api/native_module_ohos_ms.h"
5135be168c0dSopenharmony_ci+#include "src/common/log_adapter.h"
5136be168c0dSopenharmony_ci+
5137be168c0dSopenharmony_ci+/*
5138be168c0dSopenharmony_ci+ * Function registering all props and functions of ohos.ai.mslite module
5139be168c0dSopenharmony_ci+ * which involves player and the recorder
5140be168c0dSopenharmony_ci+ */
5141be168c0dSopenharmony_ci+static napi_value Export(napi_env env, napi_value exports) {
5142be168c0dSopenharmony_ci+  MS_LOG(INFO) << "Export() is called.";
5143be168c0dSopenharmony_ci+
5144be168c0dSopenharmony_ci+  mindspore::MSLiteModelNapi::Init(env, exports);
5145be168c0dSopenharmony_ci+  return exports;
5146be168c0dSopenharmony_ci+}
5147be168c0dSopenharmony_ci+
5148be168c0dSopenharmony_ci+/*
5149be168c0dSopenharmony_ci+ * module define
5150be168c0dSopenharmony_ci+ */
5151be168c0dSopenharmony_ci+static napi_module g_module = {.nm_version = 1,
5152be168c0dSopenharmony_ci+                               .nm_flags = 0,
5153be168c0dSopenharmony_ci+                               .nm_filename = nullptr,
5154be168c0dSopenharmony_ci+                               .nm_register_func = Export,
5155be168c0dSopenharmony_ci+                               .nm_modname = "ai.mindSporeLite",
5156be168c0dSopenharmony_ci+                               .nm_priv = ((void *)0),
5157be168c0dSopenharmony_ci+                               .reserved = {0}};
5158be168c0dSopenharmony_ci+
5159be168c0dSopenharmony_ci+/*
5160be168c0dSopenharmony_ci+ * module register
5161be168c0dSopenharmony_ci+ */
5162be168c0dSopenharmony_ci+extern "C" __attribute__((constructor)) void RegisterModule(void) {
5163be168c0dSopenharmony_ci+  MS_LOG(INFO) << "RegisterModule() is called";
5164be168c0dSopenharmony_ci+  napi_module_register(&g_module);
5165be168c0dSopenharmony_ci+}
5166be168c0dSopenharmony_cidiff --git a/mindspore/lite/src/litert/js_api/nnrt_device_desc.cc b/mindspore/lite/src/litert/js_api/nnrt_device_desc.cc
5167be168c0dSopenharmony_cinew file mode 100644
5168be168c0dSopenharmony_ciindex 00000000..145897db
5169be168c0dSopenharmony_ci--- /dev/null
5170be168c0dSopenharmony_ci+++ b/mindspore/lite/src/litert/js_api/nnrt_device_desc.cc
5171be168c0dSopenharmony_ci@@ -0,0 +1,216 @@
5172be168c0dSopenharmony_ci+/**
5173be168c0dSopenharmony_ci+* Copyright 2023 Huawei Technologies Co., Ltd
5174be168c0dSopenharmony_ci+*
5175be168c0dSopenharmony_ci+* Licensed under the Apache License, Version 2.0 (the "License");
5176be168c0dSopenharmony_ci+* you may not use this file except in compliance with the License.
5177be168c0dSopenharmony_ci+* You may obtain a copy of the License at
5178be168c0dSopenharmony_ci+*
5179be168c0dSopenharmony_ci+* http://www.apache.org/licenses/LICENSE-2.0
5180be168c0dSopenharmony_ci+*
5181be168c0dSopenharmony_ci+* Unless required by applicable law or agreed to in writing, software
5182be168c0dSopenharmony_ci+* distributed under the License is distributed on an "AS IS" BASIS,
5183be168c0dSopenharmony_ci+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5184be168c0dSopenharmony_ci+* See the License for the specific language governing permissions and
5185be168c0dSopenharmony_ci+* limitations under the License.
5186be168c0dSopenharmony_ci+*/
5187be168c0dSopenharmony_ci+
5188be168c0dSopenharmony_ci+#include <climits>
5189be168c0dSopenharmony_ci+#include <string.h>
5190be168c0dSopenharmony_ci+#include <map>
5191be168c0dSopenharmony_ci+#include "src/common/log.h"
5192be168c0dSopenharmony_ci+#include "include/js_api/nnrt_device_desc_napi.h"
5193be168c0dSopenharmony_ci+
5194be168c0dSopenharmony_ci+namespace mindspore {
5195be168c0dSopenharmony_ci+thread_local napi_ref NnrtDeviceDescNapi::constructor_ = nullptr;
5196be168c0dSopenharmony_ci+const std::string CLASS_NAME = "NNRTDeviceDescription";
5197be168c0dSopenharmony_ci+
5198be168c0dSopenharmony_ci+NnrtDeviceDescNapi::NnrtDeviceDescNapi() { MS_LOG(DEBUG) << "MSLITE NNRTDeviceDescNapi Instances create."; }
5199be168c0dSopenharmony_ci+
5200be168c0dSopenharmony_ci+NnrtDeviceDescNapi::~NnrtDeviceDescNapi() {
5201be168c0dSopenharmony_ci+ if (nativeNnrtDeviceDesc_ != nullptr) {
5202be168c0dSopenharmony_ci+   nativeNnrtDeviceDesc_ = nullptr;
5203be168c0dSopenharmony_ci+ }
5204be168c0dSopenharmony_ci+ MS_LOG(INFO) << "MSLITE MSTensorNapi Instances destroy.";
5205be168c0dSopenharmony_ci+}
5206be168c0dSopenharmony_ci+
5207be168c0dSopenharmony_ci+napi_value NnrtDeviceDescNapi::Constructor(napi_env env, napi_callback_info info) {
5208be168c0dSopenharmony_ci+ napi_value jsThis = nullptr;
5209be168c0dSopenharmony_ci+ napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
5210be168c0dSopenharmony_ci+ if (status != napi_ok || jsThis == nullptr) {
5211be168c0dSopenharmony_ci+   MS_LOG(ERROR) << "Failed to retrieve details about the callback";
5212be168c0dSopenharmony_ci+   return nullptr;
5213be168c0dSopenharmony_ci+ }
5214be168c0dSopenharmony_ci+
5215be168c0dSopenharmony_ci+ std::unique_ptr<NnrtDeviceDescNapi> nnrt_device_desc = std::make_unique<NnrtDeviceDescNapi>();
5216be168c0dSopenharmony_ci+ if (nnrt_device_desc == nullptr) {
5217be168c0dSopenharmony_ci+   MS_LOG(ERROR) << "No memory";
5218be168c0dSopenharmony_ci+   return nullptr;
5219be168c0dSopenharmony_ci+ }
5220be168c0dSopenharmony_ci+
5221be168c0dSopenharmony_ci+ nnrt_device_desc->env_ = env;
5222be168c0dSopenharmony_ci+ status = napi_wrap(env, jsThis, reinterpret_cast<void *>(nnrt_device_desc.get()), NnrtDeviceDescNapi::Finalize, nullptr, nullptr);
5223be168c0dSopenharmony_ci+ if (status == napi_ok) {
5224be168c0dSopenharmony_ci+   nnrt_device_desc.release();
5225be168c0dSopenharmony_ci+   return jsThis;
5226be168c0dSopenharmony_ci+ }
5227be168c0dSopenharmony_ci+
5228be168c0dSopenharmony_ci+ MS_LOG(ERROR) << "Constructor fail.";
5229be168c0dSopenharmony_ci+ return nullptr;
5230be168c0dSopenharmony_ci+}
5231be168c0dSopenharmony_ci+
5232be168c0dSopenharmony_ci+void NnrtDeviceDescNapi::Finalize(napi_env env, void *nativeObject, void *finalize) {
5233be168c0dSopenharmony_ci+ (void)env;
5234be168c0dSopenharmony_ci+ (void)finalize;
5235be168c0dSopenharmony_ci+ if (nativeObject != nullptr) {
5236be168c0dSopenharmony_ci+   delete reinterpret_cast<NnrtDeviceDesc *>(nativeObject);
5237be168c0dSopenharmony_ci+ }
5238be168c0dSopenharmony_ci+ MS_LOG(INFO) << "Finalize success.";
5239be168c0dSopenharmony_ci+}
5240be168c0dSopenharmony_ci+
5241be168c0dSopenharmony_ci+napi_value NnrtDeviceDescNapi::NewInstance(napi_env env, NnrtDeviceDesc desc) {
5242be168c0dSopenharmony_ci+ napi_value cons = GetConstructor(env);
5243be168c0dSopenharmony_ci+ if (cons == nullptr) {
5244be168c0dSopenharmony_ci+   MS_LOG(ERROR) << "NewInstance GetConstructor is nullptr!";
5245be168c0dSopenharmony_ci+   return nullptr;
5246be168c0dSopenharmony_ci+ }
5247be168c0dSopenharmony_ci+ napi_value instance;
5248be168c0dSopenharmony_ci+ napi_status status = napi_new_instance(env, cons, 0, nullptr, &instance);
5249be168c0dSopenharmony_ci+ if (status != napi_ok) {
5250be168c0dSopenharmony_ci+   MS_LOG(ERROR) << "NewInstance napi_new_instance failed! code: " << status;
5251be168c0dSopenharmony_ci+   return nullptr;
5252be168c0dSopenharmony_ci+ }
5253be168c0dSopenharmony_ci+
5254be168c0dSopenharmony_ci+ NnrtDeviceDescNapi *proxy = nullptr;
5255be168c0dSopenharmony_ci+ status = napi_unwrap(env, instance, reinterpret_cast<void **>(&proxy));
5256be168c0dSopenharmony_ci+ if (proxy == nullptr) {
5257be168c0dSopenharmony_ci+   MS_LOG(ERROR) << "NewInstance native instance is nullptr! code: " << status;
5258be168c0dSopenharmony_ci+   return instance;
5259be168c0dSopenharmony_ci+ }
5260be168c0dSopenharmony_ci+
5261be168c0dSopenharmony_ci+ proxy->nativeNnrtDeviceDesc_ = std::make_unique<NnrtDeviceDesc>(desc);
5262be168c0dSopenharmony_ci+ if (proxy->nativeNnrtDeviceDesc_ == nullptr) {
5263be168c0dSopenharmony_ci+   MS_LOG(ERROR) << "NewInstance native nnrt deivce desc unique ptr is nullptr!";
5264be168c0dSopenharmony_ci+   return instance;
5265be168c0dSopenharmony_ci+ }
5266be168c0dSopenharmony_ci+ return instance;
5267be168c0dSopenharmony_ci+}
5268be168c0dSopenharmony_ci+
5269be168c0dSopenharmony_ci+napi_value NnrtDeviceDescNapi::GetConstructor(napi_env env) {
5270be168c0dSopenharmony_ci+ napi_value cons;
5271be168c0dSopenharmony_ci+ if (constructor_ != nullptr) {
5272be168c0dSopenharmony_ci+   napi_get_reference_value(env, constructor_, &cons);
5273be168c0dSopenharmony_ci+   return cons;
5274be168c0dSopenharmony_ci+ }
5275be168c0dSopenharmony_ci+
5276be168c0dSopenharmony_ci+ MS_LOG(INFO) << "Get NnrtDeviceDesc constructor.";
5277be168c0dSopenharmony_ci+ napi_property_descriptor properties[] = {
5278be168c0dSopenharmony_ci+   DECLARE_NAPI_FUNCTION("deviceID", GetDeviceID),
5279be168c0dSopenharmony_ci+   DECLARE_NAPI_FUNCTION("deviceType", GetDeviceType),
5280be168c0dSopenharmony_ci+   DECLARE_NAPI_FUNCTION("deviceName", GetDeviceName),
5281be168c0dSopenharmony_ci+ };
5282be168c0dSopenharmony_ci+
5283be168c0dSopenharmony_ci+ napi_status status = napi_define_class(env, CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Constructor, nullptr,
5284be168c0dSopenharmony_ci+                                        sizeof(properties) / sizeof(napi_property_descriptor), properties, &cons);
5285be168c0dSopenharmony_ci+ if (status != napi_ok) {
5286be168c0dSopenharmony_ci+   MS_LOG(ERROR) << "MSLITE Failed to define NnrtDeviceDesc class";
5287be168c0dSopenharmony_ci+   return nullptr;
5288be168c0dSopenharmony_ci+ }
5289be168c0dSopenharmony_ci+
5290be168c0dSopenharmony_ci+ status = napi_create_reference(env, cons, 1, &constructor_);
5291be168c0dSopenharmony_ci+ if (status != napi_ok) {
5292be168c0dSopenharmony_ci+   MS_LOG(ERROR) << "MSLITE Failed to create reference of constructor";
5293be168c0dSopenharmony_ci+   return nullptr;
5294be168c0dSopenharmony_ci+ }
5295be168c0dSopenharmony_ci+
5296be168c0dSopenharmony_ci+ return cons;
5297be168c0dSopenharmony_ci+}
5298be168c0dSopenharmony_ci+
5299be168c0dSopenharmony_ci+napi_value NnrtDeviceDescNapi::GetDeviceName(napi_env env, napi_callback_info info) {
5300be168c0dSopenharmony_ci+ napi_value undefinedResult = nullptr;
5301be168c0dSopenharmony_ci+ napi_get_undefined(env, &undefinedResult);
5302be168c0dSopenharmony_ci+ napi_value jsThis = nullptr;
5303be168c0dSopenharmony_ci+ napi_value jsResult = nullptr;
5304be168c0dSopenharmony_ci+ NnrtDeviceDescNapi *desc = nullptr;
5305be168c0dSopenharmony_ci+
5306be168c0dSopenharmony_ci+ napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
5307be168c0dSopenharmony_ci+ if (status != napi_ok || jsThis == nullptr) {
5308be168c0dSopenharmony_ci+   MS_LOG(ERROR) << "Failed to retrieve details about the callback";
5309be168c0dSopenharmony_ci+   return undefinedResult;
5310be168c0dSopenharmony_ci+ }
5311be168c0dSopenharmony_ci+
5312be168c0dSopenharmony_ci+ status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&desc));
5313be168c0dSopenharmony_ci+ if (status != napi_ok || desc == nullptr) {
5314be168c0dSopenharmony_ci+   MS_LOG(ERROR) << "get tensor napi error";
5315be168c0dSopenharmony_ci+   return undefinedResult;
5316be168c0dSopenharmony_ci+ }
5317be168c0dSopenharmony_ci+
5318be168c0dSopenharmony_ci+ status = napi_create_string_utf8(env, desc->nativeNnrtDeviceDesc_->name.c_str(), NAPI_AUTO_LENGTH, &jsResult);
5319be168c0dSopenharmony_ci+ if (status != napi_ok) {
5320be168c0dSopenharmony_ci+   MS_LOG(ERROR) << "napi_create_string_utf8 error";
5321be168c0dSopenharmony_ci+   return undefinedResult;
5322be168c0dSopenharmony_ci+ }
5323be168c0dSopenharmony_ci+
5324be168c0dSopenharmony_ci+ MS_LOG(INFO) << "GetName success.";
5325be168c0dSopenharmony_ci+ return jsResult;
5326be168c0dSopenharmony_ci+}
5327be168c0dSopenharmony_ci+
5328be168c0dSopenharmony_ci+napi_value NnrtDeviceDescNapi::GetDeviceID(napi_env env, napi_callback_info info) {
5329be168c0dSopenharmony_ci+ napi_value undefinedResult = nullptr;
5330be168c0dSopenharmony_ci+ napi_get_undefined(env, &undefinedResult);
5331be168c0dSopenharmony_ci+ napi_value jsThis = nullptr;
5332be168c0dSopenharmony_ci+ napi_value jsResult = nullptr;
5333be168c0dSopenharmony_ci+ NnrtDeviceDescNapi *desc = nullptr;
5334be168c0dSopenharmony_ci+
5335be168c0dSopenharmony_ci+ napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
5336be168c0dSopenharmony_ci+ if (status != napi_ok || jsThis == nullptr) {
5337be168c0dSopenharmony_ci+   MS_LOG(ERROR) << "Failed to retrieve details about the callback";
5338be168c0dSopenharmony_ci+   return undefinedResult;
5339be168c0dSopenharmony_ci+ }
5340be168c0dSopenharmony_ci+
5341be168c0dSopenharmony_ci+ status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&desc));
5342be168c0dSopenharmony_ci+ if (status != napi_ok || desc == nullptr) {
5343be168c0dSopenharmony_ci+   MS_LOG(ERROR) << "get tensor napi error";
5344be168c0dSopenharmony_ci+   return undefinedResult;
5345be168c0dSopenharmony_ci+ }
5346be168c0dSopenharmony_ci+
5347be168c0dSopenharmony_ci+ auto id = static_cast<uint64_t>(desc->nativeNnrtDeviceDesc_->id);
5348be168c0dSopenharmony_ci+ status = napi_create_bigint_uint64(env, id, &jsResult);
5349be168c0dSopenharmony_ci+ if (status != napi_ok) {
5350be168c0dSopenharmony_ci+   MS_LOG(ERROR) << "napi_create_int32 error";
5351be168c0dSopenharmony_ci+   return undefinedResult;
5352be168c0dSopenharmony_ci+ }
5353be168c0dSopenharmony_ci+
5354be168c0dSopenharmony_ci+ MS_LOG(INFO) << "GetShape success.";
5355be168c0dSopenharmony_ci+ return jsResult;
5356be168c0dSopenharmony_ci+}
5357be168c0dSopenharmony_ci+
5358be168c0dSopenharmony_ci+napi_value NnrtDeviceDescNapi::GetDeviceType(napi_env env, napi_callback_info info) {
5359be168c0dSopenharmony_ci+ napi_value undefinedResult = nullptr;
5360be168c0dSopenharmony_ci+ napi_get_undefined(env, &undefinedResult);
5361be168c0dSopenharmony_ci+ napi_value jsThis = nullptr;
5362be168c0dSopenharmony_ci+ napi_value jsResult = nullptr;
5363be168c0dSopenharmony_ci+ NnrtDeviceDescNapi *desc = nullptr;
5364be168c0dSopenharmony_ci+
5365be168c0dSopenharmony_ci+ napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);
5366be168c0dSopenharmony_ci+ if (status != napi_ok || jsThis == nullptr) {
5367be168c0dSopenharmony_ci+   MS_LOG(ERROR) << "Failed to retrieve details about the callback";
5368be168c0dSopenharmony_ci+   return undefinedResult;
5369be168c0dSopenharmony_ci+ }
5370be168c0dSopenharmony_ci+
5371be168c0dSopenharmony_ci+ status = napi_unwrap(env, jsThis, reinterpret_cast<void **>(&desc));
5372be168c0dSopenharmony_ci+ if (status != napi_ok || desc == nullptr) {
5373be168c0dSopenharmony_ci+   MS_LOG(ERROR) << "get nnrt device type napi error";
5374be168c0dSopenharmony_ci+   return undefinedResult;
5375be168c0dSopenharmony_ci+ }
5376be168c0dSopenharmony_ci+
5377be168c0dSopenharmony_ci+ status = napi_create_int32(env, desc->nativeNnrtDeviceDesc_->type, &jsResult);
5378be168c0dSopenharmony_ci+
5379be168c0dSopenharmony_ci+ if (status != napi_ok) {
5380be168c0dSopenharmony_ci+   MS_LOG(ERROR) << "napi_create_int32 error";
5381be168c0dSopenharmony_ci+   return undefinedResult;
5382be168c0dSopenharmony_ci+ }
5383be168c0dSopenharmony_ci+
5384be168c0dSopenharmony_ci+ MS_LOG(INFO) << "GetDeviceType success.";
5385be168c0dSopenharmony_ci+ return jsResult;
5386be168c0dSopenharmony_ci+}
5387be168c0dSopenharmony_ci+}  // namespace mindspore
5388be168c0dSopenharmony_ci\ No newline at end of file
5389be168c0dSopenharmony_ci-- 
5390be168c0dSopenharmony_ci2.25.1
5391be168c0dSopenharmony_ci
5392