Skip to main content

ruccl/tensor_device/
error.rs

1use crate::in_process::error::InProcessError;
2use crate::rank::{NetworkError, TopologyError, error::RankError, work::WorkError};
3use ruda_tensor::{DType, ExecutionError};
4use std::error::Error;
5use std::fmt;
6
7#[derive(Debug)]
8pub enum TensorDeviceError {
9    UnsupportedDType(DType),
10    DTypeMismatch { expected: DType, actual: DType },
11    DeviceMismatch,
12    InvalidBuffer(&'static str),
13    InvalidOperation(&'static str),
14    Data(String),
15    Poisoned,
16    Execution(ExecutionError),
17    Rank(RankError),
18    Network(NetworkError),
19    Topology(TopologyError),
20    Work(WorkError),
21    InProcess(InProcessError),
22}
23
24impl fmt::Display for TensorDeviceError {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            Self::UnsupportedDType(dtype) => write!(f, "collective backend does not support {dtype:?}"),
28            Self::DTypeMismatch { expected, actual } => write!(f, "collective dtype mismatch: expected {expected:?}, got {actual:?}"),
29            Self::DeviceMismatch => f.write_str("collective buffer belongs to another device"),
30            Self::InvalidBuffer(message) | Self::InvalidOperation(message) => f.write_str(message),
31            Self::Data(message) => write!(f, "collective tensor data: {message}"),
32            Self::Poisoned => f.write_str("collective tensor buffer lock is poisoned"),
33            Self::Execution(error) => fmt::Display::fmt(error, f),
34            Self::Rank(error) => fmt::Display::fmt(error, f),
35            Self::Network(error) => fmt::Display::fmt(error, f),
36            Self::Topology(error) => fmt::Display::fmt(error, f),
37            Self::Work(error) => fmt::Display::fmt(error, f),
38            Self::InProcess(error) => fmt::Display::fmt(error, f),
39        }
40    }
41}
42
43impl Error for TensorDeviceError {
44    fn source(&self) -> Option<&(dyn Error + 'static)> {
45        match self {
46            Self::Execution(error) => Some(error),
47            Self::Rank(error) => Some(error),
48            Self::Network(error) => Some(error),
49            Self::Topology(error) => Some(error),
50            Self::Work(error) => Some(error),
51            Self::InProcess(error) => Some(error),
52            _ => None,
53        }
54    }
55}
56
57macro_rules! from_error {
58    ($source:ty, $variant:ident) => {
59        impl From<$source> for TensorDeviceError {
60            fn from(error: $source) -> Self { Self::$variant(error) }
61        }
62    };
63}
64from_error!(ExecutionError, Execution);
65from_error!(RankError, Rank);
66from_error!(NetworkError, Network);
67from_error!(TopologyError, Topology);
68from_error!(WorkError, Work);
69from_error!(InProcessError, InProcess);