Skip to content

Commit f253e04

Browse files
committed
llama : vocab fix names
1 parent 66343ab commit f253e04

File tree

5 files changed

+41
-51
lines changed

5 files changed

+41
-51
lines changed

src/llama-grammar.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ void llama_grammar_apply_impl(const struct llama_grammar & grammar, llama_token_
10941094
const llama_token id = cur_p->data[i].id;
10951095
const std::string & piece = grammar.vocab->cache_token_to_piece.at(id);
10961096

1097-
if (grammar.vocab->token_is_eog(id)) {
1097+
if (grammar.vocab->is_eog(id)) {
10981098
if (!allow_eog) {
10991099
cur_p->data[i].logit = -INFINITY;
11001100
}
@@ -1115,7 +1115,7 @@ void llama_grammar_apply_impl(const struct llama_grammar & grammar, llama_token_
11151115
void llama_grammar_accept_impl(struct llama_grammar & grammar, llama_token token) {
11161116
GGML_ASSERT(grammar.vocab != nullptr);
11171117

1118-
if (grammar.vocab->token_is_eog(token)) {
1118+
if (grammar.vocab->is_eog(token)) {
11191119
for (const auto & stack : grammar.stacks) {
11201120
if (stack.empty()) {
11211121
return;

src/llama-sampling.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,7 +2153,7 @@ static void llama_sampler_infill_apply(struct llama_sampler * smpl, llama_token_
21532153
float p_eog_sum = 0.0f;
21542154

21552155
for (size_t i = 0; i < cur_p->size; ++i) {
2156-
if (ctx->vocab->token_is_eog(cur_p->data[i].id)) {
2156+
if (ctx->vocab->is_eog(cur_p->data[i].id)) {
21572157
p_eog_sum += cur_p->data[i].p;
21582158
} else {
21592159
p_txt_sum += cur_p->data[i].p;
@@ -2175,7 +2175,7 @@ static void llama_sampler_infill_apply(struct llama_sampler * smpl, llama_token_
21752175
float p_sum = 0.0f;
21762176

21772177
for (size_t i = 0; i < size_org; ++i) {
2178-
if (ctx->vocab->token_is_eog(cur_p->data[i].id)) {
2178+
if (ctx->vocab->is_eog(cur_p->data[i].id)) {
21792179
p_sum += cur_p->data[i].p;
21802180

21812181
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_
22482248
LOG_DBG_CUR("%s: n_combined = %zu, applying thold = %.3f\n", __func__, n_combined, thold);
22492249

22502250
for (size_t i = 0; i < size_org; ++i) {
2251-
const bool is_eog = ctx->vocab->token_is_eog(cur_p->data[i].id);
2251+
const bool is_eog = ctx->vocab->is_eog(cur_p->data[i].id);
22522252

22532253
if (cur_p->data[i].p < thold && !is_eog) {
22542254
continue;
@@ -2291,7 +2291,7 @@ static void llama_sampler_infill_apply(struct llama_sampler * smpl, llama_token_
22912291
LOG_DBG_CUR("%s: applying thold = %.3f\n", __func__, thold);
22922292

22932293
for (size_t i = 0; i < size_org; ++i) {
2294-
const bool is_eog = ctx->vocab->token_is_eog(cur_p->data[i].id);
2294+
const bool is_eog = ctx->vocab->is_eog(cur_p->data[i].id);
22952295

22962296
if (cur_p->data[i].p < thold && !is_eog) {
22972297
continue;

src/llama-vocab.cpp

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -110,39 +110,43 @@ std::string llama_vocab::type_name() const{
110110
}
111111
}
112112

113-
bool llama_vocab::is_normal_token(llama_token id) const {
113+
bool llama_vocab::is_normal(llama_token id) const {
114114
GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE);
115115
return id_to_token[id].attr & LLAMA_TOKEN_ATTR_NORMAL;
116116
}
117117

118-
bool llama_vocab::is_unknown_token(llama_token id) const {
118+
bool llama_vocab::is_unknown(llama_token id) const {
119119
GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE);
120120
return id_to_token[id].attr & LLAMA_TOKEN_ATTR_UNKNOWN;
121121
}
122122

123-
bool llama_vocab::is_control_token(llama_token id) const {
123+
bool llama_vocab::is_control(llama_token id) const {
124124
GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE);
125125
return id_to_token[id].attr & LLAMA_TOKEN_ATTR_CONTROL;
126126
}
127127

128-
bool llama_vocab::is_byte_token(llama_token id) const {
128+
bool llama_vocab::is_byte(llama_token id) const {
129129
GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE);
130130
return id_to_token[id].attr & LLAMA_TOKEN_ATTR_BYTE;
131131
}
132132

133-
bool llama_vocab::is_user_defined_token(llama_token id) const {
133+
bool llama_vocab::is_user_defined(llama_token id) const {
134134
GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE);
135135
return id_to_token[id].attr & LLAMA_TOKEN_ATTR_USER_DEFINED;
136136
}
137137

138-
bool llama_vocab::is_unused_token(llama_token id) const {
138+
bool llama_vocab::is_unused(llama_token id) const {
139139
GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE);
140140
return id_to_token[id].attr & LLAMA_TOKEN_ATTR_UNUSED;
141141
}
142142

143+
bool llama_vocab::is_eog(llama_token id) const {
144+
return id != LLAMA_TOKEN_NULL && special_eog_ids.count(id) > 0;
145+
}
146+
143147
uint8_t llama_vocab::token_to_byte(llama_token id) const {
144148
GGML_ASSERT(get_type() != LLAMA_VOCAB_TYPE_NONE);
145-
GGML_ASSERT(is_byte_token(id));
149+
GGML_ASSERT(is_byte(id));
146150
const auto & token_data = id_to_token.at(id);
147151
switch (get_type()) {
148152
case LLAMA_VOCAB_TYPE_SPM:
@@ -832,18 +836,18 @@ struct llm_tokenizer_ugm : llm_tokenizer {
832836
for (unsigned int id = 0; id < vocab.id_to_token.size(); ++id) {
833837
const auto &token_data = vocab.id_to_token[id];
834838

835-
if (vocab.is_normal_token(id)) {
839+
if (vocab.is_normal(id)) {
836840
min_score = std::min<float>(min_score, token_data.score);
837841
max_score = std::max<float>(max_score, token_data.score);
838842
}
839843

840-
if (vocab.is_normal_token(id) ||
841-
vocab.is_user_defined_token(id) ||
842-
vocab.is_unused_token(id)) {
844+
if (vocab.is_normal(id) ||
845+
vocab.is_user_defined(id) ||
846+
vocab.is_unused(id)) {
843847
token_matcher.insert(token_data.text.data(), token_data.text.size(), id);
844848
}
845849

846-
if (vocab.is_user_defined_token(id)) {
850+
if (vocab.is_user_defined(id)) {
847851
user_defined_token_matcher.insert(token_data.text.data(), token_data.text.size());
848852
}
849853
}
@@ -928,7 +932,7 @@ struct llm_tokenizer_ugm_session {
928932
// (normal token scores are log probabilities, so they are negative)
929933
// score type is double here to make tokenization results exactly
930934
// the same as in the HF tokenizer using SentencePiece
931-
const double token_score = vocab.is_user_defined_token(token_id) ? 0.0 : token_data.score;
935+
const double token_score = vocab.is_user_defined(token_id) ? 0.0 : token_data.score;
932936
const double challenger_score = current_best.score_sum + token_score;
933937
struct best_tokenization & current_champ = tokenization_results[prefix_offset];
934938
if (challenger_score > current_champ.score_sum) {
@@ -1466,27 +1470,19 @@ llama_token llama_vocab::byte_to_token(uint8_t ch) const {
14661470
}
14671471
}
14681472

1469-
const char * llama_vocab::token_get_text(llama_token token) const {
1473+
const char * llama_vocab::token_get_text(llama_token id) const {
14701474
GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE);
1471-
return id_to_token[token].text.c_str();
1475+
return id_to_token[id].text.c_str();
14721476
}
14731477

1474-
float llama_vocab::token_get_score(llama_token token) const {
1478+
float llama_vocab::token_get_score(llama_token id) const {
14751479
GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE);
1476-
return id_to_token[token].score;
1480+
return id_to_token[id].score;
14771481
}
14781482

1479-
llama_token_attr llama_vocab::token_get_attr(llama_token token) const {
1483+
llama_token_attr llama_vocab::token_get_attr(llama_token id) const {
14801484
GGML_ASSERT(type != LLAMA_VOCAB_TYPE_NONE);
1481-
return id_to_token[token].attr;
1482-
}
1483-
1484-
bool llama_vocab::token_is_eog(llama_token token) const {
1485-
return token != LLAMA_TOKEN_NULL && special_eog_ids.count(token) > 0;
1486-
}
1487-
1488-
bool llama_vocab::token_is_control(llama_token token) const {
1489-
return is_control_token(token);
1485+
return id_to_token[id].attr;
14901486
}
14911487

14921488
llama_token llama_vocab::token_bos() const {

src/llama-vocab.h

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,20 @@ struct llama_vocab {
8383

8484
std::string type_name() const;
8585

86-
// TODO: fix names
87-
bool is_normal_token (llama_token id) const;
88-
bool is_unknown_token (llama_token id) const;
89-
bool is_control_token (llama_token id) const;
90-
bool is_byte_token (llama_token id) const;
91-
bool is_user_defined_token(llama_token id) const;
92-
bool is_unused_token (llama_token id) const;
86+
bool is_normal (llama_token id) const;
87+
bool is_unknown (llama_token id) const;
88+
bool is_control (llama_token id) const;
89+
bool is_byte (llama_token id) const;
90+
bool is_user_defined(llama_token id) const;
91+
bool is_unused (llama_token id) const;
92+
bool is_eog (llama_token id) const;
9393

9494
uint8_t token_to_byte(llama_token id) const;
9595
llama_token byte_to_token(uint8_t ch) const;
9696

97-
const char * token_get_text(llama_token token) const;
98-
99-
float token_get_score(llama_token token) const;
100-
101-
llama_token_attr token_get_attr(llama_token token) const;
102-
103-
bool token_is_eog(llama_token token) const;
104-
105-
bool token_is_control(llama_token token) const;
97+
const char * token_get_text (llama_token id) const;
98+
float token_get_score(llama_token id) const;
99+
llama_token_attr token_get_attr (llama_token id) const;
106100

107101
llama_token token_bos() const;
108102
llama_token token_eos() const;

src/llama.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9816,11 +9816,11 @@ enum llama_token_attr llama_token_get_attr(const struct llama_model * model, lla
98169816
}
98179817

98189818
bool llama_token_is_eog(const struct llama_model * model, llama_token token) {
9819-
return model->vocab.token_is_eog(token);
9819+
return model->vocab.is_eog(token);
98209820
}
98219821

98229822
bool llama_token_is_control(const struct llama_model * model, llama_token token) {
9823-
return model->vocab.token_is_control(token);
9823+
return model->vocab.is_control(token);
98249824
}
98259825

98269826
llama_token llama_token_bos(const struct llama_model * model) {

0 commit comments

Comments
 (0)