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