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
//! BERT Performance Comparison: Candle vs CoreML
//!
//! Comprehensive benchmark comparing BERT inference performance between:
//! - Candle (CPU/Metal) using .safetensors weights
//! - CoreML runtime using .mlpackage/.mlmodelc files
//!
//! Features:
//! - Multiple sequence lengths testing
//! - Loading time measurement  
//! - Cold vs warm inference comparison
//! - Throughput calculation
//! - Memory usage analysis
//!
//! Usage:
//! ```bash
//! # Run full benchmark suite
//! cargo run --example bert_comparison
//!
//! # Test specific sequence length
//! cargo run --example bert_comparison -- --sequence-lengths "128"
//!
//! # Quick test with fewer iterations
//! cargo run --example bert_comparison -- --warmup 1 --iterations 3
//! ```

use anyhow::{Error as E, Result};
use candle_core::{Device, Tensor};
use candle_nn::VarBuilder;
use candle_transformers::models::bert::{BertModel, Config, DTYPE};
use clap::Parser;
use hf_hub::{api::sync::Api, Repo, RepoType};
use std::path::PathBuf;
use std::time::{Duration, Instant};
use tokenizers::Tokenizer;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
    /// Number of warmup iterations
    #[arg(long, default_value = "3")]
    warmup: usize,

    /// Number of benchmark iterations  
    #[arg(long, default_value = "10")]
    iterations: usize,

    /// Test sequence lengths (comma-separated)
    #[arg(long, default_value = "64,128,256")]
    sequence_lengths: String,

    /// Test prompt for inference
    #[arg(long, default_value = "Paris is the capital of France.")]
    prompt: String,

    /// Enable verbose output
    #[arg(long)]
    verbose: bool,

    /// Use local model files instead of downloading
    #[arg(long)]
    local_models: bool,

    /// CoreML model repository to use on HuggingFace Hub
    #[arg(long, default_value = "google-bert/bert-base-uncased")]
    coreml_model_id: String,

    /// CoreML model revision (branch/tag)
    #[arg(long, default_value = "main")]
    coreml_revision: String,
}

#[derive(Debug)]
struct BenchmarkResult {
    name: String,
    sequence_length: usize,
    loading_time: Duration,
    cold_inference: Duration,
    warm_inference: Duration,
    throughput: f64, // tokens/second
    memory_mb: Option<f64>,
}

impl BenchmarkResult {
    fn print(&self) {
        println!(
            "\n=== {} (seq_len: {}) ===",
            self.name, self.sequence_length
        );
        println!("Loading time:     {:?}", self.loading_time);
        println!("Cold inference:   {:?}", self.cold_inference);
        println!("Warm inference:   {:?}", self.warm_inference);
        println!("Throughput:       {:.1} tokens/sec", self.throughput);
        if let Some(mem) = self.memory_mb {
            println!("Memory usage:     {mem:.1} MB");
        }
    }

    fn efficiency_score(&self) -> f64 {
        // Simple efficiency metric: throughput / loading_time_seconds
        self.throughput / self.loading_time.as_secs_f64()
    }
}

fn benchmark_candle_bert(device: &Device, args: &Args, seq_len: usize) -> Result<BenchmarkResult> {
    println!("🔧 Benchmarking Candle BERT on {device:?} (seq_len: {seq_len})");

    let start = Instant::now();

    // Model setup - use local files if specified, otherwise download
    let (model, tokenizer) = if args.local_models {
        load_local_bert_model(device)?
    } else {
        load_huggingface_bert_model(device)?
    };

    let loading_time = start.elapsed();

    // Prepare input
    let encoding = tokenizer
        .encode(args.prompt.clone(), true)
        .map_err(E::msg)?;

    let mut token_ids = encoding.get_ids().to_vec();

    // Pad or truncate to sequence length
    token_ids.resize(seq_len, 0);

    let token_ids = Tensor::new(&token_ids[..], device)?.unsqueeze(0)?;
    let token_type_ids = token_ids.zeros_like()?;

    // Cold inference
    let start = Instant::now();
    let _embeddings = model.forward(&token_ids, &token_type_ids, None)?;
    let cold_inference = start.elapsed();

    // Warmup
    for _ in 0..args.warmup {
        let _ = model.forward(&token_ids, &token_type_ids, None)?;
    }

    // Warm inference benchmark
    let start = Instant::now();
    for _ in 0..args.iterations {
        let _ = model.forward(&token_ids, &token_type_ids, None)?;
    }
    let total_time = start.elapsed();
    let warm_inference = total_time / args.iterations as u32;

    // Calculate throughput
    let total_tokens = seq_len * args.iterations;
    let throughput = total_tokens as f64 / total_time.as_secs_f64();

    Ok(BenchmarkResult {
        name: format!("Candle-{device:?}"),
        sequence_length: seq_len,
        loading_time,
        cold_inference,
        warm_inference,
        throughput,
        memory_mb: None, // Could add memory tracking here
    })
}

#[cfg(target_os = "macos")]
fn benchmark_coreml_bert(args: &Args, seq_len: usize) -> Result<BenchmarkResult> {
    use candle_coreml::{Config as CoreMLConfig, CoreMLModel};

    println!("🍎 Benchmarking CoreML BERT (seq_len: {seq_len})");

    let start = Instant::now();

    // Determine model path
    let model_path = if args.local_models {
        // Use local test model
        PathBuf::from(format!(
            "{}/bert-model-test/coreml/fill-mask/bert-compiled.mlmodelc/float32_model.mlmodelc",
            env!("CARGO_MANIFEST_DIR")
        ))
    } else {
        // Download from HuggingFace Hub
        println!(
            "🔄 Downloading CoreML BERT model from {}...",
            args.coreml_model_id
        );

        let repo = Repo::with_revision(
            args.coreml_model_id.clone(),
            RepoType::Model,
            args.coreml_revision.clone(),
        );
        let api = Api::new()?;
        let api = api.repo(repo);

        // Try to find available CoreML models in the repository
        let model_patterns = [
            "coreml/fill-mask/float32_model.mlpackage/Data/com.apple.CoreML/model.mlmodel",
            "coreml/bert-base-uncased.mlpackage/Data/com.apple.CoreML/model.mlmodel",
            "bert-base-uncased.mlpackage/Data/com.apple.CoreML/model.mlmodel",
            "model.mlpackage/Data/com.apple.CoreML/model.mlmodel",
            "model.mlmodelc",
            "bert.mlmodelc",
        ];

        let mut found_model_file = None;
        let mut mlpackage_name = None;

        for pattern in &model_patterns {
            if let Ok(model_file) = api.get(pattern) {
                // Extract the .mlpackage name from the path
                let path_components: Vec<&str> = pattern.split('/').collect();
                if let Some(package_component) = path_components.first() {
                    if package_component.ends_with(".mlpackage") {
                        mlpackage_name = Some(package_component.trim_end_matches(".mlpackage"));
                    }
                }

                // Also download associated files for .mlpackage models
                if pattern.contains(".mlpackage") {
                    let base_path = pattern.replace("/Data/com.apple.CoreML/model.mlmodel", "");
                    let _ = api.get(&format!(
                        "{base_path}/Data/com.apple.CoreML/weights/weight.bin"
                    ));
                    let _ = api.get(&format!("{base_path}/Manifest.json"));
                }

                found_model_file = Some(model_file);
                break;
            }
        }

        let model_file = found_model_file.ok_or_else(|| {
            E::msg(format!(
                "No CoreML BERT model found in repository {}. Try using --local-models flag.",
                args.coreml_model_id
            ))
        })?;

        // If this is an .mlmodel file, compile it
        if model_file.extension().and_then(|s| s.to_str()) == Some("mlmodel") {
            let model_name = mlpackage_name.unwrap_or("bert");
            let compiled_model_name = format!("{model_name}.mlmodelc");

            let mlpackage_dir = if model_file.to_string_lossy().contains(".mlpackage") {
                model_file
                    .parent()
                    .unwrap()
                    .parent()
                    .unwrap()
                    .parent()
                    .unwrap()
            } else {
                model_file.parent().unwrap()
            };

            let cache_dir = mlpackage_dir.parent().unwrap();
            let compiled_model_path = cache_dir.join("compiled_models").join(&compiled_model_name);

            if !compiled_model_path.exists() {
                println!("🔨 Compiling CoreML model (this may take a moment)...");
                std::fs::create_dir_all(compiled_model_path.parent().unwrap())?;

                let source_path =
                    if mlpackage_dir.extension().and_then(|s| s.to_str()) == Some("mlpackage") {
                        mlpackage_dir
                    } else {
                        &model_file
                    };

                let output = std::process::Command::new("xcrun")
                    .args([
                        "coremlc",
                        "compile",
                        &source_path.to_string_lossy(),
                        &compiled_model_path.to_string_lossy(),
                    ])
                    .output()
                    .map_err(|e| E::msg(format!("Failed to run coremlc: {e}")))?;

                if !output.status.success() {
                    let stderr = String::from_utf8_lossy(&output.stderr);
                    return Err(E::msg(format!("CoreML compilation failed: {stderr}")));
                }

                println!("✅ CoreML model compiled successfully");
            }

            // Return path to the actual compiled model directory (may be nested)
            // Check for various possible nested structures
            let possible_paths = [
                compiled_model_path.join(&compiled_model_name),
                compiled_model_path
                    .join(&compiled_model_name)
                    .join("float32_model.mlmodelc"),
                compiled_model_path.join("float32_model.mlmodelc"),
                compiled_model_path.clone(),
            ];

            let mut final_path = compiled_model_path.clone();
            for path in &possible_paths {
                if path.exists() {
                    final_path = path.clone();
                    break;
                }
            }

            final_path
        } else {
            // Already a compiled model
            model_file
        }
    };

    let config = CoreMLConfig {
        input_names: vec!["input_ids".to_string(), "attention_mask".to_string()],
        output_name: "token_scores".to_string(),
        max_sequence_length: seq_len,
        vocab_size: 30522,
        model_type: "bert-base-uncased".to_string(),
    };

    let model = CoreMLModel::load_from_file(&model_path, &config)?;
    let loading_time = start.elapsed();

    // Prepare input tensors (both input_ids and attention_mask)
    let device = Device::Cpu;
    let input_data: Vec<i64> = (0..seq_len).map(|i| (i % 30522) as i64).collect();
    let attention_data: Vec<i64> = vec![1; seq_len]; // All tokens are real (not padding)

    let input_ids = Tensor::new(&input_data[..], &device)?.unsqueeze(0)?;
    let attention_mask = Tensor::new(&attention_data[..], &device)?.unsqueeze(0)?;

    // Cold inference
    let start = Instant::now();
    let _ = model.forward(&[&input_ids, &attention_mask])?;
    let cold_inference = start.elapsed();

    // Warmup
    for _ in 0..args.warmup {
        let _ = model.forward(&[&input_ids, &attention_mask])?;
    }

    // Warm inference benchmark
    let start = Instant::now();
    for _ in 0..args.iterations {
        let _ = model.forward(&[&input_ids, &attention_mask])?;
    }
    let total_time = start.elapsed();
    let warm_inference = total_time / args.iterations as u32;

    // Calculate throughput
    let total_tokens = seq_len * args.iterations;
    let throughput = total_tokens as f64 / total_time.as_secs_f64();

    Ok(BenchmarkResult {
        name: "CoreML".to_string(),
        sequence_length: seq_len,
        loading_time,
        cold_inference,
        warm_inference,
        throughput,
        memory_mb: None,
    })
}

fn load_local_bert_model(device: &Device) -> Result<(BertModel, Tokenizer)> {
    // Load from local files (for testing without internet)
    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    let model_dir = format!("{manifest_dir}/bert-model-test");

    let config_path = format!("{model_dir}/config.json");
    let tokenizer_path = format!("{model_dir}/tokenizer.json");
    let weights_path = format!("{model_dir}/model.safetensors");

    let config: Config = serde_json::from_slice(
        &std::fs::read(&config_path)
            .map_err(|e| E::msg(format!("Failed to read config from {config_path}: {e}")))?,
    )?;

    let tokenizer = Tokenizer::from_file(&tokenizer_path).map_err(|e| {
        E::msg(format!(
            "Failed to load tokenizer from {tokenizer_path}: {e}"
        ))
    })?;

    let vb = unsafe { VarBuilder::from_mmaped_safetensors(&[weights_path], DTYPE, device)? };
    let model = BertModel::load(vb, &config)?;

    Ok((model, tokenizer))
}

fn load_huggingface_bert_model(device: &Device) -> Result<(BertModel, Tokenizer)> {
    // Download from HuggingFace Hub
    use hf_hub::{api::sync::Api, Repo, RepoType};

    let api = Api::new()?;
    let repo = api.repo(Repo::new(
        "google-bert/bert-base-uncased".to_string(),
        RepoType::Model,
    ));

    let config_filename = repo.get("config.json")?;
    let tokenizer_filename = repo.get("tokenizer.json")?;
    let weights_filename = repo.get("model.safetensors")?;

    let config = std::fs::read_to_string(config_filename)?;
    let config: Config = serde_json::from_str(&config)?;

    let tokenizer = Tokenizer::from_file(tokenizer_filename).map_err(E::msg)?;

    let vb = unsafe { VarBuilder::from_mmaped_safetensors(&[&weights_filename], DTYPE, device)? };
    let model = BertModel::load(vb, &config)?;

    Ok((model, tokenizer))
}

fn print_comparison_summary(results: &[BenchmarkResult]) {
    println!("\n📊 BENCHMARK SUMMARY");
    println!("===================");

    // Group by sequence length
    let mut seq_lengths: Vec<_> = results.iter().map(|r| r.sequence_length).collect();
    seq_lengths.sort_unstable();
    seq_lengths.dedup();

    for seq_len in seq_lengths {
        println!("\n📏 Sequence Length: {seq_len}");
        println!("┌─────────────â”Ŧ─────────────â”Ŧ─────────────â”Ŧ─────────────â”Ŧ─────────────┐");
        println!("│ Backend     │ Loading     │ Cold Inf.   │ Warm Inf.   │ Throughput  │");
        println!("├─────────────â”ŧ─────────────â”ŧ─────────────â”ŧ─────────────â”ŧ─────────────┤");

        let seq_results: Vec<_> = results
            .iter()
            .filter(|r| r.sequence_length == seq_len)
            .collect();

        for result in &seq_results {
            println!(
                "│ {:11} │ {:9.1?} │ {:9.1?} │ {:9.1?} │ {:8.1} t/s │",
                result.name,
                result.loading_time,
                result.cold_inference,
                result.warm_inference,
                result.throughput
            );
        }
        println!("└─────────────┴─────────────┴─────────────┴─────────────┴─────────────┘");

        // Find best performers
        if let Some(fastest_load) = seq_results.iter().min_by_key(|r| r.loading_time) {
            println!("🚀 Fastest loading: {}", fastest_load.name);
        }
        if let Some(best_throughput) = seq_results
            .iter()
            .max_by(|a, b| a.throughput.partial_cmp(&b.throughput).unwrap())
        {
            println!(
                "⚡ Best throughput: {} ({:.1} tokens/sec)",
                best_throughput.name, best_throughput.throughput
            );
        }
        if let Some(most_efficient) = seq_results.iter().max_by(|a, b| {
            a.efficiency_score()
                .partial_cmp(&b.efficiency_score())
                .unwrap()
        }) {
            println!(
                "đŸŽ¯ Most efficient: {} (score: {:.1})",
                most_efficient.name,
                most_efficient.efficiency_score()
            );
        }
    }
}

fn main() -> Result<()> {
    let args = Args::parse();

    println!("đŸ”Ŧ BERT Performance Benchmark");
    println!("============================");
    println!("Warmup iterations: {}", args.warmup);
    println!("Benchmark iterations: {}", args.iterations);
    println!("Test prompt: \"{}\"", args.prompt);

    let sequence_lengths: Vec<usize> = args
        .sequence_lengths
        .split(',')
        .map(|s| s.trim().parse().unwrap())
        .collect();

    let mut all_results = Vec::new();

    for &seq_len in &sequence_lengths {
        println!("\n🔄 Testing sequence length: {seq_len}");

        // Test CPU
        if let Ok(result) = benchmark_candle_bert(&Device::Cpu, &args, seq_len) {
            result.print();
            all_results.push(result);
        }

        // Test Metal (if available)
        if let Ok(metal_device) = Device::new_metal(0) {
            if let Ok(result) = benchmark_candle_bert(&metal_device, &args, seq_len) {
                result.print();
                all_results.push(result);
            }
        } else if args.verbose {
            println!("â„šī¸  Metal device not available, skipping Metal benchmark");
        }

        // Test CoreML (macOS only)
        #[cfg(target_os = "macos")]
        {
            match benchmark_coreml_bert(&args, seq_len) {
                Ok(result) => {
                    result.print();
                    all_results.push(result);
                }
                Err(e) => {
                    if args.verbose {
                        println!("âš ī¸  CoreML benchmark failed: {e}");
                    }
                }
            }
        }

        #[cfg(not(target_os = "macos"))]
        {
            if args.verbose {
                println!("â„šī¸  CoreML not available (requires macOS and 'coreml' feature)");
            }
        }
    }

    if !all_results.is_empty() {
        print_comparison_summary(&all_results);
    } else {
        println!("❌ No successful benchmarks completed");
    }

    Ok(())
}