trustformers-core 0.1.1

Core traits and utilities for TrustformeRS
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
//! Mixed precision training utilities (AMP-style).
//!
//! Maintains master weights in FP32, computes forward/backward passes in
//! BF16 or FP16 to reduce memory footprint and increase throughput on modern
//! accelerators.

use std::fmt;

// ---------------------------------------------------------------------------
// Precision mode
// ---------------------------------------------------------------------------

/// Selects the numeric format used for compute operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TrainingPrecisionMode {
    /// Pure FP32 — no mixed precision.
    Fp32,
    /// FP16 for compute, FP32 master weights.  Uses a dynamic loss scaler.
    Fp16,
    /// BF16 for compute, FP32 master weights.  No loss scaler required.
    Bf16,
    /// Experimental 8-bit float (E4M3 variant, similar to MX FP8).
    Fp8E4M3,
}

impl fmt::Display for TrainingPrecisionMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TrainingPrecisionMode::Fp32 => write!(f, "FP32"),
            TrainingPrecisionMode::Fp16 => write!(f, "FP16"),
            TrainingPrecisionMode::Bf16 => write!(f, "BF16"),
            TrainingPrecisionMode::Fp8E4M3 => write!(f, "FP8-E4M3"),
        }
    }
}

// ---------------------------------------------------------------------------
// BFloat16
// ---------------------------------------------------------------------------

/// Pure-Rust Brain Float 16 representation.
///
/// BF16 shares the same exponent as FP32 (8 bits) but reduces the mantissa
/// from 23 bits to 7 bits — it is literally the upper 16 bits of an FP32
/// word.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BFloat16 {
    /// Raw IEEE 754 BF16 bit pattern stored as `u16`.
    pub bits: u16,
}

impl BFloat16 {
    /// Convert an `f32` value to `BFloat16`.
    ///
    /// Uses round-to-nearest-even (the same rounding mode mandated by IEEE
    /// 754-2008) and correctly propagates NaN and infinity.
    pub fn from_f32(val: f32) -> Self {
        let bits = val.to_bits();

        // Propagate NaN — preserve the quiet-NaN bit.
        if val.is_nan() {
            // Set the quiet-NaN bit (bit 22 in FP32 mantissa = bit 6 in BF16).
            return BFloat16 {
                bits: ((bits >> 16) as u16) | 0x0040,
            };
        }

        // Round to nearest even.
        // The rounding bit is bit 15 of the FP32 representation.
        let lsb = (bits >> 16) & 1; // least significant bit of BF16
        let rounding_bias = 0x7FFF_u32 + lsb; // round-to-nearest-even bias

        let rounded = bits.wrapping_add(rounding_bias);
        BFloat16 {
            bits: (rounded >> 16) as u16,
        }
    }

    /// Convert this `BFloat16` back to `f32`.
    pub fn to_f32(self) -> f32 {
        // BF16 occupies the upper 16 bits of FP32; just zero-extend.
        f32::from_bits((self.bits as u32) << 16)
    }

    /// Returns `true` if this value is NaN.
    pub fn is_nan(self) -> bool {
        // Exponent all-ones (0x7F80 mask) AND non-zero mantissa.
        (self.bits & 0x7FFF) > 0x7F80
    }

    /// Returns `true` if this value is positive or negative infinity.
    pub fn is_infinite(self) -> bool {
        (self.bits & 0x7FFF) == 0x7F80
    }

    /// Returns `true` if this value is positive or negative zero.
    pub fn is_zero(self) -> bool {
        (self.bits & 0x7FFF) == 0
    }
}

impl fmt::Display for BFloat16 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_f32())
    }
}

// ---------------------------------------------------------------------------
// Batch conversion helpers
// ---------------------------------------------------------------------------

/// Convert a slice of `f32` values to BF16 bit patterns (`u16`).
pub fn cast_fp32_to_bf16(fp32_slice: &[f32]) -> Vec<u16> {
    fp32_slice
        .iter()
        .map(|&v| BFloat16::from_f32(v).bits)
        .collect()
}

/// Convert a slice of BF16 bit patterns back to `f32`.
pub fn cast_bf16_to_fp32(bf16_slice: &[u16]) -> Vec<f32> {
    bf16_slice
        .iter()
        .map(|&bits| BFloat16 { bits }.to_f32())
        .collect()
}

/// Convert a slice of `f32` values to IEEE 754 FP16 bit patterns (`u16`).
///
/// Uses round-to-nearest-even and correctly handles overflow (clamps to ±Inf),
/// underflow (flush to zero for subnormals smaller than the representable
/// range), NaN, and infinity.
pub fn cast_fp32_to_fp16(fp32_slice: &[f32]) -> Vec<u16> {
    fp32_slice.iter().map(|&v| fp32_to_fp16_bits(v)).collect()
}

/// Convert a slice of IEEE 754 FP16 bit patterns to `f32`.
pub fn cast_fp16_to_fp32(fp16_slice: &[u16]) -> Vec<f32> {
    fp16_slice.iter().map(|&bits| fp16_bits_to_fp32(bits)).collect()
}

// ---------------------------------------------------------------------------
// Internal FP16 conversion (single value)
// ---------------------------------------------------------------------------

/// Convert one `f32` to the IEEE 754 FP16 bit representation.
fn fp32_to_fp16_bits(val: f32) -> u16 {
    let bits = val.to_bits();
    let sign = (bits >> 31) as u16;
    let exp = ((bits >> 23) & 0xFF) as i32;
    let mantissa = bits & 0x007F_FFFF;

    if val.is_nan() {
        // Quiet NaN.
        return (sign << 15) | 0x7E00;
    }
    if val.is_infinite() {
        return (sign << 15) | 0x7C00;
    }

    // FP32 bias = 127; FP16 bias = 15.
    let new_exp = exp - 127 + 15;

    if new_exp >= 31 {
        // Overflow → infinity.
        return (sign << 15) | 0x7C00;
    }

    if new_exp <= 0 {
        // Underflow — try to represent as subnormal or flush to zero.
        if new_exp < -10 {
            return sign << 15; // too small even for subnormal
        }
        // Subnormal: mantissa with implicit leading 1 shifted right.
        let shift = (1 - new_exp) as u32; // 1..=10
        let mant_with_hidden = (mantissa | 0x0080_0000) >> shift;
        let round_bit = (mantissa | 0x0080_0000) >> (shift - 1) & 1;
        let fp16_mant = ((mant_with_hidden + round_bit) >> 13) as u16;
        return (sign << 15) | fp16_mant;
    }

    // Normal number — round mantissa from 23 bits to 10 bits.
    let fp16_mant = {
        let round_bit = (mantissa >> 12) & 1;
        let trunc = (mantissa >> 13) as u16;
        trunc + round_bit as u16
    };

    let fp16_exp = new_exp as u16;
    // Check for mantissa overflow after rounding.
    if fp16_mant >= 0x0400 {
        // Mantissa rounded up past 10 bits.
        let adjusted_exp = fp16_exp + 1;
        if adjusted_exp >= 31 {
            return (sign << 15) | 0x7C00; // overflow to infinity
        }
        return (sign << 15) | (adjusted_exp << 10);
    }

    (sign << 15) | (fp16_exp << 10) | fp16_mant
}

/// Convert one IEEE 754 FP16 bit pattern back to `f32`.
fn fp16_bits_to_fp32(bits: u16) -> f32 {
    let sign = (bits >> 15) as u32;
    let exp = ((bits >> 10) & 0x1F) as u32;
    let mantissa = (bits & 0x03FF) as u32;

    if exp == 0x1F {
        // NaN or infinity.
        let fp32_bits = (sign << 31) | 0x7F80_0000 | (mantissa << 13);
        return f32::from_bits(fp32_bits);
    }

    if exp == 0 {
        if mantissa == 0 {
            return f32::from_bits(sign << 31); // ±0
        }
        // Subnormal FP16 → normal FP32.
        let mant = mantissa << 1;
        // Normalize: find leading 1.
        let mut m = mant;
        let mut e = 0_u32;
        while m & 0x0400 == 0 {
            m <<= 1;
            e += 1;
        }
        m &= 0x03FF; // strip implicit leading 1
        let fp32_exp = 127 - 15 - e + 1;
        return f32::from_bits((sign << 31) | (fp32_exp << 23) | (m << 13));
    }

    // Normal.
    let fp32_exp = exp + 127 - 15;
    f32::from_bits((sign << 31) | (fp32_exp << 23) | (mantissa << 13))
}

// ---------------------------------------------------------------------------
// LossScaler (dynamic loss scaling for FP16)
// ---------------------------------------------------------------------------

/// Dynamic loss scaler for FP16 mixed-precision training.
///
/// Multiplies the loss by a large scalar before the backward pass so that
/// small gradients are not flushed to zero in FP16.  The scale is increased
/// after a run of successful steps and decreased after overflows (NaN/Inf in
/// the unscaled gradients).
#[derive(Debug, Clone)]
pub struct LossScaler {
    /// Current loss scale factor.
    pub scale: f32,
    /// Factor by which the scale is multiplied on success.
    pub growth_factor: f32,
    /// Factor by which the scale is multiplied on overflow.
    pub backoff_factor: f32,
    /// Number of consecutive overflow-free steps required before scaling up.
    pub growth_interval: usize,
    /// Steps since the last scale increase.
    pub steps_since_increase: usize,
    /// Number of consecutive overflows observed.
    pub consecutive_overflows: usize,
}

impl LossScaler {
    /// Construct a `LossScaler` with standard default hyper-parameters:
    /// initial scale 2¹⁶, growth factor 2.0, backoff 0.5, interval 2000.
    pub fn new() -> Self {
        Self {
            scale: 65536.0, // 2^16
            growth_factor: 2.0,
            backoff_factor: 0.5,
            growth_interval: 2000,
            steps_since_increase: 0,
            consecutive_overflows: 0,
        }
    }

    /// Multiply the loss by the current scale before the backward pass.
    pub fn scale_loss(&self, loss: f32) -> f32 {
        loss * self.scale
    }

    /// Divide all gradients by the current scale to undo the loss scaling.
    pub fn unscale_gradients(&self, grads: &mut [Vec<f32>]) {
        let inv = 1.0 / self.scale;
        for param_grads in grads.iter_mut() {
            for g in param_grads.iter_mut() {
                *g *= inv;
            }
        }
    }

    /// Check whether *any* gradient contains a NaN or Inf value.
    ///
    /// Returns `true` if all gradients are finite (i.e., the step is safe).
    pub fn check_gradients(&self, grads: &[Vec<f32>]) -> bool {
        grads
            .iter()
            .flat_map(|g| g.iter())
            .all(|v| v.is_finite())
    }

    /// Update the scale after one optimizer step.
    ///
    /// * If `overflow` is `true`: reduce scale, reset counter.
    /// * If `overflow` is `false`: increment counter; when it reaches
    ///   `growth_interval`, multiply scale by `growth_factor`.
    pub fn update(&mut self, overflow: bool) {
        if overflow {
            self.scale *= self.backoff_factor;
            self.steps_since_increase = 0;
            self.consecutive_overflows += 1;
        } else {
            self.consecutive_overflows = 0;
            self.steps_since_increase += 1;
            if self.steps_since_increase >= self.growth_interval {
                self.scale *= self.growth_factor;
                self.steps_since_increase = 0;
            }
        }
        // Clamp scale to avoid degenerate values.
        if self.scale < 1.0 {
            self.scale = 1.0;
        }
    }

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

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

// ---------------------------------------------------------------------------
// AmpStats
// ---------------------------------------------------------------------------

/// Accumulated statistics for mixed-precision training.
#[derive(Debug, Clone, Default)]
pub struct AmpStats {
    /// Number of optimizer steps that were skipped due to NaN/Inf gradients.
    pub overflow_count: u64,
    /// Number of optimizer steps that completed successfully.
    pub successful_steps: u64,
    /// Most recently observed loss scale.
    pub current_loss_scale: f32,
}

impl AmpStats {
    /// Record one optimizer step outcome.
    pub fn update(&mut self, overflow: bool, scale: f32) {
        if overflow {
            self.overflow_count += 1;
        } else {
            self.successful_steps += 1;
        }
        self.current_loss_scale = scale;
    }
}

// ---------------------------------------------------------------------------
// MixedPrecisionContext
// ---------------------------------------------------------------------------

/// Container that manages master weights, optional loss scaling, and weight
/// casting for a mixed-precision training session.
pub struct MixedPrecisionContext {
    /// Which precision format is used for compute.
    pub mode: TrainingPrecisionMode,
    /// Dynamic loss scaler — populated only for FP16 mode.
    pub loss_scaler: Option<LossScaler>,
    /// FP32 master copies of all parameter tensors.
    pub master_weights: Vec<Vec<f32>>,
}

impl MixedPrecisionContext {
    /// Create a new context.
    ///
    /// `param_shapes` is a slice where each element is the number of scalar
    /// parameters in that tensor.  Master weights are initialized to zero.
    pub fn new(mode: TrainingPrecisionMode, param_shapes: &[usize]) -> Self {
        let loss_scaler = if mode == TrainingPrecisionMode::Fp16 {
            Some(LossScaler::new())
        } else {
            None
        };
        let master_weights = param_shapes.iter().map(|&n| vec![0.0_f32; n]).collect();
        Self {
            mode,
            loss_scaler,
            master_weights,
        }
    }

    /// Return the master weights cast to the compute precision (BF16 or FP16),
    /// encoded as `Vec<u16>` bit patterns.
    ///
    /// For `Fp32` and `Fp8E4M3`, returns the FP32 weights re-interpreted as
    /// `u16` pairs (the caller is expected to handle these modes appropriately;
    /// in practice `Fp32` training should not call this method for the purpose
    /// of obtaining low-precision weights).
    pub fn get_compute_weights(&self) -> Vec<Vec<u16>> {
        match self.mode {
            TrainingPrecisionMode::Bf16 => self
                .master_weights
                .iter()
                .map(|w| cast_fp32_to_bf16(w))
                .collect(),
            TrainingPrecisionMode::Fp16 => self
                .master_weights
                .iter()
                .map(|w| cast_fp32_to_fp16(w))
                .collect(),
            TrainingPrecisionMode::Fp32 | TrainingPrecisionMode::Fp8E4M3 => {
                // For FP32 mode, return the raw f32 bits split into u16 pairs
                // (high word first) so that the shape is preserved.
                self.master_weights
                    .iter()
                    .map(|w| {
                        w.iter()
                            .flat_map(|&v| {
                                let b = v.to_bits();
                                [(b >> 16) as u16, b as u16]
                            })
                            .collect()
                    })
                    .collect()
            }
        }
    }

    /// Apply a vanilla SGD update to the master weights.
    ///
    /// `grads` must already be unscaled (i.e., in FP32 magnitude, not scaled
    /// by the loss scaler).  Each grad slice must have the same length as the
    /// corresponding master-weight slice.
    pub fn update_master_weights(&mut self, grads: &[Vec<f32>], learning_rate: f32) {
        for (weights, g) in self.master_weights.iter_mut().zip(grads.iter()) {
            for (w, dw) in weights.iter_mut().zip(g.iter()) {
                *w -= learning_rate * dw;
            }
        }
    }

    /// Return `true` if *any* gradient contains NaN or Inf.
    ///
    /// Delegates to `LossScaler::check_gradients` when a scaler is present,
    /// otherwise checks directly.
    pub fn check_overflow(&self, grads: &[Vec<f32>]) -> bool {
        let all_finite = grads
            .iter()
            .flat_map(|g| g.iter())
            .all(|v| v.is_finite());
        !all_finite
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // ------------------------------------------------------------------
    // 1. BF16 from_f32 → to_f32 round-trip (small precision error allowed)
    // ------------------------------------------------------------------
    #[test]
    fn test_bf16_round_trip() {
        let values: &[f32] = &[1.0, -1.0, 0.5, 3.14, 100.0, -0.001, 1024.0];
        for &v in values {
            let bf = BFloat16::from_f32(v);
            let back = bf.to_f32();
            // BF16 has ~7 bits of mantissa precision; relative error < 1%.
            let rel_err = ((back - v) / v).abs();
            assert!(
                rel_err < 0.01,
                "round-trip failed for {v}: got {back}, rel_err={rel_err}"
            );
        }
    }

    // ------------------------------------------------------------------
    // 2. BF16 NaN and Inf handling
    // ------------------------------------------------------------------
    #[test]
    fn test_bf16_special_values() {
        let nan = BFloat16::from_f32(f32::NAN);
        assert!(nan.is_nan(), "BF16 NaN should be NaN");

        let pos_inf = BFloat16::from_f32(f32::INFINITY);
        assert!(pos_inf.is_infinite(), "BF16 +Inf should be infinite");
        assert!(!pos_inf.is_nan());

        let neg_inf = BFloat16::from_f32(f32::NEG_INFINITY);
        assert!(neg_inf.is_infinite(), "BF16 -Inf should be infinite");

        let zero = BFloat16::from_f32(0.0);
        assert!(zero.is_zero(), "BF16 0.0 should be zero");
    }

    // ------------------------------------------------------------------
    // 3. FP32 → BF16 → FP32 batch conversion
    // ------------------------------------------------------------------
    #[test]
    fn test_fp32_bf16_fp32_batch_conversion() {
        let original: Vec<f32> = vec![1.0, 2.0, -3.5, 0.125, 1000.0];
        let bf16_bits = cast_fp32_to_bf16(&original);
        assert_eq!(bf16_bits.len(), original.len());
        let recovered = cast_bf16_to_fp32(&bf16_bits);
        for (o, r) in original.iter().zip(recovered.iter()) {
            let rel = ((r - o) / o).abs();
            assert!(rel < 0.02, "batch bf16 round-trip: {o} → {r}");
        }
    }

    // ------------------------------------------------------------------
    // 4. Loss scaling (scale_loss multiplies by current scale)
    // ------------------------------------------------------------------
    #[test]
    fn test_loss_scaling_multiplies() {
        let scaler = LossScaler::new();
        let loss = 0.5_f32;
        let scaled = scaler.scale_loss(loss);
        let expected = loss * scaler.current_scale();
        assert!(
            (scaled - expected).abs() < 1e-3,
            "scale_loss should multiply by scale"
        );
        assert!(scaled > loss, "scaled loss should be larger than original");
    }

    // ------------------------------------------------------------------
    // 5. Unscale divides gradients by the current scale
    // ------------------------------------------------------------------
    #[test]
    fn test_unscale_divides() {
        let scaler = LossScaler::new();
        let scale = scaler.current_scale();
        let mut grads = vec![vec![scale * 2.0_f32, scale * 4.0_f32]];
        scaler.unscale_gradients(&mut grads);
        assert!((grads[0][0] - 2.0).abs() < 1e-4, "unscaled[0] should be 2.0");
        assert!((grads[0][1] - 4.0).abs() < 1e-4, "unscaled[1] should be 4.0");
    }

    // ------------------------------------------------------------------
    // 6. Gradient overflow detection (Inf/NaN triggers overflow)
    // ------------------------------------------------------------------
    #[test]
    fn test_gradient_overflow_detection() {
        let scaler = LossScaler::new();

        let clean_grads = vec![vec![1.0_f32, 2.0, -3.0]];
        assert!(
            scaler.check_gradients(&clean_grads),
            "clean grads should not overflow"
        );

        let nan_grads = vec![vec![1.0_f32, f32::NAN]];
        assert!(
            !scaler.check_gradients(&nan_grads),
            "NaN grad should trigger overflow"
        );

        let inf_grads = vec![vec![f32::INFINITY, 1.0_f32]];
        assert!(
            !scaler.check_gradients(&inf_grads),
            "Inf grad should trigger overflow"
        );
    }

    // ------------------------------------------------------------------
    // 7. Loss scale decreases on overflow
    // ------------------------------------------------------------------
    #[test]
    fn test_loss_scale_decreases_on_overflow() {
        let mut scaler = LossScaler::new();
        let initial = scaler.current_scale();
        scaler.update(true); // overflow
        assert!(
            scaler.current_scale() < initial,
            "scale should decrease on overflow: {} → {}",
            initial,
            scaler.current_scale()
        );
        assert_eq!(
            scaler.current_scale(),
            initial * scaler.backoff_factor,
            "scale should be initial * backoff_factor"
        );
    }

    // ------------------------------------------------------------------
    // 8. Loss scale grows after growth_interval successful steps
    // ------------------------------------------------------------------
    #[test]
    fn test_loss_scale_grows_after_interval() {
        let mut scaler = LossScaler::new();
        // Shrink the interval so the test is fast.
        scaler.growth_interval = 5;
        let initial = scaler.current_scale();

        for _ in 0..5 {
            scaler.update(false); // no overflow
        }
        assert!(
            scaler.current_scale() > initial,
            "scale should grow after {} successful steps",
            scaler.growth_interval
        );
        assert_eq!(
            scaler.current_scale(),
            initial * scaler.growth_factor,
            "scale should be initial * growth_factor"
        );
    }

    // ------------------------------------------------------------------
    // 9. MixedPrecisionContext compute weights have the right shapes
    // ------------------------------------------------------------------
    #[test]
    fn test_mixed_precision_context_compute_weights_shape() {
        let param_shapes = &[4_usize, 6, 2];
        let ctx = MixedPrecisionContext::new(TrainingPrecisionMode::Bf16, param_shapes);
        let compute = ctx.get_compute_weights();
        assert_eq!(compute.len(), param_shapes.len());
        for (i, &n) in param_shapes.iter().enumerate() {
            assert_eq!(
                compute[i].len(),
                n,
                "compute weights for param {i} should have {n} elements"
            );
        }
    }

    // ------------------------------------------------------------------
    // 10. Master weight SGD update modifies weights correctly
    // ------------------------------------------------------------------
    #[test]
    fn test_master_weight_sgd_update() {
        let param_shapes = &[3_usize];
        let mut ctx = MixedPrecisionContext::new(TrainingPrecisionMode::Fp32, param_shapes);
        // Set initial weights.
        ctx.master_weights[0] = vec![1.0_f32, 2.0, 3.0];

        let grads = vec![vec![0.1_f32, 0.2, 0.3]];
        let lr = 0.1_f32;
        ctx.update_master_weights(&grads, lr);

        // Expected: w - lr * g.
        let expected = vec![1.0 - 0.01, 2.0 - 0.02, 3.0 - 0.03];
        for (w, e) in ctx.master_weights[0].iter().zip(expected.iter()) {
            assert!(
                (w - e).abs() < 1e-6,
                "SGD update: expected {e}, got {w}"
            );
        }
    }

    // ------------------------------------------------------------------
    // Bonus: check_overflow returns true for NaN/Inf
    // ------------------------------------------------------------------
    #[test]
    fn test_context_check_overflow() {
        let ctx = MixedPrecisionContext::new(TrainingPrecisionMode::Fp16, &[2_usize]);
        let clean = vec![vec![0.1_f32, -0.2]];
        assert!(!ctx.check_overflow(&clean), "no overflow for clean grads");

        let inf_grads = vec![vec![f32::INFINITY, 0.5]];
        assert!(ctx.check_overflow(&inf_grads), "overflow for Inf grad");
    }

    // ------------------------------------------------------------------
    // Bonus: AmpStats update works correctly
    // ------------------------------------------------------------------
    #[test]
    fn test_amp_stats_update() {
        let mut stats = AmpStats::default();
        stats.update(false, 65536.0);
        assert_eq!(stats.successful_steps, 1);
        assert_eq!(stats.overflow_count, 0);
        assert_eq!(stats.current_loss_scale, 65536.0);

        stats.update(true, 32768.0);
        assert_eq!(stats.overflow_count, 1);
        assert_eq!(stats.successful_steps, 1);
        assert_eq!(stats.current_loss_scale, 32768.0);
    }

    // ------------------------------------------------------------------
    // Bonus: FP16 round-trip for normal values
    // ------------------------------------------------------------------
    #[test]
    fn test_fp16_round_trip() {
        let values: &[f32] = &[1.0, -1.0, 0.5, 2.0, 4.0];
        for &v in values {
            let bits = cast_fp32_to_fp16(&[v]);
            let back = cast_fp16_to_fp32(&bits);
            // FP16 has ~10 bits of mantissa; relative error < 0.2%.
            let rel = ((back[0] - v) / v).abs();
            assert!(
                rel < 0.002,
                "fp16 round-trip for {v}: got {}, rel={rel}",
                back[0]
            );
        }
    }
}