Skip to content

Commit d8ded9f

Browse files
committed
vocab : llama_vocab_add_[be]os -> llama_vocab_get_add_[be]os (#11174)
ggml-ci
1 parent 6df37bc commit d8ded9f

File tree

15 files changed

+46
-45
lines changed

15 files changed

+46
-45
lines changed

common/speculative.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ bool common_speculative_are_compatible(
9494
return false;
9595
}
9696

97-
if (llama_vocab_add_bos(vocab_tgt) != llama_vocab_add_bos(vocab_dft) ||
98-
llama_vocab_add_eos(vocab_tgt) != llama_vocab_add_eos(vocab_dft) ||
97+
if (llama_vocab_get_add_bos(vocab_tgt) != llama_vocab_get_add_bos(vocab_dft) ||
98+
llama_vocab_get_add_eos(vocab_tgt) != llama_vocab_get_add_eos(vocab_dft) ||
9999
llama_vocab_bos(vocab_tgt) != llama_vocab_bos(vocab_dft) ||
100100
llama_vocab_eos(vocab_tgt) != llama_vocab_eos(vocab_dft)) {
101101
LOG_ERR("%s: draft vocab special tokens must match target vocab to use speculation\n", __func__);
102-
LOG_ERR("%s: tgt: bos = %d (%d), eos = %d (%d)\n", __func__, llama_vocab_bos(vocab_tgt), llama_vocab_add_bos(vocab_tgt), llama_vocab_eos(vocab_tgt), llama_vocab_add_eos(vocab_tgt));
103-
LOG_ERR("%s: dft: bos = %d (%d), eos = %d (%d)\n", __func__, llama_vocab_bos(vocab_dft), llama_vocab_add_bos(vocab_dft), llama_vocab_eos(vocab_dft), llama_vocab_add_eos(vocab_dft));
102+
LOG_ERR("%s: tgt: bos = %d (%d), eos = %d (%d)\n", __func__, llama_vocab_bos(vocab_tgt), llama_vocab_get_add_bos(vocab_tgt), llama_vocab_eos(vocab_tgt), llama_vocab_get_add_eos(vocab_tgt));
103+
LOG_ERR("%s: dft: bos = %d (%d), eos = %d (%d)\n", __func__, llama_vocab_bos(vocab_dft), llama_vocab_get_add_bos(vocab_dft), llama_vocab_eos(vocab_dft), llama_vocab_get_add_eos(vocab_dft));
104104
return false;
105105
}
106106

examples/cvector-generator/cvector-generator.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ struct tokenized_prompt {
275275
tokenized_prompt(llama_context * ctx, std::string pos, std::string neg) {
276276
const llama_model * model = llama_get_model(ctx);
277277
const llama_vocab * vocab = llama_model_get_vocab(model);
278-
const bool add_bos = llama_vocab_add_bos(vocab);
278+
const bool add_bos = llama_vocab_get_add_bos(vocab);
279279
tokens_pos = common_tokenize(ctx, pos, add_bos, true);
280280
tokens_neg = common_tokenize(ctx, neg, add_bos, true);
281281
max_seq_len = std::max(tokens_pos.size(), tokens_neg.size());

examples/eval-callback/eval-callback.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ static bool run(llama_context * ctx, const common_params & params) {
130130
const llama_model * model = llama_get_model(ctx);
131131
const llama_vocab * vocab = llama_model_get_vocab(model);
132132

133-
const bool add_bos = llama_vocab_add_bos(vocab);
133+
const bool add_bos = llama_vocab_get_add_bos(vocab);
134134

135135
std::vector<llama_token> tokens = common_tokenize(ctx, params.prompt, add_bos);
136136

examples/imatrix/imatrix.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,10 @@ static bool compute_imatrix(llama_context * ctx, const common_params & params) {
431431
const llama_model * model = llama_get_model(ctx);
432432
const llama_vocab * vocab = llama_model_get_vocab(model);
433433

434-
const bool add_bos = llama_vocab_add_bos(vocab);
434+
const bool add_bos = llama_vocab_get_add_bos(vocab);
435435
const int n_ctx = llama_n_ctx(ctx);
436436

437-
GGML_ASSERT(!llama_vocab_add_eos(vocab));
437+
GGML_ASSERT(!llama_vocab_get_add_eos(vocab));
438438

439439
auto tim1 = std::chrono::high_resolution_clock::now();
440440
LOG_INF("%s: tokenizing the input ..\n", __func__);

examples/infill/infill.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ int main(int argc, char ** argv) {
154154
LOG_INF("\n");
155155
LOG_INF("%s\n", common_params_get_system_info(params).c_str());
156156
}
157-
const bool add_bos = llama_vocab_add_bos(vocab);
158-
GGML_ASSERT(!llama_vocab_add_eos(vocab));
157+
const bool add_bos = llama_vocab_get_add_bos(vocab);
158+
GGML_ASSERT(!llama_vocab_get_add_eos(vocab));
159159

160160
std::vector<llama_token> embd_inp;
161161
std::vector<llama_token> embd_end;

examples/llama-bench/llama-bench.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,7 @@ static void test_prompt(llama_context * ctx, int n_prompt, int n_batch, int n_th
14101410

14111411
while (n_processed < n_prompt) {
14121412
int n_tokens = std::min(n_prompt - n_processed, n_batch);
1413-
tokens[0] = n_processed == 0 && llama_vocab_add_bos(vocab) ? llama_vocab_bos(vocab) : std::rand() % n_vocab;
1413+
tokens[0] = n_processed == 0 && llama_vocab_get_add_bos(vocab) ? llama_vocab_bos(vocab) : std::rand() % n_vocab;
14141414
for (int i = 1; i < n_tokens; i++) {
14151415
tokens[i] = std::rand() % n_vocab;
14161416
}
@@ -1428,7 +1428,7 @@ static void test_gen(llama_context * ctx, int n_gen, int n_threads) {
14281428
const llama_vocab * vocab = llama_model_get_vocab(model);
14291429
const int32_t n_vocab = llama_vocab_n_vocab(vocab);
14301430

1431-
llama_token token = llama_vocab_add_bos(vocab) ? llama_vocab_bos(vocab) : std::rand() % n_vocab;
1431+
llama_token token = llama_vocab_get_add_bos(vocab) ? llama_vocab_bos(vocab) : std::rand() % n_vocab;
14321432

14331433
for (int i = 0; i < n_gen; i++) {
14341434
llama_decode(ctx, llama_batch_get_one(&token, 1));

examples/main/main.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ int main(int argc, char ** argv) {
242242
}
243243
}
244244

245-
const bool add_bos = llama_vocab_add_bos(vocab);
245+
const bool add_bos = llama_vocab_get_add_bos(vocab);
246246
if (!llama_model_has_encoder(model)) {
247-
GGML_ASSERT(!llama_vocab_add_eos(vocab));
247+
GGML_ASSERT(!llama_vocab_get_add_eos(vocab));
248248
}
249249

250250
LOG_DBG("n_ctx: %d, add_bos: %d\n", n_ctx, add_bos);

examples/perplexity/perplexity.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ static results_perplexity perplexity_v2(llama_context * ctx, const common_params
299299
const llama_model * model = llama_get_model(ctx);
300300
const llama_vocab * vocab = llama_model_get_vocab(model);
301301

302-
const bool add_bos = llama_vocab_add_bos(vocab);
303-
GGML_ASSERT(!llama_vocab_add_eos(vocab));
302+
const bool add_bos = llama_vocab_get_add_bos(vocab);
303+
GGML_ASSERT(!llama_vocab_get_add_eos(vocab));
304304

305305
LOG_INF("%s: tokenizing the input ..\n", __func__);
306306

@@ -450,8 +450,8 @@ static results_perplexity perplexity(llama_context * ctx, const common_params &
450450
const llama_model * model = llama_get_model(ctx);
451451
const llama_vocab * vocab = llama_model_get_vocab(model);
452452

453-
const bool add_bos = llama_vocab_add_bos(vocab);
454-
GGML_ASSERT(!llama_vocab_add_eos(vocab));
453+
const bool add_bos = llama_vocab_get_add_bos(vocab);
454+
GGML_ASSERT(!llama_vocab_get_add_eos(vocab));
455455

456456
std::ofstream logits_stream;
457457
if (!params.logits_file.empty()) {
@@ -1717,8 +1717,8 @@ static void kl_divergence(llama_context * ctx, const common_params & params) {
17171717
const int n_batch = params.n_batch;
17181718
const int num_batches = (n_ctx + n_batch - 1)/n_batch;
17191719
const int nv = 2*((n_vocab + 1)/2) + 4;
1720-
const bool add_bos = llama_vocab_add_bos(vocab);
1721-
GGML_ASSERT(!llama_vocab_add_eos(vocab));
1720+
const bool add_bos = llama_vocab_get_add_bos(vocab);
1721+
GGML_ASSERT(!llama_vocab_get_add_eos(vocab));
17221722

17231723
std::vector<uint16_t> log_probs_uint16(size_t(n_ctx - 1 - n_ctx/2) * nv);
17241724
std::vector<float> kld_values(size_t(n_ctx - 1 - n_ctx/2)*n_chunk);

examples/server/server.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1698,7 +1698,7 @@ struct server_context {
16981698

16991699
n_ctx = llama_n_ctx(ctx);
17001700

1701-
add_bos_token = llama_vocab_add_bos(vocab);
1701+
add_bos_token = llama_vocab_get_add_bos(vocab);
17021702
has_eos_token = llama_vocab_eos(vocab) != LLAMA_TOKEN_NULL;
17031703

17041704
if (!params_base.speculative.model.empty()) {

examples/server/utils.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ static llama_tokens format_infill(
333333
auto embd_inp = spm_infill ? tokens_suffix : tokens_prefix;
334334
auto embd_end = spm_infill ? tokens_prefix : tokens_suffix;
335335

336-
if (llama_vocab_add_bos(vocab)) {
336+
if (llama_vocab_get_add_bos(vocab)) {
337337
embd_inp.insert(embd_inp.begin(), llama_vocab_bos(vocab));
338338
}
339339

examples/speculative/speculative.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ int main(int argc, char ** argv) {
106106
}
107107

108108
if (
109-
llama_vocab_add_bos(vocab_tgt) != llama_vocab_add_bos(vocab_dft) ||
110-
llama_vocab_add_eos(vocab_tgt) != llama_vocab_add_eos(vocab_dft) ||
109+
llama_vocab_get_add_bos(vocab_tgt) != llama_vocab_get_add_bos(vocab_dft) ||
110+
llama_vocab_get_add_eos(vocab_tgt) != llama_vocab_get_add_eos(vocab_dft) ||
111111
llama_vocab_bos(vocab_tgt) != llama_vocab_bos(vocab_dft) ||
112112
llama_vocab_eos(vocab_tgt) != llama_vocab_eos(vocab_dft)
113113
) {

examples/tokenize/tokenize.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ int main(int raw_argc, char ** raw_argv) {
367367
prompt = stdin_buffer.str();
368368
}
369369

370-
const bool model_wants_add_bos = llama_vocab_add_bos(vocab);
370+
const bool model_wants_add_bos = llama_vocab_get_add_bos(vocab);
371371
const bool add_bos = model_wants_add_bos && !no_bos;
372372
const bool parse_special = !no_parse_special;
373373
const bool escape = !no_escape;

include/llama.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -942,8 +942,8 @@ extern "C" {
942942
LLAMA_API llama_token llama_vocab_nl (const struct llama_vocab * vocab); // next-line
943943
LLAMA_API llama_token llama_vocab_pad(const struct llama_vocab * vocab); // padding
944944

945-
LLAMA_API bool llama_vocab_add_bos(const struct llama_vocab * vocab);
946-
LLAMA_API bool llama_vocab_add_eos(const struct llama_vocab * vocab);
945+
LLAMA_API bool llama_vocab_get_add_bos(const struct llama_vocab * vocab);
946+
LLAMA_API bool llama_vocab_get_add_eos(const struct llama_vocab * vocab);
947947

948948
LLAMA_API llama_token llama_vocab_fim_pre(const struct llama_vocab * vocab);
949949
LLAMA_API llama_token llama_vocab_fim_suf(const struct llama_vocab * vocab);
@@ -964,8 +964,8 @@ extern "C" {
964964
DEPRECATED(LLAMA_API llama_token llama_token_sep(const struct llama_vocab * vocab), "use llama_vocab_sep instead");
965965
DEPRECATED(LLAMA_API llama_token llama_token_nl (const struct llama_vocab * vocab), "use llama_vocab_nl instead");
966966
DEPRECATED(LLAMA_API llama_token llama_token_pad(const struct llama_vocab * vocab), "use llama_vocab_pad instead");
967-
DEPRECATED(LLAMA_API bool llama_add_bos_token(const struct llama_vocab * vocab), "use llama_vocab_add_bos instead");
968-
DEPRECATED(LLAMA_API bool llama_add_eos_token(const struct llama_vocab * vocab), "use llama_vocab_add_eos instead");
967+
DEPRECATED(LLAMA_API bool llama_add_bos_token(const struct llama_vocab * vocab), "use llama_vocab_get_add_bos instead");
968+
DEPRECATED(LLAMA_API bool llama_add_eos_token(const struct llama_vocab * vocab), "use llama_vocab_get_add_eos instead");
969969
DEPRECATED(LLAMA_API llama_token llama_token_fim_pre(const struct llama_vocab * vocab), "use llama_vocab_fim_pre instead");
970970
DEPRECATED(LLAMA_API llama_token llama_token_fim_suf(const struct llama_vocab * vocab), "use llama_vocab_fim_suf instead");
971971
DEPRECATED(LLAMA_API llama_token llama_token_fim_mid(const struct llama_vocab * vocab), "use llama_vocab_fim_mid instead");

src/llama-vocab.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <map>
1616
#include <queue>
1717
#include <set>
18-
#include <sstream>
1918
#include <unordered_map>
2019

2120
//
@@ -416,7 +415,7 @@ struct llm_tokenizer_bpe_session {
416415
}
417416

418417
bool append_bos(std::vector<llama_token> & output) const {
419-
if (vocab.add_bos_token()) {
418+
if (vocab.add_bos()) {
420419
GGML_ASSERT(vocab.token_bos() != LLAMA_TOKEN_NULL);
421420
output.push_back(vocab.token_bos());
422421
return true;
@@ -425,7 +424,7 @@ struct llm_tokenizer_bpe_session {
425424
}
426425

427426
bool append_eos(std::vector<llama_token> & output) const {
428-
if (vocab.add_eos_token()) {
427+
if (vocab.add_eos()) {
429428
GGML_ASSERT(vocab.token_eos() != LLAMA_TOKEN_NULL);
430429
output.push_back(vocab.token_eos());
431430
return true;
@@ -434,13 +433,13 @@ struct llm_tokenizer_bpe_session {
434433
}
435434

436435
void check_double_bos_eos(const std::vector<llama_token> & output) const {
437-
if (vocab.add_bos_token() && output.size() >= 2 && output[1] == vocab.token_bos()) {
436+
if (vocab.add_bos() && output.size() >= 2 && output[1] == vocab.token_bos()) {
438437
LLAMA_LOG_WARN(
439438
"%s: Added a BOS token to the prompt as specified by the model but the prompt "
440439
"also starts with a BOS token. So now the final prompt starts with 2 BOS tokens. "
441440
"Are you sure this is what you want?\n", __FUNCTION__);
442441
}
443-
if (vocab.add_bos_token() && output.size() >= 2 && *(output.end()-2) == vocab.token_eos()) {
442+
if (vocab.add_bos() && output.size() >= 2 && *(output.end()-2) == vocab.token_eos()) {
444443
LLAMA_LOG_WARN(
445444
"%s: Added a EOS token to the prompt as specified by the model but the prompt "
446445
"also ends with a EOS token. So now the final prompt ends with 2 EOS tokens. "
@@ -1234,6 +1233,7 @@ struct llama_vocab::impl {
12341233
llama_token special_fim_sep_id = LLAMA_TOKEN_NULL; // file separator
12351234

12361235
// tokenizer flags
1236+
// TODO: drop tokenizer_ prefix
12371237
bool tokenizer_add_space_prefix = false;
12381238
bool tokenizer_add_bos = false;
12391239
bool tokenizer_add_eos = false;
@@ -1745,7 +1745,7 @@ void llama_vocab::impl::load(llama_model_loader & ml, const LLM_KV & kv) {
17451745
}
17461746
}
17471747

1748-
// Handle add_bos_token and add_eos_token
1748+
// Handle add_bos and add_eos
17491749
{
17501750
bool temp = true;
17511751

@@ -2911,11 +2911,11 @@ bool llama_vocab::add_space_prefix() const {
29112911
return pimpl->tokenizer_add_space_prefix;
29122912
}
29132913

2914-
bool llama_vocab::add_bos_token() const {
2914+
bool llama_vocab::add_bos() const {
29152915
return pimpl->tokenizer_add_bos;
29162916
}
29172917

2918-
bool llama_vocab::add_eos_token() const {
2918+
bool llama_vocab::add_eos() const {
29192919
return pimpl->tokenizer_add_eos;
29202920
}
29212921

@@ -3087,12 +3087,12 @@ llama_token llama_vocab_pad(const struct llama_vocab * vocab) {
30873087
return vocab->token_pad();
30883088
}
30893089

3090-
bool llama_vocab_add_bos(const struct llama_vocab * vocab) {
3091-
return vocab->add_bos_token();
3090+
bool llama_vocab_get_add_bos(const struct llama_vocab * vocab) {
3091+
return vocab->add_bos();
30923092
}
30933093

3094-
bool llama_vocab_add_eos(const struct llama_vocab * vocab) {
3095-
return vocab->add_eos_token();
3094+
bool llama_vocab_get_add_eos(const struct llama_vocab * vocab) {
3095+
return vocab->add_eos();
30963096
}
30973097

30983098
llama_token llama_vocab_fim_pre(const struct llama_vocab * vocab) {
@@ -3181,12 +3181,12 @@ llama_token llama_token_pad(const struct llama_vocab * vocab) {
31813181

31823182
// deprecated
31833183
bool llama_add_bos_token(const struct llama_vocab * vocab) {
3184-
return llama_vocab_add_bos(vocab);
3184+
return llama_vocab_get_add_bos(vocab);
31853185
}
31863186

31873187
// deprecated
31883188
bool llama_add_eos_token(const struct llama_vocab * vocab) {
3189-
return llama_vocab_add_eos(vocab);
3189+
return llama_vocab_get_add_eos(vocab);
31903190
}
31913191

31923192
// deprecated

src/llama-vocab.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ struct llama_vocab {
6969
llama_token token_fim_rep() const;
7070
llama_token token_fim_sep() const;
7171

72+
// TODO: add get_ prefix
7273
bool add_space_prefix () const;
73-
bool add_bos_token () const;
74-
bool add_eos_token () const;
74+
bool add_bos () const;
75+
bool add_eos () const;
7576
bool ignore_merges () const;
7677
bool clean_spaces () const;
7778
bool remove_extra_whitespaces () const;

0 commit comments

Comments
 (0)