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
//! Core Profiling Engine
//! コアプロファイリングエンジン

use crate::error::{RusTorchError, RusTorchResult};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

/// Profiling level configuration
/// プロファイリングレベル設定
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProfilingLevel {
    /// Disabled profiling
    /// プロファイリング無効
    Disabled,
    /// Basic timing only
    /// 基本的なタイミングのみ
    Basic,
    /// Standard profiling with memory tracking
    /// メモリトラッキング付き標準プロファイリング
    Standard,
    /// Comprehensive profiling with GPU and system metrics
    /// GPU・システムメトリクス付き包括的プロファイリング
    Comprehensive,
    /// Verbose profiling with detailed analysis
    /// 詳細分析付き詳細プロファイリング
    Verbose,
}

impl Default for ProfilingLevel {
    fn default() -> Self {
        ProfilingLevel::Standard
    }
}

/// Profiler configuration
/// プロファイラー設定
#[derive(Debug, Clone)]
pub struct ProfilerConfig {
    /// Profiling level
    /// プロファイリングレベル
    pub level: ProfilingLevel,
    /// Enable memory profiling
    /// メモリプロファイリングを有効化
    pub enable_memory_profiling: bool,
    /// Enable GPU profiling
    /// GPUプロファイリングを有効化
    pub enable_gpu_profiling: bool,
    /// Enable system metrics
    /// システムメトリクスを有効化
    pub enable_system_metrics: bool,
    /// Enable call stack tracking
    /// コールスタックトラッキングを有効化
    pub enable_call_stack: bool,
    /// Maximum profiling session duration (seconds)
    /// 最大プロファイリングセッション時間(秒)
    pub max_session_duration: Option<u64>,
    /// Buffer size for metrics collection
    /// メトリクス収集用バッファサイズ
    pub metrics_buffer_size: usize,
    /// Sampling rate for continuous monitoring (Hz)
    /// 連続監視のサンプリングレート(Hz)
    pub sampling_rate: f64,
    /// Export format options
    /// エクスポート形式オプション
    pub export_chrome_trace: bool,
    /// Export to TensorBoard format
    /// TensorBoard形式にエクスポート
    pub export_tensorboard: bool,
    /// Export to JSON format
    /// JSON形式にエクスポート
    pub export_json: bool,
}

impl Default for ProfilerConfig {
    fn default() -> Self {
        Self {
            level: ProfilingLevel::Standard,
            enable_memory_profiling: true,
            enable_gpu_profiling: true,
            enable_system_metrics: true,
            enable_call_stack: true,
            max_session_duration: Some(3600), // 1 hour
            metrics_buffer_size: 10000,
            sampling_rate: 10.0, // 10 Hz
            export_chrome_trace: true,
            export_tensorboard: false,
            export_json: true,
        }
    }
}

/// Profiling session state
/// プロファイリングセッション状態
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SessionState {
    /// Not started
    /// 未開始
    NotStarted,
    /// Running
    /// 実行中
    Running,
    /// Paused
    /// 一時停止
    Paused,
    /// Completed
    /// 完了
    Completed,
    /// Error occurred
    /// エラー発生
    Error,
}

/// Profiling session
/// プロファイリングセッション
#[derive(Debug, Clone)]
pub struct ProfilingSession {
    /// Session ID
    /// セッションID
    pub session_id: String,
    /// Session name
    /// セッション名
    pub session_name: String,
    /// Session state
    /// セッション状態
    pub state: SessionState,
    /// Start time
    /// 開始時間
    pub start_time: Instant,
    /// End time
    /// 終了時間
    pub end_time: Option<Instant>,
    /// Configuration
    /// 設定
    pub config: ProfilerConfig,
    /// Operations recorded
    /// 記録された操作
    pub operations: HashMap<String, OperationMetrics>,
    /// Call stack depth
    /// コールスタック深度
    pub max_call_depth: usize,
    /// Total operations
    /// 総操作数
    pub total_operations: usize,
    /// Error message if any
    /// エラーメッセージ(ある場合)
    pub error_message: Option<String>,
}

impl ProfilingSession {
    /// Create new profiling session
    /// 新しいプロファイリングセッションを作成
    pub fn new(name: String, config: ProfilerConfig) -> Self {
        let session_id = generate_session_id();
        Self {
            session_id,
            session_name: name,
            state: SessionState::NotStarted,
            start_time: Instant::now(),
            end_time: None,
            config,
            operations: HashMap::new(),
            max_call_depth: 0,
            total_operations: 0,
            error_message: None,
        }
    }

    /// Start the session
    /// セッションを開始
    pub fn start(&mut self) -> RusTorchResult<()> {
        if self.state != SessionState::NotStarted {
            return Err(RusTorchError::Profiling {
                message: "Session already started".to_string(),
            });
        }

        self.state = SessionState::Running;
        self.start_time = Instant::now();
        Ok(())
    }

    /// Stop the session
    /// セッションを停止
    pub fn stop(&mut self) -> RusTorchResult<SessionSnapshot> {
        if self.state != SessionState::Running {
            return Err(RusTorchError::Profiling {
                message: "Session not running".to_string(),
            });
        }

        self.state = SessionState::Completed;
        self.end_time = Some(Instant::now());

        Ok(self.create_snapshot())
    }

    /// Pause the session
    /// セッションを一時停止
    pub fn pause(&mut self) -> RusTorchResult<()> {
        if self.state != SessionState::Running {
            return Err(RusTorchError::Profiling {
                message: "Session not running".to_string(),
            });
        }

        self.state = SessionState::Paused;
        Ok(())
    }

    /// Resume the session
    /// セッションを再開
    pub fn resume(&mut self) -> RusTorchResult<()> {
        if self.state != SessionState::Paused {
            return Err(RusTorchError::Profiling {
                message: "Session not paused".to_string(),
            });
        }

        self.state = SessionState::Running;
        Ok(())
    }

    /// Record operation
    /// 操作を記録
    pub fn record_operation(&mut self, name: &str, duration: Duration, call_depth: usize) {
        if self.state != SessionState::Running {
            return;
        }

        let metrics = self
            .operations
            .entry(name.to_string())
            .or_insert_with(|| OperationMetrics::new(name.to_string()));

        metrics.record_timing(duration);
        self.max_call_depth = self.max_call_depth.max(call_depth);
        self.total_operations += 1;
    }

    /// Get session duration
    /// セッション期間を取得
    pub fn duration(&self) -> Duration {
        match self.end_time {
            Some(end) => end.duration_since(self.start_time),
            None => self.start_time.elapsed(),
        }
    }

    /// Create session snapshot
    /// セッションスナップショットを作成
    pub fn create_snapshot(&self) -> SessionSnapshot {
        let operations: Vec<_> = self.operations.values().cloned().collect();

        SessionSnapshot {
            session_id: self.session_id.clone(),
            session_name: self.session_name.clone(),
            start_time: self.start_time,
            duration: self.duration(),
            operations,
            total_operations: self.total_operations,
            max_call_depth: self.max_call_depth,
            config: self.config.clone(),
        }
    }
}

/// Operation performance metrics
/// 操作パフォーマンスメトリクス
#[derive(Debug, Clone)]
pub struct OperationMetrics {
    /// Operation name
    /// 操作名
    pub name: String,
    /// Call count
    /// 呼び出し回数
    pub call_count: usize,
    /// Total time
    /// 総時間
    pub total_time: Duration,
    /// Average time
    /// 平均時間
    pub avg_time: Duration,
    /// Minimum time
    /// 最小時間
    pub min_time: Duration,
    /// Maximum time
    /// 最大時間
    pub max_time: Duration,
    /// Standard deviation
    /// 標準偏差
    pub std_dev: f64,
    /// Timing samples
    /// タイミングサンプル
    pub timing_samples: Vec<Duration>,
    /// CPU percentage
    /// CPU使用率
    pub cpu_percentage: Option<f64>,
    /// Memory usage (bytes)
    /// メモリ使用量(バイト)
    pub memory_usage: Option<u64>,
    /// GPU time (if applicable)
    /// GPU時間(該当する場合)
    pub gpu_time: Option<Duration>,
}

impl OperationMetrics {
    /// Create new operation metrics
    /// 新しい操作メトリクスを作成
    pub fn new(name: String) -> Self {
        Self {
            name,
            call_count: 0,
            total_time: Duration::ZERO,
            avg_time: Duration::ZERO,
            min_time: Duration::MAX,
            max_time: Duration::ZERO,
            std_dev: 0.0,
            timing_samples: Vec::new(),
            cpu_percentage: None,
            memory_usage: None,
            gpu_time: None,
        }
    }

    /// Record timing measurement
    /// タイミング測定を記録
    pub fn record_timing(&mut self, duration: Duration) {
        self.call_count += 1;
        self.total_time += duration;
        self.min_time = self.min_time.min(duration);
        self.max_time = self.max_time.max(duration);

        // Update average
        self.avg_time = self.total_time / self.call_count as u32;

        // Store sample for statistics
        self.timing_samples.push(duration);
        if self.timing_samples.len() > 1000 {
            // Keep only recent samples
            self.timing_samples.drain(0..500);
        }

        // Update standard deviation
        self.update_std_dev();
    }

    /// Update standard deviation
    /// 標準偏差を更新
    fn update_std_dev(&mut self) {
        if self.timing_samples.len() < 2 {
            return;
        }

        let avg_secs = self.avg_time.as_secs_f64();
        let variance: f64 = self
            .timing_samples
            .iter()
            .map(|&d| {
                let diff = d.as_secs_f64() - avg_secs;
                diff * diff
            })
            .sum::<f64>()
            / (self.timing_samples.len() - 1) as f64;

        self.std_dev = variance.sqrt();
    }

    /// Get performance statistics
    /// パフォーマンス統計を取得
    pub fn get_statistics(&self) -> PerformanceStatistics {
        PerformanceStatistics {
            operation_name: self.name.clone(),
            call_count: self.call_count,
            total_time_ms: self.total_time.as_secs_f64() * 1000.0,
            avg_time_ms: self.avg_time.as_secs_f64() * 1000.0,
            min_time_ms: self.min_time.as_secs_f64() * 1000.0,
            max_time_ms: self.max_time.as_secs_f64() * 1000.0,
            std_dev_ms: self.std_dev * 1000.0,
            throughput_ops_per_sec: if self.avg_time.as_secs_f64() > 0.0 {
                1.0 / self.avg_time.as_secs_f64()
            } else {
                0.0
            },
            cpu_percentage: self.cpu_percentage.unwrap_or(0.0),
            memory_usage_mb: self.memory_usage.unwrap_or(0) as f64 / (1024.0 * 1024.0),
            gpu_time_ms: self.gpu_time.map(|d| d.as_secs_f64() * 1000.0),
        }
    }
}

/// Session snapshot for reporting
/// レポート用セッションスナップショット
#[derive(Debug, Clone)]
pub struct SessionSnapshot {
    /// Session ID
    /// セッションID
    pub session_id: String,
    /// Session name
    /// セッション名
    pub session_name: String,
    /// Start time
    /// 開始時間
    pub start_time: Instant,
    /// Total duration
    /// 総継続時間
    pub duration: Duration,
    /// Operation metrics
    /// 操作メトリクス
    pub operations: Vec<OperationMetrics>,
    /// Total operations count
    /// 総操作数
    pub total_operations: usize,
    /// Maximum call depth observed
    /// 観測された最大コール深度
    pub max_call_depth: usize,
    /// Configuration used
    /// 使用された設定
    pub config: ProfilerConfig,
}

/// Performance statistics summary
/// パフォーマンス統計サマリー
#[derive(Debug, Clone)]
pub struct PerformanceStatistics {
    /// Operation name
    /// 操作名
    pub operation_name: String,
    /// Number of calls
    /// 呼び出し回数
    pub call_count: usize,
    /// Total time (ms)
    /// 総時間(ミリ秒)
    pub total_time_ms: f64,
    /// Average time per call (ms)
    /// 呼び出しごとの平均時間(ミリ秒)
    pub avg_time_ms: f64,
    /// Minimum time (ms)
    /// 最小時間(ミリ秒)
    pub min_time_ms: f64,
    /// Maximum time (ms)
    /// 最大時間(ミリ秒)
    pub max_time_ms: f64,
    /// Standard deviation (ms)
    /// 標準偏差(ミリ秒)
    pub std_dev_ms: f64,
    /// Throughput (operations per second)
    /// スループット(秒間操作数)
    pub throughput_ops_per_sec: f64,
    /// CPU usage percentage
    /// CPU使用率
    pub cpu_percentage: f64,
    /// Memory usage (MB)
    /// メモリ使用量(MB)
    pub memory_usage_mb: f64,
    /// GPU time if applicable (ms)
    /// GPU時間(該当する場合、ミリ秒)
    pub gpu_time_ms: Option<f64>,
}

/// Core profiler engine
/// コアプロファイラーエンジン
#[derive(Debug)]
pub struct ProfilerCore {
    /// Current session
    /// 現在のセッション
    current_session: Option<ProfilingSession>,
    /// Configuration
    /// 設定
    config: ProfilerConfig,
    /// Active timers
    /// アクティブタイマー
    active_timers: HashMap<String, Instant>,
    /// Call stack
    /// コールスタック
    call_stack: Vec<String>,
    /// Session history
    /// セッション履歴
    session_history: Vec<SessionSnapshot>,
}

impl ProfilerCore {
    /// Create new profiler core
    /// 新しいプロファイラーコアを作成
    pub fn new(config: ProfilerConfig) -> Self {
        Self {
            current_session: None,
            config,
            active_timers: HashMap::new(),
            call_stack: Vec::new(),
            session_history: Vec::new(),
        }
    }

    /// Start profiling session
    /// プロファイリングセッションを開始
    pub fn start_session(&mut self, name: String) -> RusTorchResult<()> {
        if self.current_session.is_some() {
            return Err(RusTorchError::Profiling {
                message: "Session already active".to_string(),
            });
        }

        let mut session = ProfilingSession::new(name, self.config.clone());
        session.start()?;
        self.current_session = Some(session);

        Ok(())
    }

    /// Stop current session
    /// 現在のセッションを停止
    pub fn stop_session(&mut self) -> RusTorchResult<SessionSnapshot> {
        let session = self
            .current_session
            .as_mut()
            .ok_or_else(|| RusTorchError::Profiling {
                message: "No active session".to_string(),
            })?;

        let snapshot = session.stop()?;
        self.session_history.push(snapshot.clone());
        self.current_session = None;
        self.call_stack.clear();
        self.active_timers.clear();

        Ok(snapshot)
    }

    /// Start timing operation
    /// 操作タイミング開始
    pub fn start_timer(&mut self, name: String) -> RusTorchResult<()> {
        if let Some(session) = &self.current_session {
            if session.state != SessionState::Running {
                return Err(RusTorchError::Profiling {
                    message: "Session not running".to_string(),
                });
            }
        } else {
            return Err(RusTorchError::Profiling {
                message: "No active session".to_string(),
            });
        }

        self.active_timers.insert(name.clone(), Instant::now());
        self.call_stack.push(name);

        Ok(())
    }

    /// Stop timing operation
    /// 操作タイミング停止
    pub fn stop_timer(&mut self, name: &str) -> RusTorchResult<f64> {
        let start_time =
            self.active_timers
                .remove(name)
                .ok_or_else(|| RusTorchError::Profiling {
                    message: "Timer not found".to_string(),
                })?;

        let duration = start_time.elapsed();

        if let Some(session) = &mut self.current_session {
            session.record_operation(name, duration, self.call_stack.len());
        }

        // Remove from call stack
        if let Some(pos) = self.call_stack.iter().rposition(|x| x == name) {
            self.call_stack.remove(pos);
        }

        Ok(duration.as_secs_f64() * 1000.0) // Return milliseconds
    }

    /// Record custom metric
    /// カスタムメトリクスを記録
    pub fn record_custom_metric(
        &mut self,
        name: &str,
        value: f64,
        metric_type: super::metrics_collector::MetricType,
    ) -> RusTorchResult<()> {
        if self.current_session.is_none() {
            return Err(RusTorchError::Profiling {
                message: "No active session".to_string(),
            });
        }

        // For now, store as operation with the value as duration in nanoseconds
        let duration = Duration::from_nanos((value * 1_000_000.0) as u64);

        if let Some(session) = &mut self.current_session {
            session.record_operation(name, duration, self.call_stack.len());
        }

        Ok(())
    }

    /// Get current session statistics
    /// 現在のセッション統計を取得
    pub fn get_current_statistics(&self) -> Option<Vec<PerformanceStatistics>> {
        self.current_session.as_ref().map(|session| {
            session
                .operations
                .values()
                .map(|op| op.get_statistics())
                .collect()
        })
    }

    /// Get session history
    /// セッション履歴を取得
    pub fn get_session_history(&self) -> &[SessionSnapshot] {
        &self.session_history
    }

    /// Clear session history
    /// セッション履歴をクリア
    pub fn clear_history(&mut self) {
        self.session_history.clear();
    }

    /// Get current configuration
    /// 現在の設定を取得
    pub fn get_config(&self) -> &ProfilerConfig {
        &self.config
    }

    /// Update configuration
    /// 設定を更新
    pub fn update_config(&mut self, config: ProfilerConfig) -> RusTorchResult<()> {
        if self.current_session.is_some() {
            return Err(RusTorchError::Profiling {
                message: "Cannot update config during active session".to_string(),
            });
        }

        self.config = config;
        Ok(())
    }
}

/// Generate unique session ID
/// 一意のセッションIDを生成
fn generate_session_id() -> String {
    let timestamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_millis();
    format!("session_{}", timestamp)
}

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

    #[test]
    fn test_profiler_core_creation() {
        let config = ProfilerConfig::default();
        let profiler = ProfilerCore::new(config);
        assert!(profiler.current_session.is_none());
    }

    #[test]
    fn test_session_lifecycle() {
        let config = ProfilerConfig::default();
        let mut profiler = ProfilerCore::new(config);

        // Start session
        assert!(profiler.start_session("test".to_string()).is_ok());
        assert!(profiler.current_session.is_some());

        // Stop session
        let snapshot = profiler.stop_session();
        assert!(snapshot.is_ok());
        assert!(profiler.current_session.is_none());
        assert_eq!(profiler.session_history.len(), 1);
    }

    #[test]
    fn test_timer_operations() {
        let config = ProfilerConfig::default();
        let mut profiler = ProfilerCore::new(config);

        profiler.start_session("test".to_string()).unwrap();

        // Start and stop timer
        assert!(profiler.start_timer("test_op".to_string()).is_ok());
        std::thread::sleep(Duration::from_millis(10));

        let elapsed = profiler.stop_timer("test_op");
        assert!(elapsed.is_ok());
        assert!(elapsed.unwrap() >= 10.0); // Should be at least 10ms

        // Check statistics
        let stats = profiler.get_current_statistics().unwrap();
        assert!(!stats.is_empty());
        assert_eq!(stats[0].operation_name, "test_op");
        assert_eq!(stats[0].call_count, 1);
    }

    #[test]
    fn test_operation_metrics() {
        let mut metrics = OperationMetrics::new("test".to_string());

        metrics.record_timing(Duration::from_millis(100));
        metrics.record_timing(Duration::from_millis(200));
        metrics.record_timing(Duration::from_millis(150));

        assert_eq!(metrics.call_count, 3);
        assert_eq!(metrics.min_time, Duration::from_millis(100));
        assert_eq!(metrics.max_time, Duration::from_millis(200));
        assert_eq!(metrics.avg_time, Duration::from_millis(150));

        let stats = metrics.get_statistics();
        assert_eq!(stats.call_count, 3);
        assert!((stats.avg_time_ms - 150.0).abs() < 0.1);
    }
}