speakrs 0.6.0

Fast Rust speaker diarization with pyannote-level accuracy and native CPU/CoreML/CUDA inference
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
use std::ffi::c_void;
use std::path::Path;
use std::ptr::NonNull;

use block2::RcBlock;
use objc2::rc::Retained;
use objc2::runtime::{AnyObject, ProtocolObject};
use objc2_core_ml::{MLComputeUnits, MLFeatureProvider, MLModel};
use objc2_foundation::{NSArray, NSCopying, NSError, NSMutableDictionary, NSNumber, NSString};

mod array;
mod path;
mod runtime;

use crate::inference::geometry::{CoreMlTensor, TensorLayout, TensorShapeError};
use array::{
    contiguous_strides, create_multi_array_cached_with_deallocator,
    create_multi_array_with_deallocator, extract_output, ns_number_array,
};

fn noop_deallocator() -> RcBlock<dyn Fn(NonNull<c_void>)> {
    RcBlock::new(|_ptr: NonNull<c_void>| {})
}
pub(crate) use path::{coreml_model_path, coreml_w8a16_model_path};
use runtime::{
    build_feature_provider, insert_input_feature, load_model, output_multi_array, predict_output,
};

#[derive(Debug, Clone, Copy)]
pub(crate) enum GpuPrecision {
    /// FP16 intermediate accumulations on GPU
    Low,
    /// Full FP32 accumulation on GPU
    #[expect(dead_code)]
    Full,
}

/// Errors returned by the native CoreML runtime
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum CoreMlError {
    /// A compiled `.mlmodelc` bundle could not be loaded
    #[error("CoreML load failed: {0}")]
    LoadFailed(String),
    /// CoreML rejected the inputs or failed while predicting
    #[error("CoreML prediction failed: {0}")]
    PredictionFailed(String),
    /// The prediction did not contain the named output
    #[error("CoreML output '{0}' not found")]
    OutputNotFound(String),
    /// An `MLMultiArray` could not be created for an input
    #[error("CoreML array creation failed: {0}")]
    ArrayCreationFailed(String),
    /// An input or output tensor did not match its declared shape
    #[error(transparent)]
    InvalidShape(#[from] TensorShapeError),
}

/// Pre-computed NSArray<NSNumber> for shape and strides, avoiding per-call allocation
pub(crate) struct CachedInputShape {
    name: Retained<NSString>,
    ns_shape: Retained<NSArray<NSNumber>>,
    ns_strides: Retained<NSArray<NSNumber>>,
    total_elements: usize,
}

impl CachedInputShape {
    pub fn new(name: &str, shape: &[usize]) -> Self {
        Self::try_new(name, shape).expect("CoreML input shape product must fit in usize")
    }

    pub fn try_new(name: &str, shape: &[usize]) -> Result<Self, CoreMlError> {
        let layout = TensorLayout::from_dims(shape, "coreml input")?;
        let ns_shape = ns_number_array(shape);
        let ns_strides = ns_number_array(&contiguous_strides(shape));

        Ok(Self {
            name: NSString::from_str(name),
            ns_shape,
            ns_strides,
            total_elements: layout.element_count(),
        })
    }

    /// Bind a flat buffer to this cached shape before native array construction
    pub fn bind<'a>(&'a self, data: &'a [f32]) -> Result<BoundCoreMlInput<'a>, CoreMlError> {
        crate::inference::geometry::require_exact_len(
            data.len(),
            self.total_elements,
            "coreml input",
        )?;
        Ok(BoundCoreMlInput { cached: self, data })
    }
}

/// Checked CoreML input slice paired with its cached shape
pub(crate) struct BoundCoreMlInput<'a> {
    pub cached: &'a CachedInputShape,
    pub data: &'a [f32],
}

// SAFETY: CachedInputShape fields are immutable after construction and only accessed via &self
unsafe impl Send for CachedInputShape {}
// SAFETY: CachedInputShape fields are immutable after construction and only accessed via &self
unsafe impl Sync for CachedInputShape {}

pub(crate) struct CoreMlModel {
    model: Retained<MLModel>,
    output_name: String,
    output_key: Retained<NSString>,
    noop_deallocator: RcBlock<dyn Fn(NonNull<c_void>)>,
    input_dict: Retained<NSMutableDictionary<NSString, AnyObject>>,
}

// SAFETY: CoreMlModel is only used from one thread at a time via &mut self
// SAFETY: MLModel prediction is thread-safe per Apple docs, and the remaining fields are only
// SAFETY: accessed inside predict calls that require exclusive access
unsafe impl Send for CoreMlModel {}

impl CoreMlModel {
    /// Load a compiled .mlmodelc bundle
    pub fn load(
        path: &Path,
        compute_units: MLComputeUnits,
        output_name: &str,
        gpu_precision: GpuPrecision,
    ) -> Result<Self, CoreMlError> {
        Ok(Self {
            model: load_model(path, compute_units, gpu_precision)?,
            output_key: NSString::from_str(output_name),
            noop_deallocator: noop_deallocator(),
            input_dict: NSMutableDictionary::new(),
            output_name: output_name.to_owned(),
        })
    }

    /// Run prediction with named inputs
    ///
    /// Each input is (name, shape, flat_data). The data slice must outlive this call.
    pub fn predict(
        &mut self,
        inputs: &[(&str, &[usize], &[f32])],
    ) -> Result<CoreMlTensor, CoreMlError> {
        self.input_dict.removeAllObjects();

        for &(name, shape, data) in inputs {
            let layout = TensorLayout::from_dims(shape, "coreml input")?;
            let bound = layout.bind(data)?;
            let multi_array =
                create_multi_array_with_deallocator(bound, shape, &self.noop_deallocator)?;
            let key = NSString::from_str(name);
            let key_copy: &ProtocolObject<dyn NSCopying> = ProtocolObject::from_ref(&*key);
            insert_input_feature(&self.input_dict, key_copy, &multi_array);
        }

        let provider = build_feature_provider(&self.input_dict)?;
        let input_ref: &ProtocolObject<dyn MLFeatureProvider> =
            ProtocolObject::from_ref(&*provider);
        let output_array =
            predict_output(&self.model, input_ref, &self.output_key, &self.output_name)?;
        extract_output(&output_array)
    }

    /// Run prediction with cached shape and stride objects.
    pub fn predict_cached(
        &mut self,
        inputs: &[(&CachedInputShape, &[f32])],
    ) -> Result<CoreMlTensor, CoreMlError> {
        self.input_dict.removeAllObjects();

        for &(cached, data) in inputs {
            let bound = cached.bind(data)?;
            let multi_array = create_multi_array_cached_with_deallocator(
                bound.data,
                bound.cached,
                &self.noop_deallocator,
            )?;
            let key_copy: &ProtocolObject<dyn NSCopying> = ProtocolObject::from_ref(&*cached.name);
            insert_input_feature(&self.input_dict, key_copy, &multi_array);
        }

        let provider = build_feature_provider(&self.input_dict)?;
        let input_ref: &ProtocolObject<dyn MLFeatureProvider> =
            ProtocolObject::from_ref(&*provider);
        let output_array =
            predict_output(&self.model, input_ref, &self.output_key, &self.output_name)?;
        extract_output(&output_array)
    }

    /// Default compute units. CoreML decides placement per operation.
    pub fn default_compute_units() -> MLComputeUnits {
        MLComputeUnits::All
    }
}

/// Thread-safe CoreML model wrapper that can be shared across threads.
///
/// Unlike `CoreMlModel`, this allocates a fresh input dictionary per call, so
/// prediction can take `&self`.
pub(crate) struct SharedCoreMlModel {
    model: Retained<MLModel>,
    output_name: String,
    output_key: Retained<NSString>,
}

// SAFETY: MLModel predictionFromFeatures is documented as thread-safe by Apple
// SAFETY: all per-call mutable state is allocated fresh inside predict_cached
unsafe impl Send for SharedCoreMlModel {}
// SAFETY: MLModel predictionFromFeatures is documented as thread-safe by Apple
// SAFETY: all per-call mutable state is allocated fresh inside predict_cached
unsafe impl Sync for SharedCoreMlModel {}

impl SharedCoreMlModel {
    /// Load a compiled .mlmodelc bundle
    pub fn load(
        path: &Path,
        compute_units: MLComputeUnits,
        output_name: &str,
        gpu_precision: GpuPrecision,
    ) -> Result<Self, CoreMlError> {
        Ok(Self {
            model: load_model(path, compute_units, gpu_precision)?,
            output_key: NSString::from_str(output_name),
            output_name: output_name.to_owned(),
        })
    }

    /// Run prediction with cached shape and stride objects.
    pub fn predict_cached(
        &self,
        inputs: &[(&CachedInputShape, &[f32])],
    ) -> Result<CoreMlTensor, CoreMlError> {
        let deallocator = noop_deallocator();
        let input_dict: Retained<NSMutableDictionary<NSString, AnyObject>> =
            NSMutableDictionary::new();

        for &(cached, data) in inputs {
            let bound = cached.bind(data)?;
            let multi_array =
                create_multi_array_cached_with_deallocator(bound.data, bound.cached, &deallocator)?;
            let key_copy: &ProtocolObject<dyn NSCopying> = ProtocolObject::from_ref(&*cached.name);
            insert_input_feature(&input_dict, key_copy, &multi_array);
        }

        let provider = build_feature_provider(&input_dict)?;
        let input_ref: &ProtocolObject<dyn MLFeatureProvider> =
            ProtocolObject::from_ref(&*provider);
        let output_array =
            predict_output(&self.model, input_ref, &self.output_key, &self.output_name)?;
        extract_output(&output_array)
    }

    /// Async prediction: queues work on ANE and returns via callback
    ///
    /// Uses predictionFromFeatures:completionHandler: which lets CoreML
    /// pipeline multiple predictions onto ANE simultaneously. Critical for
    /// concurrent ANE workers -- sync prediction serializes while async
    /// lets the ANE queue depth (127) fill up
    #[expect(dead_code)]
    pub fn predict_async(
        &self,
        inputs: &[(&CachedInputShape, &[f32])],
    ) -> Result<CoreMlTensor, CoreMlError> {
        let deallocator = noop_deallocator();
        let input_dict: Retained<NSMutableDictionary<NSString, AnyObject>> =
            NSMutableDictionary::new();

        for &(cached, data) in inputs {
            let bound = cached.bind(data)?;
            let multi_array =
                create_multi_array_cached_with_deallocator(bound.data, bound.cached, &deallocator)?;
            let key_copy: &ProtocolObject<dyn NSCopying> = ProtocolObject::from_ref(&*cached.name);
            insert_input_feature(&input_dict, key_copy, &multi_array);
        }

        let provider = build_feature_provider(&input_dict)?;
        let input_ref: &ProtocolObject<dyn MLFeatureProvider> =
            ProtocolObject::from_ref(&*provider);

        // bridge the async callback to a blocking channel
        let (tx, rx) = std::sync::mpsc::sync_channel::<
            Result<Retained<ProtocolObject<dyn MLFeatureProvider>>, String>,
        >(1);

        let completion = block2::RcBlock::new(
            move |output: *mut ProtocolObject<dyn MLFeatureProvider>, error: *mut NSError| {
                if !error.is_null() {
                    // SAFETY: error is non-null in this branch and points to the NSError passed
                    // SAFETY: by CoreML for the duration of the callback invocation
                    let err_msg = unsafe { (*error).localizedDescription() }.to_string();
                    let _ = tx.send(Err(err_msg));
                } else if output.is_null() {
                    let _ = tx.send(Err("nil output with no error".to_owned()));
                } else {
                    // SAFETY: output is non-null in this branch and retain extends the lifetime so
                    // SAFETY: the returned feature provider survives after the callback returns
                    let Some(retained) = (unsafe { Retained::retain(output) }) else {
                        let _ = tx.send(Err("failed to retain CoreML output".to_owned()));
                        return;
                    };
                    let _ = tx.send(Ok(retained));
                }
            },
        );

        // SAFETY: input_ref and completion stay alive for the duration of the Objective-C call and
        // SAFETY: CoreML copies/retains the callback before invoking it asynchronously
        unsafe {
            self.model
                .predictionFromFeatures_completionHandler(input_ref, &completion);
        }

        // block until the callback fires
        let output = rx
            .recv()
            .map_err(|_| CoreMlError::PredictionFailed("channel closed".to_owned()))?
            .map_err(CoreMlError::PredictionFailed)?;

        let output_array = output_multi_array(&output, &self.output_key, &self.output_name)?;
        extract_output(&output_array)
    }
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;
    use std::sync::Arc;

    use super::{CachedInputShape, CoreMlModel, GpuPrecision, SharedCoreMlModel};
    use crate::inference::geometry::TensorShapeError;

    #[test]
    fn bind_rejects_short_and_long_input_without_calling_coreml() {
        let cached = CachedInputShape::new("input", &[2, 2]);
        let short = match cached.bind(&[1.0, 2.0, 3.0]) {
            Ok(_) => panic!("expected short input to fail"),
            Err(error) => error,
        };
        let long = match cached.bind(&[1.0, 2.0, 3.0, 4.0, 5.0]) {
            Ok(_) => panic!("expected long input to fail"),
            Err(error) => error,
        };
        assert!(matches!(
            short,
            super::CoreMlError::InvalidShape(TensorShapeError::LengthMismatch {
                expected: 4,
                actual: 3,
                ..
            })
        ));
        assert!(matches!(
            long,
            super::CoreMlError::InvalidShape(TensorShapeError::LengthMismatch {
                expected: 4,
                actual: 5,
                ..
            })
        ));
        let exact = [1.0, 2.0, 3.0, 4.0];
        assert_eq!(cached.bind(&exact).unwrap().data, &exact);
    }

    #[test]
    fn try_new_rejects_shape_product_overflow() {
        let error = match CachedInputShape::try_new("input", &[usize::MAX, 2]) {
            Ok(_) => panic!("expected overflow to fail"),
            Err(error) => error,
        };
        assert!(matches!(
            error,
            super::CoreMlError::InvalidShape(TensorShapeError::Overflow { .. })
        ));
    }

    fn fixture_model_path(name: &str) -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("fixtures")
            .join("models")
            .join(name)
    }

    #[test]
    fn shared_cached_prediction_handles_concurrent_calls_when_bundle_available() {
        let model_path = fixture_model_path("segmentation-3.0.mlmodelc");
        if !model_path.exists() {
            eprintln!(
                "skipping CoreML cached prediction stress test; missing {}",
                model_path.display()
            );
            return;
        }

        let model = Arc::new(
            SharedCoreMlModel::load(
                &model_path,
                CoreMlModel::default_compute_units(),
                "output",
                GpuPrecision::Low,
            )
            .unwrap(),
        );
        let cached_shape = Arc::new(CachedInputShape::new("input", &[1, 1, 160_000]));
        let input = Arc::new(vec![0.0_f32; 160_000]);

        let handles: Vec<_> = (0..4)
            .map(|_| {
                let model = Arc::clone(&model);
                let cached_shape = Arc::clone(&cached_shape);
                let input = Arc::clone(&input);
                std::thread::spawn(move || {
                    for _ in 0..4 {
                        let tensor = model
                            .predict_cached(&[(&*cached_shape, input.as_slice())])
                            .unwrap();
                        assert_eq!(tensor.layout().dims().len(), 3);
                        assert!(!tensor.into_data().is_empty());
                    }
                })
            })
            .collect();

        for handle in handles {
            handle.join().unwrap();
        }
    }
}