Skip to main content

ConstrainedSampler

Struct ConstrainedSampler 

Source
pub struct ConstrainedSampler<C: TokenConstraint, S: Sampler> { /* private fields */ }
Expand description

Wraps any Sampler so the constraint’s mask is applied at the front of the sampling path — the drop-in integration seam for real decoding.

§How it plugs in

ConstrainedSampler::try_sample does, in order: read the generated-so-far prefix -> ask the TokenConstraint for the AllowedSet -> mask_logits the raw logits (-inf on disallowed) -> hand the masked row to the inner Sampler (temperature/top-k/top-p/… all run after the mask, exactly as required) -> record the chosen token so the next step’s DecodeState is correct. Because masking happens before the inner sampler, the sampler can only ever pick an allowed token — greedy’s argmax skips -inf, and stochastic softmax gives -inf zero probability.

§Why try_sample is fallible instead of an infallible Sampler impl

Sampler::sample returns a bare u32 — it cannot report “the constraint left nothing to sample”. That case (ConstraintError::NoTokenAllowed) must be an honest error, not a panic and not a silently-wrong token, so the constrained path is deliberately its own fallible method rather than an impl Sampler that would have to swallow the error. Callers drive it through crate::generate::generate_constrained, which threads the error out cleanly.

Implementations§

Source§

impl<C: TokenConstraint, S: Sampler> ConstrainedSampler<C, S>

Source

pub fn new(constraint: C, inner: S) -> Self

Wrap inner so constraint masks its logits every step.

Source

pub fn generated(&self) -> &[u32]

The tokens this sampler has produced so far this decode.

Source

pub fn reset(&mut self)

Forget the generated history so the same sampler can drive a fresh decode (the constraint restarts from an empty prefix).

Source

pub fn try_sample(&mut self, logits: &[f32]) -> Result<u32, ConstraintError>

Mask, then sample — the constrained step. See the type’s docs for the exact ordering it guarantees.

§Errors

ConstraintError::NoTokenAllowed if the constraint masked every in-range token this step (see mask_logits). The generated history is left unchanged on error, so a caller may retry with a widened constraint without corrupting the decode state.

Auto Trait Implementations§

§

impl<C, S> Freeze for ConstrainedSampler<C, S>
where C: Freeze, S: Freeze,

§

impl<C, S> RefUnwindSafe for ConstrainedSampler<C, S>

§

impl<C, S> Send for ConstrainedSampler<C, S>
where C: Send, S: Send,

§

impl<C, S> Sync for ConstrainedSampler<C, S>
where C: Sync, S: Sync,

§

impl<C, S> Unpin for ConstrainedSampler<C, S>
where C: Unpin, S: Unpin,

§

impl<C, S> UnsafeUnpin for ConstrainedSampler<C, S>
where C: UnsafeUnpin, S: UnsafeUnpin,

§

impl<C, S> UnwindSafe for ConstrainedSampler<C, S>
where C: UnwindSafe, S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.