Skip to main content

CommonSampler

Struct CommonSampler 

Source
pub struct CommonSampler { /* private fields */ }
Expand description

llama.cpp’s assembled sampler chain.

Implementations§

Source§

impl CommonSampler

Source

pub fn new( model: &LlamaModel, params: &mut CommonSamplerParams, ) -> Result<Self, ShimError>

Assemble the chain for model.

§Errors

Returns CommonSamplerError::Failed if llama.cpp cannot build it — most often a grammar that does not parse.

Source

pub fn sample( &mut self, ctx: &mut LlamaContext<'_>, idx: i32, grammar_first: bool, ) -> Result<LlamaToken, ShimError>

Sample a token from the logits at idx.

grammar_first applies the grammar before the other samplers rather than after. Upstream’s default is false: sample first and only fall back to grammar-constrained resampling if the pick is rejected, which is much cheaper than filtering the whole vocabulary every step.

§Errors

Returns CommonSamplerError::Failed if llama.cpp throws.

Source

pub fn accept(&mut self, token: LlamaToken, is_generated: bool)

Feed a token back into the sampler’s history.

is_generated marks a token the model produced, as opposed to one from the prompt; penalties and the grammar treat the two differently.

Source

pub fn sample_and_accept_n( &mut self, ctx: &mut LlamaContext<'_>, draft: &[LlamaToken], grammar_first: bool, ) -> Result<Vec<LlamaToken>, ShimError>

Validate a speculative draft and return the accepted prefix.

This is the acceptance half of speculative decoding: given draft tokens proposed by a drafter and a target-model forward pass covering them, it returns every draft token the target agrees with, plus one freshly sampled token at the first divergence. The result is therefore never empty and never longer than draft.len() + 1.

Accepted tokens are also fed to Self::accept internally, so the caller must not do so again.

§Errors

Returns CommonSamplerError::Failed if llama.cpp throws.

Source

pub fn reset(&mut self)

Clear all history and grammar state, keeping the configuration.

Source

pub fn try_clone(&self) -> Result<Self, ShimError>

Deep-copy this sampler, state included.

§Errors

Returns CommonSamplerError::Failed if llama.cpp returns null.

Source

pub fn seed(&self) -> u32

The seed actually in use, after any LLAMA_DEFAULT_SEED was resolved.

Source

pub fn last(&self) -> LlamaToken

The most recently sampled token.

Source

pub fn force_end_reasoning(&mut self) -> bool

End the current reasoning block now, as if the budget had run out.

Returns false when there is no budget sampler or it is not counting.

Source

pub fn describe(&self) -> Result<String, ShimError>

Describe the assembled chain, e.g. "penalties -> top_k -> temp".

§Errors

Returns CommonSamplerError::Failed if llama.cpp throws.

Source

pub fn prev_str( &mut self, ctx: &mut LlamaContext<'_>, n: i32, ) -> Result<String, ShimError>

Detokenize the last n sampled tokens.

§Errors

Returns CommonSamplerError::Failed if llama.cpp throws.

Trait Implementations§

Source§

impl Debug for CommonSampler

Source§

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

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

impl Drop for CommonSampler

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for CommonSampler

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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