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
impl Interpreter
Sourcepub fn from_bytes(data: &[u8]) -> MnnResult<Self>
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)?;Sourcepub fn create_session(&self, config: ScheduleConfig) -> MnnResult<Session>
pub fn create_session(&self, config: ScheduleConfig) -> MnnResult<Session>
Sourcepub fn model_path(&self) -> Option<&str>
pub fn model_path(&self) -> Option<&str>
Get the model path (if loaded from file).
Sourcepub fn business_code(&self) -> String
pub fn business_code(&self) -> String
Sourcepub fn inner(&self) -> *mut MNNInterpreter
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.
Sourcepub fn set_session_mode(&self, mode: SessionMode)
pub fn set_session_mode(&self, mode: SessionMode)
Sourcepub fn set_cache_file<P: AsRef<Path>>(&self, path: P, key_size: usize)
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 filekey_size- Key size for cache lookup (default: 128)
Sourcepub fn update_cache(&self, session: &Session) -> MnnResult<()>
pub fn update_cache(&self, session: &Session) -> MnnResult<()>
Sourcepub fn set_external_file<P: AsRef<Path>>(&self, path: P, flag: usize)
pub fn set_external_file<P: AsRef<Path>>(&self, path: P, flag: usize)
Set external file for model.
§Arguments
path- Path to the external fileflag- Flag for external file processing