Skip to main content

kopitiam_core/
error.rs

1use crate::{DType, Shape};
2
3/// Every way a Kopitiam Runtime operation can fail.
4///
5/// This is one shared error type across the runtime crates rather than a
6/// per-crate error enum. Inference is a single pipeline — a shape mismatch
7/// deep in a kernel surfaces to the caller who asked for a token — and
8/// threading five `From` conversions through every layer to express that
9/// buys nothing but ceremony.
10///
11/// Note what is *not* here: no `Other(String)` escape hatch. Every variant
12/// carries the structured data needed to explain the failure, because
13/// "InvalidModel: something went wrong" is exactly the kind of error that
14/// costs an afternoon to debug.
15#[derive(Debug, thiserror::Error)]
16pub enum Error {
17    #[error("shape mismatch: expected {expected}, got {actual}")]
18    ShapeMismatch { expected: Shape, actual: Shape },
19
20    #[error("shapes {left} and {right} cannot be broadcast together")]
21    NotBroadcastable { left: Shape, right: Shape },
22
23    #[error("dtype mismatch: expected {expected}, got {actual}")]
24    DTypeMismatch { expected: DType, actual: DType },
25
26    #[error("operation {op} does not support dtype {dtype}")]
27    UnsupportedDType { op: &'static str, dtype: DType },
28
29    #[error(
30        "cannot index a {dtype} tensor elementwise: it packs {block_size} elements per quantized block"
31    )]
32    QuantizedElementAccess { dtype: DType, block_size: usize },
33
34    #[error("storage holds {actual} bytes but shape {shape} of {dtype} needs {expected}")]
35    StorageTooSmall {
36        shape: Shape,
37        dtype: DType,
38        expected: usize,
39        actual: usize,
40    },
41
42    #[error("{count} elements is not a whole number of {dtype} blocks ({block_size} per block)")]
43    PartialQuantizedBlock {
44        dtype: DType,
45        count: usize,
46        block_size: usize,
47    },
48
49    #[error("index {index} is out of bounds for dimension {dim} of length {len}")]
50    IndexOutOfBounds { dim: usize, index: usize, len: usize },
51
52    #[error("malformed {format} model file: {reason}")]
53    MalformedModel {
54        format: &'static str,
55        reason: String,
56    },
57
58    #[error("{format} model uses unsupported feature: {feature}")]
59    UnsupportedModelFeature {
60        format: &'static str,
61        feature: String,
62    },
63
64    #[error("model is missing required tensor {name:?}")]
65    MissingTensor { name: String },
66
67    #[error("io error: {0}")]
68    Io(#[from] std::io::Error),
69}
70
71/// The runtime's result alias.
72pub type Result<T> = std::result::Result<T, Error>;