1use crate::shape::Shape;
2
3#[derive(Debug, thiserror::Error)]
9pub enum Error {
10 #[error("shape mismatch: expected {expected}, got {got}")]
12 ShapeMismatch { expected: Shape, got: Shape },
13
14 #[error("rank mismatch: expected rank {expected}, got {got}")]
16 RankMismatch { expected: usize, got: usize },
17
18 #[error("dtype mismatch: expected {expected:?}, got {got:?}")]
20 DTypeMismatch {
21 expected: crate::DType,
22 got: crate::DType,
23 },
24
25 #[error("dimension out of range: dim {dim} for tensor with {rank} dimensions")]
27 DimOutOfRange { dim: usize, rank: usize },
28
29 #[error("narrow out of bounds: dim {dim}, start {start}, len {len}, dim_size {dim_size}")]
31 NarrowOutOfBounds {
32 dim: usize,
33 start: usize,
34 len: usize,
35 dim_size: usize,
36 },
37
38 #[error("not a scalar: tensor has shape {shape}")]
40 NotAScalar { shape: Shape },
41
42 #[error("element count mismatch: shape {shape} requires {expected} elements, got {got}")]
44 ElementCountMismatch {
45 shape: Shape,
46 expected: usize,
47 got: usize,
48 },
49
50 #[error("matmul shape mismatch: [{m}x{k1}] @ [{k2}x{n}] — inner dims must match")]
52 MatmulShapeMismatch {
53 m: usize,
54 k1: usize,
55 k2: usize,
56 n: usize,
57 },
58
59 #[error(
61 "cannot reshape: source has {src} elements, target shape {dst_shape} has {dst} elements"
62 )]
63 ReshapeElementMismatch {
64 src: usize,
65 dst: usize,
66 dst_shape: Shape,
67 },
68
69 #[error("{0}")]
71 Msg(String),
72}
73
74impl Error {
75 pub fn msg(s: impl Into<String>) -> Self {
77 Error::Msg(s.into())
78 }
79}
80
81pub type Result<T> = std::result::Result<T, Error>;
83
84#[macro_export]
87macro_rules! bail {
88 ($($arg:tt)*) => {
89 return Err($crate::Error::Msg(format!($($arg)*)))
90 };
91}