-
Notifications
You must be signed in to change notification settings - Fork 100
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
Dynamic dimension support, fix multiple inputs issue #23
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This allows dynamic dimension (None). When '-1' is found in an onnx model, dimension will be converted to 'None'. Before sending to Run(), the None will be replaced with the input's dimension.
LGTM! I tried the following, adapted from the issue:
[package]
name = "onnxruntimerstest"
version = "0.1.0"
authors = ["Benjamin Minixhofer <bminixhofer@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
onnxruntime = { git = "https://github.com/nbigaouette/onnxruntime-rs", branch = "issue22" }
ndarray = "0.13.1"
use ndarray::Array2;
use onnxruntime::environment::Environment;
use onnxruntime::tensor::OrtOwnedTensor;
use onnxruntime::GraphOptimizationLevel;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let env = Environment::builder().with_name("env").build()?;
let mut session = env
.new_session_builder()?
.with_optimization_level(GraphOptimizationLevel::Basic)?
.with_model_from_file("model.onnx")?;
println!("{:#?}", session.inputs);
println!("{:#?}", session.outputs);
let input_ids = Array2::<i64>::from_shape_vec((1, 3), vec![1i64, 2i64, 3i64])?;
let attention_mask = Array2::<i64>::from_shape_vec((1, 3), vec![1i64, 1i64, 1i64])?;
let output: OrtOwnedTensor<f32, _> = session
.run(vec![input_ids.clone(), attention_mask])?
.remove(0);
println!("{:#?}", output.into_slice().unwrap());
Ok(())
} I get the same output as with equivalent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add support for dynamic dimension
Dynamic dimension is supported in ONNX and is stored as
-1
in the model file. Use anOption<>
for the dimension values soNone
can represent "unknown" (dynamic). Dimension of the input array is used to fix the size to send to the runtime.Fix problem with multiple inputs (and outputs)
Don't loop over the input values and call
Run()
multiple times. Multiple inputs means the model takes multiple inputs to perform a single inference. Pack all inputs together before sending to a singleRun()
call.Fix bug with input and output types
Initially inputs and outputs generic types were the same. This is not necessarily true. Inputs and outputs should be able to have different types. A new generic type is added to
Session::run()
for the output type.Closes #22.