@@ -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);
@@ -13448,7 +13453,7 @@ struct llm_tokenizer_bpe {
13448
13453
struct llm_tokenizer_wpm {
13449
13454
llm_tokenizer_wpm(const llama_vocab & vocab): vocab(vocab) {}
13450
13455
13451
- void tokenize(const std::string & text, std::vector<llama_vocab::id> & output) {
13456
+ void tokenize(const std::string & text, std::vector<llama_vocab::id> & output) const {
13452
13457
const auto & token_map = vocab.token_to_id;
13453
13458
13454
13459
// normalize and split by whitespace
@@ -13457,7 +13462,7 @@ struct llm_tokenizer_wpm {
13457
13462
// bos token prepended already
13458
13463
13459
13464
// find the longest tokens that form the words
13460
- for (const std::string &word : words) {
13465
+ for (const std::string & word : words) {
13461
13466
// skip empty words
13462
13467
if (word.size() == 0) {
13463
13468
continue;
@@ -13474,7 +13479,7 @@ struct llm_tokenizer_wpm {
13474
13479
for (int i = 0; i < n; ++i) {
13475
13480
// loop through possible match length
13476
13481
bool match = false;
13477
- for (int j = n ; j > i; j--) {
13482
+ for (int j = std::min(n, i + vocab.max_token_len + 1) ; j > i; j--) {
13478
13483
auto it = token_map.find(word1.substr(i, j - i));
13479
13484
if (it != token_map.end()) {
13480
13485
output.push_back(it->second);
@@ -13497,7 +13502,8 @@ struct llm_tokenizer_wpm {
13497
13502
}
13498
13503
}
13499
13504
13500
- std::vector<std::string> preprocess(const std::string & text) {
13505
+ // TODO: reduce string copies by using cpts_offs array
13506
+ std::vector<std::string> preprocess(const std::string & text) const {
13501
13507
const std::vector<uint32_t> cpts_nfd = unicode_cpts_normalize_nfd(unicode_cpts_from_utf8(text));
13502
13508
std::vector<std::string> words(1, "");
13503
13509
@@ -13792,14 +13798,15 @@ static std::vector<llama_vocab::id> llama_tokenize_internal(const llama_vocab &
13792
13798
output.push_back(vocab.special_cls_id);
13793
13799
}
13794
13800
13801
+ llm_tokenizer_wpm tokenizer(vocab);
13802
+
13795
13803
for (const auto & fragment : fragment_buffer) {
13796
13804
if (fragment.type == FRAGMENT_BUFFER_VARIANT_TYPE_RAW_TEXT) {
13797
13805
auto raw_text = fragment.raw_text.substr(fragment.offset, fragment.length);
13798
13806
13799
13807
#ifdef PRETOKENIZERDEBUG
13800
13808
LLAMA_LOG_WARN("TT: (%ld %ld %ld) '%s'\n", raw_text.length(), fragment.offset, fragment.length, raw_text.c_str());
13801
13809
#endif
13802
- llm_tokenizer_wpm tokenizer(vocab);
13803
13810
tokenizer.tokenize(raw_text, output);
13804
13811
} else { // if (fragment.type == FRAGMENT_BUFFER_VARIANT_TYPE_TOKEN)
13805
13812
output.push_back(fragment.token);
0 commit comments