Skip to main content

laddu_runtime/
error.rs

1use thiserror::Error;
2
3use laddu_compile::ReductionError;
4
5/// Result type returned by runtime operations.
6pub type RuntimeResult<T> = Result<T, RuntimeError>;
7
8/// Error produced while preparing or evaluating a model.
9#[derive(Clone, Debug, Error, PartialEq)]
10pub enum RuntimeError {
11    /// Execution-context configuration failed.
12    #[error(transparent)]
13    Execution(#[from] ExecutionError),
14    /// Memory discovery, planning, or reservation failed.
15    #[error(transparent)]
16    Memory(#[from] laddu_memory::MemoryError),
17    /// An expression requested an event scalar without an event lookup.
18    #[error("event scalar `{0}` was requested, but no event lookup was provided")]
19    MissingEventScalar(String),
20    /// An expression node produced an unexpected value type.
21    #[error("node #{index} expected {expected}, got {actual}")]
22    TypeMismatch {
23        /// Expression-node index.
24        index: usize,
25        /// Expected value type.
26        expected: &'static str,
27        /// Actual value type.
28        actual: &'static str,
29    },
30    /// An expression node had an invalid shape.
31    #[error("node #{index} has invalid shape: {message}")]
32    InvalidShape {
33        /// Expression-node index.
34        index: usize,
35        /// Description of the shape mismatch.
36        message: String,
37    },
38    /// A matrix factorization or solve failed.
39    #[error("matrix solve failed at node #{0}")]
40    SingularMatrix(usize),
41    /// A cache contained the wrong number of slots.
42    #[error("event cache has {actual} slots, expected {expected}")]
43    InvalidCache {
44        /// Expected slot count.
45        expected: usize,
46        /// Actual slot count.
47        actual: usize,
48    },
49    /// A cache was created for an incompatible layout.
50    #[error("event cache was built for a different cache layout")]
51    InvalidCacheLayout,
52    /// A required event column was absent from the schema.
53    #[error("event scalar `{0}` was not found in the event batch schema")]
54    MissingEventColumn(String),
55    /// Dataset access failed.
56    #[error("data error: {0}")]
57    Data(String),
58    /// Parameter validation or lookup failed.
59    #[error("parameter error: {0}")]
60    Parameter(String),
61    /// A JIT-compiled kernel returned a failure status.
62    #[error("JIT kernel execution failed with status {0}")]
63    JitExecution(i32),
64    /// A reduction transform failed.
65    #[error(transparent)]
66    Reduction(#[from] ReductionError),
67    /// Another distributed rank failed during evaluation.
68    #[error("an MPI peer failed during distributed evaluation")]
69    DistributedPeerFailure,
70    /// WebGPU preparation or execution failed.
71    #[error("WGPU execution failed: {0}")]
72    Wgpu(String),
73}
74
75/// Error produced while resolving execution options.
76#[derive(Clone, Debug, Error, PartialEq, Eq)]
77pub enum ExecutionError {
78    /// A fixed thread policy requested zero threads.
79    #[error("fixed thread count must be nonzero")]
80    ZeroThreads,
81    /// A CPU worker pool could not be constructed.
82    #[error("failed to create Rayon thread pool: {0}")]
83    ThreadPool(String),
84    /// The model cannot currently execute on the 32-bit CPU path.
85    #[error("f32 CPU execution currently requires scalar arithmetic over raw event caches")]
86    UnsupportedCpuF32Model,
87    /// CPU gradients are not available at 32-bit precision.
88    #[error("f32 CPU gradients are not implemented yet")]
89    UnsupportedCpuF32Gradient,
90    /// The requested GPU implementation is unavailable.
91    #[error("GPU backend {0:?} is not available")]
92    GpuUnavailable(crate::GpuBackend),
93    /// GPU gradients are not currently available.
94    #[error("GPU gradients are not implemented yet")]
95    UnsupportedGpuGradient,
96    /// The requested reverse-mode configuration is unsupported.
97    #[error("reverse-mode autodiff is currently only supported for f64 CPU execution")]
98    UnsupportedReverseAutodiff,
99    /// JIT execution was requested without the `jit` feature.
100    #[error("CPU JIT execution was requested but the `jit` feature is unavailable")]
101    JitUnavailable,
102}