Add some basic test.

This commit is contained in:
laurent
2023-06-19 19:50:17 +01:00
parent 8e2c534d1f
commit 634e0c88ae
4 changed files with 84 additions and 1 deletions

View File

@ -1,4 +1,4 @@
use crate::{op::Op, storage::Storage, DType, Device};
use crate::{op::Op, storage::Storage, DType, Device, Error, Result};
use std::sync::Arc;
#[allow(dead_code)]
@ -46,4 +46,56 @@ impl Tensor {
pub fn elem_count(&self) -> usize {
self.0.shape.iter().product()
}
pub fn shape1(&self) -> Result<usize> {
let shape = self.shape();
if shape.len() == 1 {
Ok(shape[0])
} else {
Err(Error::UnexpectedNumberOfDims {
expected: 1,
got: shape.len(),
shape: shape.to_vec(),
})
}
}
pub fn shape2(&self) -> Result<(usize, usize)> {
let shape = self.shape();
if shape.len() == 2 {
Ok((shape[0], shape[1]))
} else {
Err(Error::UnexpectedNumberOfDims {
expected: 2,
got: shape.len(),
shape: shape.to_vec(),
})
}
}
pub fn shape3(&self) -> Result<(usize, usize, usize)> {
let shape = self.shape();
if shape.len() == 3 {
Ok((shape[0], shape[1], shape[2]))
} else {
Err(Error::UnexpectedNumberOfDims {
expected: 3,
got: shape.len(),
shape: shape.to_vec(),
})
}
}
pub fn shape4(&self) -> Result<(usize, usize, usize, usize)> {
let shape = self.shape();
if shape.len() == 4 {
Ok((shape[0], shape[1], shape[2], shape[4]))
} else {
Err(Error::UnexpectedNumberOfDims {
expected: 4,
got: shape.len(),
shape: shape.to_vec(),
})
}
}
}