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

Dynamic dimension support, fix multiple inputs issue #23

Merged
merged 21 commits into from
Oct 6, 2020
Merged

Conversation

nbigaouette
Copy link
Owner

@nbigaouette nbigaouette commented Sep 30, 2020

  1. Add support for dynamic dimension

    Dynamic dimension is supported in ONNX and is stored as -1 in the model file. Use an Option<> for the dimension values so None can represent "unknown" (dynamic). Dimension of the input array is used to fix the size to send to the runtime.

  2. 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 single Run() call.

  3. 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.

@bminixhofer
Copy link

bminixhofer commented Oct 5, 2020

LGTM! I tried the following, adapted from the issue:

Cargo.toml

[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"

src/main.rs

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 onnxruntime Python code and the input / output dims and datatypes look correct to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Model with dynamic dimensions
2 participants