@@ -10305,6 +10305,8 @@ int32_t llama_token_to_piece(const struct llama_model * model, llama_token token
10305
10305
if (0 <= token && token < llama_n_vocab(model)) {
10306
10306
switch (llama_vocab_get_type(model->vocab)) {
10307
10307
case LLAMA_VOCAB_TYPE_SPM: {
10308
+ // NOTE: we accept all unsupported token types,
10309
+ // suppressing them like CONTROL tokens.
10308
10310
if (llama_is_normal_token(model->vocab, token)) {
10309
10311
std::string result = model->vocab.id_to_token[token].text;
10310
10312
llama_unescape_whitespace(result);
@@ -10313,6 +10315,13 @@ int32_t llama_token_to_piece(const struct llama_model * model, llama_token token
10313
10315
}
10314
10316
memcpy(buf, result.c_str(), result.length());
10315
10317
return result.length();
10318
+ } else if (llama_is_user_defined_token(model->vocab, token)) {
10319
+ std::string result = model->vocab.id_to_token[token].text;
10320
+ if (length < (int) result.length()) {
10321
+ return -result.length();
10322
+ }
10323
+ memcpy(buf, result.c_str(), result.length());
10324
+ return result.length();
10316
10325
} else if (llama_is_unknown_token(model->vocab, token)) { // NOLINT
10317
10326
if (length < 3) {
10318
10327
return -3;
@@ -10327,14 +10336,12 @@ int32_t llama_token_to_piece(const struct llama_model * model, llama_token token
10327
10336
}
10328
10337
buf[0] = llama_token_to_byte(model->vocab, token);
10329
10338
return 1;
10330
- } else {
10331
- // TODO: for now we accept all unsupported token types,
10332
- // suppressing them like CONTROL tokens.
10333
- // GGML_ASSERT(false);
10334
10339
}
10335
10340
break;
10336
10341
}
10337
10342
case LLAMA_VOCAB_TYPE_BPE: {
10343
+ // NOTE: we accept all unsupported token types,
10344
+ // suppressing them like CONTROL tokens.
10338
10345
if (llama_is_normal_token(model->vocab, token)) {
10339
10346
std::string result = model->vocab.id_to_token[token].text;
10340
10347
result = llama_decode_text(result);
@@ -10343,12 +10350,15 @@ int32_t llama_token_to_piece(const struct llama_model * model, llama_token token
10343
10350
}
10344
10351
memcpy(buf, result.c_str(), result.length());
10345
10352
return result.length();
10353
+ } else if (llama_is_user_defined_token(model->vocab, token)) {
10354
+ std::string result = model->vocab.id_to_token[token].text;
10355
+ if (length < (int) result.length()) {
10356
+ return -result.length();
10357
+ }
10358
+ memcpy(buf, result.c_str(), result.length());
10359
+ return result.length();
10346
10360
} else if (llama_is_control_token(model->vocab, token)) {
10347
10361
;
10348
- } else {
10349
- // TODO: for now we accept all unsupported token types,
10350
- // suppressing them like CONTROL tokens.
10351
- // GGML_ASSERT(false);
10352
10362
}
10353
10363
break;
10354
10364
}
0 commit comments