1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//! The error type returned by fallible operations.
use std::fmt;
use crate::{DType, Device};
/// Why an operation failed.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
/// Operand shapes are incompatible.
ShapeMismatch {
/// The operation that rejected the shapes.
op: &'static str,
/// Shape of the left operand.
lhs: Vec<usize>,
/// Shape of the right operand.
rhs: Vec<usize>,
},
/// Operand element types are incompatible.
DTypeMismatch {
/// The operation that rejected the types.
op: &'static str,
/// Element type of the left operand (or of the array).
lhs: DType,
/// Element type of the right operand (or the requested type).
rhs: DType,
},
/// The number of host values does not match the shape.
SizeMismatch {
/// Element count implied by the shape.
expected: usize,
/// Number of values provided.
actual: usize,
},
/// The operation does not support this element type.
UnsupportedDType {
/// The operation that rejected the type.
op: &'static str,
/// The unsupported element type.
dtype: DType,
},
/// Memory for this many bytes could not be allocated (or exceeds the memory limit).
OutOfMemory(usize),
/// The GPU backend failed (for example, a command buffer error).
Backend(String),
/// The device has no backend yet.
UnsupportedDevice(Device),
/// An argument is invalid for another reason.
InvalidArgument(String),
/// Reading or writing a file failed.
Io(String),
}
/// `Result` with [`Error`] as the error type.
pub type Result<T> = std::result::Result<T, Error>;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ShapeMismatch { op, lhs, rhs } => {
write!(f, "{op}: shape mismatch, {lhs:?} vs {rhs:?}")
}
Self::DTypeMismatch { op, lhs, rhs } => {
write!(f, "{op}: dtype mismatch, {lhs} vs {rhs}")
}
Self::SizeMismatch { expected, actual } => {
write!(
f,
"size mismatch: shape needs {expected} values, got {actual}"
)
}
Self::UnsupportedDType { op, dtype } => write!(f, "{op}: not supported for {dtype}"),
Self::OutOfMemory(n) => write!(f, "out of memory: cannot allocate {n} bytes"),
Self::Backend(msg) => write!(f, "GPU backend error: {msg}"),
Self::UnsupportedDevice(device) => write!(f, "no backend for device {device} yet"),
Self::InvalidArgument(msg) => write!(f, "invalid argument: {msg}"),
Self::Io(msg) => write!(f, "I/O error: {msg}"),
}
}
}
impl std::error::Error for Error {}