pub struct Interpreter { /* private fields */ }Expand description
The Interpreter holds the model and manages sessions.
It’s internals use a shared mutex so it’s thread-safe. net data holder. multiple sessions could share same net.
Implementations§
Source§impl Interpreter
impl Interpreter
Sourcepub fn from_file(path: impl AsRef<Path>) -> Result<Self>
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
Sourcepub fn from_bytes(bytes: impl AsRef<[u8]>) -> Result<Self>
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
Sourcepub fn set_session_mode(&self, mode: SessionMode)
pub fn set_session_mode(&self, mode: SessionMode)
Set session mode
mode: the session mode
Warning: It should be called before create session!
Sourcepub fn resize_session(&self, session: &mut Session<'_>)
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
Sourcepub fn resize_session_reallocate(&self, session: &mut Session<'_>)
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!
Sourcepub fn resize_tensor<H: HalideType, M: TensorMachine>(
&self,
tensor: &mut TensorRef<H, M>,
dims: impl AsTensorShape,
)
pub fn resize_tensor<H: HalideType, M: TensorMachine>( &self, tensor: &mut TensorRef<H, M>, dims: impl AsTensorShape, )
Resize the tensor using the given shape
Sourcepub fn resize_tensor_by_name(
&self,
session: &mut Session<'_>,
name: impl AsRef<str>,
dims: impl AsTensorShape,
) -> Result<()>
pub fn resize_tensor_by_name( &self, session: &mut Session<'_>, name: impl AsRef<str>, dims: impl AsTensorShape, ) -> Result<()>
Resize the tenror by name using the given shape
Sourcepub fn resize_tensor_by_nchw<H: HalideType, M: TensorMachine>(
&self,
tensor: &mut TensorRef<H, M>,
batch: u16,
channel: u16,
height: u16,
width: u16,
)
pub fn resize_tensor_by_nchw<H: HalideType, M: TensorMachine>( &self, tensor: &mut TensorRef<H, M>, batch: u16, channel: u16, height: u16, width: u16, )
Resize tensor by
- N -> batch
- C -> channel
- H -> height
- W -> width
Sourcepub fn create_session(&self, schedule: ScheduleConfig) -> Result<Session<'_>>
pub fn create_session(&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
Sourcepub unsafe fn release_model(&mut self)
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
Sourcepub fn create_multipath_session(
&self,
schedule: impl IntoIterator<Item = ScheduleConfig>,
) -> Result<Session<'_>>
pub fn create_multipath_session( &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
Sourcepub fn model_print_io(path: impl AsRef<Path>) -> Result<()>
pub fn model_print_io(path: impl AsRef<Path>) -> Result<()>
Print all input and output tensors info.
Sourcepub fn inputs<'i>(&self, session: &'i mut Session<'_>) -> TensorListMut<'i>
pub fn inputs<'i>(&self, session: &'i mut Session<'_>) -> TensorListMut<'i>
Get the input tensor of the session.
session: the session to get input tensor
return: List of input tensors
Sourcepub fn input<'s, H: HalideType>(
&self,
session: &'s mut Session<'_>,
name: impl AsRef<str>,
) -> Result<&'s mut TensorRef<H, Device>>
pub fn input<'s, H: HalideType>( &self, session: &'s mut Session<'_>, name: impl AsRef<str>, ) -> Result<&'s mut TensorRef<H, Device>>
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
Sourcepub fn raw_input<'s>(
&self,
session: &'s mut Session<'_>,
name: impl AsRef<str>,
) -> Result<&'s AnyTensorRef>
pub fn raw_input<'s>( &self, session: &'s mut Session<'_>, name: impl AsRef<str>, ) -> Result<&'s AnyTensorRef>
Get the raw input tensor of a session by name
Sourcepub unsafe fn input_unresized<'s, H: HalideType>(
&self,
session: &'s mut Session<'_>,
name: impl AsRef<str>,
) -> Result<&'s mut TensorRef<H, Device>>
pub unsafe fn input_unresized<'s, H: HalideType>( &self, session: &'s mut Session<'_>, name: impl AsRef<str>, ) -> Result<&'s mut TensorRef<H, Device>>
§Safety
Warning We Still don’t know the safety guarantees of this function so it’s marked unsafe
Sourcepub unsafe fn input_unchecked<'s, H: HalideType>(
&self,
session: &'s mut Session<'_>,
name: impl AsRef<str>,
) -> &'s mut TensorRef<H, Device>
pub unsafe fn input_unchecked<'s, H: HalideType>( &self, session: &'s mut Session<'_>, name: impl AsRef<str>, ) -> &'s mut TensorRef<H, Device>
§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
Sourcepub fn output<'s, H: HalideType>(
&self,
session: &'s Session<'_>,
name: impl AsRef<str>,
) -> Result<&'s TensorRef<H, Device>>
pub fn output<'s, H: HalideType>( &self, session: &'s Session<'_>, name: impl AsRef<str>, ) -> Result<&'s TensorRef<H, Device>>
Get the output tensor of a session by name
session : the session to get output tensor from
name : the name of the output tensor
Sourcepub fn raw_output<'s>(
&self,
session: &'s Session<'_>,
name: impl AsRef<str>,
) -> Result<&'s AnyTensorRef>
pub fn raw_output<'s>( &self, session: &'s Session<'_>, name: impl AsRef<str>, ) -> Result<&'s AnyTensorRef>
Get the raw output tensor of a session by name
Sourcepub fn run_session(&self, session: &Session<'_>) -> Result<()>
pub fn run_session(&self, session: &Session<'_>) -> Result<()>
Run a session
Sourcepub fn run_session_with_callback(
&self,
session: &Session<'_>,
before: impl Fn(&[&AnyTensorRef], OperatorInfo<'_>) -> bool + 'static,
end: impl Fn(&[&AnyTensorRef], OperatorInfo<'_>) -> bool + 'static,
sync: bool,
) -> Result<()>
pub fn run_session_with_callback( &self, session: &Session<'_>, before: impl Fn(&[&AnyTensorRef], OperatorInfo<'_>) -> bool + 'static, end: impl Fn(&[&AnyTensorRef], 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.
Sourcepub fn outputs<'o>(&self, session: &'o Session<'_>) -> TensorList<'o>
pub fn outputs<'o>(&self, session: &'o Session<'_>) -> TensorList<'o>
Get all output tensors of a session
Sourcepub fn set_cache_file(
&self,
path: impl AsRef<Path>,
key_size: usize,
) -> Result<()>
pub fn set_cache_file( &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!
Sourcepub fn update_cache_file(&self, session: &mut Session<'_>) -> Result<()>
pub fn update_cache_file(&self, session: &mut Session<'_>) -> Result<()>
Update cache file
Sourcepub fn wait(&self, session: &Session<'_>)
pub fn wait(&self, session: &Session<'_>)
Wait for all output tensors to be ready after computation
Sourcepub fn flops(&self, session: &Session<'_>) -> Result<f32>
pub fn flops(&self, session: &Session<'_>) -> Result<f32>
Get float operation needed in session in M
Sourcepub fn resize_status(&self, session: &Session<'_>) -> Result<ResizeStatus>
pub fn resize_status(&self, session: &Session<'_>) -> Result<ResizeStatus>
Get the resize status