1# neural_network_runtime.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## 概述 10 11Neural Network Runtime模块接口定义,AI推理框架使用Neural Network Runtime提供的Native接口,完成模型构建。 12 13Neural Network Runtime的接口目前均不支持多线程并发调用。 14 15**引用文件:** <neural_network_runtime/neural_network_runtime.h> 16 17**库:** libneural_network_runtime.so 18 19**系统能力:** SystemCapability.Ai.NeuralNetworkRuntime 20 21**起始版本:** 9 22 23**相关模块:** [NeuralNetworkRuntime](capi-neuralnetworkruntime.md) 24 25## 汇总 26 27### 函数 28 29| 名称 | 描述 | 30| -- | -- | 31| [NN_QuantParam *OH_NNQuantParam_Create()](#oh_nnquantparam_create) | 创建一个[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)量化参数实例。 | 32| [OH_NN_ReturnCode OH_NNQuantParam_SetScales(NN_QuantParam *quantParams, const double *scales, size_t quantCount)](#oh_nnquantparam_setscales) | 设置[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)的缩放系数。<br>参数<b>quantCount</b>是张量中量化参数的数量,例如对于per-channel量化,<b>quantCount</b>就是通道数量。 | 33| [OH_NN_ReturnCode OH_NNQuantParam_SetZeroPoints(NN_QuantParam *quantParams, const int32_t *zeroPoints, size_t quantCount)](#oh_nnquantparam_setzeropoints) | 设置[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)的零点。<br>参数<b>quantCount</b>是张量中量化参数的数量,例如对于per-channel量化,<b>quantCount</b>就是通道数量。 | 34| [OH_NN_ReturnCode OH_NNQuantParam_SetNumBits(NN_QuantParam *quantParams, const uint32_t *numBits, size_t quantCount)](#oh_nnquantparam_setnumbits) | 设置[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)的量化位数。<br>参数<b>quantCount</b>是张量中量化参数的数量,例如对于per-channel量化,<b>quantCount</b>就是通道数量。 | 35| [OH_NN_ReturnCode OH_NNQuantParam_Destroy(NN_QuantParam **quantParams)](#oh_nnquantparam_destroy) | 销毁[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)实例。 <br>当设置[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)实例到一个[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)中后,如果不再使用该实例,需要销毁它以避免内存泄漏。 <br>如果<b>quantParams</b>或<b>*quantParams</b>是空指针,那么该接口仅打印警告日志,不会执行销毁操作。 | 36| [OH_NNModel *OH_NNModel_Construct(void)](#oh_nnmodel_construct) | 创建[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)类型的模型实例,搭配OH_NNModel模块提供的其他接口,完成模型实例的构造。 | 37| [OH_NN_ReturnCode OH_NNModel_AddTensorToModel(OH_NNModel *model, const NN_TensorDesc *tensorDesc)](#oh_nnmodel_addtensortomodel) | 向模型实例中添加张量。<br>Neural Network Runtime模型中的数据节点和算子参数均由模型的张量构成。 | 38| [OH_NN_ReturnCode OH_NNModel_SetTensorData(OH_NNModel *model, uint32_t index, const void *dataBuffer, size_t length)](#oh_nnmodel_settensordata) | 设置张量的数值。 对于具有常量值的张量(如模型的权重),需要在构图阶段使用该接口设置数值。张量的索引值根据张量添加进模型的顺序决定,张量的添加参考[OH_NNModel_AddTensorToModel](capi-neural-network-runtime-h.md#oh_nnmodel_addtensortomodel)。 | 39| [OH_NN_ReturnCode OH_NNModel_SetTensorQuantParams(OH_NNModel *model, uint32_t index, NN_QuantParam *quantParam)](#oh_nnmodel_settensorquantparams) | 设置张量的量化参数,参考[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)。 | 40| [OH_NN_ReturnCode OH_NNModel_SetTensorType(OH_NNModel *model, uint32_t index, OH_NN_TensorType tensorType)](#oh_nnmodel_settensortype) | 设置张量的类型,参考[OH_NN_TensorType](capi-neural-network-runtime-type-h.md#oh_nn_tensortype)。 | 41| [OH_NN_ReturnCode 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)](#oh_nnmodel_addoperation) | 向模型实例中添加算子。 | 42| [OH_NN_ReturnCode OH_NNModel_SpecifyInputsAndOutputs(OH_NNModel *model,const OH_NN_UInt32Array *inputIndices,const OH_NN_UInt32Array *outputIndices)](#oh_nnmodel_specifyinputsandoutputs) | 指定模型的输入和输出张量的索引值。模型实例需要指定张量作为端到端的输入和输出张量。 | 43| [OH_NN_ReturnCode OH_NNModel_Finish(OH_NNModel *model)](#oh_nnmodel_finish) | 完成模型构图。完成模型拓扑结构的搭建后,调用该接口指示构图已完成。 | 44| [void OH_NNModel_Destroy(OH_NNModel **model)](#oh_nnmodel_destroy) | 销毁模型实例。调用[OH_NNModel_Construct](capi-neural-network-runtime-h.md#oh_nnmodel_construct)创建的模型实例需要调用该接口主动销毁,否则将造成内存泄漏。 如果model为空指针或者*model为空指针,该接口仅打印警告日志,不执行销毁操作。 | 45| [OH_NN_ReturnCode OH_NNModel_GetAvailableOperations(OH_NNModel *model,size_t deviceID,const bool **isSupported,uint32_t *opCount)](#oh_nnmodel_getavailableoperations) | 查询硬件对模型内所有算子的支持情况,通过布尔值序列指示支持情况。 | 46| [OH_NN_ReturnCode OH_NNModel_AddTensor(OH_NNModel *model, const OH_NN_Tensor *tensor)](#oh_nnmodel_addtensor) | 向模型实例中添加张量。<br>Neural Network Runtime模型中的数据节点和算子参数均由模型的张量构成,该接口根据tensor,向model实例中添加张量。 | 47| [OH_NN_ReturnCode OH_NNExecutor_SetInput(OH_NNExecutor *executor,uint32_t inputIndex,const OH_NN_Tensor *tensor,const void *dataBuffer,size_t length)](#oh_nnexecutor_setinput) | 设置模型单个输入的数据。该接口将dataBuffer中,长度为length个字节的数据,拷贝到底层硬件的共享内存。 | 48| [OH_NN_ReturnCode OH_NNExecutor_SetOutput(OH_NNExecutor *executor,uint32_t outputIndex,void *dataBuffer,size_t length)](#oh_nnexecutor_setoutput) | 设置模型单个输出的内存。<br>该接口将dataBuffer指向的内存与outputIndex指定的输出绑定,内存的长度由length指定。 | 49| [OH_NN_ReturnCode OH_NNExecutor_Run(OH_NNExecutor *executor)](#oh_nnexecutor_run) | 执行推理。 在执行器关联的硬件上,执行模型的端到端推理计算。 | 50| [OH_NN_Memory *OH_NNExecutor_AllocateInputMemory(OH_NNExecutor *executor, uint32_t inputIndex, size_t length)](#oh_nnexecutor_allocateinputmemory) | 在硬件上为单个输入申请共享内存。 | 51| [OH_NN_Memory *OH_NNExecutor_AllocateOutputMemory(OH_NNExecutor *executor, uint32_t outputIndex, size_t length)](#oh_nnexecutor_allocateoutputmemory) | 在硬件上为单个输出申请共享内存。 | 52| [void OH_NNExecutor_DestroyInputMemory(OH_NNExecutor *executor, uint32_t inputIndex, OH_NN_Memory **memory)](#oh_nnexecutor_destroyinputmemory) | 释放[OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md)实例指向的输入内存。 调用[OH_NNExecutor_AllocateInputMemory](capi-neural-network-runtime-h.md#oh_nnexecutor_allocateinputmemory)创建的内存实例,需要主动调用该接口进行释放,否则将造成内存泄漏。 | 53| [void OH_NNExecutor_DestroyOutputMemory(OH_NNExecutor *executor, uint32_t outputIndex, OH_NN_Memory **memory)](#oh_nnexecutor_destroyoutputmemory) | 释放[OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md)实例指向的输出内存。 调用[OH_NNExecutor_AllocateOutputMemory](capi-neural-network-runtime-h.md#oh_nnexecutor_allocateoutputmemory)创建的内存实例,需要主动调用该接口进行释放,否则将造成内存泄漏。outputIndex和memory的对应关系需要和创建内存实例时保持一致。 如果memory或*memory为空指针,该接口仅打印警告日志,不执行释放操作。 | 54| [OH_NN_ReturnCode OH_NNExecutor_SetInputWithMemory(OH_NNExecutor *executor,uint32_t inputIndex,const OH_NN_Tensor *tensor,const OH_NN_Memory *memory)](#oh_nnexecutor_setinputwithmemory) | 将[OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md)实例指向的硬件共享内存,并指定为单个输入使用的内存。 | 55| [OH_NN_ReturnCode OH_NNExecutor_SetOutputWithMemory(OH_NNExecutor *executor,uint32_t outputIndex,const OH_NN_Memory *memory)](#oh_nnexecutor_setoutputwithmemory) | 将[OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md)实例指向的硬件共享内存,并指定为单个输出使用的内存。 | 56 57## 函数说明 58 59### OH_NNQuantParam_Create() 60 61``` 62NN_QuantParam *OH_NNQuantParam_Create() 63``` 64 65**描述** 66 67创建一个[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)量化参数实例。 68 69创建[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)量化参数实例后,调用[OH_NNQuantParam_SetScales](capi-neural-network-runtime-h.md#oh_nnquantparam_setscales)、[OH_NNQuantParam_SetZeroPoints](capi-neural-network-runtime-h.md#oh_nnquantparam_setzeropoints)或[OH_NNQuantParam_SetNumBits](capi-neural-network-runtime-h.md#oh_nnquantparam_setnumbits)设置它的属性值,并调用[OH_NNModel_SetTensorQuantParams](capi-neural-network-runtime-h.md#oh_nnmodel_settensorquantparams)将它设置到[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)中。最后再调用[OH_NNQuantParam_Destroy](capi-neural-network-runtime-h.md#oh_nnquantparam_destroy)销毁它,以避免内存泄露。 70 71**起始版本:** 11 72 73**返回:** 74 75| 类型 | 说明 | 76| -- | -- | 77| [NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md) * | 指向[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)实例的指针,如果创建失败就返回NULL。 | 78 79### OH_NNQuantParam_SetScales() 80 81``` 82OH_NN_ReturnCode OH_NNQuantParam_SetScales(NN_QuantParam *quantParams, const double *scales, size_t quantCount) 83``` 84 85**描述** 86 87设置[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)的缩放系数。 88 89参数<b>quantCount</b>是张量中量化参数的数量,例如对于per-channel量化,<b>quantCount</b>就是通道数量。 90 91**起始版本:** 11 92 93 94**参数:** 95 96| 参数项 | 描述 | 97| -- | -- | 98| [NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md) *quantParams | 指向[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)实例的指针。 | 99| const double *scales | 张量中所有量化参数的缩放系数构成的数组。 | 100| size_t quantCount | 张量中量化参数的数量。 | 101 102**返回:** 103 104| 类型 | 说明 | 105| -- | -- | 106| [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)。 | 107 108### OH_NNQuantParam_SetZeroPoints() 109 110``` 111OH_NN_ReturnCode OH_NNQuantParam_SetZeroPoints(NN_QuantParam *quantParams, const int32_t *zeroPoints, size_t quantCount) 112``` 113 114**描述** 115 116设置[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)的零点。 117 118参数<b>quantCount</b>是张量中量化参数的数量,例如对于per-channel量化,<b>quantCount</b>就是通道数量。 119 120**起始版本:** 11 121 122 123**参数:** 124 125| 参数项 | 描述 | 126| -- | -- | 127| [NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md) *quantParams | 指向[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)实例的指针。 | 128| const int32_t *zeroPoints | 张量中所有量化参数的零点构成的数组。 | 129| size_t quantCount | 张量中量化参数的数量。 | 130 131**返回:** 132 133| 类型 | 说明 | 134| -- | -- | 135| [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)。 | 136 137### OH_NNQuantParam_SetNumBits() 138 139``` 140OH_NN_ReturnCode OH_NNQuantParam_SetNumBits(NN_QuantParam *quantParams, const uint32_t *numBits, size_t quantCount) 141``` 142 143**描述** 144 145设置[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)的量化位数。 146 147参数<b>quantCount</b>是张量中量化参数的数量,例如对于per-channel量化,<b>quantCount</b>就是通道数量。 148 149**起始版本:** 11 150 151 152**参数:** 153 154| 参数项 | 描述 | 155| -- | -- | 156| [NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md) *quantParams | 指向[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)实例的指针。 | 157| const uint32_t *numBits | 张量中所有量化参数的量化位数构成的数组。 | 158| size_t quantCount | 张量中量化参数的数量。 | 159 160**返回:** 161 162| 类型 | 说明 | 163| -- | -- | 164| [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)。 | 165 166### OH_NNQuantParam_Destroy() 167 168``` 169OH_NN_ReturnCode OH_NNQuantParam_Destroy(NN_QuantParam **quantParams) 170``` 171 172**描述** 173 174销毁[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)实例。 175 176当设置[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)实例到一个[NN_Tensor](capi-neuralnetworkruntime-nn-tensor.md)中后,如果不再使用该实例,需要销毁它以避免内存泄漏。 <br>如果<b>quantParams</b>或<b>*quantParams</b>是空指针,那么该接口仅打印警告日志,不会执行销毁操作。 177 178**起始版本:** 11 179 180 181**参数:** 182 183| 参数项 | 描述 | 184| -- | -- | 185| [NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md) **quantParams | 指向[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)实例的二级指针。 | 186 187**返回:** 188 189| 类型 | 说明 | 190| -- | -- | 191| [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)。 | 192 193### OH_NNModel_Construct() 194 195``` 196OH_NNModel *OH_NNModel_Construct(void) 197``` 198 199**描述** 200 201创建[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)类型的模型实例,搭配OH_NNModel模块提供的其他接口,完成模型实例的构造。 202 203在开始构图前,先调用[OH_NNModel_Construct](capi-neural-network-runtime-h.md#oh_nnmodel_construct)创建模型实例,根据模型的拓扑结构,调用[OH_NNModel_AddTensorToModel](capi-neural-network-runtime-h.md#oh_nnmodel_addtensortomodel)、[OH_NNModel_AddOperation](capi-neural-network-runtime-h.md#oh_nnmodel_addoperation)和[OH_NNModel_SetTensorData](capi-neural-network-runtime-h.md#oh_nnmodel_settensordata)方法,填充模型的数据节点和算子节点;然后调用[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)指定模型的输入和输出;当构造完模型的拓扑结构,调用[OH_NNModel_Finish](capi-neural-network-runtime-h.md#oh_nnmodel_finish)完成模型的构建。 204 205模型实例使用完毕后,需要调用[OH_NNModel_Destroy](capi-neural-network-runtime-h.md#oh_nnmodel_destroy)销毁模型实例,避免内存泄漏。 206 207**起始版本:** 9 208 209**返回:** 210 211| 类型 | 说明 | 212| -- | -- | 213| [OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md) * | 返回一个指向[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)实例的指针,如果创建失败就返回NULL。 | 214 215### OH_NNModel_AddTensorToModel() 216 217``` 218OH_NN_ReturnCode OH_NNModel_AddTensorToModel(OH_NNModel *model, const NN_TensorDesc *tensorDesc) 219``` 220 221**描述** 222 223向模型实例中添加张量。 224 225Neural Network Runtime模型中的数据节点和算子参数均由模型的张量构成。 226 227该接口根据[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)向model实例中添加张量,张量添加的顺序是模型中记录张量的索引值。[OH_NNModel_SetTensorData](capi-neural-network-runtime-h.md#oh_nnmodel_settensordata)、[OH_NNModel_AddOperation](capi-neural-network-runtime-h.md#oh_nnmodel_addoperation)和[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)接口根据该索引值,指定不同的张量。 228 229Neural Network Runtime支持动态形状的输入和输出张量。在添加动态形状的数据节点时,需要将tensor.dimensions中支持动态变化的维度设置为-1。例如可将一个四维tensor的dimensions设置为[1, -1, 2, 2],表示其第二个维度支持动态变化。 230 231**起始版本:** 11 232 233 234**参数:** 235 236| 参数项 | 描述 | 237| -- | -- | 238| [OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md) *model | 指向[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)实例的指针。 | 239| [const NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md) *tensorDesc | [NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)张量的指针,[NN_TensorDesc](capi-neuralnetworkruntime-nn-tensordesc.md)指定了添加到模型实例中张量的属性。 | 240 241**返回:** 242 243| 类型 | 说明 | 244| -- | -- | 245| [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)。 | 246 247### OH_NNModel_SetTensorData() 248 249``` 250OH_NN_ReturnCode OH_NNModel_SetTensorData(OH_NNModel *model, uint32_t index, const void *dataBuffer, size_t length) 251``` 252 253**描述** 254 255设置张量的数值。 对于具有常量值的张量(如模型的权重),需要在构图阶段使用该接口设置数值。 256 257张量的索引值根据张量添加进模型的顺序决定,张量的添加参考[OH_NNModel_AddTensorToModel](capi-neural-network-runtime-h.md#oh_nnmodel_addtensortomodel)。 258 259**起始版本:** 9 260 261 262**参数:** 263 264| 参数项 | 描述 | 265| -- | -- | 266| [OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md) *model | 指向[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)实例的指针。 | 267| uint32_t index | 张量的索引值。 | 268| const void *dataBuffer | 指向真实数据内存的指针。 | 269| size_t length | 数据内存的长度。 | 270 271**返回:** 272 273| 类型 | 说明 | 274| -- | -- | 275| [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)。 | 276 277### OH_NNModel_SetTensorQuantParams() 278 279``` 280OH_NN_ReturnCode OH_NNModel_SetTensorQuantParams(OH_NNModel *model, uint32_t index, NN_QuantParam *quantParam) 281``` 282 283**描述** 284 285设置张量的量化参数,参考[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)。 286 287**起始版本:** 11 288 289 290**参数:** 291 292| 参数项 | 描述 | 293| -- | -- | 294| [OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md) *model | 指向[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)实例的指针。 | 295| uint32_t index | 张量的索引值。 | 296| [NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md) *quantParam | 指向[NN_QuantParam](capi-neuralnetworkruntime-nn-quantparam.md)的指针。 | 297 298**返回:** 299 300| 类型 | 说明 | 301| -- | -- | 302| [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)。 | 303 304### OH_NNModel_SetTensorType() 305 306``` 307OH_NN_ReturnCode OH_NNModel_SetTensorType(OH_NNModel *model, uint32_t index, OH_NN_TensorType tensorType) 308``` 309 310**描述** 311 312设置张量的类型,参考[OH_NN_TensorType](capi-neural-network-runtime-type-h.md#oh_nn_tensortype)。 313 314**起始版本:** 11 315 316 317**参数:** 318 319| 参数项 | 描述 | 320| -- | -- | 321| [OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md) *model | 指向[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)实例的指针。 | 322| uint32_t index | 张量的索引值。 | 323| [OH_NN_TensorType](capi-neural-network-runtime-type-h.md#oh_nn_tensortype) tensorType | 张量类型。 | 324 325**返回:** 326 327| 类型 | 说明 | 328| -- | -- | 329| [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) | 330 331### OH_NNModel_AddOperation() 332 333``` 334OH_NN_ReturnCode 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) 335``` 336 337**描述** 338 339向模型实例中添加算子。 340 341该接口向模型实例中添加算子,算子类型由op指定,算子的参数、输入和输出由paramIndices、inputIndices和outputIndices指定。 342 343该接口将对算子参数的属性和输入、输出张量的数量进行校验,这些属性需要在调用[OH_NNModel_AddTensorToModel](capi-neural-network-runtime-h.md#oh_nnmodel_addtensortomodel)添加张量时正确设置。 344 345每个算子期望的参数、输入和输出属性请参考[OH_NN_OperationType](capi-neural-network-runtime-type-h.md#oh_nn_operationtype)。 346 347paramIndices、inputIndices和outputIndices中存储的是张量的索引值,每个索引值根据张量添加进模型的顺序决定,正确设置并添加算子要求准确设置每个张量的索引值。 348 349张量的添加参考[OH_NNModel_AddTensorToModel](capi-neural-network-runtime-h.md#oh_nnmodel_addtensortomodel)。 350 351如果添加算子时,添加了额外的参数(非算子需要的参数),该接口返回[OH_NN_INVALID_PARAMETER](capi-neural-network-runtime-type-h.md#oh_nn_returncode);如果没有设置算子参数,则算子按默认值设置缺省的参数,默认值请参考[OH_NN_OperationType](capi-neural-network-runtime-type-h.md#oh_nn_operationtype)。 352 353**起始版本:** 9 354 355 356**参数:** 357 358| 参数项 | 描述 | 359| -- | -- | 360| [OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md) *model | 指向[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)实例的指针。 | 361| [OH_NN_OperationType](capi-neural-network-runtime-type-h.md#oh_nn_operationtype) op | 指定添加的算子类型,取值请参考[OH_NN_OperationType](capi-neural-network-runtime-type-h.md#oh_nn_operationtype)的枚举值。 | 362| [const OH_NN_UInt32Array](capi-neuralnetworkruntime-oh-nn-uint32array.md) *paramIndices | OH_NN_UInt32Array实例的指针,设置算子的参数张量索引。 | 363| [const OH_NN_UInt32Array](capi-neuralnetworkruntime-oh-nn-uint32array.md) *inputIndices | OH_NN_UInt32Array实例的指针,指定算子的输入张量索引。 | 364| [const OH_NN_UInt32Array](capi-neuralnetworkruntime-oh-nn-uint32array.md) *outputIndices | OH_NN_UInt32Array实例的指针,设置算子的输出张量索引。 | 365 366**返回:** 367 368| 类型 | 说明 | 369| -- | -- | 370| [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)。 | 371 372### OH_NNModel_SpecifyInputsAndOutputs() 373 374``` 375OH_NN_ReturnCode OH_NNModel_SpecifyInputsAndOutputs(OH_NNModel *model,const OH_NN_UInt32Array *inputIndices,const OH_NN_UInt32Array *outputIndices) 376``` 377 378**描述** 379 380指定模型的输入和输出张量的索引值。 381 382模型实例需要指定张量作为端到端的输入和输出张量。设置一个张量为输入或输出张量后,就不能再通过调用[OH_NNModel_SetTensorData](capi-neural-network-runtime-h.md#oh_nnmodel_settensordata)设置张量数据,而需要在执行阶段调用OH_NNExecutor的方法设置输入或输出张量数据。 383 384张量的索引值根据张量添加进模型的顺序决定,张量的添加参考[OH_NNModel_AddTensorToModel](capi-neural-network-runtime-h.md#oh_nnmodel_addtensortomodel)。 暂时不支持异步设置模型输入和输出张量。 385 386**起始版本:** 9 387 388 389**参数:** 390 391| 参数项 | 描述 | 392| -- | -- | 393| [OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md) *model | 指向[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)实例的指针。 | 394| [const OH_NN_UInt32Array](capi-neuralnetworkruntime-oh-nn-uint32array.md) *inputIndices | OH_NN_UInt32Array实例的指针,指定算子的输入张量。 | 395| [const OH_NN_UInt32Array](capi-neuralnetworkruntime-oh-nn-uint32array.md) *outputIndices | OH_NN_UInt32Array实例的指针,指定算子的输出张量。 | 396 397**返回:** 398 399| 类型 | 说明 | 400| -- | -- | 401| [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)。 | 402 403### OH_NNModel_Finish() 404 405``` 406OH_NN_ReturnCode OH_NNModel_Finish(OH_NNModel *model) 407``` 408 409**描述** 410 411完成模型构图。 412 413完成模型拓扑结构的搭建后,调用该接口指示构图已完成。 414 415在调用该接口后,无法进行额外的构图操作,调用[OH_NNModel_AddTensorToModel](capi-neural-network-runtime-h.md#oh_nnmodel_addtensortomodel)、[OH_NNModel_AddOperation](capi-neural-network-runtime-h.md#oh_nnmodel_addoperation)、[OH_NNModel_SetTensorData](capi-neural-network-runtime-h.md#oh_nnmodel_settensordata)和[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)将返回[OH_NN_OPERATION_FORBIDDEN](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 416 417在调用[OH_NNModel_GetAvailableOperations](capi-neural-network-runtime-h.md#oh_nnmodel_getavailableoperations)和[OH_NNCompilation_Construct](capi-neural-network-core-h.md#oh_nncompilation_construct)之前,必须先调用该接口完成构图。 418 419**起始版本:** 9 420 421 422**参数:** 423 424| 参数项 | 描述 | 425| -- | -- | 426| [OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md) *model | 指向[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)实例的指针。 | 427 428**返回:** 429 430| 类型 | 说明 | 431| -- | -- | 432| [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)。 | 433 434### OH_NNModel_Destroy() 435 436``` 437void OH_NNModel_Destroy(OH_NNModel **model) 438``` 439 440**描述** 441 442销毁模型实例。 443 444调用[OH_NNModel_Construct](capi-neural-network-runtime-h.md#oh_nnmodel_construct)创建的模型实例需要调用该接口主动销毁,否则将造成内存泄漏。 445 446如果model为空指针或者*model为空指针,该接口仅打印警告日志,不执行销毁操作。 447 448**起始版本:** 9 449 450 451**参数:** 452 453| 参数项 | 描述 | 454| -- | -- | 455| [OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md) **model | 指向[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)实例的二级指针。模型实例销毁后,该接口会将*model主动设置为空指针。 | 456 457### OH_NNModel_GetAvailableOperations() 458 459``` 460OH_NN_ReturnCode OH_NNModel_GetAvailableOperations(OH_NNModel *model,size_t deviceID,const bool **isSupported,uint32_t *opCount) 461``` 462 463**描述** 464 465查询硬件对模型内所有算子的支持情况,通过布尔值序列指示支持情况。 466 467查询底层硬件对模型实例内每个算子的支持情况,硬件由deviceID指定,结果将通过isSupported指向的数组表示。 468 469如果支持第i个算子,则(\*isSupported)[i] == true,否则为false。 该接口成功执行后,(\*isSupported)将指向记录算子支持情况的bool数组,数组长度和模型实例的算子数量相等。 470 471该数组对应的内存由Neural Network Runtime管理,在模型实例销毁或再次调用该接口后自动销毁。 472 473**起始版本:** 9 474 475 476**参数:** 477 478| 参数项 | 描述 | 479| -- | -- | 480| [OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md) *model | 指向[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)实例的指针。 | 481| size_t deviceID | 指定查询的硬件ID,通过[OH_NNDevice_GetAllDevicesID](capi-neural-network-core-h.md#oh_nndevice_getalldevicesid)获取。 | 482| const bool **isSupported | 指向bool数组的指针。调用该接口时,要求(*isSupported)为空指针,否则返回[OH_NN_INVALID_PARAMETER](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 | 483| uint32_t *opCount | 模型实例中算子的数量,对应(*isSupported)数组的长度。 | 484 485**返回:** 486 487| 类型 | 说明 | 488| -- | -- | 489| [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)。 | 490 491### OH_NNModel_AddTensor() 492 493``` 494OH_NN_ReturnCode OH_NNModel_AddTensor(OH_NNModel *model, const OH_NN_Tensor *tensor) 495``` 496 497**描述** 498 499向模型实例中添加张量。 500 501Neural Network Runtime模型中的数据节点和算子参数均由模型的张量构成,该接口根据tensor,向model实例中添加张量。 502 503张量添加的顺序由模型中记录张量的索引值来确定,[OH_NNModel_SetTensorData](capi-neural-network-runtime-h.md#oh_nnmodel_settensordata)、[OH_NNModel_AddOperation](capi-neural-network-runtime-h.md#oh_nnmodel_addoperation)和[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)接口根据该索引值,指定不同的张量。 504 505Neural Network Runtime支持动态形状输入和输出。 506 507在添加动态形状的数据节点时,需要将tensor.dimensions中支持动态变化的维度设置为-1。例如可将一个四维tensor的dimensions设置为[1, -1, 2, 2],表示其第二个维度支持动态变化。 508 509**起始版本:** 9 510 511**废弃版本:** 11 512 513**替代接口:** [OH_NNModel_AddTensorToModel](capi-neural-network-runtime-h.md#oh_nnmodel_addtensortomodel) 514 515 516**参数:** 517 518| 参数项 | 描述 | 519| -- | -- | 520| [OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md) *model | 指向[OH_NNModel](capi-neuralnetworkruntime-oh-nnmodel.md)实例的指针。 | 521| [const OH_NN_Tensor](capi-neuralnetworkruntime-oh-nn-tensor.md) *tensor | [OH_NN_Tensor](capi-neuralnetworkruntime-oh-nn-tensor.md)张量的指针,tensor指定了添加到模型实例中张量的属性。 | 522 523**返回:** 524 525| 类型 | 说明 | 526| -- | -- | 527| [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)。 | 528 529### OH_NNExecutor_SetInput() 530 531``` 532OH_NN_ReturnCode OH_NNExecutor_SetInput(OH_NNExecutor *executor,uint32_t inputIndex,const OH_NN_Tensor *tensor,const void *dataBuffer,size_t length) 533``` 534 535**描述** 536 537设置模型单个输入的数据。 538 539该接口将dataBuffer中,长度为length个字节的数据,拷贝到底层硬件的共享内存。 540 541inputIndex指定设置的输入,tensor用于设置输入张量的形状、数据类型、量化参数等信息。 542 543由于Neural Network Runtime支持动态输入形状的模型,在固定形状输入和动态形状输入的场景下,该接口采取不同的处理策略: 544 545- 固定形状输入的场景:tensor各属性必须和构图阶段调用[OH_NNModel_AddTensor](capi-neural-network-runtime-h.md#oh_nnmodel_addtensor)添加的张量保持一致; 546- 动态形状输入的场景:在构图阶段,由于动态输入的形状不确定,调用该接口时,要求tensor.dimensions中的每个值必须大于0,以确定执行计算阶段输入的形状。设置形状时,只允许调整数值为-1的维度。<br>假设在构图阶段,输入A的维度为[-1, 224, 224, 3],调用该接口时,只能调整第一个维度的尺寸,如:[3, 224, 224, 3]。调整其他维度将返回[OH_NN_INVALID_PARAMETER](capi-neural-network-runtime-type-h.md#oh_nn_returncode)。 547 548**起始版本:** 9 549 550**废弃版本:** 11 551 552**替代接口:** [OH_NNExecutor_RunSync](capi-neural-network-core-h.md#oh_nnexecutor_runsync) 553 554 555**参数:** 556 557| 参数项 | 描述 | 558| -- | -- | 559| [OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) *executor | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的指针。 | 560| uint32_t inputIndex | 输入的索引值,与调用[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)时输入数据的顺序一致。<br>假设调用[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)时,inputIndices为{1, 5, 9},则在设置输入的阶段,三个输入的索引值分别为{0, 1, 2}。 | 561| [const OH_NN_Tensor](capi-neuralnetworkruntime-oh-nn-tensor.md) *tensor | 设置输入数据对应的张量。 | 562| const void *dataBuffer | 指向输入数据的指针。 | 563| size_t length | 数据内存的字节长度。 | 564 565**返回:** 566 567| 类型 | 说明 | 568| -- | -- | 569| [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)。 | 570 571### OH_NNExecutor_SetOutput() 572 573``` 574OH_NN_ReturnCode OH_NNExecutor_SetOutput(OH_NNExecutor *executor,uint32_t outputIndex,void *dataBuffer,size_t length) 575``` 576 577**描述** 578 579设置模型单个输出的内存。 580 581该接口将dataBuffer指向的内存与outputIndex指定的输出绑定,内存的长度由length指定。 582 583调用[OH_NNExecutor_Run](capi-neural-network-runtime-h.md#oh_nnexecutor_run)完成单次模型推理后,Neural Network Runtime将比对dataBuffer指向的内存与输出数据的长度,根据不同情况,返回不同结果: 584 585- 如果内存大小大于或等于数据长度:则推理后的结果将拷贝至内存,并返回[OH_NN_SUCCESS](capi-neural-network-runtime-type-h.md#oh_nn_returncode),可以通过访问dataBuffer读取推理结果。 586- 如果内存大小小于数据长度:则[OH_NNExecutor_Run](capi-neural-network-runtime-h.md#oh_nnexecutor_run)将返回[OH_NN_INVALID_PARAMETER](capi-neural-network-runtime-type-h.md#oh_nn_returncode),并输出日志告知内存太小的信息。 587 588**起始版本:** 9 589 590**废弃版本:** 11 591 592**替代接口:** [OH_NNExecutor_RunSync](capi-neural-network-core-h.md#oh_nnexecutor_runsync) 593 594 595**参数:** 596 597| 参数项 | 描述 | 598| -- | -- | 599| [OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) *executor | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的指针。 | 600| uint32_t outputIndex | 输出的索引值,与调用[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)时输出数据的顺序一致。<br>假设调用[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)时,outputIndices为{4, 6, 8},则在设置输出内存时,三个输出的索引值分别为{0, 1, 2}。 | 601| void *dataBuffer | 指向输出数据的指针。 | 602| size_t length | 数据内存的字节长度。 | 603 604**返回:** 605 606| 类型 | 说明 | 607| -- | -- | 608| [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)。 | 609 610### OH_NNExecutor_Run() 611 612``` 613OH_NN_ReturnCode OH_NNExecutor_Run(OH_NNExecutor *executor) 614``` 615 616**描述** 617 618执行推理。 在执行器关联的硬件上,执行模型的端到端推理计算。 619 620**起始版本:** 9 621 622**废弃版本:** 11 623 624**替代接口:** [OH_NNExecutor_RunSync](capi-neural-network-core-h.md#oh_nnexecutor_runsync) 625 626**参数:** 627 628| 参数项 | 描述 | 629| -- | -- | 630| [OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) *executor | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的指针。 | 631 632**返回:** 633 634| 类型 | 说明 | 635| -- | -- | 636| [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)。 | 637 638### OH_NNExecutor_AllocateInputMemory() 639 640``` 641OH_NN_Memory *OH_NNExecutor_AllocateInputMemory(OH_NNExecutor *executor, uint32_t inputIndex, size_t length) 642``` 643 644**描述** 645 646在硬件上为单个输入申请共享内存。 647 648Neural Network Runtime 提供主动申请硬件共享内存的方法。 649 650通过指定执行器和输入索引值,该接口在单个输入关联的硬件上,申请大小为length的共享内存,通过[OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md)实例返回。 651 652**起始版本:** 9 653 654**废弃版本:** 11 655 656**替代接口:** [OH_NNTensor_CreateWithSize](capi-neural-network-core-h.md#oh_nntensor_createwithsize) 657 658 659**参数:** 660 661| 参数项 | 描述 | 662| -- | -- | 663| [OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) *executor | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的指针。 | 664| uint32_t inputIndex | 输入的索引值,与调用[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)时输入数据的顺序一致。<br>假设调用[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)时,inputIndices为{1, 5, 9},则在申请输入内存时,三个输入的索引值分别为{0, 1, 2}。 | 665| size_t length | 申请的内存字节。 | 666 667**返回:** 668 669| 类型 | 说明 | 670| -- | -- | 671| [OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md) * | 指向[OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md)实例的指针,如果创建失败就返回NULL。 | 672 673### OH_NNExecutor_AllocateOutputMemory() 674 675``` 676OH_NN_Memory *OH_NNExecutor_AllocateOutputMemory(OH_NNExecutor *executor, uint32_t outputIndex, size_t length) 677``` 678 679**描述** 680 681在硬件上为单个输出申请共享内存。 682 683Neural Network Runtime 提供主动申请硬件共享内存的方法。 684 685通过指定执行器和输出索引值,该接口在单个输出关联的硬件上,申请大小为length的共享内存,通过[OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md)实例返回。 686 687**起始版本:** 9 688 689**废弃版本:** 11 690 691**替代接口:** [OH_NNTensor_CreateWithSize](capi-neural-network-core-h.md#oh_nntensor_createwithsize) 692 693 694**参数:** 695 696| 参数项 | 描述 | 697| -- | -- | 698| [OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) *executor | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的指针。 | 699| uint32_t outputIndex | 输出的索引值,与调用[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)时输出数据的顺序一致。<br>假设调用[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)时,outputIndices为{4, 6, 8},则在申请输出内存时,三个输出的索引值分别为{0, 1, 2}。 | 700| size_t length | 申请的内存字节。 | 701 702**返回:** 703 704| 类型 | 说明 | 705| -- | -- | 706| [OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md) * | 指向[OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md)实例的指针,如果创建失败就返回NULL。 | 707 708### OH_NNExecutor_DestroyInputMemory() 709 710``` 711void OH_NNExecutor_DestroyInputMemory(OH_NNExecutor *executor, uint32_t inputIndex, OH_NN_Memory **memory) 712``` 713 714**描述** 715 716释放[OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md)实例指向的输入内存。 717 718调用[OH_NNExecutor_AllocateInputMemory](capi-neural-network-runtime-h.md#oh_nnexecutor_allocateinputmemory)创建的内存实例,需要主动调用该接口进行释放,否则将造成内存泄漏。 719 720inputIndex和memory的对应关系需要和创建内存实例时保持一致。 如果memory或*memory为空指针,该接口仅打印警告日志,不执行释放操作。 721 722**起始版本:** 9 723 724**废弃版本:** 11 725 726**替代接口:** [OH_NNTensor_Destroy](capi-neural-network-core-h.md#oh_nntensor_destroy) 727 728 729**参数:** 730 731| 参数项 | 描述 | 732| -- | -- | 733| [OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) *executor | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的指针。 | 734| uint32_t inputIndex | 输入的索引值,与调用[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)时输入数据的顺序一致。<br>假设调用[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)时,inputIndices为{1, 5, 9},则在释放输入内存时,三个输入的索引值分别为{0, 1, 2}。 | 735| [OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md) **memory | 指向[OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md)实例的二级指针。共享内存释放后,该接口将*memory主动设置为空指针。 | 736 737### OH_NNExecutor_DestroyOutputMemory() 738 739``` 740void OH_NNExecutor_DestroyOutputMemory(OH_NNExecutor *executor, uint32_t outputIndex, OH_NN_Memory **memory) 741``` 742 743**描述** 744 745释放[OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md)实例指向的输出内存。 746 747调用[OH_NNExecutor_AllocateOutputMemory](capi-neural-network-runtime-h.md#oh_nnexecutor_allocateoutputmemory)创建的内存实例,需要主动调用该接口进行释放,否则将造成内存泄漏。 748 749outputIndex和memory的对应关系需要和创建内存实例时保持一致。 750 751如果memory或*memory为空指针,该接口仅打印警告日志,不执行释放操作。 752 753**起始版本:** 9 754 755**废弃版本:** 11 756 757**替代接口:** [OH_NNTensor_Destroy](capi-neural-network-core-h.md#oh_nntensor_destroy) 758 759 760**参数:** 761 762| 参数项 | 描述 | 763| -- | -- | 764| [OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) *executor | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的指针。 | 765| uint32_t outputIndex | 输出的索引值,与调用[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)时输出数据的顺序一致。<br>假设调用[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)时,outputIndices为{4, 6, 8},则在释放输出内存时,三个输出的索引值分别为{0, 1, 2}。 | 766| [OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md) **memory | 指向[OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md)实例的二级指针。共享内存释放后,该接口将*memory主动设置为空指针。 | 767 768### OH_NNExecutor_SetInputWithMemory() 769 770``` 771OH_NN_ReturnCode OH_NNExecutor_SetInputWithMemory(OH_NNExecutor *executor,uint32_t inputIndex,const OH_NN_Tensor *tensor,const OH_NN_Memory *memory) 772``` 773 774**描述** 775 776将[OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md)实例指向的硬件共享内存,并指定为单个输入使用的内存。 777 778在需要自行管理内存的场景下,该接口将执行输入和[OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md)内存实例绑定。 779 780执行计算时,底层硬件从内存实例指向的共享内存中读取输入数据。 781 782通过该接口,可以实现设置输入、执行计算、读取输出的并发执行,提升数据流的推理效率。 783 784**起始版本:** 9 785 786**废弃版本:** 11 787 788**替代接口:** [OH_NNExecutor_RunSync](capi-neural-network-core-h.md#oh_nnexecutor_runsync) 789 790 791**参数:** 792 793| 参数项 | 描述 | 794| -- | -- | 795| [OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) *executor | 指向[OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md)实例的指针。 | 796| uint32_t inputIndex | 输入的索引值,与调用[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)时输入数据的顺序一致。<br>假设调用[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)时,inputIndices为{1, 5, 9},则在指定输入的共享内存时,三个输入的索引值分别为{0, 1, 2}。 | 797| [const OH_NN_Tensor](capi-neuralnetworkruntime-oh-nn-tensor.md) *tensor | 指向[OH_NN_Tensor](capi-neuralnetworkruntime-oh-nn-tensor.md)的指针,设置单个输入所对应的张量。 | 798| [const OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md) *memory | 指向[OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md)的指针。 | 799 800**返回:** 801 802| 类型 | 说明 | 803| -- | -- | 804| [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)。 | 805 806### OH_NNExecutor_SetOutputWithMemory() 807 808``` 809OH_NN_ReturnCode OH_NNExecutor_SetOutputWithMemory(OH_NNExecutor *executor,uint32_t outputIndex,const OH_NN_Memory *memory) 810``` 811 812**描述** 813 814将[OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md)实例指向的硬件共享内存,并指定为单个输出使用的内存。 815 816在需要自行管理内存的场景下,该接口将执行输出和[OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md)内存实例绑定。 817 818执行计算时,底层硬件将计算结果直接写入内存实例指向的共享内存。 819 820通过该接口,可以实现设置输入、执行计算、读取输出的并发执行,提升数据流的推理效率。 821 822**起始版本:** 9 823 824**废弃版本:** 11 825 826**替代接口:** [OH_NNExecutor_RunSync](capi-neural-network-core-h.md#oh_nnexecutor_runsync) 827 828 829**参数:** 830 831| 参数项 | 描述 | 832| -- | -- | 833| [OH_NNExecutor](capi-neuralnetworkruntime-oh-nnexecutor.md) *executor | 执行器。 | 834| uint32_t outputIndex | 输出的索引值,与调用[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)时输出数据的顺序一致。<br>假设调用[OH_NNModel_SpecifyInputsAndOutputs](capi-neural-network-runtime-h.md#oh_nnmodel_specifyinputsandoutputs)时,outputIndices为{4, 6, 8},则在指定输出的共享内存时,三个输出的索引值分别为{0, 1, 2}。 | 835| [const OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md) *memory | 指向[OH_NN_Memory](capi-neuralnetworkruntime-oh-nn-memory.md)的指针。 | 836 837**返回:** 838 839| 类型 | 说明 | 840| -- | -- | 841| [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)。 | 842 843 844