@@ -1635,7 +1635,7 @@ static void llm_load_hparams(
1635
1635
}
1636
1636
1637
1637
// TODO: This should probably be in llama.h
1638
- static std::vector<llama_vocab::id> llama_tokenize_internal (const llama_vocab & vocab, const std::string & raw_text, bool bos, bool escape );
1638
+ static std::vector<llama_vocab::id> llama_tokenize_internal (const llama_vocab & vocab, const std::string & raw_text, bool bos);
1639
1639
1640
1640
static void llm_load_vocab (
1641
1641
llama_model_loader & ml,
@@ -1737,7 +1737,7 @@ static void llm_load_vocab(
1737
1737
}
1738
1738
1739
1739
// determine the newline token: LLaMA "<0x0A>" == 10 == '\n', Falcon 193 == '\n'
1740
- vocab.linefeed_id = llama_tokenize_internal (vocab, " \n " , false , false )[0 ];
1740
+ vocab.linefeed_id = llama_tokenize_internal (vocab, " \n " , false )[0 ];
1741
1741
1742
1742
// special tokens
1743
1743
GGUF_GET_KEY (ctx, vocab.special_bos_id , gguf_get_val_u32, GGUF_TYPE_UINT32, false , kv (LLM_KV_TOKENIZER_BOS_ID));
@@ -3027,14 +3027,8 @@ static llama_token llama_byte_to_token(const llama_vocab & vocab, uint8_t ch) {
3027
3027
}
3028
3028
3029
3029
static std::string llama_escape_whitespace (const std::string& text) {
3030
- std::string result = " \xe2\x96\x81 " ;
3031
- for (size_t offs = 0 ; offs < text.length (); ++offs) {
3032
- if (text[offs] == ' ' ) {
3033
- result += " \xe2\x96\x81 " ;
3034
- } else {
3035
- result += text[offs];
3036
- }
3037
- }
3030
+ std::string result = text;
3031
+ replace_all (result, " " , " \xe2\x96\x81 " );
3038
3032
return result;
3039
3033
}
3040
3034
@@ -3219,7 +3213,7 @@ struct llm_bigram_bpe {
3219
3213
};
3220
3214
3221
3215
struct llm_tokenizer_bpe {
3222
- llm_tokenizer_bpe (const llama_vocab & vocab, bool g2ws ): vocab(vocab) { flag_g2ws = g2ws; }
3216
+ llm_tokenizer_bpe (const llama_vocab & vocab): vocab(vocab) {}
3223
3217
3224
3218
void tokenize (const std::string & text, std::vector<llama_vocab::id> & output) {
3225
3219
int final_prev_index = -1 ;
@@ -3371,8 +3365,6 @@ struct llm_tokenizer_bpe {
3371
3365
return words;
3372
3366
}
3373
3367
3374
- bool flag_g2ws = false ;
3375
-
3376
3368
const llama_vocab & vocab;
3377
3369
3378
3370
std::vector<llm_symbol> symbols;
@@ -3381,39 +3373,26 @@ struct llm_tokenizer_bpe {
3381
3373
llm_bigram_bpe::queue work_queue;
3382
3374
};
3383
3375
3384
- static std::vector<llama_vocab::id> llama_tokenize_internal (const llama_vocab & vocab, const std::string & raw_text, bool bos, bool escape ) {
3376
+ static std::vector<llama_vocab::id> llama_tokenize_internal (const llama_vocab & vocab, const std::string & raw_text, bool bos) {
3385
3377
std::vector<llama_vocab::id> output;
3386
3378
3387
3379
if (raw_text.empty ()) {
3388
3380
return output;
3389
3381
}
3390
3382
3383
+ if (bos && vocab.special_bos_id != -1 ) {
3384
+ output.push_back (vocab.special_bos_id );
3385
+ }
3386
+
3391
3387
switch (vocab.type ) {
3392
3388
case LLAMA_VOCAB_TYPE_SPM:
3393
3389
{
3394
3390
llm_tokenizer_spm tokenizer (vocab);
3395
-
3396
- if (bos) {
3397
- output.push_back (vocab.special_bos_id );
3398
- }
3399
-
3400
- std::string text;
3401
- if (escape) {
3402
- text = llama_escape_whitespace (raw_text);
3403
- } else {
3404
- text = raw_text;
3405
- }
3406
-
3407
- tokenizer.tokenize (text, output);
3391
+ tokenizer.tokenize (llama_escape_whitespace (raw_text), output);
3408
3392
} break ;
3409
3393
case LLAMA_VOCAB_TYPE_BPE:
3410
3394
{
3411
- llm_tokenizer_bpe tokenizer (vocab, escape);
3412
-
3413
- if (bos && vocab.special_bos_id != -1 ) {
3414
- output.push_back (vocab.special_bos_id );
3415
- }
3416
-
3395
+ llm_tokenizer_bpe tokenizer (vocab);
3417
3396
tokenizer.tokenize (raw_text, output);
3418
3397
} break ;
3419
3398
};
@@ -6095,8 +6074,7 @@ int llama_tokenize_with_model(
6095
6074
llama_token * tokens,
6096
6075
int n_max_tokens,
6097
6076
bool add_bos) {
6098
- auto escape = llama_vocab_get_type (model->vocab ) == LLAMA_VOCAB_TYPE_SPM;
6099
- auto res = llama_tokenize_internal (model->vocab , text, add_bos, escape);
6077
+ auto res = llama_tokenize_internal (model->vocab , text, add_bos);
6100
6078
6101
6079
if (n_max_tokens < (int ) res.size ()) {
6102
6080
LLAMA_LOG_ERROR (" %s: too many tokens\n " , __func__);
0 commit comments