Skip to content

Commit 0306289

Browse files
ggerganovgoerch
authored andcommitted
llama : fix detokenization of non-special added-tokens (ggml-org#4916)
Co-authored-by: goerch <[email protected]>
1 parent cd7daea commit 0306289

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

llama.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10305,6 +10305,8 @@ int32_t llama_token_to_piece(const struct llama_model * model, llama_token token
1030510305
if (0 <= token && token < llama_n_vocab(model)) {
1030610306
switch (llama_vocab_get_type(model->vocab)) {
1030710307
case LLAMA_VOCAB_TYPE_SPM: {
10308+
// NOTE: we accept all unsupported token types,
10309+
// suppressing them like CONTROL tokens.
1030810310
if (llama_is_normal_token(model->vocab, token)) {
1030910311
std::string result = model->vocab.id_to_token[token].text;
1031010312
llama_unescape_whitespace(result);
@@ -10313,6 +10315,13 @@ int32_t llama_token_to_piece(const struct llama_model * model, llama_token token
1031310315
}
1031410316
memcpy(buf, result.c_str(), result.length());
1031510317
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();
1031610325
} else if (llama_is_unknown_token(model->vocab, token)) { // NOLINT
1031710326
if (length < 3) {
1031810327
return -3;
@@ -10327,14 +10336,12 @@ int32_t llama_token_to_piece(const struct llama_model * model, llama_token token
1032710336
}
1032810337
buf[0] = llama_token_to_byte(model->vocab, token);
1032910338
return 1;
10330-
} else {
10331-
// TODO: for now we accept all unsupported token types,
10332-
// suppressing them like CONTROL tokens.
10333-
// GGML_ASSERT(false);
1033410339
}
1033510340
break;
1033610341
}
1033710342
case LLAMA_VOCAB_TYPE_BPE: {
10343+
// NOTE: we accept all unsupported token types,
10344+
// suppressing them like CONTROL tokens.
1033810345
if (llama_is_normal_token(model->vocab, token)) {
1033910346
std::string result = model->vocab.id_to_token[token].text;
1034010347
result = llama_decode_text(result);
@@ -10343,12 +10350,15 @@ int32_t llama_token_to_piece(const struct llama_model * model, llama_token token
1034310350
}
1034410351
memcpy(buf, result.c_str(), result.length());
1034510352
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();
1034610360
} else if (llama_is_control_token(model->vocab, token)) {
1034710361
;
10348-
} else {
10349-
// TODO: for now we accept all unsupported token types,
10350-
// suppressing them like CONTROL tokens.
10351-
// GGML_ASSERT(false);
1035210362
}
1035310363
break;
1035410364
}

0 commit comments

Comments
 (0)