@@ -2293,6 +2293,8 @@ struct llama_vocab {
2293
2293
enum llama_vocab_type type = LLAMA_VOCAB_TYPE_SPM;
2294
2294
enum llama_vocab_pre_type type_pre = LLAMA_VOCAB_PRE_TYPE_DEFAULT;
2295
2295
2296
+ int max_token_len = 0; // used for optimizing longest token search
2297
+
2296
2298
std::unordered_map<token, id> token_to_id;
2297
2299
std::vector<token_data> id_to_token;
2298
2300
@@ -4939,6 +4941,7 @@ static void llm_load_vocab(
4939
4941
GGML_ASSERT(unicode_cpts_from_utf8(word).size() > 0);
4940
4942
4941
4943
vocab.token_to_id[word] = i;
4944
+ vocab.max_token_len = std::max(vocab.max_token_len, (int) word.size());
4942
4945
4943
4946
auto & token_data = vocab.id_to_token[i];
4944
4947
token_data.text = std::move(word);
@@ -5249,6 +5252,8 @@ static void llm_load_print_meta(llama_model_loader & ml, llama_model & model) {
5249
5252
if (vocab.special_middle_id != -1) { LLAMA_LOG_INFO( "%s: MID token = %d '%s'\n", __func__, vocab.special_middle_id, vocab.id_to_token[vocab.special_middle_id].text.c_str() ); }
5250
5253
if (vocab.special_eot_id != -1) { LLAMA_LOG_INFO( "%s: EOT token = %d '%s'\n", __func__, vocab.special_eot_id, vocab.id_to_token[vocab.special_eot_id].text.c_str() ); }
5251
5254
5255
+ LLAMA_LOG_INFO("%s: max token length = %d\n", __func__, vocab.max_token_len);
5256
+
5252
5257
if (model.arch == LLM_ARCH_DEEPSEEK2) {
5253
5258
LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead);
5254
5259
LLAMA_LOG_INFO("%s: n_lora_q = %d\n", __func__, hparams.n_lora_q);
@@ -13488,7 +13493,7 @@ struct llm_tokenizer_bpe {
13488
13493
struct llm_tokenizer_wpm {
13489
13494
llm_tokenizer_wpm(const llama_vocab & vocab): vocab(vocab) {}
13490
13495
13491
- void tokenize(const std::string & text, std::vector<llama_vocab::id> & output) {
13496
+ void tokenize(const std::string & text, std::vector<llama_vocab::id> & output) const {
13492
13497
const auto & token_map = vocab.token_to_id;
13493
13498
13494
13499
// normalize and split by whitespace
@@ -13497,7 +13502,7 @@ struct llm_tokenizer_wpm {
13497
13502
// bos token prepended already
13498
13503
13499
13504
// find the longest tokens that form the words
13500
- for (const std::string &word : words) {
13505
+ for (const std::string & word : words) {
13501
13506
// skip empty words
13502
13507
if (word.size() == 0) {
13503
13508
continue;
@@ -13514,7 +13519,7 @@ struct llm_tokenizer_wpm {
13514
13519
for (int i = 0; i < n; ++i) {
13515
13520
// loop through possible match length
13516
13521
bool match = false;
13517
- for (int j = n ; j > i; j--) {
13522
+ for (int j = std::min(n, i + vocab.max_token_len + 1) ; j > i; j--) {
13518
13523
auto it = token_map.find(word1.substr(i, j - i));
13519
13524
if (it != token_map.end()) {
13520
13525
output.push_back(it->second);
@@ -13537,7 +13542,8 @@ struct llm_tokenizer_wpm {
13537
13542
}
13538
13543
}
13539
13544
13540
- std::vector<std::string> preprocess(const std::string & text) {
13545
+ // TODO: reduce string copies by using cpts_offs array
13546
+ std::vector<std::string> preprocess(const std::string & text) const {
13541
13547
const std::vector<uint32_t> cpts_nfd = unicode_cpts_normalize_nfd(unicode_cpts_from_utf8(text));
13542
13548
std::vector<std::string> words(1, "");
13543
13549
@@ -13832,14 +13838,15 @@ static std::vector<llama_vocab::id> llama_tokenize_internal(const llama_vocab &
13832
13838
output.push_back(vocab.special_cls_id);
13833
13839
}
13834
13840
13841
+ llm_tokenizer_wpm tokenizer(vocab);
13842
+
13835
13843
for (const auto & fragment : fragment_buffer) {
13836
13844
if (fragment.type == FRAGMENT_BUFFER_VARIANT_TYPE_RAW_TEXT) {
13837
13845
auto raw_text = fragment.raw_text.substr(fragment.offset, fragment.length);
13838
13846
13839
13847
#ifdef PRETOKENIZERDEBUG
13840
13848
LLAMA_LOG_WARN("TT: (%ld %ld %ld) '%s'\n", raw_text.length(), fragment.offset, fragment.length, raw_text.c_str());
13841
13849
#endif
13842
- llm_tokenizer_wpm tokenizer(vocab);
13843
13850
tokenizer.tokenize(raw_text, output);
13844
13851
} else { // if (fragment.type == FRAGMENT_BUFFER_VARIANT_TYPE_TOKEN)
13845
13852
output.push_back(fragment.token);
0 commit comments