pub struct Model { /* private fields */ }Implementations§
Source§impl Model
impl Model
Sourcepub fn load_async(
path: impl AsRef<Path>,
compute_units: ComputeUnits,
) -> Result<CompletionFuture<Self>>
pub fn load_async( path: impl AsRef<Path>, compute_units: ComputeUnits, ) -> Result<CompletionFuture<Self>>
Load a compiled model asynchronously.
Returns a CompletionFuture that resolves when loading completes.
Use .await in async contexts or .block_on() for synchronous callers.
Requires macOS 12+ / iOS 15+.
Sourcepub fn load_from_bytes(
data: &[u8],
compute_units: ComputeUnits,
) -> Result<CompletionFuture<Self>>
pub fn load_from_bytes( data: &[u8], compute_units: ComputeUnits, ) -> Result<CompletionFuture<Self>>
Load a model from in-memory specification bytes asynchronously.
Creates an MLModelAsset from the specification data (synchronously),
then loads the model asynchronously via the completion handler API.
The data parameter should contain the contents of a .mlmodel file
(the protobuf specification, not a compiled .mlmodelc).
Requires macOS 14.4+ / iOS 17.4+.
Sourcepub fn predict_async(
&self,
inputs: &[(&str, &dyn AsMultiArray)],
) -> Result<CompletionFuture<Prediction>>
pub fn predict_async( &self, inputs: &[(&str, &dyn AsMultiArray)], ) -> Result<CompletionFuture<Prediction>>
Run a prediction asynchronously.
Builds the feature provider from the input tensors, then calls the async prediction API with a completion handler.
Requires macOS 14+ / iOS 17+.
Source§impl Model
impl Model
Sourcepub fn load(path: impl AsRef<Path>, compute_units: ComputeUnits) -> Result<Self>
pub fn load(path: impl AsRef<Path>, compute_units: ComputeUnits) -> Result<Self>
Examples found in repository?
7fn main() {
8 let model_path = std::env::args()
9 .nth(1)
10 .unwrap_or_else(|| "tests/fixtures/test_linear.mlmodelc".to_string());
11
12 println!("Loading model: {model_path}");
13 let model = Model::load(&model_path, ComputeUnits::All).expect("Failed to load model");
14
15 // Create a simple input tensor
16 let input_data = vec![1.0f32, 2.0, 3.0, 4.0];
17 let tensor =
18 BorrowedTensor::from_f32(&input_data, &[1, 4]).expect("Failed to create tensor");
19
20 println!("Running prediction...");
21 let prediction = model.predict(&[("input", &tensor)]).expect("Prediction failed");
22
23 let (output, shape) = prediction.get_f32("output").expect("Failed to get output");
24 println!("Output shape: {shape:?}");
25 println!("Output data: {output:?}");
26}More examples
7fn main() {
8 let model_path = std::env::args()
9 .nth(1)
10 .unwrap_or_else(|| "tests/fixtures/test_linear.mlmodelc".to_string());
11
12 println!("Loading model: {model_path}");
13 let model = Model::load(&model_path, ComputeUnits::All).expect("Failed to load model");
14
15 println!("\n--- Inputs ---");
16 for desc in model.inputs() {
17 println!(
18 " {}: {:?} shape={:?} optional={}",
19 desc.name(),
20 desc.feature_type(),
21 desc.shape(),
22 desc.is_optional(),
23 );
24 if let Some(dt) = desc.data_type() {
25 println!(" data_type: {dt}");
26 }
27 }
28
29 println!("\n--- Outputs ---");
30 for desc in model.outputs() {
31 println!(
32 " {}: {:?} shape={:?}",
33 desc.name(),
34 desc.feature_type(),
35 desc.shape(),
36 );
37 if let Some(dt) = desc.data_type() {
38 println!(" data_type: {dt}");
39 }
40 }
41
42 let meta = model.metadata();
43 println!("\n--- Metadata ---");
44 println!(" author: {:?}", meta.author);
45 println!(" description: {:?}", meta.description);
46 println!(" version: {:?}", meta.version);
47 println!(" license: {:?}", meta.license);
48}Sourcepub fn predict(
&self,
inputs: &[(&str, &dyn AsMultiArray)],
) -> Result<Prediction>
pub fn predict( &self, inputs: &[(&str, &dyn AsMultiArray)], ) -> Result<Prediction>
Run a synchronous prediction with named input tensors.
Accepts any type implementing AsMultiArray (both BorrowedTensor and OwnedTensor).
Examples found in repository?
7fn main() {
8 let model_path = std::env::args()
9 .nth(1)
10 .unwrap_or_else(|| "tests/fixtures/test_linear.mlmodelc".to_string());
11
12 println!("Loading model: {model_path}");
13 let model = Model::load(&model_path, ComputeUnits::All).expect("Failed to load model");
14
15 // Create a simple input tensor
16 let input_data = vec![1.0f32, 2.0, 3.0, 4.0];
17 let tensor =
18 BorrowedTensor::from_f32(&input_data, &[1, 4]).expect("Failed to create tensor");
19
20 println!("Running prediction...");
21 let prediction = model.predict(&[("input", &tensor)]).expect("Prediction failed");
22
23 let (output, shape) = prediction.get_f32("output").expect("Failed to get output");
24 println!("Output shape: {shape:?}");
25 println!("Output data: {output:?}");
26}Sourcepub fn inputs(&self) -> Vec<FeatureDescription>
pub fn inputs(&self) -> Vec<FeatureDescription>
Get descriptions of all model inputs.
Examples found in repository?
7fn main() {
8 let model_path = std::env::args()
9 .nth(1)
10 .unwrap_or_else(|| "tests/fixtures/test_linear.mlmodelc".to_string());
11
12 println!("Loading model: {model_path}");
13 let model = Model::load(&model_path, ComputeUnits::All).expect("Failed to load model");
14
15 println!("\n--- Inputs ---");
16 for desc in model.inputs() {
17 println!(
18 " {}: {:?} shape={:?} optional={}",
19 desc.name(),
20 desc.feature_type(),
21 desc.shape(),
22 desc.is_optional(),
23 );
24 if let Some(dt) = desc.data_type() {
25 println!(" data_type: {dt}");
26 }
27 }
28
29 println!("\n--- Outputs ---");
30 for desc in model.outputs() {
31 println!(
32 " {}: {:?} shape={:?}",
33 desc.name(),
34 desc.feature_type(),
35 desc.shape(),
36 );
37 if let Some(dt) = desc.data_type() {
38 println!(" data_type: {dt}");
39 }
40 }
41
42 let meta = model.metadata();
43 println!("\n--- Metadata ---");
44 println!(" author: {:?}", meta.author);
45 println!(" description: {:?}", meta.description);
46 println!(" version: {:?}", meta.version);
47 println!(" license: {:?}", meta.license);
48}Sourcepub fn outputs(&self) -> Vec<FeatureDescription>
pub fn outputs(&self) -> Vec<FeatureDescription>
Get descriptions of all model outputs.
Examples found in repository?
7fn main() {
8 let model_path = std::env::args()
9 .nth(1)
10 .unwrap_or_else(|| "tests/fixtures/test_linear.mlmodelc".to_string());
11
12 println!("Loading model: {model_path}");
13 let model = Model::load(&model_path, ComputeUnits::All).expect("Failed to load model");
14
15 println!("\n--- Inputs ---");
16 for desc in model.inputs() {
17 println!(
18 " {}: {:?} shape={:?} optional={}",
19 desc.name(),
20 desc.feature_type(),
21 desc.shape(),
22 desc.is_optional(),
23 );
24 if let Some(dt) = desc.data_type() {
25 println!(" data_type: {dt}");
26 }
27 }
28
29 println!("\n--- Outputs ---");
30 for desc in model.outputs() {
31 println!(
32 " {}: {:?} shape={:?}",
33 desc.name(),
34 desc.feature_type(),
35 desc.shape(),
36 );
37 if let Some(dt) = desc.data_type() {
38 println!(" data_type: {dt}");
39 }
40 }
41
42 let meta = model.metadata();
43 println!("\n--- Metadata ---");
44 println!(" author: {:?}", meta.author);
45 println!(" description: {:?}", meta.description);
46 println!(" version: {:?}", meta.version);
47 println!(" license: {:?}", meta.license);
48}Sourcepub fn metadata(&self) -> ModelMetadata
pub fn metadata(&self) -> ModelMetadata
Get model metadata (author, description, version, license).
Examples found in repository?
7fn main() {
8 let model_path = std::env::args()
9 .nth(1)
10 .unwrap_or_else(|| "tests/fixtures/test_linear.mlmodelc".to_string());
11
12 println!("Loading model: {model_path}");
13 let model = Model::load(&model_path, ComputeUnits::All).expect("Failed to load model");
14
15 println!("\n--- Inputs ---");
16 for desc in model.inputs() {
17 println!(
18 " {}: {:?} shape={:?} optional={}",
19 desc.name(),
20 desc.feature_type(),
21 desc.shape(),
22 desc.is_optional(),
23 );
24 if let Some(dt) = desc.data_type() {
25 println!(" data_type: {dt}");
26 }
27 }
28
29 println!("\n--- Outputs ---");
30 for desc in model.outputs() {
31 println!(
32 " {}: {:?} shape={:?}",
33 desc.name(),
34 desc.feature_type(),
35 desc.shape(),
36 );
37 if let Some(dt) = desc.data_type() {
38 println!(" data_type: {dt}");
39 }
40 }
41
42 let meta = model.metadata();
43 println!("\n--- Metadata ---");
44 println!(" author: {:?}", meta.author);
45 println!(" description: {:?}", meta.description);
46 println!(" version: {:?}", meta.version);
47 println!(" license: {:?}", meta.license);
48}Sourcepub fn new_state(&self) -> Result<State>
pub fn new_state(&self) -> Result<State>
Create a new state for stateful prediction (macOS 15+ / iOS 18+).
Sourcepub fn predict_stateful(
&self,
inputs: &[(&str, &dyn AsMultiArray)],
state: &State,
) -> Result<Prediction>
pub fn predict_stateful( &self, inputs: &[(&str, &dyn AsMultiArray)], state: &State, ) -> Result<Prediction>
Run prediction with persistent state (macOS 15+ / iOS 18+).
Sourcepub fn predict_batch(&self, batch: &BatchProvider) -> Result<BatchPrediction>
pub fn predict_batch(&self, batch: &BatchProvider) -> Result<BatchPrediction>
Run batch prediction for multiple input sets at once.
More efficient than calling predict() in a loop.