use crate::shape::Shape;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("shape mismatch: expected {expected}, got {got}")]
ShapeMismatch { expected: Shape, got: Shape },
#[error("rank mismatch: expected rank {expected}, got {got}")]
RankMismatch { expected: usize, got: usize },
#[error("dtype mismatch: expected {expected:?}, got {got:?}")]
DTypeMismatch {
expected: crate::DType,
got: crate::DType,
},
#[error("dimension out of range: dim {dim} for tensor with {rank} dimensions")]
DimOutOfRange { dim: usize, rank: usize },
#[error("narrow out of bounds: dim {dim}, start {start}, len {len}, dim_size {dim_size}")]
NarrowOutOfBounds {
dim: usize,
start: usize,
len: usize,
dim_size: usize,
},
#[error("not a scalar: tensor has shape {shape}")]
NotAScalar { shape: Shape },
#[error("element count mismatch: shape {shape} requires {expected} elements, got {got}")]
ElementCountMismatch {
shape: Shape,
expected: usize,
got: usize,
},
#[error("matmul shape mismatch: [{m}x{k1}] @ [{k2}x{n}] — inner dims must match")]
MatmulShapeMismatch {
m: usize,
k1: usize,
k2: usize,
n: usize,
},
#[error(
"cannot reshape: source has {src} elements, target shape {dst_shape} has {dst} elements"
)]
ReshapeElementMismatch {
src: usize,
dst: usize,
dst_shape: Shape,
},
#[error("{0}")]
Msg(String),
}
impl Error {
pub fn msg(s: impl Into<String>) -> Self {
Error::Msg(s.into())
}
}
pub type Result<T> = std::result::Result<T, Error>;
#[macro_export]
macro_rules! bail {
($($arg:tt)*) => {
return Err($crate::Error::Msg(format!($($arg)*)))
};
}