vocab : llama_vocab_add_[be]os -> llama_vocab_get_add_[be]os (#11174)

ggml-ci
This commit is contained in:
Georgi Gerganov 2025-01-11 17:43:46 +02:00
parent 6df37bc28b
commit 6540935bca
No known key found for this signature in database
GPG key ID: 449E073F9DC10735
15 changed files with 123 additions and 124 deletions

View file

@ -94,13 +94,13 @@ bool common_speculative_are_compatible(
return false; return false;
} }
if (llama_vocab_add_bos(vocab_tgt) != llama_vocab_add_bos(vocab_dft) || if (llama_vocab_get_add_bos(vocab_tgt) != llama_vocab_get_add_bos(vocab_dft) ||
llama_vocab_add_eos(vocab_tgt) != llama_vocab_add_eos(vocab_dft) || llama_vocab_get_add_eos(vocab_tgt) != llama_vocab_get_add_eos(vocab_dft) ||
llama_vocab_bos(vocab_tgt) != llama_vocab_bos(vocab_dft) || llama_vocab_bos(vocab_tgt) != llama_vocab_bos(vocab_dft) ||
llama_vocab_eos(vocab_tgt) != llama_vocab_eos(vocab_dft)) { llama_vocab_eos(vocab_tgt) != llama_vocab_eos(vocab_dft)) {
LOG_ERR("%s: draft vocab special tokens must match target vocab to use speculation\n", __func__); LOG_ERR("%s: draft vocab special tokens must match target vocab to use speculation\n", __func__);
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)); 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));
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)); 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));
return false; return false;
} }

View file

@ -275,7 +275,7 @@ struct tokenized_prompt {
tokenized_prompt(llama_context * ctx, std::string pos, std::string neg) { tokenized_prompt(llama_context * ctx, std::string pos, std::string neg) {
const llama_model * model = llama_get_model(ctx); const llama_model * model = llama_get_model(ctx);
const llama_vocab * vocab = llama_model_get_vocab(model); const llama_vocab * vocab = llama_model_get_vocab(model);
const bool add_bos = llama_vocab_add_bos(vocab); const bool add_bos = llama_vocab_get_add_bos(vocab);
tokens_pos = common_tokenize(ctx, pos, add_bos, true); tokens_pos = common_tokenize(ctx, pos, add_bos, true);
tokens_neg = common_tokenize(ctx, neg, add_bos, true); tokens_neg = common_tokenize(ctx, neg, add_bos, true);
max_seq_len = std::max(tokens_pos.size(), tokens_neg.size()); max_seq_len = std::max(tokens_pos.size(), tokens_neg.size());

View file

@ -130,7 +130,7 @@ static bool run(llama_context * ctx, const common_params & params) {
const llama_model * model = llama_get_model(ctx); const llama_model * model = llama_get_model(ctx);
const llama_vocab * vocab = llama_model_get_vocab(model); const llama_vocab * vocab = llama_model_get_vocab(model);
const bool add_bos = llama_vocab_add_bos(vocab); const bool add_bos = llama_vocab_get_add_bos(vocab);
std::vector<llama_token> tokens = common_tokenize(ctx, params.prompt, add_bos); std::vector<llama_token> tokens = common_tokenize(ctx, params.prompt, add_bos);

View file

@ -431,10 +431,10 @@ static bool compute_imatrix(llama_context * ctx, const common_params & params) {
const llama_model * model = llama_get_model(ctx); const llama_model * model = llama_get_model(ctx);
const llama_vocab * vocab = llama_model_get_vocab(model); const llama_vocab * vocab = llama_model_get_vocab(model);
const bool add_bos = llama_vocab_add_bos(vocab); const bool add_bos = llama_vocab_get_add_bos(vocab);
const int n_ctx = llama_n_ctx(ctx); const int n_ctx = llama_n_ctx(ctx);
GGML_ASSERT(!llama_vocab_add_eos(vocab)); GGML_ASSERT(!llama_vocab_get_add_eos(vocab));
auto tim1 = std::chrono::high_resolution_clock::now(); auto tim1 = std::chrono::high_resolution_clock::now();
LOG_INF("%s: tokenizing the input ..\n", __func__); LOG_INF("%s: tokenizing the input ..\n", __func__);

View file

@ -154,8 +154,8 @@ int main(int argc, char ** argv) {
LOG_INF("\n"); LOG_INF("\n");
LOG_INF("%s\n", common_params_get_system_info(params).c_str()); LOG_INF("%s\n", common_params_get_system_info(params).c_str());
} }
const bool add_bos = llama_vocab_add_bos(vocab); const bool add_bos = llama_vocab_get_add_bos(vocab);
GGML_ASSERT(!llama_vocab_add_eos(vocab)); GGML_ASSERT(!llama_vocab_get_add_eos(vocab));
std::vector<llama_token> embd_inp; std::vector<llama_token> embd_inp;
std::vector<llama_token> embd_end; std::vector<llama_token> embd_end;

View file

@ -1410,7 +1410,7 @@ static void test_prompt(llama_context * ctx, int n_prompt, int n_batch, int n_th
while (n_processed < n_prompt) { while (n_processed < n_prompt) {
int n_tokens = std::min(n_prompt - n_processed, n_batch); int n_tokens = std::min(n_prompt - n_processed, n_batch);
tokens[0] = n_processed == 0 && llama_vocab_add_bos(vocab) ? llama_vocab_bos(vocab) : std::rand() % n_vocab; tokens[0] = n_processed == 0 && llama_vocab_get_add_bos(vocab) ? llama_vocab_bos(vocab) : std::rand() % n_vocab;
for (int i = 1; i < n_tokens; i++) { for (int i = 1; i < n_tokens; i++) {
tokens[i] = std::rand() % n_vocab; tokens[i] = std::rand() % n_vocab;
} }
@ -1428,7 +1428,7 @@ static void test_gen(llama_context * ctx, int n_gen, int n_threads) {
const llama_vocab * vocab = llama_model_get_vocab(model); const llama_vocab * vocab = llama_model_get_vocab(model);
const int32_t n_vocab = llama_vocab_n_vocab(vocab); const int32_t n_vocab = llama_vocab_n_vocab(vocab);
llama_token token = llama_vocab_add_bos(vocab) ? llama_vocab_bos(vocab) : std::rand() % n_vocab; llama_token token = llama_vocab_get_add_bos(vocab) ? llama_vocab_bos(vocab) : std::rand() % n_vocab;
for (int i = 0; i < n_gen; i++) { for (int i = 0; i < n_gen; i++) {
llama_decode(ctx, llama_batch_get_one(&token, 1)); llama_decode(ctx, llama_batch_get_one(&token, 1));

View file

@ -242,9 +242,9 @@ int main(int argc, char ** argv) {
} }
} }
const bool add_bos = llama_vocab_add_bos(vocab); const bool add_bos = llama_vocab_get_add_bos(vocab);
if (!llama_model_has_encoder(model)) { if (!llama_model_has_encoder(model)) {
GGML_ASSERT(!llama_vocab_add_eos(vocab)); GGML_ASSERT(!llama_vocab_get_add_eos(vocab));
} }
LOG_DBG("n_ctx: %d, add_bos: %d\n", n_ctx, add_bos); LOG_DBG("n_ctx: %d, add_bos: %d\n", n_ctx, add_bos);

View file

@ -299,8 +299,8 @@ static results_perplexity perplexity_v2(llama_context * ctx, const common_params
const llama_model * model = llama_get_model(ctx); const llama_model * model = llama_get_model(ctx);
const llama_vocab * vocab = llama_model_get_vocab(model); const llama_vocab * vocab = llama_model_get_vocab(model);
const bool add_bos = llama_vocab_add_bos(vocab); const bool add_bos = llama_vocab_get_add_bos(vocab);
GGML_ASSERT(!llama_vocab_add_eos(vocab)); GGML_ASSERT(!llama_vocab_get_add_eos(vocab));
LOG_INF("%s: tokenizing the input ..\n", __func__); LOG_INF("%s: tokenizing the input ..\n", __func__);
@ -450,8 +450,8 @@ static results_perplexity perplexity(llama_context * ctx, const common_params &
const llama_model * model = llama_get_model(ctx); const llama_model * model = llama_get_model(ctx);
const llama_vocab * vocab = llama_model_get_vocab(model); const llama_vocab * vocab = llama_model_get_vocab(model);
const bool add_bos = llama_vocab_add_bos(vocab); const bool add_bos = llama_vocab_get_add_bos(vocab);
GGML_ASSERT(!llama_vocab_add_eos(vocab)); GGML_ASSERT(!llama_vocab_get_add_eos(vocab));
std::ofstream logits_stream; std::ofstream logits_stream;
if (!params.logits_file.empty()) { if (!params.logits_file.empty()) {
@ -1717,8 +1717,8 @@ static void kl_divergence(llama_context * ctx, const common_params & params) {
const int n_batch = params.n_batch; const int n_batch = params.n_batch;
const int num_batches = (n_ctx + n_batch - 1)/n_batch; const int num_batches = (n_ctx + n_batch - 1)/n_batch;
const int nv = 2*((n_vocab + 1)/2) + 4; const int nv = 2*((n_vocab + 1)/2) + 4;
const bool add_bos = llama_vocab_add_bos(vocab); const bool add_bos = llama_vocab_get_add_bos(vocab);
GGML_ASSERT(!llama_vocab_add_eos(vocab)); GGML_ASSERT(!llama_vocab_get_add_eos(vocab));
std::vector<uint16_t> log_probs_uint16(size_t(n_ctx - 1 - n_ctx/2) * nv); std::vector<uint16_t> log_probs_uint16(size_t(n_ctx - 1 - n_ctx/2) * nv);
std::vector<float> kld_values(size_t(n_ctx - 1 - n_ctx/2)*n_chunk); std::vector<float> kld_values(size_t(n_ctx - 1 - n_ctx/2)*n_chunk);

View file

@ -1698,7 +1698,7 @@ struct server_context {
n_ctx = llama_n_ctx(ctx); n_ctx = llama_n_ctx(ctx);
add_bos_token = llama_vocab_add_bos(vocab); add_bos_token = llama_vocab_get_add_bos(vocab);
has_eos_token = llama_vocab_eos(vocab) != LLAMA_TOKEN_NULL; has_eos_token = llama_vocab_eos(vocab) != LLAMA_TOKEN_NULL;
if (!params_base.speculative.model.empty()) { if (!params_base.speculative.model.empty()) {

View file

@ -333,7 +333,7 @@ static llama_tokens format_infill(
auto embd_inp = spm_infill ? tokens_suffix : tokens_prefix; auto embd_inp = spm_infill ? tokens_suffix : tokens_prefix;
auto embd_end = spm_infill ? tokens_prefix : tokens_suffix; auto embd_end = spm_infill ? tokens_prefix : tokens_suffix;
if (llama_vocab_add_bos(vocab)) { if (llama_vocab_get_add_bos(vocab)) {
embd_inp.insert(embd_inp.begin(), llama_vocab_bos(vocab)); embd_inp.insert(embd_inp.begin(), llama_vocab_bos(vocab));
} }

View file

@ -106,8 +106,8 @@ int main(int argc, char ** argv) {
} }
if ( if (
llama_vocab_add_bos(vocab_tgt) != llama_vocab_add_bos(vocab_dft) || llama_vocab_get_add_bos(vocab_tgt) != llama_vocab_get_add_bos(vocab_dft) ||
llama_vocab_add_eos(vocab_tgt) != llama_vocab_add_eos(vocab_dft) || llama_vocab_get_add_eos(vocab_tgt) != llama_vocab_get_add_eos(vocab_dft) ||
llama_vocab_bos(vocab_tgt) != llama_vocab_bos(vocab_dft) || llama_vocab_bos(vocab_tgt) != llama_vocab_bos(vocab_dft) ||
llama_vocab_eos(vocab_tgt) != llama_vocab_eos(vocab_dft) llama_vocab_eos(vocab_tgt) != llama_vocab_eos(vocab_dft)
) { ) {

View file

@ -367,7 +367,7 @@ int main(int raw_argc, char ** raw_argv) {
prompt = stdin_buffer.str(); prompt = stdin_buffer.str();
} }
const bool model_wants_add_bos = llama_vocab_add_bos(vocab); const bool model_wants_add_bos = llama_vocab_get_add_bos(vocab);
const bool add_bos = model_wants_add_bos && !no_bos; const bool add_bos = model_wants_add_bos && !no_bos;
const bool parse_special = !no_parse_special; const bool parse_special = !no_parse_special;
const bool escape = !no_escape; const bool escape = !no_escape;

View file

@ -942,8 +942,8 @@ extern "C" {
LLAMA_API llama_token llama_vocab_nl (const struct llama_vocab * vocab); // next-line LLAMA_API llama_token llama_vocab_nl (const struct llama_vocab * vocab); // next-line
LLAMA_API llama_token llama_vocab_pad(const struct llama_vocab * vocab); // padding LLAMA_API llama_token llama_vocab_pad(const struct llama_vocab * vocab); // padding
LLAMA_API bool llama_vocab_add_bos(const struct llama_vocab * vocab); LLAMA_API bool llama_vocab_get_add_bos(const struct llama_vocab * vocab);
LLAMA_API bool llama_vocab_add_eos(const struct llama_vocab * vocab); LLAMA_API bool llama_vocab_get_add_eos(const struct llama_vocab * vocab);
LLAMA_API llama_token llama_vocab_fim_pre(const struct llama_vocab * vocab); LLAMA_API llama_token llama_vocab_fim_pre(const struct llama_vocab * vocab);
LLAMA_API llama_token llama_vocab_fim_suf(const struct llama_vocab * vocab); LLAMA_API llama_token llama_vocab_fim_suf(const struct llama_vocab * vocab);
@ -964,8 +964,8 @@ extern "C" {
DEPRECATED(LLAMA_API llama_token llama_token_sep(const struct llama_vocab * vocab), "use llama_vocab_sep instead"); DEPRECATED(LLAMA_API llama_token llama_token_sep(const struct llama_vocab * vocab), "use llama_vocab_sep instead");
DEPRECATED(LLAMA_API llama_token llama_token_nl (const struct llama_vocab * vocab), "use llama_vocab_nl instead"); DEPRECATED(LLAMA_API llama_token llama_token_nl (const struct llama_vocab * vocab), "use llama_vocab_nl instead");
DEPRECATED(LLAMA_API llama_token llama_token_pad(const struct llama_vocab * vocab), "use llama_vocab_pad instead"); DEPRECATED(LLAMA_API llama_token llama_token_pad(const struct llama_vocab * vocab), "use llama_vocab_pad instead");
DEPRECATED(LLAMA_API bool llama_add_bos_token(const struct llama_vocab * vocab), "use llama_vocab_add_bos instead"); DEPRECATED(LLAMA_API bool llama_add_bos_token(const struct llama_vocab * vocab), "use llama_vocab_get_add_bos instead");
DEPRECATED(LLAMA_API bool llama_add_eos_token(const struct llama_vocab * vocab), "use llama_vocab_add_eos instead"); DEPRECATED(LLAMA_API bool llama_add_eos_token(const struct llama_vocab * vocab), "use llama_vocab_get_add_eos instead");
DEPRECATED(LLAMA_API llama_token llama_token_fim_pre(const struct llama_vocab * vocab), "use llama_vocab_fim_pre instead"); DEPRECATED(LLAMA_API llama_token llama_token_fim_pre(const struct llama_vocab * vocab), "use llama_vocab_fim_pre instead");
DEPRECATED(LLAMA_API llama_token llama_token_fim_suf(const struct llama_vocab * vocab), "use llama_vocab_fim_suf instead"); DEPRECATED(LLAMA_API llama_token llama_token_fim_suf(const struct llama_vocab * vocab), "use llama_vocab_fim_suf instead");
DEPRECATED(LLAMA_API llama_token llama_token_fim_mid(const struct llama_vocab * vocab), "use llama_vocab_fim_mid instead"); DEPRECATED(LLAMA_API llama_token llama_token_fim_mid(const struct llama_vocab * vocab), "use llama_vocab_fim_mid instead");

View file

@ -15,7 +15,6 @@
#include <map> #include <map>
#include <queue> #include <queue>
#include <set> #include <set>
#include <sstream>
#include <unordered_map> #include <unordered_map>
// //
@ -416,7 +415,7 @@ struct llm_tokenizer_bpe_session {
} }
bool append_bos(std::vector<llama_token> & output) const { bool append_bos(std::vector<llama_token> & output) const {
if (vocab.add_bos_token()) { if (vocab.get_add_bos()) {
GGML_ASSERT(vocab.token_bos() != LLAMA_TOKEN_NULL); GGML_ASSERT(vocab.token_bos() != LLAMA_TOKEN_NULL);
output.push_back(vocab.token_bos()); output.push_back(vocab.token_bos());
return true; return true;
@ -425,7 +424,7 @@ struct llm_tokenizer_bpe_session {
} }
bool append_eos(std::vector<llama_token> & output) const { bool append_eos(std::vector<llama_token> & output) const {
if (vocab.add_eos_token()) { if (vocab.get_add_eos()) {
GGML_ASSERT(vocab.token_eos() != LLAMA_TOKEN_NULL); GGML_ASSERT(vocab.token_eos() != LLAMA_TOKEN_NULL);
output.push_back(vocab.token_eos()); output.push_back(vocab.token_eos());
return true; return true;
@ -434,13 +433,13 @@ struct llm_tokenizer_bpe_session {
} }
void check_double_bos_eos(const std::vector<llama_token> & output) const { void check_double_bos_eos(const std::vector<llama_token> & output) const {
if (vocab.add_bos_token() && output.size() >= 2 && output[1] == vocab.token_bos()) { if (vocab.get_add_bos() && output.size() >= 2 && output[1] == vocab.token_bos()) {
LLAMA_LOG_WARN( LLAMA_LOG_WARN(
"%s: Added a BOS token to the prompt as specified by the model but the prompt " "%s: Added a BOS token to the prompt as specified by the model but the prompt "
"also starts with a BOS token. So now the final prompt starts with 2 BOS tokens. " "also starts with a BOS token. So now the final prompt starts with 2 BOS tokens. "
"Are you sure this is what you want?\n", __FUNCTION__); "Are you sure this is what you want?\n", __FUNCTION__);
} }
if (vocab.add_bos_token() && output.size() >= 2 && *(output.end()-2) == vocab.token_eos()) { if (vocab.get_add_bos() && output.size() >= 2 && *(output.end()-2) == vocab.token_eos()) {
LLAMA_LOG_WARN( LLAMA_LOG_WARN(
"%s: Added a EOS token to the prompt as specified by the model but the prompt " "%s: Added a EOS token to the prompt as specified by the model but the prompt "
"also ends with a EOS token. So now the final prompt ends with 2 EOS tokens. " "also ends with a EOS token. So now the final prompt ends with 2 EOS tokens. "
@ -462,7 +461,7 @@ struct llm_tokenizer_bpe_session {
size_t offset = 0; size_t offset = 0;
//if (vocab.tokenizer_ignore_merges && vocab.token_to_id.find(word) != vocab.token_to_id.end()) { //if (vocab.tokenizer_ignore_merges && vocab.token_to_id.find(word) != vocab.token_to_id.end()) {
if (vocab.ignore_merges() && vocab.text_to_token(word) != LLAMA_TOKEN_NULL) { if (vocab.get_ignore_merges() && vocab.text_to_token(word) != LLAMA_TOKEN_NULL) {
symbols.emplace_back(llm_symbol{-1, -1, word.c_str(), word.size()}); symbols.emplace_back(llm_symbol{-1, -1, word.c_str(), word.size()});
offset = word.size(); offset = word.size();
} }
@ -890,11 +889,11 @@ private:
normalized->clear(); normalized->clear();
normalized->reserve(input.size() * 3); normalized->reserve(input.size() * 3);
const std::string space = vocab.escape_whitespaces() ? tokenizer.escaped_space : " "; const std::string space = vocab.get_escape_whitespaces() ? tokenizer.escaped_space : " ";
const bool shall_prepend_space = !vocab.treat_whitespace_as_suffix() && vocab.add_space_prefix(); const bool shall_prepend_space = !vocab.get_treat_whitespace_as_suffix() && vocab.get_add_space_prefix();
const bool shall_append_space = vocab.treat_whitespace_as_suffix() && vocab.add_space_prefix(); const bool shall_append_space = vocab.get_treat_whitespace_as_suffix() && vocab.get_add_space_prefix();
const bool shall_merge_spaces = vocab.remove_extra_whitespaces(); const bool shall_merge_spaces = vocab.get_remove_extra_whitespaces();
bool is_space_prepended = false; bool is_space_prepended = false;
bool processing_non_ws = false; bool processing_non_ws = false;
@ -1234,14 +1233,14 @@ struct llama_vocab::impl {
llama_token special_fim_sep_id = LLAMA_TOKEN_NULL; // file separator llama_token special_fim_sep_id = LLAMA_TOKEN_NULL; // file separator
// tokenizer flags // tokenizer flags
bool tokenizer_add_space_prefix = false; bool add_space_prefix = false;
bool tokenizer_add_bos = false; bool add_bos = false;
bool tokenizer_add_eos = false; bool add_eos = false;
bool tokenizer_ignore_merges = false; bool ignore_merges = false;
bool tokenizer_clean_spaces = false; // clean_up_tokenization_spaces bool clean_spaces = false; // clean_up_tokenization_spaces
bool tokenizer_remove_extra_whitespaces = false; bool remove_extra_whitespaces = false;
bool tokenizer_escape_whitespaces = true; bool escape_whitespaces = true;
bool tokenizer_treat_whitespace_as_suffix = false; bool treat_whitespace_as_suffix = false;
std::unordered_map<std::string, llama_token> token_to_id; std::unordered_map<std::string, llama_token> token_to_id;
std::vector<token_data> id_to_token; std::vector<token_data> id_to_token;
@ -1468,8 +1467,8 @@ void llama_vocab::impl::load(llama_model_loader & ml, const LLM_KV & kv) {
// for now, only BPE models have pre-tokenizers // for now, only BPE models have pre-tokenizers
if (type == LLAMA_VOCAB_TYPE_BPE) { if (type == LLAMA_VOCAB_TYPE_BPE) {
tokenizer_add_space_prefix = false; add_space_prefix = false;
tokenizer_clean_spaces = true; clean_spaces = true;
if (tokenizer_pre.empty()) { if (tokenizer_pre.empty()) {
LLAMA_LOG_WARN("%s: missing pre-tokenizer type, using: 'default'\n", __func__); LLAMA_LOG_WARN("%s: missing pre-tokenizer type, using: 'default'\n", __func__);
LLAMA_LOG_WARN("%s: \n", __func__); LLAMA_LOG_WARN("%s: \n", __func__);
@ -1487,20 +1486,20 @@ void llama_vocab::impl::load(llama_model_loader & ml, const LLM_KV & kv) {
tokenizer_pre == "llama-bpe"|| tokenizer_pre == "llama-bpe"||
tokenizer_pre == "falcon3") { tokenizer_pre == "falcon3") {
pre_type = LLAMA_VOCAB_PRE_TYPE_LLAMA3; pre_type = LLAMA_VOCAB_PRE_TYPE_LLAMA3;
tokenizer_ignore_merges = true; ignore_merges = true;
tokenizer_add_bos = true; add_bos = true;
} else if ( } else if (
tokenizer_pre == "deepseek-llm") { tokenizer_pre == "deepseek-llm") {
pre_type = LLAMA_VOCAB_PRE_TYPE_DEEPSEEK_LLM; pre_type = LLAMA_VOCAB_PRE_TYPE_DEEPSEEK_LLM;
tokenizer_clean_spaces = false; clean_spaces = false;
} else if ( } else if (
tokenizer_pre == "deepseek-coder") { tokenizer_pre == "deepseek-coder") {
pre_type = LLAMA_VOCAB_PRE_TYPE_DEEPSEEK_CODER; pre_type = LLAMA_VOCAB_PRE_TYPE_DEEPSEEK_CODER;
tokenizer_clean_spaces = false; clean_spaces = false;
} else if ( } else if (
tokenizer_pre == "deepseek-v3") { tokenizer_pre == "deepseek-v3") {
pre_type = LLAMA_VOCAB_PRE_TYPE_DEEPSEEK3_LLM; pre_type = LLAMA_VOCAB_PRE_TYPE_DEEPSEEK3_LLM;
tokenizer_clean_spaces = false; clean_spaces = false;
} else if ( } else if (
tokenizer_pre == "falcon") { tokenizer_pre == "falcon") {
pre_type = LLAMA_VOCAB_PRE_TYPE_FALCON; pre_type = LLAMA_VOCAB_PRE_TYPE_FALCON;
@ -1528,11 +1527,11 @@ void llama_vocab::impl::load(llama_model_loader & ml, const LLM_KV & kv) {
} else if ( } else if (
tokenizer_pre == "command-r") { tokenizer_pre == "command-r") {
pre_type = LLAMA_VOCAB_PRE_TYPE_COMMAND_R; pre_type = LLAMA_VOCAB_PRE_TYPE_COMMAND_R;
tokenizer_clean_spaces = false; clean_spaces = false;
} else if ( } else if (
tokenizer_pre == "qwen2") { tokenizer_pre == "qwen2") {
pre_type = LLAMA_VOCAB_PRE_TYPE_QWEN2; pre_type = LLAMA_VOCAB_PRE_TYPE_QWEN2;
tokenizer_clean_spaces = false; clean_spaces = false;
} else if ( } else if (
tokenizer_pre == "stablelm2") { tokenizer_pre == "stablelm2") {
pre_type = LLAMA_VOCAB_PRE_TYPE_STABLELM2; pre_type = LLAMA_VOCAB_PRE_TYPE_STABLELM2;
@ -1548,7 +1547,7 @@ void llama_vocab::impl::load(llama_model_loader & ml, const LLM_KV & kv) {
} else if ( } else if (
tokenizer_pre == "poro-chat") { tokenizer_pre == "poro-chat") {
pre_type = LLAMA_VOCAB_PRE_TYPE_PORO; pre_type = LLAMA_VOCAB_PRE_TYPE_PORO;
tokenizer_clean_spaces = false; clean_spaces = false;
} else if ( } else if (
tokenizer_pre == "chatglm-bpe") { tokenizer_pre == "chatglm-bpe") {
pre_type = LLAMA_VOCAB_PRE_TYPE_CHATGLM4; pre_type = LLAMA_VOCAB_PRE_TYPE_CHATGLM4;
@ -1556,20 +1555,20 @@ void llama_vocab::impl::load(llama_model_loader & ml, const LLM_KV & kv) {
} else if ( } else if (
tokenizer_pre == "viking") { tokenizer_pre == "viking") {
pre_type = LLAMA_VOCAB_PRE_TYPE_VIKING; pre_type = LLAMA_VOCAB_PRE_TYPE_VIKING;
tokenizer_clean_spaces = false; clean_spaces = false;
} else if ( } else if (
tokenizer_pre == "jais") { tokenizer_pre == "jais") {
pre_type = LLAMA_VOCAB_PRE_TYPE_JAIS; pre_type = LLAMA_VOCAB_PRE_TYPE_JAIS;
} else if ( } else if (
tokenizer_pre == "tekken") { tokenizer_pre == "tekken") {
pre_type = LLAMA_VOCAB_PRE_TYPE_TEKKEN; pre_type = LLAMA_VOCAB_PRE_TYPE_TEKKEN;
tokenizer_clean_spaces = false; clean_spaces = false;
tokenizer_ignore_merges = true; ignore_merges = true;
tokenizer_add_bos = true; add_bos = true;
} else if ( } else if (
tokenizer_pre == "smollm") { tokenizer_pre == "smollm") {
pre_type = LLAMA_VOCAB_PRE_TYPE_SMOLLM; pre_type = LLAMA_VOCAB_PRE_TYPE_SMOLLM;
tokenizer_clean_spaces = false; clean_spaces = false;
} else if ( } else if (
tokenizer_pre == "codeshell") { tokenizer_pre == "codeshell") {
pre_type = LLAMA_VOCAB_PRE_TYPE_CODESHELL; pre_type = LLAMA_VOCAB_PRE_TYPE_CODESHELL;
@ -1585,8 +1584,8 @@ void llama_vocab::impl::load(llama_model_loader & ml, const LLM_KV & kv) {
} else if ( } else if (
tokenizer_pre == "chameleon") { tokenizer_pre == "chameleon") {
pre_type = LLAMA_VOCAB_PRE_TYPE_CHAMELEON; pre_type = LLAMA_VOCAB_PRE_TYPE_CHAMELEON;
tokenizer_add_bos = true; add_bos = true;
tokenizer_clean_spaces = false; clean_spaces = false;
} else if ( } else if (
tokenizer_pre == "minerva-7b") { tokenizer_pre == "minerva-7b") {
pre_type = LLAMA_VOCAB_PRE_TYPE_MINERVA; pre_type = LLAMA_VOCAB_PRE_TYPE_MINERVA;
@ -1598,32 +1597,32 @@ void llama_vocab::impl::load(llama_model_loader & ml, const LLM_KV & kv) {
} }
} else if (type == LLAMA_VOCAB_TYPE_SPM) { } else if (type == LLAMA_VOCAB_TYPE_SPM) {
pre_type = LLAMA_VOCAB_PRE_TYPE_DEFAULT; pre_type = LLAMA_VOCAB_PRE_TYPE_DEFAULT;
tokenizer_add_space_prefix = true; add_space_prefix = true;
tokenizer_clean_spaces = false; clean_spaces = false;
tokenizer_add_bos = true; add_bos = true;
tokenizer_add_eos = false; add_eos = false;
} else if (type == LLAMA_VOCAB_TYPE_WPM) { } else if (type == LLAMA_VOCAB_TYPE_WPM) {
pre_type = LLAMA_VOCAB_PRE_TYPE_DEFAULT; pre_type = LLAMA_VOCAB_PRE_TYPE_DEFAULT;
tokenizer_add_space_prefix = false; add_space_prefix = false;
tokenizer_clean_spaces = true; clean_spaces = true;
tokenizer_add_bos = true; add_bos = true;
tokenizer_add_eos = false; add_eos = false;
} else if (type == LLAMA_VOCAB_TYPE_UGM) { } else if (type == LLAMA_VOCAB_TYPE_UGM) {
pre_type = LLAMA_VOCAB_PRE_TYPE_DEFAULT; pre_type = LLAMA_VOCAB_PRE_TYPE_DEFAULT;
tokenizer_add_bos = false; add_bos = false;
tokenizer_add_eos = true; add_eos = true;
} else if (type == LLAMA_VOCAB_TYPE_RWKV) { } else if (type == LLAMA_VOCAB_TYPE_RWKV) {
pre_type = LLAMA_VOCAB_PRE_TYPE_DEFAULT; pre_type = LLAMA_VOCAB_PRE_TYPE_DEFAULT;
tokenizer_add_space_prefix = false; add_space_prefix = false;
tokenizer_clean_spaces = false; clean_spaces = false;
tokenizer_add_bos = false; add_bos = false;
tokenizer_add_eos = false; add_eos = false;
} else { } else {
pre_type = LLAMA_VOCAB_PRE_TYPE_DEFAULT; pre_type = LLAMA_VOCAB_PRE_TYPE_DEFAULT;
} }
ml.get_key(LLM_KV_TOKENIZER_ADD_PREFIX, tokenizer_add_space_prefix, false); ml.get_key(LLM_KV_TOKENIZER_ADD_PREFIX, add_space_prefix, false);
ml.get_key(LLM_KV_TOKENIZER_REMOVE_EXTRA_WS, tokenizer_remove_extra_whitespaces, false); ml.get_key(LLM_KV_TOKENIZER_REMOVE_EXTRA_WS, remove_extra_whitespaces, false);
} }
const int token_idx = gguf_find_key(ctx, kv(LLM_KV_TOKENIZER_LIST).c_str()); const int token_idx = gguf_find_key(ctx, kv(LLM_KV_TOKENIZER_LIST).c_str());
@ -1745,15 +1744,15 @@ void llama_vocab::impl::load(llama_model_loader & ml, const LLM_KV & kv) {
} }
} }
// Handle add_bos_token and add_eos_token // Handle add_bos and add_eos
{ {
bool temp = true; bool temp = true;
if (ml.get_key(LLM_KV_TOKENIZER_ADD_BOS, temp, false)) { if (ml.get_key(LLM_KV_TOKENIZER_ADD_BOS, temp, false)) {
tokenizer_add_bos = temp; add_bos = temp;
} }
if (ml.get_key(LLM_KV_TOKENIZER_ADD_EOS, temp, false)) { if (ml.get_key(LLM_KV_TOKENIZER_ADD_EOS, temp, false)) {
tokenizer_add_eos = temp; add_eos = temp;
} }
} }
@ -2337,7 +2336,7 @@ std::vector<llama_token> llama_vocab::impl::tokenize(
bool is_prev_special = true; // prefix with space if first token bool is_prev_special = true; // prefix with space if first token
if (add_special && tokenizer_add_bos) { if (add_special && add_bos) {
GGML_ASSERT(special_bos_id != LLAMA_TOKEN_NULL); GGML_ASSERT(special_bos_id != LLAMA_TOKEN_NULL);
output.push_back(special_bos_id); output.push_back(special_bos_id);
is_prev_special = true; is_prev_special = true;
@ -2348,7 +2347,7 @@ std::vector<llama_token> llama_vocab::impl::tokenize(
std::string text; std::string text;
// prefix with space if previous is special // prefix with space if previous is special
if (tokenizer_add_space_prefix && is_prev_special) { if (add_space_prefix && is_prev_special) {
text = ' '; text = ' ';
} }
@ -2367,14 +2366,14 @@ std::vector<llama_token> llama_vocab::impl::tokenize(
} }
} }
if (add_special && tokenizer_add_bos && output.size() >= 2 && output[1] == special_bos_id) { if (add_special && add_bos && output.size() >= 2 && output[1] == special_bos_id) {
LLAMA_LOG_WARN( LLAMA_LOG_WARN(
"%s: Added a BOS token to the prompt as specified by the model but the prompt " "%s: Added a BOS token to the prompt as specified by the model but the prompt "
"also starts with a BOS token. So now the final prompt starts with 2 BOS tokens. " "also starts with a BOS token. So now the final prompt starts with 2 BOS tokens. "
"Are you sure this is what you want?\n", __FUNCTION__); "Are you sure this is what you want?\n", __FUNCTION__);
} }
if (add_special && tokenizer_add_eos) { if (add_special && add_eos) {
GGML_ASSERT(special_eos_id != LLAMA_TOKEN_NULL); GGML_ASSERT(special_eos_id != LLAMA_TOKEN_NULL);
output.push_back(special_eos_id); output.push_back(special_eos_id);
} }
@ -2434,7 +2433,7 @@ std::vector<llama_token> llama_vocab::impl::tokenize(
} break; } break;
case LLAMA_VOCAB_TYPE_UGM: case LLAMA_VOCAB_TYPE_UGM:
{ {
if (add_special && tokenizer_add_bos) { if (add_special && add_bos) {
GGML_ASSERT(special_bos_id != LLAMA_TOKEN_NULL); GGML_ASSERT(special_bos_id != LLAMA_TOKEN_NULL);
output.push_back(special_bos_id); output.push_back(special_bos_id);
} }
@ -2452,14 +2451,14 @@ std::vector<llama_token> llama_vocab::impl::tokenize(
} }
} }
if (add_special && tokenizer_add_bos && output.size() >= 2 && output[1] == special_bos_id) { if (add_special && add_bos && output.size() >= 2 && output[1] == special_bos_id) {
LLAMA_LOG_WARN( LLAMA_LOG_WARN(
"%s: Added a BOS token to the prompt as specified by the model but the prompt " "%s: Added a BOS token to the prompt as specified by the model but the prompt "
"also starts with a BOS token. So now the final prompt starts with 2 BOS tokens. " "also starts with a BOS token. So now the final prompt starts with 2 BOS tokens. "
"Are you sure this is what you want?\n", __FUNCTION__); "Are you sure this is what you want?\n", __FUNCTION__);
} }
if (add_special && tokenizer_add_eos) { if (add_special && add_eos) {
GGML_ASSERT(special_eos_id != LLAMA_TOKEN_NULL); GGML_ASSERT(special_eos_id != LLAMA_TOKEN_NULL);
output.push_back(special_eos_id); output.push_back(special_eos_id);
} }
@ -2594,9 +2593,9 @@ int32_t llama_vocab::impl::detokenize(
int32_t total = 0; int32_t total = 0;
// remove the leading space // remove the leading space
bool remove_space = tokenizer_add_space_prefix; bool remove_space = add_space_prefix;
if (remove_special && tokenizer_add_bos) { if (remove_special && add_bos) {
if (n_tokens > 0 && tokens[0] == special_bos_id) { if (n_tokens > 0 && tokens[0] == special_bos_id) {
remove_space = false; remove_space = false;
n_tokens--; n_tokens--;
@ -2604,7 +2603,7 @@ int32_t llama_vocab::impl::detokenize(
} }
} }
if (remove_special && tokenizer_add_eos) { if (remove_special && add_eos) {
if (n_tokens > 0 && tokens[n_tokens - 1] == special_eos_id) { if (n_tokens > 0 && tokens[n_tokens - 1] == special_eos_id) {
n_tokens--; n_tokens--;
} }
@ -2628,7 +2627,7 @@ int32_t llama_vocab::impl::detokenize(
return -total; return -total;
} }
if (tokenizer_clean_spaces) { if (clean_spaces) {
text -= total; // restart text text -= total; // restart text
// first pass: characters ?!., //TODO: where do these characters come from? // first pass: characters ?!., //TODO: where do these characters come from?
@ -2907,36 +2906,36 @@ llama_token llama_vocab::token_fim_sep() const {
return pimpl->special_fim_sep_id; return pimpl->special_fim_sep_id;
} }
bool llama_vocab::add_space_prefix() const { bool llama_vocab::get_add_space_prefix() const {
return pimpl->tokenizer_add_space_prefix; return pimpl->add_space_prefix;
} }
bool llama_vocab::add_bos_token() const { bool llama_vocab::get_add_bos() const {
return pimpl->tokenizer_add_bos; return pimpl->add_bos;
} }
bool llama_vocab::add_eos_token() const { bool llama_vocab::get_add_eos() const {
return pimpl->tokenizer_add_eos; return pimpl->add_eos;
} }
bool llama_vocab::ignore_merges() const { bool llama_vocab::get_ignore_merges() const {
return pimpl->tokenizer_ignore_merges; return pimpl->ignore_merges;
} }
bool llama_vocab::clean_spaces() const { bool llama_vocab::get_clean_spaces() const {
return pimpl->tokenizer_clean_spaces; return pimpl->clean_spaces;
} }
bool llama_vocab::remove_extra_whitespaces() const { bool llama_vocab::get_remove_extra_whitespaces() const {
return pimpl->tokenizer_remove_extra_whitespaces; return pimpl->remove_extra_whitespaces;
} }
bool llama_vocab::escape_whitespaces() const { bool llama_vocab::get_escape_whitespaces() const {
return pimpl->tokenizer_escape_whitespaces; return pimpl->escape_whitespaces;
} }
bool llama_vocab::treat_whitespace_as_suffix() const { bool llama_vocab::get_treat_whitespace_as_suffix() const {
return pimpl->tokenizer_treat_whitespace_as_suffix; return pimpl->treat_whitespace_as_suffix;
} }
int llama_vocab::max_token_len() const { int llama_vocab::max_token_len() const {
@ -3087,12 +3086,12 @@ llama_token llama_vocab_pad(const struct llama_vocab * vocab) {
return vocab->token_pad(); return vocab->token_pad();
} }
bool llama_vocab_add_bos(const struct llama_vocab * vocab) { bool llama_vocab_get_add_bos(const struct llama_vocab * vocab) {
return vocab->add_bos_token(); return vocab->get_add_bos();
} }
bool llama_vocab_add_eos(const struct llama_vocab * vocab) { bool llama_vocab_get_add_eos(const struct llama_vocab * vocab) {
return vocab->add_eos_token(); return vocab->get_add_eos();
} }
llama_token llama_vocab_fim_pre(const struct llama_vocab * vocab) { llama_token llama_vocab_fim_pre(const struct llama_vocab * vocab) {
@ -3181,12 +3180,12 @@ llama_token llama_token_pad(const struct llama_vocab * vocab) {
// deprecated // deprecated
bool llama_add_bos_token(const struct llama_vocab * vocab) { bool llama_add_bos_token(const struct llama_vocab * vocab) {
return llama_vocab_add_bos(vocab); return llama_vocab_get_add_bos(vocab);
} }
// deprecated // deprecated
bool llama_add_eos_token(const struct llama_vocab * vocab) { bool llama_add_eos_token(const struct llama_vocab * vocab) {
return llama_vocab_add_eos(vocab); return llama_vocab_get_add_eos(vocab);
} }
// deprecated // deprecated

View file

@ -69,14 +69,14 @@ struct llama_vocab {
llama_token token_fim_rep() const; llama_token token_fim_rep() const;
llama_token token_fim_sep() const; llama_token token_fim_sep() const;
bool add_space_prefix () const; bool get_add_space_prefix () const;
bool add_bos_token () const; bool get_add_bos () const;
bool add_eos_token () const; bool get_add_eos () const;
bool ignore_merges () const; bool get_ignore_merges () const;
bool clean_spaces () const; bool get_clean_spaces () const;
bool remove_extra_whitespaces () const; bool get_remove_extra_whitespaces () const;
bool escape_whitespaces () const; bool get_escape_whitespaces () const;
bool treat_whitespace_as_suffix() const; bool get_treat_whitespace_as_suffix() const;
int max_token_len() const; int max_token_len() const;