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

97 lines
2.7 KiB
Rust

//! Error types for RTX-NMF operations
use thiserror::Error;
/// NMF-specific errors
#[derive(Error, Debug)]
pub enum NMFError {
#[error("Tensor operation failed: {0}")]
TensorError(#[from] rtx_tensor::error::TensorError),
#[error("Invalid matrix dimensions: expected {expected}, got {actual}")]
InvalidDimensions { expected: String, actual: String },
#[error("Matrix contains negative values at indices: {indices:?}")]
NegativeValues { indices: Vec<(usize, usize)> },
#[error("NMF failed to converge after {iterations} iterations")]
ConvergenceFailure { iterations: usize },
#[error("Invalid number of components: {components}, must be > 0 and < min(m, n)")]
InvalidComponents { components: usize },
#[error("GPU operation failed: {message}")]
GpuError { message: String },
#[error("CUDA kernel compilation failed: {error}")]
KernelCompilationError { error: String },
#[error("Invalid configuration: {message}")]
ConfigurationError { message: String },
#[error("Invalid value: {message}")]
ValueError { message: String },
#[error("IO error: {0}")]
IoError(#[from] std::io::Error),
#[error("Serialization error: {0}")]
SerializationError(#[from] serde_json::Error),
}
impl NMFError {
/// Create a new GPU error
pub fn gpu_error(message: impl Into<String>) -> Self {
Self::GpuError {
message: message.into(),
}
}
/// Create a new kernel compilation error
pub fn kernel_compilation_error(error: impl Into<String>) -> Self {
Self::KernelCompilationError {
error: error.into(),
}
}
/// Create a new configuration error
pub fn configuration_error(message: impl Into<String>) -> Self {
Self::ConfigurationError {
message: message.into(),
}
}
/// Create a new value error
pub fn value_error(message: impl Into<String>) -> Self {
Self::ValueError {
message: message.into(),
}
}
/// Check if error is recoverable (e.g., can retry with different parameters)
pub fn is_recoverable(&self) -> bool {
matches!(
self,
Self::ConvergenceFailure { .. } | Self::InvalidComponents { .. }
)
}
}
/// Result type for NMF operations
pub type Result<T> = std::result::Result<T, NMFError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_creation() {
let error = NMFError::gpu_error("Test GPU error");
assert!(matches!(error, NMFError::GpuError { .. }));
assert!(!error.is_recoverable());
let error = NMFError::ConvergenceFailure { iterations: 100 };
assert!(error.is_recoverable());
}
}