|
| bool | containsToken (const std::string &token) const |
| | Raw vocabulary membership test (no UNK fallback).
|
|
const BpeVocabularyConfig & | getConfig () const |
|
std::optional< size_t > | getMergePriority (const std::string &left, const std::string &right) const |
|
const std::vector< std::pair< std::string, std::string > > & | getMergeRules () const |
| size_t | getSize () const override |
| | Get the number of tokens in the vocabulary.
|
| std::optional< TokenId > | getSpecialTokenId (const std::string &token_str) const |
| | Look up a special token ID by its string representation.
|
| const std::vector< std::pair< std::string, TokenId > > & | getSpecialTokenList () const |
| | Return the special token list sorted longest-first.
|
| std::optional< std::string > | idToToken (TokenId id) const override |
| | Map a numeric id back to its token string.
|
|
bool | isByteLevel () const |
| void | save (const fs::path &path) const override |
| | Serialize the vocabulary to Mila binary format (content version 2).
|
| std::optional< TokenId > | tokenToId (const std::string &token) const override |
| | Convert a token string to its ID.
|
| virtual | ~TokenizerVocabulary ()=default |
| | Virtual destructor.
|
| virtual void | save (const std::filesystem::path &path) const =0 |
| | Serialize the vocabulary to disk at the given path.
|
|
|
static const std::unordered_map< std::string, unsigned char > & | getByteDecoder () |
|
static const std::unordered_map< unsigned char, std::string > & | getByteEncoder () |
| static BpeVocabulary | load (const fs::path &path) |
| | Load a vocabulary from Mila binary format (version 2).
|
| static BpeVocabulary | loadGemma (const fs::path &path) |
| | Load a pretrained Gemma 4 (SentencePiece BPE) vocabulary.
|
| static BpeVocabulary | loadGpt2 (const fs::path &tokenizer_path) |
| | Load a pretrained GPT-2 vocabulary.
|
| static BpeVocabulary | loadLlama32 (const fs::path &path) |
| | Load a pretrained Llama 3.2 vocabulary.
|
| static BpeVocabulary | loadMistral (const fs::path &vocab_path, const fs::path &merges_path) |
| | Load a pretrained Mistral vocabulary.
|
| static BpeVocabulary | loadQwen (const fs::path &path) |
| | Load a pretrained Qwen 3.x (byte-level BPE) vocabulary.
|
| static BpeVocabulary | train (const std::string &corpus, const BpeVocabularyConfig &config) |
| | Train a BPE vocabulary from a text corpus.
|
| static BpeVocabulary | trainFromFile (const fs::path &corpus_path, const BpeVocabularyConfig &config) |
| | Train a BPE vocabulary from a corpus file.
|
Unified Byte Pair Encoding (BPE) vocabulary.
Immutable after construction; safe for concurrent reads. Supports training from scratch via BpeTrainer, or loading pretrained vocabularies from:
- Mila binary format produced by save() (load)
- GPT-2 binary produced by convert_gpt2_tokenizer.py (loadGpt2)
- Llama 3.2 binary produced by convert_llama_tokenizer.py (loadLlama32)
- Gemma 4 binary produced by Gemma/convert_tokenizer.py (loadGemma)
- Qwen 3.x binary produced by Qwen/convert_tokenizer.py (loadQwen)
Special tokens are keyed on their string representation (e.g., "<|endoftext|>", "<|begin_of_text|>") and exposed via getSpecialTokenList() for O(n) pre-pass scanning in BpeTokenizer. Extended special tokens from SpecialTokens are registered automatically.
| BpeVocabulary Mila::Data::BpeVocabulary::loadQwen |
( |
const fs::path & | path | ) |
|
|
static |
Load a pretrained Qwen 3.x (byte-level BPE) vocabulary.
Reads the same binary layout loadGemma does, produced by Qwen/convert_tokenizer.py:
Header: vocab_size (uint32), use_byte_fallback (uint8),
model_type (uint8: 1=BPE), num_merges (uint32)
For each token: token_length (uint32), token_bytes, score (float32), token_id (uint32)
For each merge: left_length (uint32), left, right_length (uint32), right
has_bos/eos/pad/unk (uint32) + id (uint32, conditional)
Configures the GPT-2 byte-level runtime (byte_level=true, the GPT-2 byte encoder) with Qwen's own split pattern, and takes the merge-by-rank path rather than Llama's max-munch: Qwen's ignore_merges is false and its vocabulary is not max-munch equivalent. Chat, thinking and tool-calling markers are registered from the loaded vocabulary.
- Parameters
-
| path | Path to the converted Qwen tokenizer binary. |
- Exceptions
-
| std::runtime_error | on I/O errors or a non-BPE model type. |