cubecl_matmul/components/
error.rs

1use cubecl_core::{CubeCount, CubeDim, LineSizeError, ir::StorageType};
2use std::fmt::{Debug, Display};
3
4use crate::components::{MatrixLayout, 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 {
31        lhs: StorageType,
32        rhs: StorageType,
33        output: StorageType,
34    },
35
36    /// The required CMMA instruction is not supported for the given element types and tile size.
37    CmmaInstructionUnavailable {
38        lhs: StorageType,
39        rhs: StorageType,
40        output: StorageType,
41        size: Option<TileSize>,
42    },
43
44    /// Impossible to find a supported tile size for the problem.
45    TileSizeNotFound,
46
47    /// The layout of the matmul is unsupported
48    LayoutUnsupported {
49        lhs: MatrixLayout,
50        rhs: MatrixLayout,
51    },
52
53    /// Barrier synchronization is not available in the runtime.
54    BarrierUnavailable,
55
56    /// TMA (Tensor Memory Access) is not available in the runtime.
57    TmaUnavailable,
58
59    /// Dynamic selection of line size is unsupported in the current runtime.
60    DynamicLineSizeUnavailable,
61
62    /// Plane operations like plane_sum are unavailable
63    PlaneOpsUnavailable,
64}
65impl From<MatmulAvailabilityError> for MatmulSetupError {
66    fn from(value: MatmulAvailabilityError) -> Self {
67        Self::Unavailable(value)
68    }
69}
70
71impl From<InvalidConfigError> for MatmulSetupError {
72    fn from(value: InvalidConfigError) -> Self {
73        Self::InvalidConfig(value)
74    }
75}
76
77impl From<LineSizeError> for MatmulSetupError {
78    fn from(value: LineSizeError) -> Self {
79        Self::LineSize(value)
80    }
81}
82
83impl Display for MatmulSetupError {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        write!(f, "{self:?}")
86    }
87}
88
89impl Debug for MatmulSetupError {
90    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
91        match self {
92            MatmulSetupError::Unavailable(err) => {
93                writeln!(
94                    f,
95                    "Unable to launch matmul because a required feature is unavailable: {err:?}"
96                )
97            }
98            MatmulSetupError::InvalidConfig(err) => {
99                writeln!(
100                    f,
101                    "Unable to launch matmul because the config is invalid: {:?}",
102                    err.to_string()
103                )
104            }
105            MatmulSetupError::LineSize(err) => {
106                writeln!(
107                    f,
108                    "Unable to launch matmul because could not find supported line size: {err:?}"
109                )
110            }
111        }
112    }
113}
114
115impl Debug for MatmulAvailabilityError {
116    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117        match self {
118            MatmulAvailabilityError::CubeCountTooBig(count) => {
119                writeln!(f, "Cube count too big {count:?}")
120            }
121            MatmulAvailabilityError::CubeDimTooBig(dim) => {
122                writeln!(f, "Cube dim too big {dim:?}")
123            }
124            MatmulAvailabilityError::PlaneDimUnsupported { plane_dim } => {
125                writeln!(
126                    f,
127                    "Plane dimension unsupported: {plane_dim}. Only 32 & 64 are supported."
128                )
129            }
130            MatmulAvailabilityError::TypesUnavailable { lhs, rhs, output } => {
131                writeln!(
132                    f,
133                    "Types lhs={lhs:?}, rhs={rhs:?} and/or output={output:?} not supported.",
134                )
135            }
136            MatmulAvailabilityError::CmmaInstructionUnavailable {
137                lhs,
138                rhs,
139                output,
140                size: Some(size),
141            } => writeln!(
142                f,
143                "Cmma on lhs {:?} rhs {:?} and output {:?} with shape m={:?}, n={:?}, k={:?} not supported.",
144                lhs,
145                rhs,
146                output,
147                size.m(),
148                size.n(),
149                size.k()
150            ),
151            MatmulAvailabilityError::LayoutUnsupported { lhs, rhs } => {
152                writeln!(
153                    f,
154                    "Cmma with layouts lhs {lhs:?} and rhs {rhs:?} not supported."
155                )
156            }
157            MatmulAvailabilityError::CmmaInstructionUnavailable {
158                lhs,
159                rhs,
160                output,
161                size: None,
162            } => writeln!(
163                f,
164                "Cmma on inputs lhs {lhs:?} rhs {rhs:?} and output {output:?} not supported.",
165            ),
166            MatmulAvailabilityError::BarrierUnavailable => {
167                writeln!(f, "Barrier is not available.")
168            }
169            MatmulAvailabilityError::TmaUnavailable => {
170                writeln!(f, "TMA is not available.")
171            }
172            MatmulAvailabilityError::DynamicLineSizeUnavailable => {
173                writeln!(f, "Dynamic line size is not available.")
174            }
175            MatmulAvailabilityError::PlaneOpsUnavailable => {
176                writeln!(f, "Plane-wide operations like plane_sum are not available.")
177            }
178            MatmulAvailabilityError::TileSizeNotFound => {
179                writeln!(f, "No tile size is available for the problem.")
180            }
181        }
182    }
183}
184
185/// Error that arises from invalid configurations
186pub type InvalidConfigError = Box<dyn Display>;
187
188/// Error that arises from invalid configurations
189pub struct FormattedConfigError {
190    func: Box<dyn Fn() -> String>,
191}
192
193impl FormattedConfigError {
194    #[allow(clippy::new_ret_no_self)]
195    pub fn new<F: Fn() -> String + 'static>(func: F) -> Box<dyn Display> {
196        Box::new(Self {
197            func: Box::new(func),
198        })
199    }
200}
201
202impl Display for FormattedConfigError {
203    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
204        let string = (self.func)();
205        write!(f, "{string}")
206    }
207}