• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Neural Network Runtime对接AI推理框架开发指导
2
3## 场景介绍
4
5Neural Network Runtime作为AI推理引擎和加速芯片的桥梁,为AI推理引擎提供精简的Native接口,满足推理引擎通过加速芯片执行端到端推理的需求。
6
7本文以图1展示的`Add`单算子模型为例,介绍Neural Network Runtime的开发流程。`Add`算子包含两个输入、一个参数和一个输出,其中的`activation`参数用于指定`Add`算子中激活函数的类型。
8
9**图1** Add单算子网络示意图
10!["Add单算子网络示意图"](figures/02.png)
11
12## 环境准备
13
14### 环境要求
15
16Neural Network Runtime部件的环境要求如下:
17
18- 系统版本:OpenHarmony master分支。
19- 开发环境:Ubuntu 18.04及以上。
20- 接入设备:OpenHarmony定义的标准设备,并且系统中内置的硬件加速器驱动,已通过HDI接口对接Neural Network Runtime。
21
22由于Neural Network Runtime通过Native API对外开放,需要通过Native开发套件编译Neural Network Runtime应用。
23
24### 环境搭建
25
261. 打开Ubuntu编译服务器的终端。
272. 把Native开发套件压缩包拷贝至当前用户根目录下。
283. 执行以下命令解压Native开发套件的压缩包。
29
30```shell
31unzip native-linux-{版本号}.zip
32```
33
34解压缩后的内容如下(随版本迭代,目录下的内容可能发生变化,请以最新版本的Native API为准):
35```text
36native/
37├── build // 交叉编译工具链
38├── build-tools // 编译构建工具
39├── docs
40├── llvm
41├── nativeapi_syscap_config.json
42├── ndk_system_capability.json
43├── NOTICE.txt
44├── oh-uni-package.json
45└── sysroot // Native API头文件和库
46```
47## 接口说明
48
49这里给出Neural Network Runtime开发流程中通用的接口,具体请见下列表格。
50
51### 结构体
52
53| 结构体名称 | 描述 |
54| --------- | ---- |
55| typedef struct OH_NNModel OH_NNModel | Neural Network Runtime的模型句柄,用于构造模型。 |
56| typedef struct OH_NNCompilation OH_NNCompilation | Neural Network Runtime的编译器句柄,用于编译AI模型。 |
57| typedef struct OH_NNExecutor OH_NNExecutor | Neural Network Runtime的执行器句柄,用于在指定设备上执行推理计算。 |
58
59### 模型构造相关接口
60
61| 接口名称 | 描述 |
62| ------- | --- |
63| OH_NNModel_Construct() | 创建OH_NNModel类型的模型实例。 |
64| OH_NN_ReturnCode OH_NNModel_AddTensor(OH_NNModel *model, const OH_NN_Tensor *tensor) | 向模型实例中添加张量。 |
65| OH_NN_ReturnCode OH_NNModel_SetTensorData(OH_NNModel *model, uint32_t index, const void *dataBuffer, size_t length) | 设置张量的数值。 |
66| 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) | 向模型实例中添加算子。 |
67| OH_NN_ReturnCode OH_NNModel_SpecifyInputsAndOutputs(OH_NNModel *model, const OH_NN_UInt32Array *inputIndices, const OH_NN_UInt32Array *outputIndices) | 指定模型的输入输出。 |
68| OH_NN_ReturnCode OH_NNModel_Finish(OH_NNModel *model) | 完成模型构图。|
69| void OH_NNModel_Destroy(OH_NNModel **model) | 释放模型实例。 |
70
71### 模型编译相关接口
72
73| 接口名称 | 描述 |
74| ------- | --- |
75| OH_NNCompilation *OH_NNCompilation_Construct(const OH_NNModel *model) | 创建OH_NNCompilation类型的编译实例。 |
76| OH_NN_ReturnCode OH_NNCompilation_SetDevice(OH_NNCompilation *compilation, size_t deviceID) | 指定模型编译和计算的硬件。 |
77| OH_NN_ReturnCode OH_NNCompilation_SetCache(OH_NNCompilation *compilation, const char *cachePath, uint32_t version) | 设置编译后的模型缓存路径和缓存版本。 |
78| OH_NN_ReturnCode OH_NNCompilation_Build(OH_NNCompilation *compilation) | 进行模型编译。 |
79| void OH_NNCompilation_Destroy(OH_NNCompilation **compilation) | 释放OH_NNCompilation对象。 |
80
81### 执行推理相关接口
82
83| 接口名称 | 描述 |
84| ------- | --- |
85| OH_NNExecutor *OH_NNExecutor_Construct(OH_NNCompilation *compilation) | 创建OH_NNExecutor类型的执行器实例。 |
86| OH_NN_ReturnCode OH_NNExecutor_SetInput(OH_NNExecutor *executor, uint32_t inputIndex, const OH_NN_Tensor *tensor, const void *dataBuffer, size_t length) | 设置模型单个输入的数据。 |
87| OH_NN_ReturnCode OH_NNExecutor_SetOutput(OH_NNExecutor *executor, uint32_t outputIndex, void *dataBuffer, size_t length) | 设置模型单个输出的缓冲区。 |
88| OH_NN_ReturnCode OH_NNExecutor_Run(OH_NNExecutor *executor) | 执行推理。 |
89| void OH_NNExecutor_Destroy(OH_NNExecutor **executor) | 销毁OH_NNExecutor实例,释放实例占用的内存。 |
90
91### 设备管理相关接口
92
93| 接口名称 | 描述 |
94| ------- | --- |
95| OH_NN_ReturnCode OH_NNDevice_GetAllDevicesID(const size_t **allDevicesID, uint32_t *deviceCount) | 获取对接到 Neural Network Runtime 的硬件ID。 |
96
97
98## 开发步骤
99
100Neural Network Runtime的开发流程主要包含**模型构造**、**模型编译**和**推理执行**三个阶段。以下开发步骤以`Add`单算子模型为例,介绍调用Neural Network Runtime接口,开发应用的过程。
101
1021. 创建应用样例文件。
103
104    首先,创建Neural Network Runtime应用样例的源文件。在项目目录下执行以下命令,创建`nnrt_example/`目录,在目录下创建 `nnrt_example.cpp` 源文件。
105
106    ```shell
107    mkdir ~/nnrt_example && cd ~/nnrt_example
108    touch nnrt_example.cpp
109    ```
110
1112. 导入Neural Network Runtime。
112
113    在 `nnrt_example.cpp` 文件的开头添加以下代码,引入Neural Network Runtime模块。
114
115    ```cpp
116    #include <cstdint>
117    #include <iostream>
118    #include <vector>
119
120    #include "neural_network_runtime/neural_network_runtime.h"
121
122    // 常量,用于指定输入、输出数据的字节长度
123    const size_t DATA_LENGTH = 4 * 12;
124    ```
125
1263. 构造模型。
127
128    使用Neural Network Runtime接口,构造`Add`单算子样例模型。
129
130    ```cpp
131    OH_NN_ReturnCode BuildModel(OH_NNModel** pModel)
132    {
133        // 创建模型实例,进行模型构造
134        OH_NNModel* model = OH_NNModel_Construct();
135        if (model == nullptr) {
136            std::cout << "Create model failed." << std::endl;
137            return OH_NN_MEMORY_ERROR;
138        }
139
140        // 添加Add算子的第一个输入Tensor,类型为float32,张量形状为[1, 2, 2, 3]
141        int32_t inputDims[4] = {1, 2, 2, 3};
142        OH_NN_Tensor input1 = {OH_NN_FLOAT32, 4, inputDims, nullptr, OH_NN_TENSOR};
143        OH_NN_ReturnCode ret = OH_NNModel_AddTensor(model, &input1);
144        if (ret != OH_NN_SUCCESS) {
145            std::cout << "BuildModel failed, add Tensor of first input failed." << std::endl;
146            return ret;
147        }
148
149        // 添加Add算子的第二个输入Tensor,类型为float32,张量形状为[1, 2, 2, 3]
150        OH_NN_Tensor input2 = {OH_NN_FLOAT32, 4, inputDims, nullptr, OH_NN_TENSOR};
151        ret = OH_NNModel_AddTensor(model, &input2);
152        if (ret != OH_NN_SUCCESS) {
153            std::cout << "BuildModel failed, add Tensor of second input failed." << std::endl;
154            return ret;
155        }
156
157        // 添加Add算子的参数Tensor,该参数Tensor用于指定激活函数的类型,Tensor的数据类型为int8。
158        int32_t activationDims = 1;
159        int8_t activationValue = OH_NN_FUSED_NONE;
160        OH_NN_Tensor activation = {OH_NN_INT8, 1, &activationDims, nullptr, OH_NN_ADD_ACTIVATIONTYPE};
161        ret = OH_NNModel_AddTensor(model, &activation);
162        if (ret != OH_NN_SUCCESS) {
163            std::cout << "BuildModel failed, add Tensor of activation failed." << std::endl;
164            return ret;
165        }
166
167        // 将激活函数类型设置为OH_NN_FUSED_NONE,表示该算子不添加激活函数。
168        ret = OH_NNModel_SetTensorData(model, 2, &activationValue, sizeof(int8_t));
169        if (ret != OH_NN_SUCCESS) {
170            std::cout << "BuildModel failed, set value of activation failed." << std::endl;
171            return ret;
172        }
173
174        // 设置Add算子的输出,类型为float32,张量形状为[1, 2, 2, 3]
175        OH_NN_Tensor output = {OH_NN_FLOAT32, 4, inputDims, nullptr, OH_NN_TENSOR};
176        ret = OH_NNModel_AddTensor(model, &output);
177        if (ret != OH_NN_SUCCESS) {
178            std::cout << "BuildModel failed, add Tensor of output failed." << std::endl;
179            return ret;
180        }
181
182        // 指定Add算子的输入、参数和输出索引
183        uint32_t inputIndicesValues[2] = {0, 1};
184        uint32_t paramIndicesValues = 2;
185        uint32_t outputIndicesValues = 3;
186        OH_NN_UInt32Array paramIndices = {&paramIndicesValues, 1};
187        OH_NN_UInt32Array inputIndices = {inputIndicesValues, 2};
188        OH_NN_UInt32Array outputIndices = {&outputIndicesValues, 1};
189
190        // 向模型实例添加Add算子
191        ret = OH_NNModel_AddOperation(model, OH_NN_OPS_ADD, &paramIndices, &inputIndices, &outputIndices);
192        if (ret != OH_NN_SUCCESS) {
193            std::cout << "BuildModel failed, add operation failed." << std::endl;
194            return ret;
195        }
196
197        // 设置模型实例的输入、输出索引
198        ret = OH_NNModel_SpecifyInputsAndOutputs(model, &inputIndices, &outputIndices);
199        if (ret != OH_NN_SUCCESS) {
200            std::cout << "BuildModel failed, specify inputs and outputs failed." << std::endl;
201            return ret;
202        }
203
204        // 完成模型实例的构建
205        ret = OH_NNModel_Finish(model);
206        if (ret != OH_NN_SUCCESS) {
207            std::cout << "BuildModel failed, error happened when finishing model construction." << std::endl;
208            return ret;
209        }
210
211        *pModel = model;
212        return OH_NN_SUCCESS;
213    }
214    ```
215
2164. 查询Neural Network Runtime已经对接的加速芯片。
217
218    Neural Network Runtime支持通过HDI接口,对接多种加速芯片。在执行模型编译前,需要查询当前设备下,Neural Network Runtime已经对接的加速芯片。每个加速芯片对应唯一的ID值,在编译阶段需要通过设备ID,指定模型编译的芯片。
219    ```cpp
220    void GetAvailableDevices(std::vector<size_t>& availableDevice)
221    {
222        availableDevice.clear();
223
224        // 获取可用的硬件ID
225        const size_t* devices = nullptr;
226        uint32_t deviceCount = 0;
227        OH_NN_ReturnCode ret = OH_NNDevice_GetAllDevicesID(&devices, &deviceCount);
228        if (ret != OH_NN_SUCCESS) {
229            std::cout << "GetAllDevicesID failed, get no available device." << std::endl;
230            return;
231        }
232
233        for (uint32_t i = 0; i < deviceCount; i++) {
234            availableDevice.emplace_back(devices[i]);
235        }
236    }
237    ```
238
2395. 在指定的设备上编译模型。
240
241    Neural Network Runtime使用抽象的模型表达描述AI模型的拓扑结构,在加速芯片上执行前,需要通过Neural Network Runtime提供的编译模块,将抽象的模型表达下发至芯片驱动层,转换成可以直接推理计算的格式。
242    ```cpp
243    OH_NN_ReturnCode CreateCompilation(OH_NNModel* model, const std::vector<size_t>& availableDevice, OH_NNCompilation** pCompilation)
244    {
245        // 创建编译实例,用于将模型传递至底层硬件编译
246        OH_NNCompilation* compilation = OH_NNCompilation_Construct(model);
247        if (compilation == nullptr) {
248            std::cout << "CreateCompilation failed, error happened when creating compilation." << std::endl;
249            return OH_NN_MEMORY_ERROR;
250        }
251
252        // 设置编译的硬件、缓存路径、性能模式、计算优先级、是否开启float16低精度计算等选项
253
254        // 选择在第一个设备上编译模型
255        OH_NN_ReturnCode ret = OH_NNCompilation_SetDevice(compilation, availableDevice[0]);
256        if (ret != OH_NN_SUCCESS) {
257            std::cout << "CreateCompilation failed, error happened when setting device." << std::endl;
258            return ret;
259        }
260
261        // 将模型编译结果缓存在/data/local/tmp目录下,版本号指定为1
262        ret = OH_NNCompilation_SetCache(compilation, "/data/local/tmp", 1);
263        if (ret != OH_NN_SUCCESS) {
264            std::cout << "CreateCompilation failed, error happened when setting cache path." << std::endl;
265            return ret;
266        }
267
268        // 完成编译设置,进行模型编译
269        ret = OH_NNCompilation_Build(compilation);
270        if (ret != OH_NN_SUCCESS) {
271            std::cout << "CreateCompilation failed, error happened when building compilation." << std::endl;
272            return ret;
273        }
274
275        *pCompilation = compilation;
276        return OH_NN_SUCCESS;
277    }
278    ```
279
2806. 创建执行器。
281
282    完成模型编译后,需要调用Neural Network Runtime的执行模块,创建推理执行器。执行阶段,设置模型输入、获取模型输出和触发推理计算的操作均围绕执行器完成。
283    ```cpp
284    OH_NNExecutor* CreateExecutor(OH_NNCompilation* compilation)
285    {
286        // 创建执行实例
287        OH_NNExecutor* executor = OH_NNExecutor_Construct(compilation);
288        return executor;
289    }
290    ```
291
2927. 执行推理计算,并打印计算结果。
293
294    通过执行模块提供的接口,将推理计算所需要的输入数据传递给执行器,触发执行器完成一次推理计算,获取模型的推理计算结果。
295    ```cpp
296    OH_NN_ReturnCode Run(OH_NNExecutor* executor)
297    {
298        // 构造示例数据
299        float input1[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
300        float input2[12] = {11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22};
301
302        int32_t inputDims[4] = {1, 2, 2, 3};
303        OH_NN_Tensor inputTensor1 = {OH_NN_FLOAT32, 4, inputDims, nullptr, OH_NN_TENSOR};
304        OH_NN_Tensor inputTensor2 = {OH_NN_FLOAT32, 4, inputDims, nullptr, OH_NN_TENSOR};
305
306        // 设置执行的输入
307
308        // 设置执行的第一个输入,输入数据由input1指定
309        OH_NN_ReturnCode ret = OH_NNExecutor_SetInput(executor, 0, &inputTensor1, input1, DATA_LENGTH);
310        if (ret != OH_NN_SUCCESS) {
311            std::cout << "Run failed, error happened when setting first input." << std::endl;
312            return ret;
313        }
314
315        // 设置执行的第二个输入,输入数据由input2指定
316        ret = OH_NNExecutor_SetInput(executor, 1, &inputTensor2, input2, DATA_LENGTH);
317        if (ret != OH_NN_SUCCESS) {
318            std::cout << "Run failed, error happened when setting second input." << std::endl;
319            return ret;
320        }
321
322        // 设置输出的数据缓冲区,OH_NNExecutor_Run执行计算后,输出结果将保留在output中
323        float output[12];
324        ret = OH_NNExecutor_SetOutput(executor, 0, output, DATA_LENGTH);
325        if (ret != OH_NN_SUCCESS) {
326            std::cout << "Run failed, error happened when setting output buffer." << std::endl;
327            return ret;
328        }
329
330        // 执行计算
331        ret = OH_NNExecutor_Run(executor);
332        if (ret != OH_NN_SUCCESS) {
333            std::cout << "Run failed, error doing execution." << std::endl;
334            return ret;
335        }
336
337        // 打印输出结果
338        for (uint32_t i = 0; i < 12; i++) {
339            std::cout << "Output index: " << i << ", value is: " << output[i] << "." << std::endl;
340        }
341
342        return OH_NN_SUCCESS;
343    }
344    ```
345
3468. 构建端到端模型构造-编译-执行流程。
347
348    步骤3-步骤7实现了模型的模型构造、编译和执行流程,并封装成4个函数,便于模块化开发。以下示例代码将4个函数串联成完整的Neural Network Runtime开发流程。
349    ```cpp
350    int main()
351    {
352        OH_NNModel* model = nullptr;
353        OH_NNCompilation* compilation = nullptr;
354        OH_NNExecutor* executor = nullptr;
355        std::vector<size_t> availableDevices;
356
357        // 模型构造阶段
358        OH_NN_ReturnCode ret = BuildModel(&model);
359        if (ret != OH_NN_SUCCESS) {
360            std::cout << "BuildModel failed." << std::endl;
361            OH_NNModel_Destroy(&model);
362            return -1;
363        }
364
365        // 获取可执行的设备
366        GetAvailableDevices(availableDevices);
367        if (availableDevices.empty()) {
368            std::cout << "No available device." << std::endl;
369            OH_NNModel_Destroy(&model);
370            return -1;
371        }
372
373        // 模型编译阶段
374        ret = CreateCompilation(model, availableDevices, &compilation);
375        if (ret != OH_NN_SUCCESS) {
376            std::cout << "CreateCompilation failed." << std::endl;
377            OH_NNModel_Destroy(&model);
378            OH_NNCompilation_Destroy(&compilation);
379            return -1;
380        }
381
382        // 创建模型的推理执行器
383        executor = CreateExecutor(compilation);
384        if (executor == nullptr) {
385            std::cout << "CreateExecutor failed, no executor is created." << std::endl;
386            OH_NNModel_Destroy(&model);
387            OH_NNCompilation_Destroy(&compilation);
388            return -1;
389        }
390
391        // 使用上一步创建的执行器,执行单步推理计算
392        ret = Run(executor);
393        if (ret != OH_NN_SUCCESS) {
394            std::cout << "Run failed." << std::endl;
395            OH_NNModel_Destroy(&model);
396            OH_NNCompilation_Destroy(&compilation);
397            OH_NNExecutor_Destroy(&executor);
398            return -1;
399        }
400
401        // 释放申请的资源
402        OH_NNModel_Destroy(&model);
403        OH_NNCompilation_Destroy(&compilation);
404        OH_NNExecutor_Destroy(&executor);
405
406        return 0;
407    }
408    ```
409
410## 调测验证
411
4121. 准备应用样例的编译配置文件。
413
414    新建一个 `CMakeLists.txt` 文件,为开发步骤中的应用样例文件 `nnrt_example.cpp` 添加编译配置。以下提供简单的 `CMakeLists.txt` 示例:
415    ```text
416    cmake_minimum_required(VERSION 3.16)
417    project(nnrt_example C CXX)
418
419    add_executable(nnrt_example
420        ./nnrt_example.cpp
421    )
422
423    target_link_libraries(nnrt_example
424        neural_network_runtime.z
425    )
426    ```
427
4282. 编译应用样例。
429
430    执行以下命令,在当前目录下新建build/目录,在build/目录下编译 `nnrt_example.cpp`,得到二进制文件 `nnrt_example`。
431    ```shell
432    mkdir build && cd build
433    cmake -DCMAKE_TOOLCHAIN_FILE={交叉编译工具链的路径}/build/cmake/ohos.toolchain.cmake -DOHOS_ARCH=arm64-v8a -DOHOS_PLATFORM=OHOS -DOHOS_STL=c++_static ..
434    make
435    ```
436
4373. 执行以下代码,将样例推送到设备上执行。
438    ```shell
439    # 将编译得到的 `nnrt_example` 推送到设备上,执行样例。
440    hdc_std file send ./nnrt_example /data/local/tmp/.
441
442    # 给测试用例可执行文件加上权限。
443    hdc_std shell "chmod +x /data/local/tmp/nnrt_example"
444
445    # 执行测试用例
446    hdc_std shell "/data/local/tmp/nnrt_example"
447    ```
448
449    如果样例执行正常,应该得到以下输出。
450    ```text
451    Output index: 0, value is: 11.000000.
452    Output index: 1, value is: 13.000000.
453    Output index: 2, value is: 15.000000.
454    Output index: 3, value is: 17.000000.
455    Output index: 4, value is: 19.000000.
456    Output index: 5, value is: 21.000000.
457    Output index: 6, value is: 23.000000.
458    Output index: 7, value is: 25.000000.
459    Output index: 8, value is: 27.000000.
460    Output index: 9, value is: 29.000000.
461    Output index: 10, value is: 31.000000.
462    Output index: 11, value is: 33.000000.
463    ```
464
4654. 检查模型缓存(可选)。
466
467    如果在调测环境下,Neural Network Runtime对接的HDI服务支持模型缓存功能,执行完 `nnrt_example`, 可以在 `/data/local/tmp` 目录下找到生成的缓存文件。
468
469    > **说明:**
470    >
471    > 模型的IR需要传递到硬件驱动层,由HDI服务将统一的IR图,编译成硬件专用的计算图,编译的过程非常耗时。Neural Network Runtime支持计算图缓存的特性,可以将HDI服务编译生成的计算图,缓存到设备存储中。当下一次在同一个加速芯片上编译同一个模型时,通过指定缓存的路径,Neural Network Runtime可以直接加载缓存文件中的计算图,减少编译消耗的时间。
472
473    检查缓存目录下的缓存文件:
474
475    ```shell
476    ls /data/local/tmp
477    ```
478
479    以下为打印结果:
480
481    ```text
482    # 0.nncache  cache_info.nncache
483    ```
484
485    如果缓存不再使用,需要手动删除缓存,可以参考以下命令,删除缓存文件。
486
487    ```shell
488    rm /data/local/tmp/*nncache
489    ```
490
491## 相关实例
492
493第三方AI推理框架对接Neural Network Runtime的流程,可以参考以下相关实例:
494
495- [Tensorflow Lite接入NNRt Delegate开发指南](https://gitee.com/openharmony/ai_neural_network_runtime/tree/master/example/deep_learning_framework)
496