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
//! Gradient scaler for mixed precision training
//! 混合精度学習のための勾配スケーラー

use crate::autograd::Variable;
use crate::optim::Optimizer;
use crate::tensor::Tensor;
use std::collections::HashMap;

/// Result of a gradient scaling step
#[derive(Debug, Clone)]
pub enum StepResult {
    /// Step completed successfully
    Success {
        /// Scale factor used
        scale: f32,
        /// Gradient norm before clipping (if clipping was applied)
        grad_norm: Option<f32>,
    },
    /// Step skipped due to gradient overflow
    Overflow {
        /// Current scale factor
        scale: f32,
        /// New scale factor after backoff
        new_scale: f32,
    },
    /// Step skipped due to inf/nan in gradients
    InfNan {
        /// Current scale factor
        scale: f32,
    },
}

/// State of the gradient scaler
#[derive(Clone, Debug)]
pub struct ScalerState {
    /// Current scale factor
    pub scale: f32,
    /// Growth factor for scale
    pub growth_factor: f32,
    /// Backoff factor for scale
    pub backoff_factor: f32,
    /// Number of iterations between scale updates
    pub growth_interval: usize,
    /// Number of iterations since last scale update
    pub growth_tracker: usize,
    /// Number of consecutive non-overflowed iterations
    pub consecutive_non_overflow: usize,
    /// Whether dynamic scaling is enabled
    pub enabled: bool,
}

impl Default for ScalerState {
    fn default() -> Self {
        Self {
            scale: 65536.0, // 2^16
            growth_factor: 2.0,
            backoff_factor: 0.5,
            growth_interval: 2000,
            growth_tracker: 0,
            consecutive_non_overflow: 0,
            enabled: true,
        }
    }
}

/// Gradient scaler for automatic mixed precision training
pub struct GradScaler {
    state: ScalerState,
    /// Track if gradients overflowed in current iteration
    found_overflow: bool,
    /// Cache for scaled gradients
    scaled_grads: HashMap<usize, Tensor<f32>>,
}

impl Default for GradScaler {
    fn default() -> Self {
        Self::new(None, None, None, None, None)
    }
}

impl GradScaler {
    /// Create a new gradient scaler
    pub fn new(
        init_scale: Option<f32>,
        growth_factor: Option<f32>,
        backoff_factor: Option<f32>,
        growth_interval: Option<usize>,
        enabled: Option<bool>,
    ) -> Self {
        let mut state = ScalerState::default();

        if let Some(scale) = init_scale {
            state.scale = scale;
        }
        if let Some(factor) = growth_factor {
            state.growth_factor = factor;
        }
        if let Some(factor) = backoff_factor {
            state.backoff_factor = factor;
        }
        if let Some(interval) = growth_interval {
            state.growth_interval = interval;
        }
        if let Some(enabled) = enabled {
            state.enabled = enabled;
        }

        Self {
            state,
            found_overflow: false,
            scaled_grads: HashMap::new(),
        }
    }

    /// Create a default scaler
    #[allow(clippy::should_implement_trait)]
    pub fn default() -> Self {
        Self::new(None, None, None, None, None)
    }

    /// Scale the loss
    pub fn scale(&self, loss: &Variable<f32>) -> Variable<f32> {
        if !self.state.enabled {
            return loss.clone();
        }

        // Scale the loss by multiplying with scale factor
        let scale_tensor = Tensor::from_vec(vec![self.state.scale], vec![1]);
        let scale_var = Variable::new(scale_tensor, false);

        // loss * scale
        loss * &scale_var
    }

    /// Scale tensor directly
    pub fn scale_tensor(&self, tensor: &Tensor<f32>) -> Tensor<f32> {
        if !self.state.enabled {
            return tensor.clone();
        }

        tensor * self.state.scale
    }

    /// Unscale gradients
    pub fn unscale_grads(&mut self, _optimizer: &mut dyn Optimizer) {
        if !self.state.enabled {}

        // In a real implementation, we would iterate through optimizer's parameters
        // and unscale their gradients by dividing by the scale factor

        // For now, mark that unscaling has been done
        // This would be called before optimizer.step()
    }

    /// Check for gradient overflow/underflow
    pub fn check_overflow(&mut self, gradients: &[Tensor<f32>]) -> bool {
        if !self.state.enabled {
            return false;
        }

        for grad in gradients {
            if let Some(slice) = grad.as_slice() {
                for &value in slice {
                    if !value.is_finite() || value.abs() > 65504.0 {
                        // FP16 max
                        self.found_overflow = true;
                        return true;
                    }
                }
            }
        }

        false
    }

    /// Perform optimizer step with scaling
    pub fn step<O: Optimizer>(
        &mut self,
        optimizer: &mut O,
        params: &[Tensor<f32>],
        grads: &[Tensor<f32>],
    ) {
        if !self.state.enabled {
            // Normal step without scaling
            for (param, grad) in params.iter().zip(grads.iter()) {
                optimizer.step(param, grad);
            }
            return;
        }

        // Check for overflow
        if self.check_overflow(grads) {
            // Skip this step due to overflow
            self.found_overflow = true;
            return;
        }

        // Unscale gradients
        let unscaled_grads: Vec<Tensor<f32>> = grads.iter().map(|g| g / self.state.scale).collect();

        // Perform optimizer step with unscaled gradients
        for (param, grad) in params.iter().zip(unscaled_grads.iter()) {
            optimizer.step(param, grad);
        }

        // Update scale
        self.update_scale();
    }

    /// Advanced step with gradient clipping and inf/nan detection
    pub fn step_with_clipping<O: Optimizer>(
        &mut self,
        optimizer: &mut O,
        params: &[Tensor<f32>],
        grads: &mut [Tensor<f32>],
        max_grad_norm: Option<f32>,
    ) -> StepResult {
        if !self.state.enabled {
            // Normal step without scaling
            if let Some(max_norm) = max_grad_norm {
                crate::amp::dtype_utils::utils::clip_grad_norm(grads, max_norm);
            }
            for (param, grad) in params.iter().zip(grads.iter()) {
                optimizer.step(param, grad);
            }
            return StepResult::Success {
                scale: 1.0,
                grad_norm: None,
            };
        }

        // Check for overflow before unscaling
        if self.check_overflow(grads) {
            self.found_overflow = true;
            return StepResult::Overflow {
                scale: self.state.scale,
                new_scale: self.state.scale * self.state.backoff_factor,
            };
        }

        // Unscale gradients
        for grad in grads.iter_mut() {
            *grad = grad.clone() / self.state.scale;
        }

        // Clip gradients if requested
        let grad_norm = max_grad_norm
            .map(|max_norm| crate::amp::dtype_utils::utils::clip_grad_norm(grads, max_norm));

        // Final inf/nan check after unscaling and clipping
        if self.check_overflow(grads) {
            self.found_overflow = true;
            return StepResult::InfNan {
                scale: self.state.scale,
            };
        }

        // Perform optimizer step
        for (param, grad) in params.iter().zip(grads.iter()) {
            optimizer.step(param, grad);
        }

        // Update scale
        let old_scale = self.state.scale;
        self.update_scale();

        StepResult::Success {
            scale: old_scale,
            grad_norm,
        }
    }

    /// Update the scale factor
    pub fn update_scale(&mut self) {
        if !self.state.enabled {
            return;
        }

        self.state.growth_tracker += 1;

        if self.found_overflow {
            // Backoff the scale
            self.state.scale *= self.state.backoff_factor;
            self.state.consecutive_non_overflow = 0;
            self.found_overflow = false;
        } else {
            self.state.consecutive_non_overflow += 1;

            // Check if we should grow the scale
            if self.state.growth_tracker >= self.state.growth_interval {
                self.state.scale *= self.state.growth_factor;
                self.state.growth_tracker = 0;
            }
        }

        // Clamp scale to reasonable bounds
        self.state.scale = self.state.scale.clamp(1.0, 65536.0 * 65536.0);
    }

    /// Get current scale
    pub fn get_scale(&self) -> f32 {
        self.state.scale
    }

    /// Set scale manually
    pub fn set_scale(&mut self, scale: f32) {
        self.state.scale = scale;
    }

    /// Load scaler state
    pub fn load_state_dict(&mut self, state: ScalerState) {
        self.state = state;
    }

    /// Get scaler state
    pub fn state_dict(&self) -> ScalerState {
        self.state.clone()
    }

    /// Check if scaling is enabled
    pub fn is_enabled(&self) -> bool {
        self.state.enabled
    }

    /// Enable or disable scaling
    pub fn set_enabled(&mut self, enabled: bool) {
        self.state.enabled = enabled;
    }

    /// Reset scaler state (useful after loading checkpoints)
    pub fn reset(&mut self) {
        self.found_overflow = false;
        self.scaled_grads.clear();
        self.state.growth_tracker = 0;
        self.state.consecutive_non_overflow = 0;
    }

    /// Get detailed statistics about scaling
    pub fn get_stats(&self) -> ScalerStats {
        ScalerStats {
            current_scale: self.state.scale,
            growth_factor: self.state.growth_factor,
            backoff_factor: self.state.backoff_factor,
            growth_interval: self.state.growth_interval,
            growth_tracker: self.state.growth_tracker,
            consecutive_non_overflow: self.state.consecutive_non_overflow,
            enabled: self.state.enabled,
            has_overflow: self.found_overflow,
        }
    }

    /// Set scale bounds
    pub fn set_scale_bounds(&mut self, min_scale: f32, max_scale: f32) {
        self.state.scale = self.state.scale.max(min_scale).min(max_scale);
    }

    /// Gradually adjust growth interval based on overflow frequency
    pub fn adaptive_growth_interval(&mut self, overflow_rate: f32) {
        if overflow_rate > 0.1 {
            // Increase interval if overflow rate is high
            self.state.growth_interval = (self.state.growth_interval * 2).min(10000);
        } else if overflow_rate < 0.01 {
            // Decrease interval if overflow rate is very low
            self.state.growth_interval = (self.state.growth_interval / 2).max(100);
        }
    }
}

/// Statistics about gradient scaling
/// 勾配スケーリングの統計情報
#[derive(Debug, Clone)]
pub struct ScalerStats {
    /// Current scale factor
    /// 現在のスケール係数
    pub current_scale: f32,
    /// Growth factor for scale updates
    /// スケール更新の成長係数
    pub growth_factor: f32,
    /// Backoff factor when overflow occurs
    /// オーバーフロー時の後退係数
    pub backoff_factor: f32,
    /// Interval between growth updates
    /// 成長更新の間隔
    pub growth_interval: usize,
    /// Current growth tracking counter
    /// 現在の成長追跡カウンタ
    pub growth_tracker: usize,
    /// Number of consecutive non-overflow steps
    /// 連続非オーバーフローステップ数
    pub consecutive_non_overflow: usize,
    /// Whether scaling is enabled
    /// スケーリングが有効かどうか
    pub enabled: bool,
    /// Whether overflow was detected
    /// オーバーフローが検出されたかどうか
    pub has_overflow: bool,
}

/// Utility functions for gradient scaling
pub mod utils {}

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

    #[test]
    fn test_grad_scaler_creation() {
        let scaler = GradScaler::default();
        assert_eq!(scaler.get_scale(), 65536.0);
        assert!(scaler.is_enabled());
    }

    #[test]
    fn test_grad_scaler_custom() {
        let scaler = GradScaler::new(Some(1024.0), Some(3.0), Some(0.3), Some(1000), Some(true));
        assert_eq!(scaler.get_scale(), 1024.0);
        assert_eq!(scaler.state.growth_factor, 3.0);
        assert_eq!(scaler.state.backoff_factor, 0.3);
    }

    #[test]
    fn test_scale_tensor() {
        let scaler = GradScaler::default();
        let tensor = Tensor::from_vec(vec![1.0, 2.0, 3.0], vec![3]);
        let scaled = scaler.scale_tensor(&tensor);

        let expected = tensor * scaler.get_scale();
        assert_eq!(scaled.as_slice(), expected.as_slice());
    }

    #[test]
    fn test_overflow_detection() {
        let mut scaler = GradScaler::default();

        // Normal gradients
        let normal_grads = vec![
            Tensor::from_vec(vec![1.0, 2.0, 3.0], vec![3]),
            Tensor::from_vec(vec![4.0, 5.0, 6.0], vec![3]),
        ];
        assert!(!scaler.check_overflow(&normal_grads));

        // Overflow gradients
        let overflow_grads = vec![Tensor::from_vec(vec![1.0, 2.0, 100000.0], vec![3])];
        assert!(scaler.check_overflow(&overflow_grads));

        // NaN gradients
        let nan_grads = vec![Tensor::from_vec(vec![1.0, f32::NAN, 3.0], vec![3])];
        assert!(scaler.check_overflow(&nan_grads));
    }

    #[test]
    fn test_scale_update() {
        let mut scaler = GradScaler::new(Some(1024.0), Some(2.0), Some(0.5), Some(2), Some(true));

        let initial_scale = scaler.get_scale();

        // Simulate overflow
        scaler.found_overflow = true;
        scaler.update_scale();
        assert_eq!(scaler.get_scale(), initial_scale * 0.5);

        // Simulate successful iterations
        scaler.found_overflow = false;
        scaler.update_scale();
        scaler.update_scale();
        // Should grow after growth_interval
        assert_eq!(scaler.get_scale(), initial_scale * 0.5 * 2.0);
    }
}