1 /*
2 * Copyright (c) 2022 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 "interfaces/innerkits/c/neural_network_runtime_inner.h"
17 #include "interfaces/kits/c/neural_network_runtime.h"
18
19 #include "compilation.h"
20 #include "device_manager.h"
21 #include "executor.h"
22 #include "inner_model.h"
23 #include "common/log.h"
24
25
26 using namespace OHOS::NeuralNetworkRuntime;
27
28 #define NNRT_API __attribute__((visibility("default")))
29
OH_NNModel_Construct(void)30 NNRT_API OH_NNModel *OH_NNModel_Construct(void)
31 {
32 InnerModel *innerModel = new(std::nothrow) InnerModel();
33 if (innerModel == nullptr) {
34 LOGE("OH_NNModel_Construct failed, please check whether it has enough memory.");
35 return nullptr;
36 }
37
38 OH_NNModel *nnModel = reinterpret_cast<OH_NNModel*>(innerModel);
39 return nnModel;
40 }
41
OH_NNModel_AddTensor(OH_NNModel * model,const OH_NN_Tensor * tensor)42 NNRT_API OH_NN_ReturnCode OH_NNModel_AddTensor(OH_NNModel *model, const OH_NN_Tensor *tensor)
43 {
44 if (model == nullptr) {
45 LOGE("OH_NNModel_AddTensor failed, passed nullptr to model.");
46 return OH_NN_INVALID_PARAMETER;
47 }
48
49 if (tensor == nullptr) {
50 LOGE("OH_NNModel_AddTensor failed, passed nullptr to tensor.");
51 return OH_NN_INVALID_PARAMETER;
52 }
53
54 InnerModel *innerModel = reinterpret_cast<InnerModel*>(model);
55 return innerModel->AddTensor(*tensor);
56 }
57
OH_NNModel_AddOperation(OH_NNModel * model,OH_NN_OperationType op,const OH_NN_UInt32Array * paramIndices,const OH_NN_UInt32Array * inputIndices,const OH_NN_UInt32Array * outputIndices)58 NNRT_API OH_NN_ReturnCode OH_NNModel_AddOperation(OH_NNModel *model,
59 OH_NN_OperationType op,
60 const OH_NN_UInt32Array *paramIndices,
61 const OH_NN_UInt32Array *inputIndices,
62 const OH_NN_UInt32Array *outputIndices)
63 {
64 if (model == nullptr) {
65 LOGE("OH_NNModel_AddOperation failed, passed nullptr to model.");
66 return OH_NN_INVALID_PARAMETER;
67 }
68
69 if (paramIndices == nullptr) {
70 LOGE("OH_NNModel_AddOperation failed, passed nullptr to paramIndices.");
71 return OH_NN_INVALID_PARAMETER;
72 }
73
74 if (inputIndices == nullptr) {
75 LOGE("OH_NNModel_AddOperation failed, passed nullptr to inputIndices.");
76 return OH_NN_INVALID_PARAMETER;
77 }
78
79 if (outputIndices == nullptr) {
80 LOGE("OH_NNModel_AddOperation failed, passed nullptr to outputIndices.");
81 return OH_NN_INVALID_PARAMETER;
82 }
83
84 InnerModel *innerModel = reinterpret_cast<InnerModel*>(model);
85 return innerModel->AddOperation(op, *paramIndices, *inputIndices, *outputIndices);
86 }
87
OH_NNModel_SetTensorData(OH_NNModel * model,uint32_t index,const void * dataBuffer,size_t length)88 NNRT_API OH_NN_ReturnCode OH_NNModel_SetTensorData(OH_NNModel *model,
89 uint32_t index,
90 const void *dataBuffer,
91 size_t length)
92 {
93 if (model == nullptr) {
94 LOGE("OH_NNModel_SetTensorData failed, passed nullptr to model.");
95 return OH_NN_INVALID_PARAMETER;
96 }
97
98 if (dataBuffer == nullptr) {
99 LOGE("OH_NNModel_SetTensorData failed, passed nullptr to dataBuffer, which has no effect.");
100 return OH_NN_INVALID_PARAMETER;
101 }
102
103 if (length == 0) {
104 LOGE("OH_NNModel_SetTensorData failed, passed dataBuffer with length 0, which has no effect.");
105 return OH_NN_INVALID_PARAMETER;
106 }
107
108 InnerModel *innerModel = reinterpret_cast<InnerModel*>(model);
109 return innerModel->SetTensorValue(index, dataBuffer, length);
110 }
111
OH_NNModel_SpecifyInputsAndOutputs(OH_NNModel * model,const OH_NN_UInt32Array * inputIndices,const OH_NN_UInt32Array * outputIndices)112 NNRT_API OH_NN_ReturnCode OH_NNModel_SpecifyInputsAndOutputs(OH_NNModel *model,
113 const OH_NN_UInt32Array *inputIndices,
114 const OH_NN_UInt32Array *outputIndices)
115 {
116 if (model == nullptr) {
117 LOGE("OH_NNModel_SpecifyInputsAndOutputs failed, passed nullptr to model.");
118 return OH_NN_INVALID_PARAMETER;
119 }
120
121 if (inputIndices == nullptr) {
122 LOGE("OH_NNModel_SpecifyInputsAndOutputs failed, passed nullptr to inputIndices.");
123 return OH_NN_INVALID_PARAMETER;
124 }
125
126 if (outputIndices == nullptr) {
127 LOGE("OH_NNModel_SpecifyInputsAndOutputs failed, passed nullptr to outputIndices.");
128 return OH_NN_INVALID_PARAMETER;
129 }
130
131 InnerModel *innerModel = reinterpret_cast<InnerModel*>(model);
132 return innerModel->SpecifyInputsAndOutputs(*inputIndices, *outputIndices);
133 }
134
OH_NNModel_Finish(OH_NNModel * model)135 NNRT_API OH_NN_ReturnCode OH_NNModel_Finish(OH_NNModel *model)
136 {
137 if (model == nullptr) {
138 LOGE("OH_NNModel_Finish failed, passed nullptr to model.");
139 return OH_NN_INVALID_PARAMETER;
140 }
141
142 InnerModel *innerModel = reinterpret_cast<InnerModel*>(model);
143 return innerModel->Build();
144 }
145
OH_NNModel_BuildFromLiteGraph(OH_NNModel * model,const void * liteGraph)146 NNRT_API OH_NN_ReturnCode OH_NNModel_BuildFromLiteGraph(OH_NNModel *model, const void *liteGraph)
147 {
148 if (model == nullptr) {
149 LOGE("OH_NNModel_BuildFromLiteGraph failed, passed nullptr to model.");
150 return OH_NN_INVALID_PARAMETER;
151 }
152
153 if (liteGraph == nullptr) {
154 LOGE("OH_NNModel_BuildFromLiteGraph failed, passed nullptr to liteGraph.");
155 return OH_NN_INVALID_PARAMETER;
156 }
157
158 auto *pLiteGraph = static_cast<const mindspore::lite::LiteGraph*>(liteGraph);
159 InnerModel *innerModel = reinterpret_cast<InnerModel*>(model);
160
161 // Once the innerModel built from the liteGraph successfully, the innerModel
162 // owns the liteGraph, in which case, the invoker should not delete
163 // the liteGraph actively. Otherwise, the invoker still has the ownership.
164 return innerModel->BuildFromLiteGraph(pLiteGraph);
165 }
166
OH_NNModel_BuildFromMetaGraph(OH_NNModel * model,const void * metaGraph,const OH_NN_Extension * extensions,size_t extensionSize)167 NNRT_API OH_NN_ReturnCode OH_NNModel_BuildFromMetaGraph(OH_NNModel *model, const void *metaGraph,
168 const OH_NN_Extension *extensions, size_t extensionSize)
169 {
170 if (model == nullptr) {
171 LOGE("OH_NNModel_BuildFromMetaGraph failed, passed nullptr to model.");
172 return OH_NN_INVALID_PARAMETER;
173 }
174
175 if (metaGraph == nullptr) {
176 LOGE("OH_NNModel_BuildFromMetaGraph failed, passed nullptr to metaGraph.");
177 return OH_NN_INVALID_PARAMETER;
178 }
179
180 Buffer buffer;
181 std::string modelName;
182 for (size_t i = 0; i < extensionSize; ++i) {
183 std::string name = extensions[i].name;
184 if (name == "QuantBuffer") {
185 buffer.data = extensions[i].value;
186 buffer.length = extensions[i].valueSize;
187 } else if (name == "ModelName") {
188 modelName.assign(extensions[i].value, extensions[i].value + extensions[i].valueSize);
189 }
190 }
191
192 InnerModel *innerModel = reinterpret_cast<InnerModel*>(model);
193 return innerModel->BuildFromMetaGraph(metaGraph, buffer, modelName);
194 }
195
OH_NNModel_SetInputsAndOutputsInfo(OH_NNModel * model,const OH_NN_TensorInfo * inputsInfo,size_t inputSize,const OH_NN_TensorInfo * outputsInfo,size_t outputSize)196 NNRT_API OH_NN_ReturnCode OH_NNModel_SetInputsAndOutputsInfo(OH_NNModel *model, const OH_NN_TensorInfo *inputsInfo,
197 size_t inputSize, const OH_NN_TensorInfo *outputsInfo, size_t outputSize)
198 {
199 if (model == nullptr) {
200 LOGE("OH_NNModel_SetInputsAndOutputsInfo failed, passed nullptr to model.");
201 return OH_NN_INVALID_PARAMETER;
202 }
203
204 if ((inputsInfo == nullptr) || (inputSize == 0)) {
205 LOGE("OH_NNModel_SetInputsAndOutputsInfo failed, inputsInfo is empty.");
206 return OH_NN_INVALID_PARAMETER;
207 }
208
209 if ((outputsInfo == nullptr) || (outputSize == 0)) {
210 LOGE("OH_NNModel_SetInputsAndOutputsInfo failed, outputsInfo is empty.");
211 return OH_NN_INVALID_PARAMETER;
212 }
213
214 InnerModel *innerModel = reinterpret_cast<InnerModel*>(model);
215 return innerModel->SetInputsAndOutputsInfo(inputsInfo, inputSize, outputsInfo, outputSize);
216 }
217
OH_NNModel_Destroy(OH_NNModel ** model)218 NNRT_API void OH_NNModel_Destroy(OH_NNModel **model)
219 {
220 if (model == nullptr) {
221 LOGW("OH_NNModel_Destroy has no effect, passed nullptr to model.");
222 return;
223 }
224
225 if (*model == nullptr) {
226 LOGW("OH_NNModel_Destroy has no effect, passed nullptr to *model.");
227 return;
228 }
229
230 InnerModel *innerModel = reinterpret_cast<InnerModel*>(*model);
231 delete innerModel;
232 *model = nullptr;
233 }
234
OH_NNModel_GetAvailableOperations(OH_NNModel * model,size_t deviceID,const bool ** isAvailable,uint32_t * opCount)235 NNRT_API OH_NN_ReturnCode OH_NNModel_GetAvailableOperations(OH_NNModel *model,
236 size_t deviceID,
237 const bool **isAvailable,
238 uint32_t *opCount)
239 {
240 if (model == nullptr) {
241 LOGE("OH_NNModel_GetAvailableOperations failed, passed nullptr to model.");
242 return OH_NN_INVALID_PARAMETER;
243 }
244
245 if (isAvailable == nullptr) {
246 LOGE("OH_NNModel_GetAvailableOperations failed, passed nullptr to isAvailable.");
247 return OH_NN_INVALID_PARAMETER;
248 }
249
250 if (*isAvailable != nullptr) {
251 LOGE("OH_NNModel_GetAvailableOperations failed, *isAvailable is not nullptr.");
252 return OH_NN_INVALID_PARAMETER;
253 }
254
255 if (opCount == nullptr) {
256 LOGE("OH_NNModel_GetAvailableOperations failed, passed nullptr to opCount.");
257 return OH_NN_INVALID_PARAMETER;
258 }
259
260 InnerModel *innerModel = reinterpret_cast<InnerModel*>(model);
261 return innerModel->GetSupportedOperations(deviceID, isAvailable, *opCount);
262 }
263
OH_NNCompilation_Construct(const OH_NNModel * model)264 NNRT_API OH_NNCompilation *OH_NNCompilation_Construct(const OH_NNModel *model)
265 {
266 if (model == nullptr) {
267 LOGE("OH_NNCompilation_Construct failed, passed nullptr to model.");
268 return nullptr;
269 }
270 const InnerModel *innerModel = reinterpret_cast<const InnerModel*>(model);
271
272 if (!innerModel->IsBuild()) {
273 LOGE("OH_NNCompilation_Construct failed, should call OH_NNModel_Finish before creating compilation.");
274 return nullptr;
275 }
276
277 Compilation *compilation = new(std::nothrow) Compilation(innerModel);
278 if (compilation == nullptr) {
279 LOGE("OH_NNCompilation_Construct failed, please check whether it has enough memory.");
280 return nullptr;
281 }
282
283 OH_NNCompilation* nnCompilation = reinterpret_cast<OH_NNCompilation*>(compilation);
284 return nnCompilation;
285 }
286
OH_NNCompilation_SetDevice(OH_NNCompilation * compilation,size_t deviceID)287 NNRT_API OH_NN_ReturnCode OH_NNCompilation_SetDevice(OH_NNCompilation *compilation, size_t deviceID)
288 {
289 if (compilation == nullptr) {
290 LOGE("OH_NNCompilation_SetDevice failed, passed nullptr to compilation.");
291 return OH_NN_INVALID_PARAMETER;
292 }
293
294 Compilation* innerCompilation = reinterpret_cast<Compilation*>(compilation);
295 return innerCompilation->SetDevice(deviceID);
296 }
297
OH_NNCompilation_SetCache(OH_NNCompilation * compilation,const char * cachePath,uint32_t version)298 NNRT_API OH_NN_ReturnCode OH_NNCompilation_SetCache(OH_NNCompilation *compilation,
299 const char *cachePath,
300 uint32_t version)
301 {
302 if (compilation == nullptr) {
303 LOGE("OH_NNCompilation_SetCache failed, passed nullptr to compilation.");
304 return OH_NN_INVALID_PARAMETER;
305 }
306
307 if (cachePath == nullptr) {
308 LOGE("OH_NNCompilation_SetCache failed, passed nullptr to cachePath.");
309 return OH_NN_INVALID_PARAMETER;
310 }
311
312 Compilation* innerCompilation = reinterpret_cast<Compilation*>(compilation);
313 return innerCompilation->SetCacheDir(cachePath, version);
314 }
315
OH_NNCompilation_SetPerformanceMode(OH_NNCompilation * compilation,OH_NN_PerformanceMode performanceMode)316 NNRT_API OH_NN_ReturnCode OH_NNCompilation_SetPerformanceMode(OH_NNCompilation *compilation,
317 OH_NN_PerformanceMode performanceMode)
318 {
319 if (compilation == nullptr) {
320 LOGE("OH_NNCompilation_SetPerformanceMode failed, passed nullptr to compilation.");
321 return OH_NN_INVALID_PARAMETER;
322 }
323
324 Compilation* innerCompilation = reinterpret_cast<Compilation*>(compilation);
325 return innerCompilation->SetPerformance(performanceMode);
326 }
327
OH_NNCompilation_SetPriority(OH_NNCompilation * compilation,OH_NN_Priority priority)328 NNRT_API OH_NN_ReturnCode OH_NNCompilation_SetPriority(OH_NNCompilation *compilation,
329 OH_NN_Priority priority)
330 {
331 if (compilation == nullptr) {
332 LOGE("OH_NNCompilation_SetPriority failed, passed nullptr to compilation.");
333 return OH_NN_INVALID_PARAMETER;
334 }
335
336 Compilation* innerCompilation = reinterpret_cast<Compilation*>(compilation);
337 return innerCompilation->SetPriority(priority);
338 }
339
OH_NNCompilation_EnableFloat16(OH_NNCompilation * compilation,bool enableFloat16)340 NNRT_API OH_NN_ReturnCode OH_NNCompilation_EnableFloat16(OH_NNCompilation *compilation, bool enableFloat16)
341 {
342 if (compilation == nullptr) {
343 LOGE("OH_NNCompilation_EnableFloat16 failed, passed nullptr to compilation.");
344 return OH_NN_INVALID_PARAMETER;
345 }
346
347 Compilation* innerCompilation = reinterpret_cast<Compilation*>(compilation);
348 return innerCompilation->SetEnableFp16(enableFloat16);
349 }
350
OH_NNCompilation_Build(OH_NNCompilation * compilation)351 NNRT_API OH_NN_ReturnCode OH_NNCompilation_Build(OH_NNCompilation *compilation)
352 {
353 if (compilation == nullptr) {
354 LOGE("OH_NNCompilation_Build failed, passed nullptr to compilation.");
355 return OH_NN_INVALID_PARAMETER;
356 }
357
358 Compilation* innerCompilation = reinterpret_cast<Compilation*>(compilation);
359 return innerCompilation->Build();
360 }
361
OH_NNCompilation_Destroy(OH_NNCompilation ** compilation)362 NNRT_API void OH_NNCompilation_Destroy(OH_NNCompilation **compilation)
363 {
364 if (compilation == nullptr) {
365 LOGW("OH_NNCompilation_Destroy has no effect, passed nullptr to compilation.");
366 return;
367 }
368
369 if (*compilation == nullptr) {
370 LOGW("OH_NNCompilation_Destroy has no effect, passed nullptr to *compilation.");
371 return;
372 }
373
374 Compilation *innerCompilation = reinterpret_cast<Compilation*>(*compilation);
375 delete innerCompilation;
376 *compilation = nullptr;
377 }
378
OH_NNExecutor_Construct(OH_NNCompilation * compilation)379 NNRT_API OH_NNExecutor *OH_NNExecutor_Construct(OH_NNCompilation *compilation)
380 {
381 if (compilation == nullptr) {
382 LOGE("OH_NNExecutor_Construct failed, passed nullptr to compilation.");
383 return nullptr;
384 }
385 Compilation *innerCompilation = reinterpret_cast<Compilation*>(compilation);
386
387 if (!innerCompilation->IsBuild()) {
388 LOGE("OH_NNExecutor_Construct failed, should call OH_NNCompilation_Build before creating executor.");
389 return nullptr;
390 }
391
392 Executor* executor = new(std::nothrow) Executor(innerCompilation);
393 if (executor == nullptr) {
394 LOGE("OH_NNExecutor_Construct failed, please check whether it has enough memory.");
395 return nullptr;
396 }
397
398 OH_NNExecutor* nnExecutor = reinterpret_cast<OH_NNExecutor*>(executor);
399 return nnExecutor;
400 }
401
OH_NNExecutor_SetInput(OH_NNExecutor * executor,uint32_t inputIndex,const OH_NN_Tensor * tensor,const void * dataBuffer,size_t length)402 NNRT_API OH_NN_ReturnCode OH_NNExecutor_SetInput(OH_NNExecutor *executor,
403 uint32_t inputIndex,
404 const OH_NN_Tensor *tensor,
405 const void *dataBuffer,
406 size_t length)
407 {
408 if (executor == nullptr) {
409 LOGE("OH_NNExecutor_SetInput failed, passed nullptr to executor.");
410 return OH_NN_INVALID_PARAMETER;
411 }
412
413 if (tensor == nullptr) {
414 LOGE("OH_NNExecutor_SetInput failed, passed nullptr to tensor.");
415 return OH_NN_INVALID_PARAMETER;
416 }
417
418 if (dataBuffer == nullptr) {
419 LOGE("OH_NNExecutor_SetInput failed, passed nullptr to dataBuffer.");
420 return OH_NN_INVALID_PARAMETER;
421 }
422
423 if (length == 0) {
424 LOGE("OH_NNExecutor_SetInput failed, dataBuffer length is 0.");
425 return OH_NN_INVALID_PARAMETER;
426 }
427
428 Executor* innerExecutor = reinterpret_cast<Executor*>(executor);
429 return innerExecutor->SetInput(inputIndex, *tensor, dataBuffer, length);
430 }
431
OH_NNExecutor_SetOutput(OH_NNExecutor * executor,uint32_t outputIndex,void * dataBuffer,size_t length)432 NNRT_API OH_NN_ReturnCode OH_NNExecutor_SetOutput(OH_NNExecutor *executor,
433 uint32_t outputIndex,
434 void *dataBuffer,
435 size_t length)
436 {
437 if (executor == nullptr) {
438 LOGE("OH_NNExecutor_SetOutput failed, passed nullptr to executor.");
439 return OH_NN_INVALID_PARAMETER;
440 }
441
442 if (dataBuffer == nullptr) {
443 LOGE("OH_NNExecutor_SetOutput failed, passed nullptr to dataBuffer.");
444 return OH_NN_INVALID_PARAMETER;
445 }
446
447 if (length == 0) {
448 LOGE("OH_NNExecutor_SetOutput failed, dataBuffer length is 0.");
449 return OH_NN_INVALID_PARAMETER;
450 }
451
452 Executor* innerExecutor = reinterpret_cast<Executor*>(executor);
453 return innerExecutor->SetOutput(outputIndex, dataBuffer, length);
454 }
455
OH_NNExecutor_GetOutputShape(OH_NNExecutor * executor,uint32_t outputIndex,int32_t ** shape,uint32_t * shapeLength)456 NNRT_API OH_NN_ReturnCode OH_NNExecutor_GetOutputShape(OH_NNExecutor *executor,
457 uint32_t outputIndex,
458 int32_t **shape,
459 uint32_t *shapeLength)
460 {
461 if (executor == nullptr) {
462 LOGE("OH_NNExecutor_GetOutputShape failed, passed nullptr to executor.");
463 return OH_NN_INVALID_PARAMETER;
464 }
465
466 if (shape == nullptr) {
467 LOGE("OH_NNExecutor_GetOutputShape failed, passed nullptr to shape.");
468 return OH_NN_INVALID_PARAMETER;
469 }
470
471 if (*shape != nullptr) {
472 LOGE("OH_NNExecutor_GetOutputShape failed, *shape is not nullptr.");
473 return OH_NN_INVALID_PARAMETER;
474 }
475
476 if (shapeLength == nullptr) {
477 LOGE("OH_NNExecutor_GetOutputShape failed, passed nullptr to shapeLength.");
478 return OH_NN_INVALID_PARAMETER;
479 }
480
481 Executor* innerExecutor = reinterpret_cast<Executor*>(executor);
482 return innerExecutor->GetOutputShape(outputIndex, shape, *shapeLength);
483 }
484
OH_NNExecutor_Run(OH_NNExecutor * executor)485 NNRT_API OH_NN_ReturnCode OH_NNExecutor_Run(OH_NNExecutor *executor)
486 {
487 if (executor == nullptr) {
488 LOGE("OH_NNExecutor_Run failed, passed nullptr to executor.");
489 return OH_NN_INVALID_PARAMETER;
490 }
491
492 Executor *innerExecutor = reinterpret_cast<Executor*>(executor);
493 return innerExecutor->Run();
494 }
495
OH_NNExecutor_AllocateInputMemory(OH_NNExecutor * executor,uint32_t inputIndex,size_t length)496 NNRT_API OH_NN_Memory *OH_NNExecutor_AllocateInputMemory(OH_NNExecutor *executor, uint32_t inputIndex, size_t length)
497 {
498 if (executor == nullptr) {
499 LOGE("OH_NNExecutor_AllocateInputMemory failed, passed nullptr to executor.");
500 return nullptr;
501 }
502
503 if (length == 0) {
504 LOGW("OH_NNExecutor_AllocateInputMemory has no effect, passed length equals 0.");
505 return nullptr;
506 }
507
508 OH_NN_Memory *nnMemory = nullptr;
509 Executor *innerExecutor = reinterpret_cast<Executor*>(executor);
510 OH_NN_ReturnCode ret = innerExecutor->CreateInputMemory(inputIndex, length, &nnMemory);
511 if (ret != OH_NN_SUCCESS) {
512 LOGE("OH_NNExecutor_AllocateInputMemory failed, error happened when creating input memory in executor.");
513 return nullptr;
514 }
515
516 return nnMemory;
517 }
518
OH_NNExecutor_AllocateOutputMemory(OH_NNExecutor * executor,uint32_t outputIndex,size_t length)519 NNRT_API OH_NN_Memory *OH_NNExecutor_AllocateOutputMemory(OH_NNExecutor *executor, uint32_t outputIndex, size_t length)
520 {
521 if (executor == nullptr) {
522 LOGE("OH_NNExecutor_AllocateOutputMemory failed, passed nullptr to executor.");
523 return nullptr;
524 }
525
526 if (length == 0) {
527 LOGW("OH_NNExecutor_AllocateOutputMemory has no effect, passed length equals 0.");
528 return nullptr;
529 }
530
531 OH_NN_Memory *nnMemory = nullptr;
532 Executor *innerExecutor = reinterpret_cast<Executor*>(executor);
533 OH_NN_ReturnCode ret = innerExecutor->CreateOutputMemory(outputIndex, length, &nnMemory);
534 if (ret != OH_NN_SUCCESS) {
535 LOGE("OH_NNExecutor_AllocateOutputMemory failed, error happened when creating output memory in executor.");
536 return nullptr;
537 }
538
539 return nnMemory;
540 }
541
OH_NNExecutor_DestroyInputMemory(OH_NNExecutor * executor,uint32_t inputIndex,OH_NN_Memory ** memory)542 NNRT_API void OH_NNExecutor_DestroyInputMemory(OH_NNExecutor *executor, uint32_t inputIndex, OH_NN_Memory **memory)
543 {
544 if (executor == nullptr) {
545 LOGE("OH_NNExecutor_DestroyInputMemory failed, passed nullptr to executor.");
546 return;
547 }
548
549 if (memory == nullptr) {
550 LOGW("OH_NNExecutor_DestroyInputMemory has no effect, passed nullptr to memory.");
551 return;
552 }
553
554 if (*memory == nullptr) {
555 LOGW("OH_NNExecutor_DestroyInputMemory has no effect, passed nullptr to *memory.");
556 return;
557 }
558
559 Executor *innerExecutor = reinterpret_cast<Executor*>(executor);
560 OH_NN_ReturnCode ret = innerExecutor->DestroyInputMemory(inputIndex, memory);
561 if (ret != OH_NN_SUCCESS) {
562 LOGE("OH_NNExecutor_DestroyInputMemory failed, error happened when destroying input memory.");
563 return;
564 }
565
566 *memory = nullptr;
567 }
568
OH_NNExecutor_DestroyOutputMemory(OH_NNExecutor * executor,uint32_t outputIndex,OH_NN_Memory ** memory)569 NNRT_API void OH_NNExecutor_DestroyOutputMemory(OH_NNExecutor *executor, uint32_t outputIndex, OH_NN_Memory **memory)
570 {
571 if (executor == nullptr) {
572 LOGE("OH_NNExecutor_DestroyOutputMemory failed, passed nullptr to executor.");
573 return;
574 }
575
576 if (memory == nullptr) {
577 LOGW("OH_NNExecutor_DestroyOutputMemory has no effect, passed nullptr to memory.");
578 return;
579 }
580
581 if (*memory == nullptr) {
582 LOGW("OH_NNExecutor_DestroyOutputMemory has no effect, passed nullptr to *memory.");
583 return;
584 }
585
586 Executor *innerExecutor = reinterpret_cast<Executor*>(executor);
587 OH_NN_ReturnCode ret = innerExecutor->DestroyOutputMemory(outputIndex, memory);
588 if (ret != OH_NN_SUCCESS) {
589 LOGE("OH_NNExecutor_DestroyOutputMemory failed, error happened when destroying output memory.");
590 return;
591 }
592
593 *memory = nullptr;
594 }
595
OH_NNExecutor_SetInputWithMemory(OH_NNExecutor * executor,uint32_t inputIndex,const OH_NN_Tensor * tensor,const OH_NN_Memory * memory)596 NNRT_API OH_NN_ReturnCode OH_NNExecutor_SetInputWithMemory(OH_NNExecutor *executor,
597 uint32_t inputIndex,
598 const OH_NN_Tensor *tensor,
599 const OH_NN_Memory *memory)
600 {
601 if (executor == nullptr) {
602 LOGE("OH_NNExecutor_SetInputWithMemory failed, passed nullptr to executor.");
603 return OH_NN_INVALID_PARAMETER;
604 }
605
606 if (tensor == nullptr) {
607 LOGE("OH_NNExecutor_SetInputWithMemory failed, passed nullptr to tensor.");
608 return OH_NN_INVALID_PARAMETER;
609 }
610
611 if (memory == nullptr) {
612 LOGE("OH_NNExecutor_SetInputWithMemory failed, passed nullptr to memory.");
613 return OH_NN_INVALID_PARAMETER;
614 }
615
616 Executor *innerExecutor = reinterpret_cast<Executor*>(executor);
617 return innerExecutor->SetInputFromMemory(inputIndex, *tensor, *memory);
618 }
619
OH_NNExecutor_SetOutputWithMemory(OH_NNExecutor * executor,uint32_t outputIndex,const OH_NN_Memory * memory)620 NNRT_API OH_NN_ReturnCode OH_NNExecutor_SetOutputWithMemory(OH_NNExecutor *executor,
621 uint32_t outputIndex,
622 const OH_NN_Memory *memory)
623 {
624 if (executor == nullptr) {
625 LOGE("OH_NNExecutor_SetOutputWithMemory failed, passed nullptr to executor.");
626 return OH_NN_INVALID_PARAMETER;
627 }
628
629 if (memory == nullptr) {
630 LOGE("OH_NNExecutor_SetOutputWithMemory failed, passed nullptr to memory.");
631 return OH_NN_INVALID_PARAMETER;
632 }
633
634 Executor *innerExecutor = reinterpret_cast<Executor*>(executor);
635 return innerExecutor->SetOutputFromMemory(outputIndex, *memory);
636 }
637
OH_NNExecutor_Destroy(OH_NNExecutor ** executor)638 NNRT_API void OH_NNExecutor_Destroy(OH_NNExecutor **executor)
639 {
640 if (executor == nullptr) {
641 LOGW("OH_NNExecutor_Destroy has no effect, since executor is nullptr.");
642 return;
643 }
644
645 if ((*executor) == nullptr) {
646 LOGW("OH_NNExecutor_Destroy has no effect, since *executor is nullptr");
647 return;
648 }
649
650 Executor *innerExecutor = reinterpret_cast<Executor*>(*executor);
651 delete innerExecutor;
652 *executor = nullptr;
653 }
654
OH_NNDevice_GetAllDevicesID(const size_t ** allDevicesID,uint32_t * deviceCount)655 NNRT_API OH_NN_ReturnCode OH_NNDevice_GetAllDevicesID(const size_t **allDevicesID, uint32_t *deviceCount)
656 {
657 if (allDevicesID == nullptr) {
658 LOGE("OH_NNDevice_GetAllDevicesID failed, passed nullptr to allDevicesID.");
659 return OH_NN_INVALID_PARAMETER;
660 }
661
662 if ((*allDevicesID) != nullptr) {
663 LOGE("OH_NNDevice_GetAllDevicesID failed, *allDevicesID should be nullptr.");
664 return OH_NN_INVALID_PARAMETER;
665 }
666
667 if (deviceCount == nullptr) {
668 LOGE("OH_NNDevice_GetAllDevicesID failed, passed nullptr to deviceCount.");
669 return OH_NN_INVALID_PARAMETER;
670 }
671
672 DeviceManager& deviceManager = DeviceManager::GetInstance();
673 const std::vector<size_t>& allDevices = deviceManager.GetAllDeviceId();
674
675 if (allDevices.empty()) {
676 LOGW("OH_NNDevice_GetAllDevicesID got no device.");
677 *allDevicesID = nullptr;
678 *deviceCount = 0;
679 return OH_NN_SUCCESS;
680 }
681
682 *allDevicesID = allDevices.data();
683 // allDevices.size() will not exceed UINT32_MAX, it is safe to cast to uint32_t.
684 *deviceCount = static_cast<uint32_t>(allDevices.size());
685
686 return OH_NN_SUCCESS;
687 }
688
OH_NNDevice_GetName(size_t deviceID,const char ** name)689 NNRT_API OH_NN_ReturnCode OH_NNDevice_GetName(size_t deviceID, const char **name)
690 {
691 if (name == nullptr) {
692 LOGE("OH_NNDevice_GetName failed, passed nullptr to name.");
693 return OH_NN_INVALID_PARAMETER;
694 }
695
696 if ((*name) != nullptr) {
697 LOGE("OH_NNDevice_GetName failed, *name should be nullptr.");
698 return OH_NN_INVALID_PARAMETER;
699 }
700
701 DeviceManager& deviceManager = DeviceManager::GetInstance();
702 const std::string& deviceName = deviceManager.GetDeviceName(deviceID);
703 if (deviceName.empty()) {
704 LOGE("OH_NNDevice_GetName failed, error happened when getting name of deviceID %zu.", deviceID);
705 *name = nullptr;
706 return OH_NN_FAILED;
707 }
708
709 *name = deviceName.data();
710 return OH_NN_SUCCESS;
711 }
712
OH_NNDevice_GetType(size_t deviceID,OH_NN_DeviceType * deviceType)713 NNRT_API OH_NN_ReturnCode OH_NNDevice_GetType(size_t deviceID, OH_NN_DeviceType* deviceType)
714 {
715 DeviceManager& deviceManager = DeviceManager::GetInstance();
716 std::shared_ptr<Device> device = deviceManager.GetDevice(deviceID);
717 if (device == nullptr) {
718 LOGE("OH_NNDevice_GetName failed, passed invalid deviceID.");
719 return OH_NN_INVALID_PARAMETER;
720 }
721
722 if (deviceType == nullptr) {
723 LOGE("OH_NNDevice_GetType failed, passed nullptr to deviceType.");
724 return OH_NN_INVALID_PARAMETER;
725 }
726
727 OH_NN_ReturnCode ret = device->GetDeviceType(*deviceType);
728 if (ret != OH_NN_SUCCESS) {
729 LOGE("OH_NNDevice_GetType failed, device id: %zu.", deviceID);
730 return ret;
731 }
732 return OH_NN_SUCCESS;
733 }