Skip to main content

cubek_convolution/components/
error.rs

1use core::fmt::Debug;
2use cubecl::server::LaunchError;
3use cubek_matmul::definition::{MatmulAvailabilityError, MatmulSetupError};
4use cubek_std::InvalidConfigError;
5use cubek_tile::launch::Refusal;
6
7#[allow(clippy::large_enum_variant)]
8pub enum ConvSetupError {
9    Matmul(MatmulSetupError),
10    Groups(usize),
11    /// The depthwise routine was handed a convolution that is not depthwise: it requires
12    /// `groups == in_channels == out_channels`, one filter per channel.
13    NotDepthwise {
14        groups: usize,
15        input_channels: usize,
16        output_channels: usize,
17        weight_channels: usize,
18        weight_group_channels: usize,
19    },
20    /// A caller-provided convolution strategy cannot form valid launch geometry.
21    InvalidConfig(InvalidConfigError),
22    Unknown,
23    Launch(LaunchError),
24}
25
26impl From<LaunchError> for ConvSetupError {
27    fn from(value: LaunchError) -> Self {
28        Self::Launch(value)
29    }
30}
31
32impl Debug for ConvSetupError {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        match self {
35            ConvSetupError::Matmul(err) => {
36                write!(f, "{err:?}")
37            }
38            ConvSetupError::Groups(groups) => {
39                writeln!(
40                    f,
41                    "Unable to launch matmul because groups must be one, is actually {groups}",
42                )
43            }
44            ConvSetupError::NotDepthwise {
45                groups,
46                input_channels,
47                output_channels,
48                weight_channels,
49                weight_group_channels,
50            } => writeln!(
51                f,
52                "Unable to launch the depthwise convolution because it needs one filter per \
53                 channel, but groups is {groups}, input channels is {input_channels}, output \
54                 channels is {output_channels}, weight channels is {weight_channels}, and weight \
55                 channels per group is {weight_group_channels}",
56            ),
57            ConvSetupError::InvalidConfig(err) => {
58                write!(f, "Invalid convolution config: {err}")
59            }
60            ConvSetupError::Unknown => write!(f, "Unknown"),
61            ConvSetupError::Launch(err) => write!(f, "Launch error {err:?}"),
62        }
63    }
64}
65
66impl From<MatmulSetupError> for ConvSetupError {
67    fn from(value: MatmulSetupError) -> Self {
68        Self::Matmul(value)
69    }
70}
71
72/// A launch the tile engine refused is a configuration this routine cannot run.
73impl From<Refusal> for ConvSetupError {
74    fn from(refusal: Refusal) -> Self {
75        Self::InvalidConfig(Box::new(refusal))
76    }
77}
78
79impl From<MatmulAvailabilityError> for ConvSetupError {
80    fn from(value: MatmulAvailabilityError) -> Self {
81        Self::Matmul(MatmulSetupError::Unavailable(value))
82    }
83}
84
85#[allow(clippy::from_over_into)]
86impl Into<String> for ConvSetupError {
87    fn into(self) -> String {
88        format!("{self:?}")
89    }
90}