scirs2-linalg 0.4.2

Linear algebra module for SciRS2 (scirs2-linalg)
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
//! Element-wise operations and reductions in mixed precision
//!
//! Provides dot products, norms, AXPY, element-wise arithmetic, scaling,
//! and a dynamic loss scaler for mixed-precision training workflows.

use scirs2_core::ndarray::Array1;

use super::types::{BF16, F16};

// ============================================================================
// Dot products (accumulate in f32)
// ============================================================================

/// Compute the dot product of two F16 vectors, accumulating in f32.
///
/// Returns `0.0` when the vectors have different lengths (caller should
/// validate beforehand if this matters).
pub fn dot_f16(a: &Array1<F16>, b: &Array1<F16>) -> f32 {
    if a.len() != b.len() {
        return 0.0;
    }
    let mut acc: f32 = 0.0;
    for (av, bv) in a.iter().zip(b.iter()) {
        acc += av.to_f32() * bv.to_f32();
    }
    acc
}

/// Compute the dot product of two BF16 vectors, accumulating in f32.
pub fn dot_bf16(a: &Array1<BF16>, b: &Array1<BF16>) -> f32 {
    if a.len() != b.len() {
        return 0.0;
    }
    let mut acc: f32 = 0.0;
    for (av, bv) in a.iter().zip(b.iter()) {
        acc += av.to_f32() * bv.to_f32();
    }
    acc
}

// ============================================================================
// Norms
// ============================================================================

/// Compute the L2 (Euclidean) norm of an F16 vector, accumulating in f32.
pub fn norm_f16(a: &Array1<F16>) -> f32 {
    let mut acc: f32 = 0.0;
    for v in a.iter() {
        let f = v.to_f32();
        acc += f * f;
    }
    acc.sqrt()
}

/// Compute the L2 (Euclidean) norm of a BF16 vector, accumulating in f32.
pub fn norm_bf16(a: &Array1<BF16>) -> f32 {
    let mut acc: f32 = 0.0;
    for v in a.iter() {
        let f = v.to_f32();
        acc += f * f;
    }
    acc.sqrt()
}

// ============================================================================
// AXPY: y += alpha * x
// ============================================================================

/// Compute y += alpha * x where x is F16 and y is f32.
///
/// This is the classic BLAS Level-1 AXPY operation in mixed precision.
/// The F16 input `x` is promoted to f32 for the multiply-add.
pub fn axpy_f16(alpha: f32, x: &Array1<F16>, y: &mut Array1<f32>) {
    let n = x.len().min(y.len());
    for i in 0..n {
        y[i] += alpha * x[i].to_f32();
    }
}

/// Compute y += alpha * x where x is BF16 and y is f32.
pub fn axpy_bf16(alpha: f32, x: &Array1<BF16>, y: &mut Array1<f32>) {
    let n = x.len().min(y.len());
    for i in 0..n {
        y[i] += alpha * x[i].to_f32();
    }
}

// ============================================================================
// Element-wise arithmetic
// ============================================================================

/// Element-wise addition of two F16 arrays. Each pair is promoted to f32,
/// added, then converted back to F16.
pub fn elementwise_add_f16(a: &Array1<F16>, b: &Array1<F16>) -> Array1<F16> {
    let n = a.len().min(b.len());
    Array1::from_iter((0..n).map(|i| F16::from_f32(a[i].to_f32() + b[i].to_f32())))
}

/// Element-wise addition of two BF16 arrays.
pub fn elementwise_add_bf16(a: &Array1<BF16>, b: &Array1<BF16>) -> Array1<BF16> {
    let n = a.len().min(b.len());
    Array1::from_iter((0..n).map(|i| BF16::from_f32(a[i].to_f32() + b[i].to_f32())))
}

/// Element-wise multiplication of two F16 arrays.
pub fn elementwise_mul_f16(a: &Array1<F16>, b: &Array1<F16>) -> Array1<F16> {
    let n = a.len().min(b.len());
    Array1::from_iter((0..n).map(|i| F16::from_f32(a[i].to_f32() * b[i].to_f32())))
}

/// Element-wise multiplication of two BF16 arrays.
pub fn elementwise_mul_bf16(a: &Array1<BF16>, b: &Array1<BF16>) -> Array1<BF16> {
    let n = a.len().min(b.len());
    Array1::from_iter((0..n).map(|i| BF16::from_f32(a[i].to_f32() * b[i].to_f32())))
}

// ============================================================================
// Scaling
// ============================================================================

/// Scale an F16 array by a f32 scalar, returning a new F16 array.
pub fn scale_f16(alpha: f32, a: &Array1<F16>) -> Array1<F16> {
    Array1::from_iter(a.iter().map(|v| F16::from_f32(alpha * v.to_f32())))
}

/// Scale a BF16 array by a f32 scalar, returning a new BF16 array.
pub fn scale_bf16(alpha: f32, a: &Array1<BF16>) -> Array1<BF16> {
    Array1::from_iter(a.iter().map(|v| BF16::from_f32(alpha * v.to_f32())))
}

// ============================================================================
// Loss Scaler for mixed-precision training
// ============================================================================

/// Dynamic loss scaler for mixed-precision training.
///
/// Maintains a scaling factor that is applied to the loss before
/// backward pass to prevent gradient underflow in half-precision.
/// The scale grows geometrically after consecutive successful steps
/// and decreases when infinity/NaN is detected in gradients.
///
/// # Example
/// ```rust
/// use scirs2_linalg::mixed_precision::operations::LossScaler;
///
/// let mut scaler = LossScaler::new();
/// let loss = 0.5;
/// let scaled_loss = scaler.scale_loss(loss);
/// // ... compute gradients from scaled_loss ...
/// // Check for inf/nan in gradients, then:
/// scaler.update(false); // no inf found
/// ```
pub struct LossScaler {
    /// Current scaling factor
    scale: f64,
    /// Multiplicative factor to grow the scale after successful steps
    growth_factor: f64,
    /// Multiplicative factor to shrink the scale when inf/nan detected
    backoff_factor: f64,
    /// Number of consecutive successful steps before growing the scale
    growth_interval: usize,
    /// Counter of consecutive successful steps since last backoff
    consecutive_ok: usize,
}

impl LossScaler {
    /// Create a new `LossScaler` with default parameters.
    ///
    /// Defaults:
    /// - Initial scale: 2^16 = 65536
    /// - Growth factor: 2.0
    /// - Backoff factor: 0.5
    /// - Growth interval: 2000 steps
    pub fn new() -> Self {
        Self {
            scale: 65536.0,
            growth_factor: 2.0,
            backoff_factor: 0.5,
            growth_interval: 2000,
            consecutive_ok: 0,
        }
    }

    /// Create a `LossScaler` with custom parameters.
    pub fn with_params(
        initial_scale: f64,
        growth_factor: f64,
        backoff_factor: f64,
        growth_interval: usize,
    ) -> Self {
        Self {
            scale: initial_scale,
            growth_factor,
            backoff_factor,
            growth_interval,
            consecutive_ok: 0,
        }
    }

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

    /// Scale the loss value by the current scale factor.
    pub fn scale_loss(&self, loss: f64) -> f64 {
        loss * self.scale
    }

    /// Unscale gradients by dividing by the current scale factor.
    ///
    /// This should be called after the backward pass to restore the
    /// original gradient magnitudes.
    pub fn unscale_gradients(&self, grads: &mut Array1<f64>) {
        if self.scale.abs() < f64::EPSILON {
            return;
        }
        let inv_scale = 1.0 / self.scale;
        grads.mapv_inplace(|v| v * inv_scale);
    }

    /// Update the scaler after a training step.
    ///
    /// - If `found_inf` is `true`: scale is reduced by `backoff_factor`
    ///   and the step counter resets.
    /// - If `found_inf` is `false`: increment the counter, and if
    ///   `growth_interval` consecutive successful steps have occurred,
    ///   grow the scale by `growth_factor`.
    pub fn update(&mut self, found_inf: bool) {
        if found_inf {
            self.scale *= self.backoff_factor;
            self.consecutive_ok = 0;
        } else {
            self.consecutive_ok += 1;
            if self.consecutive_ok >= self.growth_interval {
                self.scale *= self.growth_factor;
                self.consecutive_ok = 0;
            }
        }
    }
}

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

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_dot_f16() {
        let a = array![F16::from_f32(1.0), F16::from_f32(2.0), F16::from_f32(3.0)];
        let b = array![F16::from_f32(4.0), F16::from_f32(5.0), F16::from_f32(6.0)];
        let result = dot_f16(&a, &b);
        // 1*4 + 2*5 + 3*6 = 32
        assert!((result - 32.0).abs() < 0.5);
    }

    #[test]
    fn test_dot_bf16() {
        let a = array![
            BF16::from_f32(1.0),
            BF16::from_f32(2.0),
            BF16::from_f32(3.0)
        ];
        let b = array![
            BF16::from_f32(4.0),
            BF16::from_f32(5.0),
            BF16::from_f32(6.0)
        ];
        let result = dot_bf16(&a, &b);
        assert!((result - 32.0).abs() < 1.0);
    }

    #[test]
    fn test_dot_f16_vs_f64_reference() {
        // Compare dot product against f64 reference
        let n = 100;
        let a_f16: Array1<F16> = Array1::from_iter((1..=n).map(|i| F16::from_f32(i as f32 * 0.1)));
        let b_f16: Array1<F16> = Array1::from_iter((1..=n).map(|i| F16::from_f32(i as f32 * 0.01)));

        let f16_dot = dot_f16(&a_f16, &b_f16);

        // f64 reference using the same f16 values
        let ref_dot: f64 = a_f16
            .iter()
            .zip(b_f16.iter())
            .map(|(a, b)| a.to_f64() * b.to_f64())
            .sum();

        let rel_err = ((f16_dot as f64) - ref_dot).abs() / ref_dot.abs().max(1e-30);
        assert!(
            rel_err < 0.01,
            "dot product relative error {rel_err} exceeds tolerance"
        );
    }

    #[test]
    fn test_norm_f16() {
        let a = array![F16::from_f32(3.0), F16::from_f32(4.0)];
        let n = norm_f16(&a);
        assert!((n - 5.0).abs() < 0.1);
    }

    #[test]
    fn test_norm_bf16() {
        let a = array![BF16::from_f32(3.0), BF16::from_f32(4.0)];
        let n = norm_bf16(&a);
        assert!((n - 5.0).abs() < 0.5);
    }

    #[test]
    fn test_axpy_f16() {
        let x = array![F16::from_f32(1.0), F16::from_f32(2.0), F16::from_f32(3.0)];
        let mut y = array![10.0f32, 20.0, 30.0];
        axpy_f16(2.0, &x, &mut y);
        // y = [10+2, 20+4, 30+6] = [12, 24, 36]
        assert!((y[0] - 12.0).abs() < 0.1);
        assert!((y[1] - 24.0).abs() < 0.1);
        assert!((y[2] - 36.0).abs() < 0.1);
    }

    #[test]
    fn test_elementwise_add_f16() {
        let a = array![F16::from_f32(1.0), F16::from_f32(2.0)];
        let b = array![F16::from_f32(3.0), F16::from_f32(4.0)];
        let c = elementwise_add_f16(&a, &b);
        assert!((c[0].to_f32() - 4.0).abs() < 0.01);
        assert!((c[1].to_f32() - 6.0).abs() < 0.01);
    }

    #[test]
    fn test_elementwise_mul_f16() {
        let a = array![F16::from_f32(2.0), F16::from_f32(3.0)];
        let b = array![F16::from_f32(4.0), F16::from_f32(5.0)];
        let c = elementwise_mul_f16(&a, &b);
        assert!((c[0].to_f32() - 8.0).abs() < 0.1);
        assert!((c[1].to_f32() - 15.0).abs() < 0.1);
    }

    #[test]
    fn test_scale_f16() {
        let a = array![F16::from_f32(1.0), F16::from_f32(2.0), F16::from_f32(4.0)];
        let scaled = scale_f16(3.0, &a);
        assert!((scaled[0].to_f32() - 3.0).abs() < 0.1);
        assert!((scaled[1].to_f32() - 6.0).abs() < 0.1);
        assert!((scaled[2].to_f32() - 12.0).abs() < 0.1);
    }

    #[test]
    fn test_loss_scaler_basic() {
        let scaler = LossScaler::new();
        assert_eq!(scaler.current_scale(), 65536.0);
        let scaled = scaler.scale_loss(1.0);
        assert_eq!(scaled, 65536.0);
    }

    #[test]
    fn test_loss_scaler_unscale() {
        let scaler = LossScaler::new();
        let mut grads = array![65536.0, 131072.0];
        scaler.unscale_gradients(&mut grads);
        assert!((grads[0] - 1.0).abs() < 1e-10);
        assert!((grads[1] - 2.0).abs() < 1e-10);
    }

    #[test]
    fn test_loss_scaler_backoff_on_inf() {
        let mut scaler = LossScaler::new();
        let initial = scaler.current_scale();
        scaler.update(true); // found inf
        assert!((scaler.current_scale() - initial * 0.5).abs() < 1e-10);
    }

    #[test]
    fn test_loss_scaler_growth() {
        let mut scaler = LossScaler::with_params(1.0, 2.0, 0.5, 3);
        // 3 successful steps -> scale should double
        scaler.update(false);
        scaler.update(false);
        assert!((scaler.current_scale() - 1.0).abs() < 1e-10); // not yet
        scaler.update(false);
        assert!((scaler.current_scale() - 2.0).abs() < 1e-10); // now doubled
    }

    #[test]
    fn test_loss_scaler_reset_on_inf() {
        let mut scaler = LossScaler::with_params(4.0, 2.0, 0.5, 3);
        scaler.update(false);
        scaler.update(false);
        // 2 successful steps, then inf resets counter
        scaler.update(true);
        assert!((scaler.current_scale() - 2.0).abs() < 1e-10); // 4 * 0.5
                                                               // Need 3 more successful steps to grow
        scaler.update(false);
        scaler.update(false);
        scaler.update(false);
        assert!((scaler.current_scale() - 4.0).abs() < 1e-10); // 2 * 2
    }

    #[test]
    fn test_elementwise_add_bf16() {
        let a = array![BF16::from_f32(10.0), BF16::from_f32(-5.0)];
        let b = array![BF16::from_f32(20.0), BF16::from_f32(5.0)];
        let c = elementwise_add_bf16(&a, &b);
        assert!((c[0].to_f32() - 30.0).abs() < 1.0);
        assert!((c[1].to_f32() - 0.0).abs() < 1.0);
    }

    #[test]
    fn test_scale_bf16() {
        let a = array![BF16::from_f32(2.0), BF16::from_f32(4.0)];
        let scaled = scale_bf16(0.5, &a);
        assert!((scaled[0].to_f32() - 1.0).abs() < 0.1);
        assert!((scaled[1].to_f32() - 2.0).abs() < 0.1);
    }

    #[test]
    fn test_dot_length_mismatch() {
        let a = array![F16::from_f32(1.0), F16::from_f32(2.0)];
        let b = array![F16::from_f32(1.0)];
        assert_eq!(dot_f16(&a, &b), 0.0);
    }
}