mod utils;
use maidenx_core::{
device::{auto_set_device, Device},
dtype::DType,
error::Result,
};
use utils::{setup_tensor, setup_tensor_without_dtype};
#[test]
fn with_shape() -> Result<()> {
auto_set_device();
let mut x = setup_tensor_without_dtype(vec![1, 2, 3, 4, 5, 6])?;
x.with_shape(&[2, 3])?;
assert_eq!(x.shape(), &[2, 3]);
assert_eq!(x.to_flatten_vec::<i32>()?, vec![1, 2, 3, 4, 5, 6]);
x.with_shape(&[3, 2])?;
assert_eq!(x.shape(), &[3, 2]);
assert_eq!(x.to_flatten_vec::<i32>()?, vec![1, 2, 3, 4, 5, 6]);
let result = x.with_shape(&[2, 2]);
assert!(result.is_err());
Ok(())
}
#[test]
fn to_shape() -> Result<()> {
auto_set_device();
let x = setup_tensor_without_dtype(vec![1, 2, 3, 4, 5, 6])?;
let y = x.to_shape(&[2, 3])?;
assert_eq!(y.shape(), &[2, 3]);
assert_eq!(y.to_flatten_vec::<i32>()?, vec![1, 2, 3, 4, 5, 6]);
assert_eq!(x.shape(), &[6]);
let result = x.to_shape(&[2, 2]);
assert!(result.is_err());
Ok(())
}
#[test]
fn with_device() -> Result<()> {
auto_set_device();
let mut x = setup_tensor_without_dtype(vec![1, 2, 3, 4])?;
let original_device = x.device();
x.with_device(original_device)?;
assert_eq!(x.device(), original_device);
assert_eq!(x.to_flatten_vec::<i32>()?, vec![1, 2, 3, 4]);
x.with_device(Device::CPU)?;
assert_eq!(x.device(), Device::CPU);
assert_eq!(x.to_flatten_vec::<i32>()?, vec![1, 2, 3, 4]);
Ok(())
}
#[test]
fn to_device() -> Result<()> {
auto_set_device();
let x = setup_tensor_without_dtype(vec![1, 2, 3, 4])?;
let original_device = x.device();
let y = x.to_device(Device::CPU)?;
assert_eq!(y.device(), Device::CPU);
assert_eq!(y.to_flatten_vec::<i32>()?, vec![1, 2, 3, 4]);
assert_eq!(x.device(), original_device);
Ok(())
}
#[test]
fn with_dtype() -> Result<()> {
auto_set_device();
let mut x = setup_tensor_without_dtype(vec![1, 2, 3, 4])?;
let original_dtype = x.dtype();
x.with_dtype(DType::F32)?;
assert_eq!(x.dtype(), DType::F32);
assert_eq!(x.to_flatten_vec::<f32>()?, vec![1.0, 2.0, 3.0, 4.0]);
x.with_dtype(original_dtype)?;
assert_eq!(x.dtype(), original_dtype);
#[cfg(feature = "mps")]
if x.device() == Device::MPS {
let result = x.with_dtype(DType::F64);
assert!(result.is_err());
}
Ok(())
}
#[test]
fn to_dtype() -> Result<()> {
auto_set_device();
let x = setup_tensor_without_dtype(vec![1, 2, 3, 4])?;
let original_dtype = x.dtype();
let y = x.to_dtype(DType::F32)?;
assert_eq!(y.dtype(), DType::F32);
assert_eq!(y.to_flatten_vec::<f32>()?, vec![1.0, 2.0, 3.0, 4.0]);
assert_eq!(x.dtype(), original_dtype);
#[cfg(feature = "mps")]
if x.device() == Device::MPS {
let result = x.to_dtype(DType::F64);
assert!(result.is_err());
}
Ok(())
}
#[test]
fn with_grad() -> Result<()> {
auto_set_device();
let mut x = setup_tensor(vec![1.0, 2.0, 3.0, 4.0], DType::F32)?;
assert!(!x.requires_grad());
assert!(x.grad()?.is_none());
x.with_grad()?;
assert!(x.requires_grad());
assert!(x.grad()?.is_some());
let grad = x.grad()?.unwrap();
assert_eq!(grad.shape(), x.shape());
assert_eq!(grad.to_flatten_vec::<f32>()?, vec![0.0, 0.0, 0.0, 0.0]);
let mut y = setup_tensor(vec![1, 2, 3, 4], DType::I32)?;
let result = y.with_grad();
assert!(result.is_err());
Ok(())
}