ruvector_sparse_inference/
error.rs

1//! Error types for the sparse inference engine.
2
3use thiserror::Error;
4
5/// Result type for sparse inference operations.
6pub type Result<T> = std::result::Result<T, SparseInferenceError>;
7
8/// Main error type for sparse inference operations.
9#[derive(Debug, Error)]
10pub enum SparseInferenceError {
11    /// Error in predictor operations.
12    #[error("Predictor error: {0}")]
13    Predictor(#[from] PredictorError),
14
15    /// Error in model operations.
16    #[error("Model error: {0}")]
17    Model(#[from] ModelError),
18
19    /// Error in inference operations.
20    #[error("Inference error: {0}")]
21    Inference(#[from] InferenceError),
22
23    /// Error in cache operations.
24    #[error("Cache error: {0}")]
25    Cache(String),
26
27    /// Error in quantization operations.
28    #[error("Quantization error: {0}")]
29    Quantization(String),
30
31    /// IO error.
32    #[error("IO error: {0}")]
33    Io(#[from] std::io::Error),
34
35    /// Serialization error.
36    #[error("Serialization error: {0}")]
37    Serialization(String),
38
39    /// GGUF error.
40    #[error("GGUF error: {0}")]
41    Gguf(#[from] GgufError),
42}
43
44/// Errors related to predictor operations.
45#[derive(Debug, Error)]
46pub enum PredictorError {
47    /// Invalid predictor configuration.
48    #[error("Invalid predictor configuration: {0}")]
49    InvalidConfig(String),
50
51    /// Dimension mismatch between input and predictor.
52    #[error("Dimension mismatch: expected {expected}, got {actual}")]
53    DimensionMismatch { expected: usize, actual: usize },
54
55    /// Predictor not calibrated.
56    #[error("Predictor not calibrated")]
57    NotCalibrated,
58
59    /// Invalid rank for low-rank approximation.
60    #[error("Invalid rank: {0}")]
61    InvalidRank(usize),
62
63    /// Calibration failed.
64    #[error("Calibration failed: {0}")]
65    CalibrationFailed(String),
66}
67
68/// Errors related to inference operations.
69#[derive(Debug, Error)]
70pub enum InferenceError {
71    /// Input dimension mismatch.
72    #[error("Input dimension mismatch: expected {expected}, got {actual}")]
73    InputDimensionMismatch { expected: usize, actual: usize },
74
75    /// No active neurons predicted.
76    #[error("No active neurons predicted")]
77    NoActiveNeurons,
78
79    /// Inference failed.
80    #[error("Inference failed: {0}")]
81    Failed(String),
82
83    /// Backend error.
84    #[error("Backend error: {0}")]
85    Backend(String),
86
87    /// Invalid input.
88    #[error("Invalid input: {0}")]
89    InvalidInput(String),
90}
91
92/// Errors related to model loading.
93#[derive(Debug, Error)]
94pub enum ModelError {
95    /// Invalid model configuration.
96    #[error("Invalid model configuration: {0}")]
97    InvalidConfig(String),
98
99    /// Dimension mismatch in model weights.
100    #[error("Weight dimension mismatch: {0}")]
101    WeightDimensionMismatch(String),
102
103    /// Model not loaded.
104    #[error("Model not loaded")]
105    NotLoaded,
106
107    /// Invalid activation type.
108    #[error("Invalid activation type: {0}")]
109    InvalidActivation(String),
110
111    /// Failed to load model.
112    #[error("Failed to load model: {0}")]
113    LoadFailed(String),
114}
115
116/// Errors related to GGUF model loading.
117#[derive(Debug, Error)]
118pub enum GgufError {
119    /// Invalid GGUF file format.
120    #[error("Invalid GGUF format: {0}")]
121    InvalidFormat(String),
122
123    /// IO error during GGUF loading.
124    #[error("GGUF IO error: {0}")]
125    Io(String),
126
127    /// Unsupported tensor type.
128    #[error("Unsupported tensor type: {0}")]
129    UnsupportedTensorType(String),
130
131    /// Invalid tensor type code.
132    #[error("Invalid tensor type: {0}")]
133    InvalidTensorType(u32),
134
135    /// Invalid magic number.
136    #[error("Invalid GGUF magic number: {0:#010X}")]
137    InvalidMagic(u32),
138
139    /// Unsupported GGUF version.
140    #[error("Unsupported GGUF version: {0}")]
141    UnsupportedVersion(u32),
142
143    /// Missing metadata key.
144    #[error("Missing metadata: {0}")]
145    MissingMetadata(String),
146
147    /// Invalid metadata type.
148    #[error("Invalid metadata type: {0}")]
149    InvalidMetadataType(String),
150
151    /// Invalid value type.
152    #[error("Invalid value type: {0}")]
153    InvalidValueType(u32),
154
155    /// Tensor not found.
156    #[error("Tensor not found: {0}")]
157    TensorNotFound(String),
158}
159
160impl From<std::io::Error> for GgufError {
161    fn from(err: std::io::Error) -> Self {
162        GgufError::Io(err.to_string())
163    }
164}
165
166impl From<std::string::FromUtf8Error> for GgufError {
167    fn from(err: std::string::FromUtf8Error) -> Self {
168        GgufError::InvalidFormat(format!("Invalid UTF-8 string: {}", err))
169    }
170}
171
172impl From<serde_json::Error> for SparseInferenceError {
173    fn from(err: serde_json::Error) -> Self {
174        SparseInferenceError::Serialization(err.to_string())
175    }
176}
177
178impl From<String> for SparseInferenceError {
179    fn from(err: String) -> Self {
180        SparseInferenceError::Model(ModelError::LoadFailed(err))
181    }
182}