onnxruntime/rust/BUILD.md
Boyd Johnson 96b95a24ee
Add rust bindings (#12606)
This adds updated Rust bindings that have been located at
[nbigaouette/onnxruntime-rs](https://github.com/nbigaouette/onnxruntime-rs).

check out the build instructions included in this PR at /rust/BUILD.md.

Changes to the bindings included in this PR:
- The bindings are generated with the build script on each build
- The onnxruntime shared library is built with ORT_RUST_STRATEGY=compile
which is now the default.
- A memory leak was fixed where a call to free wasn't called
- Several small memory errors were fixed
- Session is Send but not Sync, Environment is Send + Sync
- Inputs and Outputs can be ndarray::Arrays of many different types.

Some commits can be squashed, if wanted, but were left unsquashed to
show differences between old bindings and new bindings.

This PR does not cover packaging nor does it include the Rust bindings
withing the build system.

For those of you who have previous Rust code based on the bindings,
these new bindings
can be used as a `path` dependency or a `git` dependency (though I have
not tested this out).

The work addressed in this PR was discussed in #11992
2023-02-08 14:57:15 -08:00

1.8 KiB

Building and testing the Rust bindings

These instructions require cargo and rustc. To get these follow the instructions at https://rustup.rs The instructions compile the onnxruntime along with the bindings, so require cmake, a python 3 interpreter, clang (needed to parse the C headers to generate the Rust bindings), and the platform compiler to compile onnxruntime.

Local setup of onnxruntime repo

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

cargo build both crates

from the root of onnxruntime repo

    CARGO_TARGET_DIR=build/rust cargo build --manifest-path rust/Cargo.toml

The CARGO_TARGET_DIR environment variable puts the build artifacts in onnxruntime/build/rust instead of onnxruntime/rust/target.

cargo test both crates

    CARGO_TARGET_DIR=build/rust cargo test --manifest-path rust/Cargo.toml --features model-fetching

cargo test both crates while specifying the absolute path to the OnnxRuntime shared library.

    RUST_ONNXRUNTIME_LIBRARY_PATH=<absolute path to shared library/libonnxruntime.so> CARGO_TARGET_DIR=build/rust cargo test --manifest-path rust/Cargo.toml --features model-fetching

cargo test with sanitizer support

If you are using a nightly Rust compiler and are on one the platforms listed in Rust sanitizer support.

where $SAN is one of address, thread, memory or leak

    RUSTFLAGS="-Zsanitizer=$SAN" CARGO_TARGET_DIR=build/rust cargo test --manifest-path rust/Cargo.toml --features model-fetching --target <your target for example x86_64-unknown-linux-gnu> -Z build-std -- --test-threads=1