Struct ort::Session

source ·
pub struct Session {
    pub inputs: Vec<Input>,
    pub outputs: Vec<Output>,
    /* private fields */
}
Expand description

An ONNX Runtime graph to be used for inference.

let session = Session::builder()?.commit_from_file("tests/data/upsample.onnx")?;
let input = ndarray::Array4::<f32>::zeros((1, 64, 64, 3));
let outputs = session.run(ort::inputs![input]?)?;

Fields§

§inputs: Vec<Input>

Information about the graph’s inputs.

§outputs: Vec<Output>

Information about the graph’s outputs.

Implementations§

source§

impl Session

source

pub fn builder() -> Result<SessionBuilder>

Creates a new SessionBuilder.

source

pub fn allocator(&self) -> &Allocator

Returns this session’s Allocator.

source

pub fn create_binding(&self) -> Result<IoBinding<'_>>

Creates a new IoBinding for this session.

source

pub fn ptr(&self) -> *mut OrtSession

Returns the underlying ort_sys::OrtSession pointer.

source

pub fn inner(&self) -> Arc<SharedSessionInner>

Get a shared (Arc’d) reference to the underlying SharedSessionInner, which holds the ort_sys::OrtSession pointer and the session allocator.

source

pub fn run<'s, 'i, 'v: 'i, const N: usize>( &'s self, input_values: impl Into<SessionInputs<'i, 'v, N>> ) -> Result<SessionOutputs<'s>>

Run input data through the ONNX graph, performing inference.

See crate::inputs! for a convenient macro which will help you create your session inputs from ndarrays or other data. You can also provide a Vec, array, or HashMap of Values if you create your inputs dynamically.

let session = Session::builder()?.commit_from_file("tests/data/upsample.onnx")?;
let input = ndarray::Array4::<f32>::zeros((1, 64, 64, 3));
let outputs = session.run(ort::inputs![input]?)?;
source

pub fn run_with_options<'s, 'i, 'v: 'i, const N: usize>( &'s self, input_values: impl Into<SessionInputs<'i, 'v, N>>, run_options: Arc<RunOptions> ) -> Result<SessionOutputs<'s>>

Run input data through the ONNX graph, performing inference, with a RunOptions struct. The most common usage of RunOptions is to allow the session run to be terminated from a different thread.

let run_options = Arc::new(RunOptions::new()?);

let run_options_ = Arc::clone(&run_options);
std::thread::spawn(move || {
	let _ = run_options_.terminate();
});

let res = session.run_with_options(ort::inputs![input]?, run_options);
// upon termination, the session will return an `Error::SessionRun` error.`
assert_eq!(
	&res.unwrap_err().to_string(),
	"Failed to run inference on model: Exiting due to terminate flag being set to true."
);
source

pub fn run_async<'s, 'i, 'v: 'i + 's, const N: usize>( &'s self, input_values: impl Into<SessionInputs<'i, 'v, N>> + 'static ) -> Result<InferenceFut<'s>>

Asynchronously run input data through the ONNX graph, performing inference.

Inference will be performed on a thread in the session’s thread pool. Thus, the session must have been configured to have multiple intra-op threads; see SessionBuilder::with_intra_threads.

See crate::inputs! for a convenient macro which will help you create your session inputs from ndarrays or other data. You can also provide a Vec, array, or HashMap of Values if you create your inputs dynamically.

let session = Session::builder()?.with_intra_threads(2)?.commit_from_file("tests/data/upsample.onnx")?;
let input = ndarray::Array4::<f32>::zeros((1, 64, 64, 3));
let outputs = session.run_async(ort::inputs![input]?)?.await?;
source

pub fn metadata(&self) -> Result<ModelMetadata<'_>>

Gets the session model metadata. See ModelMetadata for more info.

source

pub fn end_profiling(&self) -> Result<String>

Ends profiling for this session.

Note that this must be explicitly called at the end of profiling, otherwise the profiling file will be empty.

Trait Implementations§

source§

impl Debug for Session

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Send for Session

source§

impl Sync for Session

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more