Skip to content

Commit e428393

Browse files
committed
examples : fix
ggml-ci
1 parent 963fb4d commit e428393

File tree

21 files changed

+87
-174
lines changed

21 files changed

+87
-174
lines changed

common/common.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -922,14 +922,14 @@ struct common_init_result common_init_from_params(common_params & params) {
922922
common_lora_adapter_container loaded_la;
923923
loaded_la.path = la.path;
924924
loaded_la.scale = la.scale;
925-
loaded_la.adapter = llama_lora_adapter_init(model, la.path.c_str());
925+
loaded_la.adapter.reset(llama_lora_adapter_init(model, la.path.c_str()));
926926
if (loaded_la.adapter == nullptr) {
927927
LOG_ERR("%s: failed to apply lora adapter '%s'\n", __func__, la.path.c_str());
928928
llama_free(lctx);
929929
llama_free_model(model);
930930
return iparams;
931931
}
932-
iparams.lora_adapters.push_back(loaded_la); // copy to list of loaded adapters
932+
iparams.lora_adapters.emplace_back(std::move(loaded_la)); // copy to list of loaded adapters
933933
}
934934
if (!params.lora_init_without_apply) {
935935
common_lora_adapters_apply(lctx, iparams.lora_adapters);
@@ -993,8 +993,8 @@ struct common_init_result common_init_from_params(common_params & params) {
993993
llama_perf_context_reset(lctx);
994994
}
995995

996-
iparams.model = model;
997-
iparams.context = lctx;
996+
iparams.model.reset(model);
997+
iparams.context.reset(lctx);
998998

999999
return iparams;
10001000
}
@@ -1003,7 +1003,7 @@ void common_lora_adapters_apply(struct llama_context * ctx, std::vector<common_l
10031003
llama_lora_adapter_clear(ctx);
10041004
for (auto & la : lora_adapters) {
10051005
if (la.scale != 0.0f) {
1006-
llama_lora_adapter_set(ctx, la.adapter, la.scale);
1006+
llama_lora_adapter_set(ctx, la.adapter.get(), la.scale);
10071007
}
10081008
}
10091009
}

common/common.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#pragma once
44

5-
#include "llama.h"
5+
#include "llama-cpp.h"
66

77
#include <string>
88
#include <vector>
@@ -30,7 +30,7 @@ struct common_lora_adapter_info {
3030
};
3131

3232
struct common_lora_adapter_container : common_lora_adapter_info {
33-
struct llama_lora_adapter * adapter;
33+
llama_lora_adapter_ptr adapter;
3434
};
3535

3636
using llama_tokens = std::vector<llama_token>;
@@ -479,19 +479,10 @@ std::string fs_get_cache_file(const std::string & filename);
479479
//
480480

481481
struct common_init_result {
482-
struct llama_model * model = nullptr;
483-
struct llama_context * context = nullptr;
482+
llama_model_ptr model;
483+
llama_context_ptr context;
484484

485485
std::vector<common_lora_adapter_container> lora_adapters;
486-
487-
~common_init_result() {
488-
llama_free(context);
489-
llama_free_model(model);
490-
491-
for (auto & lora_adapter : lora_adapters) {
492-
llama_lora_adapter_free(lora_adapter.adapter);
493-
}
494-
}
495486
};
496487

497488
struct common_init_result common_init_from_params(common_params & params);

examples/cvector-generator/cvector-generator.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,12 +415,13 @@ int main(int argc, char ** argv) {
415415
// load the model to get hparams
416416
common_init_result llama_init = common_init_from_params(params);
417417

418-
llama_model * model = llama_init.model;
419-
llama_context * ctx = llama_init.context;
418+
llama_model * model = llama_init.model.get();
419+
llama_context * ctx = llama_init.context.get();
420420

421421
// int n_ctx = llama_n_ctx(ctx);
422422
int n_layers = llama_n_layer(model);
423423
int n_embd = llama_n_embd(model);
424+
424425
// get model hint param (a.k.a model arch name)
425426
char model_hint[128];
426427
llama_model_meta_val_str(model, "general.architecture", model_hint, 128);
@@ -474,8 +475,6 @@ int main(int argc, char ** argv) {
474475

475476
// done with the model, we can now free it to make gain some memory
476477
printf("Done evaluate prompts, unload model...\n");
477-
llama_free(ctx);
478-
llama_free_model(model);
479478

480479
bool use_pca = params.cvector_dimre_method == DIMRE_METHOD_PCA;
481480

examples/embedding/embedding.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ int main(int argc, char ** argv) {
9797
// load the model
9898
common_init_result llama_init = common_init_from_params(params);
9999

100-
llama_model * model = llama_init.model;
101-
llama_context * ctx = llama_init.context;
100+
llama_model * model = llama_init.model.get();
101+
llama_context * ctx = llama_init.context.get();
102+
102103
if (model == NULL) {
103104
LOG_ERR("%s: unable to load model\n", __func__);
104105
return 1;
@@ -316,8 +317,6 @@ int main(int argc, char ** argv) {
316317

317318
// clean up
318319
llama_batch_free(batch);
319-
llama_free(ctx);
320-
llama_free_model(model);
321320
llama_backend_free();
322321

323322
return 0;

examples/eval-callback/eval-callback.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,9 @@ int main(int argc, char ** argv) {
162162
// init
163163
common_init_result llama_init = common_init_from_params(params);
164164

165-
llama_model * model = llama_init.model;
166-
llama_context * ctx = llama_init.context;
165+
llama_model * model = llama_init.model.get();
166+
llama_context * ctx = llama_init.context.get();
167+
167168
if (model == nullptr || ctx == nullptr) {
168169
LOG_ERR("%s : failed to init\n", __func__);
169170
return 1;
@@ -184,9 +185,6 @@ int main(int argc, char ** argv) {
184185
LOG("\n");
185186
llama_perf_context_print(ctx);
186187

187-
llama_free(ctx);
188-
llama_free_model(model);
189-
190188
llama_backend_free();
191189

192190
return 0;

examples/imatrix/imatrix.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,10 @@ static void process_logits(
430430

431431
static bool compute_imatrix(llama_context * ctx, const common_params & params) {
432432
const bool add_bos = llama_add_bos_token(llama_get_model(ctx));
433-
GGML_ASSERT(!llama_add_eos_token(llama_get_model(ctx)));
434433
const int n_ctx = llama_n_ctx(ctx);
435434

435+
GGML_ASSERT(!llama_add_eos_token(llama_get_model(ctx)));
436+
436437
auto tim1 = std::chrono::high_resolution_clock::now();
437438
LOG_INF("%s: tokenizing the input ..\n", __func__);
438439

@@ -618,8 +619,9 @@ int main(int argc, char ** argv) {
618619
// init
619620
common_init_result llama_init = common_init_from_params(params);
620621

621-
llama_model * model = llama_init.model;
622-
llama_context * ctx = llama_init.context;
622+
llama_model * model = llama_init.model.get();
623+
llama_context * ctx = llama_init.context.get();
624+
623625
if (model == nullptr || ctx == nullptr) {
624626
LOG_ERR("%s : failed to init\n", __func__);
625627
return 1;
@@ -655,9 +657,6 @@ int main(int argc, char ** argv) {
655657
LOG("\n");
656658
llama_perf_context_print(ctx);
657659

658-
llama_free(ctx);
659-
llama_free_model(model);
660-
661660
llama_backend_free();
662661

663662
return 0;

examples/infill/infill.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ int main(int argc, char ** argv) {
131131
LOG_INF("%s: load the model and apply lora adapter, if any\n", __func__);
132132
common_init_result llama_init = common_init_from_params(params);
133133

134-
model = llama_init.model;
135-
ctx = llama_init.context;
134+
model = llama_init.model.get();
135+
ctx = llama_init.context.get();
136136

137137
if (model == NULL) {
138138
LOG_ERR("%s: unable to load model\n", __func__);
@@ -581,9 +581,6 @@ int main(int argc, char ** argv) {
581581
LOG("\n");
582582
common_perf_print(ctx, smpl);
583583

584-
llama_free(ctx);
585-
llama_free_model(model);
586-
587584
common_sampler_free(smpl);
588585
llama_backend_free();
589586

examples/lookahead/lookahead.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ int main(int argc, char ** argv) {
5858
// load the target model
5959
common_init_result llama_init = common_init_from_params(params);
6060

61-
llama_model * model = llama_init.model;
62-
llama_context * ctx = llama_init.context;
61+
llama_model * model = llama_init.model.get();
62+
llama_context * ctx = llama_init.context.get();
6363

6464
// Tokenize the prompt
6565
std::vector<llama_token> inp;
@@ -474,9 +474,6 @@ int main(int argc, char ** argv) {
474474

475475
llama_batch_free(batch);
476476

477-
llama_free(ctx);
478-
llama_free_model(model);
479-
480477
llama_backend_free();
481478

482479
LOG("\n\n");

examples/lookup/lookup-create.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
#include "arg.h"
22
#include "common.h"
33
#include "ngram-cache.h"
4-
#include "ggml.h"
54
#include "llama.h"
65

7-
#include <cstdint>
8-
#include <fstream>
9-
#include <iostream>
106
#include <string>
11-
#include <unordered_map>
127
#include <vector>
138

149
int main(int argc, char ** argv){
@@ -25,16 +20,16 @@ int main(int argc, char ** argv){
2520
// load the model
2621
common_init_result llama_init = common_init_from_params(params);
2722

28-
llama_model * model = llama_init.model;
29-
llama_context * ctx = llama_init.context;
23+
llama_model_ptr & model = llama_init.model;
24+
llama_context_ptr & ctx = llama_init.context;
25+
3026
GGML_ASSERT(model != nullptr);
3127

3228
// tokenize the prompt
3329
std::vector<llama_token> inp;
34-
inp = common_tokenize(ctx, params.prompt, true, true);
30+
inp = common_tokenize(ctx.get(), params.prompt, true, true);
3531
fprintf(stderr, "%s: tokenization done\n", __func__);
3632

37-
3833
common_ngram_cache ngram_cache;
3934
common_ngram_cache_update(ngram_cache, LLAMA_NGRAM_STATIC, LLAMA_NGRAM_STATIC, inp, inp.size(), true);
4035
fprintf(stderr, "%s: hashing done, writing file to %s\n", __func__, params.lookup_cache_static.c_str());

examples/lookup/lookup-stats.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@ int main(int argc, char ** argv){
3030
// load the model
3131
common_init_result llama_init = common_init_from_params(params);
3232

33-
llama_model * model = llama_init.model;
34-
llama_context * ctx = llama_init.context;
33+
llama_context_ptr & ctx = llama_init.context;
3534

3635
// tokenize the prompt
3736
std::vector<llama_token> inp;
38-
inp = common_tokenize(ctx, params.prompt, true, true);
37+
inp = common_tokenize(ctx.get(), params.prompt, true, true);
3938

4039
common_ngram_cache ngram_cache_context;
4140
common_ngram_cache ngram_cache_dynamic;
@@ -66,7 +65,7 @@ int main(int argc, char ** argv){
6665
}
6766

6867
const int n_input = inp.size();
69-
const int n_ctx = llama_n_ctx(ctx);
68+
const int n_ctx = llama_n_ctx(ctx.get());
7069

7170
int n_drafted = 0;
7271
int n_accept = 0;
@@ -150,9 +149,6 @@ int main(int argc, char ** argv){
150149
LOG_INF("n_accept = %d\n", n_accept);
151150
LOG_INF("accept = %.3f%%\n", 100.0f * n_accept / n_drafted);
152151

153-
llama_free(ctx);
154-
llama_free_model(model);
155-
156152
llama_backend_free();
157153

158154
LOG("\n\n");

examples/lookup/lookup.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ int main(int argc, char ** argv){
3333
// load the model
3434
common_init_result llama_init = common_init_from_params(params);
3535

36-
llama_model * model = llama_init.model;
37-
llama_context * ctx = llama_init.context;
36+
llama_model * model = llama_init.model.get();
37+
llama_context * ctx = llama_init.context.get();
3838

3939
// tokenize the prompt
4040
std::vector<llama_token> inp;
@@ -243,9 +243,6 @@ int main(int argc, char ** argv){
243243

244244
llama_batch_free(batch_tgt);
245245

246-
llama_free(ctx);
247-
llama_free_model(model);
248-
249246
llama_backend_free();
250247

251248
LOG("\n\n");

examples/main/main.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,18 @@ int main(int argc, char ** argv) {
145145
llama_context * ctx = nullptr;
146146
common_sampler * smpl = nullptr;
147147

148-
std::vector<common_chat_msg> chat_msgs;
149-
150148
g_model = &model;
151149
g_ctx = &ctx;
152150
g_smpl = &smpl;
153151

152+
std::vector<common_chat_msg> chat_msgs;
153+
154154
// load the model and apply lora adapter, if any
155155
LOG_INF("%s: load the model and apply lora adapter, if any\n", __func__);
156156
common_init_result llama_init = common_init_from_params(params);
157157

158-
model = llama_init.model;
159-
ctx = llama_init.context;
158+
model = llama_init.model.get();
159+
ctx = llama_init.context.get();
160160

161161
if (model == NULL) {
162162
LOG_ERR("%s: error: unable to load model\n", __func__);
@@ -889,9 +889,6 @@ int main(int argc, char ** argv) {
889889

890890
common_sampler_free(smpl);
891891

892-
llama_free(ctx);
893-
llama_free_model(model);
894-
895892
llama_backend_free();
896893

897894
ggml_threadpool_free_fn(threadpool);

examples/parallel/parallel.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ int main(int argc, char ** argv) {
132132
// load the target model
133133
common_init_result llama_init = common_init_from_params(params);
134134

135-
llama_model * model = llama_init.model;
136-
llama_context * ctx = llama_init.context;
135+
llama_model * model = llama_init.model.get();
136+
llama_context * ctx = llama_init.context.get();
137137

138138
// load the prompts from an external file if there are any
139139
if (params.prompt.empty()) {
@@ -416,9 +416,6 @@ int main(int argc, char ** argv) {
416416

417417
llama_batch_free(batch);
418418

419-
llama_free(ctx);
420-
llama_free_model(model);
421-
422419
llama_backend_free();
423420

424421
LOG("\n\n");

examples/perplexity/perplexity.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,8 +1987,9 @@ int main(int argc, char ** argv) {
19871987
// load the model and apply lora adapter, if any
19881988
common_init_result llama_init = common_init_from_params(params);
19891989

1990-
llama_model * model = llama_init.model;
1991-
llama_context * ctx = llama_init.context;
1990+
llama_model * model = llama_init.model.get();
1991+
llama_context * ctx = llama_init.context.get();
1992+
19921993
if (model == NULL) {
19931994
LOG_ERR("%s: unable to load model\n", __func__);
19941995
return 1;
@@ -2023,9 +2024,6 @@ int main(int argc, char ** argv) {
20232024
LOG("\n");
20242025
llama_perf_context_print(ctx);
20252026

2026-
llama_free(ctx);
2027-
llama_free_model(model);
2028-
20292027
llama_backend_free();
20302028

20312029
return 0;

0 commit comments

Comments
 (0)