use numr::prelude::*;
fn client() -> (CpuClient, CpuDevice) {
let device = CpuDevice::new();
let client = CpuRuntime::default_client(&device);
(client, device)
}
#[test]
fn index_select_with_empty_inner_dim_writes_nothing() {
let (client, device) = client();
let a = Tensor::<CpuRuntime>::zeros(&[2, 3, 0], DType::F32, &device).unwrap();
let idx = Tensor::<CpuRuntime>::from_slice(&[0i64, 2i64], &[2], &device).unwrap();
let out = client.index_select(&a, 1, &idx).unwrap();
assert_eq!(out.shape(), &[2, 2, 0]);
assert_eq!(out.numel(), 0);
}
#[test]
fn index_select_with_empty_outer_dim_writes_nothing() {
let (client, device) = client();
let a = Tensor::<CpuRuntime>::zeros(&[0, 3, 2], DType::F32, &device).unwrap();
let idx = Tensor::<CpuRuntime>::from_slice(&[1i64], &[1], &device).unwrap();
let out = client.index_select(&a, 1, &idx).unwrap();
assert_eq!(out.shape(), &[0, 1, 2]);
assert_eq!(out.numel(), 0);
}
#[test]
fn logsumexp_over_empty_batch_returns_empty() {
let (client, device) = client();
let a = Tensor::<CpuRuntime>::zeros(&[0, 4], DType::F32, &device).unwrap();
let out = client.logsumexp(&a, &[1], false).unwrap();
assert_eq!(out.shape(), &[0]);
assert_eq!(out.numel(), 0);
}
#[test]
fn cumsum_over_empty_batch_returns_empty() {
let (client, device) = client();
let a = Tensor::<CpuRuntime>::zeros(&[0, 4], DType::F32, &device).unwrap();
let out = client.cumsum(&a, 1).unwrap();
assert_eq!(out.shape(), &[0, 4]);
assert_eq!(out.numel(), 0);
}
#[test]
fn rms_norm_over_empty_batch_returns_empty() {
let (client, device) = client();
let input = Tensor::<CpuRuntime>::zeros(&[0, 4], DType::F32, &device).unwrap();
let weight = Tensor::<CpuRuntime>::ones(&[4], DType::F32, &device).unwrap();
let out = client.rms_norm(&input, &weight, 1e-5).unwrap();
assert_eq!(out.shape(), &[0, 4]);
assert_eq!(out.numel(), 0);
}
#[test]
fn layer_norm_over_empty_batch_returns_empty() {
let (client, device) = client();
let input = Tensor::<CpuRuntime>::zeros(&[0, 4], DType::F32, &device).unwrap();
let weight = Tensor::<CpuRuntime>::ones(&[4], DType::F32, &device).unwrap();
let bias = Tensor::<CpuRuntime>::zeros(&[4], DType::F32, &device).unwrap();
let out = client.layer_norm(&input, &weight, &bias, 1e-5).unwrap();
assert_eq!(out.shape(), &[0, 4]);
assert_eq!(out.numel(), 0);
}
#[test]
fn group_norm_with_empty_spatial_dim_returns_empty() {
let (client, device) = client();
let input = Tensor::<CpuRuntime>::zeros(&[2, 4, 0], DType::F32, &device).unwrap();
let weight = Tensor::<CpuRuntime>::ones(&[4], DType::F32, &device).unwrap();
let bias = Tensor::<CpuRuntime>::zeros(&[4], DType::F32, &device).unwrap();
let out = client.group_norm(&input, &weight, &bias, 2, 1e-5).unwrap();
assert_eq!(out.shape(), &[2, 4, 0]);
assert_eq!(out.numel(), 0);
}
#[test]
fn batched_matmul_with_empty_batch_dim_returns_empty() {
let (client, device) = client();
let a = Tensor::<CpuRuntime>::zeros(&[0, 3, 4], DType::F32, &device).unwrap();
let b = Tensor::<CpuRuntime>::zeros(&[0, 4, 5], DType::F32, &device).unwrap();
let out = client.matmul(&a, &b).unwrap();
assert_eq!(out.shape(), &[0, 3, 5]);
assert_eq!(out.numel(), 0);
}
#[test]
fn rfft_over_empty_batch_returns_empty() {
let (client, device) = client();
let a = Tensor::<CpuRuntime>::zeros(&[0, 4], DType::F32, &device).unwrap();
let out = client.rfft(&a, FftNormalization::None).unwrap();
assert_eq!(out.shape(), &[0, 3]);
assert_eq!(out.numel(), 0);
}