Skip to main content

oxibonsai_image/
error.rs

1//! Error type for the `oxibonsai-image` DiT loader.
2
3use oxibonsai_core::error::BonsaiError;
4
5/// Result alias for the DiT loader.
6pub type DitResult<T> = Result<T, DitError>;
7
8/// Errors that can occur while loading or interrogating a FLUX.2 DiT
9/// (`bonsai-image`) GGUF file.
10#[derive(Debug, thiserror::Error)]
11pub enum DitError {
12    /// An underlying I/O error while opening or mapping the GGUF file.
13    #[error("I/O error for {path}: {source}")]
14    Io {
15        /// Path that triggered the error.
16        path: String,
17        /// Underlying I/O error.
18        source: std::io::Error,
19    },
20
21    /// An error from the OxiBonsai core GGUF reader (parse, missing tensor,
22    /// unsupported quant type, byte-slice validation, …).
23    #[error("GGUF error: {0}")]
24    Gguf(#[from] BonsaiError),
25
26    /// The file's `general.architecture` is not `"bonsai-image"`.
27    #[error("unexpected architecture {found:?} (expected {expected:?})")]
28    WrongArchitecture {
29        /// Architecture string actually found in the GGUF metadata.
30        found: String,
31        /// Architecture string that was expected.
32        expected: String,
33    },
34
35    /// A required `bonsai-image.*` metadata key was missing.
36    #[error("missing required metadata key: {key}")]
37    MissingMetadata {
38        /// The metadata key that was missing.
39        key: String,
40    },
41
42    /// A metadata value had an unexpected type or shape.
43    #[error("invalid metadata for {key}: {reason}")]
44    InvalidMetadata {
45        /// The metadata key whose value was invalid.
46        key: String,
47        /// Human-readable reason the value was rejected.
48        reason: String,
49    },
50
51    /// A tensor was found but its stored GGUF type did not match the storage
52    /// convention for its name space (quantized → `TQ2_0_g128`, plain → `BF16`).
53    #[error("tensor {name} has type {found}, expected {expected}")]
54    WrongTensorType {
55        /// Tensor name.
56        name: String,
57        /// GGUF type actually found.
58        found: String,
59        /// GGUF type that was expected.
60        expected: String,
61    },
62
63    /// A tensor's stored dimensionality was unexpected (e.g. a quantized linear
64    /// that was not 2-D).
65    #[error("tensor {name} has {found} dims, expected {expected}")]
66    WrongRank {
67        /// Tensor name.
68        name: String,
69        /// Number of dimensions actually found.
70        found: usize,
71        /// Number of dimensions that was expected.
72        expected: usize,
73    },
74
75    /// A forward-pass shape invariant was violated (e.g. a weight whose
76    /// `(out, in)` did not match the expected feature widths).
77    #[error("forward-pass shape error: {0}")]
78    Shape(String),
79
80    /// An error from the OxiBonsai kernels crate (e.g. the ternary GEMM
81    /// rejecting an unaligned contraction dimension).
82    #[error("kernel error: {0}")]
83    Kernel(String),
84}
85
86impl From<oxibonsai_kernels::KernelError> for DitError {
87    fn from(source: oxibonsai_kernels::KernelError) -> Self {
88        DitError::Kernel(source.to_string())
89    }
90}