云计算百科
云计算领域专业知识百科平台

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

服务器信息:

aarch64架构 ubuntu20.04 nvidia T4卡

编译onnxruntime-gpu前置条件:

  • 已经安装合适的cuda
  • 已经安装合适的cudnn
  • 已经安装合适的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 main
    build_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_targets
    run_subprocess(cmd_args, env=env)
    File "/home/tc/onnxruntime/tools/ci_build/build.py", line 798, in run_subprocess
    return 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 run
    completed_process = subprocess.run(
    File "/usr/lib/python3.8/subprocess.py", line 516, in run
    raise 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' failed
    status_code: 28
    status_string: "Timeout was reached"
    log:
    — LOG BEGIN —
    Trying 20.205.243.166:443…

    connect to 20.205.243.166 port 443 failed: Connection timed out

    Failed to connect to github.com port 443 after 131336 ms: Couldn't connect
    to server

    Closing 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 main
    generate_build_tree(
    File "/home/tc/onnxruntime/tools/ci_build/build.py", line 1674, in generate_build_tree
    run_subprocess(
    File "/home/tc/onnxruntime/tools/ci_build/build.py", line 867, in run_subprocess
    return 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 run
    completed_process = subprocess.run(
    File "/usr/lib/python3.8/subprocess.py", line 516, in run
    raise 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。

  • 首先,添加包含较新 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 。

  • 安装所需版本的 GCC 和 G++
  • sudo apt install gcc-12 g++-12

  • 使用 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

  • 如果系统中安装了多个版本的 GCC 和 G++,可以使用以下命令手动选择默认版本:
  • sudo update-alternatives –config gcc
    sudo update-alternatives –config g++

  • 验证当前版本
  • gcc –version
    g++ –version

  • 重新编译
  • 解决方式二:修改源码绕过 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 即可继续编译。

    赞(0)
    未经允许不得转载:网硕互联帮助中心 » 【Linux】在Arm服务器源码编译onnxruntime-gpu的whl
    分享到: 更多 (0)

    评论 抢沙发

    评论前必须登录!