Skip to main content

llama_cpp_bindings/mtmd/
mtmd_error.rs

1/// Errors that can occur when initializing MTMD context
2#[derive(thiserror::Error, Debug)]
3pub enum MtmdInitError {
4    /// Failed to create `CString` from input
5    #[error("Failed to create CString: {0}")]
6    CStringError(#[from] std::ffi::NulError),
7    /// MTMD context initialization returned null
8    #[error("MTMD context initialization returned null")]
9    NullResult,
10}
11
12/// Errors that can occur when working with MTMD bitmaps
13#[derive(thiserror::Error, Debug)]
14pub enum MtmdBitmapError {
15    /// Failed to create `CString` from input
16    #[error("Failed to create CString: {0}")]
17    CStringError(#[from] std::ffi::NulError),
18    /// Invalid data size for bitmap
19    #[error("Invalid data size for bitmap")]
20    InvalidDataSize,
21    /// Image dimensions too small for processing (minimum 2x2)
22    #[error("Image dimensions too small: {0}x{1} (minimum 2x2)")]
23    ImageDimensionsTooSmall(u32, u32),
24    /// Bitmap creation returned null
25    #[error("Bitmap creation returned null")]
26    NullResult,
27}
28
29/// Errors that can occur when working with MTMD input chunks collections
30#[derive(thiserror::Error, Debug)]
31pub enum MtmdInputChunksError {
32    /// Input chunks creation returned null
33    #[error("Input chunks creation returned null")]
34    NullResult,
35}
36
37/// Errors that can occur when working with individual MTMD input chunks
38#[derive(thiserror::Error, Debug)]
39pub enum MtmdInputChunkError {
40    /// Input chunk operation returned null
41    #[error("Input chunk operation returned null")]
42    NullResult,
43}
44
45/// Errors that can occur during tokenization
46#[derive(thiserror::Error, Debug)]
47pub enum MtmdTokenizeError {
48    /// Number of bitmaps does not match number of markers in text
49    #[error("Number of bitmaps does not match number of markers")]
50    BitmapCountMismatch,
51    /// Image preprocessing error occurred
52    #[error("Image preprocessing error")]
53    ImagePreprocessingError,
54    /// Failed to create input chunks collection
55    #[error("{0}")]
56    InputChunksError(#[from] MtmdInputChunksError),
57    /// Text contains characters that cannot be converted to C string
58    #[error("Failed to create CString from text: {0}")]
59    CStringError(#[from] std::ffi::NulError),
60    /// Unknown error occurred during tokenization
61    #[error("Unknown error: {0}")]
62    UnknownError(i32),
63}
64
65/// Errors that can occur during encoding
66#[derive(thiserror::Error, Debug)]
67pub enum MtmdEncodeError {
68    /// Encode operation failed
69    #[error("Encode failed with code: {0}")]
70    EncodeFailure(i32),
71}
72
73use crate::mtmd::image_chunk_batch_size_mismatch::ImageChunkBatchSizeMismatch;
74
75/// Errors that can occur during evaluation
76#[derive(thiserror::Error, Debug)]
77pub enum MtmdEvalError {
78    /// Requested batch size exceeds the context's maximum batch size
79    #[error("batch size {requested} exceeds context batch size {context_max}")]
80    BatchSizeExceedsContextLimit {
81        /// The batch size requested in `eval_chunks`
82        requested: i32,
83        /// The maximum batch size configured on the context
84        context_max: u32,
85    },
86    /// An image chunk's token count exceeds the per-decode `n_batch` budget,
87    /// so handing it to `llama_decode` would trip the `GGML_ASSERT`.
88    #[error(
89        "image chunk has {} tokens but n_batch is {}",
90        .0.image_tokens,
91        .0.n_batch,
92    )]
93    ImageChunkExceedsBatchSize(ImageChunkBatchSizeMismatch),
94    /// Evaluation operation failed
95    #[error("Eval failed with code: {0}")]
96    EvalFailure(i32),
97}