tensorlogic-train 0.1.0

Training loops, loss composition, and optimization schedules for TensorLogic
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
//! Mixed precision training infrastructure for memory efficiency and speed.
//!
//! This module provides utilities for training with reduced precision (FP16/BF16)
//! while maintaining numerical stability through loss scaling and gradient management.
//!
//! # Features
//! - FP16 and BF16 precision modes
//! - Dynamic and static loss scaling
//! - Gradient overflow detection
//! - Master weight management
//! - Automatic precision casting
//!
//! # Example
//!
//! ```rust,ignore
//! use tensorlogic_train::{MixedPrecisionTrainer, PrecisionMode, LossScaler};
//!
//! // Create FP16 trainer with dynamic loss scaling
//! let mut trainer = MixedPrecisionTrainer::new(
//!     PrecisionMode::FP16,
//!     LossScaler::dynamic(2.0_f32.powi(15), 2.0, 2000),
//! );
//!
//! // Train with automatic precision management
//! trainer.scale_loss(loss);
//! ```

use scirs2_core::ndarray::Array2;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::error::TrainResult;

/// Precision mode for mixed precision training.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PrecisionMode {
    /// Full precision (FP32) - baseline
    FP32,
    /// Half precision (FP16) - 2x memory reduction
    FP16,
    /// Brain floating point (BF16) - better for training
    BF16,
}

impl PrecisionMode {
    /// Returns the number of bytes per element for this precision.
    pub fn bytes_per_element(&self) -> usize {
        match self {
            PrecisionMode::FP32 => 4,
            PrecisionMode::FP16 => 2,
            PrecisionMode::BF16 => 2,
        }
    }

    /// Returns the memory reduction factor compared to FP32.
    pub fn memory_reduction(&self) -> f32 {
        match self {
            PrecisionMode::FP32 => 1.0,
            PrecisionMode::FP16 => 2.0,
            PrecisionMode::BF16 => 2.0,
        }
    }

    /// Returns the typical numerical range for this precision.
    pub fn numerical_range(&self) -> (f32, f32) {
        match self {
            PrecisionMode::FP32 => (-3.4e38, 3.4e38),
            PrecisionMode::FP16 => (-6.55e4, 6.55e4),
            PrecisionMode::BF16 => (-3.39e38, 3.39e38), // Same exponent range as FP32
        }
    }
}

/// Loss scaling strategy for mixed precision training.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum LossScaler {
    /// No loss scaling (not recommended for FP16)
    None,
    /// Static loss scaling with fixed scale factor
    Static { scale: f32 },
    /// Dynamic loss scaling that adjusts based on gradient overflow
    Dynamic {
        /// Current scale factor
        scale: f32,
        /// Growth factor when no overflow (typically 2.0)
        growth_factor: f32,
        /// Backoff factor when overflow detected (typically 0.5)
        backoff_factor: f32,
        /// Number of successful steps before growing scale
        growth_interval: usize,
        /// Current step counter
        steps_since_overflow: usize,
    },
}

impl LossScaler {
    /// Creates a static loss scaler.
    pub fn static_scale(scale: f32) -> Self {
        Self::Static { scale }
    }

    /// Creates a dynamic loss scaler with typical defaults.
    ///
    /// # Arguments
    /// * `initial_scale` - Starting scale (typically 2^15 = 32768)
    /// * `growth_factor` - How much to grow scale (typically 2.0)
    /// * `growth_interval` - Steps before growing (typically 2000)
    pub fn dynamic(initial_scale: f32, growth_factor: f32, growth_interval: usize) -> Self {
        Self::Dynamic {
            scale: initial_scale,
            growth_factor,
            backoff_factor: 0.5,
            growth_interval,
            steps_since_overflow: 0,
        }
    }

    /// Gets the current scale factor.
    pub fn get_scale(&self) -> f32 {
        match self {
            Self::None => 1.0,
            Self::Static { scale } => *scale,
            Self::Dynamic { scale, .. } => *scale,
        }
    }

    /// Scales a loss value.
    pub fn scale_loss(&self, loss: f32) -> f32 {
        loss * self.get_scale()
    }

    /// Unscales gradients (divides by scale).
    pub fn unscale_gradients(&self, gradients: &mut Array2<f32>) {
        let scale = self.get_scale();
        if scale != 1.0 {
            *gradients /= scale;
        }
    }

    /// Updates the dynamic scaler based on overflow detection.
    ///
    /// # Arguments
    /// * `overflow_detected` - Whether gradient overflow was detected
    ///
    /// # Returns
    /// True if the optimizer step should proceed
    pub fn update(&mut self, overflow_detected: bool) -> bool {
        if let Self::Dynamic {
            scale,
            growth_factor,
            backoff_factor,
            growth_interval,
            steps_since_overflow,
        } = self
        {
            if overflow_detected {
                // Backoff: reduce scale and reset counter
                *scale *= *backoff_factor;
                *steps_since_overflow = 0;
                false // Skip optimizer step
            } else {
                // Increment counter
                *steps_since_overflow += 1;

                // Grow scale if interval reached
                if *steps_since_overflow >= *growth_interval {
                    *scale *= *growth_factor;
                    *steps_since_overflow = 0;
                }
                true // Proceed with optimizer step
            }
        } else {
            // Static or None scaling always proceeds
            !overflow_detected
        }
    }
}

/// Mixed precision training manager.
pub struct MixedPrecisionTrainer {
    /// Precision mode
    mode: PrecisionMode,
    /// Loss scaler
    scaler: LossScaler,
    /// Master weights (FP32) - keeps full precision copy
    master_weights: HashMap<String, Array2<f32>>,
    /// Training statistics
    stats: MixedPrecisionStats,
}

impl MixedPrecisionTrainer {
    /// Creates a new mixed precision trainer.
    pub fn new(mode: PrecisionMode, scaler: LossScaler) -> Self {
        Self {
            mode,
            scaler,
            master_weights: HashMap::new(),
            stats: MixedPrecisionStats::default(),
        }
    }

    /// Registers weights to maintain master copy.
    pub fn register_weights(&mut self, name: String, weights: Array2<f32>) {
        self.master_weights.insert(name, weights);
    }

    /// Converts FP32 weights to working precision.
    pub fn cast_to_working_precision(&self, weights: &Array2<f32>) -> Array2<f32> {
        match self.mode {
            PrecisionMode::FP32 => weights.clone(),
            PrecisionMode::FP16 => self.simulate_fp16(weights),
            PrecisionMode::BF16 => self.simulate_bf16(weights),
        }
    }

    /// Simulates FP16 precision (in FP32 container for compatibility).
    fn simulate_fp16(&self, weights: &Array2<f32>) -> Array2<f32> {
        weights.mapv(|x| {
            // Clamp to FP16 range
            let clamped = x.clamp(-65504.0, 65504.0);
            // Simulate reduced mantissa precision (10 bits vs 23 bits)
            let scale = 2.0_f32.powi(10);
            (clamped * scale).round() / scale
        })
    }

    /// Simulates BF16 precision (in FP32 container for compatibility).
    fn simulate_bf16(&self, weights: &Array2<f32>) -> Array2<f32> {
        weights.mapv(|x| {
            // BF16 has same exponent range as FP32, reduced mantissa (7 bits vs 23 bits)
            let scale = 2.0_f32.powi(7);
            (x * scale).round() / scale
        })
    }

    /// Scales loss for backward pass.
    pub fn scale_loss(&mut self, loss: f32) -> f32 {
        self.stats.total_steps += 1;
        self.scaler.scale_loss(loss)
    }

    /// Unscales and checks gradients for overflow.
    ///
    /// # Returns
    /// (should_step, overflow_detected)
    pub fn unscale_and_check_gradients(
        &mut self,
        gradients: &mut HashMap<String, Array2<f32>>,
    ) -> TrainResult<(bool, bool)> {
        // Check for overflow before unscaling
        let mut overflow = false;
        for (_name, grad) in gradients.iter() {
            if grad.iter().any(|&x| !x.is_finite()) {
                overflow = true;
                break;
            }
        }

        if overflow {
            self.stats.overflow_steps += 1;
        }

        // Unscale gradients
        for (_name, grad) in gradients.iter_mut() {
            self.scaler.unscale_gradients(grad);
        }

        // Update scaler and determine if we should step
        let should_step = self.scaler.update(overflow);

        Ok((should_step, overflow))
    }

    /// Updates master weights from working precision weights.
    pub fn update_master_weights(&mut self, updates: &HashMap<String, Array2<f32>>) {
        for (name, update) in updates {
            if let Some(master) = self.master_weights.get_mut(name) {
                *master = master.clone() + update;
            }
        }
    }

    /// Gets the current precision mode.
    pub fn mode(&self) -> PrecisionMode {
        self.mode
    }

    /// Gets the current loss scale.
    pub fn current_scale(&self) -> f32 {
        self.scaler.get_scale()
    }

    /// Gets training statistics.
    pub fn stats(&self) -> &MixedPrecisionStats {
        &self.stats
    }

    /// Resets statistics.
    pub fn reset_stats(&mut self) {
        self.stats = MixedPrecisionStats::default();
    }
}

/// Statistics for mixed precision training.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct MixedPrecisionStats {
    /// Total training steps attempted
    pub total_steps: usize,
    /// Steps with gradient overflow
    pub overflow_steps: usize,
    /// Successful optimizer steps
    pub successful_steps: usize,
}

impl MixedPrecisionStats {
    /// Calculates overflow rate.
    pub fn overflow_rate(&self) -> f32 {
        if self.total_steps == 0 {
            0.0
        } else {
            self.overflow_steps as f32 / self.total_steps as f32
        }
    }

    /// Calculates success rate.
    pub fn success_rate(&self) -> f32 {
        if self.total_steps == 0 {
            0.0
        } else {
            self.successful_steps as f32 / self.total_steps as f32
        }
    }
}

/// Gradient scaler for automatic mixed precision.
pub struct GradientScaler {
    scaler: LossScaler,
    enabled: bool,
}

impl GradientScaler {
    /// Creates a new gradient scaler.
    pub fn new(enabled: bool) -> Self {
        let scaler = if enabled {
            LossScaler::dynamic(2.0_f32.powi(15), 2.0, 2000)
        } else {
            LossScaler::None
        };

        Self { scaler, enabled }
    }

    /// Creates a gradient scaler with custom settings.
    pub fn with_scaler(scaler: LossScaler, enabled: bool) -> Self {
        Self { scaler, enabled }
    }

    /// Scales a loss tensor.
    pub fn scale(&self, loss: f32) -> f32 {
        if self.enabled {
            self.scaler.scale_loss(loss)
        } else {
            loss
        }
    }

    /// Unscales gradients.
    pub fn unscale(&self, gradients: &mut Array2<f32>) {
        if self.enabled {
            self.scaler.unscale_gradients(gradients);
        }
    }

    /// Steps with overflow check.
    pub fn step(&mut self, overflow_detected: bool) -> bool {
        if self.enabled {
            self.scaler.update(overflow_detected)
        } else {
            !overflow_detected
        }
    }

    /// Gets the current scale.
    pub fn get_scale(&self) -> f32 {
        self.scaler.get_scale()
    }
}

/// Automatic Mixed Precision (AMP) context manager.
pub struct AutocastContext {
    enabled: bool,
    mode: PrecisionMode,
}

impl AutocastContext {
    /// Creates a new autocast context.
    pub fn new(enabled: bool, mode: PrecisionMode) -> Self {
        Self { enabled, mode }
    }

    /// Checks if autocast is enabled.
    pub fn is_enabled(&self) -> bool {
        self.enabled
    }

    /// Gets the target precision mode.
    pub fn mode(&self) -> PrecisionMode {
        self.mode
    }

    /// Casts tensor to working precision if enabled.
    pub fn cast(&self, tensor: &Array2<f32>) -> Array2<f32> {
        if !self.enabled || self.mode == PrecisionMode::FP32 {
            return tensor.clone();
        }

        match self.mode {
            PrecisionMode::FP16 => self.simulate_fp16(tensor),
            PrecisionMode::BF16 => self.simulate_bf16(tensor),
            PrecisionMode::FP32 => tensor.clone(),
        }
    }

    fn simulate_fp16(&self, tensor: &Array2<f32>) -> Array2<f32> {
        tensor.mapv(|x| {
            let clamped = x.clamp(-65504.0, 65504.0);
            let scale = 2.0_f32.powi(10);
            (clamped * scale).round() / scale
        })
    }

    fn simulate_bf16(&self, tensor: &Array2<f32>) -> Array2<f32> {
        tensor.mapv(|x| {
            let scale = 2.0_f32.powi(7);
            (x * scale).round() / scale
        })
    }
}

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

    #[test]
    fn test_precision_mode_properties() {
        assert_eq!(PrecisionMode::FP32.bytes_per_element(), 4);
        assert_eq!(PrecisionMode::FP16.bytes_per_element(), 2);
        assert_eq!(PrecisionMode::BF16.bytes_per_element(), 2);

        assert_eq!(PrecisionMode::FP16.memory_reduction(), 2.0);
        assert_eq!(PrecisionMode::BF16.memory_reduction(), 2.0);
    }

    #[test]
    fn test_static_loss_scaler() {
        let scaler = LossScaler::static_scale(1024.0);
        assert_eq!(scaler.get_scale(), 1024.0);

        let loss = 0.5;
        let scaled = scaler.scale_loss(loss);
        assert_eq!(scaled, 512.0);
    }

    #[test]
    fn test_dynamic_loss_scaler() {
        let mut scaler = LossScaler::dynamic(1000.0, 2.0, 3);
        assert_eq!(scaler.get_scale(), 1000.0);

        // No overflow, should grow after 3 steps
        assert!(scaler.update(false));
        assert!(scaler.update(false));
        assert!(scaler.update(false));
        assert_eq!(scaler.get_scale(), 2000.0); // Grew

        // Overflow, should backoff
        assert!(!scaler.update(true));
        assert_eq!(scaler.get_scale(), 1000.0); // Backoff
    }

    #[test]
    fn test_gradient_unscaling() {
        let mut gradients =
            Array2::from_shape_vec((2, 2), vec![100.0, 200.0, 300.0, 400.0]).expect("unwrap");
        let scaler = LossScaler::static_scale(10.0);

        scaler.unscale_gradients(&mut gradients);

        assert_eq!(gradients[[0, 0]], 10.0);
        assert_eq!(gradients[[0, 1]], 20.0);
        assert_eq!(gradients[[1, 0]], 30.0);
        assert_eq!(gradients[[1, 1]], 40.0);
    }

    #[test]
    fn test_mixed_precision_trainer() {
        let mut trainer =
            MixedPrecisionTrainer::new(PrecisionMode::FP16, LossScaler::static_scale(100.0));

        let loss = 0.5;
        let scaled_loss = trainer.scale_loss(loss);
        assert_eq!(scaled_loss, 50.0);
        assert_eq!(trainer.stats().total_steps, 1);
    }

    #[test]
    fn test_fp16_simulation() {
        let trainer = MixedPrecisionTrainer::new(PrecisionMode::FP16, LossScaler::None);

        let weights = Array2::from_shape_vec((2, 2), vec![1.234_567, 100000.0, -100000.0, 0.0001])
            .expect("unwrap");
        let fp16_weights = trainer.cast_to_working_precision(&weights);

        // Should be quantized
        assert_ne!(fp16_weights[[0, 0]], 1.234_567); // Reduced precision
        assert!(fp16_weights[[0, 0]] > 1.0 && fp16_weights[[0, 0]] < 2.0);

        // Large values should be clamped to FP16 range
        assert!(fp16_weights[[0, 1]] <= 65504.0);
        assert!(fp16_weights[[1, 0]] >= -65504.0);
    }

    #[test]
    fn test_bf16_simulation() {
        let trainer = MixedPrecisionTrainer::new(PrecisionMode::BF16, LossScaler::None);

        let weights =
            Array2::from_shape_vec((2, 2), vec![1.234_567, 100.5, -50.25, 0.125]).expect("unwrap");
        let bf16_weights = trainer.cast_to_working_precision(&weights);

        // Should have reduced mantissa precision
        assert_ne!(bf16_weights[[0, 0]], 1.234_567);
    }

    #[test]
    fn test_overflow_detection() {
        let mut trainer =
            MixedPrecisionTrainer::new(PrecisionMode::FP16, LossScaler::dynamic(1000.0, 2.0, 100));

        let mut gradients = HashMap::new();
        gradients.insert(
            "layer1".to_string(),
            Array2::from_shape_vec((2, 2), vec![f32::INFINITY, 1.0, 2.0, 3.0]).expect("unwrap"),
        );

        let (should_step, overflow) = trainer
            .unscale_and_check_gradients(&mut gradients)
            .expect("unwrap");

        assert!(!should_step);
        assert!(overflow);
        assert_eq!(trainer.stats().overflow_steps, 1);
    }

    #[test]
    fn test_gradient_scaler() {
        let scaler = GradientScaler::new(true);

        let loss = 1.0;
        let scaled = scaler.scale(loss);
        assert!(scaled > loss); // Should be scaled

        let mut grads = Array2::from_shape_vec((2, 2), vec![1000.0; 4]).expect("unwrap");
        scaler.unscale(&mut grads);
        assert!(grads[[0, 0]] < 1000.0); // Should be unscaled
    }

    #[test]
    fn test_autocast_context() {
        let ctx = AutocastContext::new(true, PrecisionMode::FP16);
        assert!(ctx.is_enabled());
        assert_eq!(ctx.mode(), PrecisionMode::FP16);

        let tensor = Array2::from_shape_vec((2, 2), vec![1.234_567; 4]).expect("unwrap");
        let casted = ctx.cast(&tensor);

        // Should have reduced precision
        assert_ne!(casted[[0, 0]], 1.234_567);
    }

    #[test]
    fn test_autocast_disabled() {
        let ctx = AutocastContext::new(false, PrecisionMode::FP16);
        assert!(!ctx.is_enabled());

        let tensor = Array2::from_shape_vec((2, 2), vec![1.234_567; 4]).expect("unwrap");
        let casted = ctx.cast(&tensor);

        // Should be unchanged
        assert_eq!(casted, tensor);
    }

    #[test]
    fn test_master_weights_update() {
        let mut trainer = MixedPrecisionTrainer::new(PrecisionMode::FP16, LossScaler::None);

        let weights = Array2::from_shape_vec((2, 2), vec![1.0, 2.0, 3.0, 4.0]).expect("unwrap");
        trainer.register_weights("layer1".to_string(), weights.clone());

        let mut updates = HashMap::new();
        updates.insert(
            "layer1".to_string(),
            Array2::from_shape_vec((2, 2), vec![0.1, 0.1, 0.1, 0.1]).expect("unwrap"),
        );

        trainer.update_master_weights(&updates);

        let master = &trainer.master_weights["layer1"];
        assert_relative_eq!(master[[0, 0]], 1.1, epsilon = 1e-6);
    }

    #[test]
    fn test_mixed_precision_stats() {
        let stats = MixedPrecisionStats {
            total_steps: 100,
            overflow_steps: 5,
            successful_steps: 95,
        };

        assert_eq!(stats.overflow_rate(), 0.05);
        assert_eq!(stats.success_rate(), 0.95);
    }

    #[test]
    fn test_loss_scaler_growth() {
        let mut scaler = LossScaler::dynamic(1000.0, 2.0, 2);

        // First successful step
        assert!(scaler.update(false));
        assert_eq!(scaler.get_scale(), 1000.0);

        // Second successful step - should trigger growth
        assert!(scaler.update(false));
        assert_eq!(scaler.get_scale(), 2000.0);
    }
}