Skip to main content

tenferro_tensor/
error.rs

1//! Runtime error types for tensor execution.
2//!
3//! # Examples
4//!
5//! ```ignore
6//! use tenferro_tensor::Error;
7//!
8//! let err = Error::AxisOutOfBounds {
9//!     op: "dot_general",
10//!     axis: 2,
11//!     rank: 1,
12//! };
13//! assert!(err.to_string().contains("dot_general"));
14//! ```
15
16/// Runtime failures produced by tensor execution backends and helpers.
17///
18/// # Examples
19///
20/// ```ignore
21/// use tenferro_tensor::Error;
22///
23/// let err = Error::MissingValue { slot: 3 };
24/// ```
25#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)]
26pub enum Error {
27    #[error("{op}: axis {axis} out of bounds for rank {rank}")]
28    AxisOutOfBounds {
29        op: &'static str,
30        axis: usize,
31        rank: usize,
32    },
33    #[error("{op}: duplicate {role} axis {axis}")]
34    DuplicateAxis {
35        op: &'static str,
36        axis: usize,
37        role: &'static str,
38    },
39    #[error("{op}: axis {axis} appears in both {first_role} and {second_role}")]
40    AxisRoleConflict {
41        op: &'static str,
42        axis: usize,
43        first_role: &'static str,
44        second_role: &'static str,
45    },
46    #[error("{op}: shape mismatch lhs={lhs:?} rhs={rhs:?}")]
47    ShapeMismatch {
48        op: &'static str,
49        lhs: Vec<usize>,
50        rhs: Vec<usize>,
51    },
52    #[error("{op}: rank mismatch expected {expected}, actual {actual}")]
53    RankMismatch {
54        op: &'static str,
55        expected: usize,
56        actual: usize,
57    },
58    #[error("{op}: dtype mismatch lhs={lhs:?} rhs={rhs:?}")]
59    DTypeMismatch {
60        op: &'static str,
61        lhs: crate::DType,
62        rhs: crate::DType,
63    },
64    #[error("{op}: invalid config: {message}")]
65    InvalidConfig { op: &'static str, message: String },
66    #[error("{op}: backend failure: {message}")]
67    BackendFailure { op: &'static str, message: String },
68    #[error("missing runtime value for slot {slot}")]
69    MissingValue { slot: usize },
70}
71
72/// Result type alias for runtime tensor operations.
73///
74/// # Examples
75///
76/// ```ignore
77/// use tenferro_tensor::{Error, Result};
78///
79/// let output: Result<()> = Err(Error::MissingValue { slot: 0 });
80/// ```
81pub type Result<T> = std::result::Result<T, Error>;