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
//! Learning rate schedulers for optimizers
//! オプティマイザーの学習率スケジューラー

use crate::optim::Optimizer;
use num_traits::Float;
use std::fmt::Debug;

/// Trait for learning rate schedulers
/// 学習率スケジューラーのトレイト
/// 
/// Learning rate schedulers adjust the learning rate during training
/// to improve convergence and final performance.
/// 学習率スケジューラーは訓練中に学習率を調整して、
/// 収束と最終性能を改善します。
pub trait LRScheduler<T: Float> {
    /// Get the current learning rate
    /// 現在の学習率を取得
    fn get_lr(&self) -> Vec<T>;
    
    /// Step the scheduler (usually called after each epoch)
    /// スケジューラーをステップ(通常は各エポック後に呼び出し)
    fn step(&mut self);
    
    /// Step the scheduler with a validation metric (for ReduceLROnPlateau)
    /// 検証メトリックでスケジューラーをステップ(ReduceLROnPlateau用)
    fn step_with_metric(&mut self, _metric: T) {
        // Default implementation ignores metric
        self.step();
    }
    
    /// Get the last epoch number
    /// 最後のエポック番号を取得
    fn last_epoch(&self) -> i32;
    
    /// Get the current state for saving/loading
    /// 保存/読み込み用の現在の状態を取得
    fn state_dict(&self) -> SchedulerState<T>;
    
    /// Load state from saved state
    /// 保存された状態から状態を読み込み
    fn load_state_dict(&mut self, state: SchedulerState<T>);
}

/// State structure for saving/loading schedulers
/// スケジューラーの保存/読み込み用状態構造体
#[derive(Debug, Clone)]
pub struct SchedulerState<T: Float> {
    /// Last epoch number
    /// 最後のエポック番号
    pub last_epoch: i32,
    /// Base learning rates for each parameter group
    /// 各パラメータグループの基本学習率
    pub base_lrs: Vec<T>,
    /// Current step count
    /// 現在のステップ数
    pub step_count: usize,
    /// Best metric value seen so far
    /// これまでに見た最良のメトリック値
    pub best_metric: Option<T>,
    /// Number of epochs without improvement
    /// 改善のないエポック数
    pub num_bad_epochs: usize,
    /// Cooldown counter for reduce on plateau
    /// プラトー時削減のクールダウンカウンター
    pub cooldown_counter: usize,
}

/// Step learning rate scheduler
/// ステップ学習率スケジューラー
/// 
/// Decays the learning rate by gamma every step_size epochs.
/// step_sizeエポックごとに学習率をgammaで減衰させます。
#[derive(Debug)]
pub struct StepLR<T: Float> {
    step_size: usize,
    gamma: T,
    last_epoch: i32,
    base_lrs: Vec<T>,
    current_lrs: Vec<T>,
}

impl<T: Float + Copy + From<f32>> StepLR<T> {
    /// Creates a new StepLR scheduler
    /// 新しいStepLRスケジューラーを作成
    /// 
    /// # Arguments
    /// * `optimizer` - The optimizer to schedule
    /// * `step_size` - Period of learning rate decay
    /// * `gamma` - Multiplicative factor of learning rate decay
    /// * `last_epoch` - The index of last epoch
    /// 
    /// # 引数
    /// * `optimizer` - スケジュールするオプティマイザー
    /// * `step_size` - 学習率減衰の周期
    /// * `gamma` - 学習率減衰の乗数因子
    /// * `last_epoch` - 最後のエポックのインデックス
    pub fn new(
        _optimizer: &mut dyn Optimizer<T>,
        step_size: usize,
        gamma: T,
        last_epoch: Option<i32>,
    ) -> Self {
        let last_epoch = last_epoch.unwrap_or(-1);
        let base_lrs = vec![<T as From<f32>>::from(0.01f32)]; // Default learning rate for schedulers
        let current_lrs = base_lrs.clone();
        
        StepLR {
            step_size,
            gamma,
            last_epoch,
            base_lrs,
            current_lrs,
        }
    }
    
    /// Calculate learning rate for given epoch
    /// 指定されたエポックの学習率を計算
    fn calculate_lr(&self, base_lr: T, epoch: i32) -> T {
        let step_count = ((epoch + 1) as f32 / self.step_size as f32).floor() as i32;
        base_lr * self.gamma.powi(step_count)
    }
}

impl<T: Float + Copy + From<f32>> LRScheduler<T> for StepLR<T> {
    fn get_lr(&self) -> Vec<T> {
        self.current_lrs.clone()
    }
    
    fn step(&mut self) {
        self.last_epoch += 1;
        self.current_lrs = self.base_lrs
            .iter()
            .map(|&base_lr| self.calculate_lr(base_lr, self.last_epoch))
            .collect();
    }
    
    fn last_epoch(&self) -> i32 {
        self.last_epoch
    }
    
    fn state_dict(&self) -> SchedulerState<T> {
        SchedulerState {
            last_epoch: self.last_epoch,
            base_lrs: self.base_lrs.clone(),
            step_count: 0,
            best_metric: None,
            num_bad_epochs: 0,
            cooldown_counter: 0,
        }
    }
    
    fn load_state_dict(&mut self, state: SchedulerState<T>) {
        self.last_epoch = state.last_epoch;
        self.base_lrs = state.base_lrs;
    }
}

/// Exponential learning rate scheduler
/// 指数的学習率スケジューラー
/// 
/// Decays the learning rate by gamma every epoch.
/// 毎エポック学習率をgammaで減衰させます。
#[derive(Debug)]
pub struct ExponentialLR<T: Float> {
    gamma: T,
    last_epoch: i32,
    base_lrs: Vec<T>,
    current_lrs: Vec<T>,
}

impl<T: Float + Copy + From<f32>> ExponentialLR<T> {
    /// Creates a new ExponentialLR scheduler
    /// 新しいExponentialLRスケジューラーを作成
    pub fn new(
        _optimizer: &mut dyn Optimizer<T>,
        gamma: T,
        last_epoch: Option<i32>,
    ) -> Self {
        let last_epoch = last_epoch.unwrap_or(-1);
        let base_lrs = vec![<T as From<f32>>::from(0.01f32)]; // Default learning rate for schedulers
        let current_lrs = base_lrs.clone();
        
        ExponentialLR {
            gamma,
            last_epoch,
            base_lrs,
            current_lrs,
        }
    }
}

impl<T: Float + Copy + From<f32>> LRScheduler<T> for ExponentialLR<T> {
    fn get_lr(&self) -> Vec<T> {
        self.current_lrs.clone()
    }
    
    fn step(&mut self) {
        self.last_epoch += 1;
        self.current_lrs = self.base_lrs
            .iter()
            .map(|&base_lr| base_lr * self.gamma.powi(self.last_epoch + 1))
            .collect();
    }
    
    fn last_epoch(&self) -> i32 {
        self.last_epoch
    }
    
    fn state_dict(&self) -> SchedulerState<T> {
        SchedulerState {
            last_epoch: self.last_epoch,
            base_lrs: self.base_lrs.clone(),
            step_count: 0,
            best_metric: None,
            num_bad_epochs: 0,
            cooldown_counter: 0,
        }
    }
    
    fn load_state_dict(&mut self, state: SchedulerState<T>) {
        self.last_epoch = state.last_epoch;
        self.base_lrs = state.base_lrs;
    }
}

/// Cosine Annealing learning rate scheduler
/// コサイン・アニーリング学習率スケジューラー
/// 
/// Set the learning rate using a cosine annealing schedule.
/// コサイン・アニーリングスケジュールを使用して学習率を設定します。
#[derive(Debug)]
pub struct CosineAnnealingLR<T: Float> {
    t_max: usize,
    eta_min: T,
    last_epoch: i32,
    base_lrs: Vec<T>,
    current_lrs: Vec<T>,
}

impl<T: Float + Copy + From<f32>> CosineAnnealingLR<T> {
    /// Creates a new CosineAnnealingLR scheduler
    /// 新しいCosineAnnealingLRスケジューラーを作成
    /// 
    /// # Arguments
    /// * `optimizer` - The optimizer to schedule
    /// * `t_max` - Maximum number of iterations
    /// * `eta_min` - Minimum learning rate
    /// * `last_epoch` - The index of last epoch
    /// 
    /// # 引数
    /// * `optimizer` - スケジュールするオプティマイザー
    /// * `t_max` - 最大反復数
    /// * `eta_min` - 最小学習率
    /// * `last_epoch` - 最後のエポックのインデックス
    pub fn new(
        _optimizer: &mut dyn Optimizer<T>,
        t_max: usize,
        eta_min: Option<T>,
        last_epoch: Option<i32>,
    ) -> Self {
        let last_epoch = last_epoch.unwrap_or(-1);
        let eta_min = eta_min.unwrap_or_else(|| <T as From<f32>>::from(0.0f32));
        let base_lrs = vec![<T as From<f32>>::from(0.01f32)]; // Default learning rate for schedulers
        let current_lrs = base_lrs.clone();
        
        CosineAnnealingLR {
            t_max,
            eta_min,
            last_epoch,
            base_lrs,
            current_lrs,
        }
    }
    
    /// Calculate learning rate using cosine annealing
    /// コサイン・アニーリングを使用して学習率を計算
    fn calculate_lr(&self, base_lr: T, epoch: i32) -> T {
        if epoch < 0 {
            return base_lr;
        }
        
        let t_cur = epoch as f32;
        let t_max = self.t_max as f32;
        let pi = std::f32::consts::PI;
        
        let cosine_factor = <T as From<f32>>::from((1.0 + (pi * t_cur / t_max).cos()) / 2.0);
        self.eta_min + (base_lr - self.eta_min) * cosine_factor
    }
}

impl<T: Float + Copy + From<f32>> LRScheduler<T> for CosineAnnealingLR<T> {
    fn get_lr(&self) -> Vec<T> {
        self.current_lrs.clone()
    }
    
    fn step(&mut self) {
        self.last_epoch += 1;
        self.current_lrs = self.base_lrs
            .iter()
            .map(|&base_lr| self.calculate_lr(base_lr, self.last_epoch))
            .collect();
    }
    
    fn last_epoch(&self) -> i32 {
        self.last_epoch
    }
    
    fn state_dict(&self) -> SchedulerState<T> {
        SchedulerState {
            last_epoch: self.last_epoch,
            base_lrs: self.base_lrs.clone(),
            step_count: 0,
            best_metric: None,
            num_bad_epochs: 0,
            cooldown_counter: 0,
        }
    }
    
    fn load_state_dict(&mut self, state: SchedulerState<T>) {
        self.last_epoch = state.last_epoch;
        self.base_lrs = state.base_lrs;
    }
}

/// Reduce learning rate on plateau scheduler
/// プラトー時学習率削減スケジューラー
/// 
/// Reduce learning rate when a metric has stopped improving.
/// メトリックが改善を停止したときに学習率を削減します。
#[derive(Debug)]
pub struct ReduceLROnPlateau<T: Float> {
    mode: PlateauMode,
    factor: T,
    patience: usize,
    threshold: T,
    threshold_mode: ThresholdMode,
    cooldown: usize,
    min_lr: T,
    eps: T,
    last_epoch: i32,
    base_lrs: Vec<T>,
    current_lrs: Vec<T>,
    best_metric: Option<T>,
    num_bad_epochs: usize,
    cooldown_counter: usize,
}

/// Mode for determining when to reduce learning rate on plateau
/// プラトー時の学習率削減の判定モード
#[derive(Debug, Clone, Copy)]
pub enum PlateauMode {
    /// Reduce when metric stops decreasing
    /// メトリックの減少が止まったときに削減
    Min,
    /// Reduce when metric stops increasing
    /// メトリックの増加が止まったときに削減
    Max,
}

/// Mode for threshold comparison
/// 閾値比較モード
#[derive(Debug, Clone, Copy)]
pub enum ThresholdMode {
    /// Relative threshold
    /// 相対閾値
    Rel,
    /// Absolute threshold
    /// 絶対閾値
    Abs,
}

impl<T: Float + Copy + From<f32>> ReduceLROnPlateau<T> {
    /// Creates a new ReduceLROnPlateau scheduler
    /// 新しいReduceLROnPlateauスケジューラーを作成
    pub fn new(
        _optimizer: &mut dyn Optimizer<T>,
        mode: Option<PlateauMode>,
        factor: Option<T>,
        patience: Option<usize>,
        threshold: Option<T>,
        threshold_mode: Option<ThresholdMode>,
        cooldown: Option<usize>,
        min_lr: Option<T>,
        eps: Option<T>,
    ) -> Self {
        let mode = mode.unwrap_or(PlateauMode::Min);
        let factor = factor.unwrap_or_else(|| <T as From<f32>>::from(0.1f32));
        let patience = patience.unwrap_or(10);
        let threshold = threshold.unwrap_or_else(|| <T as From<f32>>::from(1e-4f32));
        let threshold_mode = threshold_mode.unwrap_or(ThresholdMode::Rel);
        let cooldown = cooldown.unwrap_or(0);
        let min_lr = min_lr.unwrap_or_else(|| <T as From<f32>>::from(0.0f32));
        let eps = eps.unwrap_or_else(|| <T as From<f32>>::from(1e-8f32));
        let base_lrs = vec![<T as From<f32>>::from(0.01f32)]; // Default learning rate for schedulers
        let current_lrs = base_lrs.clone();
        
        ReduceLROnPlateau {
            mode,
            factor,
            patience,
            threshold,
            threshold_mode,
            cooldown,
            min_lr,
            eps,
            last_epoch: 0,
            base_lrs,
            current_lrs,
            best_metric: None,
            num_bad_epochs: 0,
            cooldown_counter: 0,
        }
    }
    
    /// Check if metric has improved
    /// メトリックが改善したかどうかをチェック
    fn is_better(&self, current: T, best: T) -> bool {
        match (self.mode, self.threshold_mode) {
            (PlateauMode::Min, ThresholdMode::Rel) => current < best * (T::one() - self.threshold),
            (PlateauMode::Min, ThresholdMode::Abs) => current < best - self.threshold,
            (PlateauMode::Max, ThresholdMode::Rel) => current > best * (T::one() + self.threshold),
            (PlateauMode::Max, ThresholdMode::Abs) => current > best + self.threshold,
        }
    }
}

impl<T: Float + Copy + From<f32>> LRScheduler<T> for ReduceLROnPlateau<T> {
    fn get_lr(&self) -> Vec<T> {
        self.current_lrs.clone()
    }
    
    fn step(&mut self) {
        // This scheduler doesn't step without a metric
    }
    
    fn step_with_metric(&mut self, metric: T) {
        if self.cooldown_counter > 0 {
            self.cooldown_counter -= 1;
            self.num_bad_epochs = 0;
        }
        
        if self.best_metric.is_none() {
            self.best_metric = Some(metric);
        } else if self.is_better(metric, self.best_metric.unwrap()) {
            self.best_metric = Some(metric);
            self.num_bad_epochs = 0;
        } else {
            self.num_bad_epochs += 1;
        }
        
        if self.cooldown_counter == 0 && self.num_bad_epochs > self.patience {
            self.reduce_lr();
            self.cooldown_counter = self.cooldown;
            self.num_bad_epochs = 0;
        }
    }
    
    fn last_epoch(&self) -> i32 {
        self.last_epoch
    }
    
    fn state_dict(&self) -> SchedulerState<T> {
        SchedulerState {
            last_epoch: self.last_epoch,
            base_lrs: self.base_lrs.clone(),
            step_count: 0,
            best_metric: self.best_metric,
            num_bad_epochs: self.num_bad_epochs,
            cooldown_counter: self.cooldown_counter,
        }
    }
    
    fn load_state_dict(&mut self, state: SchedulerState<T>) {
        self.last_epoch = state.last_epoch;
        self.base_lrs = state.base_lrs;
        self.best_metric = state.best_metric;
        self.num_bad_epochs = state.num_bad_epochs;
        self.cooldown_counter = state.cooldown_counter;
    }
}

impl<T: Float + Copy + From<f32>> ReduceLROnPlateau<T> {
    fn reduce_lr(&mut self) {
        for i in 0..self.current_lrs.len() {
            let new_lr = (self.current_lrs[i] * self.factor).max(self.min_lr);
            if (self.current_lrs[i] - new_lr).abs() > self.eps {
                self.current_lrs[i] = new_lr;
                self.base_lrs[i] = new_lr; // Update base_lrs as well for ReduceLROnPlateau
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::optim::SGD;
    use crate::autograd::Variable;
    use crate::tensor::Tensor;

    #[test]
    fn test_step_lr_creation() {
        let params = vec![Variable::new(Tensor::ones(&[2, 2]), true)];
        let mut optimizer = SGD::new(params, 0.01, Some(0.9), None, None, None);
        
        let scheduler = StepLR::new(&mut optimizer, 30, 0.1, None);
        assert_eq!(scheduler.last_epoch(), -1);
        assert_eq!(scheduler.step_size, 30);
    }

    #[test]
    fn test_step_lr_calculation() {
        let params = vec![Variable::new(Tensor::ones(&[2, 2]), true)];
        let mut optimizer = SGD::new(params, 0.01, Some(0.9), None, None, None);
        
        let mut scheduler = StepLR::new(&mut optimizer, 30, 0.1, None);
        
        // Initial learning rate
        let initial_lrs = scheduler.get_lr();
        assert_eq!(initial_lrs.len(), 1);
        
        // Step and check
        scheduler.step();
        assert_eq!(scheduler.last_epoch(), 0);
    }

    #[test]
    fn test_exponential_lr() {
        let params = vec![Variable::new(Tensor::ones(&[2, 2]), true)];
        let mut optimizer = SGD::new(params, 0.01, Some(0.9), None, None, None);
        
        let mut scheduler = ExponentialLR::new(&mut optimizer, 0.9, None);
        
        let initial_lrs = scheduler.get_lr();
        scheduler.step();
        let step1_lrs = scheduler.get_lr();
        
        // Learning rate should decay exponentially
        assert!(step1_lrs[0] < initial_lrs[0]);
    }

    #[test]
    fn test_cosine_annealing_lr() {
        let params = vec![Variable::new(Tensor::ones(&[2, 2]), true)];
        let mut optimizer = SGD::new(params, 0.01, Some(0.9), None, None, None);
        
        let mut scheduler = CosineAnnealingLR::new(&mut optimizer, 100, None, None);
        
        let initial_lrs = scheduler.get_lr();
        
        // Step through half the cycle
        for _ in 0..50 {
            scheduler.step();
        }
        let mid_lrs = scheduler.get_lr();
        
        // At T_max/2, learning rate should be at minimum (eta_min)
        assert!(mid_lrs[0] < initial_lrs[0]);
    }

    #[test]
    fn test_reduce_lr_on_plateau() {
        let params = vec![Variable::new(Tensor::ones(&[2, 2]), true)];
        let mut optimizer = SGD::new(params, 0.01, Some(0.9), None, None, None);
        
        let mut scheduler = ReduceLROnPlateau::new(
            &mut optimizer,
            Some(PlateauMode::Min),
            Some(0.5),
            Some(2),
            None,
            None,
            None,
            None,
            None,
        );
        
        let _initial_lrs = scheduler.get_lr();
        
        // Simulate no improvement for patience + 1 steps
        scheduler.step_with_metric(1.0);
        scheduler.step_with_metric(1.1);
        scheduler.step_with_metric(1.2);
        scheduler.step_with_metric(1.3);
        
        let _reduced_lrs = scheduler.get_lr();
        
        // Learning rate should be reduced after patience is exceeded
        // Note: In this simplified test, we check the state rather than actual LR change
        assert_eq!(scheduler.num_bad_epochs, 0); // Should reset after reduction
    }
}