//! Comprehensive error types for the RTX Transformers crate use rtx_runtime::error::RuntimeError; use thiserror::Error; /// Result type alias for transformer operations pub type Result = std::result::Result; /// Comprehensive error types for transformer operations #[derive(Error, Debug)] pub enum TransformerError { /// Generic errors #[error("Transformer error: {0}")] Generic(String), /// I/O errors #[error("I/O error: {0}")] IoError(#[from] std::io::Error), /// Runtime errors #[error("Runtime error: {0}")] RuntimeError(#[from] RuntimeError), /// Anyhow errors #[error("Error: {0}")] Anyhow(#[from] anyhow::Error), /// Training-related errors #[error("Training error: {0}")] Training(String), /// Gradient accumulation errors #[error("Gradient accumulation error: {0}")] GradientAccumulation(String), /// Mixed precision training errors #[error("Mixed precision error: {0}")] MixedPrecision(String), /// Model architecture errors #[error("Model architecture error: {0}")] Architecture(String), /// Tokenization errors #[error("Tokenization error: {0}")] Tokenization(String), /// Optimization errors #[error("Optimization error: {0}")] Optimization(String), /// Scheduling errors #[error("Scheduling error: {0}")] Scheduling(String), /// Tensor operation errors #[error("Tensor operation error: {0}")] TensorOp(String), /// Configuration errors #[error("Configuration error: {0}")] Config(String), /// Revolutionary feature errors (quantum, neuromorphic, edge) #[error("Revolutionary feature error ({0}): {1}")] Revolutionary(String, String), /// CUDA/GPU errors #[error("GPU error: {0}")] Gpu(String), /// Distributed training errors #[error("Distributed training error: {0}")] Distributed(String), /// Checkpoint save/load errors #[error("Checkpoint error: {0}")] Checkpoint(String), /// Data loading errors #[error("Data loading error: {0}")] DataLoading(String), /// Validation errors #[error("Validation error: {0}")] Validation(String), /// Validation errors (alias for Validation) #[error("Validation error: {0}")] ValidationError(String), /// Not implemented error #[error("Not implemented: {0}")] NotImplemented(String), /// Tensor Core optimization errors #[error("Tensor Core error: {0}")] TensorCoreError(String), /// Insufficient memory error for edge devices #[error("Insufficient memory: required {required}MB, available {available}MB")] InsufficientMemory { required: u64, available: u64 }, /// Edge device capability errors #[error("Edge device error: {0}")] EdgeDevice(String), /// Federated learning coordination errors #[error("Federated learning error: {0}")] FederatedLearning(String), /// Configuration errors for quantum and neuromorphic backends #[error("Configuration error: {0}")] ConfigError(String), /// Neuromorphic processing errors #[error("Neuromorphic error: {0}")] NeuromorphicError(String), /// Architecture errors (alias for Architecture) #[error("Architecture error: {0}")] ArchitectureError(String), /// Tokenization errors (alias for Tokenization) #[error("Tokenization error: {0}")] TokenizationError(String), /// Optimizer errors (lowercase alias for Optimization) #[error("Optimizer error: {0}")] #[allow(non_camel_case_types)] optimizer(String), /// Shape mismatch errors #[error("Shape mismatch: {0}")] #[allow(non_camel_case_types)] shape_mismatch(String), /// Shape mismatch errors (PascalCase alias) #[error("Shape mismatch: {0}")] ShapeMismatch(String), /// Invalid state errors #[error("Invalid state: {0}")] InvalidState(String), /// Dimension errors #[error("Dimension error: {0}")] #[allow(non_camel_case_types)] dimension(String), /// Invalid input errors #[error("Invalid input: {0}")] InvalidInput(String), /// Invalid parameter errors #[error("Invalid parameter: {0}")] InvalidParameter(String), /// Invalid shape errors #[error("Invalid shape: {0}")] InvalidShape(String), /// Serialization errors #[error("Serialization error: {0}")] SerializationError(String), /// Computation errors #[error("Computation error: {0}")] ComputationError(String), /// KAN Networks errors #[error("KAN error: {0}")] KAN(String), /// Configuration errors (alias for Config) #[error("Configuration error: {0}")] Configuration(String), /// Tensor errors from rtx-tensor #[error("Tensor error: {0}")] TensorError(#[from] rtx_tensor::TensorError), /// CUDA kernel errors #[error("CUDA kernel error: {0}")] KernelNotFound(String), /// CUDA runtime errors #[error("CUDA runtime error: {0}")] CudaRuntime(String), } impl TransformerError { /// Create a generic error pub fn generic>(message: S) -> Self { Self::Generic(message.into()) } /// Create a training error pub fn training>(message: S) -> Self { Self::Training(message.into()) } /// Create a gradient accumulation error pub fn gradient_accumulation>(message: S) -> Self { Self::GradientAccumulation(message.into()) } /// Create a mixed precision error pub fn mixed_precision>(message: S) -> Self { Self::MixedPrecision(message.into()) } /// Create an architecture error pub fn architecture>(message: S) -> Self { Self::Architecture(message.into()) } /// Create a tokenization error pub fn tokenization>(message: S) -> Self { Self::Tokenization(message.into()) } /// Create an optimization error pub fn optimization>(message: S) -> Self { Self::Optimization(message.into()) } /// Create a scheduling error pub fn scheduling>(message: S) -> Self { Self::Scheduling(message.into()) } /// Create a tensor operation error pub fn tensor_op>(message: S) -> Self { Self::TensorOp(message.into()) } /// Create a configuration error pub fn config>(message: S) -> Self { Self::Config(message.into()) } /// Create a revolutionary feature error pub fn revolutionary>(feature: S, message: S) -> Self { Self::Revolutionary(feature.into(), message.into()) } /// Create a GPU error pub fn gpu>(message: S) -> Self { Self::Gpu(message.into()) } /// Create a distributed training error pub fn distributed>(message: S) -> Self { Self::Distributed(message.into()) } /// Create a checkpoint error pub fn checkpoint>(message: S) -> Self { Self::Checkpoint(message.into()) } /// Create a data loading error pub fn data_loading>(message: S) -> Self { Self::DataLoading(message.into()) } /// Create a validation error pub fn validation>(message: S) -> Self { Self::Validation(message.into()) } /// Create a Tensor Core error pub fn tensor_core>(message: S) -> Self { Self::TensorCoreError(message.into()) } /// Create an insufficient memory error #[must_use] pub fn insufficient_memory(required: u64, available: u64) -> Self { Self::InsufficientMemory { required, available, } } /// Create an edge device error pub fn edge_device>(message: S) -> Self { Self::EdgeDevice(message.into()) } /// Create a federated learning error pub fn federated_learning>(message: S) -> Self { Self::FederatedLearning(message.into()) } /// Create a configuration error pub fn config_error>(message: S) -> Self { Self::ConfigError(message.into()) } /// Create a neuromorphic error pub fn neuromorphic_error>(message: S) -> Self { Self::NeuromorphicError(message.into()) } /// Create an architecture error (alias) pub fn architecture_error>(message: S) -> Self { Self::ArchitectureError(message.into()) } /// Create a tokenization error (alias) pub fn tokenization_error>(message: S) -> Self { Self::TokenizationError(message.into()) } /// Create an optimizer error (lowercase) pub fn optimizer>(message: S) -> Self { Self::optimizer(message.into()) } /// Create a shape mismatch error pub fn shape_mismatch>(message: S) -> Self { Self::shape_mismatch(message.into()) } /// Create a dimension error pub fn dimension>(message: S) -> Self { Self::dimension(message.into()) } /// Create an invalid input error pub fn invalid_input>(message: S) -> Self { Self::InvalidInput(message.into()) } /// Create an invalid parameter error pub fn invalid_parameter>(message: S) -> Self { Self::InvalidParameter(message.into()) } /// Create an invalid shape error pub fn invalid_shape>(message: S) -> Self { Self::InvalidShape(message.into()) } /// Create a validation error pub fn validation_error>(message: S) -> Self { Self::ValidationError(message.into()) } /// Create a serialization error pub fn serialization_error>(message: S) -> Self { Self::SerializationError(message.into()) } /// Create a computation error pub fn computation_error>(message: S) -> Self { Self::ComputationError(message.into()) } /// Create a KAN networks error pub fn kan>(message: S) -> Self { Self::KAN(message.into()) } } // rtx-tensor errors are now handled by the #[from] attribute on TensorError variant /// Convert from serde JSON errors to SerializationError #[cfg(feature = "serde")] impl From for TransformerError { fn from(error: serde_json::Error) -> Self { Self::SerializationError(format!("JSON serialization error: {}", error)) } } /// Convert from bincode errors to SerializationError #[cfg(feature = "bincode")] impl From for TransformerError { fn from(error: bincode::Error) -> Self { Self::SerializationError(format!("Bincode serialization error: {}", error)) } }