pub type Result<T> = core::result::Result<T, Error>;
#[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, Clone, Copy, PartialEq, Eq, Hash)]
pub struct WindowLength {
got: usize,
expected: usize,
}
impl WindowLength {
#[inline(always)]
pub const fn new(got: usize, expected: usize) -> Self {
Self { got, expected }
}
#[inline(always)]
pub const fn got(&self) -> usize {
self.got
}
#[inline(always)]
pub const fn expected(&self) -> usize {
self.expected
}
}
#[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(
"audio input has {} samples, but this door takes exactly {} (one window); \
it is neither padded nor truncated",
.0.got(),
.0.expected()
)]
WindowLength(WindowLength),
#[error("audio input contains a non-finite sample at index {0}")]
NonFiniteInput(usize),
#[error(
"model declares a required input `{0}` that this door never supplies; \
it sends `mel` and nothing else, so every prediction would fail"
)]
UnsatisfiableInput(String),
#[error(
"model declares the state buffer `{0}`, and this door predicts through the \
stateless API; a stateful graph needs an `MLState` on every prediction"
)]
UnsatisfiableState(String),
#[error("model output contains a non-finite value at index {0}")]
NonFiniteOutput(usize),
}
#[cfg(test)]
mod tests;