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
//! Error types for Realizar
//!
//! This module defines all error types used throughout the library.
use thiserror::Error;
/// Result type alias for Realizar operations
pub type Result<T> = std::result::Result<T, RealizarError>;
/// Error type for all Realizar operations
#[derive(Error, Debug, Clone, PartialEq)]
pub enum RealizarError {
/// Shape mismatch between tensors
#[error("Shape mismatch: expected {expected:?}, got {actual:?}")]
ShapeMismatch {
/// Expected shape
expected: Vec<usize>,
/// Actual shape
actual: Vec<usize>,
},
/// Invalid shape specification
#[error("Invalid shape: {reason}")]
InvalidShape {
/// Reason for invalidity
reason: String,
},
/// Data size does not match shape
#[error("Data size {data_size} does not match shape {shape:?} (expected {expected})")]
DataShapeMismatch {
/// Actual data size
data_size: usize,
/// Specified shape
shape: Vec<usize>,
/// Expected size from shape
expected: usize,
},
/// Invalid dimension for operation
#[error("Invalid dimension {dim} for tensor with {ndim} dimensions")]
InvalidDimension {
/// Requested dimension
dim: usize,
/// Number of dimensions
ndim: usize,
},
/// Matrix multiplication dimension mismatch
#[error("Matrix multiplication dimension mismatch: ({m}×{k}) × ({k2}×{n})")]
MatmulDimensionMismatch {
/// Rows in first matrix
m: usize,
/// Columns in first matrix / rows in second matrix
k: usize,
/// Rows in second matrix (should equal k)
k2: usize,
/// Columns in second matrix
n: usize,
},
/// Operation not supported for this tensor type
#[error("Operation '{operation}' not supported: {reason}")]
UnsupportedOperation {
/// Operation name
operation: String,
/// Reason it's not supported
reason: String,
},
/// Trueno backend error
#[error("Trueno backend error: {0}")]
TruenoError(String),
/// Index out of bounds
#[error("Index out of bounds: index {index} for dimension of size {size}")]
IndexOutOfBounds {
/// Requested index
index: usize,
/// Size of dimension
size: usize,
},
/// Model registry error
#[error("Model registry error: {0}")]
RegistryError(String),
/// Model not found in registry
#[error("Model '{0}' not found")]
ModelNotFound(String),
/// Model already registered
#[error("Model '{0}' already registered")]
ModelAlreadyExists(String),
/// MOE routing error
#[error("MOE routing error: {0}")]
MoeError(String),
/// Expert capacity exceeded
#[error("Expert {expert_id} capacity exceeded: {queue_depth}/{capacity}")]
ExpertCapacityExceeded {
/// Expert index
expert_id: usize,
/// Current queue depth
queue_depth: usize,
/// Maximum capacity
capacity: usize,
},
/// Invalid URI format
#[error("Invalid URI: {0}")]
InvalidUri(String),
/// File format error
#[error("Format error: {reason}")]
FormatError {
/// Reason for format error
reason: String,
},
/// IO error
#[error("IO error: {message}")]
IoError {
/// Error message
message: String,
},
/// Connection error (network/HTTP)
#[error("Connection error: {0}")]
ConnectionError(String),
/// GPU compute error
#[error("GPU error: {reason}")]
GpuError {
/// Reason for GPU error
reason: String,
},
/// Invalid configuration error
#[error("Invalid configuration: {0}")]
InvalidConfiguration(String),
/// Inference execution error
#[error("Inference error: {0}")]
InferenceError(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = RealizarError::ShapeMismatch {
expected: vec![3, 3],
actual: vec![2, 2],
};
assert!(err.to_string().contains("Shape mismatch"));
}
#[test]
fn test_error_equality() {
let err1 = RealizarError::InvalidShape {
reason: "Empty shape".to_string(),
};
let err2 = RealizarError::InvalidShape {
reason: "Empty shape".to_string(),
};
assert_eq!(err1, err2);
}
}