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
compute_units: ComputeUnitsThe 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
compute_units: ComputeUnitsCompute units to use when reloading.
Implementations§
Source§impl ModelHandle
impl ModelHandle
Sourcepub fn load(path: impl AsRef<Path>, compute_units: ComputeUnits) -> Result<Self>
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).
Sourcepub fn from_model(model: Model, compute_units: ComputeUnits) -> Self
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.
Sourcepub fn is_loaded(&self) -> bool
pub fn is_loaded(&self) -> bool
Returns true if the model is currently loaded and ready for
inference.
Sourcepub fn path(&self) -> &Path
pub fn path(&self) -> &Path
Returns the filesystem path this model was (or will be) loaded from.
Sourcepub fn compute_units(&self) -> ComputeUnits
pub fn compute_units(&self) -> ComputeUnits
Returns the compute-unit configuration.
Sourcepub fn unload(self) -> Result<Self>
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.
Sourcepub fn reload(self) -> Result<Self>
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).
Sourcepub fn predict(
&self,
inputs: &[(&str, &dyn AsMultiArray)],
) -> Result<Prediction>
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.
Sourcepub fn inputs(&self) -> Result<Vec<FeatureDescription>>
pub fn inputs(&self) -> Result<Vec<FeatureDescription>>
Sourcepub fn outputs(&self) -> Result<Vec<FeatureDescription>>
pub fn outputs(&self) -> Result<Vec<FeatureDescription>>
Sourcepub fn metadata(&self) -> Result<ModelMetadata>
pub fn metadata(&self) -> Result<ModelMetadata>
Get model metadata (author, description, version, license).
§Errors
Returns an error if the model is unloaded.