Skip to main content

laddu_kernel/
error.rs

1use thiserror::Error;
2
3/// Result type for kernel construction and validation.
4pub type KernelResult<T> = Result<T, KernelError>;
5/// Backward-compatible name for [`KernelError`] in IR APIs.
6pub type KernelIrError = KernelError;
7
8/// Errors produced while constructing or validating kernel IR.
9#[derive(Clone, Debug, Error, PartialEq, Eq)]
10pub enum KernelError {
11    /// The IR contained no values.
12    #[error("kernel IR contains no values")]
13    Empty,
14    /// The scalar root identifier was outside the value array.
15    #[error("kernel root value {root} is out of bounds for {len} values")]
16    RootOutOfBounds {
17        /// Invalid root index.
18        root: usize,
19        /// Number of IR values.
20        len: usize,
21    },
22    /// A gradient output identifier was outside the value array.
23    #[error("gradient output value {output} is out of bounds for {len} values")]
24    GradientOutOfBounds {
25        /// Invalid output index.
26        output: usize,
27        /// Number of IR values.
28        len: usize,
29    },
30    /// A cache kernel had no outputs.
31    #[error("cache kernel contains no outputs")]
32    EmptyCacheOutputs,
33    /// A cache output identifier was outside the value array.
34    #[error("cache output value {output} is out of bounds for {len} values")]
35    CacheOutputOutOfBounds {
36        /// Invalid output index.
37        output: usize,
38        /// Number of IR values.
39        len: usize,
40    },
41    /// A gradient output was not real-valued.
42    #[error("gradient output value {output} must be real, but has kind {actual:?}")]
43    GradientKindMismatch {
44        /// Output index.
45        output: usize,
46        /// Actual output kind.
47        actual: crate::ir::KernelValueKind,
48    },
49    /// An instruction referenced a value that did not precede it.
50    #[error("kernel value {value} references non-prior value {operand}")]
51    InvalidOperand {
52        /// Index of the invalid instruction.
53        value: usize,
54        /// Invalid operand index.
55        operand: usize,
56    },
57    /// An instruction requiring operands was empty.
58    #[error("kernel value {value} has no operands for {operation}")]
59    EmptyOperands {
60        /// Index of the invalid instruction.
61        value: usize,
62        /// Operation being validated.
63        operation: &'static str,
64    },
65    /// A value's declared kind did not match its instruction.
66    #[error("kernel value {value} has kind {actual:?}, but its instruction produces {expected:?}")]
67    KindMismatch {
68        /// Index of the invalid value.
69        value: usize,
70        /// Kind produced by the instruction.
71        expected: crate::ir::KernelValueKind,
72        /// Declared kind.
73        actual: crate::ir::KernelValueKind,
74    },
75    /// A value's declared evaluation class did not match its dependencies.
76    #[error("kernel value {value} has class {actual:?}, but its dependencies require {expected:?}")]
77    ClassMismatch {
78        /// Index of the invalid value.
79        value: usize,
80        /// Class required by the dependencies.
81        expected: crate::ir::KernelValueClass,
82        /// Declared class.
83        actual: crate::ir::KernelValueClass,
84    },
85    /// Operand shapes were incompatible with an instruction.
86    #[error("kernel value {value} has invalid operands for {operation}: {message}")]
87    InvalidShape {
88        /// Index of the invalid value.
89        value: usize,
90        /// Operation being validated.
91        operation: &'static str,
92        /// Description of the incompatibility.
93        message: String,
94    },
95}