Skip to main content

mnn_runtime/
error.rs

1//! Error types.
2
3use std::path::PathBuf;
4
5/// Result type returned by this crate.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// MNN runtime error.
9#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum Error {
12    /// Runtime configuration is invalid.
13    #[error("invalid runtime configuration: {0}")]
14    InvalidConfig(String),
15
16    /// The selected backend was not compiled into the binary.
17    #[error("backend `{backend}` is not compiled; enable the `{feature}` Cargo feature")]
18    BackendNotCompiled {
19        /// Stable backend name.
20        backend: &'static str,
21        /// Required Cargo feature.
22        feature: &'static str,
23    },
24
25    /// The feature is enabled but the linked MNN library lacks the backend.
26    #[error("backend `{0}` is not available in the linked MNN library")]
27    BackendUnavailable(&'static str),
28
29    /// A model file could not be read.
30    #[error("failed to read model `{path}`: {source}")]
31    ModelRead {
32        /// Model path.
33        path: PathBuf,
34        /// Underlying I/O failure.
35        #[source]
36        source: std::io::Error,
37    },
38
39    /// The native MNN layer rejected an operation.
40    #[error("MNN operation `{operation}` failed: {message}")]
41    Native {
42        /// Operation being performed.
43        operation: &'static str,
44        /// Native error details.
45        message: String,
46    },
47
48    /// A tensor name was repeated.
49    #[error("duplicate {kind} tensor `{name}`")]
50    DuplicateTensor {
51        /// Whether this was an input or output.
52        kind: &'static str,
53        /// Tensor name.
54        name: String,
55    },
56
57    /// A required input was not provided.
58    #[error("missing input tensor `{0}`")]
59    MissingInput(String),
60
61    /// The caller provided a tensor not present in the model.
62    #[error("unknown {kind} tensor `{name}`")]
63    UnknownTensor {
64        /// Whether this was an input or output.
65        kind: &'static str,
66        /// Tensor name.
67        name: String,
68    },
69
70    /// Tensor shape did not match model metadata.
71    #[error("tensor `{name}` shape mismatch: expected {expected:?}, got {actual:?}")]
72    ShapeMismatch {
73        /// Tensor name.
74        name: String,
75        /// Shape declared by the model.
76        expected: Vec<i32>,
77        /// Shape supplied by the caller.
78        actual: Vec<usize>,
79    },
80
81    /// Tensor element count does not match its shape.
82    #[error("tensor `{name}` data length mismatch: shape needs {expected} values, got {actual}")]
83    DataLengthMismatch {
84        /// Tensor name.
85        name: String,
86        /// Required element count.
87        expected: usize,
88        /// Supplied element count.
89        actual: usize,
90    },
91
92    /// Shape element count overflowed or exceeded native allocation limits.
93    #[error("tensor `{name}` shape is too large")]
94    ShapeOverflow {
95        /// Tensor name.
96        name: String,
97    },
98
99    /// A shape is not concrete yet.
100    #[error("tensor `{name}` shape is unresolved; supply concrete input dimensions")]
101    UnresolvedShape {
102        /// Tensor name.
103        name: String,
104    },
105
106    /// Concrete shapes must have positive dimensions and rank at most eight.
107    #[error("tensor `{name}` has invalid dimensions {shape:?}")]
108    InvalidShape {
109        /// Tensor name.
110        name: String,
111        /// Supplied dimensions.
112        shape: Vec<usize>,
113    },
114
115    /// Cache directory creation failed.
116    #[error("failed to prepare cache directory `{path}`: {source}")]
117    CacheDirectory {
118        /// Cache directory.
119        path: PathBuf,
120        /// Underlying I/O failure.
121        #[source]
122        source: std::io::Error,
123    },
124
125    /// The bounded model queue is full. The request was not accepted.
126    #[error("model request queue is full")]
127    QueueFull,
128
129    /// A queued request was cancelled or its handle was dropped.
130    #[error("inference request cancelled")]
131    Cancelled,
132
133    /// A request deadline or result wait expired.
134    #[error("inference deadline exceeded")]
135    DeadlineExceeded,
136
137    /// The existing native pool cannot satisfy the requested thread count.
138    #[error(
139        "requested {requested} CPU threads, but MNN provides {effective}; initialize the largest thread budget first"
140    )]
141    ThreadCountLimited {
142        /// Requested thread count.
143        requested: usize,
144        /// Actual count reported by the session.
145        effective: usize,
146    },
147
148    /// A model worker stopped unexpectedly.
149    #[error("model worker stopped unexpectedly")]
150    WorkerStopped,
151
152    /// A model worker panicked during startup.
153    #[error("model worker panicked during startup")]
154    WorkerPanicked,
155
156    /// The process-wide execution gate was poisoned by a panic.
157    #[error("process-wide MNN execution gate is poisoned")]
158    ExecutionGatePoisoned,
159}