candle-coreml 0.3.1

CoreML inference engine for Candle tensors - provides Apple CoreML/ANE integration with real tokenization, safety fixes, and model calibration awareness
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
//! Tensor conversion utilities for CoreML integration

use candle_core::{Device, Error as CandleError, Tensor};

#[cfg(target_os = "macos")]
use half::f16;
#[cfg(target_os = "macos")]
use tracing::trace;

#[cfg(target_os = "macos")]
use objc2_core_ml::{MLMultiArray, MLMultiArrayDataType};

#[cfg(target_os = "macos")]
use block2::StackBlock;
#[cfg(target_os = "macos")]
use objc2::rc::{autoreleasepool, Retained};
#[cfg(target_os = "macos")]
use objc2::runtime::{AnyObject, ProtocolObject};
#[cfg(target_os = "macos")]
use objc2::AnyThread;
#[cfg(target_os = "macos")]
use objc2_core_ml::{MLDictionaryFeatureProvider, MLFeatureProvider, MLFeatureValue};
#[cfg(target_os = "macos")]
use objc2_foundation::{NSArray, NSDictionary, NSNumber, NSString};

/// Extract tensor data from MLMultiArray with proper type handling
/// This function handles all the different MLMultiArray data types including Float16
#[cfg(target_os = "macos")]
pub fn convert_mlmultiarray_to_tensor(
    marray: &MLMultiArray,
    input_device: &Device,
) -> Result<Tensor, CandleError> {
    // Get shape
    let shape_nsarray = unsafe { marray.shape() };
    let mut shape = Vec::with_capacity(shape_nsarray.count());
    for i in 0..shape_nsarray.count() {
        let dim_number = shape_nsarray.objectAtIndex(i);
        let dim_value = dim_number.integerValue() as usize;
        shape.push(dim_value);
    }

    // Extract data with proper type handling
    let data_type = unsafe { marray.dataType() };
    let count = unsafe { marray.count() as usize };
    let mut buf = Vec::with_capacity(count);

    match data_type {
        MLMultiArrayDataType::Float32 => {
            // Extract as float values
            for i in 0..count {
                let val = unsafe { marray.objectAtIndexedSubscript(i as isize) }.floatValue();
                buf.push(val);
            }
        }
        MLMultiArrayDataType::Int32 => {
            // Extract as integer values and convert to float
            for i in 0..count {
                let val = unsafe { marray.objectAtIndexedSubscript(i as isize) }.intValue() as f32;
                buf.push(val);
            }
        }
        MLMultiArrayDataType::Double => {
            // Extract as double values and convert to float
            for i in 0..count {
                let val =
                    unsafe { marray.objectAtIndexedSubscript(i as isize) }.doubleValue() as f32;
                buf.push(val);
            }
        }
        // Prefer explicit Float16 when available
        MLMultiArrayDataType::Float16 => {
            trace!("Detected Float16 data type, using half-precision conversion");
            // SAFETY: We trust CoreML to provide a valid buffer of size count * 2 bytes
            // for Float16 MLMultiArray. We only read within these bounds and never write.
            unsafe {
                #[allow(deprecated)]
                let data_ptr = marray.dataPointer();
                let byte_slice =
                    std::slice::from_raw_parts(data_ptr.as_ptr().cast::<u8>(), count * 2);
                for i in 0..count {
                    let byte_offset = i * 2;
                    let f16_bytes = [byte_slice[byte_offset], byte_slice[byte_offset + 1]];
                    let f16_bits = u16::from_le_bytes(f16_bytes);
                    let f16_val = f16::from_bits(f16_bits);
                    buf.push(f16_val.to_f32());
                }
            }
        }
        _ => {
            // For other unknown types, try floatValue as fallback
            trace!(
                "Unknown MLMultiArray data type: {:?}, using floatValue fallback",
                data_type
            );
            for i in 0..count {
                let val = unsafe { marray.objectAtIndexedSubscript(i as isize) }.floatValue();
                buf.push(val);
            }
        }
    }

    Tensor::from_vec(buf, shape, input_device)
}
#[cfg(target_os = "macos")]
use std::sync::atomic::{AtomicBool, Ordering};

/// Tensor to MLMultiArray conversion
#[cfg(target_os = "macos")]
pub fn tensor_to_mlmultiarray(tensor: &Tensor) -> Result<Retained<MLMultiArray>, CandleError> {
    use candle_core::DType;

    let contiguous_tensor = if tensor.is_contiguous() {
        tensor.clone()
    } else {
        tensor.contiguous()?
    };

    let element_count = tensor.elem_count();
    let dims = tensor.dims();
    let mut shape = Vec::with_capacity(dims.len());
    for &dim in dims {
        shape.push(NSNumber::new_usize(dim));
    }
    let shape_nsarray = NSArray::from_retained_slice(&shape);

    // Choose MLMultiArrayDataType based on tensor dtype
    let (ml_data_type, element_size) = match tensor.dtype() {
        DType::F32 => (MLMultiArrayDataType::Float32, std::mem::size_of::<f32>()),
        DType::I64 => (MLMultiArrayDataType::Int32, std::mem::size_of::<i32>()), // Convert I64 to Int32
        _ => {
            return Err(CandleError::Msg(format!(
                "Unsupported tensor dtype {:?} for CoreML conversion. Only F32 and I64 tensors are supported.",
                tensor.dtype()
            )))
        }
    };

    let multi_array_result = unsafe {
        MLMultiArray::initWithShape_dataType_error(
            MLMultiArray::alloc(),
            &shape_nsarray,
            ml_data_type,
        )
    };

    match multi_array_result {
        Ok(ml_array) => {
            let copied = AtomicBool::new(false);

            let flattened_tensor = contiguous_tensor.flatten_all()?;

            // Handle different data types
            match tensor.dtype() {
                DType::F32 => {
                    let data_vec = flattened_tensor.to_vec1::<f32>()?;
                    unsafe {
                        ml_array.getMutableBytesWithHandler(&StackBlock::new(
                            |ptr: std::ptr::NonNull<std::ffi::c_void>, len, _| {
                                let dst = ptr.as_ptr() as *mut f32;
                                let src = data_vec.as_ptr();
                                let copy_elements = element_count.min(len as usize / element_size);

                                if copy_elements > 0 && len as usize >= copy_elements * element_size
                                {
                                    // SAFETY: The CoreML callback provides a valid writable buffer of length `len` bytes.
                                    // We compute `copy_elements` to ensure we never copy beyond either the source or dest.
                                    std::ptr::copy_nonoverlapping(src, dst, copy_elements);
                                    copied.store(true, Ordering::Relaxed);
                                }
                            },
                        ));
                    }
                }
                DType::I64 => {
                    // Convert I64 to I32 for CoreML
                    let data_vec = flattened_tensor.to_vec1::<i64>()?;
                    let i32_data: Vec<i32> = data_vec.into_iter().map(|x| x as i32).collect();

                    unsafe {
                        ml_array.getMutableBytesWithHandler(&StackBlock::new(
                            |ptr: std::ptr::NonNull<std::ffi::c_void>, len, _| {
                                let dst = ptr.as_ptr() as *mut i32;
                                let src = i32_data.as_ptr();
                                let copy_elements = element_count.min(len as usize / element_size);

                                if copy_elements > 0 && len as usize >= copy_elements * element_size
                                {
                                    // SAFETY: As above in F32 path, bounds are enforced and buffers are valid for `copy_elements`.
                                    std::ptr::copy_nonoverlapping(src, dst, copy_elements);
                                    copied.store(true, Ordering::Relaxed);
                                }
                            },
                        ));
                    }
                }
                _ => unreachable!(), // Already handled above
            }

            if copied.load(Ordering::Relaxed) {
                Ok(ml_array)
            } else {
                Err(CandleError::Msg(
                    "Failed to copy data to MLMultiArray".to_string(),
                ))
            }
        }
        Err(err) => Err(CandleError::Msg(format!(
            "Failed to create MLMultiArray: {err:?}"
        ))),
    }
}

/// Create feature provider with multiple named inputs
#[cfg(target_os = "macos")]
pub fn create_multi_feature_provider(
    input_names: &[String],
    input_arrays: &[Retained<MLMultiArray>],
) -> Result<Retained<MLDictionaryFeatureProvider>, CandleError> {
    autoreleasepool(|_| {
        let mut keys = Vec::with_capacity(input_names.len());
        let mut values: Vec<Retained<MLFeatureValue>> = Vec::with_capacity(input_arrays.len());

        for (name, array) in input_names.iter().zip(input_arrays.iter()) {
            let key = NSString::from_str(name);
            // SAFETY: `array` is a valid MLMultiArray retained object created by us; CoreML will retain it as needed.
            let value = unsafe { MLFeatureValue::featureValueWithMultiArray(array) };
            keys.push(key);
            values.push(value);
        }

        let key_refs: Vec<&NSString> = keys.iter().map(|k| &**k).collect();
        let value_refs: Vec<&AnyObject> = values.iter().map(|v| v.as_ref() as &AnyObject).collect();
        let dict: Retained<NSDictionary<NSString, AnyObject>> =
            NSDictionary::from_slices::<NSString>(&key_refs, &value_refs);

        unsafe {
            MLDictionaryFeatureProvider::initWithDictionary_error(
                MLDictionaryFeatureProvider::alloc(),
                dict.as_ref(),
            )
        }
        .map_err(|e| CandleError::Msg(format!("CoreML initWithDictionary_error: {e:?}")))
    })
}

/// Extract output tensor from CoreML prediction result
/// Extract all outputs from a CoreML prediction
///
/// This is useful for models with multiple outputs, such as the Qwen LM head
/// which produces 16 different logits chunks.
#[cfg(target_os = "macos")]
pub fn extract_all_outputs(
    prediction: &ProtocolObject<dyn MLFeatureProvider>,
    input_device: &Device,
) -> Result<std::collections::HashMap<String, Tensor>, CandleError> {
    autoreleasepool(|pool| {
        let mut outputs = std::collections::HashMap::new();

        let feature_names = unsafe { prediction.featureNames() };
        let feature_names_iter = feature_names.iter();

        for feature_name in feature_names_iter {
            let feature_name_str = unsafe { feature_name.to_str(pool) };

            let value =
                unsafe { prediction.featureValueForName(&feature_name) }.ok_or_else(|| {
                    CandleError::Msg(format!("Output '{feature_name_str}' not found"))
                })?;

            let marray = unsafe { value.multiArrayValue() }.ok_or_else(|| {
                CandleError::Msg(format!("Output '{feature_name_str}' is not MLMultiArray"))
            })?;

            // Get shape
            let shape_nsarray = unsafe { marray.shape() };
            let mut shape = Vec::with_capacity(shape_nsarray.count());
            for i in 0..shape_nsarray.count() {
                let dim_number = shape_nsarray.objectAtIndex(i);
                let dim_value = dim_number.integerValue() as usize;
                shape.push(dim_value);
            }

            // Use the shared conversion function with proper Float16 handling
            let tensor = convert_mlmultiarray_to_tensor(&marray, input_device).map_err(|e| {
                CandleError::Msg(format!(
                    "Failed to create output tensor '{feature_name_str}': {e}"
                ))
            })?;

            outputs.insert(feature_name_str.to_string(), tensor);
        }

        Ok(outputs)
    })
}

#[cfg(target_os = "macos")]
pub fn extract_output(
    prediction: &ProtocolObject<dyn MLFeatureProvider>,
    output_name: &str,
    input_device: &Device,
) -> Result<Tensor, CandleError> {
    use objc2_foundation::NSString;

    autoreleasepool(|_| {
        let name = NSString::from_str(output_name);
        let value = unsafe { prediction.featureValueForName(&name) }
            .ok_or_else(|| CandleError::Msg(format!("Output '{output_name}' not found")))?;

        let marray = unsafe { value.multiArrayValue() }.ok_or_else(|| {
            CandleError::Msg(format!("Output '{output_name}' is not MLMultiArray"))
        })?;

        // Use the shared conversion function with proper Float16 handling
        convert_mlmultiarray_to_tensor(&marray, input_device)
    })
}

// Non-macOS fallback implementations
// These functions provide stub implementations that return appropriate errors
// when CoreML functionality is not available on the target platform

#[cfg(not(target_os = "macos"))]
/// Placeholder type for MLMultiArray on non-macOS platforms
pub type MLMultiArrayStub = ();

#[cfg(not(target_os = "macos"))]
/// Placeholder type for MLDictionaryFeatureProvider on non-macOS platforms  
pub type MLFeatureProviderStub = ();

#[cfg(not(target_os = "macos"))]
pub fn tensor_to_mlmultiarray(_tensor: &Tensor) -> Result<MLMultiArrayStub, CandleError> {
    Err(CandleError::Msg(
        "CoreML tensor conversion is only available on macOS".to_string(),
    ))
}

#[cfg(not(target_os = "macos"))]
pub fn create_multi_feature_provider(
    _input_names: &[String],
    _arrays: &[MLMultiArrayStub],
) -> Result<MLFeatureProviderStub, CandleError> {
    Err(CandleError::Msg(
        "CoreML feature provider is only available on macOS".to_string(),
    ))
}

#[cfg(not(target_os = "macos"))]
pub fn extract_output(
    _prediction: &MLFeatureProviderStub,
    _output_name: &str,
    _input_device: &Device,
) -> Result<Tensor, CandleError> {
    Err(CandleError::Msg(
        "CoreML extraction is only available on macOS".to_string(),
    ))
}

#[cfg(not(target_os = "macos"))]
pub fn extract_all_outputs(
    _prediction: &MLFeatureProviderStub,
    _input_device: &Device,
) -> Result<std::collections::HashMap<String, Tensor>, CandleError> {
    Err(CandleError::Msg(
        "CoreML extraction is only available on macOS".to_string(),
    ))
}

#[cfg(test)]
mod tests {
    #[cfg(target_os = "macos")]
    mod macos_tests {
        use crate::conversion::convert_mlmultiarray_to_tensor;
        use candle_core::Device;
        use objc2::rc::Retained;
        use objc2::AnyThread;
        use objc2_core_ml::{MLMultiArray, MLMultiArrayDataType};
        use objc2_foundation::{NSArray, NSNumber};

        /// Helper function to create a test MLMultiArray with Float32 data
        fn create_test_float32_array(data: &[f32], shape: &[usize]) -> Retained<MLMultiArray> {
            let shape_numbers: Vec<Retained<NSNumber>> =
                shape.iter().map(|&dim| NSNumber::new_usize(dim)).collect();
            let shape_refs: Vec<&NSNumber> = shape_numbers.iter().map(|n| n.as_ref()).collect();
            let shape_array = NSArray::from_slice(&shape_refs);

            let array = unsafe {
                MLMultiArray::initWithShape_dataType_error(
                    MLMultiArray::alloc(),
                    &shape_array,
                    MLMultiArrayDataType::Float32,
                )
                .unwrap()
            };

            // Fill with test data
            for (i, &value) in data.iter().enumerate() {
                let number = NSNumber::new_f32(value);
                unsafe {
                    array.setObject_atIndexedSubscript(&number, i as isize);
                }
            }

            array
        }

        /// Helper function to create a test MLMultiArray with Int32 data
        fn create_test_int32_array(data: &[i32], shape: &[usize]) -> Retained<MLMultiArray> {
            let shape_numbers: Vec<Retained<NSNumber>> =
                shape.iter().map(|&dim| NSNumber::new_usize(dim)).collect();
            let shape_refs: Vec<&NSNumber> = shape_numbers.iter().map(|n| n.as_ref()).collect();
            let shape_array = NSArray::from_slice(&shape_refs);

            let array = unsafe {
                MLMultiArray::initWithShape_dataType_error(
                    MLMultiArray::alloc(),
                    &shape_array,
                    MLMultiArrayDataType::Int32,
                )
                .unwrap()
            };

            // Fill with test data
            for (i, &value) in data.iter().enumerate() {
                let number = NSNumber::new_i32(value);
                unsafe {
                    array.setObject_atIndexedSubscript(&number, i as isize);
                }
            }

            array
        }

        /// Create an MLMultiArray that contains actual f16 binary data
        /// This creates the exact memory layout that CoreML produces for MLMultiArrayDataType(65552)
        fn create_actual_float16_array(data: &[f32], shape: &[usize]) -> Retained<MLMultiArray> {
            use half::f16;

            let shape_numbers: Vec<Retained<NSNumber>> =
                shape.iter().map(|&dim| NSNumber::new_usize(dim)).collect();
            let shape_refs: Vec<&NSNumber> = shape_numbers.iter().map(|n| n.as_ref()).collect();
            let shape_array = NSArray::from_slice(&shape_refs);

            // Create MLMultiArray with Float32 type - we'll overwrite with f16 binary data
            let array = unsafe {
                MLMultiArray::initWithShape_dataType_error(
                    MLMultiArray::alloc(),
                    &shape_array,
                    MLMultiArrayDataType::Float16,
                )
                .unwrap()
            };

            // Write actual f16 binary data into the MLMultiArray memory
            unsafe {
                #[allow(deprecated)]
                let data_ptr = array.dataPointer();
                let byte_ptr = data_ptr.as_ptr() as *mut u8;

                for (i, &f32_val) in data.iter().enumerate() {
                    // Convert f32 to f16 and get raw bytes
                    let f16_val = f16::from_f32(f32_val);
                    let f16_bytes = f16_val.to_bits().to_le_bytes();

                    // Write f16 bytes directly into memory (2 bytes per f16)
                    *byte_ptr.add(i * 2) = f16_bytes[0];
                    *byte_ptr.add(i * 2 + 1) = f16_bytes[1];
                }
            }

            array
        }

        #[test]
        fn test_convert_float32_1d_array() {
            let device = Device::Cpu;
            let test_data = vec![1.0, 2.5, -3.0, 4.25];
            let shape = vec![4];

            let mlarray = create_test_float32_array(&test_data, &shape);
            let tensor = convert_mlmultiarray_to_tensor(&mlarray, &device).unwrap();

            // Verify shape matches expected dimensions
            assert_eq!(tensor.shape().dims(), &[4]);

            // Verify tensor data matches input MLMultiArray values
            let tensor_data = tensor.to_vec1::<f32>().unwrap();
            assert_eq!(tensor_data, vec![1.0, 2.5, -3.0, 4.25]);

            // Verify exact float precision preservation
            for (i, &expected) in test_data.iter().enumerate() {
                assert!(
                    (tensor_data[i] - expected).abs() < f32::EPSILON,
                    "Value at index {} differs: expected {}, got {}",
                    i,
                    expected,
                    tensor_data[i]
                );
            }
        }

        #[test]
        fn test_convert_float32_2d_array() {
            let device = Device::Cpu;
            let test_data = vec![1.0, 2.0, 3.0, 4.0];
            let shape = vec![2, 2];

            let mlarray = create_test_float32_array(&test_data, &shape);
            let tensor = convert_mlmultiarray_to_tensor(&mlarray, &device).unwrap();

            // Verify 2D shape is preserved correctly
            assert_eq!(tensor.shape().dims(), &[2, 2]);
            assert_eq!(tensor.elem_count(), 4);

            // Verify 2D tensor data structure and values
            let tensor_data = tensor.to_vec2::<f32>().unwrap();
            let expected_2d = vec![vec![1.0, 2.0], vec![3.0, 4.0]];
            assert_eq!(tensor_data, expected_2d);

            // Verify row-major ordering is maintained
            assert_eq!(tensor_data[0][0], 1.0); // First element
            assert_eq!(tensor_data[0][1], 2.0); // Second element (same row)
            assert_eq!(tensor_data[1][0], 3.0); // Third element (next row)
            assert_eq!(tensor_data[1][1], 4.0); // Fourth element
        }

        #[test]
        fn test_convert_int32_array() {
            let device = Device::Cpu;
            let test_data = vec![1, -2, 3, 4];
            let shape = vec![4];

            let mlarray = create_test_int32_array(&test_data, &shape);
            let tensor = convert_mlmultiarray_to_tensor(&mlarray, &device).unwrap();

            // Verify shape is preserved
            assert_eq!(tensor.shape().dims(), &[4]);

            // Verify Int32 → Float32 conversion works correctly
            let tensor_data = tensor.to_vec1::<f32>().unwrap();
            assert_eq!(tensor_data, vec![1.0, -2.0, 3.0, 4.0]);

            // Verify each int32 value converts to exact f32 equivalent
            for (i, &int_val) in test_data.iter().enumerate() {
                let expected_f32 = int_val as f32;
                assert_eq!(
                    tensor_data[i], expected_f32,
                    "Int32 conversion failed at index {}: {} → {}, expected {}",
                    i, int_val, tensor_data[i], expected_f32
                );
            }

            // Verify negative values are handled correctly
            assert!(
                tensor_data[1] < 0.0,
                "Negative int32 value should remain negative after conversion"
            );
        }

        #[test]
        fn test_convert_3d_array() {
            let device = Device::Cpu;
            let test_data = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
            let shape = vec![2, 2, 2];

            let mlarray = create_test_float32_array(&test_data, &shape);
            let tensor = convert_mlmultiarray_to_tensor(&mlarray, &device).unwrap();

            // Verify 3D shape dimensions are correct
            assert_eq!(tensor.shape().dims(), &[2, 2, 2]);
            assert_eq!(tensor.elem_count(), 8);
            assert_eq!(tensor.rank(), 3);

            // Verify 3D tensor structure and all values
            let tensor_data = tensor.to_vec3::<f32>().unwrap();
            let expected = vec![
                vec![vec![1.0, 2.0], vec![3.0, 4.0]],
                vec![vec![5.0, 6.0], vec![7.0, 8.0]],
            ];
            assert_eq!(tensor_data, expected);

            // Verify 3D indexing works correctly
            assert_eq!(tensor_data[0][0][0], 1.0); // First element
            assert_eq!(tensor_data[0][0][1], 2.0); // Second element
            assert_eq!(tensor_data[0][1][0], 3.0); // Third element
            assert_eq!(tensor_data[1][1][1], 8.0); // Last element

            // Verify all dimensions have expected size
            assert_eq!(tensor_data.len(), 2); // First dimension
            assert_eq!(tensor_data[0].len(), 2); // Second dimension
            assert_eq!(tensor_data[0][0].len(), 2); // Third dimension
        }

        #[test]
        fn test_convert_large_values() {
            let device = Device::Cpu;
            let test_data = vec![f32::MAX, f32::MIN, 0.0];
            let shape = vec![3];

            let mlarray = create_test_float32_array(&test_data, &shape);
            let tensor = convert_mlmultiarray_to_tensor(&mlarray, &device).unwrap();

            // Verify shape
            assert_eq!(tensor.shape().dims(), &[3]);

            // Verify extreme float values are preserved exactly
            let tensor_data = tensor.to_vec1::<f32>().unwrap();
            assert_eq!(
                tensor_data[0],
                f32::MAX,
                "f32::MAX should be preserved exactly"
            );
            assert_eq!(
                tensor_data[1],
                f32::MIN,
                "f32::MIN should be preserved exactly"
            );
            assert_eq!(tensor_data[2], 0.0, "Zero should be preserved exactly");

            // Verify the values are actually the extreme values we expect
            assert!(
                tensor_data[0] > 3.4e38,
                "MAX value should be extremely large"
            );
            assert!(
                tensor_data[1] < -3.4e38,
                "MIN value should be extremely negative"
            );
            assert_eq!(tensor_data[2], 0.0);

            // Verify no NaN or infinite values were introduced
            for &val in &tensor_data {
                if val != 0.0 {
                    // Skip zero check since it's finite
                    assert!(!val.is_nan(), "No NaN values should be present: {val}");
                    // MAX/MIN are finite extreme values, not infinity
                    assert!(
                        val.is_finite() || val == f32::MAX || val == f32::MIN,
                        "Values should be finite or expected extremes: {val}"
                    );
                }
            }
        }

        #[test]
        fn test_convert_mixed_int_conversion() {
            let device = Device::Cpu;
            let test_data = vec![i32::MAX, i32::MIN, 0, -1, 1];
            let shape = vec![5];

            let mlarray = create_test_int32_array(&test_data, &shape);
            let tensor = convert_mlmultiarray_to_tensor(&mlarray, &device).unwrap();

            // Verify shape
            assert_eq!(tensor.shape().dims(), &[5]);

            // Verify extreme int32 values convert correctly to f32
            let tensor_data = tensor.to_vec1::<f32>().unwrap();
            assert_eq!(tensor_data[0], i32::MAX as f32, "i32::MAX conversion");
            assert_eq!(tensor_data[1], i32::MIN as f32, "i32::MIN conversion");
            assert_eq!(tensor_data[2], 0.0, "Zero conversion");
            assert_eq!(tensor_data[3], -1.0, "Negative one conversion");
            assert_eq!(tensor_data[4], 1.0, "Positive one conversion");

            // Verify extreme values are in expected ranges
            assert!(
                tensor_data[0] > 2.0e9,
                "i32::MAX should convert to ~2.1 billion"
            );
            assert!(
                tensor_data[1] < -2.0e9,
                "i32::MIN should convert to ~-2.1 billion"
            );

            // Verify signs are preserved correctly
            assert!(tensor_data[0] > 0.0, "i32::MAX should be positive");
            assert!(tensor_data[1] < 0.0, "i32::MIN should be negative");
            assert_eq!(tensor_data[2], 0.0, "Zero should remain zero");
            assert!(
                tensor_data[3] < 0.0,
                "Negative values should remain negative"
            );
            assert!(
                tensor_data[4] > 0.0,
                "Positive values should remain positive"
            );

            // Verify no precision loss for small integers
            assert_eq!(tensor_data[2] as i32, 0);
            assert_eq!(tensor_data[3] as i32, -1);
            assert_eq!(tensor_data[4] as i32, 1);
        }

        #[test]
        fn test_convert_empty_array() {
            let device = Device::Cpu;
            let test_data = vec![];
            let shape = vec![0];

            let mlarray = create_test_float32_array(&test_data, &shape);
            let tensor = convert_mlmultiarray_to_tensor(&mlarray, &device).unwrap();

            // Verify empty array shape
            assert_eq!(tensor.shape().dims(), &[0]);
            assert_eq!(tensor.elem_count(), 0);
            assert_eq!(tensor.rank(), 1); // Still 1D, just with 0 elements

            // Verify empty tensor contains no data
            let tensor_data = tensor.to_vec1::<f32>().unwrap();
            assert_eq!(tensor_data, Vec::<f32>::new());
            assert!(
                tensor_data.is_empty(),
                "Empty tensor should have no elements"
            );
            assert_eq!(tensor_data.len(), 0, "Empty tensor length should be 0");

            // Verify tensor is valid but empty
            assert!(
                !tensor.shape().dims().is_empty(),
                "Tensor should have at least one dimension"
            );
            assert_eq!(
                tensor.shape().dims()[0],
                0,
                "First dimension should be 0 for empty tensor"
            );
        }

        #[test]
        fn test_convert_actual_float16_mlarray() {
            use half::f16;
            use tracing::{debug, info};

            let device = Device::Cpu;

            // Test values that demonstrate f16 precision behavior
            let test_values = vec![
                std::f32::consts::PI, // π - loses precision in f16
                1.0,                  // Should be exact
                -2.5,                 // Simple negative value
                0.0,                  // Zero
                65504.0,              // f16::MAX
                -1.0,                 // Negative one
            ];

            let shape = vec![6];

            // Create MLMultiArray with actual f16 binary data and Float16 data type
            let mlarray = create_actual_float16_array(&test_values, &shape);

            // Calculate expected results (what we should get after f16 conversion)
            let expected_results: Vec<f32> = test_values
                .iter()
                .map(|&val| f16::from_f32(val).to_f32())
                .collect();

            // TEST: Direct conversion using convert_mlmultiarray_to_tensor
            // This should now properly detect the Float16 data type and convert correctly
            let tensor = convert_mlmultiarray_to_tensor(&mlarray, &device).unwrap();

            // Verify tensor structure
            assert_eq!(tensor.shape().dims(), &[6]);
            let tensor_data = tensor.to_vec1::<f32>().unwrap();

            // Verify the conversion matches our expected f16 results
            for (i, (&tensor_val, &expected)) in
                tensor_data.iter().zip(expected_results.iter()).enumerate()
            {
                assert_eq!(
                    tensor_val, expected,
                    "F16 conversion mismatch at index {}: tensor {}, expected {} (original: {})",
                    i, tensor_val, expected, test_values[i]
                );
            }

            // Test specific precision behaviors
            assert_eq!(tensor_data[1], 1.0, "1.0 should be exact in f16");
            assert_eq!(tensor_data[2], -2.5, "-2.5 should be exact in f16");
            assert_eq!(tensor_data[3], 0.0, "0.0 should be exact in f16");
            assert_eq!(tensor_data[4], 65504.0, "f16::MAX should be preserved");
            assert_eq!(tensor_data[5], -1.0, "-1.0 should be exact in f16");

            // Verify π loses precision as expected
            let pi_f16_expected = f16::from_f32(test_values[0]).to_f32();
            assert_eq!(
                tensor_data[0], pi_f16_expected,
                "π should match f16 precision"
            );
            assert!(
                (tensor_data[0] - test_values[0]).abs() > 0.0001,
                "π should lose precision in f16"
            );

            info!("Actual Float16 MLMultiArray conversion test passed!");
            debug!("   Original values: {:?}", test_values);
            debug!("   F16 converted:   {:?}", tensor_data);
        }
    }

    #[cfg(not(target_os = "macos"))]
    mod non_macos_tests {
        #[test]
        fn test_conversion_functions_not_available() {
            // This test just ensures the module compiles on non-macOS platforms
            // The actual conversion functions are only available on macOS
            println!("Conversion functions are only available on macOS");
        }
    }
}