use tenferro_tensor::{DType, ErrorKind};
use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub(crate) enum CudaError {
#[error("{op} does not support dtype {dtype:?} on CUDA")]
UnsupportedDType { op: &'static str, dtype: DType },
#[error("{op} is unsupported on CUDA: {detail}")]
UnsupportedOperation {
op: &'static str,
detail: &'static str,
},
#[error("{library} call {call} returned status {status}")]
ProviderStatus {
library: &'static str,
call: &'static str,
status: i32,
},
#[error("{op} workspace size {size} does not fit in usize")]
WorkspaceSizeOverflow { op: &'static str, size: u64 },
#[error("{op} detected division by zero for dtype {dtype:?} on CUDA")]
DivisionByZero { op: &'static str, dtype: DType },
#[error("{op} received a negative integer exponent for dtype {dtype:?} on CUDA")]
NegativeIntegerExponent { op: &'static str, dtype: DType },
}
pub(crate) fn unsupported_dtype(op: &'static str, dtype: DType) -> crate::Error {
crate::Error::extension(
op,
"cuda",
ErrorKind::Unsupported,
CudaError::UnsupportedDType { op, dtype },
)
}
pub(crate) fn division_by_zero(op: &'static str, dtype: DType) -> crate::Error {
crate::Error::extension(
op,
"cuda",
ErrorKind::NumericalFailure,
CudaError::DivisionByZero { op, dtype },
)
}
pub(crate) fn unsupported_operation(op: &'static str, detail: &'static str) -> crate::Error {
crate::Error::extension(
op,
"cuda",
ErrorKind::Unsupported,
CudaError::UnsupportedOperation { op, detail },
)
}
pub(crate) fn provider_status(
op: &'static str,
library: &'static str,
call: &'static str,
status: i32,
) -> crate::Error {
crate::Error::extension(
op,
"cuda",
ErrorKind::BackendFailure,
CudaError::ProviderStatus {
library,
call,
status,
},
)
}
pub(crate) fn workspace_size_overflow(op: &'static str, size: u64) -> crate::Error {
crate::Error::extension(
op,
"cuda",
ErrorKind::BackendFailure,
CudaError::WorkspaceSizeOverflow { op, size },
)
}
pub(crate) fn negative_integer_exponent(op: &'static str, dtype: DType) -> crate::Error {
crate::Error::extension(
op,
"cuda",
ErrorKind::NumericalFailure,
CudaError::NegativeIntegerExponent { op, dtype },
)
}
#[cfg(test)]
mod tests;