rustorch 0.6.29

Production-ready PyTorch-compatible deep learning library in Rust with special mathematical functions (gamma, Bessel, error functions), statistical distributions, Fourier transforms (FFT/RFFT), matrix decomposition (SVD/QR/LU/eigenvalue), automatic differentiation, neural networks, computer vision transforms, complete GPU acceleration (CUDA/Metal/OpenCL), SIMD optimizations, parallel processing, WebAssembly browser support, comprehensive distributed learning support, and performance validation
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
# RusTorch CoreML統合 実装ロードマップ

> **作成日**: 2025-09-19
> **基準ドキュメント**: 01-gpu-usage-analysis.md, 02-coreml-compatibility-matrix.md
> **実装ブランチ**: feature/coreml-integration
> **想定期間**: 18週間 (4.5ヶ月)

## 🎯 Executive Summary

RusTorchへのCoreML統合を**3つのPhase**に分けて段階的に実装します。各Phaseは独立してテスト・デプロイ可能な設計とし、リスクを最小化しながら着実な進歩を目指します。

### 📊 実装概要

| Phase | 期間 | 主要機能 | 期待効果 | リスク |
|-------|------|----------|----------|--------|
| **Phase 1** | 6週間 | 基礎演算 + デバイス管理 | +30%性能 | 🟢 低 |
| **Phase 2** | 7週間 | CNN + 正規化 + メモリ統合 | +50%性能 | 🟡 中 |
| **Phase 3** | 5週間 | システム統合 + 最適化 | +60%性能 | 🟠 高 |

---

## 🚀 Phase 1: 基礎演算実装 (Week 1-6)

### 🎯 目標
- CoreMLデバイス管理の基盤構築
- 基本的なテンソル演算のCoreML実装
- 既存GPUコードとの互換性確保

### 📅 Week 1-2: 基盤アーキテクチャ

#### Week 1: プロジェクト準備 & 依存関係
```bash
# Cargo.toml依存関係追加
[target.'cfg(target_os = "macos")'.dependencies]
objc2-core-ml = { version = "0.2", optional = true }
objc2-foundation = { version = "0.2", optional = true }

[features]
coreml = ["dep:objc2-core-ml", "dep:objc2-foundation"]
```

**実装タスク**:
- [ ] プロジェクト構造の準備
- [ ] 依存関係の設定とテスト
- [ ] CI/CDでのmacOS環境設定
- [ ] 基本的なobjc2-core-mlテスト

#### Week 2: DeviceType拡張
**ファイル**: `src/gpu/mod.rs`

```rust
// DeviceTypeの拡張
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DeviceType {
    Cpu,
    Cuda(usize),
    Metal(usize),
    OpenCL(usize),
    CoreML(usize),     // 🆕 新規追加
}

impl DeviceType {
    pub fn is_coreml(&self) -> bool {
        matches!(self, DeviceType::CoreML(_))
    }

    pub fn is_apple_hardware(&self) -> bool {
        matches!(self, DeviceType::Metal(_) | DeviceType::CoreML(_))
    }
}
```

**実装タスク**:
- [ ] DeviceType::CoreMLの追加
- [ ] デバイス検出ロジックの実装
- [ ] GpuContextのCoreML対応
- [ ] 基本的なDeviceManager拡張

### 📅 Week 3-4: 基本演算実装

#### Week 3: 要素ごと演算
**新規ファイル**: `src/gpu/coreml/mod.rs`

```rust
use objc2_core_ml::*;
use objc2_foundation::*;

pub struct CoreMLExecutor {
    device: MLCDevice,
    context: MLCTrainingGraph,
}

impl CoreMLExecutor {
    pub fn new(device_id: usize) -> Result<Self> {
        let device = if let Some(device) = MLCDevice::aneDevice() {
            device  // Apple Neural Engine優先
        } else {
            MLCDevice::gpuDevice()? // Metal GPU フォールバック
        };

        let context = MLCTrainingGraph::new();
        Ok(CoreMLExecutor { device, context })
    }

    // 要素ごと加算
    pub fn elementwise_add(&self, a: &MLCTensor, b: &MLCTensor) -> Result<MLCTensor> {
        let add_layer = MLCArithmeticLayer::layer_with_operation(.add);
        let result = self.context.nodeWithLayer_sources(&add_layer, &[a, b])?;
        Ok(result.resultTensors()[0].clone())
    }
}
```

**実装タスク**:
- [ ] CoreMLExecutor基本実装
- [ ] elementwise_add/sub/mul/div
- [ ] Tensor ⇄ MLCTensor変換
- [ ] エラーハンドリング統合

#### Week 4: 活性化関数
```rust
impl CoreMLExecutor {
    pub fn relu(&self, input: &MLCTensor) -> Result<MLCTensor> {
        let relu_layer = MLCActivationLayer::layer_with_descriptor(
            &MLCActivationDescriptor::descriptor_with_type(.relu)
        );
        // ... 実装
    }

    pub fn gelu(&self, input: &MLCTensor) -> Result<MLCTensor> {
        let gelu_layer = MLCActivationLayer::layer_with_descriptor(
            &MLCActivationDescriptor::descriptor_with_type(.gelu)
        );
        // ... 実装
    }
}
```

**実装タスク**:
- [ ] ReLU、GELU、Softmax実装
- [ ] 活性化関数ベンチマーク
- [ ] Tensor trait統合
- [ ] 単体テスト作成

### 📅 Week 5-6: 行列演算 & 統合

#### Week 5: 行列演算
```rust
impl CoreMLExecutor {
    pub fn matmul(&self, a: &MLCTensor, b: &MLCTensor) -> Result<MLCTensor> {
        let matmul_layer = MLCMatMulLayer::layer();
        let result = self.context.nodeWithLayer_sources(&matmul_layer, &[a, b])?;
        Ok(result.resultTensors()[0].clone())
    }

    pub fn batch_matmul(&self, a: &MLCTensor, b: &MLCTensor) -> Result<MLCTensor> {
        // バッチ次元を考慮した行列乗算
        // ... 実装
    }
}
```

**実装タスク**:
- [ ] 基本行列乗算実装
- [ ] バッチ行列乗算実装
- [ ] 性能ベンチマーク作成
- [ ] 既存trait統合

#### Week 6: 既存システム統合
**ファイル**: `src/tensor/core.rs`

```rust
impl<T> Tensor<T>
where T: CoreMLCompatible
{
    pub fn to_coreml(&self) -> Result<Self> {
        if self.device().is_coreml() {
            return Ok(self.clone());
        }

        let coreml_device = DeviceType::CoreML(0);
        let mut result = self.clone();
        result.device = coreml_device;
        // データ転送実装...
        Ok(result)
    }

    // ハイブリッド実装
    pub fn matmul(&self, other: &Self) -> Result<Self> {
        match (self.device(), other.device()) {
            (DeviceType::CoreML(_), DeviceType::CoreML(_)) => {
                self.coreml_matmul(other)
            }
            _ => self.gpu_matmul(other) // 既存実装
        }
    }
}
```

**実装タスク**:
- [ ] Tensor<T>のCoreML統合
- [ ] デバイス間転送機能
- [ ] ハイブリッド実行戦略
- [ ] 包括的テストスイート

### 🎯 Phase 1 マイルストーン & 成果物

#### 📋 成果物
- [ ] CoreML基本実行エンジン
- [ ] 要素演算・活性化関数・行列演算
- [ ] 既存システムとの統合
- [ ] 性能ベンチマークスイート

#### 📊 期待効果
- **性能向上**: +25-35% (要素演算・行列演算)
- **メモリ効率**: +10-20% (Metal統合効果)
- **バッテリー効率**: +15-25% (Apple最適化)

---

## 🧠 Phase 2: CNN実装 & システム拡張 (Week 7-13)

### 🎯 目標
- 畳み込みニューラルネットワークの完全対応
- 正規化レイヤーの実装
- メモリ管理の最適化

### 📅 Week 7-8: 畳み込み演算

#### Week 7: 基本畳み込み
**新規ファイル**: `src/gpu/coreml/cnn.rs`

```rust
pub struct CoreMLConvolution {
    executor: CoreMLExecutor,
}

impl CoreMLConvolution {
    pub fn conv2d(&self, input: &MLCTensor, weight: &MLCTensor,
                  stride: (usize, usize), padding: (usize, usize)) -> Result<MLCTensor> {
        let conv_desc = MLCConvolutionDescriptor::descriptor_with_kernelSizes_inputFeatureChannelCount_outputFeatureChannelCount_groupCount_strides_dilationRates_paddingPolicy_paddingSizes(
            &NSArray::from_slice(&[weight.shape()[2], weight.shape()[3]]), // kernel size
            weight.shape()[1] as NSUInteger, // input channels
            weight.shape()[0] as NSUInteger, // output channels
            1, // groups
            &NSArray::from_slice(&[stride.0, stride.1]), // strides
            &NSArray::from_slice(&[1, 1]), // dilation
            MLCPaddingPolicy::usePaddingSize,
            &NSArray::from_slice(&[padding.0, padding.1]), // padding
        );

        let conv_layer = MLCConvolutionLayer::layer_with_weights_biases_descriptor(
            &MLCTensor::from_data(&weight.data()),
            ptr::null(), // no bias for now
            &conv_desc
        );

        let result = self.executor.context.nodeWithLayer_sources(&conv_layer, &[input])?;
        Ok(result.resultTensors()[0].clone())
    }
}
```

**実装タスク**:
- [ ] 2D畳み込みレイヤー実装
- [ ] パディング・ストライド対応
- [ ] バイアス項の対応
- [ ] 畳み込み性能ベンチマーク

#### Week 8: プーリング演算
```rust
impl CoreMLConvolution {
    pub fn max_pool2d(&self, input: &MLCTensor, kernel_size: (usize, usize),
                      stride: (usize, usize)) -> Result<MLCTensor> {
        let pool_desc = MLCPoolingDescriptor::maxPooling_with_kernelSizes_strides_paddingPolicy_paddingSizes(
            &NSArray::from_slice(&[kernel_size.0, kernel_size.1]),
            &NSArray::from_slice(&[stride.0, stride.1]),
            MLCPaddingPolicy::valid,
            ptr::null()
        );

        let pool_layer = MLCPoolingLayer::layer_with_descriptor(&pool_desc);
        let result = self.executor.context.nodeWithLayer_sources(&pool_layer, &[input])?;
        Ok(result.resultTensors()[0].clone())
    }

    pub fn avg_pool2d(&self, input: &MLCTensor, kernel_size: (usize, usize),
                      stride: (usize, usize)) -> Result<MLCTensor> {
        // 平均プーリング実装...
    }
}
```

**実装タスク**:
- [ ] Max Pooling実装
- [ ] Average Pooling実装
- [ ] Adaptive Pooling実装
- [ ] プーリング性能テスト

### 📅 Week 9-10: 正規化レイヤー

#### Week 9: バッチ正規化
**新規ファイル**: `src/gpu/coreml/normalization.rs`

```rust
pub struct CoreMLNormalization {
    executor: CoreMLExecutor,
}

impl CoreMLNormalization {
    pub fn batch_norm(&self, input: &MLCTensor, weight: &MLCTensor,
                      bias: &MLCTensor, mean: &MLCTensor, variance: &MLCTensor,
                      epsilon: f32) -> Result<MLCTensor> {
        let bn_desc = MLCBatchNormalizationLayer::layer_with_featureChannelCount_mean_variance_beta_gamma_varianceEpsilon_momentum(
            input.shape()[1] as NSUInteger, // feature channels
            Some(&mean),
            Some(&variance),
            Some(&bias),
            Some(&weight),
            epsilon,
            0.9 // momentum
        );

        let result = self.executor.context.nodeWithLayer_sources(&bn_desc, &[input])?;
        Ok(result.resultTensors()[0].clone())
    }

    pub fn layer_norm(&self, input: &MLCTensor, normalized_shape: &[usize],
                      weight: Option<&MLCTensor>, bias: Option<&MLCTensor>,
                      epsilon: f32) -> Result<MLCTensor> {
        let ln_desc = MLCLayerNormalizationLayer::layer_with_normalizedShape_beta_gamma_varianceEpsilon(
            &NSArray::from_slice(normalized_shape),
            bias,
            weight,
            epsilon
        );

        let result = self.executor.context.nodeWithLayer_sources(&ln_desc, &[input])?;
        Ok(result.resultTensors()[0].clone())
    }
}
```

**実装タスク**:
- [ ] バッチ正規化実装
- [ ] レイヤー正規化実装
- [ ] インスタンス正規化実装
- [ ] 正規化統合テスト

#### Week 10: リダクション演算
```rust
pub fn reduce_sum(&self, input: &MLCTensor, axes: &[usize], keep_dims: bool) -> Result<MLCTensor> {
    let reduce_desc = MLCReductionLayer::layer_with_reductionType_dimension_keepDimensions(
        MLCReductionType::sum,
        axes[0] as NSUInteger,
        keep_dims
    );

    let result = self.executor.context.nodeWithLayer_sources(&reduce_desc, &[input])?;
    Ok(result.resultTensors()[0].clone())
}
```

**実装タスク**:
- [ ] Sum/Mean/Max/Min リダクション
- [ ] 多次元リダクション対応
- [ ] Keep_dims オプション
- [ ] リダクション性能テスト

### 📅 Week 11-12: メモリ統合 & 最適化

#### Week 11: Metal-CoreML統合
**新規ファイル**: `src/gpu/coreml/memory.rs`

```rust
pub struct CoreMLMemoryManager {
    metal_device: MTLDevice,
    coreml_device: MLCDevice,
}

impl CoreMLMemoryManager {
    pub fn create_shared_buffer(&self, size: usize) -> Result<SharedBuffer> {
        // Metal Buffer作成
        let metal_buffer = self.metal_device.newBufferWithLength_options(
            size,
            MTLResourceOptions::StorageModeShared
        );

        // CoreML Tensorとして参照
        let coreml_tensor = MLCTensor::tensorWithBuffer_shape_dataType(
            &metal_buffer,
            &tensor_shape,
            MLCDataType::float32
        );

        Ok(SharedBuffer {
            metal_buffer,
            coreml_tensor,
        })
    }

    pub fn zero_copy_transfer(&self, from: &Tensor<f32>, to: DeviceType) -> Result<Tensor<f32>> {
        match (from.device(), to) {
            (DeviceType::Metal(_), DeviceType::CoreML(_)) => {
                // Metal → CoreMLのゼロコピー転送
                self.metal_to_coreml_zero_copy(from)
            }
            (DeviceType::CoreML(_), DeviceType::Metal(_)) => {
                // CoreML → Metalのゼロコピー転送
                self.coreml_to_metal_zero_copy(from)
            }
            _ => {
                // フォールバック: 通常のコピー
                from.to_device(to)
            }
        }
    }
}
```

**実装タスク**:
- [ ] Metal-CoreML共有メモリ実装
- [ ] ゼロコピー転送の実装
- [ ] メモリ使用量最適化
- [ ] メモリリーク検出テスト

#### Week 12: 実行戦略の高度化
**ファイル**: `src/gpu/coreml/strategy.rs`

```rust
pub struct HybridExecutionStrategy {
    coreml_threshold: usize,    // CoreML実行の閾値
    memory_threshold: usize,    // メモリ使用量閾値
    battery_mode: bool,         // バッテリー最適化モード
}

impl HybridExecutionStrategy {
    pub fn select_executor(&self, operation: &Operation, input_size: usize) -> ExecutorType {
        // バッテリーモードではCoreML優先
        if self.battery_mode {
            return ExecutorType::CoreML;
        }

        // 大きなテンソルはCoreMLが有利
        if input_size > self.coreml_threshold {
            return ExecutorType::CoreML;
        }

        // メモリプレッシャーがある場合はCoreML
        if self.current_memory_usage() > self.memory_threshold {
            return ExecutorType::CoreML;
        }

        // 演算タイプ別の最適選択
        match operation {
            Operation::Conv2D { .. } => ExecutorType::CoreML, // 畳み込みはCoreML最適
            Operation::MatMul { size, .. } if size > 1024 => ExecutorType::CoreML,
            _ => ExecutorType::Metal // その他はMetal
        }
    }
}
```

**実装タスク**:
- [ ] ハイブリッド実行戦略
- [ ] 動的最適化ロジック
- [ ] バッテリー効率モード
- [ ] 性能プロファイリング統合

### 📅 Week 13: Phase 2統合テスト

**実装タスク**:
- [ ] CNN推論エンドツーエンドテスト
- [ ] ResNet, VGG等での検証
- [ ] メモリ効率テスト
- [ ] 性能回帰テストスイート

### 🎯 Phase 2 マイルストーン & 成果物

#### 📋 成果物
- [ ] 完全なCNN演算サポート
- [ ] 正規化レイヤー統合
- [ ] Metal-CoreML メモリ統合
- [ ] ハイブリッド実行戦略

#### 📊 期待効果
- **CNN性能**: +40-70% (畳み込み・プーリング)
- **メモリ効率**: +25-40% (共有メモリ活用)
- **バッテリー効率**: +30-50% (CoreML最適化)

---

## 🔧 Phase 3: システム統合 & 最適化 (Week 14-18)

### 🎯 目標
- プロファイリング・モニタリング統合
- エラーハンドリング・デバッグ機能強化
- 包括的テスト・ドキュメント完成

### 📅 Week 14-15: プロファイリング統合

#### Week 14: CoreMLプロファイラ
**新規ファイル**: `src/profiler/coreml_profiler.rs`

```rust
pub struct CoreMLProfiler {
    start_times: HashMap<String, Instant>,
    inference_metrics: Vec<InferenceMetric>,
}

#[derive(Debug)]
pub struct InferenceMetric {
    operation: String,
    duration_ms: f64,
    memory_usage_bytes: u64,
    energy_consumption_mj: f64, // ミリジュール
}

impl CoreMLProfiler {
    pub fn profile_inference<F, R>(&mut self, operation: &str, f: F) -> Result<R>
    where F: FnOnce() -> Result<R>
    {
        let start = Instant::now();
        let memory_before = self.get_memory_usage();
        let energy_before = self.get_energy_consumption();

        let result = f()?;

        let duration = start.elapsed();
        let memory_after = self.get_memory_usage();
        let energy_after = self.get_energy_consumption();

        self.inference_metrics.push(InferenceMetric {
            operation: operation.to_string(),
            duration_ms: duration.as_millis() as f64,
            memory_usage_bytes: memory_after - memory_before,
            energy_consumption_mj: energy_after - energy_before,
        });

        Ok(result)
    }

    fn get_energy_consumption(&self) -> f64 {
        // macOS Energy Impact APIを使用
        // IOPMCopyPowerHistory等のシステムAPIを活用
    }
}
```

**実装タスク**:
- [ ] CoreML専用プロファイラ実装
- [ ] エネルギー消費測定
- [ ] 詳細性能メトリクス収集
- [ ] 既存プロファイラとの統合

#### Week 15: モニタリングダッシュボード
```rust
pub struct CoreMLDashboard {
    profiler: CoreMLProfiler,
    gpu_profiler: GpuProfiler,
}

impl CoreMLDashboard {
    pub fn generate_performance_report(&self) -> PerformanceReport {
        PerformanceReport {
            coreml_metrics: self.profiler.get_summary(),
            gpu_metrics: self.gpu_profiler.get_summary(),
            comparison: self.compare_performance(),
            recommendations: self.generate_recommendations(),
        }
    }

    pub fn real_time_monitoring(&self) -> impl Stream<Item = SystemMetrics> {
        // リアルタイム監視ストリーム
        interval(Duration::from_millis(100))
            .map(|_| self.collect_current_metrics())
    }
}
```

**実装タスク**:
- [ ] 統合監視ダッシュボード
- [ ] リアルタイム性能監視
- [ ] GPU vs CoreML 性能比較
- [ ] 自動最適化提案機能

### 📅 Week 16-17: 品質保証 & 最適化

#### Week 16: エラーハンドリング強化
**新規ファイル**: `src/gpu/coreml/error.rs`

```rust
#[derive(Debug, thiserror::Error)]
pub enum CoreMLError {
    #[error("CoreML device not available")]
    DeviceNotAvailable,

    #[error("Model compilation failed: {reason}")]
    ModelCompilationFailed { reason: String },

    #[error("Inference failed: {operation}")]
    InferenceFailed { operation: String },

    #[error("Memory allocation failed: {size} bytes")]
    MemoryAllocationFailed { size: usize },

    #[error("Tensor shape mismatch: expected {expected:?}, got {actual:?}")]
    TensorShapeMismatch { expected: Vec<usize>, actual: Vec<usize> },

    #[error("Unsupported operation: {operation}")]
    UnsupportedOperation { operation: String },
}

pub struct CoreMLErrorRecovery {
    fallback_strategy: FallbackStrategy,
    retry_count: usize,
}

impl CoreMLErrorRecovery {
    pub fn handle_error(&self, error: &CoreMLError) -> RecoveryAction {
        match error {
            CoreMLError::DeviceNotAvailable => {
                RecoveryAction::FallbackToCpu
            }
            CoreMLError::MemoryAllocationFailed { .. } => {
                RecoveryAction::ReduceBatchSize
            }
            CoreMLError::InferenceFailed { .. } => {
                if self.retry_count < 3 {
                    RecoveryAction::RetryWithBackoff
                } else {
                    RecoveryAction::FallbackToMetal
                }
            }
            _ => RecoveryAction::FallbackToMetal
        }
    }
}
```

**実装タスク**:
- [ ] 包括的エラーハンドリング
- [ ] 自動フォールバック機能
- [ ] エラー回復戦略
- [ ] デバッグ情報の充実

#### Week 17: 最終最適化
```rust
pub struct CoreMLOptimizer {
    cache: ModelCache,
    scheduler: InferenceScheduler,
}

impl CoreMLOptimizer {
    pub fn optimize_model(&self, model: &MLModel) -> Result<OptimizedMLModel> {
        // モデル最適化
        let optimized = model
            .quantize_weights()? // 重み量子化
            .prune_unnecessary_operations()? // 不要演算除去
            .fuse_operations()? // 演算融合
            .optimize_memory_layout()?; // メモリレイアウト最適化

        Ok(optimized)
    }

    pub fn cache_compiled_model(&mut self, model_hash: u64, compiled: CompiledModel) {
        self.cache.insert(model_hash, compiled);
    }
}
```

**実装タスク**:
- [ ] モデル最適化機能
- [ ] コンパイル済みモデルキャッシュ
- [ ] 推論スケジューリング最適化
- [ ] メモリ使用量最終調整

### 📅 Week 18: 最終統合 & リリース準備

**実装タスク**:
- [ ] 全機能統合テスト
- [ ] 性能ベンチマーク完成
- [ ] ドキュメント最終化
- [ ] リリースノート作成

### 🎯 Phase 3 マイルストーン & 成果物

#### 📋 成果物
- [ ] 完全統合CoreMLエンジン
- [ ] 包括的プロファイリングシステム
- [ ] 自動最適化機能
- [ ] 完全なテスト・ドキュメント

#### 📊 最終期待効果
- **総合性能**: +50-80% (Apple Silicon)
- **メモリ効率**: +40-60% (最適化済み)
- **バッテリー効率**: +50-70% (エネルギー最適化)
- **開発者体験**: 既存APIとの完全互換性

---

## 🧪 テスト戦略

### 🔍 各Phase共通テスト

#### Unit Tests (各週実装)
```rust
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_coreml_elementwise_add() {
        let executor = CoreMLExecutor::new(0).unwrap();
        let a = Tensor::from_slice(&[1.0, 2.0, 3.0]);
        let b = Tensor::from_slice(&[4.0, 5.0, 6.0]);

        let result = a.coreml_elementwise_add(&b).unwrap();
        let expected = Tensor::from_slice(&[5.0, 7.0, 9.0]);

        assert_tensor_eq!(result, expected, 1e-6);
    }

    #[test]
    fn test_coreml_vs_metal_accuracy() {
        // CoreML vs Metal の精度比較テスト
        let input = random_tensor([32, 64, 224, 224]);
        let kernel = random_tensor([64, 64, 3, 3]);

        let coreml_result = input.to_coreml().conv2d(&kernel).unwrap();
        let metal_result = input.to_metal().conv2d(&kernel).unwrap();

        assert_tensor_close!(coreml_result, metal_result, 1e-4);
    }
}
```

#### Integration Tests (Phase終了時)
```rust
#[test]
fn test_resnet50_inference() {
    let model = ResNet50::new().to_coreml();
    let input = random_tensor([1, 3, 224, 224]);

    let output = model.forward(input).unwrap();
    assert_eq!(output.shape(), [1, 1000]);

    // 推論時間テスト
    let start = Instant::now();
    let _ = model.forward(input).unwrap();
    let duration = start.elapsed();

    assert!(duration < Duration::from_millis(50)); // 50ms以内
}
```

#### Performance Tests (継続)
```rust
#[bench]
fn bench_coreml_conv2d(b: &mut Bencher) {
    let input = random_tensor([1, 64, 224, 224]).to_coreml();
    let kernel = random_tensor([64, 64, 3, 3]).to_coreml();

    b.iter(|| {
        black_box(input.conv2d(&kernel).unwrap())
    });
}
```

---

## 📊 継続的インテグレーション

### 🔄 CI/CD Pipeline拡張

#### GitHub Actions 追加設定
```yaml
name: CoreML Integration Tests

on: [push, pull_request]

jobs:
  test-coreml:
    runs-on: macos-latest
    steps:
    - uses: actions/checkout@v3

    - name: Setup Rust
      uses: actions-rs/toolchain@v1
      with:
        toolchain: stable

    - name: Test CoreML Features
      run: |
        cargo test --features coreml --target x86_64-apple-darwin
        cargo test --features coreml --target aarch64-apple-darwin

    - name: Benchmark CoreML vs GPU
      run: |
        cargo bench --features "coreml,cuda,metal" -- --output-format json

    - name: Upload Performance Results
      uses: actions/upload-artifact@v3
      with:
        name: coreml-benchmarks
        path: target/criterion/
```

---

## 🚨 リスク管理 & 緩和策

### 🔴 高リスク項目

| リスク | 確率 | 影響 | 緩和策 |
|-------|------|------|--------|
| **objc2-core-ml API変更** ||| バージョン固定、代替実装準備 |
| **CoreML性能期待値未達** ||| 段階的ベンチマーク、フォールバック |
| **メモリ統合複雑性** ||| Phase分割、独立テスト |
| **Apple Silicon互換性** ||| 複数デバイステスト |

### 🟡 中リスク項目

| リスク | 確率 | 影響 | 緩和策 |
|-------|------|------|--------|
| **テスト環境制約** ||| CI/CD環境拡充 |
| **第三者依存性** ||| 依存性最小化 |
| **実装複雑度増加** ||| コードレビュー強化 |

---

## 📈 成功指標 (KPI)

### 🎯 技術指標

| 指標 | Phase 1 | Phase 2 | Phase 3 | 測定方法 |
|------|---------|---------|---------|----------|
| **性能向上** | +25% | +50% | +60% | ベンチマーク比較 |
| **メモリ効率** | +15% | +35% | +50% | メモリプロファイリング |
| **バッテリー効率** | +20% | +40% | +60% | エネルギー測定 |
| **API互換性** | 100% | 100% | 100% | 回帰テスト |
| **テストカバレッジ** | >80% | >85% | >90% | カバレッジレポート |

### 📊 品質指標

| 指標 | 目標値 | 測定方法 |
|------|-------|----------|
| **コンパイル成功率** | >95% | CI/CD統計 |
| **テスト成功率** | >98% | テスト結果 |
| **メモリリーク** | 0件 | Valgrind, AddressSanitizer |
| **クリティカル脆弱性** | 0件 | セキュリティスキャン |

---

## 📚 ドキュメント計画

### 📖 技術ドキュメント

#### Phase 1
- [ ] CoreML基本使用ガイド
- [ ] API リファレンス (基本演算)
- [ ] 移行ガイド (GPU → CoreML)

#### Phase 2
- [ ] CNN実装ガイド
- [ ] パフォーマンス最適化ガイド
- [ ] トラブルシューティング

#### Phase 3
- [ ] 完全APIリファレンス
- [ ] ベストプラクティス集
- [ ] 事例集・チュートリアル

### 👥 ユーザードキュメント
- [ ] CoreML統合概要
- [ ] インストール・セットアップ
- [ ] サンプルコード集
- [ ] FAQ・よくある問題

---

## 🎯 結論

### ✅ 実装可能性: **非常に高い**

1. **技術的実現性**: 既存アーキテクチャとの高い親和性
2. **段階的実装**: リスク分散された実装計画
3. **明確なROI**: Apple Silicon環境での大幅性能向上

### 🚀 期待される成果

- **性能**: +50-80% (Apple Silicon最適化)
- **効率**: +40-60% (メモリ・エネルギー)
- **互換性**: 100% (既存API保持)
- **開発者体験**: 大幅向上 (macOS最適化)

### 📅 次のアクション

1. **Phase 1開始準備** (即時)
   - 開発環境セットアップ
   - 依存関係テスト
   - 初期プロトタイプ

2. **チーム体制構築** (Week 1)
   - CoreML専門知識習得
   - CI/CD環境拡張
   - テスト戦略実装

3. **コミュニティ連携** (継続)
   - 進捗透明性確保
   - フィードバック収集
   - ベータテスタープログラム

**総合評価**: 🟢 **実装強く推奨** - Apple生態系での競争優位性確保のための重要な投資

---

*このロードマップは、RusTorch CoreML統合の包括的な実装計画を提供します。各Phaseは独立して価値を提供し、段階的なリスク管理を可能にします。*