Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix showing unknown token at gpt_tokenize #801

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
18 changes: 17 additions & 1 deletion examples/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,22 +325,38 @@ std::vector<gpt_vocab::id> gpt_tokenize(const gpt_vocab & vocab, const std::stri

// find the longest token that forms each word in words:
std::vector<gpt_vocab::id> tokens;
// unknown token
std::vector<char> unknown;
unknown.clear();
Comment on lines +328 to +330
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// unknown token
std::vector<char> unknown;
unknown.clear();
// unknown token
std::vector<char> unknown;

for (const auto & word : words) {
for (int i = 0; i < (int) word.size(); ){
for (int j = word.size() - 1; j >= i; j--){
auto cand = word.substr(i, j-i+1);
auto it = vocab.token_to_id.find(cand);
if (it != vocab.token_to_id.end()){ // word.substr(i, j-i+1) in vocab
if (!unknown.empty()){
unknown.push_back(0); // terminator
std::string unkstr(unknown.begin(), unknown.end());
fprintf(stderr, "%s: unknown token '%s'\n", __func__, unkstr.data());
Comment on lines +339 to +340
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::string unkstr(unknown.begin(), unknown.end());
fprintf(stderr, "%s: unknown token '%s'\n", __func__, unkstr.data());
fprintf(stderr, "%s: unknown token '%s'\n", __func__, unknown.data());

unknown.clear();
}
tokens.push_back(it->second);
i = j + 1;
break;
}
else if (j == i){ // word.substr(i, 1) has no matching
fprintf(stderr, "%s: unknown token '%s'\n", __func__, word.substr(i, 1).data());
auto unk = word.substr(i, 1).data();
unknown.push_back(*unk);
Comment on lines +348 to +349
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this just:

Suggested change
auto unk = word.substr(i, 1).data();
unknown.push_back(*unk);
unknown.push_back(word[i]);

i++;
}
}
}
if (!unknown.empty()){
unknown.push_back(0); // terminator
std::string unkstr(unknown.begin(), unknown.end());
fprintf(stderr, "%s: unknown token '%s'\n", __func__, unkstr.data());
Comment on lines +356 to +357
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::string unkstr(unknown.begin(), unknown.end());
fprintf(stderr, "%s: unknown token '%s'\n", __func__, unkstr.data());
fprintf(stderr, "%s: unknown token '%s'\n", __func__, unknown.data());

unknown.clear();
}
}

return tokens;
Expand Down
Loading