ort - ONNX Runtime Rust bindings

Coverage Results GitHub Workflow Status

ort is yet another ONNX Runtime wrapper for Rust based on onnxruntime-rs. ort is updated for ONNX Runtime 1.13.1 and contains many API improvements & fixes.

Cargo features

Execution providers

To use other execution providers, you must explicitly enable them via their Cargo features. Using the compile strategy, everything should just work™️. Using the system strategy, ensure that the binaries you are linking to have been built with the execution providers you want to use, otherwise you'll get linking errors. After that, configuring & enabling these execution providers can be done through SessionBuilder::execution_providers().

Requesting an execution provider via e.g. ExecutionProviderBuilder::cuda() will silently fail if that EP is not available on the system or encounters an error and falls back to the next requested execution provider or to the CPU provider if no requested providers are available. If you must know why the execution provider is unavailable, use ExecutionProviderBuilder::try_*(), e.g. try_cuda().

For prebuilt Microsoft binaries, you can enable the CUDA or TensorRT execution providers for Windows and Linux via the cuda and tensorrt Cargo features respectively. No other execution providers are supported in these binaries and enabling other features will fail. To use other execution providers, you must build ONNX Runtime yourself to be able to use them.

Shared library hell

Because compiling ONNX Runtime from source takes so long (and static linking is not recommended by Microsoft), it may be easier to compile ONNX Runtime as a shared library or use prebuilt DLLs. However, this can cause some issues with library paths and load orders.

Windows

Some versions of Windows come bundled with onnxruntime.dll in the System32 folder. On Windows 11 build 22598.1, onnxruntime.dll is on version 1.10.0, while ort requires 1.13.1, so execution will fail because system DLLs take precedence. Luckily though, DLLs in the same folder as the application have higher priority; ort can automatically copy the DLLs to the Cargo target folder when the copy-dylibs feature is enabled.

Note that, when running tests/benchmarks for the first time, you'll have to manually copy the target/debug/onnxruntime*.dll files to target/debug/deps/, or target/debug/examples/ for examples. It should Just Work™️ when building/running a binary, however.

Linux

You'll either have to copy libonnxruntime.so to a known lib location (e.g. /usr/lib) or enable rpath if you have the copy-dylibs feature enabled.

In Cargo.toml: ```toml [profile.dev] rpath = true

[profile.release] rpath = true

do this for all profiles

```

In .cargo/config.toml: ```toml [target.x86_64-unknown-linux-gnu] rustflags = [ "-Clink-args=-Wl,-rpath,\$ORIGIN" ]

do this for all Linux targets as well

```

macOS

macOS follows the same procedure as Linux, except the rpath should point to @loader_path rather than $ORIGIN:

```toml

.cargo/config.toml

[target.x8664-apple-darwin] rustflags = [ "-Clink-args=-Wl,-rpath,@loaderpath" ] ```