Skip to main content

morok_device/
error.rs

1use snafu::Snafu;
2
3pub type Result<T, E = Error> = std::result::Result<T, E>;
4
5#[derive(Debug, Snafu)]
6#[snafu(visibility(pub))]
7pub enum Error {
8    /// Shape of target tensor does not match expected shape.
9    #[snafu(display("shape mismatch: expected {expected:?}, got {actual:?}"))]
10    ShapeMismatch { expected: Vec<usize>, actual: Vec<usize> },
11
12    #[snafu(display("size mismatch: expected {expected}, got {actual}"))]
13    SizeMismatch { expected: usize, actual: usize },
14
15    /// Failed to copy data between host and device.
16    #[snafu(display("copy operation failed: {reason}"))]
17    CopyFailed { reason: String },
18
19    /// Invalid device specification.
20    #[snafu(display("invalid device: {device}"))]
21    InvalidDevice { device: String },
22
23    /// Buffer is not allocated.
24    #[snafu(display("buffer not allocated"))]
25    NotAllocated,
26
27    /// Buffer is not CPU-accessible (device buffers require copyout).
28    #[snafu(display("buffer is not CPU-accessible (device buffers require copyout)"))]
29    NotCpuAccessible,
30
31    /// Element type mismatch.
32    #[snafu(display("type mismatch: buffer has {actual:?}, requested {expected:?}"))]
33    TypeMismatch { expected: morok_dtype::DType, actual: morok_dtype::DType },
34
35    /// Failed to create ndarray view from buffer shape.
36    #[snafu(display("ndarray shape error: {source}"))]
37    NdarrayShape { source: ndarray::ShapeError },
38
39    /// Invalid buffer view parameters.
40    #[snafu(display("invalid view: offset {offset} + size {size} exceeds buffer size {buffer_size}"))]
41    InvalidView { offset: usize, size: usize, buffer_size: usize },
42
43    /// Runtime execution error.
44    #[snafu(display("runtime error: {message}"))]
45    Runtime { message: String },
46
47    #[cfg(feature = "cuda")]
48    /// CUDA-specific errors.
49    #[snafu(display("CUDA error: {source}"))]
50    CudaError { source: cudarc::driver::DriverError },
51}