Skip to main content

Decoder

Trait Decoder 

Source
pub trait Decoder: Send + Sync {
    // Required method
    fn decode(
        &self,
        token_ids: &[TokenIdType],
        skip_special_tokens: bool,
    ) -> Result<String>;

    // Provided method
    fn decode_step(
        &self,
        token_id: TokenIdType,
        ids: &mut Vec<TokenIdType>,
        prefix: &mut String,
        prefix_index: &mut usize,
        skip_special_tokens: bool,
    ) -> Result<Option<String>> { ... }
}
Expand description

Core decoding trait - can be implemented independently

Required Methods§

Source

fn decode( &self, token_ids: &[TokenIdType], skip_special_tokens: bool, ) -> Result<String>

Provided Methods§

Source

fn decode_step( &self, token_id: TokenIdType, ids: &mut Vec<TokenIdType>, prefix: &mut String, prefix_index: &mut usize, skip_special_tokens: bool, ) -> Result<Option<String>>

Incremental decode step — called once per generated token.

Maintains mutable state (ids, prefix, prefix_index) across calls to produce incremental text output. The default implementation uses the double-decode algorithm (decode prefix, decode prefix+new, diff).

HuggingFace overrides this with the native step_decode_stream from the tokenizers crate, which uses the same algorithm internally but avoids trait-method overhead for the two decode() calls.

Implementors§