Skip to main content

rten_gemm/
errors.rs

1use std::error::Error;
2use std::fmt;
3use std::fmt::Display;
4
5/// Errors with matrix multiplication inputs.
6#[derive(Clone, Debug, PartialEq)]
7pub enum GemmError {
8    /// Number of LHS and RHS batch inputs do not match.
9    BatchSizeMismatch,
10    /// Number of columns in LHS does not match rows of RHS.
11    KSizeMismatch,
12    /// Bias vector length does not match the corresponding output matrix size.
13    WrongBiasSize,
14    /// Quantization parameter size does not match corresponding input size.
15    WrongQuantParamSize,
16    /// The buffer provided for the output is too short.
17    OutputSizeMismatch,
18    /// The data was packed with a kernel that uses a different layout than
19    /// the current kernel.
20    PackedDataKernelMismatch,
21    /// The data was packed with different cache blocking parameters than are
22    /// currently being used.
23    PackedDataBlockingMismatch,
24    /// Block-quantized inputs are not supported for this data type.
25    BlockQuantizedInputNotSupported,
26    /// Quantization element size is not supported.
27    QuantBitsNotSupported,
28}
29
30impl Display for GemmError {
31    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
32        match self {
33            Self::BatchSizeMismatch => {
34                write!(fmt, "batches of `a` and `b` matrices must have same length")
35            }
36            Self::KSizeMismatch => {
37                write!(fmt, "columns of matrix `a` must match rows of matrix `b`")
38            }
39            Self::WrongBiasSize => write!(fmt, "bias vector length is incorrect"),
40            Self::WrongQuantParamSize => {
41                write!(fmt, "quantization parameter size does not match input")
42            }
43            Self::OutputSizeMismatch => write!(fmt, "output buffer has wrong length"),
44            Self::PackedDataKernelMismatch => {
45                write!(fmt, "matrix was packed with a different kernel")
46            }
47            Self::PackedDataBlockingMismatch => {
48                write!(fmt, "matrix was packed with a different blocking size")
49            }
50            Self::BlockQuantizedInputNotSupported => {
51                write!(fmt, "block-quantized inputs not supported for data type")
52            }
53            Self::QuantBitsNotSupported => {
54                write!(
55                    fmt,
56                    "quantized input has an unsupported number of bits per element"
57                )
58            }
59        }
60    }
61}
62
63impl Error for GemmError {}
64
65/// Errors with block-quantized inputs.
66#[derive(Copy, Clone, Debug, PartialEq)]
67pub enum BlockQuantizedError {
68    /// The block size is unsupported.
69    UnsupportedBlockSize,
70    /// The number of bits per element is unsupported.
71    UnsupportedElementSize,
72}
73
74impl Display for BlockQuantizedError {
75    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76        match self {
77            Self::UnsupportedBlockSize => write!(f, "block size is unsupported"),
78            Self::UnsupportedElementSize => write!(f, "unsupported bits-per-element"),
79        }
80    }
81}
82
83impl Error for BlockQuantizedError {}