@@ -491,6 +491,32 @@ void write_tensor(struct llama_file * file, struct ggml_tensor * tensor) {
491
491
file->write_raw (tensor->data , ggml_nbytes (tensor));
492
492
}
493
493
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
+
494
520
void stuff_karpathy_weights_into_gg (struct ggml_tensor * gg_weights, float * karpathy_weights){
495
521
int ct;
496
522
switch (gg_weights->n_dims ){
@@ -737,30 +763,9 @@ int main(int argc, char ** argv) {
737
763
fclose (file);
738
764
}
739
765
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
-
746
766
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
+
764
769
struct my_llama_model model;
765
770
model.hparams .n_vocab = config.vocab_size ; // llama_n_vocab(lctx);
766
771
model.hparams .n_ctx = params.n_ctx ;
@@ -782,8 +787,6 @@ int main(int argc, char ** argv) {
782
787
783
788
printf (" Saving llama.c model file %s in ggml format at %s\n " , params.fn_llama2c_model , params.fn_llama2c_output_model );
784
789
785
- llama_free (lctx);
786
- llama_free_model (lmodel);
787
790
ggml_free (model.ctx );
788
791
free_weights (&weights);
789
792
return 0 ;
0 commit comments