coreml-native 0.2.0

Safe, ergonomic Rust bindings for Apple CoreML inference with ANE acceleration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//! Safe, ergonomic Rust bindings for Apple CoreML inference with ANE acceleration.
//!
//! # Platform Support
//!
//! Requires macOS or iOS. On non-Apple targets, types exist as stubs
//! returning `Error::UnsupportedPlatform`.

pub mod async_bridge;
pub mod compile;
pub mod description;
pub mod error;
pub(crate) mod ffi;
mod model_async;
pub mod state;
pub mod tensor;
pub mod batch;
pub mod compute;
pub mod model_lifecycle;
#[cfg(feature = "ndarray")]
pub mod ndarray_support;

pub use async_bridge::CompletionFuture;
pub use batch::{BatchPrediction, BatchProvider};
pub use compile::{compile_model, compile_model_async};
pub use compute::{available_devices, ComputeDevice};
pub use description::{FeatureDescription, FeatureType, ModelMetadata, ShapeConstraint};
pub use error::{Error, ErrorKind, Result};
pub use model_lifecycle::ModelHandle;
pub use state::State;
pub use tensor::{AsMultiArray, BorrowedTensor, DataType, OwnedTensor};
#[cfg(feature = "ndarray")]
pub use ndarray_support::PredictionNdarray;

/// Compute unit selection for CoreML model loading.
///
/// Default is `All` — uses CPU, GPU (Metal), and Apple Neural Engine
/// for maximum throughput. This is the whole point of native CoreML.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ComputeUnits {
    /// CPU only — no GPU or ANE.
    CpuOnly,
    /// CPU + GPU (Metal) — no ANE.
    CpuAndGpu,
    /// CPU + Apple Neural Engine — no GPU.
    CpuAndNeuralEngine,
    /// All available: CPU + GPU + ANE. **Use this.**
    #[default]
    All,
}

impl std::fmt::Display for ComputeUnits {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::CpuOnly => write!(f, "CPU only"),
            Self::CpuAndGpu => write!(f, "CPU + GPU"),
            Self::CpuAndNeuralEngine => write!(f, "CPU + Neural Engine"),
            Self::All => write!(f, "All (CPU + GPU + ANE)"),
        }
    }
}

// ─── Model ──────────────────────────────────────────────────────────────────

#[cfg(target_vendor = "apple")]
pub struct Model {
    inner: objc2::rc::Retained<objc2_core_ml::MLModel>,
    path: std::path::PathBuf,
}

#[cfg(target_vendor = "apple")]
impl std::fmt::Debug for Model {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Model").field("path", &self.path).finish()
    }
}

#[cfg(not(target_vendor = "apple"))]
#[derive(Debug)]
pub struct Model {
    _private: (),
}

// Apple documents MLModel.predictionFromFeatures as thread-safe for
// concurrent read-only predictions on the same model instance.
#[cfg(target_vendor = "apple")]
unsafe impl Send for Model {}
#[cfg(target_vendor = "apple")]
unsafe impl Sync for Model {}

impl Model {
    #[cfg(target_vendor = "apple")]
    pub fn load(path: impl AsRef<std::path::Path>, compute_units: ComputeUnits) -> Result<Self> {
        use objc2_core_ml::{MLComputeUnits, MLModel, MLModelConfiguration};

        let path = path.as_ref();
        let path_str = path.to_str().ok_or_else(|| {
            Error::new(ErrorKind::ModelLoad, "path contains non-UTF8 characters")
        })?;

        let url = objc2_foundation::NSURL::fileURLWithPath(&ffi::str_to_nsstring(path_str));
        let config = unsafe { MLModelConfiguration::new() };
        let ml_units = match compute_units {
            ComputeUnits::CpuOnly => MLComputeUnits(1),
            ComputeUnits::CpuAndGpu => MLComputeUnits::CPUAndGPU,
            ComputeUnits::CpuAndNeuralEngine => MLComputeUnits(2),
            ComputeUnits::All => MLComputeUnits::All,
        };
        unsafe { config.setComputeUnits(ml_units) };

        let inner = unsafe { MLModel::modelWithContentsOfURL_configuration_error(&url, &config) }
            .map_err(|e| Error::from_nserror(ErrorKind::ModelLoad, &e))?;

        Ok(Self { inner, path: path.to_path_buf() })
    }

    #[cfg(not(target_vendor = "apple"))]
    pub fn load(_path: impl AsRef<std::path::Path>, _compute_units: ComputeUnits) -> Result<Self> {
        Err(Error::new(ErrorKind::UnsupportedPlatform, "CoreML requires Apple platform"))
    }

    /// The filesystem path this model was loaded from.
    pub fn path(&self) -> &std::path::Path {
        #[cfg(target_vendor = "apple")]
        { &self.path }
        #[cfg(not(target_vendor = "apple"))]
        { std::path::Path::new("") }
    }

    /// Run a synchronous prediction with named input tensors.
    ///
    /// Accepts any type implementing `AsMultiArray` (both `BorrowedTensor` and `OwnedTensor`).
    #[cfg(target_vendor = "apple")]
    pub fn predict(&self, inputs: &[(&str, &dyn AsMultiArray)]) -> Result<Prediction> {
        use objc2::AnyThread;
        use objc2_core_ml::{MLDictionaryFeatureProvider, MLFeatureProvider, MLFeatureValue};
        use objc2_foundation::{NSDictionary, NSString};

        objc2::rc::autoreleasepool(|_pool| {
            let mut keys: Vec<objc2::rc::Retained<NSString>> = Vec::with_capacity(inputs.len());
            let mut vals: Vec<objc2::rc::Retained<MLFeatureValue>> = Vec::with_capacity(inputs.len());

            for &(name, tensor) in inputs {
                keys.push(ffi::str_to_nsstring(name));
                vals.push(unsafe { MLFeatureValue::featureValueWithMultiArray(tensor.as_ml_multi_array()) });
            }

            let key_refs: Vec<&NSString> = keys.iter().map(|k| &**k).collect();
            let val_refs: Vec<&MLFeatureValue> = vals.iter().map(|v| &**v).collect();

            let dict: objc2::rc::Retained<NSDictionary<NSString, MLFeatureValue>> =
                NSDictionary::from_slices(&key_refs, &val_refs);

            let dict_any: &NSDictionary<NSString, objc2::runtime::AnyObject> =
                unsafe { &*((&*dict) as *const NSDictionary<NSString, MLFeatureValue>
                    as *const NSDictionary<NSString, objc2::runtime::AnyObject>) };

            let provider = unsafe {
                MLDictionaryFeatureProvider::initWithDictionary_error(
                    MLDictionaryFeatureProvider::alloc(),
                    dict_any,
                )
            }
            .map_err(|e| Error::from_nserror(ErrorKind::Prediction, &e))?;

            let provider_ref: &objc2::runtime::ProtocolObject<dyn MLFeatureProvider> =
                objc2::runtime::ProtocolObject::from_ref(&*provider);

            let result = unsafe { self.inner.predictionFromFeatures_error(provider_ref) }
                .map_err(|e| Error::from_nserror(ErrorKind::Prediction, &e))?;

            Ok(Prediction { inner: result })
        })
    }

    #[cfg(not(target_vendor = "apple"))]
    pub fn predict(&self, _inputs: &[(&str, &dyn AsMultiArray)]) -> Result<Prediction> {
        Err(Error::new(ErrorKind::UnsupportedPlatform, "CoreML requires Apple platform"))
    }

    /// Get descriptions of all model inputs.
    #[cfg(target_vendor = "apple")]
    pub fn inputs(&self) -> Vec<FeatureDescription> {
        let desc = unsafe { self.inner.modelDescription() };
        let input_map = unsafe { desc.inputDescriptionsByName() };
        description::extract_features(&input_map)
    }

    /// Get descriptions of all model outputs.
    #[cfg(target_vendor = "apple")]
    pub fn outputs(&self) -> Vec<FeatureDescription> {
        let desc = unsafe { self.inner.modelDescription() };
        let output_map = unsafe { desc.outputDescriptionsByName() };
        description::extract_features(&output_map)
    }

    /// Get model metadata (author, description, version, license).
    #[cfg(target_vendor = "apple")]
    pub fn metadata(&self) -> ModelMetadata {
        let desc = unsafe { self.inner.modelDescription() };
        description::extract_metadata(&desc)
    }

    /// Create a new state for stateful prediction (macOS 15+ / iOS 18+).
    #[cfg(target_vendor = "apple")]
    pub fn new_state(&self) -> Result<State> {
        let inner = unsafe { self.inner.newState() };
        Ok(State { inner })
    }

    #[cfg(not(target_vendor = "apple"))]
    pub fn new_state(&self) -> Result<State> {
        Err(Error::new(ErrorKind::UnsupportedPlatform, "CoreML requires Apple platform"))
    }

    /// Run prediction with persistent state (macOS 15+ / iOS 18+).
    #[cfg(target_vendor = "apple")]
    pub fn predict_stateful(
        &self,
        inputs: &[(&str, &dyn AsMultiArray)],
        state: &State,
    ) -> Result<Prediction> {
        use objc2::AnyThread;
        use objc2_core_ml::{MLDictionaryFeatureProvider, MLFeatureProvider, MLFeatureValue};
        use objc2_foundation::{NSDictionary, NSString};

        objc2::rc::autoreleasepool(|_pool| {
            let mut keys: Vec<objc2::rc::Retained<NSString>> = Vec::with_capacity(inputs.len());
            let mut vals: Vec<objc2::rc::Retained<MLFeatureValue>> = Vec::with_capacity(inputs.len());

            for &(name, tensor) in inputs {
                keys.push(ffi::str_to_nsstring(name));
                vals.push(unsafe { MLFeatureValue::featureValueWithMultiArray(tensor.as_ml_multi_array()) });
            }

            let key_refs: Vec<&NSString> = keys.iter().map(|k| &**k).collect();
            let val_refs: Vec<&MLFeatureValue> = vals.iter().map(|v| &**v).collect();
            let dict: objc2::rc::Retained<NSDictionary<NSString, MLFeatureValue>> =
                NSDictionary::from_slices(&key_refs, &val_refs);
            let dict_any: &NSDictionary<NSString, objc2::runtime::AnyObject> =
                unsafe { &*((&*dict) as *const NSDictionary<NSString, MLFeatureValue>
                    as *const NSDictionary<NSString, objc2::runtime::AnyObject>) };

            let provider = unsafe {
                MLDictionaryFeatureProvider::initWithDictionary_error(
                    MLDictionaryFeatureProvider::alloc(), dict_any,
                )
            }
            .map_err(|e| Error::from_nserror(ErrorKind::Prediction, &e))?;

            let provider_ref: &objc2::runtime::ProtocolObject<dyn MLFeatureProvider> =
                objc2::runtime::ProtocolObject::from_ref(&*provider);

            let result = unsafe {
                self.inner.predictionFromFeatures_usingState_error(provider_ref, &state.inner)
            }
            .map_err(|e| Error::from_nserror(ErrorKind::Prediction, &e))?;

            Ok(Prediction { inner: result })
        })
    }

    #[cfg(not(target_vendor = "apple"))]
    pub fn predict_stateful(
        &self,
        _inputs: &[(&str, &dyn AsMultiArray)],
        _state: &State,
    ) -> Result<Prediction> {
        Err(Error::new(ErrorKind::UnsupportedPlatform, "CoreML requires Apple platform"))
    }

    /// Run batch prediction for multiple input sets at once.
    ///
    /// More efficient than calling `predict()` in a loop.
    #[cfg(target_vendor = "apple")]
    pub fn predict_batch(&self, batch: &batch::BatchProvider) -> Result<batch::BatchPrediction> {
        use objc2_core_ml::MLBatchProvider;

        let batch_ref: &objc2::runtime::ProtocolObject<dyn MLBatchProvider> =
            objc2::runtime::ProtocolObject::from_ref(&*batch.inner);

        let result = unsafe { self.inner.predictionsFromBatch_error(batch_ref) }
            .map_err(|e| Error::from_nserror(ErrorKind::Prediction, &e))?;

        Ok(batch::BatchPrediction { inner: result })
    }

    #[cfg(not(target_vendor = "apple"))]
    pub fn predict_batch(&self, _batch: &batch::BatchProvider) -> Result<batch::BatchPrediction> {
        Err(Error::new(ErrorKind::UnsupportedPlatform, "CoreML requires Apple platform"))
    }

    #[cfg(not(target_vendor = "apple"))]
    pub fn inputs(&self) -> Vec<FeatureDescription> { vec![] }

    #[cfg(not(target_vendor = "apple"))]
    pub fn outputs(&self) -> Vec<FeatureDescription> { vec![] }

    #[cfg(not(target_vendor = "apple"))]
    pub fn metadata(&self) -> ModelMetadata { ModelMetadata::default() }
}

// ─── Prediction result ──────────────────────────────────────────────────────

#[cfg(target_vendor = "apple")]
pub struct Prediction {
    inner: objc2::rc::Retained<objc2::runtime::ProtocolObject<dyn objc2_core_ml::MLFeatureProvider>>,
}

#[cfg(not(target_vendor = "apple"))]
pub struct Prediction {
    _private: (),
}

// The Prediction holds a Retained MLFeatureProvider which is reference-counted.
// Safe to move to another thread for output extraction.
#[cfg(target_vendor = "apple")]
unsafe impl Send for Prediction {}
#[cfg(target_vendor = "apple")]
unsafe impl Sync for Prediction {}

impl Prediction {
    /// Get an output as (Vec<f32>, shape), converting from the model's native data type.
    /// Allocates a new Vec for the output data.
    #[cfg(target_vendor = "apple")]
    #[allow(deprecated)]
    pub fn get_f32(&self, name: &str) -> Result<(Vec<f32>, Vec<usize>)> {
        objc2::rc::autoreleasepool(|_pool| {
            let (count, shape, data_type, array) = self.get_output_array(name)?;
            let mut buf = vec![0.0f32; count];
            Self::copy_array_to_f32(&array, data_type, count, &mut buf)?;
            Ok((buf, shape))
        })
    }

    /// Copy an output into a caller-provided f32 buffer (zero-alloc hot path).
    ///
    /// Returns the shape. The buffer must be large enough to hold all elements.
    #[cfg(target_vendor = "apple")]
    #[allow(deprecated)]
    pub fn get_f32_into(&self, name: &str, buf: &mut [f32]) -> Result<Vec<usize>> {
        objc2::rc::autoreleasepool(|_pool| {
            let (count, shape, data_type, array) = self.get_output_array(name)?;
            if buf.len() < count {
                return Err(Error::new(
                    ErrorKind::InvalidShape,
                    format!("buffer length {} < output element count {count}", buf.len()),
                ));
            }
            Self::copy_array_to_f32(&array, data_type, count, buf)?;
            Ok(shape)
        })
    }

    #[cfg(target_vendor = "apple")]
    #[allow(deprecated)]
    #[allow(clippy::type_complexity)]
    fn get_output_array(
        &self,
        name: &str,
    ) -> Result<(
        usize,
        Vec<usize>,
        Option<DataType>,
        objc2::rc::Retained<objc2_core_ml::MLMultiArray>,
    )> {
        use objc2_core_ml::MLFeatureProvider;

        let ns_name = ffi::str_to_nsstring(name);
        let feature_val = unsafe { self.inner.featureValueForName(&ns_name) }.ok_or_else(|| {
            Error::new(ErrorKind::Prediction, format!("output '{name}' not found"))
        })?;

        let array = unsafe { feature_val.multiArrayValue() }.ok_or_else(|| {
            Error::new(ErrorKind::Prediction, format!("output '{name}' is not a multi-array"))
        })?;

        let shape = ffi::nsarray_to_shape(unsafe { &array.shape() });
        let count = tensor::element_count(&shape);
        let dt_raw = unsafe { array.dataType() };
        let data_type = ffi::ml_to_datatype(dt_raw.0);

        Ok((count, shape, data_type, array))
    }

    /// Copy MLMultiArray data into a flat f32 buffer in row-major (C-contiguous) order.
    ///
    /// CoreML MLMultiArray outputs may have non-row-major strides (especially when
    /// inference runs on GPU or ANE). This function reads the array's actual strides
    /// and iterates in logical (row-major) index order, computing the physical offset
    /// for each element using the strides.
    #[cfg(target_vendor = "apple")]
    #[allow(deprecated)]
    #[allow(clippy::needless_range_loop)]
    fn copy_array_to_f32(
        array: &objc2_core_ml::MLMultiArray,
        data_type: Option<DataType>,
        count: usize,
        buf: &mut [f32],
    ) -> Result<()> {
        unsafe {
            let ptr = array.dataPointer();
            let shape = ffi::nsarray_to_shape(&array.shape());
            let strides = ffi::nsarray_to_shape(&array.strides());
            let row_major_strides = tensor::compute_strides(&shape);
            let is_contiguous = strides == row_major_strides;

            if is_contiguous {
                // Fast path: data is already row-major contiguous
                match data_type {
                    Some(DataType::Float32) => {
                        let src = ptr.as_ptr() as *const f32;
                        std::ptr::copy_nonoverlapping(src, buf.as_mut_ptr(), count);
                    }
                    Some(DataType::Float16) => {
                        let src = ptr.as_ptr() as *const u16;
                        for i in 0..count {
                            buf[i] = f16_to_f32(*src.add(i));
                        }
                    }
                    Some(DataType::Float64) => {
                        let src = ptr.as_ptr() as *const f64;
                        for i in 0..count {
                            buf[i] = *src.add(i) as f32;
                        }
                    }
                    Some(DataType::Int32) => {
                        let src = ptr.as_ptr() as *const i32;
                        for i in 0..count {
                            buf[i] = *src.add(i) as f32;
                        }
                    }
                    Some(DataType::Int16) => {
                        let src = ptr.as_ptr() as *const i16;
                        for i in 0..count {
                            buf[i] = *src.add(i) as f32;
                        }
                    }
                    Some(DataType::Int8) => {
                        let src = ptr.as_ptr() as *const i8;
                        for i in 0..count {
                            buf[i] = *src.add(i) as f32;
                        }
                    }
                    Some(DataType::UInt32) => {
                        let src = ptr.as_ptr() as *const u32;
                        for i in 0..count {
                            buf[i] = *src.add(i) as f32;
                        }
                    }
                    Some(DataType::UInt16) => {
                        let src = ptr.as_ptr() as *const u16;
                        for i in 0..count {
                            buf[i] = *src.add(i) as f32;
                        }
                    }
                    Some(DataType::UInt8) => {
                        let src = ptr.as_ptr() as *const u8;
                        for i in 0..count {
                            buf[i] = *src.add(i) as f32;
                        }
                    }
                    None => {
                        return Err(Error::new(
                            ErrorKind::Prediction,
                            "unsupported output data type",
                        ));
                    }
                }
            } else {
                // Slow path: non-contiguous strides — iterate in logical row-major order,
                // compute physical offset for each element using the actual strides.
                let ndims = shape.len();
                let mut indices = vec![0usize; ndims];

                macro_rules! strided_copy {
                    ($src_type:ty, $convert:expr) => {{
                        let src = ptr.as_ptr() as *const $src_type;
                        for logical_idx in 0..count {
                            let physical: usize = indices.iter()
                                .zip(strides.iter())
                                .map(|(&i, &s)| i * s)
                                .sum();
                            buf[logical_idx] = $convert(*src.add(physical));
                            // Increment indices in row-major order (last dim fastest)
                            for d in (0..ndims).rev() {
                                indices[d] += 1;
                                if indices[d] < shape[d] {
                                    break;
                                }
                                indices[d] = 0;
                            }
                        }
                    }};
                }

                match data_type {
                    Some(DataType::Float32) => strided_copy!(f32, |v: f32| v),
                    Some(DataType::Float16) => strided_copy!(u16, |v: u16| f16_to_f32(v)),
                    Some(DataType::Float64) => strided_copy!(f64, |v: f64| v as f32),
                    Some(DataType::Int32)   => strided_copy!(i32, |v: i32| v as f32),
                    Some(DataType::Int16)   => strided_copy!(i16, |v: i16| v as f32),
                    Some(DataType::Int8)    => strided_copy!(i8,  |v: i8|  v as f32),
                    Some(DataType::UInt32)  => strided_copy!(u32, |v: u32| v as f32),
                    Some(DataType::UInt16)  => strided_copy!(u16, |v: u16| v as f32),
                    Some(DataType::UInt8)   => strided_copy!(u8,  |v: u8|  v as f32),
                    None => {
                        return Err(Error::new(
                            ErrorKind::Prediction,
                            "unsupported output data type",
                        ));
                    }
                }
            }
        }
        Ok(())
    }

    #[cfg(not(target_vendor = "apple"))]
    pub fn get_f32(&self, _name: &str) -> Result<(Vec<f32>, Vec<usize>)> {
        Err(Error::new(ErrorKind::UnsupportedPlatform, "CoreML requires Apple platform"))
    }

    #[cfg(not(target_vendor = "apple"))]
    pub fn get_f32_into(&self, _name: &str, _buf: &mut [f32]) -> Result<Vec<usize>> {
        Err(Error::new(ErrorKind::UnsupportedPlatform, "CoreML requires Apple platform"))
    }

    /// Get an output as (Vec<i32>, shape). Only works if the output is Int32.
    #[cfg(target_vendor = "apple")]
    #[allow(deprecated)]
    pub fn get_i32(&self, name: &str) -> Result<(Vec<i32>, Vec<usize>)> {
        objc2::rc::autoreleasepool(|_pool| {
            let (count, shape, data_type, array) = self.get_output_array(name)?;
            match data_type {
                Some(DataType::Int32) => {
                    let mut buf = vec![0i32; count];
                    unsafe {
                        let ptr = array.dataPointer();
                        let src = ptr.as_ptr() as *const i32;
                        std::ptr::copy_nonoverlapping(src, buf.as_mut_ptr(), count);
                    }
                    Ok((buf, shape))
                }
                Some(dt) => Err(Error::new(
                    ErrorKind::Prediction,
                    format!("output '{name}' is {dt}, not Int32"),
                )),
                None => Err(Error::new(ErrorKind::Prediction, "unsupported output data type")),
            }
        })
    }

    /// Get an output as (Vec<f64>, shape). Only works if the output is Float64.
    #[cfg(target_vendor = "apple")]
    #[allow(deprecated)]
    pub fn get_f64(&self, name: &str) -> Result<(Vec<f64>, Vec<usize>)> {
        objc2::rc::autoreleasepool(|_pool| {
            let (count, shape, data_type, array) = self.get_output_array(name)?;
            match data_type {
                Some(DataType::Float64) => {
                    let mut buf = vec![0.0f64; count];
                    unsafe {
                        let ptr = array.dataPointer();
                        let src = ptr.as_ptr() as *const f64;
                        std::ptr::copy_nonoverlapping(src, buf.as_mut_ptr(), count);
                    }
                    Ok((buf, shape))
                }
                Some(dt) => Err(Error::new(
                    ErrorKind::Prediction,
                    format!("output '{name}' is {dt}, not Float64"),
                )),
                None => Err(Error::new(ErrorKind::Prediction, "unsupported output data type")),
            }
        })
    }

    /// Get an output as raw bytes and its shape + data type.
    #[cfg(target_vendor = "apple")]
    #[allow(deprecated)]
    pub fn get_raw(&self, name: &str) -> Result<(Vec<u8>, Vec<usize>, Option<DataType>)> {
        objc2::rc::autoreleasepool(|_pool| {
            let (count, shape, data_type, array) = self.get_output_array(name)?;
            let byte_size = data_type.map(|dt| dt.byte_size()).unwrap_or(4);
            let total_bytes = count * byte_size;
            let mut buf = vec![0u8; total_bytes];
            unsafe {
                let ptr = array.dataPointer();
                std::ptr::copy_nonoverlapping(
                    ptr.as_ptr() as *const u8,
                    buf.as_mut_ptr(),
                    total_bytes,
                );
            }
            Ok((buf, shape, data_type))
        })
    }

    #[cfg(not(target_vendor = "apple"))]
    pub fn get_i32(&self, _name: &str) -> Result<(Vec<i32>, Vec<usize>)> {
        Err(Error::new(ErrorKind::UnsupportedPlatform, "CoreML requires Apple platform"))
    }

    #[cfg(not(target_vendor = "apple"))]
    pub fn get_f64(&self, _name: &str) -> Result<(Vec<f64>, Vec<usize>)> {
        Err(Error::new(ErrorKind::UnsupportedPlatform, "CoreML requires Apple platform"))
    }

    #[cfg(not(target_vendor = "apple"))]
    pub fn get_raw(&self, _name: &str) -> Result<(Vec<u8>, Vec<usize>, Option<DataType>)> {
        Err(Error::new(ErrorKind::UnsupportedPlatform, "CoreML requires Apple platform"))
    }
}

/// Convert a half-precision float (u16 bits) to f32.
#[cfg(target_vendor = "apple")]
fn f16_to_f32(bits: u16) -> f32 {
    let sign = ((bits >> 15) & 1) as u32;
    let exp = ((bits >> 10) & 0x1f) as u32;
    let frac = (bits & 0x3ff) as u32;

    if exp == 0 {
        if frac == 0 {
            f32::from_bits(sign << 31)
        } else {
            let mut e = 0i32;
            let mut f = frac;
            while (f & 0x400) == 0 {
                f <<= 1;
                e -= 1;
            }
            f &= 0x3ff;
            let exp32 = (127 - 15 + 1 + e) as u32;
            f32::from_bits((sign << 31) | (exp32 << 23) | (f << 13))
        }
    } else if exp == 31 {
        if frac == 0 {
            f32::from_bits((sign << 31) | (0xff << 23))
        } else {
            f32::from_bits((sign << 31) | (0xff << 23) | (frac << 13))
        }
    } else {
        let exp32 = exp + (127 - 15);
        f32::from_bits((sign << 31) | (exp32 << 23) | (frac << 13))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn compute_units_default_is_all() {
        assert_eq!(ComputeUnits::default(), ComputeUnits::All);
    }

    #[test]
    fn compute_units_display() {
        assert_eq!(format!("{}", ComputeUnits::CpuAndGpu), "CPU + GPU");
        assert_eq!(format!("{}", ComputeUnits::All), "All (CPU + GPU + ANE)");
    }

    #[test]
    fn compute_units_display_cpu_only() {
        assert_eq!(format!("{}", ComputeUnits::CpuOnly), "CPU only");
    }

    #[cfg(not(target_vendor = "apple"))]
    #[test]
    fn model_load_fails_on_non_apple() {
        let err = Model::load("/tmp/fake.mlmodelc", ComputeUnits::All).unwrap_err();
        assert_eq!(err.kind(), &ErrorKind::UnsupportedPlatform);
    }
}