pub type Result<T> = core::result::Result<T, Error>;
pub use windit::WinditError;
#[derive(Debug)]
pub struct ContractMismatch {
feature: &'static str,
expected: String,
actual: String,
}
impl ContractMismatch {
#[inline(always)]
pub const fn new(feature: &'static str, expected: String, actual: String) -> Self {
Self {
feature,
expected,
actual,
}
}
#[inline(always)]
pub const fn feature(&self) -> &'static str {
self.feature
}
#[inline(always)]
pub fn expected(&self) -> &str {
&self.expected
}
#[inline(always)]
pub fn actual(&self) -> &str {
&self.actual
}
}
#[derive(Debug)]
pub struct OutputShape {
got: Vec<usize>,
expected: Vec<usize>,
}
impl OutputShape {
#[inline(always)]
pub const fn new(got: Vec<usize>, expected: Vec<usize>) -> Self {
Self { got, expected }
}
#[inline(always)]
pub fn got(&self) -> &[usize] {
&self.got
}
#[inline(always)]
pub fn expected(&self) -> &[usize] {
&self.expected
}
}
#[derive(Debug)]
pub struct EmbeddingDimMismatch {
expected: usize,
got: usize,
}
impl EmbeddingDimMismatch {
#[inline(always)]
pub const fn new(expected: usize, got: usize) -> Self {
Self { expected, got }
}
#[inline(always)]
pub const fn expected(&self) -> usize {
self.expected
}
#[inline(always)]
pub const fn got(&self) -> usize {
self.got
}
}
#[derive(Debug, thiserror::Error)]
#[error("failed to read the artifact tokenizer `{path}`: {source}")]
pub struct ArtifactTokenizerRead {
path: std::path::PathBuf,
#[source]
source: std::io::Error,
}
impl ArtifactTokenizerRead {
#[inline(always)]
pub const fn new(path: std::path::PathBuf, source: std::io::Error) -> Self {
Self { path, source }
}
#[inline(always)]
pub fn path(&self) -> &std::path::Path {
&self.path
}
#[inline(always)]
pub const fn source(&self) -> &std::io::Error {
&self.source
}
}
#[derive(Debug)]
pub struct TokenizerContractMismatch {
check: &'static str,
expected: String,
actual: String,
}
impl TokenizerContractMismatch {
#[inline(always)]
pub const fn new(check: &'static str, expected: String, actual: String) -> Self {
Self {
check,
expected,
actual,
}
}
#[inline(always)]
pub const fn check(&self) -> &'static str {
self.check
}
#[inline(always)]
pub fn expected(&self) -> &str {
&self.expected
}
#[inline(always)]
pub fn actual(&self) -> &str {
&self.actual
}
}
#[derive(Debug)]
pub struct TokenCount {
got: usize,
max: usize,
}
impl TokenCount {
#[inline(always)]
pub const fn new(got: usize, max: usize) -> Self {
Self { got, max }
}
#[inline(always)]
pub const fn got(&self) -> usize {
self.got
}
#[inline(always)]
pub const fn max(&self) -> usize {
self.max
}
}
#[derive(Debug)]
pub struct SpecialTokenOverhead {
added: usize,
window: usize,
}
impl SpecialTokenOverhead {
#[inline(always)]
pub const fn new(added: usize, window: usize) -> Self {
Self { added, window }
}
#[inline(always)]
pub const fn added(&self) -> usize {
self.added
}
#[inline(always)]
pub const fn window(&self) -> usize {
self.window
}
}
#[derive(Debug)]
pub struct WindowOverBudget {
window: usize,
max: usize,
}
impl WindowOverBudget {
#[inline(always)]
pub const fn new(window: usize, max: usize) -> Self {
Self { window, max }
}
#[inline(always)]
pub const fn window(&self) -> usize {
self.window
}
#[inline(always)]
pub const fn max(&self) -> usize {
self.max
}
}
#[derive(Debug)]
pub struct InputTooLarge {
got: usize,
max: usize,
}
impl InputTooLarge {
#[inline(always)]
pub const fn new(got: usize, max: usize) -> Self {
Self { got, max }
}
#[inline(always)]
pub const fn got(&self) -> usize {
self.got
}
#[inline(always)]
pub const fn max(&self) -> usize {
self.max
}
}
#[derive(Debug)]
pub struct ContentlessInputOverBudget {
start: usize,
end: usize,
tokens: usize,
max: usize,
}
impl ContentlessInputOverBudget {
#[inline(always)]
pub const fn new(start: usize, end: usize, tokens: usize, max: usize) -> Self {
Self {
start,
end,
tokens,
max,
}
}
#[inline(always)]
pub const fn start(&self) -> usize {
self.start
}
#[inline(always)]
pub const fn end(&self) -> usize {
self.end
}
#[inline(always)]
pub const fn tokens(&self) -> usize {
self.tokens
}
#[inline(always)]
pub const fn max(&self) -> usize {
self.max
}
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("failed to load model: {0}")]
Load(#[from] crate::LoadError),
#[error("prediction failed: {0}")]
Prediction(#[from] crate::PredictionError),
#[error("tensor failed: {0}")]
Tensor(#[from] crate::TensorError),
#[error(
"model contract mismatch on `{}`: expected {}, got {}",
.0.feature(),
.0.expected(),
.0.actual()
)]
ContractMismatch(ContractMismatch),
#[error("output shape mismatch: expected {:?}, got {:?}", .0.expected(), .0.got())]
OutputShape(OutputShape),
#[error("model output contains a non-finite value at index {0}")]
NonFiniteOutput(usize),
#[error("text input is empty")]
EmptyText,
#[error("embedding dimension mismatch: expected {}, got {}", .0.expected(), .0.got())]
EmbeddingDimMismatch(EmbeddingDimMismatch),
#[error("embedding contains a non-finite value at component {0}")]
NonFiniteEmbedding(usize),
#[error("embedding has zero magnitude and cannot be normalized")]
EmbeddingZero,
#[error("embedding is not unit-norm: |norm² − 1| = {0}")]
EmbeddingNotUnitNorm(f32),
#[error(transparent)]
ArtifactTokenizerRead(#[from] ArtifactTokenizerRead),
#[error("failed to load tokenizer: {0}")]
TokenizerLoad(#[source] tokenizers::Error),
#[error("failed to configure tokenizer: {0}")]
TokenizerConfig(#[source] tokenizers::Error),
#[error(
"tokenizer post-processor adds {} special tokens, leaving no room for text in the {}-token window",
.0.added(),
.0.window()
)]
SpecialTokenOverhead(SpecialTokenOverhead),
#[error("failed to tokenize text: {0}")]
Tokenize(#[source] tokenizers::Error),
#[error(
"tokenizer contract mismatch on `{}`: expected {}, got {}",
.0.check(),
.0.expected(),
.0.actual()
)]
TokenizerContractMismatch(TokenizerContractMismatch),
#[error(
"tokenized input has {} tokens, exceeding the fixed {}-token window",
.0.got(),
.0.max()
)]
TokenCount(TokenCount),
#[error("token id {0} exceeds the model's int32 input range")]
TokenIdRange(u32),
#[error("windowed-sequence processing failed: {0}")]
Windowing(#[from] WinditError),
#[error(
"embed_long window budget {} exceeds the model's fixed {}-token input window",
.0.window(),
.0.max()
)]
WindowOverBudget(WindowOverBudget),
#[error(
"text input is {} bytes, exceeding the configured {}-byte limit",
.0.got(),
.0.max()
)]
InputTooLarge(InputTooLarge),
#[error(
"contentless text at bytes {}..{} measures {} tokens, exceeding the model's fixed {}-token window",
.0.start(),
.0.end(),
.0.tokens(),
.0.max()
)]
ContentlessInputOverBudget(ContentlessInputOverBudget),
}
#[cfg(test)]
mod tests;