pub struct BpeTokenizer { /* private fields */ }Expand description
Deterministic byte-pair encoder compatible with OA’s oa_bpe_v1 files.
Implementations§
Source§impl BpeTokenizer
impl BpeTokenizer
Sourcepub fn new(target_vocab: usize) -> Self
pub fn new(target_vocab: usize) -> Self
Construct an untrained tokenizer, clamping the target to the 256 base bytes.
Sourcepub fn train(&mut self, bytes: &[u8], num_merges: usize)
pub fn train(&mut self, bytes: &[u8], num_merges: usize)
Learn at most num_merges deterministic pair ranks from raw bytes.
Sourcepub fn train_text(&mut self, text: &str, num_merges: usize)
pub fn train_text(&mut self, text: &str, num_merges: usize)
Learn merges from UTF-8 text without changing its byte representation.
Sourcepub fn encode_text(&self, text: &str) -> Vec<u32>
pub fn encode_text(&self, text: &str) -> Vec<u32>
Apply learned ranks to UTF-8 text.
Sourcepub fn decode(&self, tokens: &[u32]) -> Result<Vec<u8>>
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.
Sourcepub fn decode_text(&self, tokens: &[u32]) -> Result<String>
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.
Sourcepub fn encode_prompt(&self, prompt: &[u8], context_length: usize) -> Vec<u32>
pub fn encode_prompt(&self, prompt: &[u8], context_length: usize) -> Vec<u32>
Right-align an encoded prompt in a zero-padded fixed context.
Sourcepub fn save(&self, path: impl AsRef<Path>) -> Result<()>
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.
Sourcepub fn load(&mut self, path: impl AsRef<Path>) -> Result<()>
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.
Sourcepub const fn vocab_size(&self) -> usize
pub const fn vocab_size(&self) -> usize
Current vocabulary size, including the 256 base bytes.
Sourcepub const fn num_merges(&self) -> usize
pub const fn num_merges(&self) -> usize
Number of learned merge ranks.
Sourcepub fn token_byte_len(&self, token: u32) -> Result<usize>
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.