pub struct ByteLevelStreamingDecoder<'a> { /* private fields */ }Expand description
A streaming decoder for ByteLevel-encoded tokenizers (GPT-2, Llama, DeepSeek V3).
This decoder handles the ByteLevel encoding used by some tokenizers where raw bytes are mapped to printable Unicode characters. It first decodes the ByteLevel representation back to raw bytes, then assembles them into valid UTF-8 strings.
§Example
use splintr::{from_pretrained, Backend, ByteLevelStreamingDecoder};
let any = from_pretrained("deepseek_v3")?;
let Backend::Bpe(tokenizer) = any.into_backend() else {
unreachable!("deepseek_v3 loads as a BPE backend");
};
let mut decoder = ByteLevelStreamingDecoder::new(&tokenizer);
for token_id in tokenizer.encode("Hello, world!") {
if let Some(text) = decoder.add_token(token_id) {
print!("{}", text);
}
}
// Flush any remaining buffered bytes
print!("{}", decoder.flush());Implementations§
Source§impl<'a> ByteLevelStreamingDecoder<'a>
impl<'a> ByteLevelStreamingDecoder<'a>
Sourcepub fn new(tokenizer: &'a Tokenizer) -> Self
pub fn new(tokenizer: &'a Tokenizer) -> Self
Create a new ByteLevel streaming decoder for the given tokenizer.
Sourcepub fn add_token(&mut self, token_id: u32) -> Option<String>
pub fn add_token(&mut self, token_id: u32) -> Option<String>
Add a token and return any complete UTF-8 characters.
The token’s ByteLevel-encoded bytes are first decoded to raw bytes, then assembled into valid UTF-8 strings.
Returns Some(string) if there are complete characters to emit,
or None if the current bytes are still incomplete.
Sourcepub fn add_tokens(&mut self, token_ids: &[u32]) -> Option<String>
pub fn add_tokens(&mut self, token_ids: &[u32]) -> Option<String>
Add multiple tokens at once and return complete UTF-8 characters.
Sourcepub fn flush(&mut self) -> String
pub fn flush(&mut self) -> String
Flush any remaining buffered bytes.
If there are incomplete UTF-8 sequences in the buffer, they will be replaced with the Unicode replacement character (U+FFFD).
Sourcepub fn has_pending(&self) -> bool
pub fn has_pending(&self) -> bool
Check if there are buffered bytes waiting for completion.
Sourcepub fn pending_bytes(&self) -> usize
pub fn pending_bytes(&self) -> usize
Get the number of pending bytes in the buffer.
Auto Trait Implementations§
impl<'a> Freeze for ByteLevelStreamingDecoder<'a>
impl<'a> RefUnwindSafe for ByteLevelStreamingDecoder<'a>
impl<'a> Send for ByteLevelStreamingDecoder<'a>
impl<'a> Sync for ByteLevelStreamingDecoder<'a>
impl<'a> Unpin for ByteLevelStreamingDecoder<'a>
impl<'a> UnsafeUnpin for ByteLevelStreamingDecoder<'a>
impl<'a> UnwindSafe for ByteLevelStreamingDecoder<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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