Skip to main content

ModelHandle

Enum ModelHandle 

Source
pub enum ModelHandle {
    Loaded {
        model: Model,
        compute_units: ComputeUnits,
    },
    Unloaded {
        path: PathBuf,
        compute_units: ComputeUnits,
    },
}
Expand description

A model handle that supports unloading and reloading.

Wraps a Model with lifecycle management. Unloading releases the model’s GPU/ANE resources (by dropping the inner MLModel) while retaining the filesystem path and compute-unit configuration for efficient reloading.

State transitions consume self, so the compiler prevents calling predict on an unloaded model or double-unloading.

§Example

use coreml_native::{ComputeUnits, ModelHandle};

let handle = ModelHandle::load("model.mlmodelc", ComputeUnits::All)?;
let prediction = handle.predict(&[("input", &tensor)])?;

// Free GPU/ANE memory when the model is idle.
let handle = handle.unload()?;
assert!(!handle.is_loaded());

// Reload when needed again.
let handle = handle.reload()?;
let prediction = handle.predict(&[("input", &tensor)])?;

Variants§

§

Loaded

Model is loaded and ready for inference.

Fields

§model: Model

The loaded model instance.

§compute_units: ComputeUnits

The compute units used to load this model (preserved for reload).

§

Unloaded

Model has been unloaded from memory. The path and configuration are retained so the model can be reloaded without the caller needing to remember them.

Fields

§path: PathBuf

Filesystem path to the compiled .mlmodelc bundle.

§compute_units: ComputeUnits

Compute units to use when reloading.

Implementations§

Source§

impl ModelHandle

Source

pub fn load(path: impl AsRef<Path>, compute_units: ComputeUnits) -> Result<Self>

Load a compiled CoreML model and wrap it in a lifecycle handle.

This is equivalent to Model::load but returns a ModelHandle that supports later unloading and reloading.

§Errors

Returns an error if the model cannot be loaded (invalid path, corrupt model, or non-Apple platform).

Source

pub fn from_model(model: Model, compute_units: ComputeUnits) -> Self

Wrap an already-loaded Model in a lifecycle handle.

Use this when you already have a Model instance (e.g. loaded via Model::load_async) and want to add lifecycle management.

Source

pub fn is_loaded(&self) -> bool

Returns true if the model is currently loaded and ready for inference.

Source

pub fn path(&self) -> &Path

Returns the filesystem path this model was (or will be) loaded from.

Source

pub fn compute_units(&self) -> ComputeUnits

Returns the compute-unit configuration.

Source

pub fn model(&self) -> Result<&Model>

Get a reference to the loaded model.

§Errors

Returns an error if the model is currently unloaded.

Source

pub fn unload(self) -> Result<Self>

Unload the model from memory, releasing GPU/ANE resources.

The path and compute-unit configuration are preserved so the model can be reloaded later via reload.

This method consumes self and returns a new ModelHandle in the Unloaded state. The inner MLModel is dropped, which tells CoreML to release its GPU and Neural Engine allocations.

§Errors

Returns an error if the model is already unloaded.

Source

pub fn reload(self) -> Result<Self>

Reload a previously unloaded model from its original path.

Uses the same compute-unit configuration that was active when the model was first loaded.

§Errors

Returns an error if the model is already loaded, or if reloading fails (e.g. the model file was deleted while unloaded).

Source

pub fn predict( &self, inputs: &[(&str, &dyn AsMultiArray)], ) -> Result<Prediction>

Run a prediction on the loaded model.

This is a convenience method that delegates to Model::predict.

§Errors

Returns an error if the model is unloaded, or if prediction fails.

Source

pub fn inputs(&self) -> Result<Vec<FeatureDescription>>

Get descriptions of all model inputs.

§Errors

Returns an error if the model is unloaded.

Source

pub fn outputs(&self) -> Result<Vec<FeatureDescription>>

Get descriptions of all model outputs.

§Errors

Returns an error if the model is unloaded.

Source

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

Get model metadata (author, description, version, license).

§Errors

Returns an error if the model is unloaded.

Trait Implementations§

Source§

impl Debug for ModelHandle

Source§

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

Formats the value using the given formatter. Read more

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.