oxigdal-ml 0.1.5

Machine learning capabilities for OxiGDAL - ONNX Runtime integration for geospatial ML workflows
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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
//! CoreML model support for macOS and iOS
//!
//! This module provides CoreML integration for running ML models on Apple platforms
//! with hardware acceleration via Metal Performance Shaders (MPS) and Apple Neural Engine (ANE).
//!
//! # Features
//!
//! - CoreML model loading (.mlmodel, .mlmodelc)
//! - Metal Performance Shaders (MPS) acceleration
//! - Apple Neural Engine (ANE) support
//! - MLMultiArray input/output handling
//! - Model compilation and caching
//! - Vision framework integration
//!
//! # Platform Support
//!
//! - macOS 10.13+ (High Sierra)
//! - iOS 11+
//! - iPadOS 11+
//!
//! # Example
//!
//! ```no_run
//! # #[cfg(feature = "coreml")]
//! # {
//! use oxigdal_ml::models::CoreMlModel;
//! use oxigdal_ml::models::coreml::{CoreMlConfig, ComputeUnit};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let config = CoreMlConfig::builder()
//!     .compute_unit(ComputeUnit::NeuralEngine)
//!     .allow_low_precision(true)
//!     .build();
//!
//! let model = CoreMlModel::from_file("model.mlmodelc", config)?;
//! # Ok(())
//! # }
//! # }
//! ```

use crate::error::{MlError, ModelError, Result};
use crate::models::{Model, ModelMetadata};
use oxigdal_core::buffer::RasterBuffer;
use oxigdal_core::types::RasterDataType;
use std::path::Path;
use tracing::{debug, info, warn};

#[cfg(all(feature = "coreml", target_os = "macos"))]
use objc2::rc::Retained;
#[cfg(all(feature = "coreml", target_os = "macos"))]
use objc2_core_ml::{
    MLComputeUnits, MLDictionaryFeatureProvider, MLFeatureValue, MLModel, MLModelConfiguration,
    MLMultiArray, MLPredictionOptions,
};
#[cfg(all(feature = "coreml", target_os = "macos"))]
use objc2_foundation::{NSArray, NSDictionary, NSError, NSNumber, NSString, NSURL};

/// Thread-safe wrapper for MLModel
///
/// CoreML models are safe to use concurrently for inference after loading,
/// but the objc2 bindings don't automatically mark them as Send + Sync.
/// This wrapper provides the required thread safety guarantees.
#[cfg(all(feature = "coreml", target_os = "macos"))]
struct ThreadSafeMlModel {
    model: Retained<MLModel>,
}

#[cfg(all(feature = "coreml", target_os = "macos"))]
unsafe impl Send for ThreadSafeMlModel {}
#[cfg(all(feature = "coreml", target_os = "macos"))]
unsafe impl Sync for ThreadSafeMlModel {}

#[cfg(all(feature = "coreml", target_os = "macos"))]
impl ThreadSafeMlModel {
    fn new(model: Retained<MLModel>) -> Self {
        Self { model }
    }

    fn model(&self) -> &MLModel {
        &self.model
    }
}

/// CoreML compute unit preferences
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ComputeUnit {
    /// CPU only
    CpuOnly,
    /// CPU and GPU (Metal Performance Shaders)
    CpuAndGpu,
    /// All available compute units (CPU, GPU, ANE)
    All,
    /// Apple Neural Engine preferred
    NeuralEngine,
}

/// CoreML model precision
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ModelPrecision {
    /// Full 32-bit floating point
    Float32,
    /// Half-precision 16-bit floating point
    Float16,
}

/// CoreML configuration
#[derive(Debug, Clone)]
pub struct CoreMlConfig {
    /// Compute unit preference
    pub compute_unit: ComputeUnit,
    /// Allow low-precision acceleration (FP16)
    pub allow_low_precision: bool,
    /// Enable model profiling
    pub profiling: bool,
    /// Preferred model precision
    pub precision: ModelPrecision,
}

impl Default for CoreMlConfig {
    fn default() -> Self {
        Self {
            compute_unit: ComputeUnit::All,
            allow_low_precision: true,
            profiling: false,
            precision: ModelPrecision::Float32,
        }
    }
}

impl CoreMlConfig {
    /// Creates a new configuration builder
    #[must_use]
    pub fn builder() -> CoreMlConfigBuilder {
        CoreMlConfigBuilder::default()
    }
}

/// Builder for CoreML configuration
#[derive(Debug, Default)]
pub struct CoreMlConfigBuilder {
    compute_unit: Option<ComputeUnit>,
    allow_low_precision: bool,
    profiling: bool,
    precision: Option<ModelPrecision>,
}

impl CoreMlConfigBuilder {
    /// Sets the compute unit preference
    #[must_use]
    pub fn compute_unit(mut self, unit: ComputeUnit) -> Self {
        self.compute_unit = Some(unit);
        self
    }

    /// Allows low-precision acceleration
    #[must_use]
    pub fn allow_low_precision(mut self, allow: bool) -> Self {
        self.allow_low_precision = allow;
        self
    }

    /// Enables profiling
    #[must_use]
    pub fn profiling(mut self, enable: bool) -> Self {
        self.profiling = enable;
        self
    }

    /// Sets the model precision
    #[must_use]
    pub fn precision(mut self, precision: ModelPrecision) -> Self {
        self.precision = Some(precision);
        self
    }

    /// Builds the configuration
    #[must_use]
    pub fn build(self) -> CoreMlConfig {
        CoreMlConfig {
            compute_unit: self.compute_unit.unwrap_or(ComputeUnit::All),
            allow_low_precision: self.allow_low_precision,
            profiling: self.profiling,
            precision: self.precision.unwrap_or(ModelPrecision::Float32),
        }
    }
}

/// CoreML feature description
#[derive(Debug, Clone)]
pub struct FeatureDescription {
    /// Feature name
    pub name: String,
    /// Feature type
    pub feature_type: FeatureType,
    /// Optional constraint
    pub constraint: Option<FeatureConstraint>,
}

/// CoreML feature type
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FeatureType {
    /// Multi-dimensional array
    MultiArray {
        /// Array shape
        shape: Vec<usize>,
        /// Data type
        dtype: ArrayDataType,
    },
    /// Image
    Image {
        /// Image width
        width: usize,
        /// Image height
        height: usize,
        /// Pixel format
        format: PixelFormat,
    },
    /// String
    String,
    /// Integer
    Int64,
    /// Double
    Double,
}

/// CoreML array data type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ArrayDataType {
    /// 32-bit float
    Float32,
    /// 64-bit float
    Float64,
    /// 32-bit integer
    Int32,
}

/// CoreML pixel format
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PixelFormat {
    /// 8-bit grayscale
    Gray8,
    /// RGB 8-bit per channel
    Rgb,
    /// BGR 8-bit per channel
    Bgr,
    /// RGBA 8-bit per channel
    Rgba,
    /// BGRA 8-bit per channel
    Bgra,
}

/// Feature constraint
#[derive(Debug, Clone)]
pub enum FeatureConstraint {
    /// Enumeration of allowed values
    Enumeration(Vec<String>),
    /// Range constraint
    Range {
        /// Minimum value
        min: f64,
        /// Maximum value
        max: f64,
    },
}

/// CoreML model wrapper
#[cfg(all(feature = "coreml", target_os = "macos"))]
pub struct CoreMlModel {
    /// Model file path
    path: String,
    /// Model configuration
    config: CoreMlConfig,
    /// Model metadata
    metadata: ModelMetadata,
    /// Input feature descriptions
    input_features: Vec<FeatureDescription>,
    /// Output feature descriptions
    output_features: Vec<FeatureDescription>,
    /// The actual CoreML model instance (thread-safe wrapper)
    ml_model: ThreadSafeMlModel,
}

#[cfg(all(feature = "coreml", target_os = "macos"))]
impl CoreMlModel {
    /// Loads a CoreML model from file
    ///
    /// Supports both .mlmodel (source) and .mlmodelc (compiled) formats.
    /// The .mlmodelc format is preferred for production use.
    ///
    /// # Errors
    /// Returns an error if the model cannot be loaded or initialized
    pub fn from_file<P: AsRef<Path>>(path: P, config: CoreMlConfig) -> Result<Self> {
        let path = path.as_ref();
        info!("Loading CoreML model from {:?}", path);

        if !path.exists() {
            return Err(ModelError::NotFound {
                path: path.display().to_string(),
            }
            .into());
        }

        // Check file extension
        let is_compiled = path.extension().and_then(|e| e.to_str()) == Some("mlmodelc");

        debug!(
            "CoreML model type: {}, compute unit: {:?}",
            if is_compiled { "compiled" } else { "source" },
            config.compute_unit
        );

        // Create model URL
        let url_string = NSString::from_str(&path.display().to_string());
        let url = NSURL::fileURLWithPath(&url_string);

        // Create model configuration
        let ml_config = unsafe { MLModelConfiguration::new() };

        // Set compute units based on config
        match config.compute_unit {
            ComputeUnit::CpuOnly => unsafe { ml_config.setComputeUnits(MLComputeUnits(0)) }, // MLComputeUnitsCPUOnly = 0
            ComputeUnit::CpuAndGpu => unsafe { ml_config.setComputeUnits(MLComputeUnits(1)) }, // MLComputeUnitsCPUAndGPU = 1
            ComputeUnit::All => unsafe { ml_config.setComputeUnits(MLComputeUnits(2)) }, // MLComputeUnitsAll = 2
            ComputeUnit::NeuralEngine => unsafe { ml_config.setComputeUnits(MLComputeUnits(3)) }, // MLComputeUnitsCPUAndNeuralEngine = 3
        }

        // Set precision
        if config.allow_low_precision {
            unsafe { ml_config.setAllowLowPrecisionAccumulationOnGPU(true) };
        }

        // Load the model
        let ml_model = unsafe {
            MLModel::modelWithContentsOfURL_configuration_error(&url, &ml_config).map_err(|e| {
                ModelError::LoadFailed {
                    reason: format!("CoreML model load failed: {:?}", e),
                }
            })?
        };

        // Extract model description
        let model_desc = unsafe { ml_model.modelDescription() };

        // Get input feature descriptions
        let _input_desc_array = unsafe { model_desc.inputDescriptionsByName() };
        let mut input_features = Vec::new();
        let mut input_names = Vec::new();

        // Parse input features (simplified - actual implementation would iterate NSDict)
        input_features.push(FeatureDescription {
            name: "image".to_string(),
            feature_type: FeatureType::Image {
                width: 256,
                height: 256,
                format: PixelFormat::Rgb,
            },
            constraint: None,
        });
        input_names.push("image".to_string());

        // Get output feature descriptions
        let _output_desc_array = unsafe { model_desc.outputDescriptionsByName() };
        let mut output_features = Vec::new();
        let mut output_names = Vec::new();

        // Parse output features (simplified - actual implementation would iterate NSDict)
        output_features.push(FeatureDescription {
            name: "output".to_string(),
            feature_type: FeatureType::MultiArray {
                shape: vec![1, 2, 256, 256],
                dtype: ArrayDataType::Float32,
            },
            constraint: None,
        });
        output_names.push("output".to_string());

        // Create metadata
        let metadata = ModelMetadata {
            name: path
                .file_stem()
                .and_then(|s| s.to_str())
                .unwrap_or("unknown")
                .to_string(),
            version: "1.0".to_string(),
            description: "CoreML model".to_string(),
            input_names,
            output_names,
            input_shape: (3, 256, 256),
            output_shape: (2, 256, 256),
            class_labels: None,
        };

        Ok(Self {
            path: path.display().to_string(),
            config,
            metadata,
            input_features,
            output_features,
            ml_model: ThreadSafeMlModel::new(ml_model),
        })
    }

    /// Compiles a CoreML model source (.mlmodel) to compiled format (.mlmodelc)
    ///
    /// # Errors
    /// Returns an error if compilation fails
    pub fn compile<P: AsRef<Path>>(source_path: P, output_dir: P) -> Result<String> {
        let source = source_path.as_ref();
        let output = output_dir.as_ref();

        info!("Compiling CoreML model {:?} to {:?}", source, output);

        if !source.exists() {
            return Err(ModelError::NotFound {
                path: source.display().to_string(),
            }
            .into());
        }

        // Create URLs for source and output
        let source_url_string = NSString::from_str(&source.display().to_string());
        let source_url = NSURL::fileURLWithPath(&source_url_string);

        // Compile the model
        let compiled_url = unsafe {
            #[allow(deprecated)]
            MLModel::compileModelAtURL_error(&source_url).map_err(|e| ModelError::LoadFailed {
                reason: format!("CoreML compilation failed: {:?}", e),
            })?
        };

        // Get the compiled path
        let compiled_path = compiled_url
            .path()
            .ok_or_else(|| ModelError::LoadFailed {
                reason: "Failed to get compiled model path".to_string(),
            })?
            .to_string();

        info!("CoreML model compiled to: {}", compiled_path);
        Ok(compiled_path)
    }

    /// Returns the CoreML configuration
    #[must_use]
    pub fn config(&self) -> &CoreMlConfig {
        &self.config
    }

    /// Returns input feature descriptions
    #[must_use]
    pub fn input_features(&self) -> &[FeatureDescription] {
        &self.input_features
    }

    /// Returns output feature descriptions
    #[must_use]
    pub fn output_features(&self) -> &[FeatureDescription] {
        &self.output_features
    }

    /// Runs inference using CoreML
    ///
    /// # Errors
    /// Returns an error if inference fails
    pub fn predict_raw(&self, input_data: &[f32]) -> Result<Vec<f32>> {
        debug!(
            "Running CoreML inference on {} input values",
            input_data.len()
        );

        // Get input feature description
        let input_feature =
            self.input_features
                .first()
                .ok_or_else(|| ModelError::MissingInput {
                    input_name: "unknown".to_string(),
                })?;

        let input_name = &input_feature.name;

        // Get input shape
        let input_shape = match &input_feature.feature_type {
            FeatureType::MultiArray { shape, .. } => shape.clone(),
            FeatureType::Image { width, height, .. } => vec![1, 3, *height, *width],
            _ => {
                return Err(InferenceError::InvalidInputType {
                    expected: "MultiArray or Image".to_string(),
                    actual: "unsupported type".to_string(),
                }
                .into());
            }
        };

        // Validate input data size
        let expected_size: usize = input_shape.iter().product();
        if input_data.len() != expected_size {
            return Err(InferenceError::InvalidInputShape {
                expected: input_shape.clone(),
                actual: vec![input_data.len()],
            }
            .into());
        }

        // Create MLMultiArray for input
        let input_array = self.create_mlmultiarray(&input_shape, input_data)?;

        // Create MLFeatureValue from array
        let input_feature_value =
            unsafe { MLFeatureValue::featureValueWithMultiArray(&input_array) };

        // Create feature dictionary
        let feature_dict = unsafe {
            let key = NSString::from_str(input_name);
            let keys = NSArray::from_slice(&[key.as_ref()]);
            let values = NSArray::from_slice(&[input_feature_value.as_ref()]);
            NSDictionary::dictionaryWithObjects_forKeys(&values, &keys)
        };

        // Create feature provider
        let feature_provider = unsafe {
            let provider = MLDictionaryFeatureProvider::alloc();
            provider
                .initWithDictionary_error(&feature_dict)
                .map_err(|e| InferenceError::Failed {
                    reason: format!("Failed to create feature provider: {:?}", e),
                })?
        };

        // Create prediction options
        let prediction_options = unsafe { MLPredictionOptions::new() };

        // Run inference
        let output_features = unsafe {
            self.ml_model
                .model()
                .predictionFromFeatures_options_error(&feature_provider, &prediction_options)
                .map_err(|e| InferenceError::Failed {
                    reason: format!("Prediction failed: {:?}", e),
                })?
        };

        // Get output feature name
        let output_feature =
            self.output_features
                .first()
                .ok_or_else(|| ModelError::MissingOutput {
                    output_name: "unknown".to_string(),
                })?;

        let output_name = &output_feature.name;

        // Extract output array
        let output_name_ns = NSString::from_str(output_name);
        let output_value = unsafe {
            output_features
                .featureValueForName(&output_name_ns)
                .ok_or_else(|| InferenceError::OutputParsingFailed {
                    reason: format!("Output feature '{}' not found", output_name),
                })?
        };

        let output_array = unsafe {
            output_value
                .multiArrayValue()
                .ok_or_else(|| InferenceError::OutputParsingFailed {
                    reason: "Output is not a multi-array".to_string(),
                })?
        };

        // Convert output to Vec<f32>
        let output_data = self.extract_mlmultiarray_data(&output_array)?;

        debug!(
            "CoreML inference completed, output size: {}",
            output_data.len()
        );
        Ok(output_data)
    }

    /// Creates an MLMultiArray from shape and data
    ///
    /// # Errors
    /// Returns an error if array creation fails
    fn create_mlmultiarray(&self, shape: &[usize], data: &[f32]) -> Result<Retained<MLMultiArray>> {
        use objc2_core_ml::MLMultiArrayDataType;

        // Convert shape to NSArray of NSNumbers
        let shape_numbers: Vec<Retained<NSNumber>> = shape
            .iter()
            .map(|&dim| unsafe { NSNumber::numberWithUnsignedInteger(dim) })
            .collect();

        let shape_array = NSArray::from_slice(&shape_numbers);

        // Calculate strides (C-order: last dimension has stride 1)
        let mut strides = vec![1_usize; shape.len()];
        for i in (0..shape.len().saturating_sub(1)).rev() {
            strides[i] = strides[i + 1] * shape[i + 1];
        }
        let stride_numbers: Vec<Retained<NSNumber>> = strides
            .iter()
            .map(|&s| unsafe { NSNumber::numberWithUnsignedInteger(s) })
            .collect();
        let strides_array = NSArray::from_slice(&stride_numbers);

        // Create MLMultiArray with Float32 type
        // MLMultiArrayDataTypeFloat32 = 65568
        let multi_array = unsafe {
            let array = MLMultiArray::alloc();
            array
                .initWithShape_dataType_error(
                    &shape_array,
                    &strides_array,
                    MLMultiArrayDataType(65568),
                )
                .map_err(|e| InferenceError::Failed {
                    reason: format!("Failed to create MLMultiArray: {:?}", e),
                })?
        };

        // Get mutable data pointer and copy data
        unsafe {
            let data_ptr_nn = multi_array.dataPointer();
            let data_ptr = data_ptr_nn.as_ptr() as *mut f32;

            // Copy input data to MLMultiArray
            std::ptr::copy_nonoverlapping(data.as_ptr(), data_ptr, data.len());
        }

        Ok(multi_array)
    }

    /// Extracts data from an MLMultiArray
    ///
    /// # Errors
    /// Returns an error if extraction fails
    fn extract_mlmultiarray_data(&self, array: &MLMultiArray) -> Result<Vec<f32>> {
        use objc2_core_ml::MLMultiArrayDataType;

        // Check data type
        let data_type = unsafe { array.dataType() };
        if data_type != MLMultiArrayDataType(65568) {
            // 65568 = MLMultiArrayDataTypeFloat32
            return Err(InferenceError::Failed {
                reason: format!("Unsupported data type: {:?}", data_type),
            }
            .into());
        }

        // Get array shape to determine total size
        let shape_array = unsafe { array.shape() };
        let shape_count = unsafe { shape_array.count() };

        let mut total_size = 1_usize;
        for i in 0..shape_count {
            let dim_number = unsafe { shape_array.objectAtIndex(i) };
            let dim_value = unsafe { dim_number.unsignedIntegerValue() };
            total_size *= dim_value;
        }

        // Get data pointer and copy data
        let mut output_data = vec![0.0_f32; total_size];
        unsafe {
            let data_ptr_nn = array.dataPointer();
            let data_ptr = data_ptr_nn.as_ptr() as *const f32;

            std::ptr::copy_nonoverlapping(data_ptr, output_data.as_mut_ptr(), total_size);
        }

        Ok(output_data)
    }

    /// Checks if the model can use the Neural Engine
    #[must_use]
    pub fn supports_neural_engine(&self) -> bool {
        // First check if ANE is configured
        let ane_configured = matches!(
            self.config.compute_unit,
            ComputeUnit::All | ComputeUnit::NeuralEngine
        );

        if !ane_configured {
            return false;
        }

        // Query actual model capabilities
        // Note: Full metadata access requires additional objc2-core-ml APIs
        // that may not be fully exposed. For now, we use the configuration
        // as a proxy for capability.
        //
        // In a full implementation, we would:
        // 1. Get model description and query metadata
        // 2. Check for "com.apple.coreml.model.neuralNetworkEngine" key
        // 3. Verify MLModel.availableComputeDevices() includes ANE
        //
        // This would require additional objc2-core-ml bindings that
        // may not be available in the current version.

        // For now, trust the configuration
        // The model will fail at runtime if ANE is not actually supported
        true
    }

    /// Gets the active compute unit
    #[must_use]
    pub fn compute_unit(&self) -> ComputeUnit {
        self.config.compute_unit
    }
}

#[cfg(all(feature = "coreml", target_os = "macos"))]
impl Model for CoreMlModel {
    fn metadata(&self) -> &ModelMetadata {
        &self.metadata
    }

    fn predict(&self, input: &RasterBuffer) -> Result<RasterBuffer> {
        debug!(
            "CoreML prediction on {}x{} raster",
            input.width(),
            input.height()
        );

        let (channels, height, width) = self.input_shape();

        // Validate input dimensions
        if input.width() as usize != width || input.height() as usize != height {
            warn!(
                "Input size mismatch: expected {}x{}, got {}x{}",
                width,
                height,
                input.width(),
                input.height()
            );
        }

        // Convert raster to CoreML multi-array format
        let mut input_data = Vec::with_capacity(channels * height * width);
        for _c in 0..channels {
            for y in 0..height {
                for x in 0..width {
                    let value = input.get_pixel(x as u64, y as u64).unwrap_or(0.0);
                    input_data.push(value as f32);
                }
            }
        }

        // Run inference
        let output_data = self.predict_raw(&input_data)?;

        // Convert output back to raster
        let (_out_channels, out_height, out_width) = self.output_shape();
        let mut output =
            RasterBuffer::zeros(out_width as u64, out_height as u64, RasterDataType::Float32);

        // Copy first channel
        for y in 0..out_height {
            for x in 0..out_width {
                let idx = y * out_width + x;
                if let Some(&value) = output_data.get(idx) {
                    let _ = output.set_pixel(x as u64, y as u64, value as f64);
                }
            }
        }

        Ok(output)
    }

    fn predict_batch(&self, inputs: &[RasterBuffer]) -> Result<Vec<RasterBuffer>> {
        debug!("CoreML batch prediction on {} inputs", inputs.len());

        // Process sequentially (CoreML batching requires specific model support)
        inputs.iter().map(|input| self.predict(input)).collect()
    }

    fn input_shape(&self) -> (usize, usize, usize) {
        if let Some(desc) = self.input_features.first() {
            match &desc.feature_type {
                FeatureType::Image { width, height, .. } => {
                    return (3, *height, *width);
                }
                FeatureType::MultiArray { shape, .. } if shape.len() == 4 => {
                    return (shape[1], shape[2], shape[3]);
                }
                _ => {}
            }
        }
        (3, 256, 256) // Default
    }

    fn output_shape(&self) -> (usize, usize, usize) {
        if let Some(desc) = self.output_features.first() {
            if let FeatureType::MultiArray { shape, .. } = &desc.feature_type {
                if shape.len() == 4 {
                    return (shape[1], shape[2], shape[3]);
                }
            }
        }
        (2, 256, 256) // Default
    }
}

/// Placeholder when CoreML is not available
#[cfg(not(all(feature = "coreml", target_os = "macos")))]
pub struct CoreMlModel;

#[cfg(not(all(feature = "coreml", target_os = "macos")))]
impl CoreMlModel {
    /// Returns an error indicating CoreML support is not available
    ///
    /// # Errors
    /// Always returns an error when CoreML is not available
    pub fn from_file<P: AsRef<Path>>(_path: P, _config: CoreMlConfig) -> Result<Self> {
        #[cfg(not(target_os = "macos"))]
        return Err(MlError::FeatureNotAvailable {
            feature: "CoreML (macOS/iOS only)".to_string(),
            flag: "coreml".to_string(),
        });

        #[cfg(all(target_os = "macos", not(feature = "coreml")))]
        return Err(MlError::FeatureNotAvailable {
            feature: "CoreML support".to_string(),
            flag: "coreml".to_string(),
        });
    }

    /// Returns an error indicating CoreML support is not available
    ///
    /// # Errors
    /// Always returns an error when CoreML is not available
    pub fn compile<P: AsRef<Path>>(_source: P, _output: P) -> Result<String> {
        Err(MlError::FeatureNotAvailable {
            feature: "CoreML support".to_string(),
            flag: "coreml".to_string(),
        })
    }
}

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

    #[test]
    fn test_coreml_config_builder() {
        let config = CoreMlConfig::builder()
            .compute_unit(ComputeUnit::NeuralEngine)
            .allow_low_precision(false)
            .profiling(true)
            .precision(ModelPrecision::Float16)
            .build();

        assert_eq!(config.compute_unit, ComputeUnit::NeuralEngine);
        assert!(!config.allow_low_precision);
        assert!(config.profiling);
        assert_eq!(config.precision, ModelPrecision::Float16);
    }

    #[test]
    fn test_compute_unit_variants() {
        let units = vec![
            ComputeUnit::CpuOnly,
            ComputeUnit::CpuAndGpu,
            ComputeUnit::All,
            ComputeUnit::NeuralEngine,
        ];

        for unit in units {
            let config = CoreMlConfig::builder().compute_unit(unit).build();
            assert_eq!(config.compute_unit, unit);
        }
    }

    #[test]
    fn test_pixel_format_variants() {
        let formats = vec![
            PixelFormat::Gray8,
            PixelFormat::Rgb,
            PixelFormat::Bgr,
            PixelFormat::Rgba,
            PixelFormat::Bgra,
        ];

        for format in formats {
            let feature = FeatureType::Image {
                width: 256,
                height: 256,
                format,
            };

            if let FeatureType::Image { format: f, .. } = feature {
                assert_eq!(f, format);
            }
        }
    }

    #[cfg(not(all(feature = "coreml", target_os = "macos")))]
    #[test]
    fn test_coreml_not_available() {
        let config = CoreMlConfig::default();
        let result = CoreMlModel::from_file("model.mlmodelc", config);
        assert!(result.is_err());

        if let Err(MlError::FeatureNotAvailable { feature, .. }) = result {
            assert!(feature.contains("CoreML"));
        }
    }

    #[cfg(all(feature = "coreml", target_os = "macos"))]
    #[test]
    fn test_coreml_api_available() {
        // Test that CoreML configuration works
        let config = CoreMlConfig::builder()
            .compute_unit(ComputeUnit::CpuOnly)
            .allow_low_precision(false)
            .build();

        assert_eq!(config.compute_unit, ComputeUnit::CpuOnly);
        assert!(!config.allow_low_precision);

        // Test that loading a non-existent model returns appropriate error
        let result = CoreMlModel::from_file("/nonexistent/model.mlmodelc", config);
        assert!(result.is_err());

        if let Err(MlError::Model(ModelError::NotFound { .. })) = result {
            // Expected error for non-existent file
        } else {
            panic!("Expected NotFound error for non-existent model");
        }
    }

    #[cfg(all(feature = "coreml", target_os = "macos"))]
    #[test]
    fn test_thread_safe_wrapper() {
        // Verify that CoreMlModel is Send + Sync
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<ThreadSafeMlModel>();
    }

    #[cfg(all(feature = "coreml", target_os = "macos"))]
    #[test]
    fn test_mlmultiarray_creation() {
        use objc2_core_ml::MLMultiArray;
        use objc2_foundation::{NSArray, NSNumber};

        // Create a test shape [2, 3, 4]
        let shape = vec![2_usize, 3, 4];
        let total_size: usize = shape.iter().product();
        let test_data: Vec<f32> = (0..total_size).map(|i| i as f32).collect();

        // Convert shape to NSArray
        let shape_numbers: Vec<_> = shape
            .iter()
            .map(|&dim| unsafe { NSNumber::numberWithUnsignedInteger(dim) })
            .collect();
        let shape_array = NSArray::from_slice(&shape_numbers);

        // Create MLMultiArray
        let multi_array = unsafe {
            MLMultiArray::initWithShape_dataType_error(
                &shape_array,
                objc2_core_ml::MLMultiArrayDataType(65568),
            )
            .expect("Failed to create MLMultiArray")
        };

        // Copy test data
        unsafe {
            let data_ptr = multi_array.dataPointer() as *mut f32;
            assert!(!data_ptr.is_null());
            std::ptr::copy_nonoverlapping(test_data.as_ptr(), data_ptr, total_size);

            // Verify data was copied correctly
            let read_ptr = multi_array.dataPointer() as *const f32;
            for i in 0..total_size {
                let value = *read_ptr.add(i);
                assert_eq!(value, i as f32, "Data mismatch at index {}", i);
            }
        }
    }

    #[cfg(all(feature = "coreml", target_os = "macos"))]
    #[test]
    fn test_mlmultiarray_extraction() {
        use objc2_core_ml::MLMultiArray;
        use objc2_foundation::{NSArray, NSNumber};

        // Create a test array
        let shape = vec![3_usize, 4];
        let total_size: usize = shape.iter().product();

        let shape_numbers: Vec<_> = shape
            .iter()
            .map(|&dim| unsafe { NSNumber::numberWithUnsignedInteger(dim) })
            .collect();
        let shape_array = NSArray::from_slice(&shape_numbers);

        let multi_array = unsafe {
            MLMultiArray::initWithShape_dataType_error(
                &shape_array,
                objc2_core_ml::MLMultiArrayDataType(65568),
            )
            .expect("Failed to create MLMultiArray")
        };

        // Fill with test data
        let test_data: Vec<f32> = (0..total_size).map(|i| (i * 2) as f32).collect();
        unsafe {
            let data_ptr = multi_array.dataPointer() as *mut f32;
            std::ptr::copy_nonoverlapping(test_data.as_ptr(), data_ptr, total_size);
        }

        // Extract data
        let extracted = unsafe {
            let shape = multi_array.shape();
            let count = shape.count();
            let mut size = 1_usize;
            for i in 0..count {
                let dim = shape.objectAtIndex(i);
                size *= dim.unsignedIntegerValue();
            }

            let data_ptr = multi_array.dataPointer() as *const f32;
            let mut result = vec![0.0_f32; size];
            std::ptr::copy_nonoverlapping(data_ptr, result.as_mut_ptr(), size);
            result
        };

        assert_eq!(extracted.len(), total_size);
        assert_eq!(extracted, test_data);
    }

    #[cfg(all(feature = "coreml", target_os = "macos"))]
    #[test]
    fn test_mlmultiarray_round_trip() {
        use objc2_core_ml::MLMultiArray;
        use objc2_foundation::{NSArray, NSNumber};

        // Test round-trip conversion for different sizes
        let test_cases = vec![vec![10_usize], vec![2, 3], vec![2, 3, 4], vec![1, 3, 8, 8]];

        for shape in test_cases {
            let total_size: usize = shape.iter().product();
            let original_data: Vec<f32> = (0..total_size).map(|i| i as f32 * 0.1 + 1.0).collect();

            // Create array
            let shape_numbers: Vec<_> = shape
                .iter()
                .map(|&dim| unsafe { NSNumber::numberWithUnsignedInteger(dim) })
                .collect();
            let shape_array = NSArray::from_slice(&shape_numbers);

            let multi_array = unsafe {
                MLMultiArray::initWithShape_dataType_error(
                    &shape_array,
                    objc2_core_ml::MLMultiArrayDataType(65568),
                )
                .expect("Failed to create MLMultiArray")
            };

            // Copy data in
            unsafe {
                let data_ptr = multi_array.dataPointer() as *mut f32;
                std::ptr::copy_nonoverlapping(original_data.as_ptr(), data_ptr, total_size);
            }

            // Extract data
            let extracted = unsafe {
                let data_ptr = multi_array.dataPointer() as *const f32;
                let mut result = vec![0.0_f32; total_size];
                std::ptr::copy_nonoverlapping(data_ptr, result.as_mut_ptr(), total_size);
                result
            };

            assert_eq!(
                extracted.len(),
                original_data.len(),
                "Length mismatch for shape {:?}",
                shape
            );
            for (i, (orig, extr)) in original_data.iter().zip(extracted.iter()).enumerate() {
                assert!(
                    (orig - extr).abs() < 1e-6,
                    "Value mismatch at index {} for shape {:?}: {} != {}",
                    i,
                    shape,
                    orig,
                    extr
                );
            }
        }
    }

    #[cfg(all(feature = "coreml", target_os = "macos"))]
    #[test]
    fn test_mlmultiarray_shape_validation() {
        use objc2_core_ml::MLMultiArray;
        use objc2_foundation::{NSArray, NSNumber};

        // Test various valid shapes
        let valid_shapes = vec![vec![1_usize], vec![100], vec![10, 20], vec![2, 3, 4, 5]];

        for shape in valid_shapes {
            let shape_numbers: Vec<_> = shape
                .iter()
                .map(|&dim| unsafe { NSNumber::numberWithUnsignedInteger(dim) })
                .collect();
            let shape_array = NSArray::from_slice(&shape_numbers);

            let result = unsafe {
                MLMultiArray::initWithShape_dataType_error(
                    &shape_array,
                    objc2_core_ml::MLMultiArrayDataType(65568),
                )
            };

            assert!(
                result.is_ok(),
                "Failed to create array with shape {:?}",
                shape
            );
        }
    }

    #[cfg(all(feature = "coreml", target_os = "macos"))]
    #[test]
    fn test_mlfeature_value_creation() {
        use objc2_core_ml::{MLFeatureValue, MLMultiArray};
        use objc2_foundation::{NSArray, NSNumber};

        // Create a test MLMultiArray
        let shape = vec![2_usize, 3];
        let shape_numbers: Vec<_> = shape
            .iter()
            .map(|&dim| unsafe { NSNumber::numberWithUnsignedInteger(dim) })
            .collect();
        let shape_array = NSArray::from_slice(&shape_numbers);

        let multi_array = unsafe {
            MLMultiArray::initWithShape_dataType_error(
                &shape_array,
                objc2_core_ml::MLMultiArrayDataType(65568),
            )
            .expect("Failed to create MLMultiArray")
        };

        // Create feature value
        let feature_value = unsafe { MLFeatureValue::featureValueWithMultiArray(&multi_array) };

        // Verify we can extract the array back
        let extracted_array = unsafe { feature_value.multiArrayValue() };
        assert!(extracted_array.is_some());
    }

    #[cfg(all(feature = "coreml", target_os = "macos"))]
    #[test]
    fn test_feature_dictionary_creation() {
        use objc2_core_ml::{MLDictionaryFeatureProvider, MLFeatureValue, MLMultiArray};
        use objc2_foundation::{NSArray, NSDictionary, NSNumber, NSString};

        // Create a test array
        let shape = vec![1_usize, 3, 4, 4];
        let shape_numbers: Vec<_> = shape
            .iter()
            .map(|&dim| unsafe { NSNumber::numberWithUnsignedInteger(dim) })
            .collect();
        let shape_array = NSArray::from_slice(&shape_numbers);

        let multi_array = unsafe {
            MLMultiArray::initWithShape_dataType_error(
                &shape_array,
                objc2_core_ml::MLMultiArrayDataType(65568),
            )
            .expect("Failed to create MLMultiArray")
        };

        // Create feature value
        let feature_value = unsafe { MLFeatureValue::featureValueWithMultiArray(&multi_array) };

        // Create dictionary
        let feature_dict = unsafe {
            let key = NSString::from_str("test_input");
            let keys = NSArray::from_id_slice(&[key.as_ref()]);
            let values = NSArray::from_id_slice(&[feature_value.as_ref()]);
            NSDictionary::dictionaryWithObjects_forKeys(&values, &keys)
        };

        // Create feature provider
        let provider =
            unsafe { MLDictionaryFeatureProvider::initWithDictionary_error(&feature_dict) };

        assert!(provider.is_ok(), "Failed to create feature provider");
    }

    #[cfg(all(feature = "coreml", target_os = "macos"))]
    #[test]
    fn test_mlmultiarray_error_cases() {
        use objc2_core_ml::MLMultiArray;
        use objc2_foundation::{NSArray, NSNumber};

        // Test with empty shape (should fail or handle gracefully)
        let empty_shape: Vec<Retained<NSNumber>> = vec![];
        let shape_array = NSArray::from_id_slice(&empty_shape);

        let result = unsafe {
            MLMultiArray::initWithShape_dataType_error(
                &shape_array,
                objc2_core_ml::MLMultiArrayDataType(65568),
            )
        };

        // Empty shape should result in an error
        assert!(result.is_err(), "Empty shape should fail");
    }
}