Skip to main content

Interpreter

Struct Interpreter 

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

A model interpreter that holds a loaded neural network model.

The interpreter is the main entry point for MNN inference. It manages the model and can create multiple sessions for concurrent inference.

§Thread Safety

The interpreter is thread-safe and can be shared across threads. Multiple sessions can be created from a single interpreter.

§Example

use mnn_rs::{Interpreter, ScheduleConfig, BackendType};

// Load a model
let interpreter = Interpreter::from_file("model.mnn")?;

// Create a session with configuration
let config = ScheduleConfig::new()
    .backend(BackendType::CPU)
    .num_threads(4);

let mut session = interpreter.create_session(config)?;

// Get input tensor and fill with data
let input = session.get_input(None)?;
// ... fill input with data ...

// Run inference
session.run()?;

// Get output
let output = session.get_output(None)?;

Implementations§

Source§

impl Interpreter

Source

pub fn from_file<P: AsRef<Path>>(path: P) -> MnnResult<Self>

Create a new interpreter from a model file.

§Arguments
  • path - Path to the MNN model file
§Returns

A new interpreter on success, or an error if the model cannot be loaded.

§Example
use mnn_rs::Interpreter;

let interpreter = Interpreter::from_file("model.mnn")?;
Source

pub fn from_bytes(data: &[u8]) -> MnnResult<Self>

Create a new interpreter from in-memory model data.

This is useful for embedding models in the binary or loading models from non-filesystem sources.

§Arguments
  • data - The model data as bytes
§Returns

A new interpreter on success, or an error if the model cannot be loaded.

§Example
use mnn_rs::Interpreter;

let model_data = include_bytes!("model.mnn");
let interpreter = Interpreter::from_bytes(model_data)?;
Source

pub fn create_session(&self, config: ScheduleConfig) -> MnnResult<Session>

Create a new inference session.

Sessions hold the runtime state for inference and can be created with different backend configurations.

§Arguments
  • config - Schedule configuration specifying backend and settings
§Returns

A new session on success, or an error if session creation fails.

Source

pub fn model_path(&self) -> Option<&str>

Get the model path (if loaded from file).

Source

pub fn business_code(&self) -> String

Get the business code (model identifier).

§Returns

The business code string.

Source

pub fn uuid(&self) -> String

Get the model UUID.

§Returns

The UUID string.

Source

pub fn inner(&self) -> *mut MNNInterpreter

Get the raw pointer to the underlying MNN Interpreter.

§Safety

The returned pointer is owned by this Interpreter and must not be freed.

Source

pub fn set_session_mode(&self, mode: SessionMode)

Set session mode.

§Arguments
  • mode - The session mode to set
Source

pub fn set_cache_file<P: AsRef<Path>>(&self, path: P, key_size: usize)

Set cache file for optimization.

§Arguments
  • path - Path to the cache file
  • key_size - Key size for cache lookup (default: 128)
Source

pub fn update_cache(&self, session: &Session) -> MnnResult<()>

Update cache from a session.

§Arguments
  • session - The session to update cache from
§Returns

Ok(()) on success, or an error on failure.

Source

pub fn set_external_file<P: AsRef<Path>>(&self, path: P, flag: usize)

Set external file for model.

§Arguments
  • path - Path to the external file
  • flag - Flag for external file processing
Source

pub fn get_input_names(&self, session: &Session) -> Vec<String>

Get input tensor names for a session.

§Arguments
  • session - The session to get input names from
§Returns

A vector of input tensor names.

Source

pub fn get_output_names(&self, session: &Session) -> Vec<String>

Get output tensor names for a session.

§Arguments
  • session - The session to get output names from
§Returns

A vector of output tensor names.

Source

pub fn resize_tensor(&self, tensor: &mut Tensor, shape: &[i32])

Resize a tensor with new shape.

§Arguments
  • tensor - The tensor to resize
  • shape - New shape
Source

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

Resize a session after tensor resizing.

§Arguments
  • session - The session to resize
Source

pub fn get_session_flops(&self, session: &Session) -> f32

Get session FLOPS count.

§Arguments
  • session - The session to query
§Returns

FLOPS count in millions.

Source

pub fn get_session_op_count(&self, session: &Session) -> i32

Get session operator count.

§Arguments
  • session - The session to query
§Returns

Operator count (approximate, based on FLOPS).

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

Source§

impl Sync 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.