pub struct AllowListConstraint { /* private fields */ }Expand description
A constraint that forces the generated token sequence to exactly match one of a finite set of allowed token-id sequences (e.g., multiple-choice answers).
At each step only the union of next tokens across all still-active candidates is permitted. A candidate becomes inactive the moment any token in the prefix fails to match. Generation is considered complete once the full token sequence of at least one candidate has been consumed.
§Example
use oxibonsai_runtime::constrained_decoding::{AllowListConstraint, TokenConstraint};
// Two candidates: [10, 20] and [10, 30]
let mut c = AllowListConstraint::new(vec![vec![10, 20], vec![10, 30]]);
// First token: only 10 is allowed (shared prefix)
let mask = c.allowed_tokens(&[], 50).unwrap();
assert!(mask[10]);
assert!(!mask[20]);
assert!(!mask[30]);Implementations§
Trait Implementations§
Source§impl TokenConstraint for AllowListConstraint
impl TokenConstraint for AllowListConstraint
Source§fn allowed_tokens(
&self,
_generated: &[u32],
vocab_size: usize,
) -> Option<Vec<bool>>
fn allowed_tokens( &self, _generated: &[u32], vocab_size: usize, ) -> Option<Vec<bool>>
Returns a bitmask of tokens that are valid next tokens across all still-active
candidates at the current position. Always returns Some (never unconstrained).
Source§fn advance(&mut self, token: u32) -> bool
fn advance(&mut self, token: u32) -> bool
Commits token at the current position.
Any candidate where candidates[i][position] != token (or the candidate is
already exhausted) is deactivated. Returns true when at least one candidate
remains active or a candidate was just completed at this position.
Source§fn is_complete(&self) -> bool
fn is_complete(&self) -> bool
Returns true when the consumed token sequence fully matches at least one
candidate, i.e. position == candidates[i].len() for some active i.
Auto Trait Implementations§
impl Freeze for AllowListConstraint
impl RefUnwindSafe for AllowListConstraint
impl Send for AllowListConstraint
impl Sync for AllowListConstraint
impl Unpin for AllowListConstraint
impl UnsafeUnpin for AllowListConstraint
impl UnwindSafe for AllowListConstraint
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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