1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//! Error types for numr
use crate::dtype::DType;
use thiserror::Error;
/// Result type alias using numr's Error
pub type Result<T> = std::result::Result<T, Error>;
/// Errors that can occur in numr operations
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
/// Shape mismatch in an operation
#[error("Shape mismatch: expected {expected:?}, got {got:?}")]
ShapeMismatch {
/// Expected shape
expected: Vec<usize>,
/// Actual shape
got: Vec<usize>,
},
/// Shapes cannot be broadcast together
#[error("Cannot broadcast shapes {lhs:?} and {rhs:?}")]
BroadcastError {
/// Left-hand side shape
lhs: Vec<usize>,
/// Right-hand side shape
rhs: Vec<usize>,
},
/// Invalid dimension index
#[error("Invalid dimension {dim} for tensor with {ndim} dimensions")]
InvalidDimension {
/// The invalid dimension
dim: isize,
/// Number of dimensions
ndim: usize,
},
/// Unsupported dtype for an operation
#[error("Unsupported dtype {dtype:?} for operation '{op}'")]
UnsupportedDType {
/// The unsupported dtype
dtype: DType,
/// The operation name
op: &'static str,
},
/// DType mismatch between operands
#[error("DType mismatch: {lhs:?} vs {rhs:?}")]
DTypeMismatch {
/// Left-hand side dtype
lhs: DType,
/// Right-hand side dtype
rhs: DType,
},
/// Device mismatch between operands
#[error("Device mismatch: tensors must be on the same device")]
DeviceMismatch,
/// Out of memory
#[error("Out of memory: failed to allocate {size} bytes")]
OutOfMemory {
/// Requested size in bytes
size: usize,
},
/// Tensor allocation failed, with the shape/dtype/device context
///
/// Wraps the underlying allocation error (usually [`Error::OutOfMemory`])
/// so the message names what was being allocated, not only a byte count.
#[error("failed to allocate tensor {shape:?} {dtype} on {device}: {source}")]
AllocFailed {
/// Shape of the tensor that could not be allocated
shape: Vec<usize>,
/// Element type short name (e.g. "bf16", "q4_0")
dtype: String,
/// Device name (from [`Device::name`](crate::runtime::Device::name))
device: String,
/// The underlying allocation error
#[source]
source: Box<Error>,
},
/// Index out of bounds
#[error("Index {index} out of bounds for dimension of size {size}")]
IndexOutOfBounds {
/// The invalid index
index: usize,
/// Size of the dimension
size: usize,
},
/// Invalid argument provided to an operation
#[error("Invalid argument '{arg}': {reason}")]
InvalidArgument {
/// The argument name
arg: &'static str,
/// Reason for invalidity
reason: String,
},
/// Tensor is not contiguous when contiguous memory is required
#[error("Operation requires contiguous tensor")]
NotContiguous,
/// Missing gradient in backward pass
#[error("Missing gradient for tensor")]
MissingGradient,
/// Backend-specific error
#[error("Backend error: {0}")]
Backend(String),
/// Backend limitation - operation valid but exceeds backend capabilities
#[error("{backend} limitation: {operation} - {reason}")]
BackendLimitation {
/// The backend that has the limitation
backend: &'static str,
/// The operation being attempted
operation: &'static str,
/// Description of the limitation
reason: String,
},
/// CUDA-specific error
#[cfg(feature = "cuda")]
#[error("CUDA error: {0}")]
Cuda(#[from] cudarc::driver::DriverError),
/// Generic message error
#[error("{0}")]
Msg(String),
/// Generic internal error
#[error("Internal error: {0}")]
Internal(String),
/// Feature not yet implemented
#[error("Not implemented: {feature}")]
NotImplemented {
/// Description of the unimplemented feature
feature: &'static str,
},
/// Cargo feature required but not enabled
#[error(
"{dtype:?} requires the \"{feature}\" feature. Enable it with: cargo build --features {feature}"
)]
FeatureRequired {
/// The dtype that needs the feature
dtype: DType,
/// The cargo feature name to enable
feature: &'static str,
},
/// Allocator cannot reset while allocations are still live
#[error("Allocator busy: {active_allocations} allocations still active")]
AllocatorBusy {
/// Number of allocations that are still live
active_allocations: usize,
},
/// Allocator is frozen — no new allocations permitted
#[error("Allocator frozen: allocation rejected while frozen")]
AllocatorFrozen,
}
impl Error {
/// Create a shape mismatch error
pub fn shape_mismatch(expected: &[usize], got: &[usize]) -> Self {
Self::ShapeMismatch {
expected: expected.to_vec(),
got: got.to_vec(),
}
}
/// Create a broadcast error
pub fn broadcast(lhs: &[usize], rhs: &[usize]) -> Self {
Self::BroadcastError {
lhs: lhs.to_vec(),
rhs: rhs.to_vec(),
}
}
/// Create an unsupported dtype error
pub fn unsupported_dtype(dtype: DType, op: &'static str) -> Self {
Self::UnsupportedDType { dtype, op }
}
/// Create a backend limitation error
pub fn backend_limitation(
backend: &'static str,
operation: &'static str,
reason: impl Into<String>,
) -> Self {
Self::BackendLimitation {
backend,
operation,
reason: reason.into(),
}
}
}