Skip to main content

silvic_core/
error.rs

1use std::sync::Arc;
2
3/// Every failure silvic reports. Cheap to clone: a collective failure on one rank is surfaced on every call after it.
4#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
5pub enum SilvicError {
6    #[error("invalid parameter: {0}")]
7    Config(Arc<str>),
8    #[error("invalid training data: {0}")]
9    Data(Arc<str>),
10    #[error("device error: {0}")]
11    Device(Arc<str>),
12    #[error("collective failed: {0}")]
13    Collective(Arc<str>),
14    #[error("malformed model: {0}")]
15    Model(Arc<str>),
16    #[error("unsupported: {0}")]
17    Unsupported(Arc<str>),
18    /// The iteration callback asked to stop; carries the callback's reason.
19    #[error("training interrupted: {0}")]
20    Interrupted(Arc<str>),
21    /// A training invariant failed (a bug, never bad input); training stops instead of panicking.
22    #[error("internal invariant violated: {0}")]
23    Internal(Arc<str>),
24}
25
26impl SilvicError {
27    /// Stable label for the Python exception mapping and logs.
28    pub fn kind(&self) -> &'static str {
29        match self {
30            SilvicError::Config(_) => "config",
31            SilvicError::Data(_) => "data",
32            SilvicError::Device(_) => "device",
33            SilvicError::Collective(_) => "collective",
34            SilvicError::Model(_) => "model",
35            SilvicError::Unsupported(_) => "unsupported",
36            SilvicError::Interrupted(_) => "interrupted",
37            SilvicError::Internal(_) => "internal",
38        }
39    }
40
41    pub fn config(msg: impl Into<Arc<str>>) -> Self {
42        SilvicError::Config(msg.into())
43    }
44
45    pub fn data(msg: impl Into<Arc<str>>) -> Self {
46        SilvicError::Data(msg.into())
47    }
48
49    pub fn internal(msg: impl Into<Arc<str>>) -> Self {
50        SilvicError::Internal(msg.into())
51    }
52
53    pub fn device(msg: impl Into<Arc<str>>) -> Self {
54        SilvicError::Device(msg.into())
55    }
56
57    pub fn collective(msg: impl Into<Arc<str>>) -> Self {
58        SilvicError::Collective(msg.into())
59    }
60
61    pub fn model(msg: impl Into<Arc<str>>) -> Self {
62        SilvicError::Model(msg.into())
63    }
64
65    pub fn unsupported(msg: impl Into<Arc<str>>) -> Self {
66        SilvicError::Unsupported(msg.into())
67    }
68}
69
70pub type Result<T, E = SilvicError> = std::result::Result<T, E>;