nbigaouette / onnxruntime-rs

Rust wrapper for Microsoft's ONNX Runtime (version 1.8)
Apache License 2.0
276 stars 99 forks source link

how to separate the creation and running of a session #99

Closed wujian752 closed 2 years ago

wujian752 commented 2 years ago

I'm curious about how to separate the creation and running of a session so that I can create a session in a function and then run it multiple times in another function to avoid repeating session creation like this:

use onnxruntime::{environment::Environment, session::Session, GraphOptimizationLevel, ndarray::{IxDyn, Array2}, tensor::OrtOwnedTensor};

pub struct Net<'a> {
    sess: Session<'a>,
}

type Error = Box<dyn std::error::Error>;

impl<'a> Net<'a> {
    pub fn new() -> Result<Net<'a>, Error> {
        let environment = Environment::builder().build()?;

        let session: Session<'a> = environment
            .new_session_builder()?
            .with_optimization_level(GraphOptimizationLevel::Basic)?
            .with_number_threads(8)?
            .with_model_from_file("model.onnx")?;

        Ok(Net {
            sess: session
        })
    }

    pub fn run(&mut self, array: Vec<Array2<f32>>) -> Vec<OrtOwnedTensor<f32, IxDyn>> {
        self.sess.run(array).unwrap()
    }
}
error[E0597]: `environment` does not live long enough
  --> crates/dmir_nn/src/beats.rs:13:36
   |
9  | impl<'a> Net<'a> {
   |      -- lifetime `'a` defined here
...
13 |         let session: Session<'a> = environment
   |                      -----------   ^^^^^^^^^^^ borrowed value does not live long enough
   |                      |
   |                      type annotation requires that `environment` is borrowed for `'a`
...
22 |     }
   |     - `environment` dropped here while still borrowed

I have tried to save the environment in Net too, but it also causes a self-referential problem.

Is there any way to solve such a problem?

haixuanTao commented 2 years ago

So, I had the same issue to run this in a server, and to make it work I had to make the environment owned by the session. You can see the modification here: https://github.com/haixuanTao/onnxruntime-rs/commit/4ebef94d5ac6df85f3fa271fd0a0a13b9be57dd1

You can also fork the branch.

wujian752 commented 2 years ago

Thanks a lot. @haixuanTao .