cubecl_matmul/components/
error.rs

1use cubecl_core::{CubeCount, CubeDim, LineSizeError, ir::Elem};
2use std::fmt::{Debug, Display};
3
4use crate::components::TileSize;
5
6/// Errors that can occur during the setup phase of a matmul operation.
7pub enum MatmulSetupError {
8    /// A required hardware or runtime feature is not available.
9    Unavailable(MatmulAvailabilityError),
10
11    /// The provided configuration is invalid or rejected by a component.
12    InvalidConfig(InvalidConfigError),
13
14    /// No compatible line size could be found for the given constraints.
15    LineSize(LineSizeError),
16}
17
18/// A specific feature required for matmul is not available in the current runtime or hardware.
19pub enum MatmulAvailabilityError {
20    /// The requested cube count exceeds what the runtime or hardware supports.
21    CubeCountTooBig(CubeCount),
22
23    /// The requested cube dimensions are too large for the current runtime or hardware.
24    CubeDimTooBig(CubeDim),
25
26    /// The requested plane dimension is not supported.
27    PlaneDimUnsupported { plane_dim: u32 },
28
29    /// The required data types for input or output are not supported.
30    TypesUnavailable { input: Elem, output: Elem },
31
32    /// The required CMMA instruction is not supported for the given element types and tile size.
33    CmmaInstructionUnavailable {
34        input: Elem,
35        output: Elem,
36        size: Option<TileSize>,
37    },
38
39    /// Barrier synchronization is not available in the runtime.
40    BarrierUnavailable,
41
42    /// TMA (Tensor Memory Access) is not available in the runtime.
43    TmaUnavailable,
44
45    /// Dynamic selection of line size is unsupported in the current runtime.
46    DynamicLineSizeUnavailable,
47}
48impl From<MatmulAvailabilityError> for MatmulSetupError {
49    fn from(value: MatmulAvailabilityError) -> Self {
50        Self::Unavailable(value)
51    }
52}
53
54impl From<InvalidConfigError> for MatmulSetupError {
55    fn from(value: InvalidConfigError) -> Self {
56        Self::InvalidConfig(value)
57    }
58}
59
60impl From<LineSizeError> for MatmulSetupError {
61    fn from(value: LineSizeError) -> Self {
62        Self::LineSize(value)
63    }
64}
65
66impl Display for MatmulSetupError {
67    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68        write!(f, "{self:?}")
69    }
70}
71
72impl Debug for MatmulSetupError {
73    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74        match self {
75            MatmulSetupError::Unavailable(err) => {
76                writeln!(
77                    f,
78                    "Unable to launch matmul because a required feature is unavailable: {err:?}"
79                )
80            }
81            MatmulSetupError::InvalidConfig(err) => {
82                writeln!(
83                    f,
84                    "Unable to launch matmul because the config is invalid: {:?}",
85                    err.to_string()
86                )
87            }
88            MatmulSetupError::LineSize(err) => {
89                writeln!(
90                    f,
91                    "Unable to launch matmul because could not find supported line size: {err:?}"
92                )
93            }
94        }
95    }
96}
97
98impl Debug for MatmulAvailabilityError {
99    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100        match self {
101            MatmulAvailabilityError::CubeCountTooBig(count) => {
102                writeln!(f, "Cube count too big {count:?}")
103            }
104            MatmulAvailabilityError::CubeDimTooBig(dim) => {
105                writeln!(f, "Cube dim too big {dim:?}")
106            }
107            MatmulAvailabilityError::PlaneDimUnsupported { plane_dim } => {
108                writeln!(
109                    f,
110                    "Plane dimension unsupported: {plane_dim}. Only 32 & 64 are supported."
111                )
112            }
113            MatmulAvailabilityError::TypesUnavailable { input, output } => {
114                writeln!(
115                    f,
116                    "Types input={input:?} and/or output={output:?} not supported.",
117                )
118            }
119            MatmulAvailabilityError::CmmaInstructionUnavailable {
120                input,
121                output,
122                size: Some(size),
123            } => writeln!(
124                f,
125                "Cmma on inputs {:?} and outputs {:?} with shape m={:?}, n={:?}, k={:?} not supported.",
126                input,
127                output,
128                size.m(),
129                size.n(),
130                size.k()
131            ),
132            MatmulAvailabilityError::CmmaInstructionUnavailable {
133                input,
134                output,
135                size: None,
136            } => writeln!(f, "Cmma on inputs {input:?} and outputs {output:?}.",),
137            MatmulAvailabilityError::BarrierUnavailable => {
138                writeln!(f, "Barrier is not available.")
139            }
140            MatmulAvailabilityError::TmaUnavailable => {
141                writeln!(f, "TMA is not available.")
142            }
143            MatmulAvailabilityError::DynamicLineSizeUnavailable => {
144                writeln!(f, "Dynamic line size is not available.")
145            }
146        }
147    }
148}
149
150/// Error that arises from invalid configurations
151pub type InvalidConfigError = Box<dyn Display>;
152
153/// Error that arises from invalid configurations
154pub struct FormattedConfigError {
155    func: Box<dyn Fn() -> String>,
156}
157
158impl FormattedConfigError {
159    #[allow(clippy::new_ret_no_self)]
160    pub fn new<F: Fn() -> String + 'static>(func: F) -> Box<dyn Display> {
161        Box::new(Self {
162            func: Box::new(func),
163        })
164    }
165}
166
167impl Display for FormattedConfigError {
168    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
169        let string = (self.func)();
170        write!(f, "{string}")
171    }
172}