1use std::error;
4use std::fmt;
5pub use std::error::Error as StdError;
7#[derive(Debug)]
9pub enum NeuralError {
10 InvalidArchitecture(String),
12 TrainingError(String),
14 InferenceError(String),
16 SerializationError(String),
18 DeserializationError(String),
20 ValidationError(String),
22 NotImplementedError(String),
24 IOError(String),
26 InvalidArgument(String),
28 ShapeMismatch(String),
30 ComputationError(String),
32 DimensionMismatch(String),
34 DistributedError(String),
36 ConfigError(String),
38 AllocationError(String),
40 DeviceError(String),
42 DeviceNotFound(String),
44 ResourceExhausted(String),
46 InvalidState(String),
48 NotImplemented(String),
50 MemoryError(String),
52 FeatureNotEnabled(String),
54 Other(String),
56}
57impl fmt::Display for NeuralError {
58 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59 match self {
60 NeuralError::InvalidArchitecture(msg) => write!(f, "Invalid architecture: {msg}"),
61 NeuralError::TrainingError(msg) => write!(f, "Training error: {msg}"),
62 NeuralError::InferenceError(msg) => write!(f, "Inference error: {msg}"),
63 NeuralError::SerializationError(msg) => write!(f, "Serialization error: {msg}"),
64 NeuralError::DeserializationError(msg) => write!(f, "Deserialization error: {msg}"),
65 NeuralError::ValidationError(msg) => write!(f, "Validation error: {msg}"),
66 NeuralError::NotImplementedError(msg) => write!(f, "Not implemented: {msg}"),
67 NeuralError::IOError(msg) => write!(f, "IO error: {msg}"),
68 NeuralError::InvalidArgument(msg) => write!(f, "Invalid argument: {msg}"),
69 NeuralError::ShapeMismatch(msg) => write!(f, "Shape mismatch: {msg}"),
70 NeuralError::ComputationError(msg) => write!(f, "Computation error: {msg}"),
71 NeuralError::DimensionMismatch(msg) => write!(f, "Dimension mismatch: {msg}"),
72 NeuralError::DistributedError(msg) => write!(f, "Distributed training error: {msg}"),
73 NeuralError::ConfigError(msg) => write!(f, "Configuration error: {msg}"),
74 NeuralError::AllocationError(msg) => write!(f, "Allocation error: {msg}"),
75 NeuralError::DeviceError(msg) => write!(f, "Device error: {msg}"),
76 NeuralError::DeviceNotFound(msg) => write!(f, "Device not found: {msg}"),
77 NeuralError::ResourceExhausted(msg) => write!(f, "Resource exhausted: {msg}"),
78 NeuralError::InvalidState(msg) => write!(f, "Invalid state: {msg}"),
79 NeuralError::NotImplemented(msg) => write!(f, "Not implemented: {msg}"),
80 NeuralError::MemoryError(msg) => write!(f, "Memory error: {msg}"),
81 NeuralError::FeatureNotEnabled(msg) => write!(f, "Feature not enabled: {msg}"),
82 NeuralError::Other(msg) => write!(f, "Error: {msg}"),
83 }
84 }
85}
86
87impl error::Error for NeuralError {}
88pub type Error = NeuralError;
90pub type Result<T> = std::result::Result<T, Error>;
92#[cfg(not(feature = "gpu"))]
94pub struct DummyGpuBackend;
95impl From<std::io::Error> for NeuralError {
97 fn from(error: std::io::Error) -> Self {
98 NeuralError::IOError(error.to_string())
99 }
100}
101
102impl From<scirs2_core::ndarray::ShapeError> for NeuralError {
104 fn from(error: scirs2_core::ndarray::ShapeError) -> Self {
105 NeuralError::ShapeMismatch(format!("Shape error: {error}"))
106 }
107}