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
//! CoreML manifest parsing utilities
//!
//! Handles different CoreML package formats and function-based components

use super::coreml_metadata::CoreMLMetadataExtractor;
use super::file_discovery::ManifestSource;
use super::schema_extractor::{ComponentRole, SchemaExtractor};
use crate::config::model::{ComponentConfig, TensorConfig};
use anyhow::Result;
use serde_json::Value;
use std::collections::HashMap;
use std::path::Path;
use tracing::{debug, trace};

pub struct ManifestParser;

impl Default for ManifestParser {
    fn default() -> Self {
        Self::new()
    }
}

impl ManifestParser {
    pub fn new() -> Self {
        Self
    }

    /// Parse a CoreML package using enhanced manifest source detection
    pub fn parse_package_enhanced(
        &self,
        package_path: &Path,
        manifest_source: &ManifestSource,
        manifest: &Value,
        schema_extractor: &SchemaExtractor,
    ) -> Result<Vec<(String, ComponentConfig)>> {
        match manifest_source {
            ManifestSource::MetadataJson(_) | ManifestSource::ManifestJson(_) => {
                // Use existing manifest-based parsing
                self.parse_package_with_metadata_detection(package_path, manifest, schema_extractor)
            }
            ManifestSource::ModelFile(model_path) => {
                // Parse directly from model.mlmodel file
                self.parse_package_from_model_file(package_path, model_path, schema_extractor)
            }
            ManifestSource::FilenameOnly => {
                // Use pure filename-based detection
                self.parse_package_filename_only(package_path, schema_extractor)
            }
        }
    }

    /// Parse package from direct model.mlmodel file (typo-fixer style)
    fn parse_package_from_model_file(
        &self,
        package_path: &Path,
        model_path: &Path,
        schema_extractor: &SchemaExtractor,
    ) -> Result<Vec<(String, ComponentConfig)>> {
        debug!(
            "📦 Parsing from model.mlmodel file: {}",
            model_path.display()
        );

        // Use CoreML metadata extractor to get per-function tensor signatures when available
        let metadata_extractor = CoreMLMetadataExtractor::new();
        match metadata_extractor.extract_full_metadata(model_path) {
            Ok((model_inputs, model_outputs, functions)) => {
                // If per-function metadata exists, build components accordingly
                if !functions.is_empty() {
                    let mut components: Vec<(String, ComponentConfig)> = Vec::new();

                    for (fname, (inputs, outputs)) in functions {
                        // Determine role from tensors first
                        let mut role = schema_extractor.detect_component_role(&inputs, &outputs);
                        // Fallback: infer from common function names
                        if matches!(role, ComponentRole::Unknown) {
                            let lname = fname.to_lowercase();
                            if lname.contains("prefill") {
                                role = ComponentRole::FfnPrefill;
                            } else if lname.contains("infer") || lname.contains("decode") {
                                role = ComponentRole::FfnInfer;
                            }
                        }

                        let component_name = self.role_to_component_name(&role);
                        let functions_vec = vec![fname];
                        let config = ComponentConfig {
                            file_path: Some(package_path.to_string_lossy().to_string()),
                            inputs,
                            outputs,
                            functions: functions_vec,
                            input_order: None,
                        };
                        debug!(
                            "🏷️ Function component: {} (role: {:?})",
                            component_name, role
                        );
                        components.push((component_name, config));
                    }

                    // If we ended up with duplicate names (e.g., both map to ffn_prefill), keep both by disambiguating
                    // with function names appended.
                    let mut seen: HashMap<String, usize> = HashMap::new();
                    for (name, _) in &mut components {
                        let count = seen.entry(name.clone()).or_insert(0);
                        if *count > 0 {
                            // append index
                            name.push_str(&format!("_{count}"));
                        }
                        *count += 1;
                    }

                    return Ok(components);
                }

                // No per-function info; fall back to model-level IO
                debug!(
                    "ℹ️ No per-function IO; using model-level IO ({} in, {} out)",
                    model_inputs.len(),
                    model_outputs.len()
                );

                // Detect component role from tensor signatures
                let role = schema_extractor.detect_component_role(&model_inputs, &model_outputs);

                // If detection fails or tensors empty, fallback to filename
                if matches!(role, ComponentRole::Unknown)
                    || (model_inputs.is_empty() && model_outputs.is_empty())
                {
                    debug!("⚠️ Model-level detection unknown/empty, using filename fallback");
                    return self.parse_package_filename_only(package_path, schema_extractor);
                }

                let component_config = ComponentConfig {
                    file_path: Some(package_path.to_string_lossy().to_string()),
                    inputs: model_inputs,
                    outputs: model_outputs,
                    functions: Vec::new(),
                    input_order: None,
                };

                let component_name = self.role_to_component_name(&role);
                debug!(
                    "🏷️ Model file detection: {} -> {}",
                    package_path.display(),
                    component_name
                );

                Ok(vec![(component_name, component_config)])
            }
            Err(e) => {
                debug!("⚠️ Failed to extract from model.mlmodel: {}", e);
                // Fall back to filename-only detection
                self.parse_package_filename_only(package_path, schema_extractor)
            }
        }
    }

    /// Parse package using only filename patterns (ultimate fallback)
    pub fn parse_package_filename_only(
        &self,
        package_path: &Path,
        schema_extractor: &SchemaExtractor,
    ) -> Result<Vec<(String, ComponentConfig)>> {
        debug!("📦 Using filename-only parsing: {}", package_path.display());

        let filename = package_path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("unknown");

        // Use schema extractor's filename-based detection
        let role = schema_extractor.detect_component_role_from_filename(filename);

        // Try extracting tensor signatures from inner model file when available.
        // If not available (pure filename-only scenario), proceed with empty tensors.
        let inner_model = package_path.join("Data/com.apple.CoreML/model.mlmodel");
        let (inputs, outputs) = if inner_model.exists() {
            let extractor = CoreMLMetadataExtractor::new();
            match extractor.extract_tensor_signatures(&inner_model) {
                Ok((ins, outs)) => {
                    debug!(
                        "📖 Filename-only: populated tensors from inner model (inputs={}, outputs={})",
                        ins.len(),
                        outs.len()
                    );
                    (ins, outs)
                }
                Err(e) => {
                    debug!("⚠️ Filename-only: failed to extract metadata from inner model: {}. Proceeding with empty tensors.", e);
                    (HashMap::new(), HashMap::new())
                }
            }
        } else {
            debug!("ℹ️ Filename-only: no inner model file found, proceeding with empty tensors");
            (HashMap::new(), HashMap::new())
        };

        // Create component config using any extracted tensors
        let component_config = ComponentConfig {
            file_path: Some(package_path.to_string_lossy().to_string()),
            inputs,
            outputs,
            functions: Vec::new(),
            input_order: None,
        };

        let component_name = self.role_to_component_name(&role);
        debug!(
            "🏷️ Filename-only detection: {} -> {}",
            filename, component_name
        );

        Ok(vec![(component_name, component_config)])
    }

    /// Parse a CoreML package into component configurations using metadata-driven detection
    pub fn parse_package_with_metadata_detection(
        &self,
        package_path: &Path,
        manifest: &Value,
        schema_extractor: &SchemaExtractor,
    ) -> Result<Vec<(String, ComponentConfig)>> {
        let mut components = Vec::new();

        // First try to extract tensor signatures directly from the model.mlmodel file
        if let Some((inputs, outputs)) = self.extract_tensor_signatures_from_model(package_path)? {
            debug!("✅ Extracted tensor signatures from model.mlmodel file");
            let mut role = schema_extractor.detect_component_role(&inputs, &outputs);

            // If metadata-driven detection didn't yield a clear role (or tensors are empty),
            // fall back to filename-based detection to avoid returning an 'unknown' component.
            if matches!(role, ComponentRole::Unknown) || (inputs.is_empty() && outputs.is_empty()) {
                debug!("⚠️ Role unknown or empty tensors from metadata, falling back to filename-based detection");
                let filename = package_path
                    .file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or("unknown");
                role = schema_extractor.detect_component_role_from_filename(filename);
            }

            let component_config = ComponentConfig {
                file_path: Some(package_path.to_string_lossy().to_string()),
                inputs,
                outputs,
                functions: Vec::new(),
                input_order: None,
            };

            let component_name = self.role_to_component_name(&role);
            components.push((component_name, component_config));
        } else {
            debug!("⚠️ Failed to extract from model.mlmodel, falling back to manifest parsing");

            // Fall back to manifest-based extraction
            if let Some(function_components) = self.extract_function_components_with_roles(
                package_path,
                manifest,
                schema_extractor,
            )? {
                components.extend(function_components);
            } else {
                // Fall back to single component with role detection
                let component_config = self.create_base_component(package_path, manifest)?;
                let inputs = &component_config.inputs;
                let outputs = &component_config.outputs;

                // Try tensor-based detection first
                let mut role = schema_extractor.detect_component_role(inputs, outputs);

                // If tensor detection fails, try filename-based detection
                if role == ComponentRole::Unknown {
                    let filename = package_path
                        .file_name()
                        .and_then(|n| n.to_str())
                        .unwrap_or("unknown");
                    role = schema_extractor.detect_component_role_from_filename(filename);
                }

                let component_name = self.role_to_component_name(&role);
                components.push((component_name, component_config));
            }
        }

        Ok(components)
    }

    /// Parse a CoreML package into component configurations (legacy method)
    pub fn parse_package(
        &self,
        package_path: &Path,
        manifest: &Value,
        component_name: &str,
    ) -> Result<Vec<(String, ComponentConfig)>> {
        let mut components = Vec::new();

        // First try to extract function-based components
        if let Some(function_components) =
            self.extract_function_components(package_path, manifest, component_name)?
        {
            components.extend(function_components);
        } else {
            // Fall back to single component
            let component_config = self.create_base_component(package_path, manifest)?;
            components.push((component_name.to_string(), component_config));
        }

        Ok(components)
    }

    /// Extract function-based components if the package has multiple functions
    fn extract_function_components(
        &self,
        package_path: &Path,
        manifest: &Value,
        base_component_name: &str,
    ) -> Result<Option<Vec<(String, ComponentConfig)>>> {
        let functions = manifest
            .get(0)
            .and_then(|m| m.get("functions").and_then(|f| f.as_array()));

        let Some(funcs) = functions else {
            return Ok(None);
        };

        let mut function_components = Vec::new();

        for function in funcs {
            if let Some(function_name) = function.get("name").and_then(|n| n.as_str()) {
                let component_config = self.create_function_component(package_path, function)?;

                // Create component name: either "componentname_functionname" or just "functionname"
                let component_key = if funcs.len() > 1 {
                    format!("{base_component_name}_{function_name}")
                } else {
                    function_name.to_string()
                };

                debug!(
                    "📋 Function-based component '{}': inputs={:?} outputs={:?}",
                    component_key,
                    component_config.inputs.keys().collect::<Vec<_>>(),
                    component_config.outputs.keys().collect::<Vec<_>>()
                );

                function_components.push((component_key, component_config));
            }
        }

        if function_components.is_empty() {
            Ok(None)
        } else {
            Ok(Some(function_components))
        }
    }

    /// Create a base component configuration from manifest
    fn create_base_component(
        &self,
        package_path: &Path,
        manifest: &Value,
    ) -> Result<ComponentConfig> {
        let inputs = self.extract_inputs_from_manifest(manifest)?;
        let outputs = self.extract_outputs_from_manifest(manifest)?;

        Ok(ComponentConfig {
            file_path: Some(package_path.to_string_lossy().to_string()),
            inputs,
            outputs,
            functions: Vec::new(),
            input_order: None,
        })
    }

    /// Create a function-specific component configuration
    fn create_function_component(
        &self,
        package_path: &Path,
        function: &Value,
    ) -> Result<ComponentConfig> {
        let function_name = function
            .get("name")
            .and_then(|n| n.as_str())
            .unwrap_or("unknown");

        let empty_vec: Vec<Value> = Vec::new();
        let in_arr = function
            .get("inputSchema")
            .and_then(|s| s.as_array())
            .unwrap_or(&empty_vec);
        let out_arr = function
            .get("outputSchema")
            .and_then(|s| s.as_array())
            .unwrap_or(&empty_vec);

        let inputs = self.parse_tensor_schema(in_arr)?;
        let outputs = self.parse_tensor_schema(out_arr)?;

        Ok(ComponentConfig {
            file_path: Some(package_path.to_string_lossy().to_string()),
            inputs,
            outputs,
            functions: vec![function_name.to_string()],
            input_order: None,
        })
    }

    /// Extract inputs using schema extractor pattern
    fn extract_inputs_from_manifest(
        &self,
        manifest: &Value,
    ) -> Result<HashMap<String, TensorConfig>> {
        // This is a simplified version - in practice you'd use SchemaExtractor
        let mut inputs = HashMap::new();

        if let Some(input_schema) = manifest
            .get(0)
            .and_then(|m| m.get("inputSchema").and_then(|s| s.as_array()))
        {
            inputs = self.parse_tensor_schema(input_schema)?;
        }

        Ok(inputs)
    }

    /// Extract outputs using schema extractor pattern  
    fn extract_outputs_from_manifest(
        &self,
        manifest: &Value,
    ) -> Result<HashMap<String, TensorConfig>> {
        // This is a simplified version - in practice you'd use SchemaExtractor
        let mut outputs = HashMap::new();

        if let Some(output_schema) = manifest
            .get(0)
            .and_then(|m| m.get("outputSchema").and_then(|s| s.as_array()))
        {
            outputs = self.parse_tensor_schema(output_schema)?;
        }

        Ok(outputs)
    }

    /// Parse tensor schema array into tensor configs
    fn parse_tensor_schema(&self, schema: &[Value]) -> Result<HashMap<String, TensorConfig>> {
        let mut configs = HashMap::new();

        for tensor_def in schema {
            if let Some(tensor_config) = self.parse_single_tensor(tensor_def)? {
                configs.insert(tensor_config.name.clone(), tensor_config);
            }
        }

        Ok(configs)
    }

    /// Parse a single tensor definition
    fn parse_single_tensor(&self, tensor_def: &Value) -> Result<Option<TensorConfig>> {
        let (name, data_type) = match (
            tensor_def.get("name").and_then(|n| n.as_str()),
            tensor_def.get("dataType").and_then(|d| d.as_str()),
        ) {
            (Some(n), Some(d)) => (n, d),
            _ => return Ok(None),
        };

        let shape = if let Some(shape_str) = tensor_def.get("shape").and_then(|s| s.as_str()) {
            self.parse_shape_string(shape_str)?
        } else {
            vec![]
        };

        Ok(Some(TensorConfig {
            name: name.to_string(),
            shape,
            data_type: data_type.to_uppercase(),
        }))
    }

    /// Parse shape string like "[1, 64, 1024]" into Vec<usize>
    fn parse_shape_string(&self, shape_str: &str) -> Result<Vec<usize>> {
        let trimmed = shape_str.trim_start_matches('[').trim_end_matches(']');
        if trimmed.is_empty() {
            return Ok(vec![]);
        }

        let mut dims = Vec::new();
        for dim_str in trimmed.split(',') {
            match dim_str.trim().parse::<usize>() {
                Ok(dim) => dims.push(dim),
                Err(_) => return Err(anyhow::Error::msg("Failed to parse tensor dimension")),
            }
        }

        Ok(dims)
    }

    /// Extract function-based components with metadata-driven role detection
    fn extract_function_components_with_roles(
        &self,
        package_path: &Path,
        manifest: &Value,
        schema_extractor: &SchemaExtractor,
    ) -> Result<Option<Vec<(String, ComponentConfig)>>> {
        let functions = manifest
            .get(0)
            .and_then(|m| m.get("functions").and_then(|f| f.as_array()));

        let Some(funcs) = functions else {
            return Ok(None);
        };

        let mut function_components = Vec::new();

        for function in funcs {
            if let Some(function_name) = function.get("name").and_then(|n| n.as_str()) {
                let component_config = self.create_function_component(package_path, function)?;

                // Detect component role from tensor signatures
                let inputs = &component_config.inputs;
                let outputs = &component_config.outputs;
                let mut role = schema_extractor.detect_component_role(inputs, outputs);

                // Fallback: if role is Unknown, use function name to infer FFN component
                if matches!(role, ComponentRole::Unknown) {
                    if function_name == "prefill" {
                        role = ComponentRole::FfnPrefill;
                    } else if function_name == "infer" {
                        role = ComponentRole::FfnInfer;
                    }
                }

                // Use metadata-driven component name instead of function name
                let component_name = match role {
                    ComponentRole::FfnPrefill => format!("ffn_{function_name}"), // e.g., "ffn_prefill"
                    ComponentRole::FfnInfer => format!("ffn_{function_name}"), // e.g., "ffn_infer"
                    ComponentRole::FfnUnified => "ffn_prefill".to_string(), // Unified functions go to prefill
                    _ => self.role_to_component_name(&role),
                };

                debug!(
                    "📋 Metadata-driven component '{}' (role: {:?}): inputs={:?} outputs={:?}",
                    component_name,
                    role,
                    component_config.inputs.keys().collect::<Vec<_>>(),
                    component_config.outputs.keys().collect::<Vec<_>>()
                );

                function_components.push((component_name, component_config));
            }
        }

        if function_components.is_empty() {
            Ok(None)
        } else {
            Ok(Some(function_components))
        }
    }

    /// Convert component role to standard component name
    pub fn role_to_component_name(&self, role: &ComponentRole) -> String {
        match role {
            ComponentRole::Embeddings => "embeddings".to_string(),
            ComponentRole::FfnPrefill => "ffn_prefill".to_string(),
            ComponentRole::FfnInfer => "ffn_infer".to_string(),
            ComponentRole::FfnUnified => "ffn_prefill".to_string(), // Unified functions go to prefill
            ComponentRole::LmHead => "lm_head".to_string(),
            ComponentRole::Unknown => "unknown".to_string(),
        }
    }

    /// Determine execution mode from parsed components based on split vs unified FFN architecture
    /// Follows ANEMLL naming convention for FFN_PF (unified) vs separate prefill/infer (split)
    pub fn infer_execution_mode(&self, components: &[(String, ComponentConfig)]) -> String {
        // Check for split FFN architecture (separate ffn_prefill and ffn_infer components)
        let has_ffn_prefill = components.iter().any(|(name, _)| name == "ffn_prefill");
        let has_ffn_infer = components.iter().any(|(name, _)| name == "ffn_infer");

        if has_ffn_prefill && has_ffn_infer {
            trace!("🔧 Found separate ffn_prefill and ffn_infer components - using split mode");
            return "split".to_string();
        }

        // Check for ANEMLL unified FFN pattern (FFN_PF in filename)
        // This indicates a single model that handles both prefill and infer modes
        let has_ffn_pf_pattern = components.iter().any(|(_, config)| {
            if let Some(file_path) = &config.file_path {
                let filename = std::path::Path::new(file_path)
                    .file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or("")
                    .to_lowercase();
                filename.contains("ffn_pf")
            } else {
                false
            }
        });

        if has_ffn_pf_pattern {
            debug!("🔧 Found ANEMLL FFN_PF pattern in filename - using unified mode");
            return "unified".to_string();
        }

        // Check for unified FFN (single component with multiple functions)
        let multi_function_components = components
            .iter()
            .filter(|(_, config)| config.functions.len() > 1)
            .count();

        if multi_function_components > 0 {
            debug!(
                "🔧 Found {} multi-function components - using unified mode",
                multi_function_components
            );
            "unified".to_string()
        } else {
            // Check if we have any FFN-like component
            let has_ffn_like = components.iter().any(|(name, _)| name.starts_with("ffn"));

            if has_ffn_like {
                debug!("🔧 Found FFN component(s) but no clear split pattern - defaulting to unified mode");
                "unified".to_string()
            } else {
                debug!("🔧 Standard component structure - defaulting to unified mode");
                "unified".to_string()
            }
        }
    }

    /// Extract tensor signatures directly from model.mlmodel file using CoreML metadata
    #[allow(clippy::type_complexity)]
    fn extract_tensor_signatures_from_model(
        &self,
        package_path: &Path,
    ) -> Result<Option<(HashMap<String, TensorConfig>, HashMap<String, TensorConfig>)>> {
        // Look for the model.mlmodel file within the package
        let model_file_path = package_path.join("Data/com.apple.CoreML/model.mlmodel");

        if !model_file_path.exists() {
            // Try alternative path structure
            let alt_model_path = package_path.join("model.mlmodel");
            if !alt_model_path.exists() {
                debug!("❌ No model.mlmodel found in {}", package_path.display());
                return Ok(None);
            }
        }

        debug!(
            "🔍 Attempting to extract metadata from: {}",
            model_file_path.display()
        );

        // Use CoreML metadata extractor
        let extractor = CoreMLMetadataExtractor::new();
        match extractor.extract_tensor_signatures(&model_file_path) {
            Ok((inputs, outputs)) => {
                debug!(
                    "✅ Successfully extracted {} inputs and {} outputs",
                    inputs.len(),
                    outputs.len()
                );
                Ok(Some((inputs, outputs)))
            }
            Err(e) => {
                debug!("⚠️ Failed to extract metadata: {}", e);
                Ok(None)
            }
        }
    }
}