Files
rustytorch/crates/training/rtx-transformers/src/error.rs
T
Omar SobhandClaude Sonnet 4.6 a08adfbf57 fix(gaps): G4 — re-enable all rtx-transformers Phase 2/3 modules (222 compile errors fixed)
Uncommented all deferred modules in lib.rs and fixed API drift across ~60 files in
9 module groups: continual, curriculum, meta, modular, neural_ode, graph, kan,
perceiver, distributed/pipeline_parallelism.

Common patterns fixed across modules:
- Tensor::randn/zeros/ones([a,b]) → (&[a,b], device)? (slice + Result)
- Result<T, TensorError> → .map_err(Into::into)? in TransformerError contexts
- Device by value → &device references
- &Tensor where Tensor expected → .clone()
- tensor.relu()/tanh()/sigmoid() as methods not ops functions
- Tensor arithmetic returning Result: (a + b)? → (a.clone() + b)?
- shape literals → shape.dims() for Shape type
- sum(n) → sum(Some(n)), mean(None) → mean(&[], false)
- i64 indices → usize where required
- backward(x) → backward(x, None)
- Borrow conflicts on self.field resolved by extracting to locals before mut borrow
- BatchingStats private fields → pub(crate)
- TransformerError::Serialization → ::SerializationError
- Add scalar to tensor: (t + 0.1)? → t.add_scalar(0.1)?

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
2026-06-26 16:08:02 +00:00

385 lines
11 KiB
Rust

//! 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<T> = std::result::Result<T, TransformerError>;
/// 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<S: Into<String>>(message: S) -> Self {
Self::Generic(message.into())
}
/// Create a training error
pub fn training<S: Into<String>>(message: S) -> Self {
Self::Training(message.into())
}
/// Create a gradient accumulation error
pub fn gradient_accumulation<S: Into<String>>(message: S) -> Self {
Self::GradientAccumulation(message.into())
}
/// Create a mixed precision error
pub fn mixed_precision<S: Into<String>>(message: S) -> Self {
Self::MixedPrecision(message.into())
}
/// Create an architecture error
pub fn architecture<S: Into<String>>(message: S) -> Self {
Self::Architecture(message.into())
}
/// Create a tokenization error
pub fn tokenization<S: Into<String>>(message: S) -> Self {
Self::Tokenization(message.into())
}
/// Create an optimization error
pub fn optimization<S: Into<String>>(message: S) -> Self {
Self::Optimization(message.into())
}
/// Create a scheduling error
pub fn scheduling<S: Into<String>>(message: S) -> Self {
Self::Scheduling(message.into())
}
/// Create a tensor operation error
pub fn tensor_op<S: Into<String>>(message: S) -> Self {
Self::TensorOp(message.into())
}
/// Create a configuration error
pub fn config<S: Into<String>>(message: S) -> Self {
Self::Config(message.into())
}
/// Create a revolutionary feature error
pub fn revolutionary<S: Into<String>>(feature: S, message: S) -> Self {
Self::Revolutionary(feature.into(), message.into())
}
/// Create a GPU error
pub fn gpu<S: Into<String>>(message: S) -> Self {
Self::Gpu(message.into())
}
/// Create a distributed training error
pub fn distributed<S: Into<String>>(message: S) -> Self {
Self::Distributed(message.into())
}
/// Create a checkpoint error
pub fn checkpoint<S: Into<String>>(message: S) -> Self {
Self::Checkpoint(message.into())
}
/// Create a data loading error
pub fn data_loading<S: Into<String>>(message: S) -> Self {
Self::DataLoading(message.into())
}
/// Create a validation error
pub fn validation<S: Into<String>>(message: S) -> Self {
Self::Validation(message.into())
}
/// Create a Tensor Core error
pub fn tensor_core<S: Into<String>>(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<S: Into<String>>(message: S) -> Self {
Self::EdgeDevice(message.into())
}
/// Create a federated learning error
pub fn federated_learning<S: Into<String>>(message: S) -> Self {
Self::FederatedLearning(message.into())
}
/// Create a configuration error
pub fn config_error<S: Into<String>>(message: S) -> Self {
Self::ConfigError(message.into())
}
/// Create a neuromorphic error
pub fn neuromorphic_error<S: Into<String>>(message: S) -> Self {
Self::NeuromorphicError(message.into())
}
/// Create an architecture error (alias)
pub fn architecture_error<S: Into<String>>(message: S) -> Self {
Self::ArchitectureError(message.into())
}
/// Create a tokenization error (alias)
pub fn tokenization_error<S: Into<String>>(message: S) -> Self {
Self::TokenizationError(message.into())
}
/// Create an optimizer error (lowercase)
pub fn optimizer<S: Into<String>>(message: S) -> Self {
Self::optimizer(message.into())
}
/// Create a shape mismatch error
pub fn shape_mismatch<S: Into<String>>(message: S) -> Self {
Self::shape_mismatch(message.into())
}
/// Create a dimension error
pub fn dimension<S: Into<String>>(message: S) -> Self {
Self::dimension(message.into())
}
/// Create an invalid input error
pub fn invalid_input<S: Into<String>>(message: S) -> Self {
Self::InvalidInput(message.into())
}
/// Create an invalid parameter error
pub fn invalid_parameter<S: Into<String>>(message: S) -> Self {
Self::InvalidParameter(message.into())
}
/// Create an invalid shape error
pub fn invalid_shape<S: Into<String>>(message: S) -> Self {
Self::InvalidShape(message.into())
}
/// Create a validation error
pub fn validation_error<S: Into<String>>(message: S) -> Self {
Self::ValidationError(message.into())
}
/// Create a serialization error
pub fn serialization_error<S: Into<String>>(message: S) -> Self {
Self::SerializationError(message.into())
}
/// Create a computation error
pub fn computation_error<S: Into<String>>(message: S) -> Self {
Self::ComputationError(message.into())
}
/// Create a KAN networks error
pub fn kan<S: Into<String>>(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<serde_json::Error> 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<bincode::Error> for TransformerError {
fn from(error: bincode::Error) -> Self {
Self::SerializationError(format!("Bincode serialization error: {}", error))
}
}