coeus-ops 0.10.0

Numerical tensor operators for Coeus.
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
// ── Vector and matrix norm reductions ──
//
// `norm` is the L2 vector norm over all elements: `sqrt(Σ x²)`, matching
// `torch.linalg.vector_norm(x, ord=2)` (flattened). It is a performance-
// critical short-circuit over the general `norm_p` — no scalar-power kernel.
//
// `norm_p` / `norm_p_axis` support arbitrary finite `p > 0` through provider
// scalar-power dispatch. CPU execution uses Leto `PowfOp`; accelerators use
// the selected Hephaestus scalar-power kernel.
//
// `frobenius_norm` / `frobenius_norm_batched` compose on `mul + sum + sqrt`
// to match `torch.linalg.matrix_norm(A, ord='fro')` for 2-D and ≥3-D
// inputs respectively.

use crate::backend_ops::{BackendOps, ElementwiseOps, ReductionOps, ScalarPowerOps};
use crate::binary;
use coeus_core::Float;
use coeus_tensor::Tensor;

/// Euclidean (L2) norm over all elements: `sqrt(sum(x²))`.
///
/// Special case of [`norm_p`] with `p = 2`. Retained as the performance-
/// critical short-circuit when only L2 is needed; the general ord-p variant
/// dispatches scalar powers through the selected provider.
#[inline]
pub fn norm<T: Float, B: BackendOps<T> + Default>(
    a: &Tensor<T, B>,
    backend: &B,
) -> Result<T, B::Error> {
    let n = a.numel();
    if n == 0 {
        return Ok(T::from_usize(0));
    }
    let flattened = if a.is_contiguous() && a.layout().offset() == 0 {
        a.reshape([n])
    } else {
        a.to_contiguous_on(backend).reshape([n])
    };
    let sq = binary::mul(&flattened, &flattened, backend);
    Ok(<T as Float>::sqrt(super::sum(&sq, backend)?))
}
/// `L_p` norm over all elements: `(Σ|xᵢ|^p)^(1/p)` for finite `p > 0`.
///
/// Matches `torch.linalg.vector_norm(x, ord=p)` over a flattened view for
/// any `p` in `(0, ∞)`. The complete computation stays on the selected
/// provider; only the scalar-returning API copies its final one-element result
/// to the caller.
///
/// # Panics
/// Panics if `p <= 0`, `p` is not finite, or the input is empty.
#[inline]
pub fn norm_p_tensor<
    T: Float,
    B: ElementwiseOps<T> + ReductionOps<T> + ScalarPowerOps<T> + Default,
>(
    a: &Tensor<T, B>,
    p: T,
    backend: &B,
) -> Tensor<T, B> {
    let n = a.numel();
    assert!(n > 0, "norm_p: empty tensor has no norm");
    assert!(
        p > T::zero() && <T as Float>::is_finite(p),
        "norm_p: ord must be a finite positive number, got {p:?}"
    );
    let magnitudes = crate::abs(a, backend);
    let powered = crate::pow_scalar(&magnitudes, p, backend);
    let flattened = powered.reshape([n]);
    let summed = super::sum_axis(&flattened, 0, backend).expect("norm_p: provider sum");
    crate::pow_scalar(&summed, T::one() / p, backend)
}

/// `L_p` norm over all elements returned as a provider-resident `[1]` tensor.
///
/// The scalar [`norm_p`] API is a compatibility boundary that reads this one
/// output element. Tracked autograd uses this tensor form to retain the norm
/// on the selected provider for backward.
#[inline]
pub fn norm_p<T: Float, B: ElementwiseOps<T> + ReductionOps<T> + ScalarPowerOps<T> + Default>(
    a: &Tensor<T, B>,
    p: T,
    backend: &B,
) -> T {
    let result = norm_p_tensor(a, p, backend);
    let mut scalar = [T::zero()];
    backend.copy_to_host(result.storage(), &mut scalar);
    scalar[0]
}

/// Per-axis `L_p` norm: tensor reduced along `axis` to size 1, with each
/// slice evaluated as `(Σ|xᵢ|^p)^(1/p)` for finite `p > 0`.
///
/// Matches `torch.linalg.vector_norm(x, ord=p, dim=axis)` over a flattened
/// view of every `axis`-slice. The complete computation stays on the selected
/// provider and the reduced axis remains a size-one dimension.
///
/// Output shape is `input.shape` with `axis` reduced to size 1.
///
/// # Panics
/// Panics if `axis` is out of range, the axis has zero elements, `p <= 0`,
/// or `p` is not finite.
#[inline]
pub fn norm_p_axis<
    T: Float,
    B: ElementwiseOps<T> + ReductionOps<T> + ScalarPowerOps<T> + Default,
>(
    a: &Tensor<T, B>,
    p: T,
    axis: usize,
    backend: &B,
) -> Tensor<T, B> {
    assert!(axis < a.ndim(), "norm_p_axis: axis {axis} out of bounds");
    let n_axis = a.shape()[axis];
    assert!(n_axis > 0, "norm_p_axis: axis {axis} has zero elements");
    assert!(
        p > T::zero() && <T as Float>::is_finite(p),
        "norm_p_axis: ord must be a finite positive number, got {p:?}"
    );

    let magnitudes = crate::abs(a, backend);
    let powered = crate::pow_scalar(&magnitudes, p, backend);
    let summed = super::sum_axis(&powered, axis, backend).expect("norm_p_axis: provider sum");
    crate::pow_scalar(&summed, T::one() / p, backend)
}

/// Frobenius (matrix L2) norm over a single 2-D tensor: `sqrt(Σ aᵢⱼ²)`.
///
/// Matches `torch.linalg.matrix_norm(A, ord='fro')` for a 2-D input matrix.
/// Delegates to [`norm`] — no new `BinaryOp` opcodes required.
///
/// For ≥3-D tensors see [`frobenius_norm_batched`], which reduces over the
/// last two dimensions per batch.
#[inline]
pub fn frobenius_norm<T: Float, B: BackendOps<T> + Default>(
    a: &Tensor<T, B>,
    backend: &B,
) -> Result<T, B::Error> {
    norm(a, backend)
}

/// Per-batch Frobenius (matrix L2) norm: reduces over the last two
/// dimensions for every batch slot in the input.
///
/// Matches `torch.linalg.matrix_norm(A, ord='fro')` for inputs of rank
/// `≥ 2`. Leading batch dimensions are kept in the output.
///
/// # Semantics
/// - `ndim == 2`: returns a 0-D scalar Tensor (mirrors the scalar return of
///   [`norm`]). The binding at `coeus_python::matrix_norm` materialises this
///   to a Python `float`.
/// - `ndim >= 3`: returns a Tensor with shape `a.shape[..ndim-2]` holding
///   one Frobenius norm per batch slot.
///
/// # Panics
/// Panics if the input has rank < 2.
#[inline]
pub fn frobenius_norm_batched<T: Float, B: BackendOps<T> + Default>(
    a: &Tensor<T, B>,
    backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
    let ndim = a.ndim();
    assert!(
        ndim >= 2,
        "frobenius_norm_batched: tensor must have rank >= 2, got ndim={ndim}"
    );
    if ndim == 2 {
        let v = norm(a, backend)?;
        return Ok(Tensor::from_slice_on([], &[v], backend));
    }

    let contiguous = if a.is_contiguous() && a.layout().offset() == 0 {
        a.reshape(a.shape().to_vec())
    } else {
        a.to_contiguous_on(backend)
    };
    let n = contiguous.numel();
    let mut host = vec![T::zero(); n];
    backend.copy_to_host(contiguous.storage(), &mut host);

    let last_two: usize = contiguous.shape()[ndim - 1] * contiguous.shape()[ndim - 2];
    let pre: usize = n / last_two;
    let mut out_host = Vec::with_capacity(pre);
    for batch_idx in 0..pre {
        let mut acc = T::zero();
        for j in 0..last_two {
            let v = host[batch_idx * last_two + j];
            acc += v * v;
        }
        out_host.push(<T as Float>::sqrt(acc));
    }

    let out_shape: Vec<usize> = contiguous.shape()[..ndim - 2].to_vec();
    Ok(Tensor::from_slice_on(out_shape, &out_host, backend))
}

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

    fn v3() -> Tensor<f64, SequentialBackend> {
        Tensor::from_slice(vec![5], &[1.0f64, -2.0, 3.0, -4.0, 5.0])
    }

    fn ref_p(x: &[f64], p: f64) -> f64 {
        let s: f64 = x.iter().map(|&v| v.abs().powf(p)).sum();
        s.powf(1.0 / p)
    }

    #[test]
    fn norm_p_p2_matches_classical_l2() {
        let b = SequentialBackend::new();
        let x = v3();
        let got = norm_p(&x, 2.0_f64, &b);
        let want = (55.0_f64).sqrt();
        assert!(
            (got - want).abs() < 1e-12,
            "norm_p(p=2) = {got}, want {want}"
        );
    }

    #[test]
    fn norm_p_p1_matches_manhattan_distance() {
        let b = SequentialBackend::new();
        let x = v3();
        let got = norm_p(&x, 1.0_f64, &b);
        let want = 15.0_f64;
        assert!(
            (got - want).abs() < 1e-12,
            "norm_p(p=1) = {got}, want {want}"
        );
    }

    #[test]
    fn norm_p_p3_matches_cubic_reference() {
        let b = SequentialBackend::new();
        let x = v3();
        let got = norm_p(&x, 3.0_f64, &b);
        let want = ref_p(&[1.0, -2.0, 3.0, -4.0, 5.0], 3.0);
        assert!(
            (got - want).abs() < 1e-10,
            "norm_p(p=3) = {got}, want {want}"
        );
    }

    #[test]
    fn norm_p_is_identical_to_norm_at_p2() {
        let b = SequentialBackend::new();
        let x = v3();
        let n = norm(&x, &b).expect("valid norm test input");
        let n_p = norm_p(&x, 2.0_f64, &b);
        assert_eq!(n.to_bits(), n_p.to_bits());
    }

    #[test]
    #[should_panic(expected = "empty tensor has no norm")]
    fn norm_p_empty_panics() {
        let b = SequentialBackend::new();
        let x = Tensor::<f64, SequentialBackend>::from_slice(vec![0], &[0.0f64; 0]);
        let _ = norm_p(&x, 2.0_f64, &b);
    }

    #[test]
    #[should_panic(expected = "ord must be a finite positive number")]
    fn norm_p_negative_ord_panics() {
        let b = SequentialBackend::new();
        let x = v3();
        let _ = norm_p(&x, -1.0_f64, &b);
    }

    #[test]
    #[should_panic(expected = "ord must be a finite positive number")]
    fn norm_p_zero_ord_panics() {
        let b = SequentialBackend::new();
        let x = v3();
        let _ = norm_p(&x, 0.0_f64, &b);
    }

    #[test]
    #[should_panic(expected = "ord must be a finite positive number")]
    fn norm_p_infinite_ord_panics() {
        let b = SequentialBackend::new();
        let x = v3();
        let _ = norm_p(&x, f64::INFINITY, &b);
    }

    #[test]
    fn norm_p_axis_axis1_matches_row_references() {
        let b = SequentialBackend::new();
        let x = Tensor::<f64, SequentialBackend>::from_slice(
            vec![2, 3],
            &[1.0, -2.0, 3.0, -4.0, 5.0, -6.0],
        );
        let got = norm_p_axis(&x, 2.0, 1, &b);
        let want = [
            (1.0_f64 + 4.0 + 9.0).sqrt(),
            (16.0_f64 + 25.0 + 36.0).sqrt(),
        ];
        assert_eq!(got.shape(), &[2, 1]);
        assert!(
            got.as_slice()
                .iter()
                .zip(want)
                .all(|(&g, w)| (g - w).abs() < 1e-12),
            "norm_p_axis(axis=1) = {:?}, want {:?}",
            got.as_slice(),
            want
        );
    }

    #[test]
    fn norm_p_axis_axis0_matches_column_references() {
        let b = SequentialBackend::new();
        let x = Tensor::<f64, SequentialBackend>::from_slice(
            vec![2, 3],
            &[1.0, -2.0, 3.0, -4.0, 5.0, -6.0],
        );
        let got = norm_p_axis(&x, 1.0, 0, &b);
        let want = [5.0, 7.0, 9.0];
        assert_eq!(got.shape(), &[1, 3]);
        assert_eq!(got.as_slice(), &want);
    }

    #[test]
    fn norm_p_axis_rank1_reduces_to_scalar_tensor() {
        let b = SequentialBackend::new();
        let x = v3();
        let got = norm_p_axis(&x, 2.0, 0, &b);
        let n_global = norm_p(&x, 2.0, &b);
        assert_eq!(got.shape(), &[1]);
        assert_eq!(got.as_slice()[0].to_bits(), n_global.to_bits());
    }

    #[test]
    fn norm_p_axis_3d_axis1_matches_manual_per_slice() {
        let b = SequentialBackend::new();
        let x = Tensor::<f64, SequentialBackend>::from_slice(
            vec![2, 3, 2],
            &[
                1.0, 2.0, 3.0, 4.0, 5.0, 6.0, -1.0, 2.0, -3.0, 4.0, -5.0, 6.0,
            ],
        );
        let got = norm_p_axis(&x, 3.0, 1, &b);
        assert_eq!(got.shape(), &[2, 1, 2]);
        let want = [
            (1.0_f64 + 27.0 + 125.0).cbrt(),
            (8.0_f64 + 64.0 + 216.0).cbrt(),
            (1.0_f64 + 27.0 + 125.0).cbrt(),
            (8.0_f64 + 64.0 + 216.0).cbrt(),
        ];
        for (g, w) in got.as_slice().iter().zip(want) {
            assert!(
                (*g - w).abs() < 1e-9,
                "norm_p_axis(3D, axis=1) = {g}, want {w}"
            );
        }
    }

    #[test]
    fn norm_p_accepts_zero_copy_strided_views() {
        let b = SequentialBackend::new();
        let x = Tensor::<f64, SequentialBackend>::from_slice(
            vec![2, 3],
            &[1.0, -2.0, 3.0, -4.0, 5.0, -6.0],
        );
        let shared = x.clone();
        let transposed = x.permute(&[1, 0]);
        let got = norm_p(&transposed, 2.0, &b);
        let want = (91.0_f64).sqrt();
        assert!((got - want).abs() < 1e-12);

        let rows = norm_p_axis(&transposed, 2.0, 1, &b);
        let expected = [17.0_f64.sqrt(), 29.0_f64.sqrt(), 45.0_f64.sqrt()];
        assert_eq!(rows.shape(), &[3, 1]);
        for (&actual, expected) in rows.as_slice().iter().zip(expected) {
            assert!((actual - expected).abs() < 1e-12);
        }
        assert_eq!(shared.as_slice(), &[1.0, -2.0, 3.0, -4.0, 5.0, -6.0]);
    }

    #[test]
    #[should_panic(expected = "axis 2 out of bounds")]
    fn norm_p_axis_out_of_range_axis_panics() {
        let b = SequentialBackend::new();
        let x = Tensor::<f64, SequentialBackend>::from_slice(vec![2, 3], &[1.0; 6]);
        let _ = norm_p_axis(&x, 2.0, 2, &b);
    }

    #[test]
    #[should_panic(expected = "axis 1 has zero elements")]
    fn norm_p_axis_zero_size_axis_panics() {
        let b = SequentialBackend::new();
        let x = Tensor::<f64, SequentialBackend>::from_slice(vec![2, 0, 3], &[]);
        let _ = norm_p_axis(&x, 2.0, 1, &b);
    }

    #[test]
    #[should_panic(expected = "ord must be a finite positive number")]
    fn norm_p_axis_non_positive_ord_panics() {
        let b = SequentialBackend::new();
        let x = v3();
        let _ = norm_p_axis(&x, 0.0, 0, &b);
    }

    // ── frobenius_norm / frobenius_norm_batched ─────────────────────────────

    fn mat3x3(data: &[f64; 9]) -> Tensor<f64, SequentialBackend> {
        Tensor::<f64, SequentialBackend>::from_slice(vec![3, 3], data)
    }

    #[test]
    fn frobenius_norm_2d_matches_torch_oracle() {
        let b = SequentialBackend::new();
        let a = mat3x3(&[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]);
        let got = frobenius_norm(&a, &b).expect("valid Frobenius norm test input");
        let want = (204.0_f64).sqrt();
        assert!(
            (got - want).abs() < 1e-12,
            "frobenius_norm(3x3) = {got}, want {want}"
        );
    }

    #[test]
    fn frobenius_norm_2d_identity_matrix_is_sqrt_3() {
        let b = SequentialBackend::new();
        let id = mat3x3(&[1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]);
        let got = frobenius_norm(&id, &b).expect("valid Frobenius norm test input");
        let want = 3.0_f64.sqrt();
        assert!(
            (got - want).abs() < 1e-12,
            "frobenius_norm(I_3) = {got}, want {want}"
        );
    }

    #[test]
    fn frobenius_norm_batched_3d_returns_per_batch_scalars() {
        let b = SequentialBackend::new();
        let a = mat3x3(&[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]);
        let stacked = Tensor::<f64, SequentialBackend>::from_slice(
            vec![2, 3, 3],
            &[
                0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0,
                7.0, 8.0,
            ],
        );
        let got =
            frobenius_norm_batched(&stacked, &b).expect("valid batched Frobenius norm test input");
        assert_eq!(got.shape(), &[2]);
        let want = (204.0_f64).sqrt();
        for g in got.as_slice() {
            assert!((*g - want).abs() < 1e-12, "got {g}, want {want}");
        }
        let ref_scalar = frobenius_norm(&a, &b).expect("valid Frobenius norm test input");
        assert!(
            (ref_scalar - want).abs() < 1e-12,
            "2-D refr scalar = {ref_scalar}, want {want}"
        );
    }

    #[test]
    fn frobenius_norm_batched_4d_collapses_last_two_dims() {
        let b = SequentialBackend::new();
        let batch = Tensor::<f64, SequentialBackend>::from_slice(
            vec![2, 2, 3, 3],
            &[
                1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
                0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0,
                0.0, 0.0, 0.0, 1.0,
            ],
        );
        let got =
            frobenius_norm_batched(&batch, &b).expect("valid batched Frobenius norm test input");
        assert_eq!(got.shape(), &[2, 2]);
        let want = 3.0_f64.sqrt();
        for g in got.as_slice() {
            assert!((*g - want).abs() < 1e-12, "got {g}, want {want}");
        }
    }

    #[test]
    fn frobenius_norm_batched_2d_returns_zero_dim_scalar_tensor() {
        let b = SequentialBackend::new();
        let a = mat3x3(&[3.0, 0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 5.0]);
        let got = frobenius_norm_batched(&a, &b).expect("valid batched Frobenius norm test input");
        assert_eq!(got.shape(), &[]);
        let want = 50.0_f64.sqrt();
        let v = got.as_slice()[0];
        assert!((v - want).abs() < 1e-12, "got {v}, want {want}");
    }

    #[test]
    #[should_panic(expected = "rank >= 2")]
    fn frobenius_norm_batched_1d_panics() {
        let b = SequentialBackend::new();
        let x = v3();
        let _ = frobenius_norm_batched(&x, &b);
    }
}