nbigaouette/onnxruntime-rs

how to separate the creation and running of a session

wujian752 opened this issue · 2 comments

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?

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: haixuanTao@4ebef94

You can also fork the branch.

Thanks a lot. @haixuanTao .