numr 0.15.2

Numerical computing and tensor library for Rust, in the role of NumPy: n-dimensional arrays, linear algebra, FFT, statistics and autograd on CPU, CUDA and WebGPU
Documentation
// Broadcasting behavior across ranks and directions (F32/I32).

use crate::common::backend_lock::with_wgpu_backend_or_skip;
use numr::ops::BinaryOps;
use numr::runtime::wgpu::WgpuRuntime;
use numr::tensor::Tensor;

// ============================================================================
// Broadcast Operations (F32)
// ============================================================================

#[test]
fn test_broadcast_scalar_to_vector() {
    with_wgpu_backend_or_skip(|client, device| {
        // [4] + [1] -> [4]
        let a = Tensor::<WgpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0], &[4], &device).unwrap();
        let b = Tensor::<WgpuRuntime>::from_slice(&[10.0f32], &[1], &device).unwrap();

        let c = client.add(&a, &b).unwrap();

        assert_eq!(c.shape(), &[4]);
        let result: Vec<f32> = c.to_vec();
        assert_eq!(result, [11.0, 12.0, 13.0, 14.0]);
    });
}

#[test]
fn test_broadcast_vector_to_matrix_row() {
    with_wgpu_backend_or_skip(|client, device| {
        // [2, 3] + [3] -> [2, 3] (broadcast along rows)
        let a =
            Tensor::<WgpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3], &device)
                .unwrap();
        let b = Tensor::<WgpuRuntime>::from_slice(&[10.0f32, 20.0, 30.0], &[3], &device).unwrap();

        let c = client.add(&a, &b).unwrap();

        assert_eq!(c.shape(), &[2, 3]);
        let result: Vec<f32> = c.to_vec();
        assert_eq!(result, [11.0, 22.0, 33.0, 14.0, 25.0, 36.0]);
    });
}

#[test]
fn test_broadcast_vector_to_matrix_col() {
    with_wgpu_backend_or_skip(|client, device| {
        // [2, 3] + [2, 1] -> [2, 3] (broadcast along columns)
        let a =
            Tensor::<WgpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3], &device)
                .unwrap();
        let b = Tensor::<WgpuRuntime>::from_slice(&[10.0f32, 100.0], &[2, 1], &device).unwrap();

        let c = client.add(&a, &b).unwrap();

        assert_eq!(c.shape(), &[2, 3]);
        let result: Vec<f32> = c.to_vec();
        assert_eq!(result, [11.0, 12.0, 13.0, 104.0, 105.0, 106.0]);
    });
}

#[test]
fn test_broadcast_both_directions() {
    with_wgpu_backend_or_skip(|client, device| {
        // [3, 1] + [1, 4] -> [3, 4]
        let a = Tensor::<WgpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0], &[3, 1], &device).unwrap();
        let b = Tensor::<WgpuRuntime>::from_slice(&[10.0f32, 20.0, 30.0, 40.0], &[1, 4], &device)
            .unwrap();

        let c = client.add(&a, &b).unwrap();

        assert_eq!(c.shape(), &[3, 4]);
        let result: Vec<f32> = c.to_vec();
        // Row 0: 1+[10,20,30,40] = [11,21,31,41]
        // Row 1: 2+[10,20,30,40] = [12,22,32,42]
        // Row 2: 3+[10,20,30,40] = [13,23,33,43]
        assert_eq!(
            result,
            [
                11.0, 21.0, 31.0, 41.0, 12.0, 22.0, 32.0, 42.0, 13.0, 23.0, 33.0, 43.0
            ]
        );
    });
}

#[test]
fn test_broadcast_3d() {
    with_wgpu_backend_or_skip(|client, device| {
        // [2, 3, 1] + [4] -> [2, 3, 4]
        let a = Tensor::<WgpuRuntime>::from_slice(
            &[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0],
            &[2, 3, 1],
            &device,
        )
        .unwrap();
        let b =
            Tensor::<WgpuRuntime>::from_slice(&[10.0f32, 20.0, 30.0, 40.0], &[4], &device).unwrap();

        let c = client.add(&a, &b).unwrap();

        assert_eq!(c.shape(), &[2, 3, 4]);
        let result: Vec<f32> = c.to_vec();
        // Element [i,j,k] = a[i,j,0] + b[k]
        let expected: Vec<f32> = vec![
            // i=0, j=0: 1 + [10,20,30,40]
            11.0, 21.0, 31.0, 41.0, // i=0, j=1: 2 + [10,20,30,40]
            12.0, 22.0, 32.0, 42.0, // i=0, j=2: 3 + [10,20,30,40]
            13.0, 23.0, 33.0, 43.0, // i=1, j=0: 4 + [10,20,30,40]
            14.0, 24.0, 34.0, 44.0, // i=1, j=1: 5 + [10,20,30,40]
            15.0, 25.0, 35.0, 45.0, // i=1, j=2: 6 + [10,20,30,40]
            16.0, 26.0, 36.0, 46.0,
        ];
        assert_eq!(result, expected);
    });
}

#[test]
fn test_broadcast_mul() {
    with_wgpu_backend_or_skip(|client, device| {
        // [2, 3] * [3] -> [2, 3]
        let a =
            Tensor::<WgpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3], &device)
                .unwrap();
        let b = Tensor::<WgpuRuntime>::from_slice(&[2.0f32, 3.0, 4.0], &[3], &device).unwrap();

        let c = client.mul(&a, &b).unwrap();

        assert_eq!(c.shape(), &[2, 3]);
        let result: Vec<f32> = c.to_vec();
        assert_eq!(result, [2.0, 6.0, 12.0, 8.0, 15.0, 24.0]);
    });
}

#[test]
fn test_broadcast_sub() {
    with_wgpu_backend_or_skip(|client, device| {
        // [2, 3] - [2, 1] -> [2, 3]
        let a = Tensor::<WgpuRuntime>::from_slice(
            &[10.0f32, 20.0, 30.0, 40.0, 50.0, 60.0],
            &[2, 3],
            &device,
        )
        .unwrap();
        let b = Tensor::<WgpuRuntime>::from_slice(&[1.0f32, 10.0], &[2, 1], &device).unwrap();

        let c = client.sub(&a, &b).unwrap();

        assert_eq!(c.shape(), &[2, 3]);
        let result: Vec<f32> = c.to_vec();
        assert_eq!(result, [9.0, 19.0, 29.0, 30.0, 40.0, 50.0]);
    });
}

#[test]
fn test_broadcast_div() {
    with_wgpu_backend_or_skip(|client, device| {
        // [2, 3] / [3] -> [2, 3]
        let a = Tensor::<WgpuRuntime>::from_slice(
            &[10.0f32, 20.0, 30.0, 40.0, 50.0, 60.0],
            &[2, 3],
            &device,
        )
        .unwrap();
        let b = Tensor::<WgpuRuntime>::from_slice(&[2.0f32, 5.0, 10.0], &[3], &device).unwrap();

        let c = client.div(&a, &b).unwrap();

        assert_eq!(c.shape(), &[2, 3]);
        let result: Vec<f32> = c.to_vec();
        assert_eq!(result, [5.0, 4.0, 3.0, 20.0, 10.0, 6.0]);
    });
}

#[test]
fn test_broadcast_max() {
    with_wgpu_backend_or_skip(|client, device| {
        // max([2, 3], [3]) -> [2, 3]
        let a =
            Tensor::<WgpuRuntime>::from_slice(&[1.0f32, 5.0, 3.0, 4.0, 2.0, 6.0], &[2, 3], &device)
                .unwrap();
        let b = Tensor::<WgpuRuntime>::from_slice(&[3.0f32, 3.0, 3.0], &[3], &device).unwrap();

        let c = client.maximum(&a, &b).unwrap();

        assert_eq!(c.shape(), &[2, 3]);
        let result: Vec<f32> = c.to_vec();
        assert_eq!(result, [3.0, 5.0, 3.0, 4.0, 3.0, 6.0]);
    });
}

#[test]
fn test_broadcast_min() {
    with_wgpu_backend_or_skip(|client, device| {
        // min([2, 3], [3]) -> [2, 3]
        let a =
            Tensor::<WgpuRuntime>::from_slice(&[1.0f32, 5.0, 3.0, 4.0, 2.0, 6.0], &[2, 3], &device)
                .unwrap();
        let b = Tensor::<WgpuRuntime>::from_slice(&[3.0f32, 3.0, 3.0], &[3], &device).unwrap();

        let c = client.minimum(&a, &b).unwrap();

        assert_eq!(c.shape(), &[2, 3]);
        let result: Vec<f32> = c.to_vec();
        assert_eq!(result, [1.0, 3.0, 3.0, 3.0, 2.0, 3.0]);
    });
}

// ============================================================================
// Broadcast Operations (I32)
// ============================================================================

#[test]
fn test_broadcast_i32_add() {
    with_wgpu_backend_or_skip(|client, device| {
        // [2, 3] + [3] -> [2, 3]
        let a =
            Tensor::<WgpuRuntime>::from_slice(&[1i32, 2, 3, 4, 5, 6], &[2, 3], &device).unwrap();
        let b = Tensor::<WgpuRuntime>::from_slice(&[10i32, 20, 30], &[3], &device).unwrap();

        let c = client.add(&a, &b).unwrap();

        assert_eq!(c.shape(), &[2, 3]);
        let result: Vec<i32> = c.to_vec();
        assert_eq!(result, [11, 22, 33, 14, 25, 36]);
    });
}

#[test]
fn test_broadcast_i32_mul() {
    with_wgpu_backend_or_skip(|client, device| {
        // [3, 1] * [1, 4] -> [3, 4]
        let a = Tensor::<WgpuRuntime>::from_slice(&[1i32, 2, 3], &[3, 1], &device).unwrap();
        let b = Tensor::<WgpuRuntime>::from_slice(&[10i32, 20, 30, 40], &[1, 4], &device).unwrap();

        let c = client.mul(&a, &b).unwrap();

        assert_eq!(c.shape(), &[3, 4]);
        let result: Vec<i32> = c.to_vec();
        assert_eq!(result, [10, 20, 30, 40, 20, 40, 60, 80, 30, 60, 90, 120]);
    });
}