1use tenferro_tensor::{DType, ErrorKind};
22
23pub use crate::cpu::tlinalg_error::map_error;
24
25#[derive(Debug, thiserror::Error)]
27pub(crate) enum BackendError {
28 #[cfg(feature = "cuda")]
29 #[error("{library} call {call} returned status {status}")]
30 ProviderStatus {
31 library: &'static str,
32 call: &'static str,
33 status: i32,
34 },
35 #[error("{library} routine {routine} returned an invalid workspace: {detail}")]
36 InvalidWorkspace {
37 library: &'static str,
38 routine: &'static str,
39 detail: String,
40 },
41}
42
43#[derive(Debug, thiserror::Error)]
55#[non_exhaustive]
56pub enum Error {
57 #[error("{op} did not converge")]
59 NonConvergence {
60 op: &'static str,
62 },
63 #[error("{op} encountered non-finite {role}")]
65 NonFinite {
66 op: &'static str,
68 role: &'static str,
70 },
71 #[error("{op} is singular")]
73 Singular {
74 op: &'static str,
76 },
77 #[error("{op} does not support dtype {dtype:?}")]
79 UnsupportedDType {
80 op: &'static str,
82 dtype: DType,
84 },
85}
86
87impl Error {
88 #[must_use]
102 pub fn kind(&self) -> ErrorKind {
103 match self {
104 Self::NonConvergence { .. } | Self::NonFinite { .. } | Self::Singular { .. } => {
105 ErrorKind::NumericalFailure
106 }
107 Self::UnsupportedDType { .. } => ErrorKind::Unsupported,
108 }
109 }
110}
111
112pub type Result<T> = std::result::Result<T, Error>;
123
124pub(crate) fn into_tensor_error(op: &'static str, source: Error) -> tenferro_tensor::Error {
126 tenferro_tensor::Error::extension(
127 op,
128 crate::extension::LINALG_EXTENSION_FAMILY_ID,
129 source.kind(),
130 source,
131 )
132}
133
134pub(crate) fn unsupported_dtype(op: &'static str, dtype: DType) -> tenferro_tensor::Error {
136 into_tensor_error(op, Error::UnsupportedDType { op, dtype })
137}
138
139#[cfg(feature = "cuda")]
141pub(crate) fn backend_status(
142 op: &'static str,
143 library: &'static str,
144 call: &'static str,
145 status: i32,
146) -> tenferro_tensor::Error {
147 tenferro_tensor::Error::backend_source(
148 op,
149 BackendError::ProviderStatus {
150 library,
151 call,
152 status,
153 },
154 )
155}
156
157pub(crate) fn invalid_workspace(
161 op: &'static str,
162 library: &'static str,
163 routine: &'static str,
164 detail: impl Into<String>,
165) -> tenferro_tensor::Error {
166 tenferro_tensor::Error::backend_source(
167 op,
168 BackendError::InvalidWorkspace {
169 library,
170 routine,
171 detail: detail.into(),
172 },
173 )
174}
175
176#[cfg(test)]
177mod tests {
178 use std::error::Error as _;
179
180 use super::*;
181
182 #[cfg(feature = "cuda")]
183 #[test]
184 fn provider_status_keeps_typed_backend_source() {
185 let error = backend_status("svd", "cuSOLVER", "cusolverDnSgesvd", 7);
186
187 assert_eq!(error.kind(), ErrorKind::BackendFailure);
188 assert!(matches!(
189 error.source().and_then(|source| source.downcast_ref()),
190 Some(BackendError::ProviderStatus {
191 library: "cuSOLVER",
192 call: "cusolverDnSgesvd",
193 status: 7,
194 })
195 ));
196 }
197
198 #[test]
199 fn invalid_workspace_keeps_typed_backend_source() {
200 let error = invalid_workspace("eigh", "LAPACK", "dsyevd", "query was zero");
201
202 assert_eq!(error.kind(), ErrorKind::BackendFailure);
203 assert!(matches!(
204 error.source().and_then(|source| source.downcast_ref()),
205 Some(BackendError::InvalidWorkspace {
206 library: "LAPACK",
207 routine: "dsyevd",
208 detail,
209 }) if detail == "query was zero"
210 ));
211 }
212}