Skip to content

Commit 7b6ae89

Browse files
committed
llama : fix tokenizer to use llama_char_to_byte
1 parent 0ba5d48 commit 7b6ae89

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

llama.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,6 +2303,18 @@ static uint8_t llama_byte_to_char(const llama_vocab & vocab, uint8_t byte) {
23032303
return false;
23042304
}
23052305

2306+
static uint8_t llama_char_to_byte(const llama_vocab & vocab, uint8_t ch) {
2307+
if (llama_vocab_type(vocab) == "spm") {
2308+
return ch + 3;
2309+
}
2310+
2311+
if (llama_vocab_type(vocab) == "bpe") {
2312+
return ch - 32;
2313+
}
2314+
2315+
return false;
2316+
}
2317+
23062318
static std::string llama_escape_whitespace(const std::string& text) {
23072319
std::string result;
23082320
bool escaping = false;
@@ -2439,7 +2451,7 @@ struct llama_tokenizer {
24392451
if (p == rev_merge.end()) {
24402452
// output any symbols that did not form tokens as bytes.
24412453
for (int j = 0; j < (int)symbol.n; ++j) {
2442-
llama_vocab::id token_id = llama_byte_to_char(vocab_, symbol.text[j]);
2454+
llama_vocab::id token_id = llama_char_to_byte(vocab_, symbol.text[j]);
24432455
output.push_back(token_id);
24442456
}
24452457
return;
@@ -4871,8 +4883,8 @@ int llama_token_to_str_with_model(const struct llama_model * model, llama_token
48714883
return 0;
48724884
}
48734885

4874-
int llama_token_to_str(const struct llama_context * ctx, llama_token token, char * str, int length) {
4875-
return llama_token_to_str_with_model(&ctx->model, token, str, length);
4886+
int llama_token_to_str(const struct llama_context * ctx, llama_token token, char * buf, int length) {
4887+
return llama_token_to_str_with_model(&ctx->model, token, buf, length);
48764888
}
48774889

48784890
std::string llama_token_to_str(const struct llama_context * ctx, llama_token token) {
@@ -4889,13 +4901,13 @@ std::string llama_token_to_str(const struct llama_context * ctx, llama_token tok
48894901
return std::string(result.data(), result.size());
48904902
}
48914903

4892-
int llama_token_to_str_bpe(const struct llama_context * ctx, llama_token token, char * str, int length) {
4904+
int llama_token_to_str_bpe(const struct llama_context * ctx, llama_token token, char * buf, int length) {
48934905
if (0 <= token && token < llama_n_vocab_from_model(&ctx->model)) {
48944906
std::string result = ctx->model.vocab.id_to_token[token].tok;
48954907
if (length < (int) result.length()) {
48964908
return -result.length();
48974909
}
4898-
memcpy(str, result.c_str(), result.length());
4910+
memcpy(buf, result.c_str(), result.length());
48994911
return result.length();
49004912
}
49014913
return 0;

tests/test-tokenizer-0.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ int main(int argc, char **argv) {
8989
return 2;
9090
}
9191

92+
bool success = true;
93+
9294
for (const auto & test_kv : k_tests()) {
9395
std::vector<llama_token> res = llama_tokenize(ctx, test_kv.first, true);
9496
fprintf(stderr, "%s : '%s' tokenized to '%s'\n",
@@ -103,7 +105,8 @@ int main(int argc, char **argv) {
103105
}
104106

105107
if (!correct) {
106-
fprintf(stderr, "%s : failed test: '%s'\n", __func__, test_kv.first.c_str());
108+
fprintf(stderr, "%s : failed test: '%s'\n", __func__, test_kv.first.c_str());
109+
fprintf(stderr, "%s : detokenized to: '%s'\n", __func__, unescape_whitespace(ctx, test_kv.second).c_str());
107110
fprintf(stderr, "%s : expected tokens: ", __func__);
108111
for (const auto & t : test_kv.second) {
109112
fprintf(stderr, "%6d, ", t);
@@ -115,9 +118,7 @@ int main(int argc, char **argv) {
115118
}
116119
fprintf(stderr, "\n");
117120

118-
llama_free_model(model);
119-
llama_free(ctx);
120-
return 3;
121+
success = false;
121122
}
122123
}
123124

@@ -126,5 +127,5 @@ int main(int argc, char **argv) {
126127

127128
llama_backend_free();
128129

129-
return 0;
130+
return success ? 0 : 3;
130131
}

0 commit comments

Comments
 (0)