baracuda_cutlass/error.rs
1//! Error types for `baracuda-cutlass`.
2
3use thiserror::Error;
4
5/// Crate-local result alias.
6pub type Result<T> = core::result::Result<T, Error>;
7
8/// Errors raised by the safe CUTLASS wrapper.
9///
10/// Re-exported by [`baracuda-kernels`](../../baracuda-kernels) as
11/// `baracuda_kernels::Error` since the kernel facade returns the same
12/// error surface for every op family (the cuSOLVER / cuDNN / cuFFT
13/// facades all map their library-native status codes into one of these
14/// variants).
15///
16/// `#[non_exhaustive]` — error variants have grown every couple of
17/// phases as new failure modes surface (Phase 7 added cuDNN-status
18/// fallback paths; Phase 22 added the cuSOLVER facade plumbing). Match
19/// arms must include a `_ =>` catch-all so adding a new variant
20/// doesn't break downstream `match e { ... }` blocks.
21#[derive(Debug, Error)]
22#[non_exhaustive]
23pub enum Error {
24 /// The requested SKU isn't available in this build.
25 ///
26 /// Either no arch feature is enabled (no kernels were compiled), or the
27 /// problem requires a SKU that's outside the curated v0 set
28 /// (non-RCR layout, unsupported epilogue, etc.).
29 #[error("baracuda-cutlass: requested kernel is unavailable: {0}")]
30 Unsupported(&'static str),
31
32 /// A problem dimension or stride is invalid (e.g., M, N, or K is non-positive).
33 #[error("baracuda-cutlass: invalid problem: {0}")]
34 InvalidProblem(&'static str),
35
36 /// A pointer or stride is misaligned for the selected kernel's tensor-op
37 /// instructions.
38 #[error("baracuda-cutlass: misaligned operand")]
39 MisalignedOperand,
40
41 /// The provided workspace is too small for the selected plan or
42 /// `Workspace::None` was passed when scratch was required.
43 #[error("baracuda-cutlass: workspace too small (need {needed} bytes, got {got})")]
44 WorkspaceTooSmall {
45 /// Required workspace size in bytes.
46 needed: usize,
47 /// Provided workspace size in bytes.
48 got: usize,
49 },
50
51 /// A device buffer is too small for the declared matrix shape and stride.
52 #[error("baracuda-cutlass: buffer too small for declared shape (need {needed} elements, got {got})")]
53 BufferTooSmall {
54 /// Minimum elements required for `(rows, cols, ld)`.
55 needed: usize,
56 /// Elements available in the supplied slice.
57 got: usize,
58 },
59
60 /// CUTLASS reported an internal error during launch (typically a
61 /// kernel-launch failure surfaced through `cudaGetLastError`).
62 #[error("baracuda-cutlass: CUTLASS internal error (status code {0})")]
63 CutlassInternal(i32),
64
65 /// Underlying baracuda-driver error (context, stream, etc.).
66 #[error("baracuda-cutlass: driver error: {0}")]
67 Driver(#[from] baracuda_driver::Error),
68}
69
70/// Convert a status code returned by a `*_run` / `*_can_implement` extern
71/// "C" function into a typed [`Error`].
72///
73/// Handles status 1, 2, 3, and 5 (and any other non-zero) only. Status 4
74/// (workspace too small) is intentionally omitted: by the time the safe
75/// layer launches the kernel it has already pre-checked workspace size
76/// against the plan's reported requirement and returned a typed
77/// [`Error::WorkspaceTooSmall`] *with the actual byte counts*. If a
78/// kernel ever returns status 4 here it indicates a CUTLASS internal
79/// inconsistency between `get_workspace_size` and the runtime requirement
80/// — surfaced as [`Error::CutlassInternal`] to make the bug visible.
81pub(crate) fn status_to_result(code: i32) -> Result<()> {
82 match code {
83 0 => Ok(()),
84 1 => Err(Error::MisalignedOperand),
85 2 => Err(Error::InvalidProblem("CUTLASS reported invalid problem")),
86 3 => Err(Error::Unsupported("CUTLASS reported unsupported configuration")),
87 n => Err(Error::CutlassInternal(n)),
88 }
89}