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
//! Errors returned by embedding, model configuration, and prepared SIMD-dispatch operations.
//!
//! See `docs/service.md` for error boundaries and caller recovery guidance.
use thiserror::Error;
/// **Stable**: external consumers may depend on this; breaking changes require a SemVer bump.
///
/// Errors that can occur during embedding operations.
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum EmbedError {
/// Model not loaded (needs initialization).
#[error("model not loaded: {0}")]
ModelNotLoaded(String),
/// Wrong model loaded (concurrent model switch in progress).
///
/// This can happen when multiple tasks request different models concurrently.
/// The caller should retry with backoff.
#[error("wrong model loaded: expected {expected}, got {actual}")]
WrongModelLoaded {
/// Model that was expected.
expected: String,
/// Model that was actually loaded.
actual: String,
},
/// Model initialization failed.
#[error("model initialization failed: {0}")]
ModelInitialization(String),
/// Embedding inference failed.
#[error("embedding inference failed: {0}")]
InferenceFailed(String),
/// Blocking task failed (panic or cancellation).
///
/// The model cache may be lost; next call will reinitialize.
#[error("task execution failed: {0}")]
TaskFailed(String),
/// Invalid input provided.
#[error("invalid input: {0}")]
InvalidInput(String),
/// Input text exceeds maximum allowed length.
#[error("text too long: {length} bytes exceeds maximum {max} bytes")]
TextTooLong {
/// Actual length in UTF-8 bytes.
length: usize,
/// Maximum allowed length in UTF-8 bytes.
max: usize,
},
/// Dimension mismatch between expected and actual.
#[error("dimension mismatch: expected {expected}, got {actual}")]
DimensionMismatch {
/// Expected dimension.
expected: usize,
/// Actual dimension.
actual: usize,
},
/// Model not supported by this service.
#[error("model not supported: {0}")]
UnsupportedModel(String),
/// Operation not implemented by this service.
///
/// Returned by the default bodies of trait methods that an implementation has not
/// overridden. The message names the service and the operation.
#[error("operation not supported: {0}")]
Unsupported(String),
/// An attestation report is outside the closed public byte bound.
#[error("attestation report size {length} bytes is outside {min}..={max}")]
AttestationReportSize {
/// Actual report length in bytes.
length: usize,
/// Smallest accepted report length in bytes.
min: usize,
/// Largest accepted report length in bytes.
max: usize,
},
/// The independent retained and transient-work pools overflow aggregate accounting.
#[error(
"resource budget overflow: retained {retained_bytes} bytes plus transient work {transient_work_bytes} bytes"
)]
ResourceBudgetOverflow {
/// Retained-pool byte ceiling.
retained_bytes: u64,
/// Transient-work-pool byte ceiling.
transient_work_bytes: u64,
},
/// Internal logic error (count mismatch, unexpected state).
#[error("internal error: {0}")]
Internal(String),
/// A prepared SIMD operation received data at a different quantization tier.
/// See [`docs/design.md`](../docs/design.md#prepared-dispatch-errors) for recovery and failure semantics.
#[error("tier mismatch in {op}: expected {expected:?}, got {actual:?}")]
TierMismatch {
/// Name of the operation where the mismatch was detected.
op: &'static str,
/// Tier the operation required.
expected: crate::simd::QuantizationTier,
/// Tier actually supplied.
actual: crate::simd::QuantizationTier,
},
}
/// **Stable**: result type alias for embedding operations.
pub type Result<T> = std::result::Result<T, EmbedError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = EmbedError::DimensionMismatch {
expected: 384,
actual: 768,
};
assert_eq!(err.to_string(), "dimension mismatch: expected 384, got 768");
}
#[test]
fn test_error_variants() {
let err = EmbedError::ModelNotLoaded("test".into());
assert_eq!(err.to_string(), "model not loaded: test");
let err = EmbedError::WrongModelLoaded {
expected: "small".into(),
actual: "large".into(),
};
assert!(err.to_string().contains("expected small"));
let err = EmbedError::ModelInitialization("failed".into());
assert!(err.to_string().contains("initialization"));
let err = EmbedError::InferenceFailed("oom".into());
assert!(err.to_string().contains("inference"));
let err = EmbedError::TaskFailed("panic".into());
assert!(err.to_string().contains("task"));
let err = EmbedError::InvalidInput("empty".into());
assert!(err.to_string().contains("invalid input"));
let err = EmbedError::UnsupportedModel("gpt4".into());
assert!(err.to_string().contains("not supported"));
let err = EmbedError::Unsupported("probe does not implement count_tokens".into());
assert_eq!(
err.to_string(),
"operation not supported: probe does not implement count_tokens"
);
let err = EmbedError::Internal("bug".into());
assert!(err.to_string().contains("internal"));
let err = EmbedError::TextTooLong {
length: 50000,
max: 32768,
};
assert!(err.to_string().contains("50000"));
assert!(err.to_string().contains("32768"));
}
}