Replace llama_token_type with llama_token_attribs
This commit is contained in:
parent
54e9f23b8a
commit
ac40ff0e50
2 changed files with 37 additions and 38 deletions
55
llama.cpp
55
llama.cpp
|
@ -2150,13 +2150,12 @@ struct llama_vocab {
|
||||||
using id = int32_t;
|
using id = int32_t;
|
||||||
using token = std::string;
|
using token = std::string;
|
||||||
using ttype = llama_token_type;
|
using ttype = llama_token_type;
|
||||||
using tattrib = llama_token_attrib;
|
using tattribs = llama_token_attribs;
|
||||||
|
|
||||||
struct token_data {
|
struct token_data {
|
||||||
token text;
|
token text;
|
||||||
float score;
|
float score;
|
||||||
ttype type;
|
tattribs attribs;
|
||||||
tattrib attribs;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
enum llama_vocab_type type = LLAMA_VOCAB_TYPE_SPM;
|
enum llama_vocab_type type = LLAMA_VOCAB_TYPE_SPM;
|
||||||
|
@ -4742,7 +4741,20 @@ static void llm_load_vocab(
|
||||||
auto & token_data = vocab.id_to_token[i];
|
auto & token_data = vocab.id_to_token[i];
|
||||||
token_data.text = std::move(word);
|
token_data.text = std::move(word);
|
||||||
token_data.score = scores ? scores[i] : 0.0f;
|
token_data.score = scores ? scores[i] : 0.0f;
|
||||||
token_data.type = toktypes ? (llama_token_type) toktypes[i] : LLAMA_TOKEN_TYPE_NORMAL;
|
token_data.attribs = LLAMA_TOKEN_ATTRIB_NORMAL;
|
||||||
|
|
||||||
|
if (toktypes) { //TODO: remove, required until per token attribs are available from GGUF file
|
||||||
|
switch(toktypes[i]) {
|
||||||
|
case LLAMA_TOKEN_TYPE_UNKNOWN: token_data.attribs = LLAMA_TOKEN_ATTRIB_UNKNOWN; break;
|
||||||
|
case LLAMA_TOKEN_TYPE_UNUSED: token_data.attribs = LLAMA_TOKEN_ATTRIB_UNUSED; break;
|
||||||
|
case LLAMA_TOKEN_TYPE_NORMAL: token_data.attribs = LLAMA_TOKEN_ATTRIB_NORMAL; break;
|
||||||
|
case LLAMA_TOKEN_TYPE_CONTROL: token_data.attribs = LLAMA_TOKEN_ATTRIB_CONTROL; break;
|
||||||
|
case LLAMA_TOKEN_TYPE_USER_DEFINED: token_data.attribs = LLAMA_TOKEN_ATTRIB_USER_DEFINED; break;
|
||||||
|
case LLAMA_TOKEN_TYPE_BYTE: token_data.attribs = LLAMA_TOKEN_ATTRIB_BYTE; break;
|
||||||
|
case LLAMA_TOKEN_TYPE_UNDEFINED: token_data.attribs = LLAMA_TOKEN_ATTRIB_UNDEFINED; break;
|
||||||
|
default: token_data.attribs = LLAMA_TOKEN_ATTRIB_UNDEFINED; break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
GGML_ASSERT(vocab.id_to_token.size() == vocab.token_to_id.size());
|
GGML_ASSERT(vocab.id_to_token.size() == vocab.token_to_id.size());
|
||||||
|
|
||||||
|
@ -4833,7 +4845,7 @@ static void llm_load_vocab(
|
||||||
// build special tokens cache
|
// build special tokens cache
|
||||||
{
|
{
|
||||||
for (llama_vocab::id id = 0; id < (llama_vocab::id)n_vocab; ++id) {
|
for (llama_vocab::id id = 0; id < (llama_vocab::id)n_vocab; ++id) {
|
||||||
if (vocab.id_to_token[id].type != LLAMA_TOKEN_TYPE_NORMAL) {
|
if (!(vocab.id_to_token[id].attribs & LLAMA_TOKEN_ATTRIB_NORMAL)) {
|
||||||
vocab.cache_special_tokens.push_back(id);
|
vocab.cache_special_tokens.push_back(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4871,7 +4883,6 @@ static void llm_load_vocab(
|
||||||
// Handle per token attributes
|
// Handle per token attributes
|
||||||
//NOTE: Each model customizes per token attributes.
|
//NOTE: Each model customizes per token attributes.
|
||||||
//NOTE: Per token attributes are missing from the GGUF file.
|
//NOTE: Per token attributes are missing from the GGUF file.
|
||||||
//TODO: Merge llama_token_type and llama_token_attrib.
|
|
||||||
//TODO: Extract attribs from GGUF file.
|
//TODO: Extract attribs from GGUF file.
|
||||||
{
|
{
|
||||||
auto _contains_any = [] (const std::string &str, const std::vector<std::string> &substrs) -> bool {
|
auto _contains_any = [] (const std::string &str, const std::vector<std::string> &substrs) -> bool {
|
||||||
|
@ -4883,28 +4894,16 @@ static void llm_load_vocab(
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
auto _set_tokenid_attrib = [&] (const llama_vocab::id id, llama_token_attrib attrib, bool value) {
|
auto _set_tokenid_attrib = [&] (const llama_vocab::id id, llama_token_attribs attrib, bool value) {
|
||||||
uint32_t attribs = vocab.id_to_token.at(id).attribs;
|
uint32_t attribs = vocab.id_to_token.at(id).attribs;
|
||||||
attribs = value ? (attribs | attrib) : (attribs & ~attrib);
|
attribs = value ? (attribs | attrib) : (attribs & ~attrib);
|
||||||
vocab.id_to_token[id].attribs = (llama_token_attrib) attribs;
|
vocab.id_to_token[id].attribs = (llama_token_attribs) attribs;
|
||||||
};
|
};
|
||||||
|
|
||||||
auto _set_token_attrib = [&] (const std::string & token, llama_token_attrib attrib, bool value) {
|
auto _set_token_attrib = [&] (const std::string & token, llama_token_attribs attrib, bool value) {
|
||||||
_set_tokenid_attrib(vocab.token_to_id.at(token), attrib, value);
|
_set_tokenid_attrib(vocab.token_to_id.at(token), attrib, value);
|
||||||
};
|
};
|
||||||
|
|
||||||
// convert token type as an attribute
|
|
||||||
for (auto &data : vocab.id_to_token) {
|
|
||||||
uint32_t attrib = LLAMA_TOKEN_ATTRIB_UNDEFINED;
|
|
||||||
attrib |= LLAMA_TOKEN_ATTRIB_UNKNOWN * (data.type == LLAMA_TOKEN_TYPE_UNKNOWN);
|
|
||||||
attrib |= LLAMA_TOKEN_ATTRIB_UNUSED * (data.type == LLAMA_TOKEN_TYPE_UNUSED);
|
|
||||||
attrib |= LLAMA_TOKEN_ATTRIB_NORMAL * (data.type == LLAMA_TOKEN_TYPE_NORMAL);
|
|
||||||
attrib |= LLAMA_TOKEN_ATTRIB_CONTROL * (data.type == LLAMA_TOKEN_TYPE_CONTROL);
|
|
||||||
attrib |= LLAMA_TOKEN_ATTRIB_USER_DEFINED * (data.type == LLAMA_TOKEN_TYPE_USER_DEFINED);
|
|
||||||
attrib |= LLAMA_TOKEN_ATTRIB_BYTE * (data.type == LLAMA_TOKEN_TYPE_BYTE);
|
|
||||||
data.attribs = (llama_token_attrib) attrib;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string model_name;
|
std::string model_name;
|
||||||
std::string tokenizer_pre;
|
std::string tokenizer_pre;
|
||||||
|
|
||||||
|
@ -12684,27 +12683,27 @@ static enum llama_vocab_type llama_vocab_get_type(const llama_vocab & vocab) {
|
||||||
|
|
||||||
static bool llama_is_normal_token(const llama_vocab & vocab, llama_token id) {
|
static bool llama_is_normal_token(const llama_vocab & vocab, llama_token id) {
|
||||||
GGML_ASSERT(vocab.type != LLAMA_VOCAB_TYPE_NONE);
|
GGML_ASSERT(vocab.type != LLAMA_VOCAB_TYPE_NONE);
|
||||||
return vocab.id_to_token[id].type == LLAMA_TOKEN_TYPE_NORMAL;
|
return vocab.id_to_token[id].attribs & LLAMA_TOKEN_ATTRIB_NORMAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool llama_is_unknown_token(const llama_vocab & vocab, llama_token id) {
|
static bool llama_is_unknown_token(const llama_vocab & vocab, llama_token id) {
|
||||||
GGML_ASSERT(vocab.type != LLAMA_VOCAB_TYPE_NONE);
|
GGML_ASSERT(vocab.type != LLAMA_VOCAB_TYPE_NONE);
|
||||||
return vocab.id_to_token[id].type == LLAMA_TOKEN_TYPE_UNKNOWN;
|
return vocab.id_to_token[id].attribs & LLAMA_TOKEN_ATTRIB_UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool llama_is_control_token(const llama_vocab & vocab, llama_token id) {
|
static bool llama_is_control_token(const llama_vocab & vocab, llama_token id) {
|
||||||
GGML_ASSERT(vocab.type != LLAMA_VOCAB_TYPE_NONE);
|
GGML_ASSERT(vocab.type != LLAMA_VOCAB_TYPE_NONE);
|
||||||
return vocab.id_to_token[id].type == LLAMA_TOKEN_TYPE_CONTROL;
|
return vocab.id_to_token[id].attribs & LLAMA_TOKEN_ATTRIB_CONTROL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool llama_is_byte_token(const llama_vocab & vocab, llama_token id) {
|
static bool llama_is_byte_token(const llama_vocab & vocab, llama_token id) {
|
||||||
GGML_ASSERT(vocab.type != LLAMA_VOCAB_TYPE_NONE);
|
GGML_ASSERT(vocab.type != LLAMA_VOCAB_TYPE_NONE);
|
||||||
return vocab.id_to_token[id].type == LLAMA_TOKEN_TYPE_BYTE;
|
return vocab.id_to_token[id].attribs & LLAMA_TOKEN_ATTRIB_BYTE;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
GGML_ASSERT(vocab.type != LLAMA_VOCAB_TYPE_NONE);
|
GGML_ASSERT(vocab.type != LLAMA_VOCAB_TYPE_NONE);
|
||||||
return vocab.id_to_token[id].type == LLAMA_TOKEN_TYPE_USER_DEFINED;
|
return vocab.id_to_token[id].attribs & LLAMA_TOKEN_ATTRIB_USER_DEFINED;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t llama_token_to_byte(const llama_vocab& vocab, llama_token id) {
|
static uint8_t llama_token_to_byte(const llama_vocab& vocab, llama_token id) {
|
||||||
|
@ -18277,9 +18276,9 @@ float llama_token_get_score(const struct llama_model * model, llama_token token)
|
||||||
return model->vocab.id_to_token[token].score;
|
return model->vocab.id_to_token[token].score;
|
||||||
}
|
}
|
||||||
|
|
||||||
llama_token_type llama_token_get_type(const struct llama_model * model, llama_token token) {
|
llama_token_attribs llama_token_get_attribs(const struct llama_model * model, llama_token token) {
|
||||||
GGML_ASSERT(model->vocab.type != LLAMA_VOCAB_TYPE_NONE);
|
GGML_ASSERT(model->vocab.type != LLAMA_VOCAB_TYPE_NONE);
|
||||||
return model->vocab.id_to_token[token].type;
|
return model->vocab.id_to_token[token].attribs;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool llama_token_is_eog(const struct llama_model * model, llama_token token) {
|
bool llama_token_is_eog(const struct llama_model * model, llama_token token) {
|
||||||
|
|
6
llama.h
6
llama.h
|
@ -97,7 +97,7 @@ extern "C" {
|
||||||
LLAMA_ROPE_TYPE_GLM = 4,
|
LLAMA_ROPE_TYPE_GLM = 4,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum llama_token_type {
|
enum llama_token_type { //TODO: remove, required until per token attribs are available from GGUF file
|
||||||
LLAMA_TOKEN_TYPE_UNDEFINED = 0,
|
LLAMA_TOKEN_TYPE_UNDEFINED = 0,
|
||||||
LLAMA_TOKEN_TYPE_NORMAL = 1,
|
LLAMA_TOKEN_TYPE_NORMAL = 1,
|
||||||
LLAMA_TOKEN_TYPE_UNKNOWN = 2,
|
LLAMA_TOKEN_TYPE_UNKNOWN = 2,
|
||||||
|
@ -107,7 +107,7 @@ extern "C" {
|
||||||
LLAMA_TOKEN_TYPE_BYTE = 6,
|
LLAMA_TOKEN_TYPE_BYTE = 6,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum llama_token_attrib {
|
enum llama_token_attribs {
|
||||||
LLAMA_TOKEN_ATTRIB_UNDEFINED = 0,
|
LLAMA_TOKEN_ATTRIB_UNDEFINED = 0,
|
||||||
LLAMA_TOKEN_ATTRIB_UNKNOWN = 1 << 1,
|
LLAMA_TOKEN_ATTRIB_UNKNOWN = 1 << 1,
|
||||||
LLAMA_TOKEN_ATTRIB_UNUSED = 1 << 2,
|
LLAMA_TOKEN_ATTRIB_UNUSED = 1 << 2,
|
||||||
|
@ -835,7 +835,7 @@ extern "C" {
|
||||||
|
|
||||||
LLAMA_API float llama_token_get_score(const struct llama_model * model, llama_token token);
|
LLAMA_API float llama_token_get_score(const struct llama_model * model, llama_token token);
|
||||||
|
|
||||||
LLAMA_API enum llama_token_type llama_token_get_type(const struct llama_model * model, llama_token token);
|
LLAMA_API enum llama_token_attribs llama_token_get_attribs(const struct llama_model * model, llama_token token);
|
||||||
|
|
||||||
// Check if the token is supposed to end generation (end-of-generation, eg. EOS, EOT, etc.)
|
// Check if the token is supposed to end generation (end-of-generation, eg. EOS, EOT, etc.)
|
||||||
LLAMA_API bool llama_token_is_eog(const struct llama_model * model, llama_token token);
|
LLAMA_API bool llama_token_is_eog(const struct llama_model * model, llama_token token);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue