mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-06-18 01:54:05 +00:00
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
47 lines
1.4 KiB
Rust
47 lines
1.4 KiB
Rust
//! Display the input and output structure of an ONNX model.
|
|
use onnxruntime::{environment, LoggingLevel};
|
|
use std::{env::var, error::Error};
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
let path = var("RUST_ONNXRUNTIME_LIBRARY_PATH").ok();
|
|
|
|
let builder = environment::Environment::builder()
|
|
.with_name("onnx_metadata")
|
|
.with_log_level(LoggingLevel::Verbose);
|
|
|
|
let builder = if let Some(path) = path.clone() {
|
|
builder.with_library_path(path)
|
|
} else {
|
|
builder
|
|
};
|
|
|
|
let environment = builder.build().unwrap();
|
|
|
|
// provide path to .onnx model on disk
|
|
let path = std::env::args()
|
|
.nth(1)
|
|
.expect("Must provide an .onnx file as the first arg");
|
|
|
|
let session = environment
|
|
.new_session_builder()?
|
|
.with_graph_optimization_level(onnxruntime::GraphOptimizationLevel::Basic)?
|
|
.with_model_from_file(path)?;
|
|
|
|
println!("Inputs:");
|
|
for (index, input) in session.inputs.iter().enumerate() {
|
|
println!(
|
|
" {}:\n name = {}\n type = {:?}\n dimensions = {:?}",
|
|
index, input.name, input.input_type, input.dimensions
|
|
)
|
|
}
|
|
|
|
println!("Outputs:");
|
|
for (index, output) in session.outputs.iter().enumerate() {
|
|
println!(
|
|
" {}:\n name = {}\n type = {:?}\n dimensions = {:?}",
|
|
index, output.name, output.output_type, output.dimensions
|
|
);
|
|
}
|
|
|
|
Ok(())
|
|
}
|