1use std::str::Utf8Error;
2use crate::{DType, Slice, Shape};
3
4#[derive(Debug, thiserror::Error)]
5pub enum Error {
6 #[error("{msg}, expected: {expected:?}, got: {got:?}")]
8 UnexpectedDType {
9 msg: &'static str,
10 expected: DType,
11 got: DType,
12 },
13
14 #[error("dtype mismatch in {op}, lhs: {lhs:?}, rhs: {rhs:?}")]
15 DTypeMismatchBinaryOp {
16 lhs: DType,
17 rhs: DType,
18 op: &'static str,
19 },
20
21 #[error("unsupported dtype {0:?} for op {1}")]
22 UnsupportedDTypeForOp(DType, &'static str),
23
24 #[error("Index '{index}' out of range at storage({storage_len}) in take method")]
26 IndexOutOfRangeTake {
27 storage_len: usize,
28 index: usize,
29 },
30
31 #[error("index '{index}' out of range range({max_size}) in {op}")]
32 IndexOutOfRange {
33 max_size: usize,
34 index: usize,
35 op: &'static str,
36 },
37
38 #[error("{op}: dimension index {dim} out of range for shape {shape:?}")]
39 DimOutOfRange {
40 shape: Shape,
41 dim: i32,
42 op: &'static str,
43 },
44
45 #[error("{op}: dim size out of range in size {size}")]
46 DimSizeOutOfRange {
47 size: usize,
48 op: &'static str,
49 },
50
51 #[error("{op}: duplicate dim index {dims:?} for shape {shape:?}")]
52 DuplicateDimIndex {
53 shape: Shape,
54 dims: Vec<usize>,
55 op: &'static str,
56 },
57
58 #[error("try to repeat {repeats} for shape {shape}")]
59 RepeatRankOutOfRange {
60 repeats: Shape,
61 shape: Shape,
62 },
63
64 #[error("element count mismatch in reshape, try reshape {origin} to {target}")]
66 ElementCountMismatchInReshape {
67 origin: Shape,
68 target: Shape,
69 },
70
71 #[error("unexpected element size in {op}, expected: {expected}, got: {got}")]
72 ElementSizeMismatch {
73 expected: usize,
74 got: usize,
75 op: &'static str
76 },
77
78 #[error("unexpected rank, expected: {expected}, got: {got} ({shape:?})")]
79 UnexpectedNumberOfDims {
80 expected: usize,
81 got: usize,
82 shape: Shape,
83 },
84
85 #[error("{msg}, expected: {expected:?}, got: {got:?}")]
86 UnexpectedShape {
87 msg: String,
88 expected: Shape,
89 got: Shape,
90 },
91
92 #[error("requires contiguous {op}")]
93 RequiresContiguous { op: &'static str },
94
95 #[error("invalid index in {op}")]
96 InvalidIndex {
97 index: usize,
98 size: usize,
99 op: &'static str
100 },
101
102 #[error("shape mismatch in {op}, lhs: {lhs:?}, rhs: {rhs:?}")]
103 ShapeMismatchBinaryOp {
104 lhs: Shape,
105 rhs: Shape,
106 op: &'static str,
107 },
108
109 #[error("shape mismatch in cat for dim {dim}, shape for arg 1: {first_shape:?} shape for arg {n}: {nth_shape:?}")]
110 ShapeMismatchCat {
111 dim: usize,
112 first_shape: Shape,
113 n: usize,
114 nth_shape: Shape,
115 },
116
117 #[error("source Tensor shape {src:?} mismatch with condition shape {condition:?}")]
118 ShapeMismatchMaskedSelect {
119 src: Shape,
120 condition: Shape,
121 },
122
123 #[error("mask Tensor shape {mask:?} mismatch with {who} shape")]
124 ShapeMismatchSelect {
125 mask: Shape,
126 who: &'static str,
127 },
128
129 #[error("dst Tensor shape {dst:?} mismatch with src Tensor {src} shape")]
130 ShapeMismatchCopyFrom {
131 dst: Shape,
132 src: Shape,
133 },
134
135 #[error("slice invalid args {msg}: {shape:?}, dim: {dim}, slice: {slice}")]
137 SliceInvalidArgs {
138 shape: Shape,
139 dim: usize,
140 slice: Slice,
141 msg: &'static str,
142 },
143
144 #[error("narrow invalid args {msg}: {shape:?}, dim: {dim}, start: {start}, len:{len}")]
145 NarrowInvalidArgs {
146 shape: Shape,
147 dim: usize,
148 start: usize,
149 len: usize,
150 msg: &'static str,
151 },
152
153 #[error("can squeeze {dim} dim of {shape:?}(not 1)")]
154 SqueezeDimNot1 {
155 shape: Shape,
156 dim: usize,
157 },
158
159 #[error("cannot broadcast {src_shape:?} to {dst_shape:?}")]
160 BroadcastIncompatibleShapes { src_shape: Shape, dst_shape: Shape },
161
162 #[error("{op} expects at least one tensor")]
163 OpRequiresAtLeastOneTensor { op: &'static str },
164
165 #[error("rand error because {0}")]
166 Rand(String),
167
168 #[error("Tensor is not a scalar")]
169 NotScalar,
170
171 #[error("len mismatch with lhs {lhs} and rhs {rhs} in {op}")]
173 LenMismatchVector {
174 lhs: usize,
175 rhs: usize,
176 op: &'static str,
177 },
178
179 #[error("shape mismatch with lhs {lhs:?} and rhs {rhs:?} in {op}")]
180 ShapeMismatchMatrix {
181 lhs: (usize, usize),
182 rhs: (usize, usize),
183 op: &'static str,
184 },
185
186 #[error("boolean index should like vector, but got {0}")]
187 BooleanIndexShouldLikeVector(Shape),
188
189 #[error("index {index} of out range in {len} len vector")]
190 VectorIndexOutOfRange {
191 len: usize,
192 index: usize,
193 },
194
195 #[error("{position} index {index} of out range in {len} len matrix")]
196 MatrixIndexOutOfRange {
197 len: usize,
198 index: usize,
199 position: &'static str,
200 },
201
202 #[error("matrix shape not match expect {shape:?}, but got {len}")]
203 MatrixShapeUnmatch {
204 shape: (usize, usize),
205 len: usize,
206 },
207
208 #[error("vec mul matrix shape with {len:?} and {shape:?}")]
209 VecMulMatShapeMismatch {
210 len: usize, shape: (usize, usize),
211 },
212
213 #[error("vector len mismatch with {len1} and {len2} in {op}")]
214 VectorLenMismatch {
215 len1: usize, len2: usize, op: &'static str,
216 },
217
218 #[error("backward not support '{0}'")]
222 BackwardNotSupported(&'static str),
223
224 #[error(transparent)]
226 ParseInt(#[from] std::num::ParseIntError),
227
228 #[error(transparent)]
230 FromUtf8(#[from] std::string::FromUtf8Error),
231
232 #[error(transparent)]
234 Io(#[from] std::io::Error),
235
236 #[error(transparent)]
237 Utf8(#[from] Utf8Error),
238
239 #[error("visit a meta tensor!")]
241 MetaTensor,
242
243 #[error("{0}")]
245 Msg(String),
246
247 #[error("unwrap none")]
248 UnwrapNone,
249}
250
251pub type Result<T> = std::result::Result<T, Error>;
252
253#[macro_export]
254macro_rules! bail {
255 ($msg:literal $(,)?) => {
256 return Err($crate::Error::Msg(format!($msg).into()))?
257 };
258 ($err:expr $(,)?) => {
259 return Err($crate::Error::Msg(format!($err).into()))?
260 };
261 ($fmt:expr, $($arg:tt)*) => {
262 return Err($crate::Error::Msg(format!($fmt, $($arg)*).into()))?
263 };
264}