1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use std::path::PathBuf;
#[cfg(feature = "coreml")]
use super::CoreMlError;
#[cfg(feature = "cpu")]
use super::CpuError;
#[cfg(feature = "_cuda")]
use super::CudaError;
use super::ExecutionMode;
use super::TensorShapeError;
#[cfg(feature = "_ort")]
use super::ort_runtime::OrtRuntimeError;
/// Errors that can occur while running segmentation or embedding inference
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum InferenceError {
/// The native CPU backend returned an error
#[cfg(feature = "cpu")]
#[cfg_attr(docsrs, doc(cfg(feature = "cpu")))]
#[error(transparent)]
Cpu(#[from] CpuError),
/// ONNX Runtime returned an error
#[cfg(feature = "_ort")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "migraphx", feature = "load-dynamic"))))]
#[error(transparent)]
Ort(#[from] ort::Error),
/// Native CoreML returned an error
#[cfg(feature = "coreml")]
#[cfg_attr(docsrs, doc(cfg(feature = "coreml")))]
#[error(transparent)]
CoreMl(#[from] CoreMlError),
/// The native CUDA backend returned an error
#[cfg(feature = "_cuda")]
#[cfg_attr(docsrs, doc(cfg(feature = "_cuda")))]
#[error(transparent)]
Cuda(#[from] CudaError),
/// A model input or output did not match its tensor shape contract
#[error(transparent)]
Shape(#[from] TensorShapeError),
/// An output array could not be built from the model's flat output
#[error("{context}: invalid output shape: {source}")]
OutputArray {
/// Which output decode step failed
context: &'static str,
/// The ndarray shape error
#[source]
source: ndarray::ShapeError,
},
/// A model returned no output tensor
#[error("{context}: missing output tensor")]
MissingOutput {
/// Which output decode step failed
context: &'static str,
},
/// A batch has more useful rows than the loaded model accepts
#[error("{context}: useful rows {rows} exceed model capacity {capacity}")]
BatchTooLarge {
/// Which batch step failed
context: &'static str,
/// Useful rows requested by the caller
rows: usize,
/// Maximum rows the model accepts
capacity: usize,
},
/// The multi-mask mask count does not match its filterbank rows
#[error(
"multi-mask batch: expected {expected} mask rows for {fbanks} fbank rows, got {actual}"
)]
MaskCountMismatch {
/// Filterbank rows in the batch
fbanks: usize,
/// Mask rows required for those filterbank rows
expected: usize,
/// Mask rows provided by the caller
actual: usize,
},
/// The loaded backend has no model for the requested operation
#[error("{model} is not available for this model inventory and execution mode")]
ModelUnavailable {
/// The model that the operation needs
model: &'static str,
},
/// A scratch buffer was not contiguous in memory
#[error("{context}: buffer was not contiguous")]
NonContiguousBuffer {
/// Which buffer was not contiguous
context: &'static str,
},
/// A lock around a shared session was poisoned by a panic on another thread
#[error("{resource} lock was poisoned")]
LockPoisoned {
/// The resource guarded by the lock
resource: &'static str,
},
/// An inference worker thread panicked
#[error("{worker} worker panicked")]
WorkerPanic {
/// The worker that panicked
worker: &'static str,
},
}
/// Errors that can occur while loading a model or initializing its inference runtime
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ModelLoadError {
/// The native CPU backend failed while loading a model
#[cfg(feature = "cpu")]
#[cfg_attr(docsrs, doc(cfg(feature = "cpu")))]
#[error(transparent)]
Cpu(#[from] CpuError),
/// Hugging Face Hub could not resolve a required model asset
#[cfg(feature = "online")]
#[error(transparent)]
HfHub(#[from] hf_hub::api::sync::ApiError),
/// Requested execution mode is not supported by this build
#[error(transparent)]
UnsupportedExecutionMode(#[from] ExecutionModeError),
/// ONNX Runtime could not be prepared for this process
#[cfg(feature = "_ort")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "migraphx", feature = "load-dynamic"))))]
#[error(transparent)]
Runtime(#[from] OrtRuntimeError),
/// ONNX Runtime returned an error after initialization completed
#[cfg(feature = "_ort")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "migraphx", feature = "load-dynamic"))))]
#[error(transparent)]
Ort(#[from] ort::Error),
/// The native CUDA backend failed while loading a model
#[cfg(feature = "_cuda")]
#[cfg_attr(docsrs, doc(cfg(feature = "_cuda")))]
#[error(transparent)]
Cuda(#[from] CudaError),
/// A required native model asset is missing for the selected execution mode
#[error("{mode} requires native asset `{path}`")]
MissingNativeAsset {
/// The execution mode that requires the asset
mode: ExecutionMode,
/// The missing native weights or compiled model bundle path
path: PathBuf,
},
/// The safetensors weights that the CUDA modes load are missing
#[cfg(feature = "_cuda")]
#[cfg_attr(docsrs, doc(cfg(feature = "_cuda")))]
#[error(
"{mode} requires the CUDA weights `{path}`; export them with `scripts/cuda/export_weights.py --runtime-assets <dir>`"
)]
MissingCudaWeights {
/// The execution mode that requires the weights
mode: ExecutionMode,
/// The missing weights file
path: PathBuf,
},
/// The CUDA model assets could not be downloaded from Hugging Face
#[cfg(all(feature = "_cuda", feature = "online"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "_cuda", feature = "online"))))]
#[error(
"could not download the CUDA model assets for {mode} from Hugging Face: {source}; export them with `scripts/cuda/export_weights.py --runtime-assets <dir>` and load that directory with `from_dir`"
)]
CudaAssetsUnavailable {
/// The execution mode whose assets were requested
mode: ExecutionMode,
/// The Hugging Face Hub error
#[source]
source: hf_hub::api::sync::ApiError,
},
/// A required native model asset exists but failed to load
#[error("{mode} failed to load native asset `{path}`: {message}")]
NativeAssetLoad {
/// The execution mode that requires the asset
mode: ExecutionMode,
/// The compiled CoreML bundle path that failed to load
path: PathBuf,
/// The backend load error
message: String,
},
/// A typed model or execution configuration is invalid
#[error("invalid model configuration: {message}")]
InvalidConfiguration {
/// Boundary validation error
message: String,
},
}
/// Errors from requesting an execution mode that is not supported in the current build
#[derive(Debug, Clone, thiserror::Error)]
#[non_exhaustive]
#[error("{mode} requires the `{feature}` Cargo feature")]
pub struct ExecutionModeError {
pub(super) mode: ExecutionMode,
pub(super) feature: &'static str,
}