• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# neural_network_core.h
2<!--Kit: Neural Network Runtime Kit-->
3<!--Subsystem: AI-->
4<!--Owner: @GbuzhidaoR-->
5<!--Designer: @GbuzhidaoR-->
6<!--Tester: @GbuzhidaoR-->
7<!--Adviser: @ge-yafang-->
8## 概述
9
10Neural Network Core模块接口定义,AI推理框架使用Neural Network Core提供的Native接口,完成模型编译,并在加速硬件上执行推理和计算。
11
12部分接口定义从neural_network_runtime.h移动至此头文件统一呈现,对于此类接口,API version 11 版本之前即支持使用,各版本均可正常使用。
13
14Neural Network Core的接口目前均不支持多线程并发调用。
15
16**引用文件:** <neural_network_runtime/neural_network_core.h>
17
18**库:** libneural_network_core.so
19
20**系统能力:** SystemCapability.Ai.NeuralNetworkRuntime
21
22**起始版本:** 11
23
24**相关模块:** [NeuralNetworkRuntime](capi-neuralnetworkruntime.md)
25
26## 汇总
27
28### 函数
29
30| 名称                                                         | 描述                                                         |
31| ------------------------------------------------------------ | ------------------------------------------------------------ |
32| [OH_NNCompilation *OH_NNCompilation_Construct(const OH_NNModel *model)](#oh_nncompilation_construct) | 创建[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)类型的编译实例。<br>使用OH_NNModel模块完成模型的构造后,借助OH_NNCompilation模块提供的接口,将模型传递到底层硬件完成编译。 |
33| [OH_NNCompilation *OH_NNCompilation_ConstructWithOfflineModelFile(const char *modelPath)](#oh_nncompilation_constructwithofflinemodelfile) | 基于离线模型文件创建编译实例。<br>该接口与传递在线构建模型或离线模型文件内存的方式冲突,您只能选择三种构建接口中的一种。<br>离线模型是由硬件供应商提供的模型转换器离线编译的模型类型,所以离线模型只能在指定的设备上使用,但离线模型的编译时间通常远小于构图实例[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)的编译时间。<br>在开发过程中需要离线执行编译,并在应用包中部署离线模型。 |
34| [OH_NNCompilation *OH_NNCompilation_ConstructWithOfflineModelBuffer(const void *modelBuffer, size_t modelSize)](#oh_nncompilation_constructwithofflinemodelbuffer) | 基于离线模型文件内存创建编译实例。<br>该接口与传递在线构建模型或离线模型文件路径的方式冲突,您只能选择三种构建接口中的一种。<br>返回的[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例只将<b>modelBuffer</b>指针保存在里面,而不是复制其数据。在销毁[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例之前,不应释放<b>modelBuffer</b>。 |
35| [OH_NNCompilation *OH_NNCompilation_ConstructForCache()](#oh_nncompilation_constructforcache) | 创建一个空的编译实例,以便稍后从模型缓存中恢复。<br>模型缓存的相关描述参考[OH_NNCompilation_SetCache](capi-neural-network-core-h.md#oh_nncompilation_setcache)。<br>从模型缓存恢复的时间少于使用[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)进行编译的时间。<br>应该先调用[OH_NNCompilation_SetCache](capi-neural-network-core-h.md#oh_nncompilation_setcache)或[OH_NNCompilation_ImportCacheFromBuffer](capi-neural-network-core-h.md#oh_nncompilation_importcachefrombuffer),然后调用[OH_NNCompilation_Build](capi-neural-network-core-h.md#oh_nncompilation_build)完成恢复。 |
36| [OH_NN_ReturnCode OH_NNCompilation_ExportCacheToBuffer(OH_NNCompilation *compilation,const void *buffer,size_t length,size_t *modelSize)](#oh_nncompilation_exportcachetobuffer) | 将模型缓存写入到指定内存区域。<br>模型缓存的相关描述参考[OH_NNCompilation_SetCache](capi-neural-network-core-h.md#oh_nncompilation_setcache)。<br>模型缓存是编译构建的结果[OH_NNCompilation_Build](capi-neural-network-core-h.md#oh_nncompilation_build),因此必须在[OH_NNCompilation_Build](capi-neural-network-core-h.md#oh_nncompilation_build)之后调用该接口。 |
37| [OH_NN_ReturnCode OH_NNCompilation_ImportCacheFromBuffer(OH_NNCompilation *compilation,const void *buffer,size_t modelSize)](#oh_nncompilation_importcachefrombuffer) | 从指定内存区域读取模型缓存。<br>模型缓存的相关描述参考[OH_NNCompilation_SetCache](capi-neural-network-core-h.md#oh_nncompilation_setcache)。<br>调用[OH_NNCompilation_ImportCacheFromBuffer](capi-neural-network-core-h.md#oh_nncompilation_importcachefrombuffer)后,应调用[OH_NNCompilation_Build](capi-neural-network-core-h.md#oh_nncompilation_build)完成恢复。<br><b>compilation</b>只将<b>buffer</b>指针保存在里面,而不是复制其数据。您不能在<b>compilation</b>被销毁之前释放内存<b>buffer</b>。 |
38| [OH_NN_ReturnCode OH_NNCompilation_AddExtensionConfig(OH_NNCompilation *compilation,const char *configName,const void *configValue,const size_t configValueSize)](#oh_nncompilation_addextensionconfig) | 为自定义硬件属性添加扩展配置。<br>某些设备有自己的特定属性,这些属性尚未在NNRt中打开。该接口为您提供了另一种方式设置设备的这些自定义硬件属性。<br>您应该从设备供应商的文档查询它们的名称和值,并将它们逐一添加到编译实例中。这些属性将直接传递给设备驱动程序,如果驱动程序无法解析它们,该接口将返回错误码。<br>调用[OH_NNCompilation_Build](capi-neural-network-core-h.md#oh_nncompilation_build)后,<b>configName</b>和<b>configValue</b>就可以释放了。 |
39| [OH_NN_ReturnCode OH_NNCompilation_SetDevice(OH_NNCompilation *compilation, size_t deviceID)](#oh_nncompilation_setdevice) | 指定模型编译和计算的硬件。<br>编译阶段,需要指定模型编译和执行计算的硬件设备。先调用[OH_NNDevice_GetAllDevicesID](capi-neural-network-core-h.md#oh_nndevice_getalldevicesid)获取可用的设备ID,通过[OH_NNDevice_GetType](capi-neural-network-core-h.md#oh_nndevice_gettype)和[OH_NNDevice_GetType](capi-neural-network-core-h.md#oh_nndevice_gettype)获取设备信息后,将期望编译执行的设备ID传入该接口进行设置。 |
40| [OH_NN_ReturnCode OH_NNCompilation_SetCache(OH_NNCompilation *compilation, const char *cachePath, uint32_t version)](#oh_nncompilation_setcache) | 设置编译模型的缓存目录和版本。                               |
41| [OH_NN_ReturnCode OH_NNCompilation_SetPerformanceMode(OH_NNCompilation *compilation,OH_NN_PerformanceMode performanceMode)](#oh_nncompilation_setperformancemode) | 设置模型计算的性能模式。<br>Neural Network Runtime 支持为模型计算设置性能模式,满足低功耗到极致性能的需求。如果编译阶段没有调用该接口设置性能模式,编译实例为模型默认分配[OH_NN_PERFORMANCE_NONE](capi-neural-network-runtime-type-h.md#oh_nn_performancemode)模式。在[OH_NN_PERFORMANCE_NONE](capi-neural-network-runtime-type-h.md#oh_nn_performancemode)模式下,硬件按默认的性能模式执行计算。 在不支持性能模式设置的硬件上调用该接口,将返回[OH_NN_UNAVAILABLE_DEVICE](capi-neural-network-runtime-type-h.md#oh_nn_returncode)错误码。 |
42| [OH_NN_ReturnCode OH_NNCompilation_SetPriority(OH_NNCompilation *compilation, OH_NN_Priority priority)](#oh_nncompilation_setpriority) | 设置模型计算的优先级。<br>Neural Network Runtime 支持为模型设置计算优先级,优先级仅作用于相同uid进程创建的模型,不同uid进程、不同设备的优先级不会相互影响。 在不支持优先级设置的硬件上调用该接口,将返回[OH_NN_UNAVAILABLE_DEVICE](capi-neural-network-runtime-type-h.md#oh_nn_returncode)错误码。 |
43| [OH_NN_ReturnCode OH_NNCompilation_EnableFloat16(OH_NNCompilation *compilation, bool enableFloat16)](#oh_nncompilation_enablefloat16) | 是否以float16的浮点数精度计算。<br>浮点模型默认使用float32精度计算。如果在支持float16精度的硬件上调用该接口,float32浮点数精度的模型将以float16的精度执行计算,可减少内存占用和执行时间。 该选项对于定点模型是无效的,例如int8类型的定点模型。<br>在不支持float16精度计算的硬件上调用该接口,将返回[OH_NN_UNAVAILABLE_DEVICE](capi-neural-network-runtime-type-h.md#oh_nn_returncode)错误码。 |
44| [OH_NN_ReturnCode OH_NNCompilation_Build(OH_NNCompilation *compilation)](#oh_nncompilation_build) | 执行模型编译。<br>完成编译配置后,调用该接口执行模型编译。编译实例将模型和编译选项推送至硬件设备进行编译。<br>在调用该接口后,无法进行额外的编译操作,调用[OH_NNCompilation_SetDevice](capi-neural-network-core-h.md#oh_nncompilation_setdevice)、[OH_NNCompilation_SetCache](capi-neural-network-core-h.md#oh_nncompilation_setcache)、[OH_NNCompilation_SetPerformanceMode](capi-neural-network-core-h.md#oh_nncompilation_setperformancemode)、[OH_NNCompilation_SetPriority](capi-neural-network-core-h.md#oh_nncompilation_setpriority)和[OH_NNCompilation_EnableFloat16](capi-neural-network-core-h.md#oh_nncompilation_enablefloat16)接口将返回[OH_NN_OPERATION_FORBIDDEN](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
45| [void OH_NNCompilation_Destroy(OH_NNCompilation **compilation)](#oh_nncompilation_destroy) | 销毁Compilation实例。<br>调用[OH_NNCompilation_Construct](capi-neural-network-core-h.md#oh_nncompilation_construct)、[OH_NNCompilation_ConstructWithOfflineModelFile](capi-neural-network-core-h.md#oh_nncompilation_constructwithofflinemodelfile)、[OH_NNCompilation_ConstructWithOfflineModelBuffer](capi-neural-network-core-h.md#oh_nncompilation_constructwithofflinemodelbuffer)、[OH_NNCompilation_ConstructForCache](capi-neural-network-core-h.md#oh_nncompilation_constructforcache)创建的编译实例需要调用该接口主动销毁。 如果compilation为空指针或者*compilation为空指针,该接口仅打印警告日志,不执行销毁操作。 |
46| [NN_TensorDesc *OH_NNTensorDesc_Create()](#oh_nntensordesc_create) | 创建一个[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例。<br>[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)描述了各种张量属性,如名称/数据类型/形状/格式等。 <br>可以调用以下接口,基于传入的[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例创建[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例:<br>[OH_NNTensor_Create](capi-neural-network-core-h.md#oh_nntensor_create) <br>[OH_NNTensor_CreateWithSize](capi-neural-network-core-h.md#oh_nntensor_createwithsize) <br>[OH_NNTensor_CreateWithFd](capi-neural-network-core-h.md#oh_nntensor_createwithfd) <br>该接口会将[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例复制到[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)中,因此您可以创建多个[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)个实例,并持有相同的[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例。当[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例不再使用时,您应该调用[OH_NNTensorDesc_Destroy](capi-neural-network-core-h.md#oh_nntensordesc_destroy)接口销毁它。 |
47| [OH_NN_ReturnCode OH_NNTensorDesc_Destroy(NN_TensorDesc **tensorDesc)](#oh_nntensordesc_destroy) | 释放一个[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例。<br>当[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例不再使用时,需要调用该接口销毁,否则将发生内存泄漏。<br>如果<b>tensorDesc</b>或<b>*tensorDesc</b>为空指针,则该接口将返回错误码,并且不会执行销毁操作。 |
48| [OH_NN_ReturnCode OH_NNTensorDesc_SetName(NN_TensorDesc *tensorDesc, const char *name)](#oh_nntensordesc_setname) | 设置[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的名称。<br>[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例创建完成后,调用该接口设置张量的名称,<b>*name</b>的值是以<b>'\0'</b>结尾的C风格字符串。<br>如果<b>tensorDesc</b>或<b>name</b>为空指针,则该接口将返回错误码。 |
49| [OH_NN_ReturnCode OH_NNTensorDesc_GetName(const NN_TensorDesc *tensorDesc, const char **name)](#oh_nntensordesc_getname) | 获取[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的名称。<br>调用该接口获取指定[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的名称,**\*name**的值是以**'\0'**结尾的C风格字符串。<br>如果<b>tensorDesc</b>或<b>name</b>为空指针,则该接口将返回错误码。作为输出参数,\*name必须为空指针,否则该接口将返回错误码。<br>例如您应该定义char* tensorName = NULL,并传递&tensorName作为name的参数。<br>您不需要释放<b>name</b>的内存,当<b>tensorDesc</b>被销毁时,它会被自动释放。 |
50| [OH_NN_ReturnCode OH_NNTensorDesc_SetDataType(NN_TensorDesc *tensorDesc, OH_NN_DataType dataType)](#oh_nntensordesc_setdatatype) | 设置[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的数据类型。<br>[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例创建完成后,调用该接口设置张量数据类型。<br>如果<b>tensorDesc</b>为空指针,则该接口将返回错误码。 |
51| [OH_NN_ReturnCode OH_NNTensorDesc_GetDataType(const NN_TensorDesc *tensorDesc, OH_NN_DataType *dataType)](#oh_nntensordesc_getdatatype) | 获取[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的数据类型。<br>调用该接口获取指定[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的数据类型。<br>如果<b>tensorDesc</b>或<b>dataType</b>为空指针,则该接口将返回错误码。 |
52| [OH_NN_ReturnCode OH_NNTensorDesc_SetShape(NN_TensorDesc *tensorDesc, const int32_t *shape, size_t shapeLength)](#oh_nntensordesc_setshape) | 设置[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的数据形状。<br>[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例创建完成后,调用该接口设置张量形状。<br>如果<b>tensorDesc</b>或<b>shape</b>为空指针,或<b>shapeLength</b>为0,则该接口将返回错误码。 |
53| [OH_NN_ReturnCode OH_NNTensorDesc_GetShape(const NN_TensorDesc *tensorDesc, int32_t **shape, size_t *shapeLength)](#oh_nntensordesc_getshape) | 获取[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的形状。<br>调用该接口获取指定[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的形状。<br>如果<b>tensorDesc</b>、<b>shape</b>或<b>shapeLength</b>为空指针,则该接口将返回错误码。作为输出参数,\*shape必须为空指针,否则该接口将返回错误码。<br>例如您应该定义 int32_t* tensorShape = NULL,并传递&tensorShape作为<b>shape</b>的参数。<br>您不需要释放<b>shape</b>的内存。当<b>tensorDesc</b>被销毁时,它会自动释放。 |
54| [OH_NN_ReturnCode OH_NNTensorDesc_SetFormat(NN_TensorDesc *tensorDesc, OH_NN_Format format)](#oh_nntensordesc_setformat) | 设置[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的数据布局。<br>[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例创建完成后,调用该接口设置张量的数据布局[OH_NN_Format](capi-neural-network-runtime-type-h.md#oh_nn_format)。<br>如果<b>tensorDesc</b>为空指针,则该接口将返回错误码。 |
55| [OH_NN_ReturnCode OH_NNTensorDesc_GetFormat(const NN_TensorDesc *tensorDesc, OH_NN_Format *format)](#oh_nntensordesc_getformat) | 获取[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的数据布局。<br>调用该接口获取指定[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的数据布局[OH_NN_Format](capi-neural-network-runtime-type-h.md#oh_nn_format)。<br>如果<b>tensorDesc</b>或<b>format</b>为空指针,则该接口将返回错误码。 |
56| [OH_NN_ReturnCode OH_NNTensorDesc_GetElementCount(const NN_TensorDesc *tensorDesc, size_t *elementCount)](#oh_nntensordesc_getelementcount) | 获取[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的元素个数。<br>调用该接口获取指定[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的元素个数。如果需要获取张量数据的字节大小,请调用[OH_NNTensorDesc_GetByteSize](capi-neural-network-core-h.md#oh_nntensordesc_getbytesize)。<br>如果张量形状是动态可变的,则该接口将返回错误码,<b>elementCount</b>将为0。<br>如果<b>tensorDesc</b>或<b>elementCount</b>为空指针,则该接口将返回错误码。 |
57| [OH_NN_ReturnCode OH_NNTensorDesc_GetByteSize(const NN_TensorDesc *tensorDesc, size_t *byteSize)](#oh_nntensordesc_getbytesize) | 获取基于[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的形状和数据类型计算的数据占用字节数。<br>调用该接口可基于[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的形状和数据类型计算得到的数据占用字节数。<br>如果张量形状是动态可变的,该接口将返回错误码,<b>byteSize</b>将为0。<br>如果需要获取张量数据的元素个数,请调用[OH_NNTensorDesc_GetElementCount](capi-neural-network-core-h.md#oh_nntensordesc_getelementcount)。<br>如果<b>tensorDesc</b>或<b>byteSize</b>为空指针,则该接口将返回错误码。 |
58| [NN_Tensor *OH_NNTensor_Create(size_t deviceID, NN_TensorDesc *tensorDesc)](#oh_nntensor_create) | 从[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)创建一个[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例。<br>该接口使用[OH_NNTensorDesc_GetByteSize](capi-neural-network-core-h.md#oh_nntensordesc_getbytesize)计算张量数据的字节数,并为其分配设备内存。设备驱动将直接通过“零拷贝”方式获取张量数据。<br>该接口会将<b>tensorDesc</b>复制到[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)中,因此当<b>tensorDesc</b>不再使用时,您应该调用[OH_NNTensorDesc_Destroy](capi-neural-network-core-h.md#oh_nntensordesc_destroy)接口销毁它。<br>如果张量形状是动态的,该接口将返回错误码。<br><b>deviceID</b>表示所选设备。如果为0,则默认使用设备列表中的第1台设备。<br><b>必须提供tensorDesc</b>,如果它是空指针,则返回错误码。<br>当[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例不再使用时,需要调用[OH_NNTensor_Destroy](capi-neural-network-core-h.md#oh_nntensor_destroy)销毁它。 |
59| [NN_Tensor *OH_NNTensor_CreateWithSize(size_t deviceID, NN_TensorDesc *tensorDesc, size_t size)](#oh_nntensor_createwithsize) | 按照指定内存大小和[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)创建[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例。<br>该接口使用<b>size</b>作为张量数据的字节数,并为其分配设备内存。设备将直接通过“零拷贝”方式获取张量数据。<br>该接口会将<b>tensorDesc</b>复制到[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)中。因此当<b>tensorDesc</b>不再使用时,您应该调用[OH_NNTensorDesc_Destroy](capi-neural-network-core-h.md#oh_nntensordesc_destroy)接口销毁它。<br><b>deviceID</b>表示所选设备ID,如果为0,则使用第1台设备。<br><b>tensorDesc</b>必须提供,如果它是空指针,则该接口返回错误码。<br><b>size</b>必须不小于<b>tensorDesc</b>的数据占用字节数(可由[OH_NNTensorDesc_GetByteSize](capi-neural-network-core-h.md#oh_nntensordesc_getbytesize)获取),否则该接口将返回错误码。如果张量形状是动态的,不会检查<b>size</b>。<br>当[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例不再使用时,需要调用[OH_NNTensor_Destroy](capi-neural-network-core-h.md#oh_nntensor_destroy)销毁它。 |
60| [NN_Tensor *OH_NNTensor_CreateWithFd(size_t deviceID,NN_TensorDesc *tensorDesc,int fd,size_t size,size_t offset)](#oh_nntensor_createwithfd) | 按照指定共享内存的文件描述符和[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)创建[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例。<br>该接口复用文件描述符<b>fd</b>对应的共享内存,<b>fd</b>可能来自另一个[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例。当调用[OH_NNTensor_Destroy](capi-neural-network-core-h.md#oh_nntensor_destroy)接口销毁该接口创建的张量时,不会释放该张量数据的内存。<br>该接口会将<b>tensorDesc</b>复制到[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)中。因此当<b>tensorDesc</b>不再使用时,您应该调用[OH_NNTensorDesc_Destroy](capi-neural-network-core-h.md#oh_nntensordesc_destroy)接口销毁它。<br><b>deviceID</b>表示所选设备。如果为0,则默认使用当前设备列表中的第1台设备。<br>必须提供<b>tensorDesc</b>,如果为空指针,则该接口返回错误码。<br>当[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例不再使用时,需要调用[OH_NNTensor_Destroy](capi-neural-network-core-h.md#oh_nntensor_destroy)销毁它。 |
61| [OH_NN_ReturnCode OH_NNTensor_Destroy(NN_Tensor **tensor)](#oh_nntensor_destroy) | 销毁一个[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例。<br>当不再使用[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例时,需要调用该接口销毁该实例,否则将发生内存泄漏。<br>如果<b>tensor</b>或<b>*tensor</b>为空指针,则该接口将返回错误码,并且不执行销毁操作。 |
62| [NN_TensorDesc *OH_NNTensor_GetTensorDesc(const NN_Tensor *tensor)](#oh_nntensor_gettensordesc) | 获取[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)的[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例。<br>调用该接口获取指定[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例的内部[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例指针。<br>您可以从返回的[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例中获取各种类型的张量属性,例如名称/数据布局/数据类型/形状等。<br>您不应销毁返回的[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例,因为它指向了[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)的内部实例,否则一旦调用[OH_NNTensor_Destroy](capi-neural-network-core-h.md#oh_nntensor_destroy)将会发生双重释放的内存崩溃。<br>如果<b>Tensor</b>是空指针,则该接口将会返回空指针。 |
63| [void *OH_NNTensor_GetDataBuffer(const NN_Tensor *tensor)](#oh_nntensor_getdatabuffer) | 获取[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)数据的内存地址。<br>您可以从张量数据内存读取/写入数据。数据内存是从设备上的共享内存映射的,因此设备驱动可通过这种“零拷贝”方式直接获取张量数据。<br>张量数据仅能使用对应共享内存中的[offset, size)一段,其中offset是共享内存上的偏移量,可以通过[OH_NNTensor_GetOffset](capi-neural-network-core-h.md#oh_nntensor_getoffset)获取,而size是共享内存的总大小,可以通过[OH_NNTensor_GetSize](capi-neural-network-core-h.md#oh_nntensor_getsize)获取。 <br>如果<b>Tensor</b>是空指针,则该接口将会返回空指针。 |
64| [OH_NN_ReturnCode OH_NNTensor_GetFd(const NN_Tensor *tensor, int *fd)](#oh_nntensor_getfd) | 获取[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)数据所在共享内存的文件描述符。<br>文件描述符<b>fd</b>对应了一块设备共享内存,可以通过[OH_NNTensor_CreateWithFd](capi-neural-network-core-h.md#oh_nntensor_createwithfd)被另外一个[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)使用。 <br>如果<b>tensor</b>或<b>fd</b>为空指针,该接口将返回错误。 |
65| [OH_NN_ReturnCode OH_NNTensor_GetSize(const NN_Tensor *tensor, size_t *size)](#oh_nntensor_getsize) | 获取[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)数据所在共享内存的大小。<br><b>size</b>与接口[OH_NNTensor_CreateWithSize](capi-neural-network-core-h.md#oh_nntensor_createwithsize)和[OH_NNTensor_CreateWithFd](capi-neural-network-core-h.md#oh_nntensor_createwithfd)的参数<b>size</b>相同,但对于通过[OH_NNTensor_Create](capi-neural-network-core-h.md#oh_nntensor_create)创建的张量,<b>size</b>等于张量数据实际占用字节数(可由[OH_NNTensorDesc_GetByteSize](capi-neural-network-core-h.md#oh_nntensordesc_getbytesize)获取)。 <br>张量数据仅能使用文件描述符<b>fd</b>对应的共享内存中的[offset, size)一段,其中offset是共享内存上的偏移量,可以通过[OH_NNTensor_GetOffset](capi-neural-network-core-h.md#oh_nntensor_getoffset)获取,而size是共享内存的总大小,可以通过[OH_NNTensor_GetSize](capi-neural-network-core-h.md#oh_nntensor_getsize)获取。 <br>如果<b>tensor</b>或<b>size</b>为空指针,该接口将返回错误。 |
66| [OH_NN_ReturnCode OH_NNTensor_GetOffset(const NN_Tensor *tensor, size_t *offset)](#oh_nntensor_getoffset) | 获取[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)数据所在共享内存上的偏移量。<br><b>offset</b>是张量数据在对应共享内存上的偏移量,可以通过[OH_NNTensor_CreateWithFd](capi-neural-network-core-h.md#oh_nntensor_createwithfd)接口,连同共享内存文件描述符、共享内存总大小一起被另外的[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)使用。 <br>张量数据仅能使用文件描述符<b>fd</b>对应的共享内存中的[offset, size)一段,其中offset是共享内存上的偏移量,可以通过[OH_NNTensor_GetOffset](capi-neural-network-core-h.md#oh_nntensor_getoffset)获取,而size是共享内存的总大小,可以通过[OH_NNTensor_GetSize](capi-neural-network-core-h.md#oh_nntensor_getsize)获取。 <br>如果<b>tensor</b>或<b>offset</b>为空指针,该接口将返回错误。 |
67| [OH_NNExecutor *OH_NNExecutor_Construct(OH_NNCompilation *compilation)](#oh_nnexecutor_construct) | 创建[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)执行器实例。<br>该接口接受一个[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例,构造一个与硬件关联的模型推理执行器。通过[OH_NNExecutor_SetInput](capi-neural-network-runtime-h.md#oh_nnexecutor_setinput)设置模型输入数据,设置输入数据后,调用[OH_NNExecutor_Run](capi-neural-network-runtime-h.md#oh_nnexecutor_run)接口执行推理,最后通过[OH_NNExecutor_SetOutput](capi-neural-network-runtime-h.md#oh_nnexecutor_setoutput)获取计算结果。  通过[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例创建一个[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例后,如果不再使用[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例创建其他[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例,就可以销毁[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例了。 |
68| [OH_NN_ReturnCode OH_NNExecutor_GetOutputShape(OH_NNExecutor *executor,uint32_t outputIndex,int32_t **shape,uint32_t *shapeLength)](#oh_nnexecutor_getoutputshape) | 获取输出张量的维度信息。                                     |
69| [void OH_NNExecutor_Destroy(OH_NNExecutor **executor)](#oh_nnexecutor_destroy) | 销毁执行器实例,释放执行器占用的内存。<br>调用[OH_NNExecutor_Construct](capi-neural-network-core-h.md#oh_nnexecutor_construct)创建的执行器实例需要调用该接口主动销毁,否则将造成内存泄漏。  如果executor为空指针或者*executor为空指针,该接口仅打印警告日志,不执行销毁操作。 |
70| [OH_NN_ReturnCode OH_NNExecutor_GetInputCount(const OH_NNExecutor *executor, size_t *inputCount)](#oh_nnexecutor_getinputcount) | 获取输入张量的数量。<br>可以先从executor中获取输入张量的数量,然后通过[OH_NNExecutor_CreateInputTensorDesc](capi-neural-network-core-h.md#oh_nnexecutor_createinputtensordesc)由指定张量索引创建张量描述。 |
71| [OH_NN_ReturnCode OH_NNExecutor_GetOutputCount(const OH_NNExecutor *executor, size_t *outputCount)](#oh_nnexecutor_getoutputcount) | 获取输出张量的数量。<br>可以先从executor中获取输出张量的数量,然后通过[OH_NNExecutor_CreateOutputTensorDesc](capi-neural-network-core-h.md#oh_nnexecutor_createoutputtensordesc)由指定张量索引创建张量描述。 |
72| [NN_TensorDesc *OH_NNExecutor_CreateInputTensorDesc(const OH_NNExecutor *executor, size_t index)](#oh_nnexecutor_createinputtensordesc) | 由指定索引值创建一个输入张量的描述。<br>输入张量描述包含了该张量所有类型的属性值。如果索引值<b>index</b>达到或超过输入张量的数量,接口将返回错误码。输入张量的数量可以通过[OH_NNExecutor_GetInputCount](capi-neural-network-core-h.md#oh_nnexecutor_getinputcount)获取。 |
73| [NN_TensorDesc *OH_NNExecutor_CreateOutputTensorDesc(const OH_NNExecutor *executor, size_t index)](#oh_nnexecutor_createoutputtensordesc) | 由指定索引值创建一个输出张量的描述。<br>输出张量描述包含了该张量所有类型的属性值。如果索引值<b>index</b>达到或超过输出张量的数量,接口将返回错误码。输出张量的数量可以通过[OH_NNExecutor_GetOutputCount](capi-neural-network-core-h.md#oh_nnexecutor_getoutputcount)获取。 |
74| [OH_NN_ReturnCode OH_NNExecutor_GetInputDimRange(const OH_NNExecutor *executor,size_t index,size_t **minInputDims,size_t **maxInputDims,size_t *shapeLength)](#oh_nnexecutor_getinputdimrange) | 获取所有输入张量的维度范围。<br>当输入张量具有动态形状时,它在不同硬件上支持的维度范围可能是不同的,可以通过该接口获取当前设备上支持的维度范围。<b>\*minInputDims</b>保存了指定输入张量的最小维度(维度数与形状匹配),而<b>\*maxInputDims</b>则保存了最大维度。 |
75| [OH_NN_ReturnCode OH_NNExecutor_SetOnRunDone(OH_NNExecutor *executor, NN_OnRunDone onRunDone)](#oh_nnexecutor_setonrundone) | 设置异步推理结束后的回调处理函数。<br>回调函数的定义详见[NN_OnRunDone](capi-neural-network-runtime-type-h.md#nn_onrundone)。 |
76| [OH_NN_ReturnCode OH_NNExecutor_SetOnServiceDied(OH_NNExecutor *executor, NN_OnServiceDied onServiceDied)](#oh_nnexecutor_setonservicedied) | 设置异步推理执行期间设备驱动服务突然死亡时的回调处理函数。<br>回调函数的定义详见[NN_OnServiceDied](capi-neural-network-runtime-type-h.md#nn_onservicedied)。 |
77| [OH_NN_ReturnCode OH_NNExecutor_RunSync(OH_NNExecutor *executor,NN_Tensor *inputTensor[],size_t inputCount,NN_Tensor *outputTensor[],size_t outputCount)](#oh_nnexecutor_runsync) | 执行同步推理。<br>需要先通过[OH_NNTensor_Create](capi-neural-network-core-h.md#oh_nntensor_create)、[OH_NNTensor_CreateWithSize](capi-neural-network-core-h.md#oh_nntensor_createwithsize)或[OH_NNTensor_CreateWithFd](capi-neural-network-core-h.md#oh_nntensor_createwithfd)接口创建输入和输出张量。然后由[OH_NNTensor_GetDataBuffer](capi-neural-network-core-h.md#oh_nntensor_getdatabuffer)获取张量数据指针并向其拷贝输入数据。执行器会通过执行推理产生推理结果,并将结果写入输出张量中。 <br>如果输出张量具有动态形状,可以通过[OH_NNExecutor_GetOutputShape](capi-neural-network-core-h.md#oh_nnexecutor_getoutputshape)接口获取输出张量的实际形状。或者通过[OH_NNTensor_GetTensorDesc](capi-neural-network-core-h.md#oh_nntensor_gettensordesc)接口从输入张量中获取张量描述,然后通过[OH_NNTensorDesc_GetShape](capi-neural-network-core-h.md#oh_nntensordesc_getshape)接口获取实际形状。 |
78| [OH_NN_ReturnCode OH_NNExecutor_RunAsync(OH_NNExecutor *executor,NN_Tensor *inputTensor[],size_t inputCount,NN_Tensor *outputTensor[],size_t outputCount,int32_t timeout,void *userData)](#oh_nnexecutor_runasync) | 执行异步推理。<br>需要先通过[OH_NNTensor_Create](capi-neural-network-core-h.md#oh_nntensor_create)、[OH_NNTensor_CreateWithSize](capi-neural-network-core-h.md#oh_nntensor_createwithsize)或[OH_NNTensor_CreateWithFd](capi-neural-network-core-h.md#oh_nntensor_createwithfd)接口创建输入和输出张量。然后由[OH_NNTensor_GetDataBuffer](capi-neural-network-core-h.md#oh_nntensor_getdatabuffer)获取张量数据指针并向其拷贝输入数据。执行器会通过执行推理产生推理结果,并将结果写入输出张量中。 <br>如果输出张量具有动态形状,可以通过[OH_NNExecutor_GetOutputShape](capi-neural-network-core-h.md#oh_nnexecutor_getoutputshape)接口获取输出张量的实际形状。或者通过[OH_NNTensor_GetTensorDesc](capi-neural-network-core-h.md#oh_nntensor_gettensordesc)接口从输入张量中获取张量描述,然后通过[OH_NNTensorDesc_GetShape](capi-neural-network-core-h.md#oh_nntensordesc_getshape)接口获取实际形状。 <br>该接口是非阻塞式的,调用后会立刻返回,而推理结果、执行返回状态可以通过回调函数[NN_OnRunDone](capi-neural-network-runtime-type-h.md#nn_onrundone)来获取。如果设备驱动服务在执行过程中异常终止,可以通过回调函数[NN_OnServiceDied](capi-neural-network-runtime-type-h.md#nn_onservicedied)来处理。 <br>可以通过接口[OH_NNExecutor_SetOnRunDone](capi-neural-network-core-h.md#oh_nnexecutor_setonrundone)和[OH_NNExecutor_SetOnServiceDied](capi-neural-network-core-h.md#oh_nnexecutor_setonservicedied)设置回调函数[NN_OnRunDone](capi-neural-network-runtime-type-h.md#nn_onrundone)和[NN_OnServiceDied](capi-neural-network-runtime-type-h.md#nn_onservicedied)。 <br>如果推理时长超过了<b>timeout</b>,会立刻终止推理,回调函数[NN_OnRunDone](capi-neural-network-runtime-type-h.md#nn_onrundone)的<b>errCode<b>参数会返回[OH_NN_TIMEOUT](capi-neural-network-runtime-type-h.md#oh_nn_returncode)错误。 <br><b>userData</b>是区分不同次异步执行的标识符,会作为回调函数的第一个参数返回,您可以使用能够区分不同次执行的任意数据作为标识符。 |
79| [OH_NN_ReturnCode OH_NNDevice_GetAllDevicesID(const size_t **allDevicesID, uint32_t *deviceCount)](#oh_nndevice_getalldevicesid) | 获取对接到Neural Network Runtime的硬件ID。<br>每个硬件存在唯一且固定的ID,该接口通过uint32_t数组返回当前设备上已经对接的硬件ID。 <br>硬件ID通过size_t数组返回,数组的每个元素是单个硬件的ID值。数组内存由内部进行管理,在下次调用该接口前,数据指针将一直有效。 |
80| [OH_NN_ReturnCode OH_NNDevice_GetName(size_t deviceID, const char **name)](#oh_nndevice_getname) | 获取指定硬件的名称。<br>通过deviceID指定计算硬件,获取硬件的名称。硬件ID需要调用[OH_NNDevice_GetAllDevicesID](capi-neural-network-core-h.md#oh_nndevice_getalldevicesid)获取。如果deviceID是0,那么会默认使用设备列表中的第一个设备。  <b>\*name</b>是一个C风格的字符串,以<b>'\0'</b>作为结束符。 <br><b>\*name</b>必须是一个空指针,否则接口会返回[OH_NN_INVALID_PARAMETER](capi-neural-network-runtime-type-h.md#oh_nn_returncode)错误。例如您应该定义char* deviceName = NULL,然后将&deviceName作为参数传入。 |
81| [OH_NN_ReturnCode OH_NNDevice_GetType(size_t deviceID, OH_NN_DeviceType *deviceType)](#oh_nndevice_gettype) | 获取指定硬件的类别信息。<br>通过deviceID指定计算硬件,获取硬件的类别。如果deviceID是0,那么会默认使用设备列表中的第一个设备。目前支持的设备类型有:<br>- CPU设备:OH_NN_CPU <br>- GPU设备:OH_NN_GPU <br>- 机器学习专用加速器:OH_NN_ACCELERATOR <br>- 不属于以上类型的其他硬件类型:OH_NN_OTHERS |
82
83## 函数说明
84
85### OH_NNCompilation_Construct()
86
87```
88OH_NNCompilation *OH_NNCompilation_Construct(const OH_NNModel *model)
89```
90
91**描述**
92
93创建[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)类型的编译实例。
94
95使用OH_NNModel模块完成模型的构造后,借助OH_NNCompilation模块提供的接口,将模型传递到底层硬件完成编译。
96
97该接口接受一个[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)实例,创建出[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例;通过[OH_NNCompilation_SetDevice](capi-neural-network-core-h.md#oh_nncompilation_setdevice)接口,设置编译的设备,最后调用[OH_NNCompilation_Build](capi-neural-network-core-h.md#oh_nncompilation_build)完成编译。
98
99除了计算硬件的选择,OH_NNCompilation模块支持模型缓存、性能偏好、优先级设置、float16计算等特性,参考以下接口:
100
101[OH_NNCompilation_SetCache](capi-neural-network-core-h.md#oh_nncompilation_setcache)
102
103[OH_NNCompilation_SetPerformanceMode](capi-neural-network-core-h.md#oh_nncompilation_setperformancemode)
104
105[OH_NNCompilation_SetPriority](capi-neural-network-core-h.md#oh_nncompilation_setpriority)
106
107[OH_NNCompilation_EnableFloat16](capi-neural-network-core-h.md#oh_nncompilation_enablefloat16)
108
109调用该接口创建[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)后,[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)实例就可以释放了。
110
111**起始版本:** 9
112
113
114**参数:**
115
116| 参数项 | 描述 |
117| -- | -- |
118| [const OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md) *model | 指向[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)实例的指针。 |
119
120**返回:**
121
122| 类型 | 说明 |
123| -- | -- |
124| [OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md) * | 指向[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例的指针,如果创建失败就返回NULL。 |
125
126### OH_NNCompilation_ConstructWithOfflineModelFile()
127
128```
129OH_NNCompilation *OH_NNCompilation_ConstructWithOfflineModelFile(const char *modelPath)
130```
131
132**描述**
133
134基于离线模型文件创建编译实例。
135
136该接口与传递在线构建模型或离线模型文件内存的方式冲突,您只能选择三种构建接口中的一种。
137
138离线模型是由硬件供应商提供的模型转换器离线编译的模型类型,所以离线模型只能在指定的设备上使用,但离线模型的编译时间通常远小于构图实例[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)的编译时间。
139
140在开发过程中需要离线执行编译,并在应用包中部署离线模型。
141
142**起始版本:** 11
143
144
145**参数:**
146
147| 参数项 | 描述 |
148| -- | -- |
149| const char *modelPath | 离线模型文件路径。 |
150
151**返回:**
152
153| 类型 | 说明 |
154| -- | -- |
155| [OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md) * | 指向[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例的指针,如果创建失败就返回NULL。 |
156
157### OH_NNCompilation_ConstructWithOfflineModelBuffer()
158
159```
160OH_NNCompilation *OH_NNCompilation_ConstructWithOfflineModelBuffer(const void *modelBuffer, size_t modelSize)
161```
162
163**描述**
164
165基于离线模型文件内存创建编译实例。
166
167该接口与传递在线构建模型或离线模型文件路径的方式冲突,您只能选择三种构建接口中的一种。
168
169返回的[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例只将<b>modelBuffer</b>指针保存在里面,而不是复制其数据。在销毁[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例之前,不应释放<b>modelBuffer</b>。
170
171**起始版本:** 11
172
173
174**参数:**
175
176| 参数项 | 描述 |
177| -- | -- |
178| const void *modelBuffer | 离线模型文件内存。 |
179| size_t modelSize | 离线模型内存大小。 |
180
181**返回:**
182
183| 类型 | 说明 |
184| -- | -- |
185| [OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md) * | 指向[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例的指针,如果创建失败就返回NULL。 |
186
187### OH_NNCompilation_ConstructForCache()
188
189```
190OH_NNCompilation *OH_NNCompilation_ConstructForCache()
191```
192
193**描述**
194
195创建一个空的编译实例,以便稍后从模型缓存中恢复。
196
197模型缓存的相关描述参考[OH_NNCompilation_SetCache](capi-neural-network-core-h.md#oh_nncompilation_setcache)。
198
199从模型缓存恢复的时间少于使用[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)进行编译的时间。
200
201应该先调用[OH_NNCompilation_SetCache](capi-neural-network-core-h.md#oh_nncompilation_setcache)或[OH_NNCompilation_ImportCacheFromBuffer](capi-neural-network-core-h.md#oh_nncompilation_importcachefrombuffer),然后调用[OH_NNCompilation_Build](capi-neural-network-core-h.md#oh_nncompilation_build)完成恢复。
202
203**起始版本:** 11
204
205**返回:**
206
207| 类型 | 说明 |
208| -- | -- |
209| [OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md) * | 指向[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例的指针,如果创建失败就返回NULL。 |
210
211### OH_NNCompilation_ExportCacheToBuffer()
212
213```
214OH_NN_ReturnCode OH_NNCompilation_ExportCacheToBuffer(OH_NNCompilation *compilation,const void *buffer,size_t length,size_t *modelSize)
215```
216
217**描述**
218
219将模型缓存写入到指定内存区域。
220
221模型缓存的相关描述参考[OH_NNCompilation_SetCache](capi-neural-network-core-h.md#oh_nncompilation_setcache)。
222
223模型缓存是编译构建的结果[OH_NNCompilation_Build](capi-neural-network-core-h.md#oh_nncompilation_build),因此必须在[OH_NNCompilation_Build](capi-neural-network-core-h.md#oh_nncompilation_build)之后调用该接口。
224
225**起始版本:** 11
226
227
228**参数:**
229
230| 参数项 | 描述 |
231| -- | -- |
232| [OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md) *compilation | 指向[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例的指针。 |
233| const void *buffer | 指向给定内存的指针。 |
234| size_t length | 内存长度。 |
235| size_t *modelSize | 模型缓存的字节大小。 |
236
237**返回:**
238
239| 类型 | 说明 |
240| -- | -- |
241| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
242
243### OH_NNCompilation_ImportCacheFromBuffer()
244
245```
246OH_NN_ReturnCode OH_NNCompilation_ImportCacheFromBuffer(OH_NNCompilation *compilation,const void *buffer,size_t modelSize)
247```
248
249**描述**
250
251从指定内存区域读取模型缓存。
252
253模型缓存的相关描述参考[OH_NNCompilation_SetCache](capi-neural-network-core-h.md#oh_nncompilation_setcache)。
254
255调用[OH_NNCompilation_ImportCacheFromBuffer](capi-neural-network-core-h.md#oh_nncompilation_importcachefrombuffer)后,应调用[OH_NNCompilation_Build](capi-neural-network-core-h.md#oh_nncompilation_build)完成恢复。
256
257<b>compilation</b>只将<b>buffer</b>指针保存在里面,而不是复制其数据。您不能在<b>compilation</b>被销毁之前释放内存<b>buffer</b>。
258
259**起始版本:** 11
260
261
262**参数:**
263
264| 参数项 | 描述 |
265| -- | -- |
266| [OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md) *compilation | 指向[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例的指针。 |
267| const void *buffer | 指向给定内存的指针。 |
268| size_t modelSize | 模型缓存的字节大小。 |
269
270**返回:**
271
272| 类型 | 说明 |
273| -- | -- |
274| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
275
276### OH_NNCompilation_AddExtensionConfig()
277
278```
279OH_NN_ReturnCode OH_NNCompilation_AddExtensionConfig(OH_NNCompilation *compilation,const char *configName,const void *configValue,const size_t configValueSize)
280```
281
282**描述**
283
284为自定义硬件属性添加扩展配置。
285
286某些设备有自己的特定属性,这些属性尚未在NNRt中打开。该接口为您提供了另一种方式设置设备的这些自定义硬件属性。
287
288您应该从设备供应商的文档查询它们的名称和值,并将它们逐一添加到编译实例中。这些属性将直接传递给设备驱动程序,如果驱动程序无法解析它们,该接口将返回错误码。<br>调用[OH_NNCompilation_Build](capi-neural-network-core-h.md#oh_nncompilation_build)后,<b>configName</b>和<b>configValue</b>就可以释放了。
289
290**起始版本:** 11
291
292
293**参数:**
294
295| 参数项 | 描述 |
296| -- | -- |
297| [OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md) *compilation | 指向[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例的指针。 |
298| const char *configName | 配置名称。 |
299| const void *configValue | 保存配置值的地址。 |
300| const size_t configValueSize | 配置值的字节大小。 |
301
302**返回:**
303
304| 类型 | 说明 |
305| -- | -- |
306| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
307
308### OH_NNCompilation_SetDevice()
309
310```
311OH_NN_ReturnCode OH_NNCompilation_SetDevice(OH_NNCompilation *compilation, size_t deviceID)
312```
313
314**描述**
315
316指定模型编译和计算的硬件。
317
318编译阶段,需要指定模型编译和执行计算的硬件设备。先调用[OH_NNDevice_GetAllDevicesID](capi-neural-network-core-h.md#oh_nndevice_getalldevicesid)获取可用的设备ID,通过[OH_NNDevice_GetType](capi-neural-network-core-h.md#oh_nndevice_gettype)和[OH_NNDevice_GetType](capi-neural-network-core-h.md#oh_nndevice_gettype)获取设备信息后,将期望编译执行的设备ID传入该接口进行设置。
319
320**起始版本:** 9
321
322
323**参数:**
324
325| 参数项 | 描述 |
326| -- | -- |
327| [OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md) *compilation | 指向[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例的指针。 |
328| size_t deviceID | 指定的硬件ID。如果为0,则默认使用当前设备列表中的第1台设备。 |
329
330**返回:**
331
332| 类型 | 说明 |
333| -- | -- |
334| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
335
336### OH_NNCompilation_SetCache()
337
338```
339OH_NN_ReturnCode OH_NNCompilation_SetCache(OH_NNCompilation *compilation, const char *cachePath, uint32_t version)
340```
341
342**描述**
343
344设置编译模型的缓存目录和版本。
345
346在支持模型缓存的硬件上,模型在硬件驱动层编译后可以保存为模型缓存文件,下次编译时直接从模型缓存文件读取模型,减少重新编译的耗时。
347
348该接口接受模型缓存路径和版本,根据缓存路径中和版本的不同情况,该接口采取不同的行为:
349
350- 模型缓存路径指定的目录下没有文件:将编译后的模型缓存到目录下,设置缓存版本等于version。
351- 模型缓存路径指定的目录下存在完整的缓存文件,且版本号 == version:读取路径下的缓存文件,传递到底层硬件中转换为可以执行的模型实例。
352- 模型缓存路径指定的目录下存在完整的缓存文件,但版本号 < version:路径下的缓存文件需要更新,模型在底层硬件完成编译后,覆写路径下的缓存文件,将版本号更新为version。
353- 模型缓存路径指定的目录下存在完整的缓存文件,但版本号 > version:路径下的缓存文件版本高于version,不读取缓存文件,同时返回[OH_NN_INVALID_PARAMETER](capi-neural-network-runtime-type-h.md#oh_nn_returncode)错误码。
354- 模型缓存路径指定的目录下的缓存文件不完整或没有缓存文件的访问权限:返回[OH_NN_INVALID_FILE](capi-neural-network-runtime-type-h.md#oh_nn_returncode)错误码。
355- 模型缓存目录不存在,或者没有访问权限:返回[OH_NN_INVALID_PATH](capi-neural-network-runtime-type-h.md#oh_nn_returncode)错误码。
356
357**起始版本:** 9
358
359
360**参数:**
361
362| 参数项 | 描述 |
363| -- | -- |
364| [OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md) *compilation | 指向[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例的指针。 |
365| const char *cachePath | 模型缓存文件目录,该接口在cachePath目录下为不同的硬件创建模型缓存目录。建议每个模型使用单独的模型缓存目录。 |
366| uint32_t version | 模型缓存版本。 |
367
368**返回:**
369
370| 类型 | 说明 |
371| -- | -- |
372| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
373
374### OH_NNCompilation_SetPerformanceMode()
375
376```
377OH_NN_ReturnCode OH_NNCompilation_SetPerformanceMode(OH_NNCompilation *compilation,OH_NN_PerformanceMode performanceMode)
378```
379
380**描述**
381
382设置模型计算的性能模式。
383
384Neural Network Runtime 支持为模型计算设置性能模式,满足低功耗到极致性能的需求。如果编译阶段没有调用该接口设置性能模式,编译实例为模型默认分配[OH_NN_PERFORMANCE_NONE](capi-neural-network-runtime-type-h.md#oh_nn_performancemode)模式。在[OH_NN_PERFORMANCE_NONE](capi-neural-network-runtime-type-h.md#oh_nn_performancemode)模式下,硬件按默认的性能模式执行计算。 在不支持性能模式设置的硬件上调用该接口,将返回[OH_NN_UNAVAILABLE_DEVICE](capi-neural-network-runtime-type-h.md#oh_nn_returncode)错误码。
385
386**起始版本:** 9
387
388
389**参数:**
390
391| 参数项 | 描述 |
392| -- | -- |
393| [OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md) *compilation | 指向[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例的指针。 |
394| [OH_NN_PerformanceMode](capi-neural-network-runtime-type-h.md#oh_nn_performancemode) performanceMode | 指定性能模式,可选的性能模式参考[OH_NN_PerformanceMode](capi-neural-network-runtime-type-h.md#oh_nn_performancemode)。 |
395
396**返回:**
397
398| 类型 | 说明 |
399| -- | -- |
400| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
401
402### OH_NNCompilation_SetPriority()
403
404```
405OH_NN_ReturnCode OH_NNCompilation_SetPriority(OH_NNCompilation *compilation, OH_NN_Priority priority)
406```
407
408**描述**
409
410设置模型计算的优先级。
411
412Neural Network Runtime 支持为模型设置计算优先级,优先级仅作用于相同uid进程创建的模型,不同uid进程、不同设备的优先级不会相互影响。 在不支持优先级设置的硬件上调用该接口,将返回[OH_NN_UNAVAILABLE_DEVICE](capi-neural-network-runtime-type-h.md#oh_nn_returncode)错误码。
413
414**起始版本:** 9
415
416
417**参数:**
418
419| 参数项 | 描述 |
420| -- | -- |
421| [OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md) *compilation | 指向[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例的指针。 |
422| [OH_NN_Priority](capi-neural-network-runtime-type-h.md#oh_nn_priority) priority | 指定优先级,可选的优先级参考[OH_NN_Priority](capi-neural-network-runtime-type-h.md#oh_nn_priority)。 |
423
424**返回:**
425
426| 类型 | 说明 |
427| -- | -- |
428| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
429
430### OH_NNCompilation_EnableFloat16()
431
432```
433OH_NN_ReturnCode OH_NNCompilation_EnableFloat16(OH_NNCompilation *compilation, bool enableFloat16)
434```
435
436**描述**
437
438是否以float16的浮点数精度计算。
439
440浮点模型默认使用float32精度计算。如果在支持float16精度的硬件上调用该接口,float32浮点数精度的模型将以float16的精度执行计算,可减少内存占用和执行时间。 该选项对于定点模型是无效的,例如int8类型的定点模型。<br>在不支持float16精度计算的硬件上调用该接口,将返回[OH_NN_UNAVAILABLE_DEVICE](capi-neural-network-runtime-type-h.md#oh_nn_returncode)错误码。
441
442**起始版本:** 9
443
444
445**参数:**
446
447| 参数项 | 描述 |
448| -- | -- |
449| [OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md) *compilation | 指向[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例的指针。 |
450| bool enableFloat16 | Float16低精度计算标志位。设置为true时,执行Float16推理;设置为false时,执行float32推理。 |
451
452**返回:**
453
454| 类型 | 说明 |
455| -- | -- |
456| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
457
458### OH_NNCompilation_Build()
459
460```
461OH_NN_ReturnCode OH_NNCompilation_Build(OH_NNCompilation *compilation)
462```
463
464**描述**
465
466执行模型编译。
467
468完成编译配置后,调用该接口执行模型编译。编译实例将模型和编译选项推送至硬件设备进行编译。
469
470在调用该接口后,无法进行额外的编译操作,调用[OH_NNCompilation_SetDevice](capi-neural-network-core-h.md#oh_nncompilation_setdevice)、[OH_NNCompilation_SetCache](capi-neural-network-core-h.md#oh_nncompilation_setcache)、[OH_NNCompilation_SetPerformanceMode](capi-neural-network-core-h.md#oh_nncompilation_setperformancemode)、[OH_NNCompilation_SetPriority](capi-neural-network-core-h.md#oh_nncompilation_setpriority)和[OH_NNCompilation_EnableFloat16](capi-neural-network-core-h.md#oh_nncompilation_enablefloat16)接口将返回[OH_NN_OPERATION_FORBIDDEN](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。
471
472**起始版本:** 9
473
474
475**参数:**
476
477| 参数项 | 描述 |
478| -- | -- |
479| [OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md) *compilation | 指向[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例的指针。 |
480
481**返回:**
482
483| 类型 | 说明 |
484| -- | -- |
485| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
486
487### OH_NNCompilation_Destroy()
488
489```
490void OH_NNCompilation_Destroy(OH_NNCompilation **compilation)
491```
492
493**描述**
494
495销毁Compilation实例。
496
497调用[OH_NNCompilation_Construct](capi-neural-network-core-h.md#oh_nncompilation_construct)、[OH_NNCompilation_ConstructWithOfflineModelFile](capi-neural-network-core-h.md#oh_nncompilation_constructwithofflinemodelfile)、[OH_NNCompilation_ConstructWithOfflineModelBuffer](capi-neural-network-core-h.md#oh_nncompilation_constructwithofflinemodelbuffer)、[OH_NNCompilation_ConstructForCache](capi-neural-network-core-h.md#oh_nncompilation_constructforcache)创建的编译实例需要调用该接口主动销毁。 如果compilation为空指针或者*compilation为空指针,该接口仅打印警告日志,不执行销毁操作。
498
499**起始版本:** 9
500
501
502**参数:**
503
504| 参数项 | 描述 |
505| -- | -- |
506| [OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md) **compilation | 指向[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例的二级指针。编译实例销毁后,该接口将*compilation主动设置为空指针。 |
507
508### OH_NNTensorDesc_Create()
509
510```
511NN_TensorDesc *OH_NNTensorDesc_Create()
512```
513
514**描述**
515
516创建一个[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例。
517
518[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)描述了各种张量属性,如名称/数据类型/形状/格式等。
519
520可以调用以下接口,基于传入的[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例创建[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例:
521
522[OH_NNTensor_Create](capi-neural-network-core-h.md#oh_nntensor_create)
523
524[OH_NNTensor_CreateWithSize](capi-neural-network-core-h.md#oh_nntensor_createwithsize)
525
526[OH_NNTensor_CreateWithFd](capi-neural-network-core-h.md#oh_nntensor_createwithfd)
527
528该接口会将[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例复制到[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)中,因此您可以创建多个[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)个实例,并持有相同的[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例。当[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例不再使用时,您应该调用[OH_NNTensorDesc_Destroy](capi-neural-network-core-h.md#oh_nntensordesc_destroy)接口销毁它。
529
530**起始版本:** 11
531
532**返回:**
533
534| 类型 | 说明 |
535| -- | -- |
536| [NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md) * | 指向[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的指针,如果创建失败就返回NULL。 |
537
538### OH_NNTensorDesc_Destroy()
539
540```
541OH_NN_ReturnCode OH_NNTensorDesc_Destroy(NN_TensorDesc **tensorDesc)
542```
543
544**描述**
545
546释放一个[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例。
547
548当[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例不再使用时,需要调用该接口销毁,否则将发生内存泄漏。
549
550如果<b>tensorDesc</b>或<b>*tensorDesc</b>为空指针,则该接口将返回错误码,并且不会执行销毁操作。
551
552**起始版本:** 11
553
554
555**参数:**
556
557| 参数项 | 描述 |
558| -- | -- |
559| [NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md) **tensorDesc | 指向[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的二级指针。 |
560
561**返回:**
562
563| 类型 | 说明 |
564| -- | -- |
565| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
566
567### OH_NNTensorDesc_SetName()
568
569```
570OH_NN_ReturnCode OH_NNTensorDesc_SetName(NN_TensorDesc *tensorDesc, const char *name)
571```
572
573**描述**
574
575设置[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的名称。
576
577[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例创建完成后,调用该接口设置张量的名称,<b>*name</b>的值是以<b>'\0'</b>结尾的C风格字符串。
578
579如果<b>tensorDesc</b>或<b>name</b>为空指针,则该接口将返回错误码。
580
581**起始版本:** 11
582
583
584**参数:**
585
586| 参数项 | 描述 |
587| -- | -- |
588| [NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md) *tensorDesc | 指向[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的指针。 |
589| const char *name | 需要设置的张量名称。 |
590
591**返回:**
592
593| 类型 | 说明 |
594| -- | -- |
595| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
596
597### OH_NNTensorDesc_GetName()
598
599```
600OH_NN_ReturnCode OH_NNTensorDesc_GetName(const NN_TensorDesc *tensorDesc, const char **name)
601```
602
603**描述**
604
605获取[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的名称。
606
607调用该接口获取指定[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的名称,<b>\*name</b>的值是以<b>'\0'</b>结尾的C风格字符串。
608
609如果<b>tensorDesc</b>或<b>name</b>为空指针,则该接口将返回错误码。作为输出参数,<b>*name</b>必须为空指针,否则该接口将返回错误码。
610
611例如您应该定义char\* tensorName = NULL,并传递&tensorName作为<b>name</b>的参数。
612
613您不需要释放<b>name</b>的内存,当<b>tensorDesc</b>被销毁时,它会被自动释放。
614
615**起始版本:** 11
616
617**参数:**
618
619| 参数项 | 描述 |
620| -- | -- |
621| [const NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md) *tensorDesc | 指向[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的指针。 |
622| const char **name | 返回的张量名称。 |
623
624**返回:**
625
626| 类型 | 说明 |
627| -- | -- |
628| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
629
630### OH_NNTensorDesc_SetDataType()
631
632```
633OH_NN_ReturnCode OH_NNTensorDesc_SetDataType(NN_TensorDesc *tensorDesc, OH_NN_DataType dataType)
634```
635
636**描述**
637
638设置[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的数据类型。
639
640[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例创建完成后,调用该接口设置张量数据类型。
641
642如果<b>tensorDesc</b>为空指针,则该接口将返回错误码。
643
644**起始版本:** 11
645
646
647**参数:**
648
649| 参数项 | 描述 |
650| -- | -- |
651| [NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md) *tensorDesc | 指向[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的指针。 |
652| [OH_NN_DataType](capi-neural-network-runtime-type-h.md#oh_nn_datatype) dataType | 需要设置的张量数据类型。 |
653
654**返回:**
655
656| 类型 | 说明 |
657| -- | -- |
658| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
659
660### OH_NNTensorDesc_GetDataType()
661
662```
663OH_NN_ReturnCode OH_NNTensorDesc_GetDataType(const NN_TensorDesc *tensorDesc, OH_NN_DataType *dataType)
664```
665
666**描述**
667
668获取[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的数据类型。
669
670调用该接口获取指定[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的数据类型。
671
672如果<b>tensorDesc</b>或<b>dataType</b>为空指针,则该接口将返回错误码。
673
674**起始版本:** 11
675
676
677**参数:**
678
679| 参数项 | 描述 |
680| -- | -- |
681| [const NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md) *tensorDesc | 指向[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的指针。 |
682| [OH_NN_DataType](capi-neural-network-runtime-type-h.md#oh_nn_datatype) *dataType | 返回的张量数据类型。 |
683
684**返回:**
685
686| 类型 | 说明 |
687| -- | -- |
688| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
689
690### OH_NNTensorDesc_SetShape()
691
692```
693OH_NN_ReturnCode OH_NNTensorDesc_SetShape(NN_TensorDesc *tensorDesc, const int32_t *shape, size_t shapeLength)
694```
695
696**描述**
697
698设置[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的数据形状。
699
700[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例创建完成后,调用该接口设置张量形状。
701
702如果<b>tensorDesc</b>或<b>shape</b>为空指针,或<b>shapeLength</b>为0,则该接口将返回错误码。
703
704**起始版本:** 11
705
706
707**参数:**
708
709| 参数项 | 描述 |
710| -- | -- |
711| [NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md) *tensorDesc | 指向[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的指针。 |
712| const int32_t *shape | 需要设置的张量形状列表。 |
713| size_t shapeLength | 需要设置的张量形状列表长度。 |
714
715**返回:**
716
717| 类型 | 说明 |
718| -- | -- |
719| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
720
721### OH_NNTensorDesc_GetShape()
722
723```
724OH_NN_ReturnCode OH_NNTensorDesc_GetShape(const NN_TensorDesc *tensorDesc, int32_t **shape, size_t *shapeLength)
725```
726
727**描述**
728
729获取[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的形状。
730
731调用该接口获取指定[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的形状。
732
733如果<b>tensorDesc</b>、<b>shape</b>或<b>shapeLength</b>为空指针,则该接口将返回错误码。作为输出参数,<b>*shape</b>必须为空指针,否则该接口将返回错误码。
734
735例如您应该定义 int32_t\* tensorShape = NULL,并传递&tensorShape作为<b>shape</b>的参数。
736
737您不需要释放<b>shape</b>的内存。当<b>tensorDesc</b>被销毁时,它会自动释放。
738
739**起始版本:** 11
740
741
742**参数:**
743
744| 参数项 | 描述 |
745| -- | -- |
746| [const NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md) *tensorDesc | 指向[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的指针。 |
747| int32_t **shape | 返回的张量形状列表。 |
748| size_t *shapeLength | 返回的形状列表长度。 |
749
750**返回:**
751
752| 类型 | 说明 |
753| -- | -- |
754| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
755
756### OH_NNTensorDesc_SetFormat()
757
758```
759OH_NN_ReturnCode OH_NNTensorDesc_SetFormat(NN_TensorDesc *tensorDesc, OH_NN_Format format)
760```
761
762**描述**
763
764设置[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的数据布局。
765
766[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例创建完成后,调用该接口设置张量的数据布局[OH_NN_Format](capi-neural-network-runtime-type-h.md#oh_nn_format)。
767
768如果<b>tensorDesc</b>为空指针,则该接口将返回错误码。
769
770**起始版本:** 11
771
772
773**参数:**
774
775| 参数项 | 描述 |
776| -- | -- |
777| [NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md) *tensorDesc | 指向[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的指针。 |
778| [OH_NN_Format](capi-neural-network-runtime-type-h.md#oh_nn_format) format | 需要设置的张量数据布局。 |
779
780**返回:**
781
782| 类型 | 说明 |
783| -- | -- |
784| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
785
786### OH_NNTensorDesc_GetFormat()
787
788```
789OH_NN_ReturnCode OH_NNTensorDesc_GetFormat(const NN_TensorDesc *tensorDesc, OH_NN_Format *format)
790```
791
792**描述**
793
794获取[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的数据布局。
795
796调用该接口获取指定[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的数据布局[OH_NN_Format](capi-neural-network-runtime-type-h.md#oh_nn_format)。
797
798如果<b>tensorDesc</b>或<b>format</b>为空指针,则该接口将返回错误码。
799
800**起始版本:** 11
801
802
803**参数:**
804
805| 参数项 | 描述 |
806| -- | -- |
807| [const NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md) *tensorDesc | 指向[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的指针。 |
808| [OH_NN_Format](capi-neural-network-runtime-type-h.md#oh_nn_format) *format | 返回的张量数据布局。 |
809
810**返回:**
811
812| 类型 | 说明 |
813| -- | -- |
814| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
815
816### OH_NNTensorDesc_GetElementCount()
817
818```
819OH_NN_ReturnCode OH_NNTensorDesc_GetElementCount(const NN_TensorDesc *tensorDesc, size_t *elementCount)
820```
821
822**描述**
823
824获取[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的元素个数。
825
826调用该接口获取指定[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的元素个数。如果需要获取张量数据的字节大小,请调用[OH_NNTensorDesc_GetByteSize](capi-neural-network-core-h.md#oh_nntensordesc_getbytesize)。
827
828如果张量形状是动态可变的,则该接口将返回错误码,<b>elementCount</b>将为0。
829
830如果<b>tensorDesc</b>或<b>elementCount</b>为空指针,则该接口将返回错误码。
831
832**起始版本:** 11
833
834
835**参数:**
836
837| 参数项 | 描述 |
838| -- | -- |
839| [const NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md) *tensorDesc | 指向[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的指针。 |
840| size_t *elementCount | 张量返回的元素个数。 |
841
842**返回:**
843
844| 类型 | 说明 |
845| -- | -- |
846| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
847
848### OH_NNTensorDesc_GetByteSize()
849
850```
851OH_NN_ReturnCode OH_NNTensorDesc_GetByteSize(const NN_TensorDesc *tensorDesc, size_t *byteSize)
852```
853
854**描述**
855
856获取基于[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的形状和数据类型计算的数据占用字节数。
857
858调用该接口可基于[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)的形状和数据类型计算得到的数据占用字节数。
859
860如果张量形状是动态可变的,该接口将返回错误码,<b>byteSize</b>将为0。
861
862如果需要获取张量数据的元素个数,请调用[OH_NNTensorDesc_GetElementCount](capi-neural-network-core-h.md#oh_nntensordesc_getelementcount)。
863
864如果<b>tensorDesc</b>或<b>byteSize</b>为空指针,则该接口将返回错误码。
865
866**起始版本:** 11
867
868
869**参数:**
870
871| 参数项 | 描述 |
872| -- | -- |
873| [const NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md) *tensorDesc | 指向[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的指针。 |
874| size_t *byteSize | 返回的数据字节数。 |
875
876**返回:**
877
878| 类型 | 说明 |
879| -- | -- |
880| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
881
882### OH_NNTensor_Create()
883
884```
885NN_Tensor *OH_NNTensor_Create(size_t deviceID, NN_TensorDesc *tensorDesc)
886```
887
888**描述**
889
890从[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)创建一个[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例。
891
892该接口使用[OH_NNTensorDesc_GetByteSize](capi-neural-network-core-h.md#oh_nntensordesc_getbytesize)计算张量数据的字节数,并为其分配设备内存。设备驱动将直接通过“零拷贝”方式获取张量数据。
893
894该接口会将<b>tensorDesc</b>复制到[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)中,因此当<b>tensorDesc</b>不再使用时,您应该调用[OH_NNTensorDesc_Destroy](capi-neural-network-core-h.md#oh_nntensordesc_destroy)接口销毁它。
895
896如果张量形状是动态的,该接口将返回错误码。
897
898<b>deviceID</b>表示所选设备。如果为0,则默认使用设备列表中的第1台设备。
899
900<b>必须提供tensorDesc</b>,如果它是空指针,则返回错误码。
901
902当[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例不再使用时,需要调用[OH_NNTensor_Destroy](capi-neural-network-core-h.md#oh_nntensor_destroy)销毁它。
903
904**起始版本:** 11
905
906
907**参数:**
908
909| 参数项 | 描述 |
910| -- | -- |
911| size_t deviceID | 设备 ID。如果为0,则默认使用当前设备列表中的第1台设备。 |
912| [NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md) *tensorDesc | 指向[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的指针。 |
913
914**返回:**
915
916| 类型 | 说明 |
917| -- | -- |
918| [NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md) * | 指向[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例的指针,如果创建失败就返回NULL。 |
919
920### OH_NNTensor_CreateWithSize()
921
922```
923NN_Tensor *OH_NNTensor_CreateWithSize(size_t deviceID, NN_TensorDesc *tensorDesc, size_t size)
924```
925
926**描述**
927
928按照指定内存大小和[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)创建[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例。
929
930该接口使用<b>size</b>作为张量数据的字节数,并为其分配设备内存。设备将直接通过“零拷贝”方式获取张量数据。
931
932该接口会将<b>tensorDesc</b>复制到[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)中。因此当<b>tensorDesc</b>不再使用时,您应该调用[OH_NNTensorDesc_Destroy](capi-neural-network-core-h.md#oh_nntensordesc_destroy)接口销毁它。
933
934<b>deviceID</b>表示所选设备ID,如果为0,则使用第1台设备。
935
936<b>tensorDesc</b>必须提供,如果它是空指针,则该接口返回错误码。
937
938<b>size</b>必须不小于<b>tensorDesc</b>的数据占用字节数(可由[OH_NNTensorDesc_GetByteSize](capi-neural-network-core-h.md#oh_nntensordesc_getbytesize)获取),否则该接口将返回错误码。如果张量形状是动态的,不会检查<b>size</b>。
939
940当[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例不再使用时,需要调用[OH_NNTensor_Destroy](capi-neural-network-core-h.md#oh_nntensor_destroy)销毁它。
941
942**起始版本:** 11
943
944
945**参数:**
946
947| 参数项 | 描述 |
948| -- | -- |
949| size_t deviceID | 设备ID。如果为0,则默认使用当前设备列表中的第1台设备。 |
950| [NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md) *tensorDesc | 指向[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的指针。 |
951| size_t size | 需要分配的张量数据的大小。 |
952
953**返回:**
954
955| 类型 | 说明 |
956| -- | -- |
957| [NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md) * | 指向[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例的指针,如果创建失败就返回NULL。 |
958
959### OH_NNTensor_CreateWithFd()
960
961```
962NN_Tensor *OH_NNTensor_CreateWithFd(size_t deviceID,NN_TensorDesc *tensorDesc,int fd,size_t size,size_t offset)
963```
964
965**描述**
966
967按照指定共享内存的文件描述符和[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)创建[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例。
968
969该接口复用文件描述符<b>fd</b>对应的共享内存,<b>fd</b>可能来自另一个[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例。当调用[OH_NNTensor_Destroy](capi-neural-network-core-h.md#oh_nntensor_destroy)接口销毁该接口创建的张量时,不会释放该张量数据的内存。
970
971该接口会将<b>tensorDesc</b>复制到[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)中。因此当<b>tensorDesc</b>不再使用时,您应该调用[OH_NNTensorDesc_Destroy](capi-neural-network-core-h.md#oh_nntensordesc_destroy)接口销毁它。<br><b>deviceID</b>表示所选设备。如果为0,则默认使用当前设备列表中的第1台设备。
972
973必须提供<b>tensorDesc</b>,如果为空指针,则该接口返回错误码。
974
975当[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例不再使用时,需要调用[OH_NNTensor_Destroy](capi-neural-network-core-h.md#oh_nntensor_destroy)销毁它。
976
977**起始版本:** 11
978
979
980**参数:**
981
982| 参数项 | 描述 |
983| -- | -- |
984| size_t deviceID | 设备ID,如果为0,则默认使用当前设备列表中的第1台设备。 |
985| [NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md) *tensorDesc | 指向[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的指针。 |
986| int fd | 要使用的共享内存的文件描述符。 |
987| size_t size | 要使用的共享内存的大小。 |
988| size_t offset | 要使用的共享内存的偏移量。 |
989
990**返回:**
991
992| 类型 | 说明 |
993| -- | -- |
994| [NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md) * | 指向[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例的指针,如果创建失败就返回NULL。 |
995
996### OH_NNTensor_Destroy()
997
998```
999OH_NN_ReturnCode OH_NNTensor_Destroy(NN_Tensor **tensor)
1000```
1001
1002**描述**
1003
1004销毁一个[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例。
1005
1006当不再使用[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例时,需要调用该接口销毁该实例,否则将发生内存泄漏。
1007
1008如果<b>tensor</b>或<b>*tensor</b>为空指针,则该接口将返回错误码,并且不执行销毁操作。
1009
1010**起始版本:** 11
1011
1012**参数:**
1013
1014| 参数项 | 描述 |
1015| -- | -- |
1016| [NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md) **tensor | 指向[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例的二级指针。 |
1017
1018**返回:**
1019
1020| 类型 | 说明 |
1021| -- | -- |
1022| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
1023
1024### OH_NNTensor_GetTensorDesc()
1025
1026```
1027NN_TensorDesc *OH_NNTensor_GetTensorDesc(const NN_Tensor *tensor)
1028```
1029
1030**描述**
1031
1032获取[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)的[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例。
1033
1034调用该接口获取指定[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例的内部[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例指针。
1035
1036您可以从返回的[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例中获取各种类型的张量属性,例如名称/数据布局/数据类型/形状等。
1037
1038您不应销毁返回的[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例,因为它指向了[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)的内部实例,否则一旦调用[OH_NNTensor_Destroy](capi-neural-network-core-h.md#oh_nntensor_destroy)将会发生双重释放的内存崩溃。
1039
1040如果<b>Tensor</b>是空指针,则该接口将会返回空指针。
1041
1042**起始版本:** 11
1043
1044
1045**参数:**
1046
1047| 参数项 | 描述 |
1048| -- | -- |
1049| [const NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md) *tensor | 指向[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例的指针。 |
1050
1051**返回:**
1052
1053| 类型 | 说明 |
1054| -- | -- |
1055| [NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md) * | 指向[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的指针,如果创建失败就返回NULL。 |
1056
1057### OH_NNTensor_GetDataBuffer()
1058
1059```
1060void *OH_NNTensor_GetDataBuffer(const NN_Tensor *tensor)
1061```
1062
1063**描述**
1064
1065获取[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)数据的内存地址。
1066
1067您可以从张量数据内存读取/写入数据。数据内存是从设备上的共享内存映射的,因此设备驱动可通过这种“零拷贝”方式直接获取张量数据。
1068
1069张量数据仅能使用对应共享内存中的[offset, size)一段,其中offset是共享内存上的偏移量,可以通过[OH_NNTensor_GetOffset](capi-neural-network-core-h.md#oh_nntensor_getoffset)获取,而size是共享内存的总大小,可以通过[OH_NNTensor_GetSize](capi-neural-network-core-h.md#oh_nntensor_getsize)获取。
1070
1071如果<b>Tensor</b>是空指针,则该接口将会返回空指针。
1072
1073**起始版本:** 11
1074
1075
1076**参数:**
1077
1078| 参数项 | 描述 |
1079| -- | -- |
1080| [const NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md) *tensor | 指向[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例的指针。 |
1081
1082**返回:**
1083
1084| 类型 | 说明 |
1085| -- | -- |
1086| void * | 指向张量数据内存的指针。如果操作失败,则返回空指针。 |
1087
1088### OH_NNTensor_GetFd()
1089
1090```
1091OH_NN_ReturnCode OH_NNTensor_GetFd(const NN_Tensor *tensor, int *fd)
1092```
1093
1094**描述**
1095
1096获取[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)数据所在共享内存的文件描述符。
1097
1098文件描述符<b>fd</b>对应了一块设备共享内存,可以通过[OH_NNTensor_CreateWithFd](capi-neural-network-core-h.md#oh_nntensor_createwithfd)被另外一个[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)使用。
1099
1100如果<b>tensor</b>或<b>fd</b>为空指针,该接口将返回错误。
1101
1102**起始版本:** 11
1103
1104
1105**参数:**
1106
1107| 参数项 | 描述 |
1108| -- | -- |
1109| [const NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md) *tensor | 指向[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例的指针。 |
1110| int *fd | 返回的共享内存文件描述符。 |
1111
1112**返回:**
1113
1114| 类型 | 说明 |
1115| -- | -- |
1116| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
1117
1118### OH_NNTensor_GetSize()
1119
1120```
1121OH_NN_ReturnCode OH_NNTensor_GetSize(const NN_Tensor *tensor, size_t *size)
1122```
1123
1124**描述**
1125
1126获取[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)数据所在共享内存的大小。
1127
1128<b>size</b>与接口[OH_NNTensor_CreateWithSize](capi-neural-network-core-h.md#oh_nntensor_createwithsize)和[OH_NNTensor_CreateWithFd](capi-neural-network-core-h.md#oh_nntensor_createwithfd)的参数<b>size</b>相同,但对于通过[OH_NNTensor_Create](capi-neural-network-core-h.md#oh_nntensor_create)创建的张量,<b>size</b>等于张量数据实际占用字节数(可由[OH_NNTensorDesc_GetByteSize](capi-neural-network-core-h.md#oh_nntensordesc_getbytesize)获取)。
1129
1130张量数据仅能使用文件描述符<b>fd</b>对应的共享内存中的[offset, size)一段,其中offset是共享内存上的偏移量,可以通过[OH_NNTensor_GetOffset](capi-neural-network-core-h.md#oh_nntensor_getoffset)获取,而size是共享内存的总大小,可以通过[OH_NNTensor_GetSize](capi-neural-network-core-h.md#oh_nntensor_getsize)获取。
1131
1132如果<b>tensor</b>或<b>size</b>为空指针,该接口将返回错误。
1133
1134**起始版本:** 11
1135
1136
1137**参数:**
1138
1139| 参数项 | 描述 |
1140| -- | -- |
1141| [const NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md) *tensor | 指向[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例的指针。 |
1142| size_t *size | 返回的数据所在共享内存的大小。 |
1143
1144**返回:**
1145
1146| 类型 | 说明 |
1147| -- | -- |
1148| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
1149
1150### OH_NNTensor_GetOffset()
1151
1152```
1153OH_NN_ReturnCode OH_NNTensor_GetOffset(const NN_Tensor *tensor, size_t *offset)
1154```
1155
1156**描述**
1157
1158获取[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)数据所在共享内存上的偏移量。
1159
1160<b>offset</b>是张量数据在对应共享内存上的偏移量,可以通过[OH_NNTensor_CreateWithFd](capi-neural-network-core-h.md#oh_nntensor_createwithfd)接口,连同共享内存文件描述符、共享内存总大小一起被另外的[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)使用。
1161
1162张量数据仅能使用文件描述符<b>fd</b>对应的共享内存中的[offset, size)一段,其中offset是共享内存上的偏移量,可以通过[OH_NNTensor_GetOffset](capi-neural-network-core-h.md#oh_nntensor_getoffset)获取,而size是共享内存的总大小,可以通过[OH_NNTensor_GetSize](capi-neural-network-core-h.md#oh_nntensor_getsize)获取。
1163
1164如果<b>tensor</b>或<b>offset</b>为空指针,该接口将返回错误。
1165
1166**起始版本:** 11
1167
1168**参数:**
1169
1170| 参数项 | 描述 |
1171| -- | -- |
1172| [const NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md) *tensor | 指向[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)实例的指针。 |
1173| size_t *offset | 返回的张量内存fd的偏移量。 |
1174
1175**返回:**
1176
1177| 类型 | 说明 |
1178| -- | -- |
1179| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
1180
1181### OH_NNExecutor_Construct()
1182
1183```
1184OH_NNExecutor *OH_NNExecutor_Construct(OH_NNCompilation *compilation)
1185```
1186
1187**描述**
1188
1189创建[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)执行器实例。
1190
1191该接口接受一个[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例,构造一个与硬件关联的模型推理执行器。通过[OH_NNExecutor_SetInput](capi-neural-network-runtime-h.md#oh_nnexecutor_setinput)设置模型输入数据,设置输入数据后,调用[OH_NNExecutor_Run](capi-neural-network-runtime-h.md#oh_nnexecutor_run)接口执行推理,最后通过[OH_NNExecutor_SetOutput](capi-neural-network-runtime-h.md#oh_nnexecutor_setoutput)获取计算结果。  通过[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例创建一个[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例后,如果不再使用[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例创建其他[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例,就可以销毁[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例了。
1192
1193**起始版本:** 9
1194
1195**参数:**
1196
1197| 参数项 | 描述 |
1198| -- | -- |
1199| [OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md) *compilation | 指向[OH_NNCompilation](capi-neuralnetworkruntime-oh-nncompilation.md)实例的指针。 |
1200
1201**返回:**
1202
1203| 类型 | 说明 |
1204| -- | -- |
1205| [OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) * | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的指针,如果创建失败就返回NULL。 |
1206
1207### OH_NNExecutor_GetOutputShape()
1208
1209```
1210OH_NN_ReturnCode OH_NNExecutor_GetOutputShape(OH_NNExecutor *executor,uint32_t outputIndex,int32_t **shape,uint32_t *shapeLength)
1211```
1212
1213**描述**
1214
1215获取输出张量的维度信息。
1216
1217调用[OH_NNExecutor_Run](capi-neural-network-runtime-h.md#oh_nnexecutor_run)完成单次推理后,该接口获取指定输出的维度信息和维数。在动态形状输入、输出的场景中常用。
1218
1219如果索引值<b>outputIndex</b>达到或超过输出张量的数量,接口将返回错误。输出张量的数量可以通过[OH_NNExecutor_GetOutputCount](capi-neural-network-core-h.md#oh_nnexecutor_getoutputcount)获取。
1220
1221作为输出参数,<b>\*shape</b>不能为空指针,否则会返回错误。例如您应该定义int32_t* tensorShape = NULL,然后将&tensorShape作为参数传入。
1222
1223您无需释放<b>shape</b>的内存,它会随<b>executor</b>一起被释放。
1224
1225**起始版本:** 9
1226
1227**参数:**
1228
1229| 参数项 | 描述 |
1230| -- | -- |
1231| [OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) *executor | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的指针。 |
1232| uint32_t outputIndex | 输出的索引值,与调用[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)时输出数据的顺序一致。假设调用[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)时,outputIndices为{4, 6, 8},则在获取输出张量维度信息时,三个输出的索引值分别为{0, 1, 2}。 |
1233| int32_t **shape | 指向int32_t数组的指针,数组中的每个元素值,是输出张量在每个维度上的长度。 |
1234| uint32_t *shapeLength | uint32_t类型的指针,返回输出的维数。 |
1235
1236**返回:**
1237
1238| 类型 | 说明 |
1239| -- | -- |
1240| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
1241
1242### OH_NNExecutor_Destroy()
1243
1244```
1245void OH_NNExecutor_Destroy(OH_NNExecutor **executor)
1246```
1247
1248**描述**
1249
1250销毁执行器实例,释放执行器占用的内存。
1251
1252调用[OH_NNExecutor_Construct](capi-neural-network-core-h.md#oh_nnexecutor_construct)创建的执行器实例需要调用该接口主动销毁,否则将造成内存泄漏。  如果executor为空指针或者*executor为空指针,该接口仅打印警告日志,不执行销毁操作。
1253
1254**起始版本:** 9
1255
1256
1257**参数:**
1258
1259| 参数项 | 描述 |
1260| -- | -- |
1261| [OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) **executor | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的二级指针。 |
1262
1263### OH_NNExecutor_GetInputCount()
1264
1265```
1266OH_NN_ReturnCode OH_NNExecutor_GetInputCount(const OH_NNExecutor *executor, size_t *inputCount)
1267```
1268
1269**描述**
1270
1271获取输入张量的数量。
1272
1273可以先从executor中获取输入张量的数量,然后通过[OH_NNExecutor_CreateInputTensorDesc](capi-neural-network-core-h.md#oh_nnexecutor_createinputtensordesc)由指定张量索引创建张量描述。
1274
1275**起始版本:** 11
1276
1277
1278**参数:**
1279
1280| 参数项 | 描述 |
1281| -- | -- |
1282| [const OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) *executor | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的指针。 |
1283| size_t *inputCount | 返回的输入张量数量。 |
1284
1285**返回:**
1286
1287| 类型 | 说明 |
1288| -- | -- |
1289| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
1290
1291### OH_NNExecutor_GetOutputCount()
1292
1293```
1294OH_NN_ReturnCode OH_NNExecutor_GetOutputCount(const OH_NNExecutor *executor, size_t *outputCount)
1295```
1296
1297**描述**
1298
1299获取输出张量的数量。
1300
1301可以先从executor中获取输出张量的数量,然后通过[OH_NNExecutor_CreateOutputTensorDesc](capi-neural-network-core-h.md#oh_nnexecutor_createoutputtensordesc)由指定张量索引创建张量描述。
1302
1303**起始版本:** 11
1304
1305
1306**参数:**
1307
1308| 参数项 | 描述 |
1309| -- | -- |
1310| [const OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) *executor | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的指针。 |
1311| size_t *outputCount | 返回的输出张量数量。 |
1312
1313**返回:**
1314
1315| 类型 | 说明 |
1316| -- | -- |
1317| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
1318
1319### OH_NNExecutor_CreateInputTensorDesc()
1320
1321```
1322NN_TensorDesc *OH_NNExecutor_CreateInputTensorDesc(const OH_NNExecutor *executor, size_t index)
1323```
1324
1325**描述**
1326
1327由指定索引值创建一个输入张量的描述。
1328
1329输入张量描述包含了该张量所有类型的属性值。如果索引值<b>index</b>达到或超过输入张量的数量,接口将返回错误码。输入张量的数量可以通过[OH_NNExecutor_GetInputCount](capi-neural-network-core-h.md#oh_nnexecutor_getinputcount)获取。
1330
1331**起始版本:** 11
1332
1333
1334**参数:**
1335
1336| 参数项 | 描述 |
1337| -- | -- |
1338| [const OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) *executor | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的指针。 |
1339| size_t index | 输入张量的索引值。 |
1340
1341**返回:**
1342
1343| 类型 | 说明 |
1344| -- | -- |
1345| [NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md) * | 指向[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的指针,如果创建失败就返回NULL。 |
1346
1347### OH_NNExecutor_CreateOutputTensorDesc()
1348
1349```
1350NN_TensorDesc *OH_NNExecutor_CreateOutputTensorDesc(const OH_NNExecutor *executor, size_t index)
1351```
1352
1353**描述**
1354
1355由指定索引值创建一个输出张量的描述。
1356
1357输出张量描述包含了该张量所有类型的属性值。如果索引值<b>index</b>达到或超过输出张量的数量,接口将返回错误码。输出张量的数量可以通过[OH_NNExecutor_GetOutputCount](capi-neural-network-core-h.md#oh_nnexecutor_getoutputcount)获取。
1358
1359**起始版本:** 11
1360
1361
1362**参数:**
1363
1364| 参数项 | 描述 |
1365| -- | -- |
1366| [const OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) *executor | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的指针。 |
1367| size_t index | 输出张量的索引值。 |
1368
1369**返回:**
1370
1371| 类型 | 说明 |
1372| -- | -- |
1373| [NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md) * | 指向[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)实例的指针,如果创建失败就返回NULL。 |
1374
1375### OH_NNExecutor_GetInputDimRange()
1376
1377```
1378OH_NN_ReturnCode OH_NNExecutor_GetInputDimRange(const OH_NNExecutor *executor,size_t index,size_t **minInputDims,size_t **maxInputDims,size_t *shapeLength)
1379```
1380
1381**描述**
1382
1383获取所有输入张量的维度范围。
1384
1385当输入张量具有动态形状时,它在不同硬件上支持的维度范围可能是不同的,可以通过该接口获取当前设备上支持的维度范围。<b>\*minInputDims</b>保存了指定输入张量的最小维度(维度数与形状匹配),而<b>\*maxInputDims</b>则保存了最大维度。
1386
1387例如,一个输入张量具有动态形状 [-1, -1, -1, 3],那么当前设备上它的<b>\*minInputDims</b>可以是[1, 10, 10, 3],而<b>\*maxInputDims</b>可以是[100, 1024, 1024, 3]。
1388
1389如果索引值<b>index</b>达到或超过输入张量的数量,接口将返回错误。输入张量的数量可以通过[OH_NNExecutor_GetInputCount](capi-neural-network-core-h.md#oh_nnexecutor_getinputcount)获取。
1390
1391作为输出参数,<b>\*minInputDims</b>和<b>\*maxInputDims</b>不能为空指针,否则会返回错误。例如您应该定义int32_t* minInDims = NULL,然后将&minInDims作为参数传入。
1392
1393您无需释放<b>\*minInputDims</b>和<b>\*maxInputDims</b>的内存,它会随<b>executor</b>一起被释放。
1394
1395**起始版本:** 11
1396
1397**参数:**
1398
1399| 参数项 | 描述 |
1400| -- | -- |
1401| [const OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) *executor | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的指针。 |
1402| size_t index | 输入张量的索引值。 |
1403| size_t **minInputDims | 返回的数组的指针,保存了指定输入张量的最小维度(维度数与形状匹配)。 |
1404| size_t **maxInputDims | 返回的数组的指针,保存了指定输入张量的最大维度(维度数与形状匹配)。 |
1405| size_t *shapeLength | 返回的输入张量的维度数量,与形状一致。 |
1406
1407**返回:**
1408
1409| 类型 | 说明 |
1410| -- | -- |
1411| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
1412
1413### OH_NNExecutor_SetOnRunDone()
1414
1415```
1416OH_NN_ReturnCode OH_NNExecutor_SetOnRunDone(OH_NNExecutor *executor, NN_OnRunDone onRunDone)
1417```
1418
1419**描述**
1420
1421设置异步推理结束后的回调处理函数。
1422
1423回调函数的定义详见[NN_OnRunDone](capi-neural-network-runtime-type-h.md#nn_onrundone)。
1424
1425**起始版本:** 11
1426
1427
1428**参数:**
1429
1430| 参数项 | 描述 |
1431| -- | -- |
1432| [OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) *executor | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的指针。 |
1433| [NN_OnRunDone](capi-neural-network-runtime-type-h.md#nn_onrundone) onRunDone | 回调函数句柄[NN_OnRunDone](capi-neural-network-runtime-type-h.md#nn_onrundone)。 |
1434
1435**返回:**
1436
1437| 类型 | 说明 |
1438| -- | -- |
1439| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
1440
1441### OH_NNExecutor_SetOnServiceDied()
1442
1443```
1444OH_NN_ReturnCode OH_NNExecutor_SetOnServiceDied(OH_NNExecutor *executor, NN_OnServiceDied onServiceDied)
1445```
1446
1447**描述**
1448
1449设置异步推理执行期间设备驱动服务突然死亡时的回调处理函数。
1450
1451回调函数的定义详见[NN_OnServiceDied](capi-neural-network-runtime-type-h.md#nn_onservicedied)。
1452
1453**起始版本:** 11
1454
1455
1456**参数:**
1457
1458| 参数项 | 描述 |
1459| -- | -- |
1460| [OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) *executor | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的指针。 |
1461| [NN_OnServiceDied](capi-neural-network-runtime-type-h.md#nn_onservicedied) onServiceDied | 回调函数句柄[NN_OnServiceDied](capi-neural-network-runtime-type-h.md#nn_onservicedied)。 |
1462
1463**返回:**
1464
1465| 类型 | 说明 |
1466| -- | -- |
1467| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
1468
1469### OH_NNExecutor_RunSync()
1470
1471```
1472OH_NN_ReturnCode OH_NNExecutor_RunSync(OH_NNExecutor *executor,NN_Tensor *inputTensor[],size_t inputCount,NN_Tensor *outputTensor[],size_t outputCount)
1473```
1474
1475**描述**
1476
1477执行同步推理。
1478
1479需要先通过[OH_NNTensor_Create](capi-neural-network-core-h.md#oh_nntensor_create)、[OH_NNTensor_CreateWithSize](capi-neural-network-core-h.md#oh_nntensor_createwithsize)或[OH_NNTensor_CreateWithFd](capi-neural-network-core-h.md#oh_nntensor_createwithfd)接口创建输入和输出张量。然后由[OH_NNTensor_GetDataBuffer](capi-neural-network-core-h.md#oh_nntensor_getdatabuffer)获取张量数据指针并向其拷贝输入数据。执行器会通过执行推理产生推理结果,并将结果写入输出张量中。
1480
1481如果输出张量具有动态形状,可以通过[OH_NNExecutor_GetOutputShape](capi-neural-network-core-h.md#oh_nnexecutor_getoutputshape)接口获取输出张量的实际形状。或者通过[OH_NNTensor_GetTensorDesc](capi-neural-network-core-h.md#oh_nntensor_gettensordesc)接口从输入张量中获取张量描述,然后通过[OH_NNTensorDesc_GetShape](capi-neural-network-core-h.md#oh_nntensordesc_getshape)接口获取实际形状。
1482
1483**起始版本:** 11
1484
1485
1486**参数:**
1487
1488| 参数项                                                                   | 描述 |
1489|-----------------------------------------------------------------------| -- |
1490| [OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) *executor | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的指针。 |
1491| [NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md) *inputTensor[]                                          | 输入张量的数组。 |
1492| size_t inputCount                                                     | 输入张量的数量。 |
1493| [NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md) *outputTensor[]                                             | 输出张量的数组。 |
1494| size_t outputCount                                                    | 输出张量的数量。 |
1495
1496**返回:**
1497
1498| 类型 | 说明 |
1499| -- | -- |
1500| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
1501
1502### OH_NNExecutor_RunAsync()
1503
1504```
1505OH_NN_ReturnCode OH_NNExecutor_RunAsync(OH_NNExecutor *executor,NN_Tensor *inputTensor[],size_t inputCount,NN_Tensor *outputTensor[],size_t outputCount,int32_t timeout,void *userData)
1506```
1507
1508**描述**
1509
1510执行异步推理。
1511
1512需要先通过[OH_NNTensor_Create](capi-neural-network-core-h.md#oh_nntensor_create)、[OH_NNTensor_CreateWithSize](capi-neural-network-core-h.md#oh_nntensor_createwithsize)或[OH_NNTensor_CreateWithFd](capi-neural-network-core-h.md#oh_nntensor_createwithfd)接口创建输入和输出张量。然后由[OH_NNTensor_GetDataBuffer](capi-neural-network-core-h.md#oh_nntensor_getdatabuffer)获取张量数据指针并向其拷贝输入数据。执行器会通过执行推理产生推理结果,并将结果写入输出张量中。
1513
1514如果输出张量具有动态形状,可以通过[OH_NNExecutor_GetOutputShape](capi-neural-network-core-h.md#oh_nnexecutor_getoutputshape)接口获取输出张量的实际形状。或者通过[OH_NNTensor_GetTensorDesc](capi-neural-network-core-h.md#oh_nntensor_gettensordesc)接口从输入张量中获取张量描述,然后通过[OH_NNTensorDesc_GetShape](capi-neural-network-core-h.md#oh_nntensordesc_getshape)接口获取实际形状。
1515
1516该接口是非阻塞式的,调用后会立刻返回,而推理结果、执行返回状态可以通过回调函数[NN_OnRunDone](capi-neural-network-runtime-type-h.md#nn_onrundone)来获取。如果设备驱动服务在执行过程中异常终止,可以通过回调函数[NN_OnServiceDied](capi-neural-network-runtime-type-h.md#nn_onservicedied)来处理。
1517
1518可以通过接口[OH_NNExecutor_SetOnRunDone](capi-neural-network-core-h.md#oh_nnexecutor_setonrundone)和[OH_NNExecutor_SetOnServiceDied](capi-neural-network-core-h.md#oh_nnexecutor_setonservicedied)设置回调函数[NN_OnRunDone](capi-neural-network-runtime-type-h.md#nn_onrundone)和[NN_OnServiceDied](capi-neural-network-runtime-type-h.md#nn_onservicedied)。
1519
1520如果推理时长超过了timeout,会立刻终止推理,回调函数[NN_OnRunDone](capi-neural-network-runtime-type-h.md#nn_onrundone)的errCode参数会返回[OH_NN_TIMEOUT](capi-neural-network-runtime-type-h.md#oh_nn_returncode)错误。
1521
1522userData是区分不同次异步执行的标识符,会作为回调函数的第一个参数返回,您可以使用能够区分不同次执行的任意数据作为标识符。
1523
1524**起始版本:** 11
1525
1526
1527**参数:**
1528
1529| 参数项 | 描述 |
1530| -- | -- |
1531| [OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) *executor | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的指针。 |
1532| [NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md) *inputTensor[] | 输入张量的数组。 |
1533| size_t inputCount | 输入张量的数量。 |
1534| [NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md) *outputTensor[] | 输出张量的数组。 |
1535| size_t outputCount | 输出张量的数量。 |
1536| int32_t timeout | 异步推理的超时时间(单位ms),例如1000。 |
1537| void *userData | 异步执行的标识符。 |
1538
1539**返回:**
1540
1541| 类型 | 说明 |
1542| -- | -- |
1543| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
1544
1545### OH_NNDevice_GetAllDevicesID()
1546
1547```
1548OH_NN_ReturnCode OH_NNDevice_GetAllDevicesID(const size_t **allDevicesID, uint32_t *deviceCount)
1549```
1550
1551**描述**
1552
1553获取对接到Neural Network Runtime的硬件ID。
1554
1555每个硬件存在唯一且固定的ID,该接口通过uint32_t数组返回当前设备上已经对接的硬件ID。
1556
1557硬件ID通过size_t数组返回,数组的每个元素是单个硬件的ID值。数组内存由内部进行管理,在下次调用该接口前,数据指针将一直有效。
1558
1559**起始版本:** 9
1560
1561
1562**参数:**
1563
1564| 参数项 | 描述 |
1565| -- | -- |
1566| const size_t **allDevicesID | 指向size_t数组的指针。要求传入的*allDevicesID为空指针,否则将返回错误码[OH_NN_INVALID_PARAMETER](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
1567| uint32_t *deviceCount | uint32_t类型的指针,用于返回*allDevicesID的长度。 |
1568
1569**返回:**
1570
1571| 类型 | 说明 |
1572| -- | -- |
1573| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
1574
1575### OH_NNDevice_GetName()
1576
1577```
1578OH_NN_ReturnCode OH_NNDevice_GetName(size_t deviceID, const char **name)
1579```
1580
1581**描述**
1582
1583获取指定硬件的名称。
1584
1585通过deviceID指定计算硬件,获取硬件的名称。硬件ID需要调用[OH_NNDevice_GetAllDevicesID](capi-neural-network-core-h.md#oh_nndevice_getalldevicesid)获取。如果deviceID是0,那么会默认使用设备列表中的第一个设备。 \*name是一个C风格的字符串,以'\0'作为结束符。\*name必须是一个空指针,否则接口会返回[OH_NN_INVALID_PARAMETER](capi-neural-network-runtime-type-h.md#oh_nn_returncode)错误。例如您应该定义char* deviceName = NULL,然后将&deviceName作为参数传入。
1586
1587**起始版本:** 9
1588
1589**参数:**
1590
1591| 参数项 | 描述 |
1592| -- | -- |
1593| size_t deviceID | 指定硬件ID。如果deviceID是0,那么会默认使用设备列表中的第一个设备。 |
1594| const char **name | 指向char数组的指针,保存返回的硬件名称。 |
1595
1596**返回:**
1597
1598| 类型 | 说明 |
1599| -- | -- |
1600| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
1601
1602### OH_NNDevice_GetType()
1603
1604```
1605OH_NN_ReturnCode OH_NNDevice_GetType(size_t deviceID, OH_NN_DeviceType *deviceType)
1606```
1607
1608**描述**
1609
1610获取指定硬件的类别信息。
1611
1612通过deviceID指定计算硬件,获取硬件的类别。如果deviceID是0,那么会默认使用设备列表中的第一个设备。目前支持的设备类型有:
1613
1614- CPU设备:OH_NN_CPU
1615
1616- GPU设备:OH_NN_GPU
1617- 机器学习专用加速器:OH_NN_ACCELERATOR
1618- 不属于以上类型的其他硬件类型:OH_NN_OTHERS
1619
1620**起始版本:** 9
1621
1622
1623**参数:**
1624
1625| 参数项 | 描述 |
1626| -- | -- |
1627| size_t deviceID | 指定硬件ID。如果deviceID是0,那么会默认使用设备列表中的第一个设备。 |
1628| [OH_NN_DeviceType](capi-neural-network-runtime-type-h.md#oh_nn_devicetype) *deviceType | 指向[OH_NN_DeviceType](capi-neural-network-runtime-type-h.md#oh_nn_devicetype)实例的指针,返回硬件的类别信息。 |
1629
1630**返回:**
1631
1632| 类型 | 说明 |
1633| -- | -- |
1634| [OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode) | 函数执行的结果状态。执行成功返回OH_NN_SUCCESS;失败返回具体错误码,具体失败错误码可参考[OH_NN_ReturnCode](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 |
1635
1636
1637