use crate::common::backend_lock::with_wgpu_backend_or_skip;
use numr::ops::BinaryOps;
use numr::runtime::wgpu::WgpuRuntime;
use numr::tensor::Tensor;
#[test]
fn test_broadcast_scalar_to_vector() {
with_wgpu_backend_or_skip(|client, device| {
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| {
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| {
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| {
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();
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| {
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();
let expected: Vec<f32> = vec![
11.0, 21.0, 31.0, 41.0, 12.0, 22.0, 32.0, 42.0, 13.0, 23.0, 33.0, 43.0, 14.0, 24.0, 34.0, 44.0, 15.0, 25.0, 35.0, 45.0, 16.0, 26.0, 36.0, 46.0,
];
assert_eq!(result, expected);
});
}
#[test]
fn test_broadcast_mul() {
with_wgpu_backend_or_skip(|client, device| {
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| {
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| {
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| {
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| {
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]);
});
}
#[test]
fn test_broadcast_i32_add() {
with_wgpu_backend_or_skip(|client, device| {
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| {
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]);
});
}