More sentencepiece compatibility by eliminating magic numbers
This commit is contained in:
parent
da837401cd
commit
aea173f5af
2 changed files with 19 additions and 28 deletions
39
llama.cpp
39
llama.cpp
|
@ -2368,6 +2368,11 @@ static bool llama_is_eos_token(const llama_vocab & vocab, llama_token id ) {
|
||||||
return id == vocab.special_eos_id;
|
return id == vocab.special_eos_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool llama_is_pad_token(const llama_vocab & vocab, llama_token id ) {
|
||||||
|
GGML_ASSERT(id < 0 || llama_is_control_token(vocab, id));
|
||||||
|
return id == vocab.special_pad_id;
|
||||||
|
}
|
||||||
|
|
||||||
static bool llama_is_user_defined_token(const llama_vocab & vocab, llama_token id) {
|
static bool llama_is_user_defined_token(const llama_vocab & vocab, llama_token id) {
|
||||||
return vocab.id_to_token[id].toktype == 4;
|
return vocab.id_to_token[id].toktype == 4;
|
||||||
}
|
}
|
||||||
|
@ -2380,28 +2385,18 @@ static bool llama_is_byte_token(const llama_vocab & vocab, llama_token id) {
|
||||||
return vocab.id_to_token[id].toktype == 6;
|
return vocab.id_to_token[id].toktype == 6;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t llama_byte_to_char(const llama_vocab & vocab, uint8_t byte) {
|
static uint8_t llama_token_to_byte(const llama_vocab & vocab, llama_token id) {
|
||||||
if (llama_vocab_get_type(vocab) == LLAMA_VOCAB_TYPE_SPM) {
|
GGML_ASSERT(llama_is_byte_token(vocab, id));
|
||||||
return byte - 3;
|
const auto& token_data = vocab.id_to_token.at(id);
|
||||||
|
auto buf = token_data.tok.substr(3, 2);
|
||||||
|
return strtol(buf.c_str(), NULL, 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (llama_vocab_get_type(vocab) == LLAMA_VOCAB_TYPE_BPE) {
|
static llama_token llama_byte_to_token(const llama_vocab & vocab, uint8_t ch) {
|
||||||
return byte + 32;
|
char buf[7];
|
||||||
}
|
int result = snprintf(buf, sizeof(buf), "<0x%02X>", ch);
|
||||||
|
GGML_ASSERT(0 <= result && result < 7);
|
||||||
return false;
|
return vocab.token_to_id.at(buf);
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t llama_char_to_byte(const llama_vocab & vocab, uint8_t ch) {
|
|
||||||
if (llama_vocab_get_type(vocab) == LLAMA_VOCAB_TYPE_SPM) {
|
|
||||||
return ch + 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (llama_vocab_get_type(vocab) == LLAMA_VOCAB_TYPE_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) {
|
||||||
|
@ -2540,7 +2535,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_char_to_byte(vocab_, symbol.text[j]);
|
llama_vocab::id token_id = llama_byte_to_token(vocab_, symbol.text[j]);
|
||||||
output.push_back(token_id);
|
output.push_back(token_id);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
@ -5080,7 +5075,7 @@ int llama_token_to_str_with_model(const struct llama_model * model, llama_token
|
||||||
if (length < 1) {
|
if (length < 1) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
buf[0] = llama_byte_to_char(model->vocab, token);
|
buf[0] = llama_token_to_byte(model->vocab, token);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,10 +10,6 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <locale>
|
#include <locale>
|
||||||
|
|
||||||
static std::string vocab_type(llama_context * ctx) {
|
|
||||||
return llama_n_vocab(ctx) == 32000 ? "spm": "bpe";
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::string escape_whitespace(const std::string& text) {
|
static std::string escape_whitespace(const std::string& text) {
|
||||||
std::string result;
|
std::string result;
|
||||||
bool escaping = false;
|
bool escaping = false;
|
||||||
|
@ -91,8 +87,8 @@ int main(int argc, char **argv) {
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if ((vocab_type(ctx) == "spm" && i <= 258) ||
|
// TODO: needs access to token types
|
||||||
(vocab_type(ctx) == "bpe" && (i == 0 || i >= 100000))) {
|
if (0 <= i && i < 259) {
|
||||||
fprintf(stderr, "%s : info: token %d is string %s and bpe returns tokens %s\n",
|
fprintf(stderr, "%s : info: token %d is string %s and bpe returns tokens %s\n",
|
||||||
__func__, i, llama_token_to_str(ctx, i).c_str(), unescape_whitespace(ctx, tokens).c_str());
|
__func__, i, llama_token_to_str(ctx, i).c_str(), unescape_whitespace(ctx, tokens).c_str());
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue