Skip to main content

BpeTokenizer

Struct BpeTokenizer 

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

Deterministic byte-pair encoder compatible with OA’s oa_bpe_v1 files.

Implementations§

Source§

impl BpeTokenizer

Source

pub fn new(target_vocab: usize) -> Self

Construct an untrained tokenizer, clamping the target to the 256 base bytes.

Source

pub fn train(&mut self, bytes: &[u8], num_merges: usize)

Learn at most num_merges deterministic pair ranks from raw bytes.

Source

pub fn train_text(&mut self, text: &str, num_merges: usize)

Learn merges from UTF-8 text without changing its byte representation.

Source

pub fn encode(&self, bytes: &[u8]) -> Vec<u32>

Apply learned ranks to raw bytes.

Source

pub fn encode_text(&self, text: &str) -> Vec<u32>

Apply learned ranks to UTF-8 text.

Source

pub fn decode(&self, tokens: &[u32]) -> Result<Vec<u8>>

Expand valid tokens recursively to their exact original bytes.

Negative C++ token IDs cannot occur in this Rust spelling. Unknown token IDs are ignored, matching the donor decoder’s failure behavior.

§Errors

Returns crate::ErrorKind::ResourceExhausted when expansion storage cannot be reserved. Expansion is iterative, so nested persisted ranks do not consume the native call stack.

Source

pub fn decode_text(&self, tokens: &[u32]) -> Result<String>

Decode tokens as UTF-8 text.

§Errors

Returns crate::ErrorKind::DataLoss when the exact decoded bytes are not valid UTF-8.

Source

pub fn encode_prompt(&self, prompt: &[u8], context_length: usize) -> Vec<u32>

Right-align an encoded prompt in a zero-padded fixed context.

Source

pub fn save(&self, path: impl AsRef<Path>) -> Result<()>

Persist exact merge ranks in OA’s architecture-independent text format.

§Errors

Returns an I/O error when the file cannot be written.

Source

pub fn load(&mut self, path: impl AsRef<Path>) -> Result<()>

Load exact merge ranks transactionally from an OA oa_bpe_v1 file.

§Errors

Returns an I/O error when reading fails or crate::ErrorKind::DataLoss when the header, count, integer syntax, or merge dependency order is invalid.

Source

pub const fn vocab_size(&self) -> usize

Current vocabulary size, including the 256 base bytes.

Source

pub const fn num_merges(&self) -> usize

Number of learned merge ranks.

Source

pub fn merges(&self) -> &[BpeMerge]

Borrow the deterministic learned merge sequence.

Source

pub fn token_byte_len(&self, token: u32) -> Result<usize>

Return the exact decoded byte width of one valid token.

Unknown tokens have width zero, matching decode’s skip behavior.

§Errors

Returns crate::ErrorKind::ResourceExhausted when the expansion width or temporary traversal storage cannot be represented.

Trait Implementations§

Source§

impl Default for BpeTokenizer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Forget for BpeTokenizer

§

impl Freeze for BpeTokenizer

§

impl Move for BpeTokenizer

§

impl RefUnwindSafe for BpeTokenizer

§

impl Send for BpeTokenizer
where RawVecInner: Send,

§

impl Sync for BpeTokenizer
where RawVecInner: Sync,

§

impl Unpin for BpeTokenizer
where *const u8 is !null: Unpin,

§

impl UnsafeUnpin for BpeTokenizer

§

impl UnwindSafe for BpeTokenizer
where RawVecInner: UnwindSafe,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

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<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

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.