pub struct SamplerState { /* private fields */ }Expand description
Everything a generation call mutates while sampling. See the module docs for the purity contract.
Implementations§
Source§impl SamplerState
impl SamplerState
Sourcepub fn grammar_complete(&self) -> bool
pub fn grammar_complete(&self) -> bool
True iff any constraint matcher — including an activated deferred grammar — has reached its accept state. The Session uses this to break out of the piece loop as soon as structured output is complete.
Sourcepub fn constraint_incomplete_at_end(&self) -> bool
pub fn constraint_incomplete_at_end(&self) -> bool
True iff generation ended while a byte-constraint was still mid-structure — the Session’s incomplete-at-end violation check. Two shapes count:
- an eager constraint (active from token 0) where none of the matchers reached accept;
- a deferred grammar that activated — its trigger fired —
and is incomplete. A deferred grammar that never triggered is
NOT a violation: never calling a tool is legal on the Auto
path. This clause is the inverse of the deferred arm in
Self::grammar_complete, and its absence was issue #38’s defect 1 — a truncated Auto call parsed as (and seated as) plain text, poisoning the transcript for the next ingest.
A terminal token whose bytes complete the constraint is not
visible here: it never advances the matchers (tip invariant), so
the exemption lives one level up in
crate::TokenPredictor::constraint_incomplete_at_end.
Sourcepub fn ngram_stats(&self) -> &NGramStats
pub fn ngram_stats(&self) -> &NGramStats
The repetition-penalty n-gram accumulator (read-only
observability — probes and tests compare fold results;
crate::NGramStats derives PartialEq for exactly that).
Sourcepub fn constrained_ngram_stats(&self) -> &NGramStats
pub fn constrained_ngram_stats(&self) -> &NGramStats
The call-local constrained-region accumulator (read-only
observability, mirroring Self::ngram_stats).
Sourcepub fn constrained_step(&self) -> u64
pub fn constrained_step(&self) -> u64
The constrained-region step counter (see the field docs).
Sourcepub fn advance<M: Model>(
&mut self,
config: &SamplerConfig,
token: Token,
model: &M,
)
pub fn advance<M: Model>( &mut self, config: &SamplerConfig, token: Token, model: &M, )
Advance every active matcher (and the activated deferred grammar) by the chosen token’s piece bytes. Advance errors are deliberately swallowed: they mean the constraint was violated on the prior step (EOS fallback chose an out-of-grammar token) and generation terminates on the next step via the EOS stop sequence.
Candidates::sample_token
deliberately does not call this: whether the chosen token
continues generation is the caller’s call, and a token that
terminates it must never mutate the state (tip invariant). Call
this only for tokens that keep generating.
Sourcepub fn resumed_from<M: Model>(
cached: &SamplerState,
config: &SamplerConfig,
model: &M,
) -> SamplerState
pub fn resumed_from<M: Model>( cached: &SamplerState, config: &SamplerConfig, model: &M, ) -> SamplerState
Build the working state for a call that resumes cached under
config — the reconcile-by-grammar-identity load rule (design
memo, Phase 2 round):
- The matchers vec is built fresh from
config.modes(never cloned wholesale — the cached vec was aligned to a different effective config’s modes, so verbatim reuse risks out-of-bounds rule indices). Each new grammar mode carries the cached position forward iff a cached matcher walked the identical compiled grammar (CompiledGrammar::source_hash; same source ⇒ same deterministic compile ⇒ same indices); otherwise it starts at root. This is what makes assistant-prefill / partial-completion resume work while a changed grammar resets only the matcher. - The JSON matcher carries unconditionally (fixed built-in grammar, no identity to mismatch).
- The deferred matcher carries (flag + position) iff the spec’s grammar identity matches; else fresh inactive root.
mu, the working rng, andngram_statscarry unconditionally — they are the stream being resumed.resolved_ignoredis recomputed fromconfig×model: it is a config memo riding the state, and repetition knobs are a free per-call override.
Trait Implementations§
Source§impl Clone for SamplerState
impl Clone for SamplerState
Source§fn clone(&self) -> SamplerState
fn clone(&self) -> SamplerState
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SamplerState
impl Debug for SamplerState
Source§impl<'de> Deserialize<'de> for SamplerState
impl<'de> Deserialize<'de> for SamplerState
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for SamplerState
impl PartialEq for SamplerState
Source§impl Serialize for SamplerState
impl Serialize for SamplerState
impl StructuralPartialEq for SamplerState
Auto Trait Implementations§
impl Freeze for SamplerState
impl RefUnwindSafe for SamplerState
impl Send for SamplerState
impl Sync for SamplerState
impl Unpin for SamplerState
impl UnsafeUnpin for SamplerState
impl UnwindSafe for SamplerState
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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