目录
引言
驱动移植
例程编译
修改build.sh
执行编译
运行测试
部署libc的库文件
执行测试程序
结语
引言
鸿睿创智的H01开发板是基于RK3568芯片的,瑞芯微芯片的一大特色就是提供了NPU推理的支持。本文将对其NPU推理进行测试。
驱动移植
H01的开发板已经移植好了RKNN的驱动程序。如果是其他RK3568的开发板使用OpenHarmony提供的主线代码,可能没有做这个移植,可以参考:OpenHarmony标准系统上实现对rk系列芯片NPU的支持(驱动移植)-鸿蒙开发者社区-51CTO.COMhttps://ost.51cto.com/posts/29962?login=from_csdn我使用hdc shell dmesg | grep "RKNPU"
查看开机内核打印,看不到任何相关信息。改用查看可以看到如下信息证明驱动是正常的:
例程编译
首先启动Docker编译环境(参见【鸿睿创智开发板试用】移植OpenCV 4到OpenHarmony 4.1-CSDN博客)。然后下载RKNN Toolkit2:
git clone https://github.com/airockchip/rknn-toolkit2.git
我选择的是rknn_benchmark示例(rknpu2/examples/rknn_benchmark),这是测试RKNN工作性能的。
修改build.sh
RKNN的运行库是用GCC编译的,对GCC库有依赖。厂商提供的样例也是GCC编译的。这点和一般的OpenHarmony程序基于LLVM编译不同。
这是build-linux_RK3566-3568.sh文件的修改:
# for aarch64
GCC_COMPILER=/home/openharmony/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu
执行编译
./build-linux_RK3566_RK3568.sh
程序编译的过程如下:
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /home/openharmony/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc
-- Check for working C compiler: /home/openharmony/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /home/openharmony/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++
-- Check for working CXX compiler: /home/openharmony/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
x86_64
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
TARGET_SOC
-- Build files have been written to: /home/openharmony/rknn-toolkit2/rknpu2/examples/rknn_benchmark/build/build_linux_aarch64
Scanning dependencies of target rknn_benchmark
[ 66%] Building CXX object CMakeFiles/rknn_benchmark.dir/src/cnpy/cnpy.cpp.o
[ 66%] Building CXX object CMakeFiles/rknn_benchmark.dir/src/rknn_benchmark.cpp.o
[100%] Linking CXX executable rknn_benchmark
[100%] Built target rknn_benchmark
[100%] Built target rknn_benchmark
Install the project...
-- Install configuration: ""
-- Installing: /home/openharmony/rknn-toolkit2/rknpu2/examples/rknn_benchmark/install/rknn_benchmark_Linux/./rknn_benchmark
-- Set runtime path of "/home/openharmony/rknn-toolkit2/rknpu2/examples/rknn_benchmark/install/rknn_benchmark_Linux/./rknn_benchmark" to "lib"
-- Up-to-date: /home/openharmony/rknn-toolkit2/rknpu2/examples/rknn_benchmark/install/rknn_benchmark_Linux/lib/librknnrt.so
/home/openharmony/rknn-toolkit2/rknpu2/examples/rknn_benchmark
最后所有测试所需要的文件都被打包到install目录中。
运行测试
部署libc的库文件
程序是基于gcc编译的,而gcc所使用的库文件在开发板上是没有的,所以需要将/home/openharmony/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/aarch64-linux-gnu/libc/lib下的所有文件打包,然后拷贝到开发板上解包到/lib目录下。
执行测试程序
最后就是用mobilenet_v1.rknn文件进行测试,结果如下:
# ./rknn_benchmark mobilenet_v1.rknn
rknn_api/rknnrt version: 2.3.0 (c949ad889d@2024-11-07T11:35:33), driver version: 0.8.8
total weight size: 4365632, total internal size: 1756160
total dma used size: 10706944
model input num: 1, output num: 1
input tensors:
index=0, name=input, n_dims=4, dims=[1, 224, 224, 3], n_elems=150528, size=150528, w_stride = 224, size_with_stride=150528, fmt=NHWC, type=INT8, qnt_type=AFFINE, zp=0, scale=0.007812
output tensors:
index=0, name=MobilenetV1/Predictions/Reshape_1, n_dims=2, dims=[1, 1001], n_elems=1001, size=2002, w_stride = 0, size_with_stride=2002, fmt=UNDEFINED, type=FP16, qnt_type=AFFINE, zp=0, scale=1.000000
custom string:
Warmup ...
0: Elapse Time = 8.19ms, FPS = 122.12
1: Elapse Time = 7.86ms, FPS = 127.31
2: Elapse Time = 7.86ms, FPS = 127.24
3: Elapse Time = 7.76ms, FPS = 128.78
4: Elapse Time = 7.79ms, FPS = 128.39
Begin perf ...
0: Elapse Time = 7.74ms, FPS = 129.20
1: Elapse Time = 7.78ms, FPS = 128.62
2: Elapse Time = 7.80ms, FPS = 128.12
3: Elapse Time = 7.97ms, FPS = 125.41
4: Elapse Time = 7.88ms, FPS = 126.95
5: Elapse Time = 7.71ms, FPS = 129.67
6: Elapse Time = 5.13ms, FPS = 195.08
7: Elapse Time = 5.16ms, FPS = 193.91
8: Elapse Time = 5.12ms, FPS = 195.20
9: Elapse Time = 5.11ms, FPS = 195.73
Avg Time 6.74ms, Avg FPS = 148.372
Save output to rt_output0.npy
---- Top5 ----
0.049622 - 620
0.044678 - 645
0.032288 - 627
0.021011 - 819
0.016953 - 790
结语
经过一番折腾,最后实现了在开发板上运行RKNN样例的工作。不过这还是不是最终结果,还需要测试一下在App中是否可以正常调用,特别是和OpenCV结合。