llama : vocab fix names

This commit is contained in:
Georgi Gerganov 2025-01-08 20:03:07 +02:00
parent 2c9f20d4bb
commit 695a0037db
No known key found for this signature in database
GPG key ID: 449E073F9DC10735
5 changed files with 41 additions and 51 deletions

View file

@ -1094,7 +1094,7 @@ void llama_grammar_apply_impl(const struct llama_grammar & grammar, llama_token_
const llama_token id = cur_p->data[i].id; const llama_token id = cur_p->data[i].id;
const std::string & piece = grammar.vocab->cache_token_to_piece.at(id); const std::string & piece = grammar.vocab->cache_token_to_piece.at(id);
if (grammar.vocab->token_is_eog(id)) { if (grammar.vocab->is_eog(id)) {
if (!allow_eog) { if (!allow_eog) {
cur_p->data[i].logit = -INFINITY; cur_p->data[i].logit = -INFINITY;
} }
@ -1115,7 +1115,7 @@ void llama_grammar_apply_impl(const struct llama_grammar & grammar, llama_token_
void llama_grammar_accept_impl(struct llama_grammar & grammar, llama_token token) { void llama_grammar_accept_impl(struct llama_grammar & grammar, llama_token token) {
GGML_ASSERT(grammar.vocab != nullptr); GGML_ASSERT(grammar.vocab != nullptr);
if (grammar.vocab->token_is_eog(token)) { if (grammar.vocab->is_eog(token)) {
for (const auto & stack : grammar.stacks) { for (const auto & stack : grammar.stacks) {
if (stack.empty()) { if (stack.empty()) {
return; return;

View file

@ -2153,7 +2153,7 @@ static void llama_sampler_infill_apply(struct llama_sampler * smpl, llama_token_
float p_eog_sum = 0.0f; float p_eog_sum = 0.0f;
for (size_t i = 0; i < cur_p->size; ++i) { for (size_t i = 0; i < cur_p->size; ++i) {
if (ctx->vocab->token_is_eog(cur_p->data[i].id)) { if (ctx->vocab->is_eog(cur_p->data[i].id)) {
p_eog_sum += cur_p->data[i].p; p_eog_sum += cur_p->data[i].p;
} else { } else {
p_txt_sum += cur_p->data[i].p; p_txt_sum += cur_p->data[i].p;
@ -2175,7 +2175,7 @@ static void llama_sampler_infill_apply(struct llama_sampler * smpl, llama_token_
float p_sum = 0.0f; float p_sum = 0.0f;
for (size_t i = 0; i < size_org; ++i) { for (size_t i = 0; i < size_org; ++i) {
if (ctx->vocab->token_is_eog(cur_p->data[i].id)) { if (ctx->vocab->is_eog(cur_p->data[i].id)) {
p_sum += cur_p->data[i].p; p_sum += cur_p->data[i].p;
cur_p->data[cur_p->size++] = cur_p->data[i]; cur_p->data[cur_p->size++] = cur_p->data[i];
@ -2248,7 +2248,7 @@ static void llama_sampler_infill_apply(struct llama_sampler * smpl, llama_token_
LOG_DBG_CUR("%s: n_combined = %zu, applying thold = %.3f\n", __func__, n_combined, thold); LOG_DBG_CUR("%s: n_combined = %zu, applying thold = %.3f\n", __func__, n_combined, thold);
for (size_t i = 0; i < size_org; ++i) { for (size_t i = 0; i < size_org; ++i) {
const bool is_eog = ctx->vocab->token_is_eog(cur_p->data[i].id); const bool is_eog = ctx->vocab->is_eog(cur_p->data[i].id);
if (cur_p->data[i].p < thold && !is_eog) { if (cur_p->data[i].p < thold && !is_eog) {
continue; continue;
@ -2291,7 +2291,7 @@ static void llama_sampler_infill_apply(struct llama_sampler * smpl, llama_token_
LOG_DBG_CUR("%s: applying thold = %.3f\n", __func__, thold); LOG_DBG_CUR("%s: applying thold = %.3f\n", __func__, thold);
for (size_t i = 0; i < size_org; ++i) { for (size_t i = 0; i < size_org; ++i) {
const bool is_eog = ctx->vocab->token_is_eog(cur_p->data[i].id); const bool is_eog = ctx->vocab->is_eog(cur_p->data[i].id);
if (cur_p->data[i].p < thold && !is_eog) { if (cur_p->data[i].p < thold && !is_eog) {
continue; continue;

View file

@ -110,39 +110,43 @@ std::string llama_vocab::type_name() const{
} }
} }
bool llama_vocab::is_normal_token(llama_token id) const { bool llama_vocab::is_normal(llama_token id) const {
GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE); GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE);
return id_to_token[id].attr & LLAMA_TOKEN_ATTR_NORMAL; return id_to_token[id].attr & LLAMA_TOKEN_ATTR_NORMAL;
} }
bool llama_vocab::is_unknown_token(llama_token id) const { bool llama_vocab::is_unknown(llama_token id) const {
GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE); GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE);
return id_to_token[id].attr & LLAMA_TOKEN_ATTR_UNKNOWN; return id_to_token[id].attr & LLAMA_TOKEN_ATTR_UNKNOWN;
} }
bool llama_vocab::is_control_token(llama_token id) const { bool llama_vocab::is_control(llama_token id) const {
GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE); GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE);
return id_to_token[id].attr & LLAMA_TOKEN_ATTR_CONTROL; return id_to_token[id].attr & LLAMA_TOKEN_ATTR_CONTROL;
} }
bool llama_vocab::is_byte_token(llama_token id) const { bool llama_vocab::is_byte(llama_token id) const {
GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE); GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE);
return id_to_token[id].attr & LLAMA_TOKEN_ATTR_BYTE; return id_to_token[id].attr & LLAMA_TOKEN_ATTR_BYTE;
} }
bool llama_vocab::is_user_defined_token(llama_token id) const { bool llama_vocab::is_user_defined(llama_token id) const {
GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE); GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE);
return id_to_token[id].attr & LLAMA_TOKEN_ATTR_USER_DEFINED; return id_to_token[id].attr & LLAMA_TOKEN_ATTR_USER_DEFINED;
} }
bool llama_vocab::is_unused_token(llama_token id) const { bool llama_vocab::is_unused(llama_token id) const {
GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE); GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE);
return id_to_token[id].attr & LLAMA_TOKEN_ATTR_UNUSED; return id_to_token[id].attr & LLAMA_TOKEN_ATTR_UNUSED;
} }
bool llama_vocab::is_eog(llama_token id) const {
return id != LLAMA_TOKEN_NULL && special_eog_ids.count(id) > 0;
}
uint8_t llama_vocab::token_to_byte(llama_token id) const { uint8_t llama_vocab::token_to_byte(llama_token id) const {
GGML_ASSERT(get_type() != LLAMA_VOCAB_TYPE_NONE); GGML_ASSERT(get_type() != LLAMA_VOCAB_TYPE_NONE);
GGML_ASSERT(is_byte_token(id)); GGML_ASSERT(is_byte(id));
const auto & token_data = id_to_token.at(id); const auto & token_data = id_to_token.at(id);
switch (get_type()) { switch (get_type()) {
case LLAMA_VOCAB_TYPE_SPM: case LLAMA_VOCAB_TYPE_SPM:
@ -832,18 +836,18 @@ struct llm_tokenizer_ugm : llm_tokenizer {
for (unsigned int id = 0; id < vocab.id_to_token.size(); ++id) { for (unsigned int id = 0; id < vocab.id_to_token.size(); ++id) {
const auto &token_data = vocab.id_to_token[id]; const auto &token_data = vocab.id_to_token[id];
if (vocab.is_normal_token(id)) { if (vocab.is_normal(id)) {
min_score = std::min<float>(min_score, token_data.score); min_score = std::min<float>(min_score, token_data.score);
max_score = std::max<float>(max_score, token_data.score); max_score = std::max<float>(max_score, token_data.score);
} }
if (vocab.is_normal_token(id) || if (vocab.is_normal(id) ||
vocab.is_user_defined_token(id) || vocab.is_user_defined(id) ||
vocab.is_unused_token(id)) { vocab.is_unused(id)) {
token_matcher.insert(token_data.text.data(), token_data.text.size(), id); token_matcher.insert(token_data.text.data(), token_data.text.size(), id);
} }
if (vocab.is_user_defined_token(id)) { if (vocab.is_user_defined(id)) {
user_defined_token_matcher.insert(token_data.text.data(), token_data.text.size()); user_defined_token_matcher.insert(token_data.text.data(), token_data.text.size());
} }
} }
@ -928,7 +932,7 @@ struct llm_tokenizer_ugm_session {
// (normal token scores are log probabilities, so they are negative) // (normal token scores are log probabilities, so they are negative)
// score type is double here to make tokenization results exactly // score type is double here to make tokenization results exactly
// the same as in the HF tokenizer using SentencePiece // the same as in the HF tokenizer using SentencePiece
const double token_score = vocab.is_user_defined_token(token_id) ? 0.0 : token_data.score; const double token_score = vocab.is_user_defined(token_id) ? 0.0 : token_data.score;
const double challenger_score = current_best.score_sum + token_score; const double challenger_score = current_best.score_sum + token_score;
struct best_tokenization & current_champ = tokenization_results[prefix_offset]; struct best_tokenization & current_champ = tokenization_results[prefix_offset];
if (challenger_score > current_champ.score_sum) { if (challenger_score > current_champ.score_sum) {
@ -1466,27 +1470,19 @@ llama_token llama_vocab::byte_to_token(uint8_t ch) const {
} }
} }
const char * llama_vocab::token_get_text(llama_token token) const { const char * llama_vocab::token_get_text(llama_token id) const {
GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE); GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE);
return id_to_token[token].text.c_str(); return id_to_token[id].text.c_str();
} }
float llama_vocab::token_get_score(llama_token token) const { float llama_vocab::token_get_score(llama_token id) const {
GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE); GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE);
return id_to_token[token].score; return id_to_token[id].score;
} }
llama_token_attr llama_vocab::token_get_attr(llama_token token) const { llama_token_attr llama_vocab::token_get_attr(llama_token id) const {
GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE); GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE);
return id_to_token[token].attr; return id_to_token[id].attr;
}
bool llama_vocab::token_is_eog(llama_token token) const {
return token != LLAMA_TOKEN_NULL && special_eog_ids.count(token) > 0;
}
bool llama_vocab::token_is_control(llama_token token) const {
return is_control_token(token);
} }
llama_token llama_vocab::token_bos() const { llama_token llama_vocab::token_bos() const {

View file

@ -83,26 +83,20 @@ struct llama_vocab {
std::string type_name() const; std::string type_name() const;
// TODO: fix names bool is_normal (llama_token id) const;
bool is_normal_token (llama_token id) const; bool is_unknown (llama_token id) const;
bool is_unknown_token (llama_token id) const; bool is_control (llama_token id) const;
bool is_control_token (llama_token id) const; bool is_byte (llama_token id) const;
bool is_byte_token (llama_token id) const; bool is_user_defined(llama_token id) const;
bool is_user_defined_token(llama_token id) const; bool is_unused (llama_token id) const;
bool is_unused_token (llama_token id) const; bool is_eog (llama_token id) const;
uint8_t token_to_byte(llama_token id) const; uint8_t token_to_byte(llama_token id) const;
llama_token byte_to_token(uint8_t ch) const; llama_token byte_to_token(uint8_t ch) const;
const char * token_get_text(llama_token token) const; const char * token_get_text (llama_token id) const;
float token_get_score(llama_token id) const;
float token_get_score(llama_token token) const; llama_token_attr token_get_attr (llama_token id) const;
llama_token_attr token_get_attr(llama_token token) const;
bool token_is_eog(llama_token token) const;
bool token_is_control(llama_token token) const;
llama_token token_bos() const; llama_token token_bos() const;
llama_token token_eos() const; llama_token token_eos() const;

View file

@ -9973,11 +9973,11 @@ enum llama_token_attr llama_token_get_attr(const struct llama_model * model, lla
} }
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) {
return model->vocab.token_is_eog(token); return model->vocab.is_eog(token);
} }
bool llama_token_is_control(const struct llama_model * model, llama_token token) { bool llama_token_is_control(const struct llama_model * model, llama_token token) {
return model->vocab.token_is_control(token); return model->vocab.is_control(token);
} }
llama_token llama_token_bos(const struct llama_model * model) { llama_token llama_token_bos(const struct llama_model * model) {