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

Update to onnx 1.15.0 and support OSX arm64 #124

Open
wants to merge 3 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
onnxruntime.git
Cargo.lock
**/synset.txt

.vscode
.vs
/.idea
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,24 @@ The C++ example that uses the C API
([`C_Api_Sample.cpp`](https://github.com/microsoft/onnxruntime/blob/v1.3.1/csharp/test/Microsoft.ML.OnnxRuntime.EndToEndTests.Capi/C_Api_Sample.cpp))
was ported to both the low level crate (`onnxruntime-sys`) and the high level on (`onnxruntime`).

When using **Windows**, do not rely on having the DLL in the `Path` directory. Manually copy the ONNX DLLs into the target build folder. If not, an error similar to the following happens:

```text
The given version [15] is not supported, only version 1 to 10 is supported in this build.
thread 'main' panicked at 'assertion failed: `(left != right)`
left: `0x0`,
right: `0x0`', onnxruntime-sys\examples\c_api_sample.rs:14:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\examples\c_api_sample.exe` (exit code: 101)
```

### onnxruntime-sys

To run this example ([`onnxruntime-sys/examples/c_api_sample.rs`](onnxruntime-sys/examples/c_api_sample.rs)):

```sh
# Download the model (SqueezeNet 1.0, ONNX version: 1.3, Opset version: 8)
❯ curl -LO "https://github.com/onnx/models/raw/master/vision/classification/squeezenet/model/squeezenet1.0-8.onnx"
❯ curl -LO "https://github.com/onnx/models/raw/main/vision/classification/squeezenet/model/squeezenet1.0-8.onnx"
❯ cargo run --example c_api_sample
[...]
Finished dev [unoptimized + debuginfo] target(s) in 1.88s
Expand Down Expand Up @@ -150,7 +161,7 @@ To run this example ([`onnxruntime/examples/sample.rs`](onnxruntime/examples/sam

```sh
# Download the model (SqueezeNet 1.0, ONNX version: 1.3, Opset version: 8)
❯ curl -LO "https://github.com/onnx/models/raw/master/vision/classification/squeezenet/model/squeezenet1.0-8.onnx"
❯ curl -LO "https://github.com/onnx/models/raw/main/vision/classification/squeezenet/model/squeezenet1.0-8.onnx"
❯ cargo run --example sample
[...]
Finished dev [unoptimized + debuginfo] target(s) in 13.62s
Expand Down
4 changes: 2 additions & 2 deletions onnxruntime-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ keywords = ["neuralnetworks", "onnx", "bindings"]
[dependencies]

[build-dependencies]
bindgen = { version = "0.59", optional = true }
bindgen = { version = "0.66.1", optional = true }
ureq = "2.1"

# Used on Windows
zip = "0.5"
zip = "0.6.0"

# Used on unix
flate2 = "1.0"
Expand Down
26 changes: 15 additions & 11 deletions onnxruntime-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ use std::{
path::{Path, PathBuf},
str::FromStr,
};
#[cfg(feature = "generate-bindings")]
use bindgen::Formatter;

/// ONNX Runtime version
///
/// WARNING: If version is changed, bindings for all platforms will have to be re-generated.
/// To do so, run this:
/// cargo build --package onnxruntime-sys --features generate-bindings
const ORT_VERSION: &str = "1.8.1";
const ORT_VERSION: &str = "1.15.0";

/// Base Url from which to download pre-built releases/
const ORT_RELEASE_BASE_URL: &str = "https://github.com/microsoft/onnxruntime/releases/download";
Expand Down Expand Up @@ -109,7 +111,7 @@ fn generate_bindings(include_dir: &Path) {
// Set `size_t` to be translated to `usize` for win32 compatibility.
.size_t_is_usize(true)
// Format using rustfmt
.rustfmt_bindings(true)
.formatter(Formatter::Rustfmt)
.rustified_enum("*")
// Finish the builder and generate the bindings.
.generate()
Expand Down Expand Up @@ -310,31 +312,33 @@ struct Triplet {
impl OnnxPrebuiltArchive for Triplet {
fn as_onnx_str(&self) -> Cow<str> {
match (&self.os, &self.arch, &self.accelerator) {
// onnxruntime-win-x86-1.8.1.zip
// onnxruntime-win-x64-1.8.1.zip
// onnxruntime-win-arm-1.8.1.zip
// onnxruntime-win-arm64-1.8.1.zip
// onnxruntime-linux-x64-1.8.1.tgz
// onnxruntime-osx-x64-1.8.1.tgz
// onnxruntime-win-x86-1.15.0.zip
// onnxruntime-win-x64-1.15.0.zip
// onnxruntime-win-arm-1.15.0.zip
// onnxruntime-win-arm64-1.15.0.zip
// onnxruntime-linux-x64-1.15.0.tgz
// onnxruntime-osx-x64-1.15.0.tgz
// onnxruntime-osx-arm64-1.15.0.tgz
(Os::Windows, Architecture::X86, Accelerator::None)
| (Os::Windows, Architecture::X86_64, Accelerator::None)
| (Os::Windows, Architecture::Arm, Accelerator::None)
| (Os::Windows, Architecture::Arm64, Accelerator::None)
| (Os::Linux, Architecture::X86_64, Accelerator::None)
| (Os::MacOs, Architecture::X86_64, Accelerator::None) => Cow::from(format!(
| (Os::MacOs, Architecture::X86_64, Accelerator::None)
| (Os::MacOs, Architecture::Arm64, Accelerator::None) => Cow::from(format!(
"{}-{}",
self.os.as_onnx_str(),
self.arch.as_onnx_str()
)),
// onnxruntime-win-gpu-x64-1.8.1.zip
// onnxruntime-win-gpu-x64-1.15.0.zip
// Note how this one is inverted from the linux one next
(Os::Windows, Architecture::X86_64, Accelerator::Gpu) => Cow::from(format!(
"{}-{}-{}",
self.os.as_onnx_str(),
self.accelerator.as_onnx_str(),
self.arch.as_onnx_str(),
)),
// onnxruntime-linux-x64-gpu-1.8.1.tgz
// onnxruntime-linux-x64-gpu-1.15.0.tgz
// Note how this one is inverted from the windows one above
(Os::Linux, Architecture::X86_64, Accelerator::Gpu) => Cow::from(format!(
"{}-{}-{}",
Expand Down
16 changes: 8 additions & 8 deletions onnxruntime-sys/examples/c_api_sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() {
let env_name = std::ffi::CString::new("test").unwrap();
let status = unsafe {
g_ort.as_ref().unwrap().CreateEnv.unwrap()(
OrtLoggingLevel::ORT_LOGGING_LEVEL_VERBOSE,
OrtLoggingLevel_ORT_LOGGING_LEVEL_VERBOSE,
env_name.as_ptr(),
&mut env_ptr,
)
Expand All @@ -44,7 +44,7 @@ fn main() {
.SetSessionGraphOptimizationLevel
.unwrap()(
session_options_ptr,
GraphOptimizationLevel::ORT_ENABLE_BASIC,
GraphOptimizationLevel_ORT_ENABLE_BASIC,
)
};

Expand All @@ -57,7 +57,7 @@ fn main() {
// NOTE: Original C version loaded SqueezeNet 1.0 (ONNX version: 1.3, Opset version: 8,
// https://github.com/onnx/models/blob/master/vision/classification/squeezenet/model/squeezenet1.0-8.onnx)
// Download it:
// curl -LO "https://github.com/onnx/models/raw/master/vision/classification/squeezenet/model/squeezenet1.0-8.onnx"
// curl -LO "https://github.com/onnx/models/raw/main/vision/classification/squeezenet/model/squeezenet1.0-8.onnx"
// Reference: https://github.com/onnx/models/tree/master/vision/classification/squeezenet#model
let model_path = std::ffi::OsString::from("squeezenet1.0-8.onnx");

Expand Down Expand Up @@ -159,14 +159,14 @@ fn main() {
assert_ne!(tensor_info_ptr, std::ptr::null_mut());

let mut type_: ONNXTensorElementDataType =
ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED;
ONNXTensorElementDataType_ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED;
let status = unsafe {
g_ort.as_ref().unwrap().GetTensorElementType.unwrap()(tensor_info_ptr, &mut type_)
};
CheckStatus(g_ort, status).unwrap();
assert_ne!(
type_,
ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED
ONNXTensorElementDataType_ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED
);

println!("Input {} : type={}", i, type_ as i32);
Expand Down Expand Up @@ -229,8 +229,8 @@ fn main() {
let mut memory_info_ptr: *mut OrtMemoryInfo = std::ptr::null_mut();
let status = unsafe {
g_ort.as_ref().unwrap().CreateCpuMemoryInfo.unwrap()(
OrtAllocatorType::OrtArenaAllocator,
OrtMemType::OrtMemTypeDefault,
OrtAllocatorType_OrtArenaAllocator,
OrtMemType_OrtMemTypeDefault,
&mut memory_info_ptr,
)
};
Expand Down Expand Up @@ -258,7 +258,7 @@ fn main() {
input_tensor_size * std::mem::size_of::<f32>(),
shape,
4,
ONNXTensorElementDataType::ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT,
ONNXTensorElementDataType_ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT,
input_tensor_ptr_ptr,
)
};
Expand Down
Loading