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
//! CoreML integration and hybrid execution patterns
//! CoreML統合とハイブリッド実行パターン

use super::backend::CoreMLBackend;
use super::common::*;
use super::device::CoreMLDeviceManager;
use super::operations::*;
use crate::error::RusTorchError;
use crate::tensor::Tensor;
use ndarray::ScalarOperand;
use num_traits::{Float, FromPrimitive};
use std::sync::Arc;

/// Hybrid execution strategy for CoreML operations
/// CoreML演算用ハイブリッド実行戦略
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HybridStrategy {
    /// CoreML first, CPU fallback on failure
    /// CoreML優先、失敗時CPU フォールバック
    CoreMLPreferred,

    /// CPU first, CoreML on large operations
    /// CPU優先、大規模演算時CoreML
    CPUPreferred,

    /// Automatic selection based on operation characteristics
    /// 演算特性に基づく自動選択
    Automatic,

    /// Force CoreML execution (may fail)
    /// CoreML実行を強制(失敗する可能性あり)
    ForceCoreMI,

    /// Force CPU execution
    /// CPU実行を強制
    ForceCPU,
}

impl Default for HybridStrategy {
    fn default() -> Self {
        Self::Automatic
    }
}

/// CoreML hybrid executor for intelligent operation routing
/// インテリジェント演算ルーティング用CoreMLハイブリッド実行器
pub struct CoreMLHybridExecutor {
    backend: Arc<CoreMLBackend>,
    strategy: HybridStrategy,
    performance_stats: Arc<std::sync::Mutex<PerformanceStats>>,
}

/// Performance statistics for execution decisions
/// 実行決定用パフォーマンス統計
#[derive(Debug, Default)]
struct PerformanceStats {
    coreml_success_rate: f64,
    coreml_avg_time: std::time::Duration,
    cpu_avg_time: std::time::Duration,
    total_operations: usize,
}

impl CoreMLHybridExecutor {
    /// Create new hybrid executor
    /// 新しいハイブリッド実行器を作成
    pub fn new(strategy: HybridStrategy) -> CoreMLResult<Self> {
        let backend = Arc::new((*CoreMLBackend::global()).clone());

        Ok(Self {
            backend,
            strategy,
            performance_stats: Arc::new(std::sync::Mutex::new(PerformanceStats::default())),
        })
    }

    /// Execute operation with hybrid strategy
    /// ハイブリッド戦略で演算を実行
    pub fn execute<T, Op, CpuFn>(
        &self,
        operation: &Op,
        cpu_fallback: CpuFn,
    ) -> Result<Tensor<T>, RusTorchError>
    where
        T: Float + FromPrimitive + ScalarOperand + 'static,
        Op: CoreMLOperation<T>,
        CpuFn: FnOnce() -> Result<Tensor<T>, RusTorchError>,
    {
        match self.decide_execution_path(operation) {
            ExecutionPath::CoreML => self.execute_coreml_with_fallback(operation, cpu_fallback),
            ExecutionPath::CPU => self.execute_cpu_with_stats(cpu_fallback),
            ExecutionPath::TryBoth => {
                // Try CoreML first, compare with CPU if both succeed
                let coreml_result = self.try_coreml_execution(operation);
                match coreml_result {
                    Ok(result) => Ok(result),
                    Err(_) => self.execute_cpu_with_stats(cpu_fallback),
                }
            }
        }
    }

    /// Decide execution path based on strategy and operation characteristics
    /// 戦略と演算特性に基づいて実行パスを決定
    fn decide_execution_path<T, Op>(&self, operation: &Op) -> ExecutionPath
    where
        T: Float + FromPrimitive + ScalarOperand + 'static,
        Op: CoreMLOperation<T>,
    {
        match self.strategy {
            HybridStrategy::ForceCoreMI => ExecutionPath::CoreML,
            HybridStrategy::ForceCPU => ExecutionPath::CPU,
            HybridStrategy::CoreMLPreferred => {
                if operation.is_supported_by_coreml() {
                    ExecutionPath::CoreML
                } else {
                    ExecutionPath::CPU
                }
            }
            HybridStrategy::CPUPreferred => {
                if self.should_use_coreml_for_large_ops(operation) {
                    ExecutionPath::CoreML
                } else {
                    ExecutionPath::CPU
                }
            }
            HybridStrategy::Automatic => self.automatic_decision(operation),
        }
    }

    /// Make automatic execution decision based on performance data
    /// パフォーマンスデータに基づいて自動実行決定を行う
    fn automatic_decision<T, Op>(&self, operation: &Op) -> ExecutionPath
    where
        T: Float + FromPrimitive + ScalarOperand + 'static,
        Op: CoreMLOperation<T>,
    {
        if !operation.is_supported_by_coreml() {
            return ExecutionPath::CPU;
        }

        if let Ok(stats) = self.performance_stats.lock() {
            if stats.total_operations < 10 {
                // Not enough data, use conservative approach
                return ExecutionPath::CoreML;
            }

            // Use CoreML if success rate is good and it's faster
            if stats.coreml_success_rate > 0.8 && stats.coreml_avg_time < stats.cpu_avg_time * 2 {
                ExecutionPath::CoreML
            } else {
                ExecutionPath::CPU
            }
        } else {
            ExecutionPath::CoreML
        }
    }

    /// Check if operation should use CoreML based on size
    /// サイズに基づいて演算がCoreMLを使用すべきかチェック
    fn should_use_coreml_for_large_ops<T, Op>(&self, operation: &Op) -> bool
    where
        T: Float + FromPrimitive + ScalarOperand + 'static,
        Op: CoreMLOperation<T>,
    {
        if !operation.is_supported_by_coreml() {
            return false;
        }

        // Use estimated execution time as a proxy for operation size
        if let Some(estimated_time) = operation.estimated_execution_time() {
            estimated_time.as_millis() > 10 // > 10ms operations
        } else {
            false
        }
    }

    /// Execute CoreML with CPU fallback
    /// CPUフォールバック付きでCoreMLを実行
    fn execute_coreml_with_fallback<T, Op, CpuFn>(
        &self,
        operation: &Op,
        cpu_fallback: CpuFn,
    ) -> Result<Tensor<T>, RusTorchError>
    where
        T: Float + FromPrimitive + ScalarOperand + 'static,
        Op: CoreMLOperation<T>,
        CpuFn: FnOnce() -> Result<Tensor<T>, RusTorchError>,
    {
        let start_time = std::time::Instant::now();

        match self.try_coreml_execution(operation) {
            Ok(result) => {
                self.update_stats(true, start_time.elapsed(), None);
                Ok(result)
            }
            Err(e) => {
                // CoreML failed, try CPU fallback
                let cpu_start = std::time::Instant::now();
                match cpu_fallback() {
                    Ok(result) => {
                        self.update_stats(false, start_time.elapsed(), Some(cpu_start.elapsed()));
                        Ok(result)
                    }
                    Err(cpu_err) => {
                        // Both failed, return the original CoreML error
                        Err(e)
                    }
                }
            }
        }
    }

    /// Execute CPU with performance tracking
    /// パフォーマンストラッキング付きでCPUを実行
    fn execute_cpu_with_stats<T, CpuFn>(
        &self,
        cpu_fallback: CpuFn,
    ) -> Result<Tensor<T>, RusTorchError>
    where
        T: num_traits::Float + 'static,
        CpuFn: FnOnce() -> Result<Tensor<T>, RusTorchError>,
    {
        let start_time = std::time::Instant::now();
        let result = cpu_fallback();

        if result.is_ok() {
            self.update_stats(false, std::time::Duration::ZERO, Some(start_time.elapsed()));
        }

        result
    }

    /// Try CoreML execution
    /// CoreML実行を試行
    fn try_coreml_execution<T, Op>(&self, operation: &Op) -> CoreMLResult<Tensor<T>>
    where
        T: Float + FromPrimitive + ScalarOperand + 'static,
        Op: CoreMLOperation<T>,
    {
        operation.execute_coreml(0) // Use device 0 for now
    }

    /// Update performance statistics
    /// パフォーマンス統計を更新
    fn update_stats(
        &self,
        coreml_success: bool,
        coreml_time: std::time::Duration,
        cpu_time: Option<std::time::Duration>,
    ) {
        if let Ok(mut stats) = self.performance_stats.lock() {
            stats.total_operations += 1;

            // Update CoreML success rate
            let success_count =
                (stats.coreml_success_rate * (stats.total_operations - 1) as f64) as usize;
            let new_success_count = if coreml_success {
                success_count + 1
            } else {
                success_count
            };
            stats.coreml_success_rate = new_success_count as f64 / stats.total_operations as f64;

            // Update timing averages
            if coreml_success && coreml_time > std::time::Duration::ZERO {
                stats.coreml_avg_time = self.update_running_average(
                    stats.coreml_avg_time,
                    coreml_time,
                    stats.total_operations,
                );
            }

            if let Some(cpu_time) = cpu_time {
                stats.cpu_avg_time = self.update_running_average(
                    stats.cpu_avg_time,
                    cpu_time,
                    stats.total_operations,
                );
            }
        }
    }

    /// Update running average
    /// 移動平均を更新
    fn update_running_average(
        &self,
        current_avg: std::time::Duration,
        new_value: std::time::Duration,
        count: usize,
    ) -> std::time::Duration {
        let current_total = current_avg.as_nanos() as f64 * (count - 1) as f64;
        let new_total = current_total + new_value.as_nanos() as f64;
        std::time::Duration::from_nanos((new_total / count as f64) as u64)
    }

    /// Get current performance statistics
    /// 現在のパフォーマンス統計を取得
    pub fn get_performance_stats(
        &self,
    ) -> Option<(f64, std::time::Duration, std::time::Duration, usize)> {
        if let Ok(stats) = self.performance_stats.lock() {
            Some((
                stats.coreml_success_rate,
                stats.coreml_avg_time,
                stats.cpu_avg_time,
                stats.total_operations,
            ))
        } else {
            None
        }
    }

    /// Reset performance statistics
    /// パフォーマンス統計をリセット
    pub fn reset_stats(&self) {
        if let Ok(mut stats) = self.performance_stats.lock() {
            *stats = PerformanceStats::default();
        }
    }
}

/// Execution path decision
/// 実行パス決定
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ExecutionPath {
    CoreML,
    CPU,
    TryBoth,
}

/// Convenience macro for hybrid CoreML execution
/// ハイブリッドCoreML実行用便利マクロ
#[macro_export]
macro_rules! coreml_hybrid {
    ($operation:expr, $cpu_fallback:expr) => {{
        let executor = CoreMLHybridExecutor::new(HybridStrategy::Automatic)?;
        executor.execute(&$operation, || $cpu_fallback)
    }};
    ($operation:expr, $cpu_fallback:expr, $strategy:expr) => {{
        let executor = CoreMLHybridExecutor::new($strategy)?;
        executor.execute(&$operation, || $cpu_fallback)
    }};
}

/// Global hybrid executor instance
/// グローバルハイブリッド実行器インスタンス
static GLOBAL_HYBRID_EXECUTOR: std::sync::OnceLock<CoreMLHybridExecutor> =
    std::sync::OnceLock::new();

/// Get global hybrid executor
/// グローバルハイブリッド実行器を取得
pub fn global_hybrid_executor() -> &'static CoreMLHybridExecutor {
    GLOBAL_HYBRID_EXECUTOR.get_or_init(|| {
        CoreMLHybridExecutor::new(HybridStrategy::Automatic).unwrap_or_else(|_| {
            // Fallback to CPU-preferred strategy if initialization fails
            CoreMLHybridExecutor::new(HybridStrategy::CPUPreferred)
                .expect("Failed to create fallback hybrid executor")
        })
    })
}

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

    #[test]
    fn test_hybrid_executor_creation() {
        let executor = CoreMLHybridExecutor::new(HybridStrategy::Automatic);

        match executor {
            Ok(_) => {
                println!("Hybrid executor created successfully");
            }
            Err(e) => {
                println!("Expected failure on platforms without CoreML: {}", e);
            }
        }
    }

    #[test]
    fn test_global_hybrid_executor() {
        let executor = global_hybrid_executor();

        // Should always succeed (uses fallback strategy if needed)
        let stats = executor.get_performance_stats();
        assert!(stats.is_some());

        let (success_rate, _, _, operations) = stats.unwrap();
        assert_eq!(operations, 0); // No operations yet
        assert_eq!(success_rate, 0.0); // No operations yet
    }

    #[test]
    fn test_strategy_decisions() {
        use super::super::operations::linear_algebra::MatMulOperation;
        use crate::tensor::Tensor;

        // Create a small matrix operation (not suitable for CoreML)
        let a = Tensor::<f32>::zeros(&[2, 2]);
        let b = Tensor::<f32>::zeros(&[2, 2]);
        let operation = MatMulOperation::new(a, b);

        if let Ok(executor) = CoreMLHybridExecutor::new(HybridStrategy::CPUPreferred) {
            let path = executor.decide_execution_path(&operation);
            assert_eq!(path, ExecutionPath::CPU); // Should prefer CPU for small ops
        }
    }
}