Skip to main content

SamplerState

Struct SamplerState 

Source
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

Source

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.

Source

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.

Source

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).

Source

pub fn step(&self) -> u64

The prose-corpus step counter (see the field docs).

Source

pub fn constrained_ngram_stats(&self) -> &NGramStats

The call-local constrained-region accumulator (read-only observability, mirroring Self::ngram_stats).

Source

pub fn constrained_step(&self) -> u64

The constrained-region step counter (see the field docs).

Source

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.

Source

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, and ngram_stats carry unconditionally — they are the stream being resumed.
  • resolved_ignored is recomputed from config × 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

Source§

fn clone(&self) -> SamplerState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SamplerState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for SamplerState

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for SamplerState

Source§

fn eq(&self, other: &SamplerState) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for SamplerState

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for SamplerState

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SerializableAny for T
where T: 'static + Any + Clone + for<'a> Send + Sync,

Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(value: T, _simd: S) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more