1use std::str::Utf8Error;
2
3use crate::{DType, Shape};
4
5#[derive(Debug, thiserror::Error)]
6pub enum Error {
7 #[error("{msg}, expected: {expected:?}, got: {got:?}")]
9 UnexpectedDType { msg: &'static str, expected: DType, got: DType },
10
11 #[error("dtype mismatch in {op}, lhs: {lhs:?}, rhs: {rhs:?}")]
12 DTypeMismatchBinaryOp { lhs: DType, rhs: DType, op: &'static str },
13
14 #[error("dtype mismatch in {op}, lhs: {lhs:?}, rhs: {rhs:?} (align them with `cast`)")]
17 DTypeMismatch { lhs: DType, rhs: DType, op: &'static str },
18
19 #[error("unsupported dtype {0:?} for op {1}")]
20 UnsupportedDTypeForOp(DType, &'static str),
21
22 #[error("Index '{index}' out of range at storage({storage_len}) in take method")]
24 IndexOutOfRangeTake { storage_len: usize, index: usize },
25
26 #[error("index '{index}' out of range range({max_size}) in {op}")]
27 IndexOutOfRange { max_size: usize, index: usize, op: &'static str },
28
29 #[error("{op}: dimension index {dim} out of range for shape {shape:?}")]
30 DimOutOfRange { shape: Shape, dim: i32, op: &'static str },
31
32 #[error("{op}: dim size out of range in size {size}")]
33 DimSizeOutOfRange { size: usize, op: &'static str },
34
35 #[error("{op}: duplicate dim index {dims:?} for shape {shape:?}")]
36 DuplicateDimIndex { shape: Shape, dims: Vec<usize>, op: &'static str },
37
38 #[error("try to repeat {repeats} for shape {shape}")]
39 RepeatRankOutOfRange { repeats: Shape, shape: Shape },
40
41 #[error("element count mismatch in reshape, try reshape {origin} to {target}")]
43 ElementCountMismatchInReshape { origin: Shape, target: Shape },
44
45 #[error("unexpected element size in {op}, expected: {expected}, got: {got}")]
46 ElementSizeMismatch { expected: usize, got: usize, op: &'static str },
47
48 #[error("unexpected rank, expected: {expected}, got: {got} ({shape:?})")]
49 UnexpectedNumberOfDims { expected: usize, got: usize, shape: Shape },
50
51 #[error("{msg}, expected: {expected:?}, got: {got:?}")]
52 UnexpectedShape { msg: String, expected: Shape, got: Shape },
53
54 #[error("requires contiguous {op}")]
55 RequiresContiguous { op: &'static str },
56
57 #[error("invalid index in {op}")]
58 InvalidIndex { index: usize, size: usize, op: &'static str },
59
60 #[error("shape mismatch in {op}, lhs: {lhs:?}, rhs: {rhs:?}")]
61 ShapeMismatchBinaryOp { lhs: Shape, rhs: Shape, op: &'static str },
62
63 #[error("device mismatch in {op}, lhs: {lhs:?}, rhs: {rhs:?}")]
64 DeviceMismatchBinaryOp { lhs: String, rhs: String, op: &'static str },
65
66 #[error("shape mismatch in cat for dim {dim}, shape for arg1: {first_shape:?} shape for arg {n}: {nth_shape:?}")]
67 ShapeMismatchCat { dim: usize, first_shape: Shape, n: usize, nth_shape: Shape },
68
69 #[error("source Tensor shape {src:?} mismatch with condition shape {condition:?}")]
70 ShapeMismatchMaskedSelect { src: Shape, condition: Shape },
71
72 #[error("mask Tensor shape {mask:?} mismatch with {who} shape")]
73 ShapeMismatchSelect { mask: Shape, who: &'static str },
74
75 #[error("dst Tensor shape {dst:?} mismatch with src Tensor {src} shape")]
76 ShapeMismatchCopyFrom { dst: Shape, src: Shape },
77
78 #[error("narrow invalid args {msg}: {shape:?}, dim: {dim}, start: {start}, len:{len}")]
80 NarrowInvalidArgs { shape: Shape, dim: usize, start: usize, len: usize, msg: &'static str },
81
82 #[error("can squeeze {dim} dim of {shape:?}(not 1)")]
83 SqueezeDimNot1 { shape: Shape, dim: usize },
84
85 #[error("cannot broadcast {src_shape:?} to {dst_shape:?}")]
86 BroadcastIncompatibleShapes { src_shape: Shape, dst_shape: Shape },
87
88 #[error("{op} expects at least one tensor")]
89 OpRequiresAtLeastOneTensor { op: &'static str },
90
91 #[error("rand error because {0}")]
92 Rand(String),
93
94 #[error("Tensor is not a scalar")]
95 NotScalar,
96
97 #[error("backward not support '{0}'")]
98 BackwardNotSupported(&'static str),
99
100 #[error(transparent)]
102 ParseInt(#[from] std::num::ParseIntError),
103
104 #[error(transparent)]
106 FromUtf8(#[from] std::string::FromUtf8Error),
107
108 #[error(transparent)]
110 Io(#[from] std::io::Error),
111
112 #[cfg(feature = "cuda")]
113 #[error(transparent)]
114 Cuda(#[from] crate::device::cuda::CudaError),
115
116 #[error(transparent)]
117 Utf8(#[from] Utf8Error),
118
119 #[error("visit a meta tensor!")]
121 MetaTensor,
122
123 #[error("{0}")]
125 Msg(String),
126
127 #[error("unwrap none")]
128 UnwrapNone,
129}
130
131pub type Result<T> = std::result::Result<T, Error>;
132
133#[macro_export]
134macro_rules! bail {
135 ($msg:literal $(,)?) => {
136 return Err($crate::Error::Msg(format!($msg).into()))?
137 };
138 ($err:expr $(,)?) => {
139 return Err($crate::Error::Msg(format!($err).into()))?
140 };
141 ($fmt:expr, $($arg:tt)*) => {
142 return Err($crate::Error::Msg(format!($fmt, $($arg)*).into()))?
143 };
144}