在上一实践《sherpa-onnx AI语音框架添加acl加速库实践》中,笔者基于最新github源码版本编译出的onnxruntime动态库(包括acl库)测试效果不理想,后续尝试下载onnxruntime的最新基线版本V.1.22.0源码重新验证。
由于基于RK3588目标平台的本地native模式编译过程效率低,耗时久,而且编译过程中频繁导致系统假挂死(表现为编译过程停滞,串口无法活动;强制退出编译又可恢复;问题可能与笔者具体环境有关),后来干脆转为基于x86系统的交叉编译模式。
笔者输出本文的目的:
1)onnxruntime官网没有专门介绍交叉编译的章节(笔者没有找到);网上已有一些介绍onnxruntime交叉编译的文章,不过介绍时的版本偏低(v1.18以下),内容与目前onnxruntime的版本更新已对不上。
2)笔者交叉编译过程中碰到不少问题,这里一起分享一下(可少走弯路)。
笔者编译环境为:i7-4770 CPU + openEuler 22.03 (LTS-SP2);其他较新版本的centos或ubuntu应该也OK。
onnxruntime-1.22.0的交叉编译依赖要求:
1)Python版本要求3.10以上;否则会出现后面3.1的编译语法错误。
2)cmake版本要求3.28以上;笔者下载cmake官网v3.31.8版本源码编译安装升级。
3)aarch64-none-linux-gnu-gcc交叉编译器版本要求11.0以上;笔者使用为11.3-aarch64(2022-12-06)版本。交叉编译器的$PATH路径环境变量可以事先在"~/.bashrc"文件中设置好,笔者的配置如下:
export PATH=~/toolchain/11.3-aarch64/bin:$PATH
#下载onnxruntime的最新v1.22.0
cd ~
wget -c https://github.com/microsoft/onnxruntime/archive/refs/tags/v1.22.0.tar.gz
tar xvf v1.22.0.tar.gz
cd onnxruntime-1.22.0
./build.sh --build_shared_lib --config Release --allow_running_as_root --skip_submodule_sync --skip_tests --cmake_extra_defines CMAKE_TOOLCHAIN_FILE=linux_arm64_crosscompile_toolchain.cmake --parallel 6
说明:
1)笔者写这篇文章时,onnxruntime的最新打标版本已经升级到v1.22.1了(有小修订,读者可以直接下载最新);
2)v1.22.0版本中有个小bug,直接编译会有问题,具体措施参见后续3.2章节的描述;
3)编译参数“skip_submodule_sync”为避免编译过程中的自动子模块git同步操作(非必须);
4)交叉编译的工具链配置参数:“--cmake_extra_defines CMAKE_TOOLCHAIN_FILE=linux_arm64_crosscompile_toolchain.cmake”,具体参数配置可以参见./cmake/linux_arm64_crosscompile_toolchain.cmake文件,其中编译器“ CMAKE_C_COMPILER和CMAKE_CXX_COMPILER”的名称要与2.1章节“11.3-aarch64”工具链中的名称要一致,否则编译时会告警找不到编译器。
后续为笔者编译过程中碰到的一些问题和解决办法,供有兴趣的读者参考。
# ./build.sh --build_shared_lib --config Release --allow_running_as_root --skip_submodule_sync --skip_tests
Traceback (most recent call last): File "./onnxruntime-1.22.0/tools/ci_build/build.py", line 32, in
from build_args import parse_arguments # noqa: E402 File ./onnxruntime-1.22.0/tools/ci_build/build_args.py", line 18 match s.lower(): ^ SyntaxError: invalid syntax
原因:match s.lower(): 是 Python 3.10 引入的 structural pattern matching 语法。
笔者环境中默认使用的 Python 版本为3.9.9,不支持这个语法解析,从而导致语法错误。
解决办法:升级到3.10以上版本的Python,笔者下载Python官网版本3.12.11源码编译安装后问题解决。
-- Using src='https://gitlab.com/libeigen/eigen/-/archive/1d8b82b0740839c0de7f1242a3585e3390ff5f33/eigen-1d8b82b0740839c0de7f1242a3585e3390ff5f33.zip'
-- verifying file...
file='~/onnxruntime-1.22.0/build/Linux/Release/_deps/eigen3-subbuild/eigen3-populate-prefix/src/eigen-1d8b82b0740839c0de7f1242a3585e3390ff5f33.zip'
-- SHA1 hash of
~/onnxruntime-1.22.0/build/Linux/Release/_deps/eigen3-subbuild/eigen3-populate-prefix/src/eigen-1d8b82b0740839c0de7f1242a3585e3390ff5f33.zip
does not match expected value
expected: '5ea4d05e62d7f954a46b3213f9b2535bdd866803'
actual: '51982be81bbe52572b54180454df11a3ece9a934'
-- Hash mismatch, removing...
CMake Error at eigen3-subbuild/eigen3-populate-prefix/src/eigen3-populate-stamp/download-eigen3-populate.cmake:163 (message):
Each download failed!
原因:onnxruntime-1.22.0源码包的一个bug,在cmake/deps.txt文件中下载依赖文件eigen-1d8b82b0740839c0de7f1242a3585e3390ff5f33.zip对应的hash值错误导致。
解决办法:执行“sed -i 's/5ea4d05e62d7f954a46b3213f9b2535bdd866803/51982be81bbe52572b54180454df11a3ece9a934/g' cmake/deps.txt”,修正错误的hash值即可。另外,最新onnxruntime主线上的cmake/deps.txt文件已经修正过来了。
-- Loading Dependencies URLs ...
-- Loading Dependencies ...
[ 11%] Creating directories for 'abseil_cpp-populate'
[ 22%] Performing download step (download, verify and extract) for 'abseil_cpp-populate'
-- Downloading...
dst='~/onnxruntime-1.22.0/build/Linux/Release/_deps/abseil_cpp-subbuild/abseil_cpp-populate-prefix/src/20240722.0.zip'
timeout='none'
inactivity timeout='none'
-- Using src='https://github.com/abseil/abseil-cpp/archive/refs/tags/20240722.0.zip'
CMake Error at abseil_cpp-subbuild/abseil_cpp-populate-prefix/src/abseil_cpp-populate-stamp/download-abseil_cpp-populate.cmake:163 (message):
Each download failed!error: downloading 'https://github.com/abseil/abseil-cpp/archive/refs/tags/20240722.0.zip' failed
原因:国内访问github.com网络情况不好,时断时续,下载速度也奇慢,容易导致超时失败。
解决办法:最好/VPN代理,或者耐心等待网络情况变好的时间。
[ 14%] Built target gen_onnx_operators_proto
[ 14%] Building CXX object _deps/onnx-build/CMakeFiles/onnx_proto.dir/onnx/onnx-ml.pb.cc.o
In file included from~/onnxruntime-1.22.0/build/Linux/Release/_deps/onnx-build/onnx/onnx-ml.pb.cc:4:
~/onnxruntime-1.22.0/build/Linux/Release/_deps/onnx-build/onnx/onnx-ml.pb.h:17:2: error: #error This file was generated by an older version of protoc which is
17 | #error This file was generated by an older version of protoc which is
| ^~~~~
~/onnxruntime-1.22.0/build/Linux/Release/_deps/onnx-build/onnx/onnx-ml.pb.h:18:2: error: #error incompatible with your Protocol Buffer headers. Please
18 | #error incompatible with your Protocol Buffer headers. Please
| ^~~~~
~/onnxruntime-1.22.0/build/Linux/Release/_deps/onnx-build/onnx/onnx-ml.pb.h:19:2: error: #error regenerate this file with a newer version of protoc.
19 | #error regenerate this file with a newer version of protoc.
| ^~~~~
原因:本地安装的 protobuf库版本高于onnx-ml.pb.h头文件生成时所依赖的protobuf版本,文件要求 protoc 的版本在 3.21.0(3021000)和 3.21.12(3021012)之间,但当前环境中的 protoc 版本不符合这一要求。
#include
#if PROTOBUF_VERSION < 3021000
#error This file was generated by a newer version of protoc which is
#error incompatible with your Protocol Buffer headers. Please update
#error your headers.
#endif
#if 3021012 < PROTOBUF_MIN_PROTOC_VERSION
#error This file was generated by an older version of protoc which is
#error incompatible with your Protocol Buffer headers. Please
#error regenerate this file with a newer version of protoc.
#endif
解决办法:
这个问题比较费解,笔者最初编译碰到这个问题的是一台配置相当比较复杂的ubuntu22.04.5的系统(安装的软件比较多),卸载高版本的protobuf后,始终无法成功安装到3.21.x的版本,直接编译也还是同样错误告警(像是卸载不干净);最后更换了另外一台新的openEuler 22.03系统,该系统没有安装过protobuf,直接编译就可以通过了。
实际上,后来查看cmake/deps.txt(依赖下载描述文件,如下),onnxruntime实际依赖于编译过程中自动下载的protobuf-v21.12源码版本(对应于PROTOBUF_VERSION=3.21.12 ?),而不是依赖于本地安装的protobuf,只是二者不能冲突而已。
protobuf;https://github.com/protocolbuffers/protobuf/archive/refs/tags/v21.12.zip;7cf2733949036c7d52fda017badcab093fe73bfa
... ...(省略无关)
protoc_linux_aarch64;https://github.com/protocolbuffers/protobuf/releases/download/v21.12/protoc-21.12-linux-aarch_64.zip;df9d45470b0b8cf939dd2f0ec6b88e9cafc4d617