Ubuntu下的onnxruntime(c++)编译

仓库下载

git clone --depth=1 --branch v1.12.1 https://github.com.cnpmjs.org/microsoft/onnxruntime.git

注意:需要更换国内镜像源

编译

GPU

./build.sh --skip_tests --use_cuda --config Release --build_shared_lib --parallel --cuda_home /usr/local/cuda-11.3 --cudnn_home /usr/local/cuda-11.3

其中的use_cuda表示你要使用CUDA的onnxruntime,cuda_home和cudnn_home均指向你的CUDA安装目录即可。

onnxruntime版本和cuda、cudnn版本要对应,具体参考官方推荐https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html

TensorRT

Tensorrt安装参考这里

./build.sh \
	--parallel 8 \
	--use_cuda \
	--cuda_version=11.1 \
	--cuda_home=/usr/local/cuda \
	--cudnn_home=/usr/local/cuda \
	--use_tensorrt --tensorrt_home=/usr/local/lib/python3.8/dist-packages/tensorrt \
	--build_shared_lib --enable_pybind \
	--build_wheel --update --build \
	--config Release

注意

  1. cmake版本过低会导致编译失败;
  2. 科学上网问题。编译过程中会链接其他github仓库,需要手动添加镜像源。
cd ${your git repo root}
cd .git
vim config

修改为:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = https://github.com.cnpmjs.org/microsoft/onnxruntime.git
    fetch = +refs/tags/v1.7.0:refs/tags/v1.7.0

也不多,大概几十个把

编译成功

[ 98%] Building CXX object CMakeFiles/onnxruntime_providers.dir/Users/xxx/Desktop/third_party/library/onnxruntime/onnxruntime/contrib_ops/cpu/skip_layer_norm.cc.o
[ 98%] Building CXX object CMakeFiles/onnxruntime_providers.dir/Users/xxx/Desktop/third_party/library/onnxruntime/onnxruntime/contrib_ops/cpu/tokenizer.cc.o
[100%] Building CXX object CMakeFiles/onnxruntime_providers.dir/Users/xxx/Desktop/third_party/library/onnxruntime/onnxruntime/contrib_ops/cpu/trilu.cc.o
[100%] Building CXX object CMakeFiles/onnxruntime_providers.dir/Users/xxx/Desktop/third_party/library/onnxruntime/onnxruntime/contrib_ops/cpu/unique.cc.o
[100%] Building CXX object CMakeFiles/onnxruntime_providers.dir/Users/xxx/Desktop/third_party/library/onnxruntime/onnxruntime/contrib_ops/cpu/word_conv_embedding.cc.o
[100%] Linking CXX static library libonnxruntime_providers.a
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: libonnxruntime_providers.a(dft.cc.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: libonnxruntime_providers.a(window_functions.cc.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: libonnxruntime_providers.a(dft.cc.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: libonnxruntime_providers.a(window_functions.cc.o) has no symbols
[100%] Built target onnxruntime_providers
[100%] Building C object CMakeFiles/onnxruntime.dir/generated_source.c.o
[100%] Linking CXX shared library libonnxruntime.dylib
[100%] Built target onnxruntime
2021-04-15 22:39:10,495 util.run [DEBUG] - Subprocess completed. Return code: 0
2021-04-15 22:39:10,496 build [INFO] - Build complete

install

cd ./build/Linux/release
make install

在这里插入图片描述

参考

https://zhuanlan.zhihu.com/p/411887386

### 安装 ONNX Runtime C++ 版本 为了在 Ubuntu 20.04 上成功安装并配置 ONNX Runtime 的 C++ 接口,需遵循一系列特定步骤来确保所有依赖项都已正确设置。 #### 准备工作环境 首先,更新系统的包列表,并安装 Python3 及其必要的工具: ```bash sudo apt-get update && sudo apt-get upgrade -y sudo apt-get install python3 python3-pip -y ``` 接着升级 `pip` 工具以便后续操作更加顺畅: ```bash pip3 install --upgrade pip ``` #### 安装构建所需的基础库 为支持 ONNX Runtime 构建过程中的需求,还需额外安装一些基础软件包: ```bash sudo apt-get install cmake build-essential pkg-config libprotobuf-dev protobuf-compiler ninja-build -y ``` 这些命令将安装编译器、链接器以及其他必需的支持文件[^3]。 #### 获取源码并编译 ONNX Runtime 通过 Git 克隆仓库至本地目录下: ```bash git clone --recursive https://github.com/microsoft/onnxruntime.git cd onnxruntime ``` 执行构建脚本来完成 ONNX Runtime 库的编译。这里选择了带有调试信息但接近发布质量级别的优化选项 (`RelWithDebInfo`) 和共享库形式(`--build_shared_lib`) 来满足大多数应用场景的需求: ```bash ./build.sh --config RelWithDebInfo --use_cuda --cuda_version=11.6 --cudnn_home=/usr/lib/x86_64-linux-gnu/ --build_shared_lib --parallel ``` 注意,在此命令中加入了 CUDA 支持参数以匹配指定版本的要求[^2]。 #### 验证安装成果 一旦上述流程顺利完成,则可以在项目根目录下的 `build/Linux/RelWithDebInfo/bin` 文件夹内找到生成的目标文件以及动态链接库(.so),这标志着 ONNX Runtime 成功被部署到了当前环境中。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

点PY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值