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

168 lines
4.9 KiB
Rust

use serde::{Deserialize, Serialize};
use thiserror::Error;
/// AutoML-specific error types
#[derive(Error, Debug, Clone, Serialize, Deserialize)]
pub enum AutoMLError {
/// Configuration or setup errors
#[error("Configuration error: {0}")]
ConfigurationError(String),
/// Optimization process errors
#[error("Optimization error: {0}")]
OptimizationError(String),
/// Resource constraint violations
#[error("Resource error: {0}")]
ResourceError(String),
/// Model training or evaluation errors
#[error("Model error: {0}")]
ModelError(String),
/// Data validation or preprocessing errors
#[error("Validation error: {0}")]
ValidationError(String),
/// Feature engineering errors
#[error("Preprocessing error: {0}")]
PreprocessingError(String),
/// Pipeline construction or execution errors
#[error("Pipeline error: {0}")]
PipelineError(String),
/// Timeout during optimization
#[error("Timeout error: {0}")]
TimeoutError(String),
/// Serialization/deserialization errors
#[error("Serialization error: {0}")]
SerializationError(String),
/// I/O errors during model persistence
#[error("I/O error: {0}")]
IoError(String),
/// Meta-learning related errors
#[error("Meta-learning error: {0}")]
MetaLearningError(String),
/// Transfer learning errors
#[error("Transfer learning error: {0}")]
TransferLearningError(String),
/// Ensemble building errors
#[error("Ensemble error: {0}")]
EnsembleError(String),
/// Multi-fidelity optimization errors
#[error("Multi-fidelity error: {0}")]
MultiFidelityError(String),
/// Early stopping strategy errors
#[error("Early stopping error: {0}")]
EarlyStoppingError(String),
/// System monitoring errors
#[error("Monitoring error: {0}")]
MonitoringError(String),
/// GPU/CUDA related errors
#[error("GPU error: {0}")]
GpuError(String),
/// External library integration errors
#[error("External library error: {0}")]
ExternalError(String),
}
/// Result type for AutoML operations
pub type AutoMLResult<T> = Result<T, AutoMLError>;
// Conversion from rtx-validation error type
impl From<rtx_validation::ValidationError> for AutoMLError {
fn from(err: rtx_validation::ValidationError) -> Self {
Self::ValidationError(err.to_string())
}
}
// TODO: Implement conversions from other error types once dependencies are stable
// impl From<rtx_preprocessing::error::PreprocessingError> for AutoMLError {
// fn from(err: rtx_preprocessing::error::PreprocessingError) -> Self {
// AutoMLError::PreprocessingError(err.to_string())
// }
// }
// impl From<rtx_ml_classic::error::MLClassicError> for AutoMLError {
// fn from(err: rtx_ml_classic::error::MLClassicError) -> Self {
// AutoMLError::ModelError(err.to_string())
// }
// }
impl From<std::io::Error> for AutoMLError {
fn from(err: std::io::Error) -> Self {
Self::IoError(err.to_string())
}
}
impl From<serde_json::Error> for AutoMLError {
fn from(err: serde_json::Error) -> Self {
Self::SerializationError(err.to_string())
}
}
impl From<tokio::time::error::Elapsed> for AutoMLError {
fn from(err: tokio::time::error::Elapsed) -> Self {
Self::TimeoutError(err.to_string())
}
}
impl From<anyhow::Error> for AutoMLError {
fn from(err: anyhow::Error) -> Self {
Self::ExternalError(err.to_string())
}
}
impl From<rtx_tensor::TensorError> for AutoMLError {
fn from(err: rtx_tensor::TensorError) -> Self {
Self::ModelError(err.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let error = AutoMLError::ConfigurationError("Invalid config".to_string());
assert!(error.to_string().contains("Invalid config"));
}
#[test]
fn test_error_serialization() {
let error = AutoMLError::OptimizationError("Test error".to_string());
let serialized = serde_json::to_string(&error).unwrap();
let deserialized: AutoMLError = serde_json::from_str(&serialized).unwrap();
match deserialized {
AutoMLError::OptimizationError(msg) => assert_eq!(msg, "Test error"),
_ => panic!("Wrong error type after deserialization"),
}
}
#[test]
fn test_error_conversions() {
// Test conversion from std::io::Error
let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "File not found");
let automl_error: AutoMLError = io_error.into();
assert!(matches!(automl_error, AutoMLError::IoError(_)));
// Test conversion from serde_json::Error
let json_error = serde_json::from_str::<serde_json::Value>("invalid json").unwrap_err();
let automl_error: AutoMLError = json_error.into();
assert!(matches!(automl_error, AutoMLError::SerializationError(_)));
}
}