1e41f4b71Sopenharmony_ci# Connecting NNRt to an AI Inference Framework 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## When to Use 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciAs a bridge between the AI inference engine and acceleration chip, Neural Network Runtime (NNRt) provides simplified native APIs for the AI inference engine to perform end-to-end inference through the acceleration chip. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciThis topic uses the `Add` single-operator model shown in Figure 1 as an example to describe the NNRt development process. The `Add` operator involves two inputs, one parameter, and one output. Wherein, the `activation` parameter is used to specify the type of the activation function in the `Add` operator. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci**Figure 1** Add single-operator model<br> 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci## Preparing the Environment 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci### Environment Requirements 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ciThe environment requirements for NNRt are as follows: 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci- Development environment: Ubuntu 18.04 or later. 19e41f4b71Sopenharmony_ci- Access device: a standard device whose built-in hardware accelerator driver has been connected to NNRt. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ciNNRt is opened to external systems through native APIs. Therefore, you need to use the native development suite to build NNRt applications. You can download the ohos-sdk package of the corresponding version from the daily build in the OpenHarmony community and then decompress the package to obtain the native development suite of the corresponding platform. Take Linux as an example. The package of the native development suite is named `native-linux-{version number}.zip`. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci### Environment Setup 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci1. Start the Ubuntu server. 26e41f4b71Sopenharmony_ci2. Copy the downloaded package of the Native development suite to the root directory of the current user. 27e41f4b71Sopenharmony_ci3. Decompress the package of the native development suite. 28e41f4b71Sopenharmony_ci ```shell 29e41f4b71Sopenharmony_ci unzip native-linux-{version number}.zip 30e41f4b71Sopenharmony_ci ``` 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci The directory structure after decompression is as follows. The content in the directory may vary depending on the version. Use the native APIs of the latest version. 33e41f4b71Sopenharmony_ci ```text 34e41f4b71Sopenharmony_ci native/ 35e41f4b71Sopenharmony_ci ├── build // Cross-compilation toolchain 36e41f4b71Sopenharmony_ci ├── build-tools // Compilation and build tools 37e41f4b71Sopenharmony_ci ├── docs 38e41f4b71Sopenharmony_ci ├── llvm 39e41f4b71Sopenharmony_ci ├── nativeapi_syscap_config.json 40e41f4b71Sopenharmony_ci ├── ndk_system_capability.json 41e41f4b71Sopenharmony_ci ├── NOTICE.txt 42e41f4b71Sopenharmony_ci ├── oh-uni-package.json 43e41f4b71Sopenharmony_ci └── sysroot // Native API header files and libraries 44e41f4b71Sopenharmony_ci ``` 45e41f4b71Sopenharmony_ci## Available APIs 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ciThis section describes the common APIs used in the NNRt development process. 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci### Structs 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci| Name| Description| 52e41f4b71Sopenharmony_ci| --------- | ---- | 53e41f4b71Sopenharmony_ci| typedef struct OH_NNModel OH_NNModel | Model handle of NNRt. It is used to construct a model.| 54e41f4b71Sopenharmony_ci| typedef struct OH_NNCompilation OH_NNCompilation | Compiler handle of NNRt. It is used to compile an AI model.| 55e41f4b71Sopenharmony_ci| typedef struct OH_NNExecutor OH_NNExecutor | Executor handle of NNRt. It is used to perform inference computing on a specified device.| 56e41f4b71Sopenharmony_ci| typedef struct NN_QuantParam NN_QuantParam | Quantization parameter handle, which is used to specify the quantization parameter of the tensor during model construction.| 57e41f4b71Sopenharmony_ci| typedef struct NN_TensorDesc NN_TensorDesc | Tensor description handle, which is used to describe tensor attributes, such as the data format, data type, and shape.| 58e41f4b71Sopenharmony_ci| typedef struct NN_Tensor NN_Tensor | Tensor handle, which is used to set the inference input and output tensors of the executor.| 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci### Model Construction APIs 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci| Name| Description| 63e41f4b71Sopenharmony_ci| ------- | --- | 64e41f4b71Sopenharmony_ci| OH_NNModel_Construct() | Creates a model instance of the OH_NNModel type.| 65e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNModel_AddTensorToModel(OH_NNModel *model, const NN_TensorDesc *tensorDesc) | Adds a tensor to a model instance.| 66e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNModel_SetTensorData(OH_NNModel *model, uint32_t index, const void *dataBuffer, size_t length) | Sets the tensor value.| 67e41f4b71Sopenharmony_ci| 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) | Adds an operator to a model instance.| 68e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNModel_SpecifyInputsAndOutputs(OH_NNModel *model, const OH_NN_UInt32Array *inputIndices, const OH_NN_UInt32Array *outputIndices) | Sets an index value for the input and output tensors of a model.| 69e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNModel_Finish(OH_NNModel *model) | Completes model composition.| 70e41f4b71Sopenharmony_ci| void OH_NNModel_Destroy(OH_NNModel **model) | Destroys a model instance.| 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci### Model Compilation APIs 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci| Name| Description| 76e41f4b71Sopenharmony_ci| ------- | --- | 77e41f4b71Sopenharmony_ci| OH_NNCompilation *OH_NNCompilation_Construct(const OH_NNModel *model) | Creates an **OH_NNCompilation** instance based on the specified model instance.| 78e41f4b71Sopenharmony_ci| OH_NNCompilation *OH_NNCompilation_ConstructWithOfflineModelFile(const char *modelPath) | Creates an **OH_NNCompilation** instance based on the specified offline model file path.| 79e41f4b71Sopenharmony_ci| OH_NNCompilation *OH_NNCompilation_ConstructWithOfflineModelBuffer(const void *modelBuffer, size_t modelSize) | Creates an **OH_NNCompilation** instance based on the specified offline model buffer.| 80e41f4b71Sopenharmony_ci| OH_NNCompilation *OH_NNCompilation_ConstructForCache() | Creates an empty model building instance for later recovery from the model cache.| 81e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNCompilation_ExportCacheToBuffer(OH_NNCompilation *compilation, const void *buffer, size_t length, size_t *modelSize) | Writes the model cache to the specified buffer.| 82e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNCompilation_ImportCacheFromBuffer(OH_NNCompilation *compilation, const void *buffer, size_t modelSize) | Reads the model cache from the specified buffer.| 83e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNCompilation_AddExtensionConfig(OH_NNCompilation *compilation, const char *configName, const void *configValue, const size_t configValueSize) | Adds extended configurations for custom device attributes. For details about the extended attribute names and values, see the documentation that comes with the device.| 84e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNCompilation_SetDevice(OH_NNCompilation *compilation, size_t deviceID) | Sets the Device for model building and computing, which can be obtained through the device management APIs.| 85e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNCompilation_SetCache(OH_NNCompilation *compilation, const char *cachePath, uint32_t version) | Sets the cache directory and version for model building.| 86e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNCompilation_SetPerformanceMode(OH_NNCompilation *compilation, OH_NN_PerformanceMode performanceMode) | Sets the performance mode for model computing.| 87e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNCompilation_SetPriority(OH_NNCompilation *compilation, OH_NN_Priority priority) | Sets the priority for model computing.| 88e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNCompilation_EnableFloat16(OH_NNCompilation *compilation, bool enableFloat16) | Enables float16 for computing.| 89e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNCompilation_Build(OH_NNCompilation *compilation) | Performs model building.| 90e41f4b71Sopenharmony_ci| void OH_NNCompilation_Destroy(OH_NNCompilation **compilation) | Destroys a model building instance.| 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci### Tensor Description APIs 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci| Name| Description| 95e41f4b71Sopenharmony_ci| ------- | --- | 96e41f4b71Sopenharmony_ci| NN_TensorDesc *OH_NNTensorDesc_Create() | Creates an **NN_TensorDesc** instance for creating an **NN_Tensor** instance at a later time.| 97e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNTensorDesc_SetName(NN_TensorDesc *tensorDesc, const char *name) | Sets the name of the **NN_TensorDesc** instance.| 98e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNTensorDesc_GetName(const NN_TensorDesc *tensorDesc, const char **name) | Obtains the name of the **NN_TensorDesc** instance.| 99e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNTensorDesc_SetDataType(NN_TensorDesc *tensorDesc, OH_NN_DataType dataType) | Sets the data type of the **NN_TensorDesc** instance.| 100e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNTensorDesc_GetDataType(const NN_TensorDesc *tensorDesc, OH_NN_DataType *dataType) | Obtains the data type of the **NN_TensorDesc** instance.| 101e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNTensorDesc_SetShape(NN_TensorDesc *tensorDesc, const int32_t *shape, size_t shapeLength) | Sets the shape of the **NN_TensorDesc** instance.| 102e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNTensorDesc_GetShape(const NN_TensorDesc *tensorDesc, int32_t **shape, size_t *shapeLength) | Obtains the shape of the **NN_TensorDesc** instance.| 103e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNTensorDesc_SetFormat(NN_TensorDesc *tensorDesc, OH_NN_Format format) | Sets the data format of the **NN_TensorDesc** instance.| 104e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNTensorDesc_GetFormat(const NN_TensorDesc *tensorDesc, OH_NN_Format *format) | Obtains the data format of the **NN_TensorDesc** instance.| 105e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNTensorDesc_GetElementCount(const NN_TensorDesc *tensorDesc, size_t *elementCount) | Obtains the number of elements in the **NN_TensorDesc** instance.| 106e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNTensorDesc_GetByteSize(const NN_TensorDesc *tensorDesc, size_t *byteSize) | Obtains the number of bytes occupied by the tensor data obtained through calculation based on the shape and data type of an **NN_TensorDesc** instance.| 107e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNTensorDesc_Destroy(NN_TensorDesc **tensorDesc) | Destroys an **NN_TensorDesc** instance.| 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci### Tensor APIs 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci| Name| Description| 112e41f4b71Sopenharmony_ci| ------- | --- | 113e41f4b71Sopenharmony_ci| NN_Tensor* OH_NNTensor_Create(size_t deviceID, NN_TensorDesc *tensorDesc) | Creates an **NN_Tensor** instance based on the specified tensor description. This API will request for device shared memory.| 114e41f4b71Sopenharmony_ci| NN_Tensor* OH_NNTensor_CreateWithSize(size_t deviceID, NN_TensorDesc *tensorDesc, size_t size) | Creates an **NN_Tensor** instance based on the specified memory size and tensor description. This API will request for device shared memory.| 115e41f4b71Sopenharmony_ci| NN_Tensor* OH_NNTensor_CreateWithFd(size_t deviceID, NN_TensorDesc *tensorDesc, int fd, size_t size, size_t offset) | Creates an **NN_Tensor** instance based on the specified file descriptor of the shared memory and tensor description. This way, the device shared memory of other tensors can be reused.| 116e41f4b71Sopenharmony_ci| NN_TensorDesc* OH_NNTensor_GetTensorDesc(const NN_Tensor *tensor) | Obtains the pointer to the **NN_TensorDesc** instance in a tensor to read tensor attributes, such as the data type and shape.| 117e41f4b71Sopenharmony_ci| void* OH_NNTensor_GetDataBuffer(const NN_Tensor *tensor) | Obtains the memory address of tensor data to read or write tensor data.| 118e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNTensor_GetFd(const NN_Tensor *tensor, int *fd) | Obtains the file descriptor of the shared memory where the tensor data is located. A file descriptor corresponds to a device shared memory block.| 119e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNTensor_GetSize(const NN_Tensor *tensor, size_t *size) | Obtains the size of the shared memory where tensor data is located.| 120e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNTensor_GetOffset(const NN_Tensor *tensor, size_t *offset) | Obtains the offset of the tensor data in the shared memory. The available size of the tensor data is the size of the shared memory minus the offset.| 121e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNTensor_Destroy(NN_Tensor **tensor) | Destroys an **NN_Tensor** instance.| 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci### Inference APIs 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci| Name| Description| 126e41f4b71Sopenharmony_ci| ------- | --- | 127e41f4b71Sopenharmony_ci| OH_NNExecutor *OH_NNExecutor_Construct(OH_NNCompilation *compilation) | Creates an **OH_NNExecutor** instance.| 128e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNExecutor_GetOutputShape(OH_NNExecutor *executor, uint32_t outputIndex, int32_t **shape, uint32_t *shapeLength) | Obtains the dimension information about the output tensor. This API is applicable only if the output tensor has a dynamic shape.| 129e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNExecutor_GetInputCount(const OH_NNExecutor *executor, size_t *inputCount) | Obtains the number of input tensors.| 130e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNExecutor_GetOutputCount(const OH_NNExecutor *executor, size_t *outputCount) | Obtains the number of output tensors.| 131e41f4b71Sopenharmony_ci| NN_TensorDesc* OH_NNExecutor_CreateInputTensorDesc(const OH_NNExecutor *executor, size_t index) | Creates an **NN_TensorDesc** instance for an input tensor based on the specified index value. This instance will be used to read tensor attributes or create **NN_Tensor** instances.| 132e41f4b71Sopenharmony_ci| NN_TensorDesc* OH_NNExecutor_CreateOutputTensorDesc(const OH_NNExecutor *executor, size_t index) | Creates an **NN_TensorDesc** instance for an output tensor based on the specified index value. This instance will be used to read tensor attributes or create **NN_Tensor** instances.| 133e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNExecutor_GetInputDimRange(const OH_NNExecutor *executor, size_t index, size_t **minInputDims, size_t **maxInputDims, size_t *shapeLength) |Obtains the dimension range of all input tensors. If the input tensor has a dynamic shape, the dimension range supported by the tensor may vary according to device. | 134e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNExecutor_SetOnRunDone(OH_NNExecutor *executor, NN_OnRunDone onRunDone) | Sets the callback function invoked when the asynchronous inference ends. For the definition of the callback function, see the *API Reference*.| 135e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNExecutor_SetOnServiceDied(OH_NNExecutor *executor, NN_OnServiceDied onServiceDied) | Sets the callback function invoked when the device driver service terminates unexpectedly during asynchronous inference. For the definition of the callback function, see the *API Reference*.| 136e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNExecutor_RunSync(OH_NNExecutor *executor, NN_Tensor *inputTensor[], size_t inputCount, NN_Tensor *outputTensor[], size_t outputCount) | Performs synchronous inference.| 137e41f4b71Sopenharmony_ci| 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) | Performs asynchronous inference.| 138e41f4b71Sopenharmony_ci| void OH_NNExecutor_Destroy(OH_NNExecutor **executor) | Destroys an **OH_NNExecutor** instance.| 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci### Device Management APIs 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci| Name| Description| 143e41f4b71Sopenharmony_ci| ------- | --- | 144e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNDevice_GetAllDevicesID(const size_t **allDevicesID, uint32_t *deviceCount) | Obtains the ID of the device connected to NNRt.| 145e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNDevice_GetName(size_t deviceID, const char **name) | Obtains the name of the specified device.| 146e41f4b71Sopenharmony_ci| OH_NN_ReturnCode OH_NNDevice_GetType(size_t deviceID, OH_NN_DeviceType *deviceType) | Obtains the type of the specified device.| 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci## How to Develop 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ciThe development process of NNRt consists of three phases: model construction, model compilation, and inference execution. The following uses the `Add` single-operator model as an example to describe how to call NNRt APIs during application development. 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci1. Create an application sample file. 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci Create the source file of the NNRt application sample. Run the following commands in the project directory to create the `nnrt_example/` directory and create the `nnrt_example.cpp` source file in the directory: 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci ```shell 158e41f4b71Sopenharmony_ci mkdir ~/nnrt_example && cd ~/nnrt_example 159e41f4b71Sopenharmony_ci touch nnrt_example.cpp 160e41f4b71Sopenharmony_ci ``` 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci2. Import the NNRt module. 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci Add the following code at the beginning of the `nnrt_example.cpp` file to import NNRt: 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci ```cpp 167e41f4b71Sopenharmony_ci #include <iostream> 168e41f4b71Sopenharmony_ci #include <cstdarg> 169e41f4b71Sopenharmony_ci #include "neural_network_runtime/neural_network_runtime.h" 170e41f4b71Sopenharmony_ci ``` 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci3. Defines auxiliary functions, such as log printing, input data setting, and data printing. 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci ```cpp 175e41f4b71Sopenharmony_ci // Macro for checking the return value 176e41f4b71Sopenharmony_ci #define CHECKNEQ(realRet, expectRet, retValue, ...) \ 177e41f4b71Sopenharmony_ci do { \ 178e41f4b71Sopenharmony_ci if ((realRet) != (expectRet)) { \ 179e41f4b71Sopenharmony_ci printf(__VA_ARGS__); \ 180e41f4b71Sopenharmony_ci return (retValue); \ 181e41f4b71Sopenharmony_ci } \ 182e41f4b71Sopenharmony_ci } while (0) 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci #define CHECKEQ(realRet, expectRet, retValue, ...) \ 185e41f4b71Sopenharmony_ci do { \ 186e41f4b71Sopenharmony_ci if ((realRet) == (expectRet)) { \ 187e41f4b71Sopenharmony_ci printf(__VA_ARGS__); \ 188e41f4b71Sopenharmony_ci return (retValue); \ 189e41f4b71Sopenharmony_ci } \ 190e41f4b71Sopenharmony_ci } while (0) 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci // Set the input data for inference. 193e41f4b71Sopenharmony_ci OH_NN_ReturnCode SetInputData(NN_Tensor* inputTensor[], size_t inputSize) 194e41f4b71Sopenharmony_ci { 195e41f4b71Sopenharmony_ci OH_NN_DataType dataType(OH_NN_FLOAT32); 196e41f4b71Sopenharmony_ci OH_NN_ReturnCode ret{OH_NN_FAILED}; 197e41f4b71Sopenharmony_ci size_t elementCount = 0; 198e41f4b71Sopenharmony_ci for (size_t i = 0; i < inputSize; ++i) { 199e41f4b71Sopenharmony_ci // Obtain the data memory of the tensor. 200e41f4b71Sopenharmony_ci auto data = OH_NNTensor_GetDataBuffer(inputTensor[i]); 201e41f4b71Sopenharmony_ci CHECKEQ(data, nullptr, OH_NN_FAILED, "Failed to get data buffer."); 202e41f4b71Sopenharmony_ci // Obtain the tensor description. 203e41f4b71Sopenharmony_ci auto desc = OH_NNTensor_GetTensorDesc(inputTensor[i]); 204e41f4b71Sopenharmony_ci CHECKEQ(desc, nullptr, OH_NN_FAILED, "Failed to get desc."); 205e41f4b71Sopenharmony_ci // Obtain the data type of the tensor. 206e41f4b71Sopenharmony_ci ret = OH_NNTensorDesc_GetDataType(desc, &dataType); 207e41f4b71Sopenharmony_ci CHECKNEQ(ret, OH_NN_SUCCESS, OH_NN_FAILED, "Failed to get data type."); 208e41f4b71Sopenharmony_ci // Obtain the number of elements in the tensor. 209e41f4b71Sopenharmony_ci ret = OH_NNTensorDesc_GetElementCount(desc, &elementCount); 210e41f4b71Sopenharmony_ci CHECKNEQ(ret, OH_NN_SUCCESS, OH_NN_FAILED, "Failed to get element count."); 211e41f4b71Sopenharmony_ci switch(dataType) { 212e41f4b71Sopenharmony_ci case OH_NN_FLOAT32: { 213e41f4b71Sopenharmony_ci float* floatValue = reinterpret_cast<float*>(data); 214e41f4b71Sopenharmony_ci for (size_t j = 0; j < elementCount; ++j) { 215e41f4b71Sopenharmony_ci floatValue[j] = static_cast<float>(j); 216e41f4b71Sopenharmony_ci } 217e41f4b71Sopenharmony_ci break; 218e41f4b71Sopenharmony_ci } 219e41f4b71Sopenharmony_ci case OH_NN_INT32: { 220e41f4b71Sopenharmony_ci int* intValue = reinterpret_cast<int*>(data); 221e41f4b71Sopenharmony_ci for (size_t j = 0; j < elementCount; ++j) { 222e41f4b71Sopenharmony_ci intValue[j] = static_cast<int>(j); 223e41f4b71Sopenharmony_ci } 224e41f4b71Sopenharmony_ci break; 225e41f4b71Sopenharmony_ci } 226e41f4b71Sopenharmony_ci default: 227e41f4b71Sopenharmony_ci return OH_NN_FAILED; 228e41f4b71Sopenharmony_ci } 229e41f4b71Sopenharmony_ci } 230e41f4b71Sopenharmony_ci return OH_NN_SUCCESS; 231e41f4b71Sopenharmony_ci } 232e41f4b71Sopenharmony_ci 233e41f4b71Sopenharmony_ci OH_NN_ReturnCode Print(NN_Tensor* outputTensor[], size_t outputSize) 234e41f4b71Sopenharmony_ci { 235e41f4b71Sopenharmony_ci OH_NN_DataType dataType(OH_NN_FLOAT32); 236e41f4b71Sopenharmony_ci OH_NN_ReturnCode ret{OH_NN_FAILED}; 237e41f4b71Sopenharmony_ci size_t elementCount = 0; 238e41f4b71Sopenharmony_ci for (size_t i = 0; i < outputSize; ++i) { 239e41f4b71Sopenharmony_ci auto data = OH_NNTensor_GetDataBuffer(outputTensor[i]); 240e41f4b71Sopenharmony_ci CHECKEQ(data, nullptr, OH_NN_FAILED, "Failed to get data buffer."); 241e41f4b71Sopenharmony_ci auto desc = OH_NNTensor_GetTensorDesc(outputTensor[i]); 242e41f4b71Sopenharmony_ci CHECKEQ(desc, nullptr, OH_NN_FAILED, "Failed to get desc."); 243e41f4b71Sopenharmony_ci ret = OH_NNTensorDesc_GetDataType(desc, &dataType); 244e41f4b71Sopenharmony_ci CHECKNEQ(ret, OH_NN_SUCCESS, OH_NN_FAILED, "Failed to get data type."); 245e41f4b71Sopenharmony_ci ret = OH_NNTensorDesc_GetElementCount(desc, &elementCount); 246e41f4b71Sopenharmony_ci CHECKNEQ(ret, OH_NN_SUCCESS, OH_NN_FAILED, "Failed to get element count."); 247e41f4b71Sopenharmony_ci switch(dataType) { 248e41f4b71Sopenharmony_ci case OH_NN_FLOAT32: { 249e41f4b71Sopenharmony_ci float* floatValue = reinterpret_cast<float*>(data); 250e41f4b71Sopenharmony_ci for (size_t j = 0; j < elementCount; ++j) { 251e41f4b71Sopenharmony_ci std::cout << "Output index: " << j << ", value is: " << floatValue[j] << "." << std::endl; 252e41f4b71Sopenharmony_ci } 253e41f4b71Sopenharmony_ci break; 254e41f4b71Sopenharmony_ci } 255e41f4b71Sopenharmony_ci case OH_NN_INT32: { 256e41f4b71Sopenharmony_ci int* intValue = reinterpret_cast<int*>(data); 257e41f4b71Sopenharmony_ci for (size_t j = 0; j < elementCount; ++j) { 258e41f4b71Sopenharmony_ci std::cout << "Output index: " << j << ", value is: " << intValue[j] << "." << std::endl; 259e41f4b71Sopenharmony_ci } 260e41f4b71Sopenharmony_ci break; 261e41f4b71Sopenharmony_ci } 262e41f4b71Sopenharmony_ci default: 263e41f4b71Sopenharmony_ci return OH_NN_FAILED; 264e41f4b71Sopenharmony_ci } 265e41f4b71Sopenharmony_ci } 266e41f4b71Sopenharmony_ci 267e41f4b71Sopenharmony_ci return OH_NN_SUCCESS; 268e41f4b71Sopenharmony_ci } 269e41f4b71Sopenharmony_ci ``` 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ci4. Construct a model. 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ci Use the model construction APIs to construct a single `Add` operator model. 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ci ```cpp 276e41f4b71Sopenharmony_ci OH_NN_ReturnCode BuildModel(OH_NNModel** pmodel) 277e41f4b71Sopenharmony_ci { 278e41f4b71Sopenharmony_ci // Create a model instance and construct a model. 279e41f4b71Sopenharmony_ci OH_NNModel* model = OH_NNModel_Construct(); 280e41f4b71Sopenharmony_ci CHECKEQ(model, nullptr, OH_NN_FAILED, "Create model failed."); 281e41f4b71Sopenharmony_ci 282e41f4b71Sopenharmony_ci // Add the first input tensor of the float32 type for the Add operator. The tensor shape is [1, 2, 2, 3]. 283e41f4b71Sopenharmony_ci NN_TensorDesc* tensorDesc = OH_NNTensorDesc_Create(); 284e41f4b71Sopenharmony_ci CHECKEQ(tensorDesc, nullptr, OH_NN_FAILED, "Create TensorDesc failed."); 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ci int32_t inputDims[4] = {1, 2, 2, 3}; 287e41f4b71Sopenharmony_ci auto returnCode = OH_NNTensorDesc_SetShape(tensorDesc, inputDims, 4); 288e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Set TensorDesc shape failed."); 289e41f4b71Sopenharmony_ci 290e41f4b71Sopenharmony_ci returnCode = OH_NNTensorDesc_SetDataType(tensorDesc, OH_NN_FLOAT32); 291e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Set TensorDesc data type failed."); 292e41f4b71Sopenharmony_ci 293e41f4b71Sopenharmony_ci returnCode = OH_NNTensorDesc_SetFormat(tensorDesc, OH_NN_FORMAT_NONE); 294e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Set TensorDesc format failed."); 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_ci returnCode = OH_NNModel_AddTensorToModel(model, tensorDesc); 297e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Add first TensorDesc to model failed."); 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ci returnCode = OH_NNModel_SetTensorType(model, 0, OH_NN_TENSOR); 300e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Set model tensor type failed."); 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci // Add the second input tensor of the float32 type for the Add operator. The tensor shape is [1, 2, 2, 3]. 303e41f4b71Sopenharmony_ci tensorDesc = OH_NNTensorDesc_Create(); 304e41f4b71Sopenharmony_ci CHECKEQ(tensorDesc, nullptr, OH_NN_FAILED, "Create TensorDesc failed."); 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_ci returnCode = OH_NNTensorDesc_SetShape(tensorDesc, inputDims, 4); 307e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Set TensorDesc shape failed."); 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ci returnCode = OH_NNTensorDesc_SetDataType(tensorDesc, OH_NN_FLOAT32); 310e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Set TensorDesc data type failed."); 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ci returnCode = OH_NNTensorDesc_SetFormat(tensorDesc, OH_NN_FORMAT_NONE); 313e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Set TensorDesc format failed."); 314e41f4b71Sopenharmony_ci 315e41f4b71Sopenharmony_ci returnCode = OH_NNModel_AddTensorToModel(model, tensorDesc); 316e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Add second TensorDesc to model failed."); 317e41f4b71Sopenharmony_ci 318e41f4b71Sopenharmony_ci returnCode = OH_NNModel_SetTensorType(model, 1, OH_NN_TENSOR); 319e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Set model tensor type failed."); 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci // Add the parameter tensor of the int8 type for the Add operator. The parameter tensor is used to specify the type of the activation function. 322e41f4b71Sopenharmony_ci tensorDesc = OH_NNTensorDesc_Create(); 323e41f4b71Sopenharmony_ci CHECKEQ(tensorDesc, nullptr, OH_NN_FAILED, "Create TensorDesc failed."); 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_ci int32_t activationDims = 1; 326e41f4b71Sopenharmony_ci returnCode = OH_NNTensorDesc_SetShape(tensorDesc, &activationDims, 1); 327e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Set TensorDesc shape failed."); 328e41f4b71Sopenharmony_ci 329e41f4b71Sopenharmony_ci returnCode = OH_NNTensorDesc_SetDataType(tensorDesc, OH_NN_INT8); 330e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Set TensorDesc data type failed."); 331e41f4b71Sopenharmony_ci 332e41f4b71Sopenharmony_ci returnCode = OH_NNTensorDesc_SetFormat(tensorDesc, OH_NN_FORMAT_NONE); 333e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Set TensorDesc format failed."); 334e41f4b71Sopenharmony_ci 335e41f4b71Sopenharmony_ci returnCode = OH_NNModel_AddTensorToModel(model, tensorDesc); 336e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Add second TensorDesc to model failed."); 337e41f4b71Sopenharmony_ci 338e41f4b71Sopenharmony_ci returnCode = OH_NNModel_SetTensorType(model, 2, OH_NN_ADD_ACTIVATIONTYPE); 339e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Set model tensor type failed."); 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_ci // Set the type of the activation function to OH_NN_FUSED_NONE, indicating that no activation function is added to the operator. 342e41f4b71Sopenharmony_ci int8_t activationValue = OH_NN_FUSED_NONE; 343e41f4b71Sopenharmony_ci returnCode = OH_NNModel_SetTensorData(model, 2, &activationValue, sizeof(int8_t)); 344e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Set model tensor data failed."); 345e41f4b71Sopenharmony_ci 346e41f4b71Sopenharmony_ci // Add the output tensor of the float32 type for the Add operator. The tensor shape is [1, 2, 2, 3]. 347e41f4b71Sopenharmony_ci tensorDesc = OH_NNTensorDesc_Create(); 348e41f4b71Sopenharmony_ci CHECKEQ(tensorDesc, nullptr, OH_NN_FAILED, "Create TensorDesc failed."); 349e41f4b71Sopenharmony_ci 350e41f4b71Sopenharmony_ci returnCode = OH_NNTensorDesc_SetShape(tensorDesc, inputDims, 4); 351e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Set TensorDesc shape failed."); 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ci returnCode = OH_NNTensorDesc_SetDataType(tensorDesc, OH_NN_FLOAT32); 354e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Set TensorDesc data type failed."); 355e41f4b71Sopenharmony_ci 356e41f4b71Sopenharmony_ci returnCode = OH_NNTensorDesc_SetFormat(tensorDesc, OH_NN_FORMAT_NONE); 357e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Set TensorDesc format failed."); 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ci returnCode = OH_NNModel_AddTensorToModel(model, tensorDesc); 360e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Add forth TensorDesc to model failed."); 361e41f4b71Sopenharmony_ci 362e41f4b71Sopenharmony_ci returnCode = OH_NNModel_SetTensorType(model, 3, OH_NN_TENSOR); 363e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Set model tensor type failed."); 364e41f4b71Sopenharmony_ci 365e41f4b71Sopenharmony_ci // Specify index values of the input tensor, parameter tensor, and output tensor for the Add operator. 366e41f4b71Sopenharmony_ci uint32_t inputIndicesValues[2] = {0, 1}; 367e41f4b71Sopenharmony_ci uint32_t paramIndicesValues = 2; 368e41f4b71Sopenharmony_ci uint32_t outputIndicesValues = 3; 369e41f4b71Sopenharmony_ci OH_NN_UInt32Array paramIndices = {¶mIndicesValues, 1}; 370e41f4b71Sopenharmony_ci OH_NN_UInt32Array inputIndices = {inputIndicesValues, 2}; 371e41f4b71Sopenharmony_ci OH_NN_UInt32Array outputIndices = {&outputIndicesValues, 1}; 372e41f4b71Sopenharmony_ci 373e41f4b71Sopenharmony_ci // Add the Add operator to the model instance. 374e41f4b71Sopenharmony_ci returnCode = OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, ¶mIndices, &inputIndices, &outputIndices); 375e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Add operation to model failed."); 376e41f4b71Sopenharmony_ci 377e41f4b71Sopenharmony_ci // Set the index values of the input tensor and output tensor for the model instance. 378e41f4b71Sopenharmony_ci returnCode = OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices); 379e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Specify model inputs and outputs failed."); 380e41f4b71Sopenharmony_ci 381e41f4b71Sopenharmony_ci // Complete the model instance construction. 382e41f4b71Sopenharmony_ci returnCode = OH_NNModel_Finish(model); 383e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "Build model failed."); 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_ci // Return the model instance. 386e41f4b71Sopenharmony_ci *pmodel = model; 387e41f4b71Sopenharmony_ci return OH_NN_SUCCESS; 388e41f4b71Sopenharmony_ci } 389e41f4b71Sopenharmony_ci ``` 390e41f4b71Sopenharmony_ci 391e41f4b71Sopenharmony_ci5. Query the AI acceleration chips connected to NNRt. 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ci NNRt can connect to multiple AI acceleration chips through HDIs. Before model building, you need to query the AI acceleration chips connected to NNRt on the current device. Each AI acceleration chip has a unique ID. In the compilation phase, you need to specify the chip for model compilation based on the ID. 394e41f4b71Sopenharmony_ci ```cpp 395e41f4b71Sopenharmony_ci void GetAvailableDevices(std::vector<size_t>& availableDevice) 396e41f4b71Sopenharmony_ci { 397e41f4b71Sopenharmony_ci availableDevice.clear(); 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ci // Obtain the available hardware IDs. 400e41f4b71Sopenharmony_ci const size_t* devices = nullptr; 401e41f4b71Sopenharmony_ci uint32_t deviceCount = 0; 402e41f4b71Sopenharmony_ci OH_NN_ReturnCode ret = OH_NNDevice_GetAllDevicesID(&devices, &deviceCount); 403e41f4b71Sopenharmony_ci if (ret != OH_NN_SUCCESS) { 404e41f4b71Sopenharmony_ci std::cout << "GetAllDevicesID failed, get no available device." << std::endl; 405e41f4b71Sopenharmony_ci return; 406e41f4b71Sopenharmony_ci } 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ci for (uint32_t i = 0; i < deviceCount; i++) { 409e41f4b71Sopenharmony_ci availableDevice.emplace_back(devices[i]); 410e41f4b71Sopenharmony_ci } 411e41f4b71Sopenharmony_ci } 412e41f4b71Sopenharmony_ci ``` 413e41f4b71Sopenharmony_ci 414e41f4b71Sopenharmony_ci6. Compile a model on the specified device. 415e41f4b71Sopenharmony_ci 416e41f4b71Sopenharmony_ci NNRt uses abstract model expressions to describe the topology structure of an AI model. Before inference execution on an AI acceleration chip, the build module provided by NNRt needs to deliver the abstract model expressions to the chip driver layer and convert the abstract model expressions into a format that supports inference and computing. 417e41f4b71Sopenharmony_ci ```cpp 418e41f4b71Sopenharmony_ci OH_NN_ReturnCode CreateCompilation(OH_NNModel* model, const std::vector<size_t>& availableDevice, 419e41f4b71Sopenharmony_ci OH_NNCompilation** pCompilation) 420e41f4b71Sopenharmony_ci { 421e41f4b71Sopenharmony_ci // Create an OH_NNCompilation instance and pass the image composition model instance or the MindSpore Lite model instance to it. 422e41f4b71Sopenharmony_ci OH_NNCompilation* compilation = OH_NNCompilation_Construct(model); 423e41f4b71Sopenharmony_ci CHECKEQ(compilation, nullptr, OH_NN_FAILED, "OH_NNCore_ConstructCompilationWithNNModel failed."); 424e41f4b71Sopenharmony_ci 425e41f4b71Sopenharmony_ci // Set compilation options, such as the compilation hardware, cache path, performance mode, computing priority, and whether to enable float16 low-precision computing. 426e41f4b71Sopenharmony_ci // Choose to perform model compilation on the first device. 427e41f4b71Sopenharmony_ci auto returnCode = OH_NNCompilation_SetDevice(compilation, availableDevice[0]); 428e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "OH_NNCompilation_SetDevice failed."); 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ci // Have the model compilation result cached in the /data/local/tmp directory, with the version number set to 1. 431e41f4b71Sopenharmony_ci returnCode = OH_NNCompilation_SetCache(compilation, "/data/local/tmp", 1); 432e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "OH_NNCompilation_SetCache failed."); 433e41f4b71Sopenharmony_ci 434e41f4b71Sopenharmony_ci // Set the performance mode of the device. 435e41f4b71Sopenharmony_ci returnCode = OH_NNCompilation_SetPerformanceMode(compilation, OH_NN_PERFORMANCE_EXTREME); 436e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "OH_NNCompilation_SetPerformanceMode failed."); 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ci // Set the inference priority. 439e41f4b71Sopenharmony_ci returnCode = OH_NNCompilation_SetPriority(compilation, OH_NN_PRIORITY_HIGH); 440e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "OH_NNCompilation_SetPriority failed."); 441e41f4b71Sopenharmony_ci 442e41f4b71Sopenharmony_ci // Specify whether to enable FP16 computing. 443e41f4b71Sopenharmony_ci returnCode = OH_NNCompilation_EnableFloat16(compilation, false); 444e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "OH_NNCompilation_EnableFloat16 failed."); 445e41f4b71Sopenharmony_ci 446e41f4b71Sopenharmony_ci // Perform model building 447e41f4b71Sopenharmony_ci returnCode = OH_NNCompilation_Build(compilation); 448e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "OH_NNCompilation_Build failed."); 449e41f4b71Sopenharmony_ci 450e41f4b71Sopenharmony_ci *pCompilation = compilation; 451e41f4b71Sopenharmony_ci return OH_NN_SUCCESS; 452e41f4b71Sopenharmony_ci } 453e41f4b71Sopenharmony_ci ``` 454e41f4b71Sopenharmony_ci 455e41f4b71Sopenharmony_ci7. Create an executor. 456e41f4b71Sopenharmony_ci 457e41f4b71Sopenharmony_ci After the model building is complete, you need to call the NNRt execution module to create an executor. In the inference phase, operations such as setting the model input, obtaining the model output, and triggering inference computing are performed through the executor. 458e41f4b71Sopenharmony_ci ```cpp 459e41f4b71Sopenharmony_ci OH_NNExecutor* CreateExecutor(OH_NNCompilation* compilation) 460e41f4b71Sopenharmony_ci { 461e41f4b71Sopenharmony_ci // Create an executor based on the specified OH_NNCompilation instance. 462e41f4b71Sopenharmony_ci OH_NNExecutor *executor = OH_NNExecutor_Construct(compilation); 463e41f4b71Sopenharmony_ci CHECKEQ(executor, nullptr, nullptr, "OH_NNExecutor_Construct failed."); 464e41f4b71Sopenharmony_ci return executor; 465e41f4b71Sopenharmony_ci } 466e41f4b71Sopenharmony_ci ``` 467e41f4b71Sopenharmony_ci 468e41f4b71Sopenharmony_ci8. Perform inference computing, and print the inference result. 469e41f4b71Sopenharmony_ci 470e41f4b71Sopenharmony_ci The input data required for inference computing is passed to the executor through the API provided by the execution module. This way, the executor is triggered to perform inference computing once to obtain and print the inference computing result. 471e41f4b71Sopenharmony_ci ```cpp 472e41f4b71Sopenharmony_ci OH_NN_ReturnCode Run(OH_NNExecutor* executor, const std::vector<size_t>& availableDevice) 473e41f4b71Sopenharmony_ci { 474e41f4b71Sopenharmony_ci // Obtain information about the input and output tensors from the executor. 475e41f4b71Sopenharmony_ci // Obtain the number of input tensors. 476e41f4b71Sopenharmony_ci size_t inputCount = 0; 477e41f4b71Sopenharmony_ci auto returnCode = OH_NNExecutor_GetInputCount(executor, &inputCount); 478e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "OH_NNExecutor_GetInputCount failed."); 479e41f4b71Sopenharmony_ci std::vector<NN_TensorDesc*> inputTensorDescs; 480e41f4b71Sopenharmony_ci NN_TensorDesc* tensorDescTmp = nullptr; 481e41f4b71Sopenharmony_ci for (size_t i = 0; i < inputCount; ++i) { 482e41f4b71Sopenharmony_ci // Create the description of the input tensor. 483e41f4b71Sopenharmony_ci tensorDescTmp = OH_NNExecutor_CreateInputTensorDesc(executor, i); 484e41f4b71Sopenharmony_ci CHECKEQ(tensorDescTmp, nullptr, OH_NN_FAILED, "OH_NNExecutor_CreateInputTensorDesc failed."); 485e41f4b71Sopenharmony_ci inputTensorDescs.emplace_back(tensorDescTmp); 486e41f4b71Sopenharmony_ci } 487e41f4b71Sopenharmony_ci // Obtain the number of output tensors. 488e41f4b71Sopenharmony_ci size_t outputCount = 0; 489e41f4b71Sopenharmony_ci returnCode = OH_NNExecutor_GetOutputCount(executor, &outputCount); 490e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "OH_NNExecutor_GetOutputCount failed."); 491e41f4b71Sopenharmony_ci std::vector<NN_TensorDesc*> outputTensorDescs; 492e41f4b71Sopenharmony_ci for (size_t i = 0; i < outputCount; ++i) { 493e41f4b71Sopenharmony_ci // Create the description of the output tensor. 494e41f4b71Sopenharmony_ci tensorDescTmp = OH_NNExecutor_CreateOutputTensorDesc(executor, i); 495e41f4b71Sopenharmony_ci CHECKEQ(tensorDescTmp, nullptr, OH_NN_FAILED, "OH_NNExecutor_CreateOutputTensorDesc failed."); 496e41f4b71Sopenharmony_ci outputTensorDescs.emplace_back(tensorDescTmp); 497e41f4b71Sopenharmony_ci } 498e41f4b71Sopenharmony_ci 499e41f4b71Sopenharmony_ci // Create input and output tensors. 500e41f4b71Sopenharmony_ci NN_Tensor* inputTensors[inputCount]; 501e41f4b71Sopenharmony_ci NN_Tensor* tensor = nullptr; 502e41f4b71Sopenharmony_ci for (size_t i = 0; i < inputCount; ++i) { 503e41f4b71Sopenharmony_ci tensor = nullptr; 504e41f4b71Sopenharmony_ci tensor = OH_NNTensor_Create(availableDevice[0], inputTensorDescs[i]); 505e41f4b71Sopenharmony_ci CHECKEQ(tensor, nullptr, OH_NN_FAILED, "OH_NNTensor_Create failed."); 506e41f4b71Sopenharmony_ci inputTensors[i] = tensor; 507e41f4b71Sopenharmony_ci } 508e41f4b71Sopenharmony_ci NN_Tensor* outputTensors[outputCount]; 509e41f4b71Sopenharmony_ci for (size_t i = 0; i < outputCount; ++i) { 510e41f4b71Sopenharmony_ci tensor = nullptr; 511e41f4b71Sopenharmony_ci tensor = OH_NNTensor_Create(availableDevice[0], outputTensorDescs[i]); 512e41f4b71Sopenharmony_ci CHECKEQ(tensor, nullptr, OH_NN_FAILED, "OH_NNTensor_Create failed."); 513e41f4b71Sopenharmony_ci outputTensors[i] = tensor; 514e41f4b71Sopenharmony_ci } 515e41f4b71Sopenharmony_ci 516e41f4b71Sopenharmony_ci // Set the data of the input tensor. 517e41f4b71Sopenharmony_ci returnCode = SetInputData(inputTensors, inputCount); 518e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "SetInputData failed."); 519e41f4b71Sopenharmony_ci 520e41f4b71Sopenharmony_ci // Perform inference 521e41f4b71Sopenharmony_ci returnCode = OH_NNExecutor_RunSync(executor, inputTensors, inputCount, outputTensors, outputCount); 522e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "OH_NNExecutor_RunSync failed."); 523e41f4b71Sopenharmony_ci 524e41f4b71Sopenharmony_ci // Print the data of the output tensor. 525e41f4b71Sopenharmony_ci Print(outputTensors, outputCount); 526e41f4b71Sopenharmony_ci 527e41f4b71Sopenharmony_ci // Clear the input and output tensors and tensor description. 528e41f4b71Sopenharmony_ci for (size_t i = 0; i < inputCount; ++i) { 529e41f4b71Sopenharmony_ci returnCode = OH_NNTensor_Destroy(&inputTensors[i]); 530e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "OH_NNTensor_Destroy failed."); 531e41f4b71Sopenharmony_ci returnCode = OH_NNTensorDesc_Destroy(&inputTensorDescs[i]); 532e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "OH_NNTensorDesc_Destroy failed."); 533e41f4b71Sopenharmony_ci } 534e41f4b71Sopenharmony_ci for (size_t i = 0; i < outputCount; ++i) { 535e41f4b71Sopenharmony_ci returnCode = OH_NNTensor_Destroy(&outputTensors[i]); 536e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "OH_NNTensor_Destroy failed."); 537e41f4b71Sopenharmony_ci returnCode = OH_NNTensorDesc_Destroy(&outputTensorDescs[i]); 538e41f4b71Sopenharmony_ci CHECKNEQ(returnCode, OH_NN_SUCCESS, OH_NN_FAILED, "OH_NNTensorDesc_Destroy failed."); 539e41f4b71Sopenharmony_ci } 540e41f4b71Sopenharmony_ci 541e41f4b71Sopenharmony_ci return OH_NN_SUCCESS; 542e41f4b71Sopenharmony_ci } 543e41f4b71Sopenharmony_ci ``` 544e41f4b71Sopenharmony_ci 545e41f4b71Sopenharmony_ci9. Build an end-to-end process from model construction to model compilation and execution. 546e41f4b71Sopenharmony_ci 547e41f4b71Sopenharmony_ci Steps 4 to 8 implement the model construction, compilation, and execution processes and encapsulates them into multiple functions to facilitate modular development. The following sample code shows how to apply these functions into a complete NNRt development process. 548e41f4b71Sopenharmony_ci ```cpp 549e41f4b71Sopenharmony_ci int main(int argc, char** argv) 550e41f4b71Sopenharmony_ci { 551e41f4b71Sopenharmony_ci OH_NNModel* model = nullptr; 552e41f4b71Sopenharmony_ci OH_NNCompilation* compilation = nullptr; 553e41f4b71Sopenharmony_ci OH_NNExecutor* executor = nullptr; 554e41f4b71Sopenharmony_ci std::vector<size_t> availableDevices; 555e41f4b71Sopenharmony_ci 556e41f4b71Sopenharmony_ci // Construct a model. 557e41f4b71Sopenharmony_ci OH_NN_ReturnCode ret = BuildModel(&model); 558e41f4b71Sopenharmony_ci if (ret != OH_NN_SUCCESS) { 559e41f4b71Sopenharmony_ci std::cout << "BuildModel failed." << std::endl; 560e41f4b71Sopenharmony_ci OH_NNModel_Destroy(&model); 561e41f4b71Sopenharmony_ci return -1; 562e41f4b71Sopenharmony_ci } 563e41f4b71Sopenharmony_ci 564e41f4b71Sopenharmony_ci // Obtain the available devices. 565e41f4b71Sopenharmony_ci GetAvailableDevices(availableDevices); 566e41f4b71Sopenharmony_ci if (availableDevices.empty()) { 567e41f4b71Sopenharmony_ci std::cout << "No available device." << std::endl; 568e41f4b71Sopenharmony_ci OH_NNModel_Destroy(&model); 569e41f4b71Sopenharmony_ci return -1; 570e41f4b71Sopenharmony_ci } 571e41f4b71Sopenharmony_ci 572e41f4b71Sopenharmony_ci // Build the model. 573e41f4b71Sopenharmony_ci ret = CreateCompilation(model, availableDevices, &compilation); 574e41f4b71Sopenharmony_ci if (ret != OH_NN_SUCCESS) { 575e41f4b71Sopenharmony_ci std::cout << "CreateCompilation failed." << std::endl; 576e41f4b71Sopenharmony_ci OH_NNModel_Destroy(&model); 577e41f4b71Sopenharmony_ci OH_NNCompilation_Destroy(&compilation); 578e41f4b71Sopenharmony_ci return -1; 579e41f4b71Sopenharmony_ci } 580e41f4b71Sopenharmony_ci 581e41f4b71Sopenharmony_ci // Destroy the model instance. 582e41f4b71Sopenharmony_ci OH_NNModel_Destroy(&model); 583e41f4b71Sopenharmony_ci 584e41f4b71Sopenharmony_ci // Create an inference executor for the model. 585e41f4b71Sopenharmony_ci executor = CreateExecutor(compilation); 586e41f4b71Sopenharmony_ci if (executor == nullptr) { 587e41f4b71Sopenharmony_ci std::cout << "CreateExecutor failed, no executor is created." << std::endl; 588e41f4b71Sopenharmony_ci OH_NNCompilation_Destroy(&compilation); 589e41f4b71Sopenharmony_ci return -1; 590e41f4b71Sopenharmony_ci } 591e41f4b71Sopenharmony_ci 592e41f4b71Sopenharmony_ci // Destroy the model building instance. 593e41f4b71Sopenharmony_ci OH_NNCompilation_Destroy(&compilation); 594e41f4b71Sopenharmony_ci 595e41f4b71Sopenharmony_ci // Use the created executor to perform inference. 596e41f4b71Sopenharmony_ci ret = Run(executor, availableDevices); 597e41f4b71Sopenharmony_ci if (ret != OH_NN_SUCCESS) { 598e41f4b71Sopenharmony_ci std::cout << "Run failed." << std::endl; 599e41f4b71Sopenharmony_ci OH_NNExecutor_Destroy(&executor); 600e41f4b71Sopenharmony_ci return -1; 601e41f4b71Sopenharmony_ci } 602e41f4b71Sopenharmony_ci 603e41f4b71Sopenharmony_ci // Destroy the executor instance. 604e41f4b71Sopenharmony_ci OH_NNExecutor_Destroy(&executor); 605e41f4b71Sopenharmony_ci 606e41f4b71Sopenharmony_ci return 0; 607e41f4b71Sopenharmony_ci } 608e41f4b71Sopenharmony_ci ``` 609e41f4b71Sopenharmony_ci 610e41f4b71Sopenharmony_ci## Verification 611e41f4b71Sopenharmony_ci 612e41f4b71Sopenharmony_ci1. Prepare the compilation configuration file of the application sample. 613e41f4b71Sopenharmony_ci 614e41f4b71Sopenharmony_ci Create a `CMakeLists.txt` file, and add compilation configurations to the application sample file `nnrt_example.cpp`. The following is a simple example of the `CMakeLists.txt` file: 615e41f4b71Sopenharmony_ci ```text 616e41f4b71Sopenharmony_ci cmake_minimum_required(VERSION 3.16) 617e41f4b71Sopenharmony_ci project(nnrt_example C CXX) 618e41f4b71Sopenharmony_ci 619e41f4b71Sopenharmony_ci add_executable(nnrt_example 620e41f4b71Sopenharmony_ci ./nnrt_example.cpp 621e41f4b71Sopenharmony_ci ) 622e41f4b71Sopenharmony_ci 623e41f4b71Sopenharmony_ci target_link_libraries(nnrt_example 624e41f4b71Sopenharmony_ci neural_network_runtime 625e41f4b71Sopenharmony_ci neural_network_core 626e41f4b71Sopenharmony_ci ) 627e41f4b71Sopenharmony_ci ``` 628e41f4b71Sopenharmony_ci 629e41f4b71Sopenharmony_ci2. Compile the application sample. 630e41f4b71Sopenharmony_ci 631e41f4b71Sopenharmony_ci Create the **build/** directory in the current directory, and compile `nnrt\_example.cpp` in the **build/** directory to obtain the binary file `nnrt\_example`: 632e41f4b71Sopenharmony_ci ```shell 633e41f4b71Sopenharmony_ci mkdir build && cd build 634e41f4b71Sopenharmony_ci cmake -DCMAKE_TOOLCHAIN_FILE={Path of the cross-compilation toolchain}/build/cmake/ohos.toolchain.cmake -DOHOS_ARCH=arm64-v8a -DOHOS_PLATFORM=OHOS -DOHOS_STL=c++_static .. 635e41f4b71Sopenharmony_ci make 636e41f4b71Sopenharmony_ci ``` 637e41f4b71Sopenharmony_ci 638e41f4b71Sopenharmony_ci3. Push the application sample to the device for execution. 639e41f4b71Sopenharmony_ci ```shell 640e41f4b71Sopenharmony_ci # Push the `nnrt_example` obtained through compilation to the device, and execute it. 641e41f4b71Sopenharmony_ci hdc_std file send ./nnrt_example /data/local/tmp/. 642e41f4b71Sopenharmony_ci 643e41f4b71Sopenharmony_ci # Grant required permissions to the executable file of the test case. 644e41f4b71Sopenharmony_ci hdc_std shell "chmod +x /data/local/tmp/nnrt_example" 645e41f4b71Sopenharmony_ci 646e41f4b71Sopenharmony_ci # Execute the test case. 647e41f4b71Sopenharmony_ci hdc_std shell "/data/local/tmp/nnrt_example" 648e41f4b71Sopenharmony_ci ``` 649e41f4b71Sopenharmony_ci 650e41f4b71Sopenharmony_ci If the execution is normal, information similar to the following is displayed: 651e41f4b71Sopenharmony_ci ```text 652e41f4b71Sopenharmony_ci Output index: 0, value is: 0.000000. 653e41f4b71Sopenharmony_ci Output index: 1, value is: 2.000000. 654e41f4b71Sopenharmony_ci Output index: 2, value is: 4.000000. 655e41f4b71Sopenharmony_ci Output index: 3, value is: 6.000000. 656e41f4b71Sopenharmony_ci Output index: 4, value is: 8.000000. 657e41f4b71Sopenharmony_ci Output index: 5, value is: 10.000000. 658e41f4b71Sopenharmony_ci Output index: 6, value is: 12.000000. 659e41f4b71Sopenharmony_ci Output index: 7, value is: 14.000000. 660e41f4b71Sopenharmony_ci Output index: 8, value is: 16.000000. 661e41f4b71Sopenharmony_ci Output index: 9, value is: 18.000000. 662e41f4b71Sopenharmony_ci Output index: 10, value is: 20.000000. 663e41f4b71Sopenharmony_ci Output index: 11, value is: 22.000000. 664e41f4b71Sopenharmony_ci ``` 665e41f4b71Sopenharmony_ci 666e41f4b71Sopenharmony_ci4. (Optional) Check the model cache. 667e41f4b71Sopenharmony_ci 668e41f4b71Sopenharmony_ci If the HDI service connected to NNRt supports the model cache function, you can find the generated cache file in the `/data/local/tmp` directory after the `nnrt_example` is executed successfully. 669e41f4b71Sopenharmony_ci 670e41f4b71Sopenharmony_ci > **NOTE** 671e41f4b71Sopenharmony_ci > 672e41f4b71Sopenharmony_ci > The IR graphs of the model need to be passed to the hardware driver layer, so that the HDI service compiles the IR graphs into a computing graph dedicated to hardware. The compilation process is time-consuming. The NNRt supports the computing graph cache feature. It can cache the computing graphs compiled by the HDI service to the device storage. If the same model is compiled on the same acceleration chip next time, you can specify the cache path so that NNRt can directly load the computing graphs in the cache file, reducing the compilation time. 673e41f4b71Sopenharmony_ci 674e41f4b71Sopenharmony_ci Check the cached files in the cache directory. 675e41f4b71Sopenharmony_ci ```shell 676e41f4b71Sopenharmony_ci ls /data/local/tmp 677e41f4b71Sopenharmony_ci ``` 678e41f4b71Sopenharmony_ci 679e41f4b71Sopenharmony_ci The command output is as follows: 680e41f4b71Sopenharmony_ci ```text 681e41f4b71Sopenharmony_ci # 0.nncache 1.nncache 2.nncache cache_info.nncache 682e41f4b71Sopenharmony_ci ``` 683e41f4b71Sopenharmony_ci 684e41f4b71Sopenharmony_ci If the cache is no longer used, manually delete the cache files. 685e41f4b71Sopenharmony_ci ```shell 686e41f4b71Sopenharmony_ci rm /data/local/tmp/*nncache 687e41f4b71Sopenharmony_ci ``` 688