pub struct GrammarSampler { /* private fields */ }Expand description
A grammar being applied to one generation, over one vocabulary.
Implementations§
Source§impl GrammarSampler
impl GrammarSampler
Sourcepub fn new(
grammar: Grammar,
n_vocab: usize,
piece_of: impl Fn(usize) -> Vec<u8>,
is_eog: impl Fn(usize) -> bool,
) -> Self
pub fn new( grammar: Grammar, n_vocab: usize, piece_of: impl Fn(usize) -> Vec<u8>, is_eog: impl Fn(usize) -> bool, ) -> Self
Snapshot n_vocab token pieces and their end-of-generation flags,
and start applying grammar over them.
Sourcepub fn vocab_size(&self) -> usize
pub fn vocab_size(&self) -> usize
The vocabulary size this was built over.
Sourcepub fn grammar(&self) -> &Grammar
pub fn grammar(&self) -> &Grammar
The live grammar, for a caller that wants to ask it something.
Sourcepub fn mask_logits(
&self,
logits: &mut [f32],
) -> Result<MaskOutcome, ConstraintError>
pub fn mask_logits( &self, logits: &mut [f32], ) -> Result<MaskOutcome, ConstraintError>
Set every logit the grammar forbids to -inf, in place.
llama_grammar_apply_impl. A logit already at -inf is left
alone and never offered to the grammar: it is forbidden whatever
the grammar thinks, and the walk in reject_candidates is
linear in the candidates it is given.
Sourcepub fn accept(&mut self, token: usize) -> Result<(), ConstraintError>
pub fn accept(&mut self, token: usize) -> Result<(), ConstraintError>
Advance the grammar over a sampled token.
llama_grammar_accept_impl. An end-of-generation token does not
consume characters – it asserts the parse is finished – so it
goes to Grammar::accept_eog, which refuses if it is not.
The order of the two tests is upstream’s and matters: the trigger check comes FIRST, so while a lazy grammar is awaiting, even an end-of-generation token is buffered rather than asserted against a parse that has not started.
Sourcepub fn allows_eog(&self) -> bool
pub fn allows_eog(&self) -> bool
Whether a parse is complete, so generation may end here.
True throughout an untriggered lazy grammar: it has not been applied, so it has no say in when generation ends.
Sourcepub fn is_awaiting_trigger(&self) -> bool
pub fn is_awaiting_trigger(&self) -> bool
Whether this is a lazy grammar that has not switched on yet.
A caller deciding whether it needs full vocabulary logits must NOT read this as “unconstrained”: the trigger can fire on any token, so the grammar needs a real logit vector from the first one. It is for reporting and for tests.
Auto Trait Implementations§
impl Freeze for GrammarSampler
impl RefUnwindSafe for GrammarSampler
impl Send for GrammarSampler
impl Sync for GrammarSampler
impl Unpin for GrammarSampler
impl UnsafeUnpin for GrammarSampler
impl UnwindSafe for GrammarSampler
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> 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