1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "common/native_common.h"
17 #include "hilog/log.h"
18 #include "napi/native_api.h"
19 #include <cstring>
20 #include <js_native_api.h>
21 #include <neural_network_runtime/neural_network_runtime.h>
22 #include <node_api.h>
23 #include <string>
24
25 #define PARAM_0 0
26 #define PARAM_1 1
27 #define PARAM_2 2
28 #define PARAM_3 3
29 #define PARAM_4 4
30 #define MPARAM_1 (-1)
31 #define PARAM_100000 100000
32
33 #define SUCCESS 0
34 #define RETURN_1 1
35 #define FAIL (-1)
36
Free(OH_NNModel * model)37 void Free(OH_NNModel *model) { OH_NNModel_Destroy(&model); };
FreeCompilation(OH_NNCompilation * compilation)38 void FreeCompilation(OH_NNCompilation *compilation) { OH_NNCompilation_Destroy(&compilation); };
39 struct OHNNOperandTest {
40 OH_NN_DataType dataType;
41 OH_NN_TensorType type;
42 std::vector<int32_t> shape;
43 void *data{nullptr};
44 int32_t length{PARAM_0};
45 const OH_NN_QuantParam *quantParam = nullptr;
46 };
47 struct OHNNGraphArgs {
48 OH_NN_OperationType operationType;
49 std::vector<OHNNOperandTest> operands;
50 std::vector<uint32_t> paramIndices;
51 std::vector<uint32_t> inputIndices;
52 std::vector<uint32_t> outputIndices;
53 bool build = true;
54 bool specifyIO = true;
55 bool addOperation = true;
56 };
57
OHNNModelConstructOne(napi_env env,napi_callback_info info)58 static napi_value OHNNModelConstructOne(napi_env env, napi_callback_info info)
59 {
60 int ret = FAIL;
61 OH_NNModel *model = OH_NNModel_Construct();
62 if (model != nullptr) {
63 ret = SUCCESS;
64 }
65 napi_value result = nullptr;
66 napi_create_int32(env, ret, &result);
67
68 return result;
69 }
70
OHNNModelConstructTwo(napi_env env,napi_callback_info info)71 static napi_value OHNNModelConstructTwo(napi_env env, napi_callback_info info)
72 {
73 int ret = FAIL;
74 OH_NNModel *model_first = OH_NNModel_Construct();
75 OH_NNModel *model_second = OH_NNModel_Construct();
76 if ((model_first != nullptr) && (model_second != nullptr) && (model_first != model_second)) {
77 ret = SUCCESS;
78 }
79 Free(model_first);
80 Free(model_second);
81 napi_value result = nullptr;
82 napi_create_int32(env, ret, &result);
83
84 return result;
85 }
86
OHNNModelAddTensorOne(napi_env env,napi_callback_info info)87 static napi_value OHNNModelAddTensorOne(napi_env env, napi_callback_info info)
88 {
89 int ret = FAIL;
90 OH_NNModel *model = OH_NNModel_Construct();
91 if (model != nullptr) {
92 int32_t dimensions[PARAM_3]{PARAM_3, PARAM_2, PARAM_2};
93 OH_NN_Tensor operand{OH_NN_FLOAT32, PARAM_3, dimensions, nullptr, OH_NN_TENSOR};
94 OH_NN_ReturnCode addTensor_ret = OH_NNModel_AddTensor(model, &operand);
95 if (addTensor_ret == OH_NN_SUCCESS) {
96 ret = SUCCESS;
97 }
98 }
99 Free(model);
100 napi_value result = nullptr;
101 napi_create_int32(env, ret, &result);
102
103 return result;
104 }
OHNNModelAddTensorTwo(napi_env env,napi_callback_info info)105 static napi_value OHNNModelAddTensorTwo(napi_env env, napi_callback_info info)
106 {
107 int ret = FAIL;
108 int32_t dimensions[PARAM_3]{PARAM_3, PARAM_2, PARAM_2};
109 OH_NN_Tensor operand{OH_NN_FLOAT32, PARAM_3, dimensions, nullptr, OH_NN_TENSOR};
110 OH_NN_ReturnCode addTensor_ret = OH_NNModel_AddTensor(nullptr, &operand);
111 if (addTensor_ret == OH_NN_INVALID_PARAMETER) {
112 ret = SUCCESS;
113 }
114 napi_value result = nullptr;
115 napi_create_int32(env, ret, &result);
116
117 return result;
118 }
OHNNModelSetTensorDataOne(napi_env env,napi_callback_info info)119 static napi_value OHNNModelSetTensorDataOne(napi_env env, napi_callback_info info)
120 {
121 int ret = FAIL;
122 OH_NNModel *model = OH_NNModel_Construct();
123 if (model != nullptr) {
124
125 float inputValue0[PARAM_4] = {PARAM_0, PARAM_1, PARAM_2, PARAM_3};
126
127 OHNNOperandTest operand;
128 operand.dataType = OH_NN_FLOAT32;
129 operand.type = OH_NN_TENSOR;
130 operand.shape = {PARAM_1, PARAM_2, PARAM_2, PARAM_1};
131 operand.data = inputValue0;
132 operand.length = PARAM_4 * sizeof(float);
133 OH_NN_Tensor operandSor{OH_NN_FLOAT32, PARAM_4, operand.shape.data(), nullptr, OH_NN_TENSOR};
134 OH_NNModel_AddTensor(model, &operandSor);
135
136 OH_NN_ReturnCode setTensor_ret = OH_NNModel_SetTensorData(model, PARAM_0, operand.data, operand.length);
137 if (setTensor_ret == OH_NN_SUCCESS) {
138 ret = SUCCESS;
139 }
140 }
141 Free(model);
142 napi_value result = nullptr;
143 napi_create_int32(env, ret, &result);
144
145 return result;
146 }
OHNNModelSetTensorDataTwo(napi_env env,napi_callback_info info)147 static napi_value OHNNModelSetTensorDataTwo(napi_env env, napi_callback_info info)
148 {
149 int ret = FAIL;
150 int8_t activationValue{PARAM_0};
151 OH_NN_ReturnCode setTensor_ret =
152 OH_NNModel_SetTensorData(nullptr, PARAM_1, (void *)&activationValue, sizeof(int8_t));
153 if (setTensor_ret == OH_NN_INVALID_PARAMETER) {
154 ret = SUCCESS;
155 }
156 napi_value result = nullptr;
157 napi_create_int32(env, ret, &result);
158
159 return result;
160 }
161
addTesorAndSetTensor(OH_NNModel * model)162 OHNNGraphArgs addTesorAndSetTensor(OH_NNModel *model)
163 {
164 float inputValue[PARAM_4] = {PARAM_0, PARAM_1, PARAM_2, PARAM_3};
165 OHNNOperandTest operand;
166 operand.dataType = OH_NN_FLOAT32;
167 operand.type = OH_NN_TENSOR;
168 operand.shape = {PARAM_1, PARAM_2, PARAM_2, PARAM_1};
169 operand.data = inputValue;
170 operand.length = PARAM_4 * sizeof(float);
171
172 OHNNOperandTest operand2;
173 operand2.dataType = OH_NN_FLOAT32;
174 operand2.type = OH_NN_TENSOR;
175 operand2.shape = {PARAM_1, PARAM_2, PARAM_2, PARAM_1};
176 operand2.data = inputValue;
177 operand2.length = PARAM_4 * sizeof(float);
178
179 float outputValue[PARAM_4] = {PARAM_0};
180 int8_t activationValue = OH_NN_FUSED_NONE;
181 OHNNOperandTest activation = {OH_NN_INT8, OH_NN_ADD_ACTIVATIONTYPE, {}, &activationValue, sizeof(int8_t)};
182
183 OHNNOperandTest output = {
184 OH_NN_FLOAT32, OH_NN_TENSOR, {PARAM_1, PARAM_2, PARAM_2, PARAM_1}, outputValue, PARAM_4 * sizeof(float)};
185
186 OHNNGraphArgs graphArgs;
187 graphArgs.operationType = OH_NN_OPS_ADD;
188 graphArgs.operands = {operand, operand2, activation, output};
189 graphArgs.paramIndices = {PARAM_2};
190 graphArgs.inputIndices = {PARAM_0, PARAM_1};
191 graphArgs.outputIndices = {PARAM_3};
192
193 for (int i = 0; i < graphArgs.operands.size(); i++) {
194 const OHNNOperandTest &operandTem = graphArgs.operands[i];
195 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
196 nullptr, operandTem.type};
197 OH_NNModel_AddTensor(model, &operand);
198
199 if (std::find(graphArgs.paramIndices.begin(), graphArgs.paramIndices.end(), i) !=
200 graphArgs.paramIndices.end()) {
201 OH_NNModel_SetTensorData(model, i, operandTem.data, operandTem.length);
202 }
203 }
204 return graphArgs;
205 }
OHNNModelAddOperationOne(napi_env env,napi_callback_info info)206 static napi_value OHNNModelAddOperationOne(napi_env env, napi_callback_info info)
207 {
208 int ret = FAIL;
209 OH_NNModel *model = OH_NNModel_Construct();
210 if (model != nullptr) {
211 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
212
213 OH_NN_UInt32Array paramIndices;
214 paramIndices.data = graphArgs.paramIndices.data();
215 paramIndices.size = graphArgs.paramIndices.size();
216 OH_NN_UInt32Array inputIndices;
217 inputIndices.data = graphArgs.inputIndices.data();
218 inputIndices.size = graphArgs.inputIndices.size();
219 OH_NN_UInt32Array outputIndices;
220 outputIndices.data = graphArgs.outputIndices.data();
221 outputIndices.size = graphArgs.outputIndices.size();
222
223 OH_NN_ReturnCode addOperation_ret =
224 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
225 if (addOperation_ret == OH_NN_SUCCESS) {
226 ret = SUCCESS;
227 }
228 }
229 Free(model);
230 napi_value result = nullptr;
231 napi_create_int32(env, ret, &result);
232
233 return result;
234 }
OHNNModelAddOperationTwo(napi_env env,napi_callback_info info)235 static napi_value OHNNModelAddOperationTwo(napi_env env, napi_callback_info info)
236 {
237 int ret = FAIL;
238 OH_NN_ReturnCode addOperation_ret = OH_NNModel_AddOperation(nullptr, OH_NN_OPS_ADD, nullptr, nullptr, nullptr);
239 if (addOperation_ret == OH_NN_INVALID_PARAMETER) {
240 ret = SUCCESS;
241 }
242 napi_value result = nullptr;
243 napi_create_int32(env, ret, &result);
244
245 return result;
246 }
247
OHNNModelSpecifyInputsAndOutputsOne(napi_env env,napi_callback_info info)248 static napi_value OHNNModelSpecifyInputsAndOutputsOne(napi_env env, napi_callback_info info)
249 {
250 int ret = FAIL;
251 OH_NNModel *model = OH_NNModel_Construct();
252 if (model != nullptr) {
253 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
254
255 OH_NN_UInt32Array inputIndices;
256 inputIndices.data = graphArgs.inputIndices.data();
257 inputIndices.size = graphArgs.inputIndices.size();
258 OH_NN_UInt32Array outputIndices;
259 outputIndices.data = graphArgs.outputIndices.data();
260 outputIndices.size = graphArgs.outputIndices.size();
261
262 OH_NN_ReturnCode specifyInputsAndOutputs_ret =
263 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
264 if (specifyInputsAndOutputs_ret == OH_NN_SUCCESS) {
265 ret = SUCCESS;
266 }
267 }
268 Free(model);
269 napi_value result = nullptr;
270 napi_create_int32(env, ret, &result);
271
272 return result;
273 }
OHNNModelSpecifyInputsAndOutputsTwo(napi_env env,napi_callback_info info)274 static napi_value OHNNModelSpecifyInputsAndOutputsTwo(napi_env env, napi_callback_info info)
275 {
276 int ret = FAIL;
277 OH_NN_ReturnCode specifyInputsAndOutputs_ret = OH_NNModel_SpecifyInputsAndOutputs(nullptr, nullptr, nullptr);
278 if (specifyInputsAndOutputs_ret == OH_NN_INVALID_PARAMETER) {
279 ret = SUCCESS;
280 }
281 napi_value result = nullptr;
282 napi_create_int32(env, ret, &result);
283
284 return result;
285 }
OHNNModelFinishOne(napi_env env,napi_callback_info info)286 static napi_value OHNNModelFinishOne(napi_env env, napi_callback_info info)
287 {
288 int ret = FAIL;
289 OH_NNModel *model = OH_NNModel_Construct();
290 if (model != nullptr) {
291 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
292
293 OH_NN_UInt32Array paramIndices;
294 paramIndices.data = graphArgs.paramIndices.data();
295 paramIndices.size = graphArgs.paramIndices.size();
296 OH_NN_UInt32Array inputIndices;
297 inputIndices.data = graphArgs.inputIndices.data();
298 inputIndices.size = graphArgs.inputIndices.size();
299 OH_NN_UInt32Array outputIndices;
300 outputIndices.data = graphArgs.outputIndices.data();
301 outputIndices.size = graphArgs.outputIndices.size();
302
303 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
304 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
305
306 OH_NN_ReturnCode finish_ret = OH_NNModel_Finish(model);
307 if (finish_ret == OH_NN_SUCCESS) {
308 ret = SUCCESS;
309 }
310 }
311 Free(model);
312 napi_value result = nullptr;
313 napi_create_int32(env, ret, &result);
314
315 return result;
316 }
OHNNModelFinishTwo(napi_env env,napi_callback_info info)317 static napi_value OHNNModelFinishTwo(napi_env env, napi_callback_info info)
318 {
319 int ret = FAIL;
320 OH_NN_ReturnCode finish_ret = OH_NNModel_Finish(nullptr);
321 if (finish_ret == OH_NN_INVALID_PARAMETER) {
322 ret = SUCCESS;
323 }
324 napi_value result = nullptr;
325 napi_create_int32(env, ret, &result);
326
327 return result;
328 }
OHNNModelDestroy(napi_env env,napi_callback_info info)329 static napi_value OHNNModelDestroy(napi_env env, napi_callback_info info)
330 {
331 int ret = FAIL;
332 OH_NNModel *model = OH_NNModel_Construct();
333 if (model != nullptr) {
334 OH_NNModel_Destroy(&model);
335 if (model == nullptr) {
336 ret = SUCCESS;
337 }
338 }
339 napi_value result = nullptr;
340 napi_create_int32(env, ret, &result);
341
342 return result;
343 }
344
OHNNModelGetAvailableoperationsOne(napi_env env,napi_callback_info info)345 static napi_value OHNNModelGetAvailableoperationsOne(napi_env env, napi_callback_info info)
346 {
347 int ret = FAIL;
348 OH_NNModel *model = OH_NNModel_Construct();
349 if (model != nullptr) {
350
351 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
352
353 OH_NN_UInt32Array paramIndices;
354 paramIndices.data = graphArgs.paramIndices.data();
355 paramIndices.size = graphArgs.paramIndices.size();
356 OH_NN_UInt32Array inputIndices;
357 inputIndices.data = graphArgs.inputIndices.data();
358 inputIndices.size = graphArgs.inputIndices.size();
359 OH_NN_UInt32Array outputIndices;
360 outputIndices.data = graphArgs.outputIndices.data();
361 outputIndices.size = graphArgs.outputIndices.size();
362
363 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
364 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
365 OH_NNModel_Finish(model);
366
367 const bool *realSupported{nullptr};
368 uint32_t opCount;
369 uint32_t devicesCount{PARAM_0};
370 const size_t *devicesID = nullptr;
371 OH_NN_ReturnCode allDevicesId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
372 if (allDevicesId_ret == OH_NN_SUCCESS) {
373 if (devicesCount > PARAM_0) {
374 size_t targetDevice = devicesID[PARAM_0];
375 OH_NN_ReturnCode availableOperations_ret =
376 OH_NNModel_GetAvailableOperations(model, targetDevice, &realSupported, &opCount);
377 if (availableOperations_ret == OH_NN_SUCCESS) {
378 ret = SUCCESS;
379 }
380 } else if (devicesCount == PARAM_0) {
381 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
382 ret = SUCCESS;
383 }
384 }
385 }
386 Free(model);
387 napi_value result = nullptr;
388 napi_create_int32(env, ret, &result);
389
390 return result;
391 }
OHNNModelGetAvailableoperationsTwo(napi_env env,napi_callback_info info)392 static napi_value OHNNModelGetAvailableoperationsTwo(napi_env env, napi_callback_info info)
393 {
394 int ret = FAIL;
395 size_t targetDevice{PARAM_100000};
396 const bool *isSupported{nullptr};
397 uint32_t opCount{PARAM_0};
398 OH_NN_ReturnCode availableOperations_ret =
399 OH_NNModel_GetAvailableOperations(nullptr, targetDevice, &isSupported, &opCount);
400 if (availableOperations_ret == OH_NN_INVALID_PARAMETER) {
401 ret = SUCCESS;
402 }
403 napi_value result = nullptr;
404 napi_create_int32(env, ret, &result);
405
406 return result;
407 }
OHNNCompilationConstructOne(napi_env env,napi_callback_info info)408 static napi_value OHNNCompilationConstructOne(napi_env env, napi_callback_info info)
409 {
410 int ret = FAIL;
411 OH_NNModel *model = OH_NNModel_Construct();
412 if (model != nullptr) {
413 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
414
415 OH_NN_UInt32Array paramIndices;
416 paramIndices.data = graphArgs.paramIndices.data();
417 paramIndices.size = graphArgs.paramIndices.size();
418 OH_NN_UInt32Array inputIndices;
419 inputIndices.data = graphArgs.inputIndices.data();
420 inputIndices.size = graphArgs.inputIndices.size();
421 OH_NN_UInt32Array outputIndices;
422 outputIndices.data = graphArgs.outputIndices.data();
423 outputIndices.size = graphArgs.outputIndices.size();
424
425 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
426 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
427 OH_NN_ReturnCode finish_ret = OH_NNModel_Finish(model);
428 if (finish_ret == OH_NN_SUCCESS) {
429 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
430 if (compilation != nullptr) {
431 ret = SUCCESS;
432 }
433 FreeCompilation(compilation);
434 }
435 }
436 Free(model);
437 napi_value result = nullptr;
438 napi_create_int32(env, ret, &result);
439
440 return result;
441 }
OHNNCompilationConstructTwo(napi_env env,napi_callback_info info)442 static napi_value OHNNCompilationConstructTwo(napi_env env, napi_callback_info info)
443 {
444 int ret = FAIL;
445 OH_NNCompilation *compilation = OH_NNCompilation_Construct(nullptr);
446 if (compilation == nullptr) {
447 ret = SUCCESS;
448 }
449 napi_value result = nullptr;
450 napi_create_int32(env, ret, &result);
451
452 return result;
453 }
454
OHNNCompilationSetDeviceOne(napi_env env,napi_callback_info info)455 static napi_value OHNNCompilationSetDeviceOne(napi_env env, napi_callback_info info)
456 {
457 int ret = FAIL;
458 OH_NNModel *model = OH_NNModel_Construct();
459 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
460 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
461
462 OH_NN_UInt32Array paramIndices;
463 paramIndices.data = graphArgs.paramIndices.data();
464 paramIndices.size = graphArgs.paramIndices.size();
465 OH_NN_UInt32Array inputIndices;
466 inputIndices.data = graphArgs.inputIndices.data();
467 inputIndices.size = graphArgs.inputIndices.size();
468 OH_NN_UInt32Array outputIndices;
469 outputIndices.data = graphArgs.outputIndices.data();
470 outputIndices.size = graphArgs.outputIndices.size();
471
472 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
473 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
474 OH_NNModel_Finish(model);
475
476 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
477 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
478 const size_t *devicesID{nullptr};
479 uint32_t devicesCount{PARAM_0};
480 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
481 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
482 if (devicesCount > PARAM_0) {
483 const char *name = nullptr;
484 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
485 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetName Error");
486 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
487 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
488 ret = SUCCESS;
489 } else if (devicesCount == PARAM_0) {
490 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
491 ret = SUCCESS;
492 }
493 FreeCompilation(compilation);
494
495 napi_value result = nullptr;
496 napi_create_int32(env, ret, &result);
497
498 return result;
499 }
500
OHNNCompilationSetDeviceTwo(napi_env env,napi_callback_info info)501 static napi_value OHNNCompilationSetDeviceTwo(napi_env env, napi_callback_info info)
502 {
503 int ret = FAIL;
504 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(nullptr, PARAM_0);
505 if (setDevice_ret == OH_NN_INVALID_PARAMETER) {
506 ret = SUCCESS;
507 }
508 napi_value result = nullptr;
509 napi_create_int32(env, ret, &result);
510
511 return result;
512 }
513
OHNNCompilationSetCacheOne(napi_env env,napi_callback_info info)514 static napi_value OHNNCompilationSetCacheOne(napi_env env, napi_callback_info info)
515 {
516 int ret = FAIL;
517 OH_NNModel *model = OH_NNModel_Construct();
518 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
519 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
520 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
521 const size_t *devicesID{nullptr};
522 uint32_t devicesCount{PARAM_0};
523 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
524 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
525 if (devicesCount > PARAM_0) {
526 const char *name = nullptr;
527 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
528 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetAllDevicesID Error");
529 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
530 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
531 OH_NN_ReturnCode setCache_ret = OH_NNCompilation_SetCache(compilation, "./", PARAM_0);
532 NAPI_ASSERT(env, setCache_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetCache Error");
533 ret = SUCCESS;
534 } else if (devicesCount == PARAM_0) {
535 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
536 ret = SUCCESS;
537 }
538 FreeCompilation(compilation);
539 napi_value result = nullptr;
540 napi_create_int32(env, ret, &result);
541
542 return result;
543 }
OHNNCompilationSetCacheTwo(napi_env env,napi_callback_info info)544 static napi_value OHNNCompilationSetCacheTwo(napi_env env, napi_callback_info info)
545 {
546 int ret = FAIL;
547 OH_NN_ReturnCode setCache_ret = OH_NNCompilation_SetCache(nullptr, "./", PARAM_0);
548 if (setCache_ret == OH_NN_INVALID_PARAMETER) {
549 ret = SUCCESS;
550 }
551 napi_value result = nullptr;
552 napi_create_int32(env, ret, &result);
553
554 return result;
555 }
OHNNCompilationSetPerformanceModeOne(napi_env env,napi_callback_info info)556 static napi_value OHNNCompilationSetPerformanceModeOne(napi_env env, napi_callback_info info)
557 {
558 int ret = FAIL;
559 OH_NNModel *model = OH_NNModel_Construct();
560 NAPI_ASSERT(env, model != nullptr, "OH_NNCompilation_SetCache Error");
561 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
562 OH_NN_UInt32Array paramIndices;
563 paramIndices.data = graphArgs.paramIndices.data();
564 paramIndices.size = graphArgs.paramIndices.size();
565 OH_NN_UInt32Array inputIndices;
566 inputIndices.data = graphArgs.inputIndices.data();
567 inputIndices.size = graphArgs.inputIndices.size();
568 OH_NN_UInt32Array outputIndices;
569 outputIndices.data = graphArgs.outputIndices.data();
570 outputIndices.size = graphArgs.outputIndices.size();
571 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
572 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
573 OH_NNModel_Finish(model);
574 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
575 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
576 const size_t *devicesID{nullptr};
577 uint32_t devicesCount{PARAM_0};
578 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
579 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
580 if (devicesCount > PARAM_0) {
581 const char *name = nullptr;
582 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
583 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetName Error");
584 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
585 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
586 OH_NN_ReturnCode performanceMode_ret = OH_NNCompilation_SetPerformanceMode(compilation, OH_NN_PERFORMANCE_NONE);
587 if (performanceMode_ret == OH_NN_SUCCESS) {
588 ret = SUCCESS;
589 } else {
590 ret = performanceMode_ret;
591 }
592 } else if (devicesCount == PARAM_0) {
593 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
594 ret = SUCCESS;
595 }
596 FreeCompilation(compilation);
597 napi_value result = nullptr;
598 napi_create_int32(env, ret, &result);
599 return result;
600 }
OHNNCompilationSetPerformanceModeTwo(napi_env env,napi_callback_info info)601 static napi_value OHNNCompilationSetPerformanceModeTwo(napi_env env, napi_callback_info info)
602 {
603 int ret = FAIL;
604 OH_NNModel *model = OH_NNModel_Construct();
605 NAPI_ASSERT(env, model != nullptr, "OH_NNCompilation_SetDevice Error");
606 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
607
608 OH_NN_UInt32Array paramIndices;
609 paramIndices.data = graphArgs.paramIndices.data();
610 paramIndices.size = graphArgs.paramIndices.size();
611 OH_NN_UInt32Array inputIndices;
612 inputIndices.data = graphArgs.inputIndices.data();
613 inputIndices.size = graphArgs.inputIndices.size();
614 OH_NN_UInt32Array outputIndices;
615 outputIndices.data = graphArgs.outputIndices.data();
616 outputIndices.size = graphArgs.outputIndices.size();
617
618 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
619 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
620 OH_NNModel_Finish(model);
621
622 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
623 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
624 const size_t *devicesID{nullptr};
625 uint32_t devicesCount{PARAM_0};
626 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
627 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
628 if (devicesCount > PARAM_0) {
629 const char *name = nullptr;
630 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
631 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetName Error");
632 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
633 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
634 OH_NN_ReturnCode performanceMode_ret = OH_NNCompilation_SetPerformanceMode(compilation, OH_NN_PERFORMANCE_LOW);
635 if (performanceMode_ret == OH_NN_SUCCESS) {
636 ret = SUCCESS;
637 } else {
638 ret = performanceMode_ret;
639 }
640 } else if (devicesCount == PARAM_0) {
641 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
642 ret = SUCCESS;
643 }
644 FreeCompilation(compilation);
645 napi_value result = nullptr;
646 napi_create_int32(env, ret, &result);
647 return result;
648 }
OHNNCompilationSetPerformanceModeThree(napi_env env,napi_callback_info info)649 static napi_value OHNNCompilationSetPerformanceModeThree(napi_env env, napi_callback_info info)
650 {
651 int ret = FAIL;
652 OH_NNModel *model = OH_NNModel_Construct();
653 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
654 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
655 OH_NN_UInt32Array paramIndices;
656 paramIndices.data = graphArgs.paramIndices.data();
657 paramIndices.size = graphArgs.paramIndices.size();
658 OH_NN_UInt32Array inputIndices;
659 inputIndices.data = graphArgs.inputIndices.data();
660 inputIndices.size = graphArgs.inputIndices.size();
661 OH_NN_UInt32Array outputIndices;
662 outputIndices.data = graphArgs.outputIndices.data();
663 outputIndices.size = graphArgs.outputIndices.size();
664 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
665 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
666 OH_NNModel_Finish(model);
667 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
668 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
669 const size_t *devicesID{nullptr};
670 uint32_t devicesCount{PARAM_0};
671 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
672 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
673 if (devicesCount > PARAM_0) {
674 const char *name = nullptr;
675 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
676 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetName Error");
677 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
678 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
679 OH_NN_ReturnCode performanceMode_ret =
680 OH_NNCompilation_SetPerformanceMode(compilation, OH_NN_PERFORMANCE_MEDIUM);
681 if (performanceMode_ret == OH_NN_SUCCESS) {
682 ret = SUCCESS;
683 } else {
684 ret = performanceMode_ret;
685 }
686 } else if (devicesCount == PARAM_0) {
687 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
688 ret = SUCCESS;
689 }
690 FreeCompilation(compilation);
691 napi_value result = nullptr;
692 napi_create_int32(env, ret, &result);
693 return result;
694 }
OHNNCompilationSetPerformanceModeFour(napi_env env,napi_callback_info info)695 static napi_value OHNNCompilationSetPerformanceModeFour(napi_env env, napi_callback_info info)
696 {
697 int ret = FAIL;
698 OH_NN_ReturnCode performanceMode_ret = OH_NNCompilation_SetPerformanceMode(nullptr, OH_NN_PERFORMANCE_MEDIUM);
699 if (performanceMode_ret == OH_NN_INVALID_PARAMETER) {
700 ret = SUCCESS;
701 }
702 napi_value result = nullptr;
703 napi_create_int32(env, ret, &result);
704
705 return result;
706 }
OHNNCompilationSetPriorityOne(napi_env env,napi_callback_info info)707 static napi_value OHNNCompilationSetPriorityOne(napi_env env, napi_callback_info info)
708 {
709 int ret = FAIL;
710 OH_NNModel *model = OH_NNModel_Construct();
711 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
712 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
713
714 OH_NN_UInt32Array paramIndices;
715 paramIndices.data = graphArgs.paramIndices.data();
716 paramIndices.size = graphArgs.paramIndices.size();
717 OH_NN_UInt32Array inputIndices;
718 inputIndices.data = graphArgs.inputIndices.data();
719 inputIndices.size = graphArgs.inputIndices.size();
720 OH_NN_UInt32Array outputIndices;
721 outputIndices.data = graphArgs.outputIndices.data();
722 outputIndices.size = graphArgs.outputIndices.size();
723
724 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
725 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
726 OH_NNModel_Finish(model);
727
728 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
729 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
730
731 const size_t *devicesID{nullptr};
732 uint32_t devicesCount{PARAM_0};
733 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
734 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
735
736 if (devicesCount > PARAM_0) {
737 const char *name = nullptr;
738 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
739 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetName Error");
740 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
741 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
742 OH_NN_ReturnCode priority_ret = OH_NNCompilation_SetPriority(compilation, OH_NN_PRIORITY_NONE);
743 NAPI_ASSERT(env, priority_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetPriority Error");
744 ret = SUCCESS;
745 } else if (devicesCount == PARAM_0) {
746 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
747 ret = SUCCESS;
748 }
749 FreeCompilation(compilation);
750 napi_value result = nullptr;
751 napi_create_int32(env, ret, &result);
752 return result;
753 }
OHNNCompilationSetPriorityTwo(napi_env env,napi_callback_info info)754 static napi_value OHNNCompilationSetPriorityTwo(napi_env env, napi_callback_info info)
755 {
756 int ret = FAIL;
757 OH_NN_ReturnCode priority_ret = OH_NNCompilation_SetPriority(nullptr, OH_NN_PRIORITY_MEDIUM);
758 if (priority_ret == OH_NN_INVALID_PARAMETER) {
759 ret = SUCCESS;
760 }
761 napi_value result = nullptr;
762 napi_create_int32(env, ret, &result);
763
764 return result;
765 }
OHNNCompilationEnableFloat16One(napi_env env,napi_callback_info info)766 static napi_value OHNNCompilationEnableFloat16One(napi_env env, napi_callback_info info)
767 {
768 int ret = FAIL;
769 OH_NNModel *model = OH_NNModel_Construct();
770 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
771 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
772 OH_NN_UInt32Array paramIndices;
773 paramIndices.data = graphArgs.paramIndices.data();
774 paramIndices.size = graphArgs.paramIndices.size();
775 OH_NN_UInt32Array inputIndices;
776 inputIndices.data = graphArgs.inputIndices.data();
777 inputIndices.size = graphArgs.inputIndices.size();
778 OH_NN_UInt32Array outputIndices;
779 outputIndices.data = graphArgs.outputIndices.data();
780 outputIndices.size = graphArgs.outputIndices.size();
781 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
782 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
783 OH_NNModel_Finish(model);
784 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
785 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
786 const size_t *devicesID{nullptr};
787 uint32_t devicesCount{PARAM_0};
788 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
789 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
790 if (devicesCount > PARAM_0) {
791 const char *name = nullptr;
792 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
793 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetName Error");
794 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
795 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
796 OH_NN_ReturnCode priority_ret = OH_NNCompilation_EnableFloat16(compilation, false);
797 NAPI_ASSERT(env, priority_ret == OH_NN_SUCCESS, "OH_NNCompilation_EnableFloat16 Error");
798 ret = SUCCESS;
799 } else if (devicesCount == PARAM_0) {
800 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
801 ret = SUCCESS;
802 }
803 FreeCompilation(compilation);
804 napi_value result = nullptr;
805 napi_create_int32(env, ret, &result);
806 return result;
807 }
OHNNCompilationEnableFloat16Two(napi_env env,napi_callback_info info)808 static napi_value OHNNCompilationEnableFloat16Two(napi_env env, napi_callback_info info)
809 {
810 int ret = FAIL;
811 OH_NN_ReturnCode priority_ret = OH_NNCompilation_EnableFloat16(nullptr, OH_NN_PRIORITY_MEDIUM);
812 if (priority_ret == OH_NN_INVALID_PARAMETER) {
813 ret = SUCCESS;
814 }
815 napi_value result = nullptr;
816 napi_create_int32(env, ret, &result);
817
818 return result;
819 }
OHNNCompilationBuildOne(napi_env env,napi_callback_info info)820 static napi_value OHNNCompilationBuildOne(napi_env env, napi_callback_info info)
821 {
822 int ret = FAIL;
823 OH_NNModel *model = OH_NNModel_Construct();
824 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
825 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
826
827 OH_NN_UInt32Array paramIndices;
828 paramIndices.data = graphArgs.paramIndices.data();
829 paramIndices.size = graphArgs.paramIndices.size();
830 OH_NN_UInt32Array inputIndices;
831 inputIndices.data = graphArgs.inputIndices.data();
832 inputIndices.size = graphArgs.inputIndices.size();
833 OH_NN_UInt32Array outputIndices;
834 outputIndices.data = graphArgs.outputIndices.data();
835 outputIndices.size = graphArgs.outputIndices.size();
836
837 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
838 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
839 OH_NNModel_Finish(model);
840 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
841 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
842 const size_t *devicesID{nullptr};
843 uint32_t devicesCount{PARAM_0};
844 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
845 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
846 if (devicesCount > PARAM_0) {
847 const char *name = nullptr;
848 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
849 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetName Error");
850 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
851 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
852 OH_NN_ReturnCode build_ret = OH_NNCompilation_Build(compilation);
853 NAPI_ASSERT(env, build_ret == OH_NN_SUCCESS, "OH_NNCompilation_Build Error");
854 ret = SUCCESS;
855 } else if (devicesCount == PARAM_0) {
856 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
857 ret = SUCCESS;
858 }
859 FreeCompilation(compilation);
860 napi_value result = nullptr;
861 napi_create_int32(env, ret, &result);
862 return result;
863 }
OHNNCompilationBuildTwo(napi_env env,napi_callback_info info)864 static napi_value OHNNCompilationBuildTwo(napi_env env, napi_callback_info info)
865 {
866 int ret = FAIL;
867 OH_NN_ReturnCode build_ret = OH_NNCompilation_Build(nullptr);
868 if (build_ret == OH_NN_INVALID_PARAMETER) {
869 ret = SUCCESS;
870 }
871 napi_value result = nullptr;
872 napi_create_int32(env, ret, &result);
873
874 return result;
875 }
OHNNCompilationDestroy(napi_env env,napi_callback_info info)876 static napi_value OHNNCompilationDestroy(napi_env env, napi_callback_info info)
877 {
878 int ret = FAIL;
879 OH_NNModel *model = OH_NNModel_Construct();
880 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
881 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
882 OH_NN_UInt32Array paramIndices;
883 paramIndices.data = graphArgs.paramIndices.data();
884 paramIndices.size = graphArgs.paramIndices.size();
885 OH_NN_UInt32Array inputIndices;
886 inputIndices.data = graphArgs.inputIndices.data();
887 inputIndices.size = graphArgs.inputIndices.size();
888 OH_NN_UInt32Array outputIndices;
889 outputIndices.data = graphArgs.outputIndices.data();
890 outputIndices.size = graphArgs.outputIndices.size();
891 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
892 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
893 OH_NNModel_Finish(model);
894 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
895 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
896 OH_NNCompilation_Destroy(&compilation);
897 NAPI_ASSERT(env, compilation == nullptr, "OH_NNCompilation_Destroy Error");
898 ret = SUCCESS;
899 napi_value result = nullptr;
900 napi_create_int32(env, ret, &result);
901 return result;
902 }
OHNNExecutorConstructOne(napi_env env,napi_callback_info info)903 static napi_value OHNNExecutorConstructOne(napi_env env, napi_callback_info info)
904 {
905 int ret = FAIL;
906 OH_NNModel *model = OH_NNModel_Construct();
907 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
908 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
909 OH_NN_UInt32Array paramIndices;
910 paramIndices.data = graphArgs.paramIndices.data();
911 paramIndices.size = graphArgs.paramIndices.size();
912 OH_NN_UInt32Array inputIndices;
913 inputIndices.data = graphArgs.inputIndices.data();
914 inputIndices.size = graphArgs.inputIndices.size();
915 OH_NN_UInt32Array outputIndices;
916 outputIndices.data = graphArgs.outputIndices.data();
917 outputIndices.size = graphArgs.outputIndices.size();
918 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
919 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
920 OH_NNModel_Finish(model);
921 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
922 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
923 const size_t *devicesID{nullptr};
924 uint32_t devicesCount{PARAM_0};
925 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
926 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
927 if (devicesCount > PARAM_0) {
928 const char *name = nullptr;
929 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
930 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetName Error");
931 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
932 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
933 OH_NN_ReturnCode build_ret = OH_NNCompilation_Build(compilation);
934 NAPI_ASSERT(env, build_ret == OH_NN_SUCCESS, "OH_NNCompilation_Build Error");
935 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
936 NAPI_ASSERT(env, executor != nullptr, "OH_NNExecutor_Construct Error");
937 ret = SUCCESS;
938 } else if (devicesCount == PARAM_0) {
939 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
940 ret = SUCCESS;
941 }
942 FreeCompilation(compilation);
943 Free(model);
944 napi_value result = nullptr;
945 napi_create_int32(env, ret, &result);
946 return result;
947 }
OHNNExecutorConstructTwo(napi_env env,napi_callback_info info)948 static napi_value OHNNExecutorConstructTwo(napi_env env, napi_callback_info info)
949 {
950 int ret = FAIL;
951 OH_NNExecutor *executor = OH_NNExecutor_Construct(nullptr);
952 if (executor == nullptr) {
953 ret = SUCCESS;
954 }
955 napi_value result = nullptr;
956 napi_create_int32(env, ret, &result);
957
958 return result;
959 }
OHNNExecutorSetInputOne(napi_env env,napi_callback_info info)960 static napi_value OHNNExecutorSetInputOne(napi_env env, napi_callback_info info)
961 {
962 int ret = FAIL;
963 OH_NNModel *model = OH_NNModel_Construct();
964 NAPI_ASSERT(env, model != nullptr, "OH_NNExecutor_Construct Error");
965 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
966
967 OH_NN_UInt32Array paramIndices;
968 paramIndices.data = graphArgs.paramIndices.data();
969 paramIndices.size = graphArgs.paramIndices.size();
970 OH_NN_UInt32Array inputIndices;
971 inputIndices.data = graphArgs.inputIndices.data();
972 inputIndices.size = graphArgs.inputIndices.size();
973 OH_NN_UInt32Array outputIndices;
974 outputIndices.data = graphArgs.outputIndices.data();
975 outputIndices.size = graphArgs.outputIndices.size();
976
977 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
978 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
979 OH_NNModel_Finish(model);
980 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
981 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
982
983 const size_t *devicesID{nullptr};
984 uint32_t devicesCount{PARAM_0};
985 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
986 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
987 if (devicesCount > PARAM_0) {
988 const char *name = nullptr;
989 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
990 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetName Error");
991 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
992 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
993 OH_NN_ReturnCode build_ret = OH_NNCompilation_Build(compilation);
994 NAPI_ASSERT(env, build_ret == OH_NN_SUCCESS, "OH_NNCompilation_Build Error");
995 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
996 NAPI_ASSERT(env, executor != nullptr, "OH_NNExecutor_Construct Error");
997 uint32_t inputIndex = PARAM_0;
998 const OHNNOperandTest &operandTem = graphArgs.operands[PARAM_0];
999 auto quantParam = operandTem.quantParam;
1000 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
1001 quantParam, operandTem.type};
1002 OH_NN_ReturnCode setInput =
1003 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length);
1004 NAPI_ASSERT(env, setInput == OH_NN_SUCCESS, "OH_NNExecutor_SetInput Error");
1005 ret = SUCCESS;
1006 } else if (devicesCount == PARAM_0) {
1007 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
1008 ret = SUCCESS;
1009 }
1010 FreeCompilation(compilation);
1011 Free(model);
1012 napi_value result = nullptr;
1013 napi_create_int32(env, ret, &result);
1014 return result;
1015 }
OHNNExecutorSetInputTwo(napi_env env,napi_callback_info info)1016 static napi_value OHNNExecutorSetInputTwo(napi_env env, napi_callback_info info)
1017 {
1018 int ret = FAIL;
1019 OH_NN_ReturnCode setInput = OH_NNExecutor_SetInput(nullptr, PARAM_0, nullptr, nullptr, PARAM_0);
1020 if (setInput == OH_NN_INVALID_PARAMETER) {
1021 ret = SUCCESS;
1022 }
1023 napi_value result = nullptr;
1024 napi_create_int32(env, ret, &result);
1025
1026 return result;
1027 }
OHNNExecutorSetOutputOne(napi_env env,napi_callback_info info)1028 static napi_value OHNNExecutorSetOutputOne(napi_env env, napi_callback_info info)
1029 {
1030 int ret = FAIL;
1031 OH_NNModel *model = OH_NNModel_Construct();
1032 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
1033 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
1034 OH_NN_UInt32Array paramIndices;
1035 paramIndices.data = graphArgs.paramIndices.data();
1036 paramIndices.size = graphArgs.paramIndices.size();
1037 OH_NN_UInt32Array inputIndices;
1038 inputIndices.data = graphArgs.inputIndices.data();
1039 inputIndices.size = graphArgs.inputIndices.size();
1040 OH_NN_UInt32Array outputIndices;
1041 outputIndices.data = graphArgs.outputIndices.data();
1042 outputIndices.size = graphArgs.outputIndices.size();
1043 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
1044 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
1045 OH_NNModel_Finish(model);
1046 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
1047 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
1048 const size_t *devicesID{nullptr};
1049 uint32_t devicesCount{PARAM_0};
1050 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
1051 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
1052 if (devicesCount > PARAM_0) {
1053 const char *name = nullptr;
1054 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
1055 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetName Error");
1056 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
1057 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
1058 OH_NN_ReturnCode build_ret = OH_NNCompilation_Build(compilation);
1059 NAPI_ASSERT(env, build_ret == OH_NN_SUCCESS, "OH_NNCompilation_Build Error");
1060 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
1061 NAPI_ASSERT(env, executor != nullptr, "OH_NNExecutor_Construct Error");
1062 uint32_t inputIndex = PARAM_0;
1063 uint32_t outputIndex = PARAM_0;
1064 const OHNNOperandTest &operandTem = graphArgs.operands[PARAM_0];
1065 auto quantParam = operandTem.quantParam;
1066 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
1067 quantParam, operandTem.type};
1068 OH_NN_ReturnCode setInput_ret =
1069 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length);
1070 NAPI_ASSERT(env, setInput_ret == OH_NN_SUCCESS, "OH_NNExecutor_SetInput Error");
1071 inputIndex += 1;
1072 const OHNNOperandTest &operandOut = graphArgs.operands[PARAM_3];
1073 OH_NN_ReturnCode setOutput_ret =
1074 OH_NNExecutor_SetOutput(executor, outputIndex, operandOut.data, operandOut.length);
1075 NAPI_ASSERT(env, setOutput_ret == OH_NN_SUCCESS, "OH_NNExecutor_SetOutput Error");
1076 ret = SUCCESS;
1077 } else if (devicesCount == PARAM_0) {
1078 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
1079 ret = SUCCESS;
1080 }
1081 FreeCompilation(compilation);
1082 Free(model);
1083 napi_value result = nullptr;
1084 napi_create_int32(env, ret, &result);
1085
1086 return result;
1087 }
OHNNExecutorSetOutputTwo(napi_env env,napi_callback_info info)1088 static napi_value OHNNExecutorSetOutputTwo(napi_env env, napi_callback_info info)
1089 {
1090 int ret = FAIL;
1091 OH_NN_ReturnCode setOutput_ret = OH_NNExecutor_SetOutput(nullptr, PARAM_0, nullptr, PARAM_0);
1092 if (setOutput_ret == OH_NN_INVALID_PARAMETER) {
1093 ret = SUCCESS;
1094 }
1095 napi_value result = nullptr;
1096 napi_create_int32(env, ret, &result);
1097
1098 return result;
1099 }
OHNNExecutorGetOutputShapeOne(napi_env env,napi_callback_info info)1100 static napi_value OHNNExecutorGetOutputShapeOne(napi_env env, napi_callback_info info)
1101 {
1102 int ret = FAIL;
1103 OH_NNModel *model = OH_NNModel_Construct();
1104 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
1105 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
1106 OH_NN_UInt32Array paramIndices;
1107 paramIndices.data = graphArgs.paramIndices.data();
1108 paramIndices.size = graphArgs.paramIndices.size();
1109 OH_NN_UInt32Array inputIndices;
1110 inputIndices.data = graphArgs.inputIndices.data();
1111 inputIndices.size = graphArgs.inputIndices.size();
1112 OH_NN_UInt32Array outputIndices;
1113 outputIndices.data = graphArgs.outputIndices.data();
1114 outputIndices.size = graphArgs.outputIndices.size();
1115 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
1116 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
1117 OH_NNModel_Finish(model);
1118 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
1119 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
1120 const size_t *devicesID{nullptr};
1121 uint32_t devicesCount{PARAM_0};
1122 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
1123 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
1124 if (devicesCount > PARAM_0) {
1125 const char *name = nullptr;
1126 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
1127 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetName Error");
1128 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
1129 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
1130 OH_NN_ReturnCode build_ret = OH_NNCompilation_Build(compilation);
1131 NAPI_ASSERT(env, build_ret == OH_NN_SUCCESS, "OH_NNCompilation_Build Error");
1132 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
1133 uint32_t inputIndex = PARAM_0;
1134 uint32_t outputIndex = PARAM_0;
1135 for (auto i = PARAM_0; i < graphArgs.operands.size(); i++) {
1136 const OHNNOperandTest &operandTem = graphArgs.operands[i];
1137 auto quantParam = operandTem.quantParam;
1138 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
1139 quantParam, operandTem.type};
1140 if (std::find(graphArgs.inputIndices.begin(), graphArgs.inputIndices.end(), i) !=
1141 graphArgs.inputIndices.end()) {
1142 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length);
1143 inputIndex += 1;
1144 } else if (std::find(graphArgs.outputIndices.begin(), graphArgs.outputIndices.end(), i) !=
1145 graphArgs.outputIndices.end()) {
1146 OH_NNExecutor_SetOutput(executor, outputIndex, operandTem.data, operandTem.length);
1147 outputIndex += 1;
1148 }
1149 }
1150 OH_NN_ReturnCode run_ret = OH_NNExecutor_Run(executor);
1151 NAPI_ASSERT(env, run_ret == OH_NN_SUCCESS, "OH_NNExecutor_Run Error");
1152 int32_t *outputDimensions = nullptr;
1153 uint32_t outputDimensionCount{PARAM_0};
1154 uint32_t addOutputIndex = {PARAM_0};
1155 OH_NN_ReturnCode getOutput_ret =
1156 OH_NNExecutor_GetOutputShape(executor, addOutputIndex, &outputDimensions, &outputDimensionCount);
1157 NAPI_ASSERT(env, getOutput_ret == OH_NN_SUCCESS, "OH_NNExecutor_GetOutputShape Error");
1158 ret = SUCCESS;
1159 } else if (devicesCount == PARAM_0) {
1160 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
1161 ret = SUCCESS;
1162 }
1163 FreeCompilation(compilation);
1164 Free(model);
1165 napi_value result = nullptr;
1166 napi_create_int32(env, ret, &result);
1167 return result;
1168 }
OHNNExecutorGetOutputShapeTwo(napi_env env,napi_callback_info info)1169 static napi_value OHNNExecutorGetOutputShapeTwo(napi_env env, napi_callback_info info)
1170 {
1171 int ret = FAIL;
1172 int32_t *outputDimensions = nullptr;
1173 uint32_t outputDimensionCount{PARAM_0};
1174 uint32_t addOutputIndex = {PARAM_0};
1175 OH_NN_ReturnCode getOutput_ret =
1176 OH_NNExecutor_GetOutputShape(nullptr, addOutputIndex, &outputDimensions, &outputDimensionCount);
1177 if (getOutput_ret == OH_NN_INVALID_PARAMETER) {
1178 ret = SUCCESS;
1179 }
1180 napi_value result = nullptr;
1181 napi_create_int32(env, ret, &result);
1182
1183 return result;
1184 }
OHNNExecutorRunOne(napi_env env,napi_callback_info info)1185 static napi_value OHNNExecutorRunOne(napi_env env, napi_callback_info info)
1186 {
1187 int ret = FAIL;
1188 OH_NNModel *model = OH_NNModel_Construct();
1189 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
1190 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
1191 OH_NN_UInt32Array paramIndices;
1192 paramIndices.data = graphArgs.paramIndices.data();
1193 paramIndices.size = graphArgs.paramIndices.size();
1194 OH_NN_UInt32Array inputIndices;
1195 inputIndices.data = graphArgs.inputIndices.data();
1196 inputIndices.size = graphArgs.inputIndices.size();
1197 OH_NN_UInt32Array outputIndices;
1198 outputIndices.data = graphArgs.outputIndices.data();
1199 outputIndices.size = graphArgs.outputIndices.size();
1200 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
1201 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
1202 OH_NNModel_Finish(model);
1203 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
1204 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
1205 const size_t *devicesID{nullptr};
1206 uint32_t devicesCount{PARAM_0};
1207 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
1208 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
1209 if (devicesCount > PARAM_0) {
1210 const char *name = nullptr;
1211 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
1212 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetName Error");
1213 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
1214 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
1215 OH_NN_ReturnCode build_ret = OH_NNCompilation_Build(compilation);
1216 NAPI_ASSERT(env, build_ret == OH_NN_SUCCESS, "OH_NNCompilation_Build Error");
1217 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
1218 uint32_t inputIndex = PARAM_0;
1219 uint32_t outputIndex = PARAM_0;
1220 for (auto i = 0; i < graphArgs.operands.size(); i++) {
1221 const OHNNOperandTest &operandTem = graphArgs.operands[i];
1222 auto quantParam = operandTem.quantParam;
1223 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
1224 quantParam, operandTem.type};
1225 if (std::find(graphArgs.inputIndices.begin(), graphArgs.inputIndices.end(), i) !=
1226 graphArgs.inputIndices.end()) {
1227 OH_NNExecutor_SetInput(executor, inputIndex, &operand, operandTem.data, operandTem.length);
1228 inputIndex += 1;
1229 } else if (std::find(graphArgs.outputIndices.begin(), graphArgs.outputIndices.end(), i) !=
1230 graphArgs.outputIndices.end()) {
1231 OH_NNExecutor_SetOutput(executor, outputIndex, operandTem.data, operandTem.length);
1232 outputIndex += 1;
1233 }
1234 }
1235 OH_NN_ReturnCode run_ret = OH_NNExecutor_Run(executor);
1236 NAPI_ASSERT(env, run_ret == OH_NN_SUCCESS, "OH_NNExecutor_Run Error");
1237 ret = SUCCESS;
1238 } else if (devicesCount == PARAM_0) {
1239 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
1240 ret = SUCCESS;
1241 }
1242 FreeCompilation(compilation);
1243 Free(model);
1244 napi_value result = nullptr;
1245 napi_create_int32(env, ret, &result);
1246 return result;
1247 }
OHNNExecutorRunTwo(napi_env env,napi_callback_info info)1248 static napi_value OHNNExecutorRunTwo(napi_env env, napi_callback_info info)
1249 {
1250 int ret = FAIL;
1251 OH_NN_ReturnCode run_ret = OH_NNExecutor_Run(nullptr);
1252 if (run_ret == OH_NN_INVALID_PARAMETER) {
1253 ret = SUCCESS;
1254 }
1255 napi_value result = nullptr;
1256 napi_create_int32(env, ret, &result);
1257
1258 return result;
1259 }
OHNNExecutorAllocateInputMemoryOne(napi_env env,napi_callback_info info)1260 static napi_value OHNNExecutorAllocateInputMemoryOne(napi_env env, napi_callback_info info)
1261 {
1262 int ret = FAIL;
1263 OH_NNModel *model = OH_NNModel_Construct();
1264 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
1265 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
1266 OH_NN_UInt32Array paramIndices;
1267 paramIndices.data = graphArgs.paramIndices.data();
1268 paramIndices.size = graphArgs.paramIndices.size();
1269 OH_NN_UInt32Array inputIndices;
1270 inputIndices.data = graphArgs.inputIndices.data();
1271 inputIndices.size = graphArgs.inputIndices.size();
1272 OH_NN_UInt32Array outputIndices;
1273 outputIndices.data = graphArgs.outputIndices.data();
1274 outputIndices.size = graphArgs.outputIndices.size();
1275 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
1276 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
1277 OH_NNModel_Finish(model);
1278 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
1279 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
1280 const size_t *devicesID{nullptr};
1281 uint32_t devicesCount{PARAM_0};
1282 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
1283 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
1284 if (devicesCount > PARAM_0) {
1285 const char *name = nullptr;
1286 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
1287 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetName Error");
1288 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
1289 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
1290 OH_NN_ReturnCode build_ret = OH_NNCompilation_Build(compilation);
1291 NAPI_ASSERT(env, build_ret == OH_NN_SUCCESS, "OH_NNCompilation_Build Error");
1292 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
1293 NAPI_ASSERT(env, executor != nullptr, "OH_NNExecutor_Construct Error");
1294 const OHNNOperandTest &operandTem = graphArgs.operands[PARAM_0];
1295 OH_NN_Memory *inputMemory = OH_NNExecutor_AllocateInputMemory(executor, PARAM_0, operandTem.length);
1296 NAPI_ASSERT(env, inputMemory != nullptr, "OH_NNExecutor_AllocateInputMemory Error");
1297 ret = SUCCESS;
1298 OH_NNExecutor_DestroyInputMemory(executor, PARAM_0, &inputMemory);
1299 } else if (devicesCount == PARAM_0) {
1300 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
1301 ret = SUCCESS;
1302 }
1303 FreeCompilation(compilation);
1304 Free(model);
1305 napi_value result = nullptr;
1306 napi_create_int32(env, ret, &result);
1307 return result;
1308 }
OHNNExecutorAllocateInputMemoryTwo(napi_env env,napi_callback_info info)1309 static napi_value OHNNExecutorAllocateInputMemoryTwo(napi_env env, napi_callback_info info)
1310 {
1311 int ret = FAIL;
1312 OH_NN_Memory *inputMemory = OH_NNExecutor_AllocateInputMemory(nullptr, PARAM_0, PARAM_0);
1313 if (inputMemory == nullptr) {
1314 ret = SUCCESS;
1315 }
1316 napi_value result = nullptr;
1317 napi_create_int32(env, ret, &result);
1318
1319 return result;
1320 }
OHNNExecutorAllocateOutputMemoryOne(napi_env env,napi_callback_info info)1321 static napi_value OHNNExecutorAllocateOutputMemoryOne(napi_env env, napi_callback_info info)
1322 {
1323 int ret = FAIL;
1324 OH_NNModel *model = OH_NNModel_Construct();
1325 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
1326 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
1327 OH_NN_UInt32Array paramIndices;
1328 paramIndices.data = graphArgs.paramIndices.data();
1329 paramIndices.size = graphArgs.paramIndices.size();
1330 OH_NN_UInt32Array inputIndices;
1331 inputIndices.data = graphArgs.inputIndices.data();
1332 inputIndices.size = graphArgs.inputIndices.size();
1333 OH_NN_UInt32Array outputIndices;
1334 outputIndices.data = graphArgs.outputIndices.data();
1335 outputIndices.size = graphArgs.outputIndices.size();
1336 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
1337 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
1338 OH_NNModel_Finish(model);
1339 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
1340 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
1341 const size_t *devicesID{nullptr};
1342 uint32_t devicesCount{PARAM_0};
1343 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
1344 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
1345 if (devicesCount > PARAM_0) {
1346 const char *name = nullptr;
1347 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
1348 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetName Error");
1349 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
1350 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
1351 OH_NN_ReturnCode build_ret = OH_NNCompilation_Build(compilation);
1352 NAPI_ASSERT(env, build_ret == OH_NN_SUCCESS, "OH_NNCompilation_Build Error");
1353 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
1354 NAPI_ASSERT(env, executor != nullptr, "OH_NNExecutor_Construct Error");
1355 const OHNNOperandTest &operandTem = graphArgs.operands[PARAM_0];
1356 OH_NN_Memory *inputMemory = OH_NNExecutor_AllocateInputMemory(executor, PARAM_0, operandTem.length);
1357 NAPI_ASSERT(env, inputMemory != nullptr, "OH_NNExecutor_AllocateInputMemory Error");
1358 OH_NN_Memory *outputMemory = OH_NNExecutor_AllocateOutputMemory(executor, PARAM_0, operandTem.length);
1359 NAPI_ASSERT(env, outputMemory != nullptr, "OH_NNExecutor_AllocateOutputMemory Error");
1360 ret = SUCCESS;
1361 OH_NNExecutor_DestroyOutputMemory(executor, PARAM_0, &outputMemory);
1362 } else if (devicesCount == PARAM_0) {
1363 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
1364 ret = SUCCESS;
1365 }
1366 FreeCompilation(compilation);
1367 Free(model);
1368 napi_value result = nullptr;
1369 napi_create_int32(env, ret, &result);
1370 return result;
1371 }
OHNNExecutorAllocateOutputMemoryTwo(napi_env env,napi_callback_info info)1372 static napi_value OHNNExecutorAllocateOutputMemoryTwo(napi_env env, napi_callback_info info)
1373 {
1374 int ret = FAIL;
1375 OH_NN_Memory *inputMemory = OH_NNExecutor_AllocateOutputMemory(nullptr, PARAM_0, PARAM_0);
1376 if (inputMemory == nullptr) {
1377 ret = SUCCESS;
1378 }
1379 napi_value result = nullptr;
1380 napi_create_int32(env, ret, &result);
1381
1382 return result;
1383 }
OHNNExecutorDestroyInputMemory(napi_env env,napi_callback_info info)1384 static napi_value OHNNExecutorDestroyInputMemory(napi_env env, napi_callback_info info)
1385 {
1386 int ret = FAIL;
1387 OH_NNModel *model = OH_NNModel_Construct();
1388 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
1389 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
1390 OH_NN_UInt32Array paramIndices;
1391 paramIndices.data = graphArgs.paramIndices.data();
1392 paramIndices.size = graphArgs.paramIndices.size();
1393 OH_NN_UInt32Array inputIndices;
1394 inputIndices.data = graphArgs.inputIndices.data();
1395 inputIndices.size = graphArgs.inputIndices.size();
1396 OH_NN_UInt32Array outputIndices;
1397 outputIndices.data = graphArgs.outputIndices.data();
1398 outputIndices.size = graphArgs.outputIndices.size();
1399 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
1400 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
1401 OH_NNModel_Finish(model);
1402 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
1403 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
1404 const size_t *devicesID{nullptr};
1405 uint32_t devicesCount{PARAM_0};
1406 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
1407 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
1408 if (devicesCount > PARAM_0) {
1409 const char *name = nullptr;
1410 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
1411 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetName Error");
1412 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
1413 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
1414 OH_NN_ReturnCode build_ret = OH_NNCompilation_Build(compilation);
1415 NAPI_ASSERT(env, build_ret == OH_NN_SUCCESS, "OH_NNCompilation_Build Error");
1416 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
1417 NAPI_ASSERT(env, executor != nullptr, "OH_NNExecutor_Construct Error");
1418 const OHNNOperandTest &operandTem = graphArgs.operands[PARAM_0];
1419 OH_NN_Memory *inputMemory = OH_NNExecutor_AllocateInputMemory(executor, PARAM_0, operandTem.length);
1420 OH_NNExecutor_DestroyInputMemory(executor, PARAM_0, &inputMemory);
1421 NAPI_ASSERT(env, inputMemory == nullptr, "OH_NNExecutor_DestroyInputMemory Error");
1422 ret = SUCCESS;
1423 } else if (devicesCount == PARAM_0) {
1424 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
1425 ret = SUCCESS;
1426 }
1427 FreeCompilation(compilation);
1428 Free(model);
1429 napi_value result = nullptr;
1430 napi_create_int32(env, ret, &result);
1431 return result;
1432 }
OHNNExecutorDestroyOutputMemory(napi_env env,napi_callback_info info)1433 static napi_value OHNNExecutorDestroyOutputMemory(napi_env env, napi_callback_info info)
1434 {
1435 int ret = FAIL;
1436 OH_NNModel *model = OH_NNModel_Construct();
1437 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
1438 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
1439 OH_NN_UInt32Array paramIndices;
1440 paramIndices.data = graphArgs.paramIndices.data();
1441 paramIndices.size = graphArgs.paramIndices.size();
1442 OH_NN_UInt32Array inputIndices;
1443 inputIndices.data = graphArgs.inputIndices.data();
1444 inputIndices.size = graphArgs.inputIndices.size();
1445 OH_NN_UInt32Array outputIndices;
1446 outputIndices.data = graphArgs.outputIndices.data();
1447 outputIndices.size = graphArgs.outputIndices.size();
1448 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
1449 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
1450 OH_NNModel_Finish(model);
1451 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
1452 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
1453 const size_t *devicesID{nullptr};
1454 uint32_t devicesCount{PARAM_0};
1455 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
1456 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
1457 if (devicesCount > PARAM_0) {
1458 const char *name = nullptr;
1459 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
1460 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetName Error");
1461 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
1462 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
1463 OH_NN_ReturnCode build_ret = OH_NNCompilation_Build(compilation);
1464 NAPI_ASSERT(env, build_ret == OH_NN_SUCCESS, "OH_NNCompilation_Build Error");
1465 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
1466 NAPI_ASSERT(env, executor != nullptr, "OH_NNExecutor_Construct Error");
1467 const OHNNOperandTest &operandTem = graphArgs.operands[PARAM_0];
1468 OH_NN_Memory *inputMemory = OH_NNExecutor_AllocateInputMemory(executor, PARAM_0, operandTem.length);
1469 NAPI_ASSERT(env, inputMemory != nullptr, "OH_NNExecutor_AllocateInputMemory Error");
1470 OH_NN_Memory *outputMemory = OH_NNExecutor_AllocateOutputMemory(executor, PARAM_0, operandTem.length);
1471 OH_NNExecutor_DestroyOutputMemory(executor, PARAM_0, &outputMemory);
1472 NAPI_ASSERT(env, outputMemory == nullptr, "OH_NNExecutor_DestroyOutputMemory Error");
1473 ret = SUCCESS;
1474 } else if (devicesCount == PARAM_0) {
1475 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
1476 ret = SUCCESS;
1477 }
1478 FreeCompilation(compilation);
1479 Free(model);
1480 napi_value result = nullptr;
1481 napi_create_int32(env, ret, &result);
1482 return result;
1483 }
OHNNExecutorSetInputWithMemoryOne(napi_env env,napi_callback_info info)1484 static napi_value OHNNExecutorSetInputWithMemoryOne(napi_env env, napi_callback_info info)
1485 {
1486 int ret = FAIL;
1487 OH_NNModel *model = OH_NNModel_Construct();
1488 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
1489 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
1490 OH_NN_UInt32Array paramIndices;
1491 paramIndices.data = graphArgs.paramIndices.data();
1492 paramIndices.size = graphArgs.paramIndices.size();
1493 OH_NN_UInt32Array inputIndices;
1494 inputIndices.data = graphArgs.inputIndices.data();
1495 inputIndices.size = graphArgs.inputIndices.size();
1496 OH_NN_UInt32Array outputIndices;
1497 outputIndices.data = graphArgs.outputIndices.data();
1498 outputIndices.size = graphArgs.outputIndices.size();
1499 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
1500 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
1501 OH_NNModel_Finish(model);
1502 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
1503 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
1504 const size_t *devicesID{nullptr};
1505 uint32_t devicesCount{PARAM_0};
1506 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
1507 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
1508 if (devicesCount > PARAM_0) {
1509 const char *name = nullptr;
1510 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
1511 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetName Error");
1512 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
1513 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
1514 OH_NN_ReturnCode build_ret = OH_NNCompilation_Build(compilation);
1515 NAPI_ASSERT(env, build_ret == OH_NN_SUCCESS, "OH_NNCompilation_Build Error");
1516 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
1517 NAPI_ASSERT(env, executor != nullptr, "OH_NNExecutor_Construct Error");
1518 const OHNNOperandTest &operandTem = graphArgs.operands[PARAM_0];
1519 OH_NN_Memory *inputMemory = OH_NNExecutor_AllocateInputMemory(executor, PARAM_0, operandTem.length);
1520 NAPI_ASSERT(env, inputMemory != nullptr, "OH_NNExecutor_AllocateInputMemory Error");
1521 auto quantParam = operandTem.quantParam;
1522 OH_NN_Tensor operand = {operandTem.dataType, (uint32_t)operandTem.shape.size(), operandTem.shape.data(),
1523 quantParam, operandTem.type};
1524 OH_NN_ReturnCode setInputMemory_ret =
1525 OH_NNExecutor_SetInputWithMemory(executor, PARAM_0, &operand, inputMemory);
1526 NAPI_ASSERT(env, setInputMemory_ret == OH_NN_SUCCESS, "OH_NNExecutor_SetInputWithMemory Error");
1527 ret = SUCCESS;
1528 } else if (devicesCount == PARAM_0) {
1529 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
1530 ret = SUCCESS;
1531 }
1532 FreeCompilation(compilation);
1533 Free(model);
1534 napi_value result = nullptr;
1535 napi_create_int32(env, ret, &result);
1536
1537 return result;
1538 }
OHNNExecutorSetInputWithMemoryTwo(napi_env env,napi_callback_info info)1539 static napi_value OHNNExecutorSetInputWithMemoryTwo(napi_env env, napi_callback_info info)
1540 {
1541 int ret = FAIL;
1542 OH_NN_ReturnCode setInputMemory_ret = OH_NNExecutor_SetInputWithMemory(nullptr, PARAM_0, nullptr, nullptr);
1543 if (setInputMemory_ret == OH_NN_INVALID_PARAMETER) {
1544 ret = SUCCESS;
1545 }
1546 napi_value result = nullptr;
1547 napi_create_int32(env, ret, &result);
1548
1549 return result;
1550 }
OHNNExecutorSetOutputWithMemoryOne(napi_env env,napi_callback_info info)1551 static napi_value OHNNExecutorSetOutputWithMemoryOne(napi_env env, napi_callback_info info)
1552 {
1553 int ret = FAIL;
1554 OH_NNModel *model = OH_NNModel_Construct();
1555 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
1556 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
1557 OH_NN_UInt32Array paramIndices;
1558 paramIndices.data = graphArgs.paramIndices.data();
1559 paramIndices.size = graphArgs.paramIndices.size();
1560 OH_NN_UInt32Array inputIndices;
1561 inputIndices.data = graphArgs.inputIndices.data();
1562 inputIndices.size = graphArgs.inputIndices.size();
1563 OH_NN_UInt32Array outputIndices;
1564 outputIndices.data = graphArgs.outputIndices.data();
1565 outputIndices.size = graphArgs.outputIndices.size();
1566 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
1567 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
1568 OH_NNModel_Finish(model);
1569 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
1570 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
1571 const size_t *devicesID{nullptr};
1572 uint32_t devicesCount{PARAM_0};
1573 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
1574 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
1575 if (devicesCount > PARAM_0) {
1576 const char *name = nullptr;
1577 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
1578 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetName Error");
1579 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
1580 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
1581 OH_NN_ReturnCode build_ret = OH_NNCompilation_Build(compilation);
1582 NAPI_ASSERT(env, build_ret == OH_NN_SUCCESS, "OH_NNCompilation_Build Error");
1583 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
1584 NAPI_ASSERT(env, executor != nullptr, "OH_NNExecutor_Construct Error");
1585 const OHNNOperandTest &operandTem = graphArgs.operands[PARAM_0];
1586 OH_NN_Memory *inputMemory = OH_NNExecutor_AllocateInputMemory(executor, PARAM_0, operandTem.length);
1587 NAPI_ASSERT(env, inputMemory != nullptr, "OH_NNExecutor_AllocateInputMemory Error");
1588 OH_NN_Memory *outputMemory = OH_NNExecutor_AllocateOutputMemory(executor, PARAM_0, operandTem.length);
1589 NAPI_ASSERT(env, outputMemory != nullptr, "OH_NNExecutor_AllocateOutputMemory Error");
1590 OH_NN_ReturnCode setOutputMempry_ret = OH_NNExecutor_SetOutputWithMemory(executor, PARAM_0, outputMemory);
1591 NAPI_ASSERT(env, setOutputMempry_ret == OH_NN_SUCCESS, "OH_NNExecutor_SetOutputWithMemory Error");
1592 ret = SUCCESS;
1593 } else if (devicesCount == PARAM_0) {
1594 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
1595 ret = SUCCESS;
1596 }
1597 FreeCompilation(compilation);
1598 Free(model);
1599 napi_value result = nullptr;
1600 napi_create_int32(env, ret, &result);
1601 return result;
1602 }
OHNNExecutorSetOutputWithMemoryTwo(napi_env env,napi_callback_info info)1603 static napi_value OHNNExecutorSetOutputWithMemoryTwo(napi_env env, napi_callback_info info)
1604 {
1605 int ret = FAIL;
1606 OH_NN_ReturnCode setOutputMempry_ret = OH_NNExecutor_SetOutputWithMemory(nullptr, 0, nullptr);
1607 if (setOutputMempry_ret == OH_NN_INVALID_PARAMETER) {
1608 ret = SUCCESS;
1609 }
1610 napi_value result = nullptr;
1611 napi_create_int32(env, ret, &result);
1612
1613 return result;
1614 }
OHNNExecutorDestroy(napi_env env,napi_callback_info info)1615 static napi_value OHNNExecutorDestroy(napi_env env, napi_callback_info info)
1616 {
1617 int ret = FAIL;
1618 OH_NNModel *model = OH_NNModel_Construct();
1619 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
1620 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
1621 OH_NN_UInt32Array paramIndices;
1622 paramIndices.data = graphArgs.paramIndices.data();
1623 paramIndices.size = graphArgs.paramIndices.size();
1624 OH_NN_UInt32Array inputIndices;
1625 inputIndices.data = graphArgs.inputIndices.data();
1626 inputIndices.size = graphArgs.inputIndices.size();
1627 OH_NN_UInt32Array outputIndices;
1628 outputIndices.data = graphArgs.outputIndices.data();
1629 outputIndices.size = graphArgs.outputIndices.size();
1630 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
1631 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
1632 OH_NNModel_Finish(model);
1633 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
1634 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
1635 const size_t *devicesID{nullptr};
1636 uint32_t devicesCount{PARAM_0};
1637 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
1638 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
1639 if (devicesCount > PARAM_0) {
1640 const char *name = nullptr;
1641 OH_NNDevice_GetName(devicesID[PARAM_0], &name);
1642 NAPI_ASSERT(env, name != nullptr, "OH_NNDevice_GetName Error");
1643 OH_NN_ReturnCode setDevice_ret = OH_NNCompilation_SetDevice(compilation, devicesID[PARAM_0]);
1644 NAPI_ASSERT(env, setDevice_ret == OH_NN_SUCCESS, "OH_NNCompilation_SetDevice Error");
1645 OH_NN_ReturnCode build_ret = OH_NNCompilation_Build(compilation);
1646 NAPI_ASSERT(env, build_ret == OH_NN_SUCCESS, "OH_NNCompilation_Build Error");
1647 OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation);
1648 OH_NNExecutor_Destroy(&executor);
1649 NAPI_ASSERT(env, executor == nullptr, "OH_NNExecutor_Destroy Error");
1650 ret = SUCCESS;
1651 } else if (devicesCount == PARAM_0) {
1652 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
1653 ret = SUCCESS;
1654 }
1655 FreeCompilation(compilation);
1656 Free(model);
1657 napi_value result = nullptr;
1658 napi_create_int32(env, ret, &result);
1659 return result;
1660 }
OHNNDeviceGetAllDevicesIDOne(napi_env env,napi_callback_info info)1661 static napi_value OHNNDeviceGetAllDevicesIDOne(napi_env env, napi_callback_info info)
1662 {
1663 int ret = FAIL;
1664 OH_NNModel *model = OH_NNModel_Construct();
1665 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
1666 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
1667 OH_NN_UInt32Array paramIndices;
1668 paramIndices.data = graphArgs.paramIndices.data();
1669 paramIndices.size = graphArgs.paramIndices.size();
1670 OH_NN_UInt32Array inputIndices;
1671 inputIndices.data = graphArgs.inputIndices.data();
1672 inputIndices.size = graphArgs.inputIndices.size();
1673 OH_NN_UInt32Array outputIndices;
1674 outputIndices.data = graphArgs.outputIndices.data();
1675 outputIndices.size = graphArgs.outputIndices.size();
1676 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
1677 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
1678 OH_NNModel_Finish(model);
1679 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
1680 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
1681 const size_t *devicesID{nullptr};
1682 uint32_t devicesCount{PARAM_0};
1683 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
1684 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
1685 ret = SUCCESS;
1686 Free(model);
1687 napi_value result = nullptr;
1688 napi_create_int32(env, ret, &result);
1689 return result;
1690 }
OHNNDeviceGetAllDevicesIDTwo(napi_env env,napi_callback_info info)1691 static napi_value OHNNDeviceGetAllDevicesIDTwo(napi_env env, napi_callback_info info)
1692 {
1693 int ret = FAIL;
1694 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(nullptr, PARAM_0);
1695 if (allDeviceId_ret == OH_NN_INVALID_PARAMETER) {
1696 ret = SUCCESS;
1697 }
1698 napi_value result = nullptr;
1699 napi_create_int32(env, ret, &result);
1700
1701 return result;
1702 }
OHNNDeviceGetNameOne(napi_env env,napi_callback_info info)1703 static napi_value OHNNDeviceGetNameOne(napi_env env, napi_callback_info info)
1704 {
1705 int ret = FAIL;
1706 OH_NNModel *model = OH_NNModel_Construct();
1707 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
1708 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
1709 OH_NN_UInt32Array paramIndices;
1710 paramIndices.data = graphArgs.paramIndices.data();
1711 paramIndices.size = graphArgs.paramIndices.size();
1712 OH_NN_UInt32Array inputIndices;
1713 inputIndices.data = graphArgs.inputIndices.data();
1714 inputIndices.size = graphArgs.inputIndices.size();
1715 OH_NN_UInt32Array outputIndices;
1716 outputIndices.data = graphArgs.outputIndices.data();
1717 outputIndices.size = graphArgs.outputIndices.size();
1718 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
1719 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
1720 OH_NNModel_Finish(model);
1721 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
1722 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
1723 const size_t *devicesID{nullptr};
1724 uint32_t devicesCount{PARAM_0};
1725 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
1726 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
1727 if (devicesCount > PARAM_0) {
1728 const char *name = nullptr;
1729 OH_NN_ReturnCode getDeviceName_ret = OH_NNDevice_GetName(devicesID[PARAM_0], &name);
1730 NAPI_ASSERT(env, getDeviceName_ret == OH_NN_SUCCESS, "OH_NNDevice_GetName Error");
1731 ret = SUCCESS;
1732 } else if (devicesCount == PARAM_0) {
1733 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
1734 ret = SUCCESS;
1735 }
1736 FreeCompilation(compilation);
1737 Free(model);
1738 napi_value result = nullptr;
1739 napi_create_int32(env, ret, &result);
1740 return result;
1741 }
OHNNDeviceGetNameTwo(napi_env env,napi_callback_info info)1742 static napi_value OHNNDeviceGetNameTwo(napi_env env, napi_callback_info info)
1743 {
1744 int ret = FAIL;
1745 const char *name = nullptr;
1746 OH_NN_ReturnCode getDeviceName_ret = OH_NNDevice_GetName(PARAM_1, &name);
1747 if (getDeviceName_ret == OH_NN_FAILED) {
1748 ret = SUCCESS;
1749 }
1750 napi_value result = nullptr;
1751 napi_create_int32(env, ret, &result);
1752
1753 return result;
1754 }
OHNNDeviceGetTypeOne(napi_env env,napi_callback_info info)1755 static napi_value OHNNDeviceGetTypeOne(napi_env env, napi_callback_info info)
1756 {
1757 int ret = FAIL;
1758 OH_NNModel *model = OH_NNModel_Construct();
1759 NAPI_ASSERT(env, model != nullptr, "OH_NNModel_Construct Error");
1760 OHNNGraphArgs graphArgs = addTesorAndSetTensor(model);
1761 OH_NN_UInt32Array paramIndices;
1762 paramIndices.data = graphArgs.paramIndices.data();
1763 paramIndices.size = graphArgs.paramIndices.size();
1764 OH_NN_UInt32Array inputIndices;
1765 inputIndices.data = graphArgs.inputIndices.data();
1766 inputIndices.size = graphArgs.inputIndices.size();
1767 OH_NN_UInt32Array outputIndices;
1768 outputIndices.data = graphArgs.outputIndices.data();
1769 outputIndices.size = graphArgs.outputIndices.size();
1770 OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices);
1771 OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
1772 OH_NNModel_Finish(model);
1773 OH_NNCompilation *compilation = OH_NNCompilation_Construct(model);
1774 NAPI_ASSERT(env, compilation != nullptr, "OH_NNCompilation_Construct Error");
1775 const size_t *devicesID{nullptr};
1776 uint32_t devicesCount{PARAM_0};
1777 OH_NN_ReturnCode allDeviceId_ret = OH_NNDevice_GetAllDevicesID(&devicesID, &devicesCount);
1778 NAPI_ASSERT(env, allDeviceId_ret == OH_NN_SUCCESS, "OH_NNDevice_GetAllDevicesID Error");
1779 if (devicesCount > PARAM_0) {
1780 OH_NN_DeviceType type{OH_NN_OTHERS};
1781 OH_NN_ReturnCode getDeviceName_ret = OH_NNDevice_GetType(devicesID[PARAM_0], &type);
1782 NAPI_ASSERT(env, getDeviceName_ret == OH_NN_SUCCESS, "OH_NNDevice_GetType Error");
1783 ret = SUCCESS;
1784 } else if (devicesCount == PARAM_0) {
1785 OH_LOG_Print(LOG_APP, LOG_INFO, 1, "devicesCount is 0", "no devices");
1786 ret = SUCCESS;
1787 }
1788 FreeCompilation(compilation);
1789 Free(model);
1790 napi_value result = nullptr;
1791 napi_create_int32(env, ret, &result);
1792 return result;
1793 }
OHNNDeviceGetTypeTwo(napi_env env,napi_callback_info info)1794 static napi_value OHNNDeviceGetTypeTwo(napi_env env, napi_callback_info info)
1795 {
1796 int ret = FAIL;
1797 OH_NN_DeviceType type{OH_NN_OTHERS};
1798 OH_NN_ReturnCode getDeviceName_ret = OH_NNDevice_GetType(MPARAM_1, &type);
1799 if (getDeviceName_ret == OH_NN_INVALID_PARAMETER) {
1800 ret = SUCCESS;
1801 }
1802 napi_value result = nullptr;
1803 napi_create_int32(env, ret, &result);
1804
1805 return result;
1806 }
1807 EXTERN_C_START
Init(napi_env env,napi_value exports)1808 static napi_value Init(napi_env env, napi_value exports)
1809 {
1810 napi_property_descriptor desc[] = {
1811 {"oHNNModelConstructOne", nullptr, OHNNModelConstructOne, nullptr, nullptr, nullptr, napi_default, nullptr},
1812 {"oHNNModelConstructTwo", nullptr, OHNNModelConstructTwo, nullptr, nullptr, nullptr, napi_default, nullptr},
1813 {"oHNNModelAddTensorOne", nullptr, OHNNModelAddTensorOne, nullptr, nullptr, nullptr, napi_default, nullptr},
1814 {"oHNNModelAddTensorTwo", nullptr, OHNNModelAddTensorTwo, nullptr, nullptr, nullptr, napi_default, nullptr},
1815 {"oHNNModelSetTensorDataOne", nullptr, OHNNModelSetTensorDataOne, nullptr, nullptr, nullptr, napi_default,
1816 nullptr},
1817 {"oHNNModelSetTensorDataTwo", nullptr, OHNNModelSetTensorDataTwo, nullptr, nullptr, nullptr, napi_default,
1818 nullptr},
1819 {"oHNNModelAddOperationOne", nullptr, OHNNModelAddOperationOne, nullptr, nullptr, nullptr, napi_default,
1820 nullptr},
1821 {"oHNNModelAddOperationTwo", nullptr, OHNNModelAddOperationTwo, nullptr, nullptr, nullptr, napi_default,
1822 nullptr},
1823 {"oHNNModelSpecifyInputsAndOutputsOne", nullptr, OHNNModelSpecifyInputsAndOutputsOne, nullptr, nullptr, nullptr,
1824 napi_default, nullptr},
1825 {"oHNNModelSpecifyInputsAndOutputsTwo", nullptr, OHNNModelSpecifyInputsAndOutputsTwo, nullptr, nullptr, nullptr,
1826 napi_default, nullptr},
1827 {"oHNNModelFinishOne", nullptr, OHNNModelFinishOne, nullptr, nullptr, nullptr, napi_default, nullptr},
1828 {"oHNNModelFinishTwo", nullptr, OHNNModelFinishTwo, nullptr, nullptr, nullptr, napi_default, nullptr},
1829 {"oHNNModelDestroy", nullptr, OHNNModelDestroy, nullptr, nullptr, nullptr, napi_default, nullptr},
1830 {"oHNNModelGetAvailableoperationsOne", nullptr, OHNNModelGetAvailableoperationsOne, nullptr, nullptr, nullptr,
1831 napi_default, nullptr},
1832 {"oHNNModelGetAvailableoperationsTwo", nullptr, OHNNModelGetAvailableoperationsTwo, nullptr, nullptr, nullptr,
1833 napi_default, nullptr},
1834 {"oHNNCompilationConstructOne", nullptr, OHNNCompilationConstructOne, nullptr, nullptr, nullptr, napi_default,
1835 nullptr},
1836 {"oHNNCompilationConstructTwo", nullptr, OHNNCompilationConstructTwo, nullptr, nullptr, nullptr, napi_default,
1837 nullptr},
1838 {"oHNNCompilationSetDeviceOne", nullptr, OHNNCompilationSetDeviceOne, nullptr, nullptr, nullptr, napi_default,
1839 nullptr},
1840 {"oHNNCompilationSetDeviceTwo", nullptr, OHNNCompilationSetDeviceTwo, nullptr, nullptr, nullptr, napi_default,
1841 nullptr},
1842 {"oHNNCompilationSetCacheOne", nullptr, OHNNCompilationSetCacheOne, nullptr, nullptr, nullptr, napi_default,
1843 nullptr},
1844 {"oHNNCompilationSetCacheTwo", nullptr, OHNNCompilationSetCacheTwo, nullptr, nullptr, nullptr, napi_default,
1845 nullptr},
1846 {"oHNNCompilationSetPerformanceModeOne", nullptr, OHNNCompilationSetPerformanceModeOne, nullptr, nullptr,
1847 nullptr, napi_default, nullptr},
1848 {"oHNNCompilationSetPerformanceModeTwo", nullptr, OHNNCompilationSetPerformanceModeTwo, nullptr, nullptr,
1849 nullptr, napi_default, nullptr},
1850 {"oHNNCompilationSetPerformanceModeThree", nullptr, OHNNCompilationSetPerformanceModeThree, nullptr, nullptr,
1851 nullptr, napi_default, nullptr},
1852 {"oHNNCompilationSetPerformanceModeFour", nullptr, OHNNCompilationSetPerformanceModeFour, nullptr, nullptr,
1853 nullptr, napi_default, nullptr},
1854 {"oHNNCompilationSetPriorityOne", nullptr, OHNNCompilationSetPriorityOne, nullptr, nullptr, nullptr,
1855 napi_default, nullptr},
1856 {"oHNNCompilationSetPriorityTwo", nullptr, OHNNCompilationSetPriorityTwo, nullptr, nullptr, nullptr,
1857 napi_default, nullptr},
1858 {"oHNNCompilationEnableFloat16One", nullptr, OHNNCompilationEnableFloat16One, nullptr, nullptr, nullptr,
1859 napi_default, nullptr},
1860 {"oHNNCompilationEnableFloat16Two", nullptr, OHNNCompilationEnableFloat16Two, nullptr, nullptr, nullptr,
1861 napi_default, nullptr},
1862 {"oHNNCompilationBuildOne", nullptr, OHNNCompilationBuildOne, nullptr, nullptr, nullptr, napi_default, nullptr},
1863 {"oHNNCompilationBuildTwo", nullptr, OHNNCompilationBuildTwo, nullptr, nullptr, nullptr, napi_default, nullptr},
1864 {"oHNNCompilationDestroy", nullptr, OHNNCompilationDestroy, nullptr, nullptr, nullptr, napi_default, nullptr},
1865 {"oHNNExecutorConstructOne", nullptr, OHNNExecutorConstructOne, nullptr, nullptr, nullptr, napi_default,
1866 nullptr},
1867 {"oHNNExecutorConstructTwo", nullptr, OHNNExecutorConstructTwo, nullptr, nullptr, nullptr, napi_default,
1868 nullptr},
1869 {"oHNNExecutorSetInputOne", nullptr, OHNNExecutorSetInputOne, nullptr, nullptr, nullptr, napi_default, nullptr},
1870 {"oHNNExecutorSetInputTwo", nullptr, OHNNExecutorSetInputTwo, nullptr, nullptr, nullptr, napi_default, nullptr},
1871 {"oHNNExecutorSetOutputOne", nullptr, OHNNExecutorSetOutputOne, nullptr, nullptr, nullptr, napi_default,
1872 nullptr},
1873 {"oHNNExecutorSetOutputTwo", nullptr, OHNNExecutorSetOutputTwo, nullptr, nullptr, nullptr, napi_default,
1874 nullptr},
1875 {"oHNNExecutorGetOutputShapeOne", nullptr, OHNNExecutorGetOutputShapeOne, nullptr, nullptr, nullptr,
1876 napi_default, nullptr},
1877 {"oHNNExecutorGetOutputShapeTwo", nullptr, OHNNExecutorGetOutputShapeTwo, nullptr, nullptr, nullptr,
1878 napi_default, nullptr},
1879 {"oHNNExecutorRunOne", nullptr, OHNNExecutorRunOne, nullptr, nullptr, nullptr, napi_default, nullptr},
1880 {"oHNNExecutorRunTwo", nullptr, OHNNExecutorRunTwo, nullptr, nullptr, nullptr, napi_default, nullptr},
1881 {"oHNNExecutorAllocateInputMemoryOne", nullptr, OHNNExecutorAllocateInputMemoryOne, nullptr, nullptr, nullptr,
1882 napi_default, nullptr},
1883 {"oHNNExecutorAllocateInputMemoryTwo", nullptr, OHNNExecutorAllocateInputMemoryTwo, nullptr, nullptr, nullptr,
1884 napi_default, nullptr},
1885 {"oHNNExecutorAllocateOutputMemoryOne", nullptr, OHNNExecutorAllocateOutputMemoryOne, nullptr, nullptr, nullptr,
1886 napi_default, nullptr},
1887 {"oHNNExecutorAllocateOutputMemoryTwo", nullptr, OHNNExecutorAllocateOutputMemoryTwo, nullptr, nullptr, nullptr,
1888 napi_default, nullptr},
1889 {"oHNNExecutorDestroyInputMemory", nullptr, OHNNExecutorDestroyInputMemory, nullptr, nullptr, nullptr,
1890 napi_default, nullptr},
1891 {"oHNNExecutorDestroyOutputMemory", nullptr, OHNNExecutorDestroyOutputMemory, nullptr, nullptr, nullptr,
1892 napi_default, nullptr},
1893 {"oHNNExecutorSetInputWithMemoryOne", nullptr, OHNNExecutorSetInputWithMemoryOne, nullptr, nullptr, nullptr,
1894 napi_default, nullptr},
1895 {"oHNNExecutorSetInputWithMemoryTwo", nullptr, OHNNExecutorSetInputWithMemoryTwo, nullptr, nullptr, nullptr,
1896 napi_default, nullptr},
1897 {"oHNNExecutorSetOutputWithMemoryOne", nullptr, OHNNExecutorSetOutputWithMemoryOne, nullptr, nullptr, nullptr,
1898 napi_default, nullptr},
1899 {"oHNNExecutorSetOutputWithMemoryTwo", nullptr, OHNNExecutorSetOutputWithMemoryTwo, nullptr, nullptr, nullptr,
1900 napi_default, nullptr},
1901 {"oHNNExecutorDestroy", nullptr, OHNNExecutorDestroy, nullptr, nullptr, nullptr, napi_default, nullptr},
1902 {"oHNNDeviceGetAllDevicesIDOne", nullptr, OHNNDeviceGetAllDevicesIDOne, nullptr, nullptr, nullptr, napi_default,
1903 nullptr},
1904 {"oHNNDeviceGetAllDevicesIDTwo", nullptr, OHNNDeviceGetAllDevicesIDTwo, nullptr, nullptr, nullptr, napi_default,
1905 nullptr},
1906 {"oHNNDeviceGetNameOne", nullptr, OHNNDeviceGetNameOne, nullptr, nullptr, nullptr, napi_default, nullptr},
1907 {"oHNNDeviceGetNameTwo", nullptr, OHNNDeviceGetNameTwo, nullptr, nullptr, nullptr, napi_default, nullptr},
1908 {"oHNNDeviceGetTypeOne", nullptr, OHNNDeviceGetTypeOne, nullptr, nullptr, nullptr, napi_default, nullptr},
1909 {"oHNNDeviceGetTypeTwo", nullptr, OHNNDeviceGetTypeTwo, nullptr, nullptr, nullptr, napi_default, nullptr},
1910 };
1911 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
1912 return exports;
1913 }
1914 EXTERN_C_END
1915
1916 static napi_module demoModule = {
1917 .nm_version = 1,
1918 .nm_flags = 0,
1919 .nm_filename = nullptr,
1920 .nm_register_func = Init,
1921 .nm_modname = "nnrt",
1922 .nm_priv = ((void *)0),
1923 .reserved = {0},
1924 };
1925
RegisterEntryModule(void)1926 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
1927