llama : fix tokenizer to use llama_char_to_byte
This commit is contained in:
parent
0ba5d488e5
commit
7b6ae89041
2 changed files with 23 additions and 10 deletions
22
llama.cpp
22
llama.cpp
|
@ -2303,6 +2303,18 @@ static uint8_t llama_byte_to_char(const llama_vocab & vocab, uint8_t byte) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static uint8_t llama_char_to_byte(const llama_vocab & vocab, uint8_t ch) {
|
||||||
|
if (llama_vocab_type(vocab) == "spm") {
|
||||||
|
return ch + 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (llama_vocab_type(vocab) == "bpe") {
|
||||||
|
return ch - 32;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
static std::string llama_escape_whitespace(const std::string& text) {
|
static std::string llama_escape_whitespace(const std::string& text) {
|
||||||
std::string result;
|
std::string result;
|
||||||
bool escaping = false;
|
bool escaping = false;
|
||||||
|
@ -2439,7 +2451,7 @@ private:
|
||||||
if (p == rev_merge.end()) {
|
if (p == rev_merge.end()) {
|
||||||
// output any symbols that did not form tokens as bytes.
|
// output any symbols that did not form tokens as bytes.
|
||||||
for (int j = 0; j < (int)symbol.n; ++j) {
|
for (int j = 0; j < (int)symbol.n; ++j) {
|
||||||
llama_vocab::id token_id = llama_byte_to_char(vocab_, symbol.text[j]);
|
llama_vocab::id token_id = llama_char_to_byte(vocab_, symbol.text[j]);
|
||||||
output.push_back(token_id);
|
output.push_back(token_id);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
@ -4871,8 +4883,8 @@ int llama_token_to_str_with_model(const struct llama_model * model, llama_token
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int llama_token_to_str(const struct llama_context * ctx, llama_token token, char * str, int length) {
|
int llama_token_to_str(const struct llama_context * ctx, llama_token token, char * buf, int length) {
|
||||||
return llama_token_to_str_with_model(&ctx->model, token, str, length);
|
return llama_token_to_str_with_model(&ctx->model, token, buf, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string llama_token_to_str(const struct llama_context * ctx, llama_token token) {
|
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
|
||||||
return std::string(result.data(), result.size());
|
return std::string(result.data(), result.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
int llama_token_to_str_bpe(const struct llama_context * ctx, llama_token token, char * str, int length) {
|
int llama_token_to_str_bpe(const struct llama_context * ctx, llama_token token, char * buf, int length) {
|
||||||
if (0 <= token && token < llama_n_vocab_from_model(&ctx->model)) {
|
if (0 <= token && token < llama_n_vocab_from_model(&ctx->model)) {
|
||||||
std::string result = ctx->model.vocab.id_to_token[token].tok;
|
std::string result = ctx->model.vocab.id_to_token[token].tok;
|
||||||
if (length < (int) result.length()) {
|
if (length < (int) result.length()) {
|
||||||
return -result.length();
|
return -result.length();
|
||||||
}
|
}
|
||||||
memcpy(str, result.c_str(), result.length());
|
memcpy(buf, result.c_str(), result.length());
|
||||||
return result.length();
|
return result.length();
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -89,6 +89,8 @@ int main(int argc, char **argv) {
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool success = true;
|
||||||
|
|
||||||
for (const auto & test_kv : k_tests()) {
|
for (const auto & test_kv : k_tests()) {
|
||||||
std::vector<llama_token> res = llama_tokenize(ctx, test_kv.first, true);
|
std::vector<llama_token> res = llama_tokenize(ctx, test_kv.first, true);
|
||||||
fprintf(stderr, "%s : '%s' tokenized to '%s'\n",
|
fprintf(stderr, "%s : '%s' tokenized to '%s'\n",
|
||||||
|
@ -104,6 +106,7 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
if (!correct) {
|
if (!correct) {
|
||||||
fprintf(stderr, "%s : failed test: '%s'\n", __func__, test_kv.first.c_str());
|
fprintf(stderr, "%s : failed test: '%s'\n", __func__, test_kv.first.c_str());
|
||||||
|
fprintf(stderr, "%s : detokenized to: '%s'\n", __func__, unescape_whitespace(ctx, test_kv.second).c_str());
|
||||||
fprintf(stderr, "%s : expected tokens: ", __func__);
|
fprintf(stderr, "%s : expected tokens: ", __func__);
|
||||||
for (const auto & t : test_kv.second) {
|
for (const auto & t : test_kv.second) {
|
||||||
fprintf(stderr, "%6d, ", t);
|
fprintf(stderr, "%6d, ", t);
|
||||||
|
@ -115,9 +118,7 @@ int main(int argc, char **argv) {
|
||||||
}
|
}
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
|
|
||||||
llama_free_model(model);
|
success = false;
|
||||||
llama_free(ctx);
|
|
||||||
return 3;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,5 +127,5 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
llama_backend_free();
|
llama_backend_free();
|
||||||
|
|
||||||
return 0;
|
return success ? 0 : 3;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue