cubecl_matmul/components/
error.rs1use cubecl_core::{CubeCount, CubeDim, LineSizeError, ir::Elem};
2use std::fmt::{Debug, Display};
3
4use crate::components::TileSize;
5
6pub enum MatmulSetupError {
8 Unavailable(MatmulAvailabilityError),
10
11 InvalidConfig(InvalidConfigError),
13
14 LineSize(LineSizeError),
16}
17
18pub enum MatmulAvailabilityError {
20 CubeCountTooBig(CubeCount),
22
23 CubeDimTooBig(CubeDim),
25
26 PlaneDimUnsupported { plane_dim: u32 },
28
29 TypesUnavailable { input: Elem, output: Elem },
31
32 CmmaInstructionUnavailable {
34 input: Elem,
35 output: Elem,
36 size: Option<TileSize>,
37 },
38
39 BarrierUnavailable,
41
42 TmaUnavailable,
44
45 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
150pub type InvalidConfigError = Box<dyn Display>;
152
153pub 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}