Files
rustytorch/crates/integration/rtx-candle/src/error.rs
T
2026-03-04 00:08:42 +00:00

96 lines
2.3 KiB
Rust

//! Error types for Candle integration
use thiserror::Error;
/// Result type for Candle operations
pub type Result<T> = std::result::Result<T, CandleError>;
/// Errors that can occur in Candle integration
#[derive(Debug, Error)]
pub enum CandleError {
/// Failed to load model
#[error("Failed to load model: {0}")]
ModelLoad(String),
/// Failed to create session
#[error("Failed to create session: {0}")]
SessionCreation(String),
/// Inference failed
#[error("Inference failed: {0}")]
Inference(String),
/// Tensor conversion error
#[error("Tensor conversion error: {0}")]
TensorConversion(String),
/// Backend not available
#[error("Backend not available: {0}")]
BackendUnavailable(String),
/// Shape mismatch
#[error("Shape mismatch: expected {expected:?}, got {actual:?}")]
ShapeMismatch {
expected: Vec<usize>,
actual: Vec<usize>,
},
/// Unsupported data type
#[error("Unsupported data type: {0}")]
UnsupportedDType(String),
/// `HuggingFace` Hub error
#[error("Hub error: {0}")]
Hub(String),
/// Tokenization error
#[error("Tokenization error: {0}")]
Tokenization(String),
/// IO error
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
/// Candle core error
#[error("Candle error: {0}")]
Candle(String),
/// `SafeTensors` error
#[error("SafeTensors error: {0}")]
SafeTensors(String),
}
impl CandleError {
/// Create a model load error
pub fn model_load(msg: impl Into<String>) -> Self {
Self::ModelLoad(msg.into())
}
/// Create a tensor conversion error
pub fn tensor_conversion(msg: impl Into<String>) -> Self {
Self::TensorConversion(msg.into())
}
/// Create a hub error
pub fn hub(msg: impl Into<String>) -> Self {
Self::Hub(msg.into())
}
/// Create a shape mismatch error
pub fn shape_mismatch(expected: Vec<usize>, actual: Vec<usize>) -> Self {
Self::ShapeMismatch { expected, actual }
}
}
impl From<candle_core::Error> for CandleError {
fn from(err: candle_core::Error) -> Self {
Self::Candle(err.to_string())
}
}
impl From<safetensors::SafeTensorError> for CandleError {
fn from(err: safetensors::SafeTensorError) -> Self {
Self::SafeTensors(err.to_string())
}
}