Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Onnxruntime v1.8.1 with GPU/CUDA 11 support #87

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions .github/workflows/general.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,17 @@ jobs:
# ******************************************************************
- name: Download prebuilt archive (GPU, x86_64-unknown-linux-gnu)
uses: actions-rs/cargo@v1
env:
ORT_USE_CUDA: "yes"
with:
command: build
args: --target x86_64-unknown-linux-gnu
args: --target x86_64-unknown-linux-gnu --features cuda
- name: Verify prebuilt archive downloaded (GPU, x86_64-unknown-linux-gnu)
run: ls -lh target/x86_64-unknown-linux-gnu/debug/build/onnxruntime-sys-*/out/onnxruntime-linux-x64-gpu-1.*.tgz
# ******************************************************************
- name: Download prebuilt archive (GPU, x86_64-pc-windows-msvc)
uses: actions-rs/cargo@v1
env:
ORT_USE_CUDA: "yes"
with:
command: build
args: --target x86_64-pc-windows-msvc
args: --target x86_64-pc-windows-msvc --features cuda
- name: Verify prebuilt archive downloaded (GPU, x86_64-pc-windows-msvc)
run: ls -lh target/x86_64-pc-windows-msvc/debug/build/onnxruntime-sys-*/out/onnxruntime-win-gpu-x64-1.*.zip

Expand Down Expand Up @@ -141,7 +137,6 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: build
# Use --manifest-path instead of --package. See https://github.com/actions-rs/cargo/issues/86
args: --manifest-path onnxruntime/Cargo.toml --features model-fetching
- name: Test onnxruntime-sys
uses: actions-rs/cargo@v1
Expand Down
54 changes: 32 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ This is an attempt at a Rust wrapper for

This project consist on two crates:

* [`onnxruntime-sys`](onnxruntime-sys): Low-level binding to the C API;
* [`onnxruntime`](onnxruntime): High-level and safe API.
- [`onnxruntime-sys`](onnxruntime-sys): Low-level binding to the C API;
- [`onnxruntime`](onnxruntime): High-level and safe API.

[Changelog](CHANGELOG.md)

Expand All @@ -21,25 +21,25 @@ which provides the following targets:

CPU:

* Linux x86_64
* macOS x86_64
* macOS aarch64 (no pre-built binaries, no CI testing, see [#74](https://github.com/nbigaouette/onnxruntime-rs/pull/74))
* Windows i686
* Windows x86_64
- Linux x86_64
- macOS x86_64
- macOS aarch64 (no pre-built binaries, no CI testing, see [#74](https://github.com/nbigaouette/onnxruntime-rs/pull/74))
- Windows i686
- Windows x86_64

GPU:

* Linux x86_64
* Windows x86_64
- Linux x86_64
- Windows x86_64

---

**WARNING**:

* This is an experiment and work in progress; it is _not_ complete/working/safe. Help welcome!
* Basic inference works, see [`onnxruntime/examples/sample.rs`](onnxruntime/examples/sample.rs) or [`onnxruntime/tests/integration_tests.rs`](onnxruntime/tests/integration_tests.rs)
* ONNX Runtime has many options to control the inference process but those options are not yet exposed.
* This was developed and tested on macOS Catalina. Other platforms should work but have not been tested.
- This is an experiment and work in progress; it is _not_ complete/working/safe. Help welcome!
- Basic inference works, see [`onnxruntime/examples/sample.rs`](onnxruntime/examples/sample.rs) or [`onnxruntime/tests/integration_tests.rs`](onnxruntime/tests/integration_tests.rs)
- ONNX Runtime has many options to control the inference process but those options are not yet exposed.
- This was developed and tested on macOS Catalina. Other platforms should work but have not been tested.

---

Expand All @@ -58,11 +58,21 @@ To select which strategy to use, set the `ORT_STRATEGY` environment variable to:
3. `compile`: To compile the library

The `download` strategy supports downloading a version of ONNX that supports CUDA. To use this, set the
environment variable `ORT_USE_CUDA=1` (only supports Linux or Windows).
feature `cuda` in `Cargo.toml`.

Until the build script allow compilation of the runtime, see the [compilation notes](ONNX_Compilation_Notes.md)
for some details on the process.

### Note on using CUDA

To use CUDA you will need to set the feature `cuda` but also to set your session with the method `use_cuda` as such:

```
let mut session = environment
.new_session_builder()?
.use_cuda(0)?
```

### Note on 'ORT_STRATEGY=system'

When using `ORT_STRATEGY=system`, executing a built crate binary (for example the tests) might fail, at least on macOS,
Expand All @@ -76,9 +86,9 @@ dyld: Library not loaded: @rpath/libonnxruntime.1.7.1.dylib

To fix, one can either:

* Set the `LD_LIBRARY_PATH` environment variable to point to the path where the library can be found.
* Adapt the `.cargo/config` file to contain a linker flag to provide the **full** path:
- Set the `LD_LIBRARY_PATH` environment variable to point to the path where the library can be found.
- Adapt the `.cargo/config` file to contain a linker flag to provide the **full** path:

```toml
[target.aarch64-apple-darwin]
rustflags = ["-C", "link-args=-Wl,-rpath,/full/path/to/onnxruntime/lib"]
Expand Down Expand Up @@ -232,7 +242,7 @@ Generate the bindings:
```sh
❯ docker exec -it --user "$(id -u)":"$(id -g)" rustbuilder /bin/bash
❯ cd onnxruntime-sys
❯ cargo build --features generate-bindings
❯ cargo build --features 'generate-bindings, cuda'
```

### Generating Bindings for Windows With Vagrant
Expand All @@ -245,8 +255,8 @@ Windows can build both x86 and x86_64 bindings:
```sh
❯ rustup target add i686-pc-windows-msvc x86_64-pc-windows-msvc
❯ cd onnxruntime-sys
❯ cargo build --features generate-bindings --target i686-pc-windows-msvc
❯ cargo build --features generate-bindings --target x86_64-pc-windows-msvc
❯ cargo build --features 'generate-bindings, cuda' --target i686-pc-windows-msvc
❯ cargo build --features 'generate-bindings, cuda' --target x86_64-pc-windows-msvc
```

## Conduct
Expand All @@ -259,9 +269,9 @@ instead of the Rust moderation team.

This project is licensed under either of

* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or
- MIT license ([LICENSE-MIT](LICENSE-MIT) or
http://opensource.org/licenses/MIT)

at your option.
1 change: 1 addition & 0 deletions onnxruntime-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ default = []
disable-sys-build-script = []
# Use bindgen to generate bindings in build.rs
generate-bindings = ["bindgen"]
cuda = []

[package.metadata.docs.rs]
# Disable the build.rs on https://docs.rs since it can cause
Expand Down
22 changes: 7 additions & 15 deletions onnxruntime-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ const ORT_ENV_STRATEGY: &str = "ORT_STRATEGY";
/// Name of environment variable that, if present, contains the location of a pre-built library.
/// Only used if `ORT_STRATEGY=system`.
const ORT_ENV_SYSTEM_LIB_LOCATION: &str = "ORT_LIB_LOCATION";
/// Name of environment variable that, if present, controls wether to use CUDA or not.
const ORT_ENV_GPU: &str = "ORT_USE_CUDA";

/// Subdirectory (of the 'target' directory) into which to extract the prebuilt library.
const ORT_PREBUILT_EXTRACT_DIR: &str = "onnxruntime";
Expand All @@ -54,7 +52,6 @@ fn main() {
println!("cargo:rustc-link-search=native={}", lib_dir.display());

println!("cargo:rerun-if-env-changed={}", ORT_ENV_STRATEGY);
println!("cargo:rerun-if-env-changed={}", ORT_ENV_GPU);
println!("cargo:rerun-if-env-changed={}", ORT_ENV_SYSTEM_LIB_LOCATION);

generate_bindings(&include_dir);
Expand Down Expand Up @@ -280,17 +277,6 @@ enum Accelerator {
Gpu,
}

impl FromStr for Accelerator {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"1" | "yes" | "true" | "on" => Ok(Accelerator::Gpu),
_ => Ok(Accelerator::None),
}
}
}

impl OnnxPrebuiltArchive for Accelerator {
fn as_onnx_str(&self) -> Cow<str> {
match self {
Expand Down Expand Up @@ -353,6 +339,12 @@ impl OnnxPrebuiltArchive for Triplet {
}

fn prebuilt_archive_url() -> (PathBuf, String) {
let accelerator = if cfg!(feature = "cuda") {
Accelerator::Gpu
} else {
Accelerator::None
};

let triplet = Triplet {
os: env::var("CARGO_CFG_TARGET_OS")
.expect("Unable to get TARGET_OS")
Expand All @@ -362,7 +354,7 @@ fn prebuilt_archive_url() -> (PathBuf, String) {
.expect("Unable to get TARGET_ARCH")
.parse()
.unwrap(),
accelerator: env::var(ORT_ENV_GPU).unwrap_or_default().parse().unwrap(),
accelerator,
};

let prebuilt_archive = format!(
Expand Down
2 changes: 0 additions & 2 deletions onnxruntime-sys/examples/c_api_sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ fn main() {
};

// Optionally add more execution providers via session_options
// E.g. for CUDA include cuda_provider_factory.h and uncomment the following line:
// OrtSessionOptionsAppendExecutionProvider_CUDA(sessionOptions, 0);

//*************************************************************************
// create session and load model into memory
Expand Down
Loading