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
//! Device Initialization Cache
//! デバイス初期化キャッシュ
//!
//! This module provides caching for expensive device initialization operations
//! 高コストなデバイス初期化操作のキャッシュを提供

use crate::gpu::DeviceType;
use std::collections::HashMap;
use std::sync::{Arc, Mutex, OnceLock};
use std::time::{Duration, Instant};

/// Device initialization result
/// デバイス初期化結果
#[derive(Debug, Clone)]
pub enum DeviceStatus {
    Available,
    Unavailable(String), // Error message
    Initializing,
}

/// Cached device information
/// キャッシュされたデバイス情報
#[derive(Debug, Clone)]
pub struct CachedDevice {
    pub status: DeviceStatus,
    pub last_checked: Instant,
    pub initialization_time: Option<Duration>,
}

impl CachedDevice {
    pub fn new(status: DeviceStatus) -> Self {
        Self {
            status,
            last_checked: Instant::now(),
            initialization_time: None,
        }
    }

    pub fn with_init_time(mut self, duration: Duration) -> Self {
        self.initialization_time = Some(duration);
        self
    }

    /// Check if cache entry is still valid (within 30 seconds)
    /// キャッシュエントリが有効かチェック(30秒以内)
    pub fn is_valid(&self) -> bool {
        self.last_checked.elapsed() < Duration::from_secs(30)
    }
}

/// Device availability cache with lazy initialization
/// 遅延初期化付きデバイス可用性キャッシュ
#[derive(Debug, Clone)]
pub struct DeviceCache {
    cache: Arc<Mutex<HashMap<DeviceType, CachedDevice>>>,
}

impl DeviceCache {
    /// Get global device cache instance
    /// グローバルデバイスキャッシュインスタンスを取得
    pub fn global() -> &'static DeviceCache {
        static CACHE: OnceLock<DeviceCache> = OnceLock::new();
        CACHE.get_or_init(|| DeviceCache::new())
    }

    /// Create new device cache
    /// 新しいデバイスキャッシュを作成
    pub fn new() -> Self {
        Self {
            cache: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Check if device is available (with caching)
    /// デバイスが利用可能かチェック(キャッシュ付き)
    pub fn is_device_available(&self, device: &DeviceType) -> bool {
        // Check cache first
        if let Some(cached) = self.get_cached_status(device) {
            if cached.is_valid() {
                return matches!(cached.status, DeviceStatus::Available);
            }
        }

        // Cache miss or expired - check device availability
        let start = Instant::now();
        let is_available = self.check_device_availability_impl(device);
        let check_duration = start.elapsed();

        // Update cache
        let status = if is_available {
            DeviceStatus::Available
        } else {
            DeviceStatus::Unavailable("Device check failed".to_string())
        };

        let cached_device = CachedDevice::new(status).with_init_time(check_duration);
        self.update_cache(device.clone(), cached_device);

        is_available
    }

    /// Get cached device status
    /// キャッシュされたデバイス状態を取得
    pub fn get_cached_status(&self, device: &DeviceType) -> Option<CachedDevice> {
        let cache = self.cache.lock().ok()?;
        cache.get(device).cloned()
    }

    /// Update device cache
    /// デバイスキャッシュを更新
    pub fn update_cache(&self, device: DeviceType, cached_device: CachedDevice) {
        if let Ok(mut cache) = self.cache.lock() {
            cache.insert(device, cached_device);
        }
    }

    /// Clear expired cache entries
    /// 期限切れキャッシュエントリをクリア
    pub fn cleanup_expired(&self) {
        if let Ok(mut cache) = self.cache.lock() {
            cache.retain(|_, cached| cached.is_valid());
        }
    }

    /// Get cache statistics
    /// キャッシュ統計を取得
    pub fn get_stats(&self) -> CacheStats {
        let cache = self.cache.lock().unwrap();
        let total_entries = cache.len();
        let valid_entries = cache.values().filter(|c| c.is_valid()).count();
        let available_devices = cache
            .values()
            .filter(|c| c.is_valid() && matches!(c.status, DeviceStatus::Available))
            .count();

        CacheStats {
            total_entries,
            valid_entries,
            available_devices,
            cache_hit_rate: if total_entries > 0 {
                valid_entries as f64 / total_entries as f64
            } else {
                0.0
            },
        }
    }

    /// Actual device availability check implementation
    /// 実際のデバイス可用性チェック実装
    fn check_device_availability_impl(&self, device: &DeviceType) -> bool {
        match device {
            DeviceType::Cpu => true,  // CPU always available
            DeviceType::Auto => true, // Auto always available (fallback logic)

            #[cfg(any(
                feature = "coreml",
                feature = "coreml-hybrid",
                feature = "coreml-fallback"
            ))]
            DeviceType::CoreML(_) => {
                use crate::backends::DeviceManager;
                DeviceManager::is_coreml_available()
            }

            #[cfg(feature = "metal")]
            DeviceType::Metal(_) => {
                use crate::backends::DeviceManager;
                DeviceManager::is_metal_available()
            }

            #[cfg(not(feature = "metal"))]
            DeviceType::Metal(_) => false,

            #[cfg(feature = "cuda")]
            DeviceType::Cuda(_) => {
                // TODO: Implement CUDA availability check
                false
            }

            #[cfg(not(feature = "cuda"))]
            DeviceType::Cuda(_) => false,

            #[cfg(feature = "opencl")]
            DeviceType::OpenCL(_) => {
                // TODO: Implement OpenCL availability check
                false
            }

            #[cfg(not(feature = "opencl"))]
            DeviceType::OpenCL(_) => false,

            #[cfg(feature = "coreml-hybrid")]
            DeviceType::CoreMLHybrid { .. } => {
                // Check if CoreML or fallback GPU is available
                cfg!(target_os = "macos")
            }

            #[cfg(feature = "mac-hybrid")]
            DeviceType::MacHybrid => {
                // MacHybrid is available if either Metal or CoreML is available
                cfg!(target_os = "macos")
            }
        }
    }

    /// Warmup device cache by checking all common devices
    /// 一般的なデバイスをすべてチェックしてキャッシュをウォームアップ
    pub fn warmup(&self) {
        #[allow(unused_mut)]
        let mut devices_to_check = vec![DeviceType::Cpu];

        #[cfg(any(
            feature = "coreml",
            feature = "coreml-hybrid",
            feature = "coreml-fallback"
        ))]
        devices_to_check.push(DeviceType::CoreML(0));

        #[cfg(feature = "metal")]
        devices_to_check.push(DeviceType::Metal(0));

        #[cfg(feature = "cuda")]
        devices_to_check.push(DeviceType::Cuda(0));

        for device in devices_to_check {
            self.is_device_available(&device);
        }
    }
}

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

/// Cache performance statistics
/// キャッシュパフォーマンス統計
#[derive(Debug, Clone)]
pub struct CacheStats {
    pub total_entries: usize,
    pub valid_entries: usize,
    pub available_devices: usize,
    pub cache_hit_rate: f64,
}

/// CoreML-specific initialization cache
/// CoreML固有の初期化キャッシュ
#[derive(Debug)]
pub struct CoreMLCache {
    is_initialized: Arc<Mutex<bool>>,
    initialization_result: Arc<Mutex<Option<Result<(), String>>>>,
}

impl CoreMLCache {
    /// Get global CoreML cache instance
    /// グローバルCoreMLキャッシュインスタンスを取得
    pub fn global() -> &'static CoreMLCache {
        static COREML_CACHE: OnceLock<CoreMLCache> = OnceLock::new();
        COREML_CACHE.get_or_init(|| CoreMLCache::new())
    }

    pub fn new() -> Self {
        Self {
            is_initialized: Arc::new(Mutex::new(false)),
            initialization_result: Arc::new(Mutex::new(None)),
        }
    }

    /// Initialize CoreML with caching
    /// キャッシュ付きCoreML初期化
    pub fn ensure_initialized(&self) -> Result<(), String> {
        // Check if already initialized
        if let Ok(initialized) = self.is_initialized.lock() {
            if *initialized {
                // Return cached result
                if let Ok(result) = self.initialization_result.lock() {
                    if let Some(ref cached_result) = *result {
                        return cached_result.clone();
                    }
                }
            }
        }

        // Perform initialization
        let result = self.initialize_coreml();

        // Cache the result
        if let (Ok(mut initialized), Ok(mut cached_result)) = (
            self.is_initialized.lock(),
            self.initialization_result.lock(),
        ) {
            *initialized = true;
            *cached_result = Some(result.clone());
        }

        result
    }

    /// Actual CoreML initialization implementation
    /// 実際のCoreML初期化実装
    fn initialize_coreml(&self) -> Result<(), String> {
        #[cfg(any(
            feature = "coreml",
            feature = "coreml-hybrid",
            feature = "coreml-fallback"
        ))]
        {
            // Check if CoreML is available on this platform
            if !cfg!(target_os = "macos") {
                return Err("CoreML is only available on macOS".to_string());
            }

            // Perform any necessary CoreML setup here
            // For now, just check availability
            use crate::backends::DeviceManager;
            if DeviceManager::is_coreml_available() {
                Ok(())
            } else {
                Err("CoreML not available on this system".to_string())
            }
        }

        #[cfg(not(any(
            feature = "coreml",
            feature = "coreml-hybrid",
            feature = "coreml-fallback"
        )))]
        {
            Err("CoreML features not enabled".to_string())
        }
    }

    /// Reset initialization state (for testing)
    /// 初期化状態をリセット(テスト用)
    pub fn reset(&self) {
        if let (Ok(mut initialized), Ok(mut result)) = (
            self.is_initialized.lock(),
            self.initialization_result.lock(),
        ) {
            *initialized = false;
            *result = None;
        }
    }
}

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

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

    #[test]
    fn test_device_cache_basic() {
        let cache = DeviceCache::new();

        // CPU should always be available
        assert!(cache.is_device_available(&DeviceType::Cpu));

        // Should be cached now
        let cached = cache.get_cached_status(&DeviceType::Cpu);
        assert!(cached.is_some());
        assert!(matches!(cached.unwrap().status, DeviceStatus::Available));
    }

    #[test]
    fn test_cache_expiration() {
        let cache = DeviceCache::new();

        // Manually insert an expired entry
        let expired_device = CachedDevice {
            status: DeviceStatus::Available,
            last_checked: Instant::now() - Duration::from_secs(60), // 1 minute ago
            initialization_time: None,
        };

        cache.update_cache(DeviceType::Cpu, expired_device);

        // Should re-check because cache is expired
        assert!(cache.is_device_available(&DeviceType::Cpu));
    }

    #[test]
    fn test_coreml_cache() {
        let cache = CoreMLCache::new();

        // First call should initialize
        let result1 = cache.ensure_initialized();

        // Second call should use cached result
        let result2 = cache.ensure_initialized();

        // Results should be consistent
        match (result1, result2) {
            (Ok(()), Ok(())) => {}
            (Err(e1), Err(e2)) => assert_eq!(e1, e2),
            _ => panic!("Inconsistent cache results"),
        }
    }

    #[test]
    #[cfg(feature = "coreml")]
    fn test_unsupported_operation_bypass() {
        use crate::gpu::smart_device_selector::{
            OperationProfile, OperationType, SmartDeviceSelector,
        };
        use crate::gpu::DeviceType;

        let available_devices = vec![DeviceType::CoreML(0), DeviceType::Metal(0), DeviceType::Cpu];
        let selector = SmartDeviceSelector::new(available_devices);

        // Test CoreML-supported operation
        let supported_profile = OperationProfile::new(
            OperationType::MatrixMultiplication,
            &[128, 128],
            4, // f32
        );
        let selected = selector.select_device(&supported_profile);
        // Should allow CoreML for supported operations
        assert!(
            matches!(selected, DeviceType::CoreML(_)) || matches!(selected, DeviceType::Metal(_))
        );

        // Test CoreML-unsupported operations
        let unsupported_ops = vec![
            OperationType::ComplexNumber,
            OperationType::StatisticalDistribution,
            OperationType::CustomKernel,
            OperationType::DistributedOp,
        ];

        for op_type in unsupported_ops {
            let unsupported_profile = OperationProfile::new(
                op_type,
                &[128, 128],
                8, // Complex64
            );
            let selected = selector.select_device(&unsupported_profile);
            // Should bypass CoreML and select GPU or CPU directly
            assert!(!matches!(selected, DeviceType::CoreML(_)));
            assert!(
                matches!(selected, DeviceType::Metal(_)) || matches!(selected, DeviceType::Cpu)
            );
        }
    }
}