Skip to content

Commit 330bd07

Browse files
committed
llama : llama_n_vocab() now uses struct llama_vocab
1 parent 68db765 commit 330bd07

File tree

16 files changed

+55
-39
lines changed

16 files changed

+55
-39
lines changed

common/common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ struct common_init_result common_init_from_params(common_params & params) {
949949
}
950950

951951
if (params.sampling.ignore_eos) {
952-
for (llama_token i = 0; i < llama_n_vocab(model); i++) {
952+
for (llama_token i = 0; i < llama_n_vocab(vocab); i++) {
953953
if (llama_token_is_eog(vocab, i)) {
954954
LOG_INF("%s: added %s logit bias = %f\n", __func__, common_token_to_piece(lctx, i).c_str(), -INFINITY);
955955
params.sampling.logit_bias.push_back({i, -INFINITY});

common/sampling.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ struct common_sampler {
113113
void set_logits(struct llama_context * ctx, int idx) {
114114
const auto * logits = llama_get_logits_ith(ctx, idx);
115115

116-
const int n_vocab = llama_n_vocab(llama_get_model(ctx));
116+
const llama_model * model = llama_get_model(ctx);
117+
const llama_vocab * vocab = llama_get_vocab(model);
118+
119+
const int n_vocab = llama_n_vocab(vocab);
117120

118121
cur.resize(n_vocab);
119122

@@ -159,7 +162,7 @@ struct common_sampler * common_sampler_init(const struct llama_model * model, co
159162

160163
llama_sampler_chain_add(result->chain,
161164
llama_sampler_init_logit_bias(
162-
llama_n_vocab(model),
165+
llama_n_vocab(vocab),
163166
params.logit_bias.size(),
164167
params.logit_bias.data()));
165168

@@ -208,7 +211,7 @@ struct common_sampler * common_sampler_init(const struct llama_model * model, co
208211
llama_sampler_chain_add(result->chain, llama_sampler_init_dist(params.seed));
209212
} else if (params.mirostat == 1) {
210213
llama_sampler_chain_add(result->chain, llama_sampler_init_temp(params.temp));
211-
llama_sampler_chain_add(result->chain, llama_sampler_init_mirostat(llama_n_vocab(model), params.seed, params.mirostat_tau, params.mirostat_eta, 100));
214+
llama_sampler_chain_add(result->chain, llama_sampler_init_mirostat(llama_n_vocab(vocab), params.seed, params.mirostat_tau, params.mirostat_eta, 100));
212215
} else if (params.mirostat == 2) {
213216
llama_sampler_chain_add(result->chain, llama_sampler_init_temp(params.temp));
214217
llama_sampler_chain_add(result->chain, llama_sampler_init_mirostat_v2(params.seed, params.mirostat_tau, params.mirostat_eta));

common/speculative.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,15 @@ bool common_speculative_are_compatible(
105105
}
106106

107107
{
108-
const int n_vocab_tgt = llama_n_vocab(model_tgt);
109-
const int n_vocab_dft = llama_n_vocab(model_dft);
108+
const int n_vocab_tgt = llama_n_vocab(vocab_tgt);
109+
const int n_vocab_dft = llama_n_vocab(vocab_dft);
110110

111111
const int vocab_diff = std::abs(n_vocab_tgt - n_vocab_dft);
112112

113113
if (vocab_diff > SPEC_VOCAB_MAX_SIZE_DIFFERENCE) {
114114
LOG_ERR("%s: draft model vocab must closely match target model to use speculation but "
115115
"target vocab size %d does not match draft vocab size %d - difference %d, max allowed %d\n",
116-
__func__, n_vocab_tgt, llama_n_vocab(model_dft), vocab_diff, SPEC_VOCAB_MAX_SIZE_DIFFERENCE);
116+
__func__, n_vocab_tgt, llama_n_vocab(vocab_dft), vocab_diff, SPEC_VOCAB_MAX_SIZE_DIFFERENCE);
117117
return false;
118118
}
119119

examples/imatrix/imatrix.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ static bool compute_imatrix(llama_context * ctx, const common_params & params) {
471471
const int n_chunk_max = tokens.size() / n_ctx;
472472

473473
const int n_chunk = params.n_chunks < 0 ? n_chunk_max : std::min(params.n_chunks, n_chunk_max);
474-
const int n_vocab = llama_n_vocab(llama_get_model(ctx));
474+
const int n_vocab = llama_n_vocab(vocab);
475475
const int n_batch = params.n_batch;
476476

477477
int count = 0;

examples/llama-bench/llama-bench.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,7 @@ static void test_prompt(llama_context * ctx, int n_prompt, int n_batch, int n_th
14021402

14031403
const llama_model * model = llama_get_model(ctx);
14041404
const llama_vocab * vocab = llama_get_vocab(model);
1405-
const int32_t n_vocab = llama_n_vocab(model);
1405+
const int32_t n_vocab = llama_n_vocab(vocab);
14061406

14071407
std::vector<llama_token> tokens(n_batch);
14081408

@@ -1426,7 +1426,7 @@ static void test_gen(llama_context * ctx, int n_gen, int n_threads) {
14261426

14271427
const llama_model * model = llama_get_model(ctx);
14281428
const llama_vocab * vocab = llama_get_vocab(model);
1429-
const int32_t n_vocab = llama_n_vocab(model);
1429+
const int32_t n_vocab = llama_n_vocab(vocab);
14301430

14311431
llama_token token = llama_add_bos_token(vocab) ? llama_token_bos(vocab) : std::rand() % n_vocab;
14321432

examples/lookahead/lookahead.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ int main(int argc, char ** argv) {
149149
}
150150

151151
// here we keep adding new n-grams as we go
152-
ngram_container ngrams_observed(llama_n_vocab(model), N, G);
152+
ngram_container ngrams_observed(llama_n_vocab(vocab), N, G);
153153

154154
// debug
155155
struct llama_kv_cache_view kvc_view = llama_kv_cache_view_init(ctx, W + G + 1);

examples/perplexity/perplexity.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ static results_perplexity perplexity_v2(llama_context * ctx, const common_params
341341
const int n_chunk = params.n_chunks < 0 ? n_chunk_max : std::min(params.n_chunks, n_chunk_max);
342342
const int n_batch = params.n_batch;
343343

344-
const int n_vocab = llama_n_vocab(llama_get_model(ctx));
344+
const int n_vocab = llama_n_vocab(vocab);
345345

346346
int count = 0;
347347
double nll = 0.0;
@@ -491,7 +491,7 @@ static results_perplexity perplexity(llama_context * ctx, const common_params &
491491
const int n_chunk = params.n_chunks < 0 ? n_chunk_max : std::min(params.n_chunks, n_chunk_max);
492492
const int n_batch = params.n_batch;
493493

494-
const int n_vocab = llama_n_vocab(llama_get_model(ctx));
494+
const int n_vocab = llama_n_vocab(vocab);
495495

496496
int count = 0;
497497
double nll = 0.0;
@@ -857,7 +857,7 @@ static void hellaswag_score(llama_context * ctx, const common_params & params) {
857857
const int n_ctx = llama_n_ctx(ctx);
858858
const int n_batch = params.n_batch;
859859

860-
const int n_vocab = llama_n_vocab(llama_get_model(ctx));
860+
const int n_vocab = llama_n_vocab(vocab);
861861

862862
const int max_tasks_per_batch = 32;
863863
const int max_seq = std::min(4*max_tasks_per_batch, (int) llama_n_seq_max(ctx));
@@ -1081,6 +1081,8 @@ static std::vector<winogrande_entry> load_winogrande_from_csv(const std::string
10811081
*
10821082
*/
10831083
static void winogrande_score(llama_context * ctx, const common_params & params) {
1084+
const llama_model * model = llama_get_model(ctx);
1085+
const llama_vocab * vocab = llama_get_vocab(model);
10841086

10851087
constexpr int k_min_trailing_ctx = 3;
10861088

@@ -1139,7 +1141,7 @@ static void winogrande_score(llama_context * ctx, const common_params & params)
11391141
const int n_ctx = llama_n_ctx(ctx);
11401142
const int n_batch = params.n_batch;
11411143

1142-
const int n_vocab = llama_n_vocab(llama_get_model(ctx));
1144+
const int n_vocab = llama_n_vocab(vocab);
11431145

11441146
const int max_tasks_per_batch = 128;
11451147
const int max_seq = std::min(2*max_tasks_per_batch, (int) llama_n_seq_max(ctx));
@@ -1383,6 +1385,8 @@ static bool multiple_choice_prepare_one_task(llama_context * ctx, multiple_choic
13831385
// https://huggingface.co/datasets/truthful_qa
13841386
//
13851387
static void multiple_choice_score(llama_context * ctx, const common_params & params) {
1388+
const llama_model * model = llama_get_model(ctx);
1389+
const llama_vocab * vocab = llama_get_vocab(model);
13861390

13871391
std::istringstream strstream(params.prompt);
13881392
uint32_t n_task;
@@ -1491,7 +1495,7 @@ static void multiple_choice_score(llama_context * ctx, const common_params & par
14911495
const int n_ctx = llama_n_ctx(ctx);
14921496
const int n_batch = params.n_batch;
14931497

1494-
const int n_vocab = llama_n_vocab(llama_get_model(ctx));
1498+
const int n_vocab = llama_n_vocab(vocab);
14951499

14961500
const int max_tasks_per_batch = 32;
14971501
const int max_seq = std::min(4*max_tasks_per_batch, (int) llama_n_seq_max(ctx));
@@ -1700,8 +1704,8 @@ static void kl_divergence(llama_context * ctx, const common_params & params) {
17001704
LOG_ERR("%s: failed reading n_vocab, n_chunk from %s\n", __func__, params.logits_file.c_str());
17011705
return;
17021706
}
1703-
if (n_vocab != llama_n_vocab(llama_get_model(ctx))) {
1704-
LOG_ERR("%s: inconsistent vocabulary (%d vs %d)\n", __func__, n_vocab, llama_n_vocab(llama_get_model(ctx)));
1707+
if (n_vocab != llama_n_vocab(vocab)) {
1708+
LOG_ERR("%s: inconsistent vocabulary (%d vs %d)\n", __func__, n_vocab, llama_n_vocab(vocab));
17051709
}
17061710

17071711
std::vector<llama_token> tokens(size_t(n_ctx) * n_chunk);

examples/server/server.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,12 @@ struct server_task {
203203
server_task(server_task_type type) : type(type) {}
204204

205205
static slot_params params_from_json_cmpl(
206-
const llama_model * model,
207206
const llama_context * ctx,
208207
const common_params & params_base,
209208
const json & data) {
209+
const llama_model * model = llama_get_model(ctx);
210+
const llama_vocab * vocab = llama_get_vocab(model);
211+
210212
slot_params params;
211213

212214
// Sampling parameter defaults are loaded from the global server context (but individual requests can still override them)
@@ -329,7 +331,7 @@ struct server_task {
329331

330332
const auto & logit_bias = data.find("logit_bias");
331333
if (logit_bias != data.end() && logit_bias->is_array()) {
332-
const int n_vocab = llama_n_vocab(model);
334+
const int n_vocab = llama_n_vocab(vocab);
333335
for (const auto & el : *logit_bias) {
334336
// TODO: we may want to throw errors here, in case "el" is incorrect
335337
if (el.is_array() && el.size() == 2) {
@@ -348,7 +350,7 @@ struct server_task {
348350
params.sampling.logit_bias.push_back({tok, bias});
349351
}
350352
} else if (el[0].is_string()) {
351-
auto toks = common_tokenize(llama_get_vocab(model), el[0].get<std::string>(), false);
353+
auto toks = common_tokenize(vocab, el[0].get<std::string>(), false);
352354
for (auto tok : toks) {
353355
params.sampling.logit_bias.push_back({tok, bias});
354356
}
@@ -2079,7 +2081,7 @@ struct server_context {
20792081

20802082
void populate_token_probs(const server_slot & slot, completion_token_output & result, bool post_sampling, bool special, int idx) {
20812083
size_t n_probs = slot.params.sampling.n_probs;
2082-
size_t n_vocab = llama_n_vocab(llama_get_model(ctx));
2084+
size_t n_vocab = llama_n_vocab(vocab);
20832085
if (post_sampling) {
20842086
const auto * cur_p = common_sampler_get_candidates(slot.smpl);
20852087
const size_t max_probs = cur_p->size;
@@ -3135,7 +3137,7 @@ struct server_context {
31353137
json model_meta() const {
31363138
return json {
31373139
{"vocab_type", llama_vocab_type (vocab)},
3138-
{"n_vocab", llama_n_vocab (model)},
3140+
{"n_vocab", llama_n_vocab (vocab)},
31393141
{"n_ctx_train", llama_n_ctx_train (model)},
31403142
{"n_embd", llama_n_embd (model)},
31413143
{"n_params", llama_model_n_params(model)},
@@ -3654,7 +3656,6 @@ int main(int argc, char ** argv) {
36543656

36553657
task.prompt_tokens = std::move(tokenized_prompts[i]);
36563658
task.params = server_task::params_from_json_cmpl(
3657-
ctx_server.model,
36583659
ctx_server.ctx,
36593660
ctx_server.params_base,
36603661
data);

examples/server/utils.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -765,14 +765,18 @@ static json format_logit_bias(const std::vector<llama_logit_bias> & logit_bias)
765765
return data;
766766
}
767767

768-
static std::string safe_json_to_str(json data) {
768+
static std::string safe_json_to_str(const json & data) {
769769
return data.dump(-1, ' ', false, json::error_handler_t::replace);
770770
}
771771

772772
static std::vector<llama_token_data> get_token_probabilities(llama_context * ctx, int idx) {
773773
std::vector<llama_token_data> cur;
774774
const auto * logits = llama_get_logits_ith(ctx, idx);
775-
const int n_vocab = llama_n_vocab(llama_get_model(ctx));
775+
776+
const llama_model * model = llama_get_model(ctx);
777+
const llama_vocab * vocab = llama_get_vocab(model);
778+
779+
const int n_vocab = llama_n_vocab(vocab);
776780

777781
cur.resize(n_vocab);
778782
for (llama_token token_id = 0; token_id < n_vocab; token_id++) {

examples/speculative/speculative.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,16 @@ int main(int argc, char ** argv) {
116116
}
117117

118118
{
119-
const int n_vocab_tgt = llama_n_vocab(model_tgt);
120-
const int n_vocab_dft = llama_n_vocab(model_dft);
119+
const int n_vocab_tgt = llama_n_vocab(vocab_tgt);
120+
const int n_vocab_dft = llama_n_vocab(vocab_dft);
121121
const int vocab_diff = n_vocab_tgt > n_vocab_dft
122122
? n_vocab_tgt - n_vocab_dft
123123
: n_vocab_dft - n_vocab_tgt;
124124

125125
if (vocab_diff > SPEC_VOCAB_MAX_SIZE_DIFFERENCE) {
126126
LOG_ERR("%s: draft model vocab must closely match target model to use speculation but ", __func__);
127127
LOG_ERR("target vocab size %d does not match draft vocab size %d - difference %d, max allowed %d\n",
128-
n_vocab_tgt, llama_n_vocab(model_dft), vocab_diff, SPEC_VOCAB_MAX_SIZE_DIFFERENCE);
128+
n_vocab_tgt, llama_n_vocab(vocab_dft), vocab_diff, SPEC_VOCAB_MAX_SIZE_DIFFERENCE);
129129
return 1;
130130
}
131131

include/llama.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,12 +449,13 @@ extern "C" {
449449
LLAMA_API uint32_t llama_n_ubatch (const struct llama_context * ctx);
450450
LLAMA_API uint32_t llama_n_seq_max (const struct llama_context * ctx);
451451

452-
LLAMA_API int32_t llama_n_vocab (const struct llama_model * model);
453452
LLAMA_API int32_t llama_n_ctx_train(const struct llama_model * model);
454453
LLAMA_API int32_t llama_n_embd (const struct llama_model * model);
455454
LLAMA_API int32_t llama_n_layer (const struct llama_model * model);
456455
LLAMA_API int32_t llama_n_head (const struct llama_model * model);
457456

457+
LLAMA_API int32_t llama_n_vocab (const struct llama_vocab * vocab);
458+
458459
LLAMA_API const struct llama_model * llama_get_model(const struct llama_context * ctx);
459460
LLAMA_API const struct llama_vocab * llama_get_vocab(const struct llama_model * model);
460461

src/llama-model.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3691,10 +3691,6 @@ void llama_model_free(struct llama_model * model) {
36913691
delete model;
36923692
}
36933693

3694-
int32_t llama_n_vocab(const struct llama_model * model) {
3695-
return model->hparams.n_vocab;
3696-
}
3697-
36983694
int32_t llama_n_ctx_train(const struct llama_model * model) {
36993695
return model->hparams.n_ctx_train;
37003696
}

src/llama-sampling.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,10 @@ void llama_sampler_free(struct llama_sampler * smpl) {
371371
llama_token llama_sampler_sample(struct llama_sampler * smpl, struct llama_context * ctx, int32_t idx) {
372372
const auto * logits = llama_get_logits_ith(ctx, idx);
373373

374-
const int n_vocab = llama_n_vocab(llama_get_model(ctx));
374+
const llama_model * model = llama_get_model(ctx);
375+
const llama_vocab * vocab = llama_get_vocab(model);
376+
377+
const int n_vocab = llama_n_vocab(vocab);
375378

376379
// TODO: do not allocate each time
377380
std::vector<llama_token_data> cur;

src/llama-vocab.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,16 +1987,20 @@ void llama_vocab::load(llama_model_loader & ml, const LLM_KV & kv) {
19871987
for (auto id : cache_special_tokens) {
19881988
_set_tokenid_attr(id, LLAMA_TOKEN_ATTR_RSTRIP, true);
19891989
}
1990-
for (auto token : {"</s>"}) {
1990+
for (const auto * token : {"</s>"}) {
19911991
_set_token_attr(token, LLAMA_TOKEN_ATTR_RSTRIP, true);
19921992
}
1993-
for (auto token : {"<unk>", "<s>", "<|endoftext|>"}) {
1993+
for (const auto * token : {"<unk>", "<s>", "<|endoftext|>"}) {
19941994
_set_token_attr(token, LLAMA_TOKEN_ATTR_RSTRIP, false);
19951995
}
19961996
}
19971997
}
19981998
}
19991999

2000+
int32_t llama_n_vocab(const struct llama_vocab * vocab) {
2001+
return vocab->n_vocab();
2002+
}
2003+
20002004
enum llama_vocab_type llama_vocab::get_type() const {
20012005
return type;
20022006
}

tests/test-tokenizer-1-bpe.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ int main(int argc, char **argv) {
7777
atexit([]() { console::cleanup(); });
7878
#endif
7979

80-
const int n_vocab = llama_n_vocab(model);
80+
const int n_vocab = llama_n_vocab(vocab);
8181

8282
for (int i = 0; i < n_vocab; ++i) {
8383
std::string str = common_detokenize(ctx, std::vector<int>(1, i));

tests/test-tokenizer-1-spm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ int main(int argc, char ** argv) {
6565
atexit([]() { console::cleanup(); });
6666
#endif
6767

68-
const int n_vocab = llama_n_vocab(model);
68+
const int n_vocab = llama_n_vocab(vocab);
6969

7070
for (int i = 0; i < n_vocab; ++i) {
7171
std::string str = common_detokenize(ctx, std::vector<int>(1, i), true);

0 commit comments

Comments
 (0)