tenferro_internal_error/lib.rs
1//! Internal shared error definitions for `tenferro` surface crates.
2//!
3//! # Examples
4//!
5//! ```
6//! use tenferro_internal_error::{Error, Result};
7//!
8//! fn require_runtime(ready: bool) -> Result<()> {
9//! if ready {
10//! Ok(())
11//! } else {
12//! Err(Error::RuntimeNotConfigured)
13//! }
14//! }
15//!
16//! assert!(require_runtime(true).is_ok());
17//! assert!(require_runtime(false).is_err());
18//! ```
19
20use thiserror::Error;
21
22/// Shared error type for dynamic `tenferro` surface crates.
23///
24/// # Examples
25///
26/// ```
27/// use tenferro_internal_error::{Error, Result};
28///
29/// fn maybe_fail(flag: bool) -> Result<()> {
30/// if flag {
31/// Ok(())
32/// } else {
33/// Err(Error::InvalidTensorOperands {
34/// message: "demo".into(),
35/// })
36/// }
37/// }
38///
39/// assert!(maybe_fail(true).is_ok());
40/// assert!(maybe_fail(false).is_err());
41/// ```
42#[derive(Debug, Error)]
43pub enum Error {
44 /// The runtime used by builder `.run()` was not configured.
45 #[error("default runtime is not configured; call `set_default_runtime(...)` first")]
46 RuntimeNotConfigured,
47
48 /// Wrapper for backend/linalg/einsum errors from tenferro crates.
49 #[error(transparent)]
50 Backend(#[from] tenferro_device::Error),
51
52 /// Wrapper for AD-rule level errors from `chainrules-core`.
53 #[error(transparent)]
54 Autodiff(#[from] chainrules_core::AutodiffError),
55
56 /// Tensor operands are structurally invalid for the requested operation.
57 #[error("invalid tensor operands: {message}")]
58 InvalidTensorOperands { message: String },
59
60 /// Reverse-mode operands belong to different value graphs.
61 #[error(
62 "reverse-mode operands must share one value graph: expected {expected}, found {found}"
63 )]
64 MixedReverseGraph { expected: u64, found: u64 },
65
66 /// Operation is not available for the currently selected runtime.
67 #[error("operation `{op}` is not supported on runtime `{runtime}`")]
68 UnsupportedRuntimeOp {
69 op: &'static str,
70 runtime: &'static str,
71 },
72
73 /// AD operation is not available for the requested mode.
74 #[error("AD operation `{op}` is not supported for the provided inputs")]
75 UnsupportedAdOp { op: &'static str },
76
77 /// Linalg operation is available only for dense tensor inputs.
78 #[error("linalg operation `{op}` requires dense tensor inputs")]
79 UnsupportedStructuredLinalg { op: &'static str },
80}
81
82/// Convenience result alias for `tenferro` surface errors.
83///
84/// # Examples
85///
86/// ```
87/// use tenferro_internal_error::{Error, Result};
88///
89/// let ok: Result<i32> = Ok(1);
90/// let err: Result<i32> = Err(Error::InvalidTensorOperands {
91/// message: "sample".into(),
92/// });
93///
94/// assert_eq!(ok.unwrap(), 1);
95/// assert!(err.is_err());
96/// ```
97pub type Result<T> = std::result::Result<T, Error>;
98
99#[cfg(test)]
100mod tests;