pub struct ConstrainedSampler { /* private fields */ }Expand description
Wraps a crate::sampling_advanced::SamplerChain with a TokenConstraint.
Before each sampling step the logits for disallowed tokens are masked to
-1e9 so they are effectively excluded from the distribution.
Implementations§
Source§impl ConstrainedSampler
impl ConstrainedSampler
Sourcepub fn new(
sampler: SamplerChain,
constraint: Box<dyn TokenConstraint>,
vocab_size: usize,
) -> Self
pub fn new( sampler: SamplerChain, constraint: Box<dyn TokenConstraint>, vocab_size: usize, ) -> Self
Create a new ConstrainedSampler.
Sourcepub fn sample(&mut self, logits: &mut Vec<f32>) -> u32
pub fn sample(&mut self, logits: &mut Vec<f32>) -> u32
Sample the next token, masking logits for disallowed tokens first.
Steps:
- Query the constraint for an allowed-token mask.
- Set
logits[i] = -1e9for everyfalseentry in the mask. - Delegate to the inner sampler chain.
- Call
constraint.advance(token). - Track the token in
self.generated.
Sourcepub fn is_complete(&self) -> bool
pub fn is_complete(&self) -> bool
Returns true if the constraint considers the current output complete.
Sourcepub fn generated_text_len(&self) -> usize
pub fn generated_text_len(&self) -> usize
Number of tokens generated so far.
Sourcepub fn constraint_name(&self) -> &str
pub fn constraint_name(&self) -> &str
Human-readable name of the active constraint.
Auto Trait Implementations§
impl Freeze for ConstrainedSampler
impl !RefUnwindSafe for ConstrainedSampler
impl Send for ConstrainedSampler
impl Sync for ConstrainedSampler
impl Unpin for ConstrainedSampler
impl UnsafeUnpin for ConstrainedSampler
impl !UnwindSafe for ConstrainedSampler
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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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