Files
rustytorch/crates/specialized/rtx-ml-classic/src/error.rs
T
2026-03-04 00:08:42 +00:00

95 lines
2.6 KiB
Rust

//! Error types for the rtx-ml-classic crate
use rtx_runtime::RuntimeError;
use rtx_tensor::TensorError;
use thiserror::Error;
/// Result type alias for ML operations
pub type Result<T> = std::result::Result<T, MLError>;
/// Main error type for classical ML operations
#[derive(Error, Debug)]
pub enum MLError {
/// Tensor operation failed
#[error("Tensor error: {0}")]
Tensor(#[from] TensorError),
/// Runtime error from GPU operations
#[error("Runtime error: {0}")]
Runtime(#[from] RuntimeError),
/// Invalid input parameters
#[error("Invalid parameter: {message}")]
InvalidParameter { message: String },
/// Model not fitted
#[error("Model must be fitted before making predictions")]
ModelNotFitted,
/// Convergence error
#[error("Algorithm failed to converge after {iterations} iterations")]
ConvergenceError { iterations: usize },
/// Dimension mismatch
#[error("Dimension mismatch: expected {expected}, got {actual}")]
DimensionMismatch { expected: String, actual: String },
/// Insufficient data
#[error("Insufficient data: {message}")]
InsufficientData { message: String },
/// GPU operation not supported
#[error("GPU operation not supported: {operation}")]
GPUNotSupported { operation: String },
}
impl MLError {
/// Create invalid parameter error
pub fn invalid_parameter(message: impl Into<String>) -> Self {
Self::InvalidParameter {
message: message.into(),
}
}
/// Create dimension mismatch error
pub fn dimension_mismatch(expected: impl Into<String>, actual: impl Into<String>) -> Self {
Self::DimensionMismatch {
expected: expected.into(),
actual: actual.into(),
}
}
/// Create insufficient data error
pub fn insufficient_data(message: impl Into<String>) -> Self {
Self::InsufficientData {
message: message.into(),
}
}
/// Create invalid input error
pub fn invalid_input(message: impl Into<String>) -> Self {
Self::InvalidParameter {
message: message.into(),
}
}
/// Create model not fitted error
pub fn not_fitted(_message: impl Into<String>) -> Self {
Self::ModelNotFitted
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_creation() {
let err = MLError::invalid_parameter("test message");
assert!(matches!(err, MLError::InvalidParameter { .. }));
let err = MLError::dimension_mismatch("(2, 3)", "(2, 4)");
assert!(matches!(err, MLError::DimensionMismatch { .. }));
}
}