Skip to main content

SampledTokenClassifier

Struct SampledTokenClassifier 

Source
pub struct SampledTokenClassifier<'model> { /* private fields */ }

Implementations§

Source§

impl<'model> SampledTokenClassifier<'model>

Source

pub fn new(model: &'model LlamaModel, markers: StreamingMarkers) -> Self

Source

pub fn ingest(&mut self, token: LlamaToken) -> Vec<IngestOutcome>

Ingest one sampled token. Returns the outcomes that have finalised this turn — typically a single outcome, occasionally zero (the classifier is holding back tokens that may yet form a marker), or several when a buffered marker prefix diverges and the held-back tokens flush.

Each IngestOutcome carries both the SampledToken variant for classification and the decoded visible_piece for streaming. Marker boundaries get an empty visible_piece so their text never reaches user-visible streams.

Source

pub fn ingest_prompt_token(&mut self, token: LlamaToken)

Replay one prompt token through the marker state machine so that the section at end-of-prompt reflects the chat template’s rendered tail (e.g. for Qwen3.5/3.6 with enable_thinking=false the prompt ends with a closed empty <think>...</think> block, leaving the section in Content; with enable_thinking=true it ends inside an open <think>, leaving the section in Reasoning).

Prompt tokens never produce IngestOutcomes and never increment usage counters — they are not generated content.

Source

pub fn ingest_prompt_tokens(&mut self, tokens: &[LlamaToken])

Source

pub fn flush(&mut self) -> Vec<IngestOutcome>

Drain every still-buffered token. Call once at end of generation (EOG) to make sure no decoded text is silently dropped. After flush() the classifier behaves as if freshly constructed in terms of buffer state.

Source

pub fn sample( &mut self, sampler: &mut LlamaSampler, context: &LlamaContext<'_>, idx: i32, ) -> Result<(LlamaToken, Vec<IngestOutcome>), SampleError>

§Errors

Forwards LlamaSampler::sample errors verbatim. Nothing is recorded on failure.

Returns the raw sampled token (for downstream batch.add / is_eog_token calls) alongside the outcomes that finalised this turn — see Self::ingest for buffering semantics.

Source

pub fn feed_prompt_to_batch( &mut self, batch: &mut LlamaBatch<'_>, token: LlamaToken, position: llama_pos, seq_ids: &[llama_seq_id], logits: bool, ) -> Result<(), BatchAddError>

§Errors

Forwards LlamaBatch::add errors verbatim. Nothing is staged on failure.

Source

pub fn feed_prompt_sequence_to_batch( &mut self, batch: &mut LlamaBatch<'_>, tokens: &[LlamaToken], seq_id: llama_seq_id, logits_all: bool, ) -> Result<(), BatchAddError>

§Errors

Forwards LlamaBatch::add_sequence errors verbatim. Nothing is staged on failure.

Source

pub const fn commit_prompt_tokens(&mut self) -> u64

Source

pub const fn discard_pending_prompt_tokens(&mut self) -> u64

Source

pub const fn pending_prompt_tokens(&self) -> u64

Source

pub fn eval_multimodal_chunks( &mut self, chunks: &MtmdInputChunks, mtmd_ctx: &MtmdContext, llama_ctx: &LlamaContext<'_>, start_position: llama_pos, seq_id: llama_seq_id, n_batch: i32, logits_last: bool, ) -> Result<llama_pos, EvalMultimodalChunksError>

§Errors

Returns EvalMultimodalChunksError::EvalFailed when the underlying eval_chunks call fails (no counters move), EvalMultimodalChunksError::UnknownChunkType when a chunk reports a type unknown to this binding, or EvalMultimodalChunksError::ChunkOutOfBounds when a valid index returns None from chunks.get.

Source

pub const fn record_prompt_tokens(&mut self, count: u64)

Source

pub const fn record_input_image_tokens(&mut self, count: u64)

Source

pub const fn record_input_audio_tokens(&mut self, count: u64)

Source

pub const fn record_cached_prompt_tokens( &mut self, count: u64, ) -> Result<(), TokenUsageError>

§Errors

Forwards TokenUsageError::CachedExceedsPrompt when the running cached total would exceed the prompt total.

Source

pub const fn usage(&self) -> &TokenUsage

Source

pub fn into_usage(self) -> TokenUsage

Source

pub const fn current_section(&self) -> SampledTokenSection

Source

pub const fn markers(&self) -> &StreamingMarkers

Auto Trait Implementations§

§

impl<'model> Freeze for SampledTokenClassifier<'model>

§

impl<'model> RefUnwindSafe for SampledTokenClassifier<'model>

§

impl<'model> Send for SampledTokenClassifier<'model>

§

impl<'model> Sync for SampledTokenClassifier<'model>

§

impl<'model> Unpin for SampledTokenClassifier<'model>

§

impl<'model> UnsafeUnpin for SampledTokenClassifier<'model>

§

impl<'model> UnwindSafe for SampledTokenClassifier<'model>

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