pub enum OpError {
OutOfVocabulary {
id: u32,
vocab_size: usize,
position: usize,
},
ShapeMismatch {
expected: Vec<usize>,
got: Vec<usize>,
},
AllPaddingRow {
row: usize,
},
LengthMismatch {
ids: usize,
mask: usize,
},
ZeroDimension {
which: &'static str,
},
ShapeOverflow {
dims: Vec<usize>,
},
NonBinaryMaskValue {
value: u8,
position: usize,
},
NonFiniteInput {
position: usize,
},
InvalidEpsilon {
eps_bits: u32,
},
}Expand description
Failure modes of the batched SetFit primitives in crate::autograd.
These ops sit on a trust boundary: ids, masks and shapes arrive from tokenizer output today and from model files from Phase 4 onward, so every argument is untrusted. Each variant therefore names the specific condition that failed rather than collapsing to a generic “bad input”.
Two known traps in this repository are exactly what this enum exists to close:
crates/aprender-train/src/transformer/embedding.rssilently zero-fills out-of-vocabulary ids. A zero row is indistinguishable from a legitimately zero embedding downstream, so the corruption never raises an error — it only ever surfaces as unexplained accuracy loss.crates/aprender-core/src/models/bert/embeddings.rsassert!s on over-length input and then slices unchecked.
Neither is acceptable here. Every op that returns this error fails closed: it never panics, never zero-fills, and never lets a NaN into the autograd graph.
Plans 01-03 and 01-09 extend this enum with further variants. Do not
rename the existing ones — they are named in
contracts/setfit-encoder-conformance-v1.yaml.
Variants§
OutOfVocabulary
A token id was at or beyond vocab_size.
position is the index into the FLATTENED B*S id slice, so a caller
can recover (batch, seq) as (position / seq, position % seq).
Fields
ShapeMismatch
A tensor did not have the required shape.
A 0 extent inside expected means unconstrained — it encodes a
rank requirement whose extents the op cannot know in advance. expected: [0, 0] therefore reads as “any 2-D shape”.
Fields
AllPaddingRow
A batch row had no valid (non-padding) position.
This is the checked-denominator guard (D-03). Pooling such a row would
divide by zero, and masking it would produce an all--1e9 softmax row.
LengthMismatch
A mask length did not match the position count implied by the shape.
ids carries the expected element count derived from the batch and
sequence dimensions; mask carries the length actually supplied.
Fields
ZeroDimension
A dimension was zero.
Returned instead of an empty tensor, because an empty tensor silently no-ops every downstream op and the failure then surfaces far from its cause.
ShapeOverflow
A shape product would overflow usize.
Detected with checked_mul before any allocation, so a wrapping
element count can never become an under-sized buffer.
NonBinaryMaskValue
An attention-mask entry was neither 0 nor 1.
A 2 must never be silently treated as “keep”: that would let a
malformed mask quietly widen attention over padding.
NonFiniteInput
An input tensor contained a non-finite value (NaN or ±Inf).
Rejected at the op boundary so a corrupt weight cannot poison every downstream gradient with a NaN that is untraceable to its source.
InvalidEpsilon
The epsilon floor was not a positive, finite number (plan 01-03).
l2_normalize_rows and cosine_similarity_rows divide by
max(norm, eps). A zero, negative, NaN or infinite eps therefore
removes the only guard standing between a zero-norm row and a NaN (or,
with a negative eps, silently flips the sign of a whole row). The floor
is an explicit parameter with no hidden default precisely so that it can
be validated here.
§Why the value is stored as BITS rather than as an f32
Two independent reasons, both of which bite:
OpErrorderivesEq. Anf32field would forbid that derive for the whole enum, changing the API of seven pre-existing variants for the sake of one.NaN != NaNunderPartialEq. Had the variant carried anf32,assert_eq!(err, OpError::InvalidEpsilon { eps: f32::NAN })would be unsatisfiable against a correct implementation — for exactly theNaNinput this variant exists to reject. That is the same class of self-defeating assertion the ENC-04 gradient gate was rewritten to avoid.
OpError::epsilon recovers the original value for display or
inspection.
Implementations§
Trait Implementations§
impl Eq for OpError
Source§impl Error for OpError
impl Error for OpError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
impl StructuralPartialEq for OpError
Auto Trait Implementations§
impl Freeze for OpError
impl RefUnwindSafe for OpError
impl Send for OpError
impl Sync for OpError
impl Unpin for OpError
impl UnsafeUnpin for OpError
impl UnwindSafe for OpError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more