use std::fmt;
pub type Result<T> = std::result::Result<T, UcciError>;
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum UcciError {
Empty {
what: &'static str,
},
LengthMismatch {
what: &'static str,
left: usize,
right: usize,
},
NonFinite {
what: &'static str,
index: usize,
},
OutOfRange {
what: &'static str,
index: usize,
value: f64,
expected: &'static str,
},
TooFewCandidates {
token: usize,
found: usize,
},
InvalidParameter {
name: &'static str,
reason: String,
},
Infeasible {
tau: f64,
best_accuracy: f64,
best_theta: f64,
},
OverBudget {
budget: f64,
min_cost: f64,
cheapest_theta: f64,
},
InvalidCalibrator {
reason: String,
},
InvalidRouter {
reason: String,
},
}
impl fmt::Display for UcciError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
UcciError::Empty { what } => write!(f, "{what} must not be empty"),
UcciError::LengthMismatch { what, left, right } => {
write!(
f,
"{what} must have the same length, got {left} and {right}"
)
}
UcciError::NonFinite { what, index } => {
write!(f, "{what} contains NaN or inf (at index {index})")
}
UcciError::OutOfRange {
what,
index,
value,
expected,
} => write!(
f,
"{what} must be {expected}, got {value} (at index {index})"
),
UcciError::TooFewCandidates { token, found } => write!(
f,
"token {token}: need the top-2 log-probs, got {found}; \
request top_logprobs >= 2 (OpenAI API) or logprobs >= 2 (vLLM)"
),
UcciError::InvalidParameter { name, reason } => write!(f, "invalid {name}: {reason}"),
UcciError::Infeasible {
tau,
best_accuracy,
best_theta,
} => write!(
f,
"no threshold on the grid reaches tau={tau}; the best accuracy on the grid is \
{best_accuracy} (theta={best_theta}). Lower tau or check that the large model \
beats the small one on this split."
),
UcciError::OverBudget {
budget,
min_cost,
cheapest_theta,
} => write!(
f,
"no threshold on the grid has cost <= budget={budget}; the cheapest costs \
{min_cost} (theta={cheapest_theta})"
),
UcciError::InvalidCalibrator { reason } => write!(f, "invalid calibrator: {reason}"),
UcciError::InvalidRouter { reason } => write!(f, "invalid router: {reason}"),
}
}
}
impl std::error::Error for UcciError {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display_messages_are_informative() {
let e = UcciError::Infeasible {
tau: 0.95,
best_accuracy: 0.9312,
best_theta: 0.1,
};
assert!(e
.to_string()
.starts_with("no threshold on the grid reaches tau=0.95; the best accuracy on the grid is 0.9312 (theta=0.1)"));
let e = UcciError::OverBudget {
budget: 1.5,
min_cost: 2.0,
cheapest_theta: 1.0,
};
assert_eq!(
e.to_string(),
"no threshold on the grid has cost <= budget=1.5; the cheapest costs 2 (theta=1)"
);
let e = UcciError::TooFewCandidates { token: 3, found: 1 };
assert!(e.to_string().contains("token 3"));
let e = UcciError::LengthMismatch {
what: "u and e",
left: 2,
right: 3,
};
assert_eq!(
e.to_string(),
"u and e must have the same length, got 2 and 3"
);
let e = UcciError::OutOfRange {
what: "probability",
index: 1,
value: 1.5,
expected: "in [0, 1]",
};
assert_eq!(
e.to_string(),
"probability must be in [0, 1], got 1.5 (at index 1)"
);
}
#[test]
fn is_std_error() {
fn takes_error(_: &dyn std::error::Error) {}
takes_error(&UcciError::Empty { what: "u" });
}
}