ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

Rust 项目如何配置 ONNX Runtime 库获取策略(ORT_RUST_STRATEGY 的 download、system 与 compile)

2026/9/14 20:26:42 拓冰建站 浏览量
Rust 项目如何配置 ONNX Runtime 库获取策略(ORT_RUST_STRATEGY 的 download、system 与 compile) Rust 项目如何配置 ONNX Runtime 库获取策略ORT_RUST_STRATEGY 的 download、system 与 compile【免费下载链接】onnxruntimeONNX Runtime: cross-platform, high performance ML inferencing and training accelerator项目地址: https://gitcode.com/GitHub_Trending/on/onnxruntime在 Rust 项目中集成 ONNX Runtime 时最先要解决的问题是构建阶段去哪里找 ONNX Runtime 动态库onnxruntime-rs的构建脚本 build.rs 会在cargo build时根据环境变量ORT_RUST_STRATEGY选择不同的库获取策略——download从上游下载预编译包、system指向本机已安装的版本、compile从源码编译。本文基于 rust/README.md 和 rust/BUILD.md 说明三种策略的配置方法、适用条件以及如何用示例程序验证配置是否生效。onnxruntime-rs 由两个 crate 组成见 rust/Cargo.toml 工作区定义onnxruntime-sysC API 的低层绑定onnxruntime高层安全 API。库的获取逻辑全部在onnxruntime-sys的构建脚本中执行两种策略之外的细节支持的平台、报错信息也都能从该文件确认。三种策略各自解决什么问题prepare_libort_dir()会读取ORT_RUST_STRATEGY取值与行为对应如下取值行为配套环境变量download下载预编译的 onnxruntime 包并解压到构建目录target下的onnxruntime子目录ORT_RUST_USE_CUDA1可下载 CUDA 版本system直接使用本机已安装的版本ORT_RUST_LIB_LOCATION指向安装路径compile用 cmake 从 vendored 源码编译出共享库onnxruntime_BUILD_SHARED_LIBONCUDA 时追加onnxruntime_USE_CUDAON无关于默认值文档内部存在一处不一致rust/README.md 写明 “compile: To compile the library. This is the default.”而 build.rs 中未设置ORT_RUST_STRATEGYErr(_)分支时实际走的是prepare_libort_dir_compiled()的注释虽写着 download 是默认但Ok(download)分支才是显式 download 的入口——两处表述冲突本文不替文档统一口径。建议显式设置ORT_RUST_STRATEGY不依赖默认行为。准备条件Rust 工具链cargo、rustc获取方式按 rust/BUILD.md 指引到 rustup.rscompile策略额外需要cmake、Python 3 解释器、clang用于解析 C 头文件生成绑定、平台编译器system策略需要本机已有 onnxruntime 安装并知道其安装路径如果要在 onnxruntime 仓库内部构建 Rust 绑定如运行集成测试需要完整克隆仓库并初始化子模块git clone https://github.com/microsoft/onnxruntime cd onnxruntime git submodule update --init --recursive策略一download预编译包设置环境变量后构建即可构建脚本会按目标平台拼接预编译包文件名并下载、解压ORT_RUST_STRATEGYdownload cargo build --manifest-path rust/Cargo.toml构建脚本能识别的平台组合超出范围会直接 panic 并提示改用system策略CPULinux x86_64、macOS x86_64、macOS aarch64、Windows i686、Windows x86_64rust/README.md 给出的支持列表GPUCUDALinux x86_64、Windows x86_64需额外设置ORT_RUST_USE_CUDA1仅支持 Linux 或 WindowsORT_RUST_STRATEGYdownload ORT_RUST_USE_CUDA1 cargo build --manifest-path rust/Cargo.toml下载的包名由三元组OS、架构、加速器和版本号拼成版本号读取自 rust/onnxruntime-sys/vendor/onnxruntime-src/VERSION_NUMBER 中include_str!的文件因此 vendored 目录需要先就位见 compile 策略 一节。不支持的平台组合会报出类似Unsupported prebuilt triplet: ...的 panic提示Please use ORT_RUST_STRATEGYsystem and ORT_RUST_LIB_LOCATION/path/to/onnxruntime。注意download策略需要访问网络构建脚本用 ureq 发起下载超时 300 秒离线环境应改用system或compile。策略二system本机已安装的库ORT_RUST_LIB_LOCATION指向 onnxruntime 安装目录脚本取该目录下的include与lib子目录用于生成绑定和链接ORT_RUST_STRATEGYsystem ORT_RUST_LIB_LOCATION/path/to/onnxruntime cargo build --manifest-path rust/Cargo.toml其中/path/to/onnxruntime需替换为你本机 onnxruntime 安装的实际路径。rust/README.md 特别提示system策略下运行构建出的二进制例如测试时如果库不在系统库路径中至少 macOS 上会加载失败报错形如文档示例dyld: Library not loaded: rpath/libonnxruntime.1.7.1.dylib Referenced from: onnxruntime-rs.git/target/debug/deps/onnxruntime_sys-22eb0e3e89a0278c Reason: image not found文档给出两种修复方式任选其一设置LD_LIBRARY_PATH指向库所在路径在项目.cargo/config中加入 linker flag提供完整路径文档示例/full/path/to/onnxruntime/lib需替换为你本机库路径[target.aarch64-apple-darwin] rustflags [-C, link-args-Wl,-rpath,/full/path/to/onnxruntime/lib]策略三compile从源码编译该策略通过 cmake 构建 vendored 的 onnxruntime 源码onnxruntime-sys/vendor/onnxruntime-src并开启onnxruntime_BUILD_SHARED_LIB设置ORT_RUST_USE_CUDA为1/yes/true/on之一时追加onnxruntime_USE_CUDAON。前提是把 onnxruntime 仓库内容复制进 vendor 目录。rust/justfile 的vendor配方在rust/目录下执行just vendor会执行mkdir -p ./onnxruntime-sys/vendor/onnxruntime-src然后拷贝../onnxruntime、../cmake、../include、../tools/ci_build、../samples、../requirements.txt.in、../VERSION_NUMBER到 vendor 目录并删除vendor/onnxruntime-src/cmake/external/onnx。该配方会把仓库根目录的文件复制到rust/onnxruntime-sys/vendor/下不修改这些源文件本身。之后正常构建ORT_RUST_STRATEGYcompile cargo build --manifest-path rust/Cargo.toml由于会完整编译 onnxruntime 本体所需依赖见上文“准备条件”cmake、Python 3、clang、平台编译器耗时也最长。验证配置是否生效配置完成后文档给出的验证路径是运行示例程序或集成测试。方式一运行示例rust/README.md Example 一节先下载示例模型 SqueezeNet 1.0ONNX version 1.3, Opset 8curl -LO https://github.com/onnx/models/raw/main/vision/classification/squeezenet/model/squeezenet1.0-8.onnx再运行高层 API 示例 rust/onnxruntime/examples/sample.rscargo run --example sample该示例加载本地squeezenet1.0-8.onnx对输入/输出形状做了断言输入[1, 3, 224, 224]输出[1, 1000, 1, 1]成功时打印各类别的 ScoreREADME 中的完整输出为文档示例数值随运行时环境变化不应作为固定预期。低层 crate 对应示例是c_api_samplecargo run --example c_api_sample方式二仓库内构建并跑测试rust/BUILD.md在 onnxruntime 仓库根目录执行CARGO_TARGET_DIRbuild/rust cargo build --manifest-path rust/Cargo.tomlCARGO_TARGET_DIR只是把产物放到onnxruntime/build/rust而不是rust/target。测试命令CARGO_TARGET_DIRbuild/rust cargo test --manifest-path rust/Cargo.toml --features model-fetching也可以显式指定共享库绝对路径运行时由 rust/onnxruntime/tests/integration_tests.rs 通过RUST_ONNXRUNTIME_LIBRARY_PATH加载示例程序同理RUST_ONNXRUNTIME_LIBRARY_PATHabsolute path to shared library/libonnxruntime.so CARGO_TARGET_DIRbuild/rust cargo test --manifest-path rust/Cargo.toml --features model-fetching其中absolute path to shared library/libonnxruntime.so需替换为共享库的实际绝对路径。集成测试会下载模型、执行推理并校验结果model-fetchingfeature 用于从 ONNX Model Zoo 拉取模型见 rust/onnxruntime/Cargo.toml。限制与注意事项rust/README.md 明确标注这是实验性、进行中的工作not complete/working/safe基础推理可用但 ONNX Runtime 很多控制推理过程的选项尚未暴露download策略的平台支持范围有限超出的平台组合构建会 panic 并建议改走systemsystem策略在 macOS 上运行产物时可能出现上文 dyld 报错需按文档设置LD_LIBRARY_PATH或 rpath构建脚本对ORT_RUST_STRATEGY做了rerun-if-env-changed注册切换策略后需要重新构建才会生效默认值在 README 与 build.rs 注释间不一致README 称compile为默认build.rs 注释称download为默认请显式设置环境变量。如果你的目标平台不在预编译支持列表内或者仓库内的 Rust 绑定尚不满足需求例如缺少所需的推理选项可以先用system策略指向自行安装的 onnxruntime 版本验证 API 行为再决定是否提交 issue 或自行扩展绑定。【免费下载链接】onnxruntimeONNX Runtime: cross-platform, high performance ML inferencing and training accelerator项目地址: https://gitcode.com/GitHub_Trending/on/onnxruntime创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考