mnn::interpreter

Struct Interpreter

source
pub struct Interpreter { /* private fields */ }
Expand description

net data holder. multiple sessions could share same net.

Implementations§

source§

impl Interpreter

source

pub fn from_file(path: impl AsRef<Path>) -> Result<Self>

Create an net/interpreter from a file.

path: the file path of the model

return: the created net/interpreter

source

pub fn from_bytes(bytes: impl AsRef<[u8]>) -> Result<Self>

Create an net/interpreter from a buffer.

bytes: the buffer of the model

return: the created net/interpreter

source

pub fn set_session_mode(&mut self, mode: SessionMode)

Set session mode

mode: the session mode

Warning: It should be called before create session!

source

pub fn resize_session(&self, session: &mut Session)

call this function to get tensors ready.

output tensor buffer (host or deviceId) should be retrieved after resize of any input tensor.

session: the session to be prepared

source

pub fn resize_session_reallocate(&self, session: &mut Session)

Resize session and reallocate the buffer.

session: the session to be prepared.

§Note

NeedRelloc is default to 1, 1 means need realloc!

source

pub fn resize_tensor<T: TensorType>( &self, tensor: &mut Tensor<T>, dims: impl AsTensorShape, )

Resize the tensor using the given shape

source

pub fn resize_tensor_by_nchw<T: TensorType>( &self, tensor: &mut Tensor<T>, batch: u16, channel: u16, height: u16, width: u16, )

Resize tensor by

  • N -> batch
  • C -> channel
  • H -> height
  • W -> width
source

pub fn create_session(&mut self, schedule: ScheduleConfig) -> Result<Session>

Create a session with session config. Session will be managed in net/interpreter.

schedule : the config of the session

return: the created session

source

pub unsafe fn release_model(&mut self)

Release the model file buffer

§Safety

This function is marked unsafe since it’s not clear what the safety guarantees are right now. With a simple test it caused a segfault so it’s marked unsafe

source

pub fn create_multipath_session( &mut self, schedule: impl IntoIterator<Item = ScheduleConfig>, ) -> Result<Session>

Create multi-path session with schedule configs and user-specified runtime. created session will be managed in net/interpreter.

schedule : the config of the session

return: the created session

source

pub fn model_print_io(path: impl AsRef<Path>) -> Result<()>

Print all input and output tensors info.

source

pub fn inputs<'i>(&self, session: &'i Session) -> TensorList<'i>

Get the input tensor of the session.

session: the session to get input tensor

return: List of input tensors

source

pub fn input<'s, H: HalideType>( &self, session: &'s Session, name: impl AsRef<str>, ) -> Result<Tensor<RefMut<'s, Device<H>>>>

Get the input tensor of the session by name.

session: the session to get input tensor from

name: the name of the input tensor

return: the input tensor

source

pub fn raw_input<'s>( &self, session: &'s Session, name: impl AsRef<str>, ) -> Result<RawTensor<'s>>

Get the raw input tensor of a session by name

source

pub unsafe fn input_unresized<'s, H: HalideType>( &self, session: &'s Session, name: impl AsRef<str>, ) -> Result<Tensor<RefMut<'s, Device<H>>>>

§Safety

Warning We Still don’t know the safety guarantees of this function so it’s marked unsafe

source

pub unsafe fn input_unchecked<'s, H: HalideType>( &self, session: &'s Session, name: impl AsRef<str>, ) -> Tensor<RefMut<'s, Device<H>>>

§Safety

Very unsafe since it doesn’t check the type of the tensor as well as the shape of the tensor

Panics if the name is not ascii Undefined Behavior if the tensor is not of type H

source

pub fn output<'s, H: HalideType>( &self, session: &'s Session, name: impl AsRef<str>, ) -> Result<Tensor<Ref<'s, Device<H>>>>

Get the output tensor of a session by name

session : the session to get output tensor from

name : the name of the output tensor

source

pub fn raw_output<'s>( &self, session: &'s Session, name: impl AsRef<str>, ) -> Result<RawTensor<'s>>

Get the raw output tensor of a session by name

source

pub fn run_session(&mut self, session: &Session) -> Result<()>

Run a session

source

pub fn run_session_with_callback( &mut self, session: &Session, before: impl Fn(&[RawTensor<'_>], OperatorInfo<'_>) -> bool + 'static, end: impl Fn(&[RawTensor<'_>], OperatorInfo<'_>) -> bool + 'static, sync: bool, ) -> Result<()>

Run a session with a callback

session : the session to run

before : a callback before each op. return true to run the op; return false to skip the op.

after : a callback after each op. return true to continue running; return false to interrupt the session.

sync : synchronously wait for finish of execution or not.

source

pub fn outputs<'o>(&self, session: &'o Session) -> TensorList<'o>

Get all output tensors of a session

source

pub fn set_cache_file( &mut self, path: impl AsRef<Path>, key_size: usize, ) -> Result<()>

If the cache exist, try to load cache from file. After createSession, try to save cache to file.

cache_file : the file path to save or load cache.

key_size : the size of key

§Note

The API should be called before create session.

Key Depercerate, keeping for future use!

source

pub fn update_cache_file(&mut self, session: &mut Session) -> Result<()>

Update cache file

source

pub fn wait(&self, session: &Session)

Wait for all output tensors to be ready after computation

source

pub fn memory(&self, session: &Session) -> Result<f32>

Get memory usage of a session in MB

source

pub fn flops(&self, session: &Session) -> Result<f32>

Get float operation needed in session in M

source

pub fn resize_status(&self, session: &Session) -> Result<ResizeStatus>

Get the resize status

Trait Implementations§

source§

impl Debug for Interpreter

source§

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

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

impl Drop for Interpreter

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Send for Interpreter

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, 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>,

source§

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>,

source§

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.