pub struct CommonSampler { /* private fields */ }Expand description
llama.cpp’s assembled sampler chain.
Implementations§
Source§impl CommonSampler
impl CommonSampler
Sourcepub fn new(
model: &LlamaModel,
params: &mut CommonSamplerParams,
) -> Result<Self, ShimError>
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.
Sourcepub fn sample(
&mut self,
ctx: &mut LlamaContext<'_>,
idx: i32,
grammar_first: bool,
) -> Result<LlamaToken, ShimError>
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.
Sourcepub fn accept(&mut self, token: LlamaToken, is_generated: bool)
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.
Sourcepub fn sample_and_accept_n(
&mut self,
ctx: &mut LlamaContext<'_>,
draft: &[LlamaToken],
grammar_first: bool,
) -> Result<Vec<LlamaToken>, ShimError>
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.
Sourcepub fn try_clone(&self) -> Result<Self, ShimError>
pub fn try_clone(&self) -> Result<Self, ShimError>
Deep-copy this sampler, state included.
§Errors
Returns CommonSamplerError::Failed if llama.cpp returns null.
Sourcepub fn last(&self) -> LlamaToken
pub fn last(&self) -> LlamaToken
The most recently sampled token.
Sourcepub fn force_end_reasoning(&mut self) -> bool
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.
Sourcepub fn describe(&self) -> Result<String, ShimError>
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.
Sourcepub fn prev_str(
&mut self,
ctx: &mut LlamaContext<'_>,
n: i32,
) -> Result<String, ShimError>
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.