Skip to main content

cubek_reduce/
error.rs

1use cubecl::{ir::ElemType, server::LaunchError};
2use thiserror::Error;
3
4#[derive(Error, Debug, Clone)]
5/// This error should be caught and properly handled.
6pub enum ReduceError {
7    /// Indicate that the hardware / API doesn't support SIMT plane instructions.
8    #[error(
9        "Trying to launch a kernel using plane instructions, but there are not supported by the hardware."
10    )]
11    PlanesUnavailable,
12    /// When the cube count is bigger than the max supported.
13    #[error("The cube count is larger than the max supported.")]
14    CubeCountTooLarge,
15
16    /// A generic validation error
17    #[error("A generic validation error: {details}")]
18    Validation { details: &'static str },
19
20    /// Indicate that min_plane_dim != max_plane_dim, thus the exact plane_dim is not fixed.
21    #[error(
22        "Trying to launch a kernel using plane instructions, but the min and max plane dimensions are different."
23    )]
24    ImprecisePlaneDim,
25    /// Indicate the axis is too large.
26    #[error("The provided axis ({axis}) must be smaller than the input tensor rank ({rank}).")]
27    InvalidAxis { axis: usize, rank: usize },
28    /// Indicate that the shape of the input tensor is too small for the given input and axis.
29    #[error(
30        "The input reduce axis length (currently {axis_length:?}) should be at least k ({k:?})."
31    )]
32    ReduceAxisTooSmall { axis_length: usize, k: usize },
33    /// Indicate that the shape of the output tensor is invalid for the given input and axis.
34    #[error("The output shape (currently {output_shape:?}) should be {expected_shape:?}.")]
35    MismatchOutputShape {
36        expected_shape: Vec<usize>,
37        output_shape: Vec<usize>,
38    },
39    /// Indicate that the indices output shape does not match the values output shape.
40    #[error(
41        "The indices shape (currently {indices_shape:?}) should match the values shape ({values_shape:?})."
42    )]
43    MismatchIndicesShape {
44        values_shape: Vec<usize>,
45        indices_shape: Vec<usize>,
46    },
47    /// Indicate that the indices output strides do not match the values output strides.
48    #[error(
49        "The indices strides (currently {indices_strides:?}) should match the values strides ({values_strides:?})."
50    )]
51    MismatchIndicesStrides {
52        values_strides: Vec<usize>,
53        indices_strides: Vec<usize>,
54    },
55    /// Indicate the operation cannot report indices alongside its values.
56    #[error(
57        "The operation {operation} has no index to report; reduce_with_indices only supports TopK, ArgTopK, Max, ArgMax, Min and ArgMin."
58    )]
59    IndicesUnsupported { operation: &'static str },
60
61    /// Indicate that we can't launch a shared sum because the atomic addition is not supported.
62    #[error("Atomic add not supported by the client for {0}")]
63    MissingAtomicAdd(ElemType),
64
65    /// The selected blueprint stages more accumulators in shared memory than the
66    /// device allows.
67    #[error(
68        "Reduce blueprint requires {requested} bytes of shared memory, but only {available} bytes are available on the device."
69    )]
70    SharedMemoryOverflow { requested: usize, available: usize },
71
72    /// An error happened during launch.
73    #[error("An error happened during launch\nCaused by:\n  {0}")]
74    Launch(LaunchError),
75}