当前位置: 首页 > news >正文

【Linux】在Arm服务器源码编译onnxruntime-gpu的whl

服务器信息:

aarch64架构
ubuntu20.04
nvidia T4卡

编译onnxruntime-gpu前置条件:

  1. 已经安装合适的cuda
  2. 已经安装合适的cudnn
  3. 已经安装合适的cmake

源码编译onnxruntime-gpu的步骤

1. 下载源码

git clone --recursive https://github.com/microsoft/onnxruntime.git
cd onnxruntime

2. 选择版本

然后根据需要安装的onnxruntime-gpu版本号,切换版本

git checkout v1.16.3

3. 执行bulid指令

在onnxruntime根目录下执行:

./build.sh \--config Release \--update \--build \--parallel \--build_wheel \--use_cuda \--allow_running_as_root \--cuda_home /usr/local/cuda \--cudnn_home /usr/lib/aarch64-linux-gnu \--skip_tests \--cmake_extra_defines \CMAKE_CUDA_ARCHITECTURES=75 \onnxruntime_ENABLE_NVTX_PROFILE=ON \onnxruntime_USE_MEMORY_EFFICIENT_ATTENTION=OFF \onnxruntime_USE_FLASH_ATTENTION=OFF \onnxruntime_BUILD_UNIT_TESTS=OFF \CMAKE_POLICY_VERSION_MINIMUM=3.5

4. 报错解决方式

4.1 算力设置不匹配
[ 53%] Built target onnxruntime_optimizer
make: *** [Makefile:166: all] Error 2
Traceback (most recent call last):File "/home/tc/onnxruntime/tools/ci_build/build.py", line 2684, in <module>sys.exit(main())File "/home/tc/onnxruntime/tools/ci_build/build.py", line 2577, in mainbuild_targets(args, cmake_path, build_dir, configs, num_parallel_jobs, args.target)File "/home/tc/onnxruntime/tools/ci_build/build.py", line 1487, in build_targetsrun_subprocess(cmd_args, env=env)File "/home/tc/onnxruntime/tools/ci_build/build.py", line 798, in run_subprocessreturn run(*args, cwd=cwd, capture_stdout=capture_stdout, shell=shell, env=my_env)File "/home/tc/onnxruntime/tools/python/util/run.py", line 49, in runcompleted_process = subprocess.run(File "/usr/lib/python3.8/subprocess.py", line 516, in runraise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['/home/tc/cmake-3.26.0-linux-aarch64/bin/cmake', '--build', '/home/tc/onnxruntime/build/Linux/Release', '--config', 'Release', '--', '-j40']' returned non-zero exit status 2.

问题原因:
./bulid.sh的参数CMAKE_CUDA_ARCHITECTURES=87 表示目标 GPU 的计算能力为 8.7,请确认您的硬件是否匹配:
可以直接用AI来查询,T4需要填写75,解决此问题

4.2 下载库文件超时
-- Using src='https://github.com/pytorch/cpuinfo/archive/ca678952a9a8eaa6de112d154e8e104b22f9ab3f.zip'
CMake Error at pytorch_cpuinfo-subbuild/pytorch_cpuinfo-populate-prefix/src/pytorch_cpuinfo-populate-stamp/download-pytorch_cpuinfo-populate.cmake:170 (message):Each download failed!error: downloading 'https://github.com/pytorch/cpuinfo/archive/ca678952a9a8eaa6de112d154e8e104b22f9ab3f.zip' failedstatus_code: 28status_string: "Timeout was reached"log:--- LOG BEGIN ---Trying 20.205.243.166:443...connect to 20.205.243.166 port 443 failed: Connection timed outFailed to connect to github.com port 443 after 131336 ms: Couldn't connectto serverClosing connection 0--- LOG END ---

问题原因:
下载库文件速度过慢超时
解决方案
手动下载,并放到指定位置,再重新执行编译指令。
以上述报错为例:
网页下载资源,浏览器输入地址自动下载压缩包:

https://github.com/pytorch/cpuinfo/archive/ca678952a9a8eaa6de112d154e8e104b22f9ab3f.zip

将下载的 cpuinfo-ca678952a9a8eaa6de112d154e8e104b22f9ab3f.zip,解压到相对路径onnxruntime/build/Linux/Release/_deps/pytorch_cpuinfo-subbuild/pytorch_cpuinfo-populate-prefix/src/ 目录下。(报错里会写明需要放置的位置,根据实际情况修改路径)

然后重新执行bulid.sh的指令就可以继续编译。

4.3 不支持BFLOAT16
NVCC_ERROR = nvcc fatal   : Unknown option '-Wstrict-aliasing'NVCC_OUT = 1
CMake Error at CMakeLists.txt:695 (message):The compiler doesn't support BFLOAT16!!!-- Configuring incomplete, errors occurred!
Traceback (most recent call last):File "/home/tc/onnxruntime/tools/ci_build/build.py", line 2998, in <module>sys.exit(main())File "/home/tc/onnxruntime/tools/ci_build/build.py", line 2853, in maingenerate_build_tree(File "/home/tc/onnxruntime/tools/ci_build/build.py", line 1674, in generate_build_treerun_subprocess(File "/home/tc/onnxruntime/tools/ci_build/build.py", line 867, in run_subprocessreturn run(*args, cwd=cwd, capture_stdout=capture_stdout, shell=shell, env=my_env)File "/home/tc/onnxruntime/tools/python/util/run.py", line 49, in runcompleted_process = subprocess.run(File "/usr/lib/python3.8/subprocess.py", line 516, in runraise CalledProcessError(retcode, process.args,

问题原因:
ONNX Runtime 从 v1.17.0 起要求 ARM 架构支持 BFLOAT16 指令集,当前编译器版本不支持。
解决方式一:升级编译器 / 系统环境
官方文档和讨论指出:在 JetPack 5.x (Ubuntu 18.04/20.04) 上编译时,GCC 必须 ≥10 (JetPack 6 对应 Ubuntu 22.04,自带 GCC11+)。也就是说,最简单的解决方案是升级到 JetPack 6 (Ubuntu 22.04/GCC11) 或手动安装较新版本的 GCC(如通过 sudo apt install gcc-11 g+±11 并更新 alternatives)。升级要求:Ubuntu 22.04,不然可能无法直接安装gcc-12 和 g+±12 。
升级后,-march=armv8.2-a+bf16 检查就会通过。表明需要更高版本的编译器以支持 BF16。

  1. 首先,添加包含较新 GCC 版本的 PPA:
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update

此 PPA 提供了多个版本的 GCC 和 G++,包括 gcc-12 和 g+±12 。

  1. 安装所需版本的 GCC 和 G++
sudo apt install gcc-12 g++-12
  1. 使用 update-alternatives 设置默认版本
sudo update-alternatives --install /usr/bin/gcc  gcc  /usr/bin/gcc-12 120
sudo update-alternatives --install /usr/bin/g++  g++  /usr/bin/g++-12 120
  1. 如果系统中安装了多个版本的 GCC 和 G++,可以使用以下命令手动选择默认版本:
sudo update-alternatives --config gcc
sudo update-alternatives --config g++
  1. 验证当前版本
gcc --version
g++ --version
  1. 重新编译

解决方式二:修改源码绕过 BFLOAT16 检查
若必须在现有环境下编译,可以手动修改源码跳过 BFLOAT16 检查。在 onnxruntime/cmake/CMakeLists.txt 中找到如下检查段:

check_cxx_compiler_flag(-march=armv8.2-a+bf16 HAS_ARM64_BFLOAT16)
if(NOT HAS_ARM64_BFLOAT16)message(FATAL_ERROR "The compiler doesn't support BFLOAT16!!!")
endif()

修改为:

if(NOT HAS_ARM64_BFLOAT16)#message(WARNING "BFLOAT16 not supported, disabling BF16 optimizations")set(HAS_ARM64_BFLOAT16 TRUE)
endif()

这样跳过了编译器不支持 BF16 的致命错误。不过需要注意:跳过检查后仍可能缺少 BF16 优化代码,性能或功能可能受影响。修改后保存并重新运行 CMake 即可继续编译。

相关文章:

  • LeRobot 项目部署运行逻辑(七)—— ACT 在 Mobile ALOHA 训练与部署
  • 系统架构-嵌入式系统架构
  • python-75-Nacos技术之Python+Nacos实现微服务架构
  • LInux系统文件与目录管理(二)
  • 风电功率预测方法与准确性提升方案详解
  • node .js 启动基于express框架的后端服务报错解决
  • Spark,RDD中的转换算子
  • 《Vue.js》阅读之响应式数据与副作用函数
  • Html5新特性_js 给元素自定义属性_json 详解_浅克隆与深克隆
  • 动态会话日志记录 ngx_stream_log_module
  • 介电测试的基本原理与方法及应用领域
  • 摆脱拖延症的详细计划示例
  • C——五子棋小游戏
  • 坐标系概述
  • 湖北理元理律师事务所:企业债务危机的“止血”与“造血”平衡术
  • spark的处理过程-转换算子和行动算子
  • 视频编码原理讲解一:VCL层和NAL层的讲解
  • 在文档里如何引用在线SVG甘特图
  • 17.three官方示例+编辑器+AI快速学习webgl_buffergeometry_lines
  • NVIDIA Quantum-2 QM9700系列利用400G infinniband扩展数据中心智能交换机 NVIDIA Quantum-2 InfiniBand 交换机系列
  • 铁路部门:确保沿线群众安全,焦柳铁路6个区段将陆续安装防护栅栏
  • 季后赛主场优势消失之谜,这事竟然要赖库里
  • 行知读书会|换一个角度看见社会
  • 让“五颜六色”面孔讲述上海故事,2025年上海城市推荐官开启选拔
  • 眉山“笑气”迷局:草莓熊瓶背后的隐秘与危机
  • 三大交易所多举措支持科创债再扩容,约160亿证券公司科创债有望近期落地