| 110 | } |
| 111 | |
| 112 | void LoadTokenizer(const std::string& tokenizer_path, const std::string& bpe_path, Llama4Tokenizer& tokenizer) { |
| 113 | // hard code it for now. |
| 114 | std::string pattern_str = "[^\\r\\n\\p{L}\\p{N}]?[\\p{Lu}\\p{Lt}\\p{Lm}\\p{Lo}\\p{M}]*[\\p{Ll}\\p{Lm}\\p{Lo}\\p{M}]+(?i:'s|'t|'re|'ve|'m|'ll|'d)?|[^\\r\\n\\p{L}\\p{N}]?[\\p{Lu}\\p{Lt}\\p{Lm}\\p{Lo}\\p{M}]+[\\p{Ll}\\p{Lm}\\p{Lo}\\p{M}]*(?i:'s|'t|'re|'ve|'m|'ll|'d)?|\\p{N}{1,3}| ?[^\\s\\p{L}\\p{N}]+[\\r\\n/]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+"; |
| 115 | |
| 116 | // load base vocab. |
| 117 | std::vector<VocabItem> vocab; |
| 118 | LoadBPEFile(bpe_path, vocab); |
| 119 | |
| 120 | // load special tokens. |
| 121 | std::ifstream file(tokenizer_path); |
| 122 | nlohmann::json json_data; |
| 123 | file >> json_data; |
| 124 | // Parse config |
| 125 | tokenizer.config = json_data.get<TokenizerConfig>(); |
| 126 | std::vector<VocabItem> special_vocab; |
| 127 | for (const auto& [token_str, special_token] : tokenizer.config.added_tokens_decoder) { |
| 128 | VocabItem item; |
| 129 | item.rank = std::stoi(token_str); |
| 130 | item.token_string = special_token.content; |
| 131 | item.token_bytes = std::vector<unsigned char>(special_token.content.begin(), special_token.content.end()); |
| 132 | special_vocab.push_back(item); |
| 133 | } |
| 134 | |
| 135 | // Create the BPE tokenizer and initialize it |
| 136 | tokenizer.bpe = std::make_unique<tiktoken::CoreBPE>(pattern_str, vocab, special_vocab); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | void Tokenize(const Llama4Tokenizer& tokenizer, const std::string& prompt) { |
no test coverage detected