Skip to content

Commit d2b95e7

Browse files
committed
refactor vocab loading into its own method
1 parent aab15de commit d2b95e7

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,32 @@ void write_tensor(struct llama_file * file, struct ggml_tensor * tensor) {
491491
file->write_raw(tensor->data, ggml_nbytes(tensor));
492492
}
493493

494+
void load_vocab(const char *filename, struct llama_vocab *vocab) {
495+
struct llama_context_params llama_params = llama_context_default_params();
496+
llama_params.vocab_only = true;
497+
498+
struct llama_model * lmodel = llama_load_model_from_file(filename, llama_params);
499+
struct llama_context * lctx = llama_new_context_with_model(lmodel, llama_params);
500+
501+
std::vector<const char *> strings;
502+
std::vector<float> scores;
503+
int n_vocab = llama_n_vocab(lctx);
504+
strings.resize(n_vocab, NULL);
505+
scores.resize(n_vocab, 0);
506+
n_vocab = llama_get_vocab(lctx, strings.data(), scores.data(), n_vocab);
507+
GGML_ASSERT(n_vocab == llama_n_vocab(lctx));
508+
vocab->id_to_token.resize(n_vocab);
509+
for (int i=0; i<n_vocab; ++i) {
510+
std::string tok = std::string(strings[i]);
511+
float score = scores[i];
512+
vocab->id_to_token[i].tok = tok;
513+
vocab->id_to_token[i].score = score;
514+
vocab->token_to_id.emplace(tok, i);
515+
}
516+
llama_free(lctx);
517+
llama_free_model(lmodel);
518+
}
519+
494520
void stuff_karpathy_weights_into_gg(struct ggml_tensor * gg_weights, float * karpathy_weights){
495521
int ct;
496522
switch (gg_weights->n_dims){
@@ -737,30 +763,9 @@ int main(int argc, char ** argv) {
737763
fclose(file);
738764
}
739765

740-
struct llama_context_params llama_params = llama_context_default_params();
741-
llama_params.vocab_only = true;
742-
743-
struct llama_model * lmodel = llama_load_model_from_file(params.fn_vocab_model, llama_params);
744-
struct llama_context * lctx = llama_new_context_with_model(lmodel, llama_params);
745-
746766
struct llama_vocab vocab;
747-
{
748-
std::vector<const char *> strings;
749-
std::vector<float> scores;
750-
int n_vocab = llama_n_vocab(lctx);
751-
strings.resize(n_vocab, NULL);
752-
scores.resize(n_vocab, 0);
753-
n_vocab = llama_get_vocab(lctx, strings.data(), scores.data(), n_vocab);
754-
GGML_ASSERT(n_vocab == llama_n_vocab(lctx));
755-
vocab.id_to_token.resize(n_vocab);
756-
for (int i=0; i<n_vocab; ++i) {
757-
std::string tok = std::string(strings[i]);
758-
float score = scores[i];
759-
vocab.id_to_token[i].tok = tok;
760-
vocab.id_to_token[i].score = score;
761-
vocab.token_to_id.emplace(tok, i);
762-
}
763-
}
767+
load_vocab(params.fn_vocab_model, &vocab);
768+
764769
struct my_llama_model model;
765770
model.hparams.n_vocab = config.vocab_size; //llama_n_vocab(lctx);
766771
model.hparams.n_ctx = params.n_ctx;
@@ -782,8 +787,6 @@ int main(int argc, char ** argv) {
782787

783788
printf("Saving llama.c model file %s in ggml format at %s\n", params.fn_llama2c_model, params.fn_llama2c_output_model);
784789

785-
llama_free(lctx);
786-
llama_free_model(lmodel);
787790
ggml_free(model.ctx);
788791
free_weights(&weights);
789792
return 0;

0 commit comments

Comments
 (0)