1use std::error::Error as StdError;
2use std::path::PathBuf;
3
4use tenferro_tensor::{DType, ErrorKind, ValidationKind};
5
6pub type BoxError = Box<dyn StdError + Send + Sync + 'static>;
8
9#[derive(Debug, thiserror::Error)]
28pub enum Error {
29 #[error("XLA lowering does not support dtype {dtype:?} in {context}")]
30 UnsupportedDType { dtype: DType, context: &'static str },
31 #[error("XLA lowering does not support ExecOp::{op}: {reason}")]
32 UnsupportedOp {
33 op: &'static str,
34 reason: &'static str,
35 },
36 #[error(
37 "XLA lowering supports only exact static shapes; ExecOp::{op} output {output_index} axis {axis} is {kind}"
38 )]
39 NonStaticShape {
40 op: &'static str,
41 output_index: usize,
42 axis: usize,
43 kind: &'static str,
44 },
45 #[error("invalid XLA program: {message}")]
46 InvalidProgram { message: String },
47 #[error("XLA tensor input/output error: {0}")]
48 Tensor(#[from] tenferro_tensor::Error),
49 #[error("XLA extension standard-op lowering failed: {source}")]
50 ExtensionLowering {
51 #[source]
52 source: tenferro_ops::ext_op::ExtensionLoweringError,
53 },
54 #[error("PJRT support requires enabling the tenferro-xla `pjrt` feature")]
55 PjrtFeatureDisabled,
56 #[error("PJRT execution requires an executor created from a loaded plugin")]
57 PjrtPluginNotLoaded,
58 #[error("PJRT call {call} failed: {message}")]
59 PjrtCall { call: &'static str, message: String },
60 #[error("environment variable {var} is not set; set it to a PJRT plugin .so path")]
61 MissingEnv { var: &'static str },
62 #[error("failed to load PJRT plugin from {path}: {source}")]
63 PluginLoad {
64 path: PathBuf,
65 #[source]
66 source: BoxError,
67 },
68}
69
70impl Error {
71 #[must_use]
85 pub fn kind(&self) -> ErrorKind {
86 match self {
87 Self::UnsupportedDType { .. } | Self::UnsupportedOp { .. } => ErrorKind::Unsupported,
88 Self::NonStaticShape { .. } => ErrorKind::Validation(ValidationKind::ShapeMismatch),
89 Self::InvalidProgram { .. } => ErrorKind::Validation(ValidationKind::InvalidArgument),
90 Self::Tensor(source) => source.kind(),
91 Self::ExtensionLowering { source } => source.kind(),
92 Self::PjrtFeatureDisabled | Self::PjrtPluginNotLoaded | Self::MissingEnv { .. } => {
93 ErrorKind::RuntimeState
94 }
95 Self::PluginLoad { .. } => ErrorKind::Io,
96 Self::PjrtCall { .. } => ErrorKind::BackendFailure,
97 }
98 }
99}
100
101pub type Result<T> = std::result::Result<T, Error>;
115
116#[cfg(test)]
117mod tests;