Skip to main content

tenferro_xla/
error.rs

1use std::path::PathBuf;
2
3use tenferro_tensor::DType;
4
5/// Error type for StableHLO lowering and runtime PJRT plugin loading.
6///
7/// # Examples
8///
9/// ```
10/// use tenferro_runtime::{DType, GraphCompiler, TracedTensor};
11/// use tenferro_xla::{lower_to_stablehlo, Error};
12///
13/// let x = TracedTensor::input_symbolic_shape(DType::I64, 1).unwrap();
14/// let mut compiler = GraphCompiler::new();
15/// let y = x.neg().unwrap();
16/// let program = compiler
17///     .compile_with_input_specs(&y, &[(&x, DType::I64, &[2])])
18///     .unwrap();
19/// let err = lower_to_stablehlo(&program).unwrap_err();
20/// assert!(matches!(err, Error::UnsupportedDType { .. }));
21/// ```
22#[derive(Debug, thiserror::Error)]
23pub enum Error {
24    #[error("XLA lowering does not support dtype {dtype:?} in {context}")]
25    UnsupportedDType { dtype: DType, context: &'static str },
26    #[error("XLA lowering does not support ExecOp::{op}: {reason}")]
27    UnsupportedOp {
28        op: &'static str,
29        reason: &'static str,
30    },
31    #[error(
32        "XLA lowering supports only exact static shapes; ExecOp::{op} output {output_index} axis {axis} is {kind}"
33    )]
34    NonStaticShape {
35        op: &'static str,
36        output_index: usize,
37        axis: usize,
38        kind: &'static str,
39    },
40    #[error("invalid XLA program: {message}")]
41    InvalidProgram { message: String },
42    #[error("PJRT support requires enabling the tenferro-xla `pjrt` feature")]
43    PjrtFeatureDisabled,
44    #[error("PJRT execution requires an executor created from a loaded plugin")]
45    PjrtPluginNotLoaded,
46    #[error("PJRT call {call} failed: {message}")]
47    PjrtCall { call: &'static str, message: String },
48    #[error("environment variable {var} is not set; set it to a PJRT plugin .so path")]
49    MissingEnv { var: &'static str },
50    #[error("failed to load PJRT plugin from {path}: {message}")]
51    PluginLoad { path: PathBuf, message: String },
52}
53
54/// Result alias for `tenferro-xla`.
55///
56/// # Examples
57///
58/// ```
59/// use tenferro_xla::Result;
60///
61/// fn ok() -> Result<()> {
62///     Ok(())
63/// }
64///
65/// ok().unwrap();
66/// ```
67pub type Result<T> = std::result::Result<T, Error>;