llama : deprecate llama_free_model, add llama_model_free

ggml-ci
This commit is contained in:
Georgi Gerganov 2025-01-03 14:37:28 +02:00
parent 4b0c638b9a
commit 0261d4f02f
No known key found for this signature in database
GPG key ID: 449E073F9DC10735
21 changed files with 44 additions and 38 deletions

View file

@ -873,7 +873,7 @@ struct common_init_result common_init_from_params(common_params & params) {
} }
if (!ok) { if (!ok) {
llama_free_model(model); llama_model_free(model);
return iparams; return iparams;
} }
@ -884,7 +884,7 @@ struct common_init_result common_init_from_params(common_params & params) {
llama_context * lctx = llama_new_context_with_model(model, cparams); llama_context * lctx = llama_new_context_with_model(model, cparams);
if (lctx == NULL) { if (lctx == NULL) {
LOG_ERR("%s: failed to create context with model '%s'\n", __func__, params.model.c_str()); LOG_ERR("%s: failed to create context with model '%s'\n", __func__, params.model.c_str());
llama_free_model(model); llama_model_free(model);
return iparams; return iparams;
} }
@ -900,7 +900,7 @@ struct common_init_result common_init_from_params(common_params & params) {
const auto cvec = common_control_vector_load(params.control_vectors); const auto cvec = common_control_vector_load(params.control_vectors);
if (cvec.n_embd == -1) { if (cvec.n_embd == -1) {
llama_free(lctx); llama_free(lctx);
llama_free_model(model); llama_model_free(model);
return iparams; return iparams;
} }
@ -913,7 +913,7 @@ struct common_init_result common_init_from_params(common_params & params) {
params.control_vector_layer_end); params.control_vector_layer_end);
if (err) { if (err) {
llama_free(lctx); llama_free(lctx);
llama_free_model(model); llama_model_free(model);
return iparams; return iparams;
} }
@ -926,7 +926,7 @@ struct common_init_result common_init_from_params(common_params & params) {
if (lora == nullptr) { if (lora == nullptr) {
LOG_ERR("%s: failed to apply lora adapter '%s'\n", __func__, la.path.c_str()); LOG_ERR("%s: failed to apply lora adapter '%s'\n", __func__, la.path.c_str());
llama_free(lctx); llama_free(lctx);
llama_free_model(model); llama_model_free(model);
return iparams; return iparams;
} }

View file

@ -194,7 +194,7 @@ int main(int argc, char ** argv) {
llama_batch_free(batch); llama_batch_free(batch);
llama_free(ctx); llama_free(ctx);
llama_free_model(model); llama_model_free(model);
llama_backend_free(); llama_backend_free();

View file

@ -236,7 +236,7 @@ int main(int argc, char ** argv) {
llama_sampler_free(smpl); llama_sampler_free(smpl);
llama_free(ctx); llama_free(ctx);
llama_free_model(model); llama_model_free(model);
llama_backend_free(); llama_backend_free();

View file

@ -219,7 +219,7 @@ int main(int argc, char * argv[]) {
llama_sampler_free(smpl); llama_sampler_free(smpl);
llama_free(ctx); llama_free(ctx);
llama_free_model(model); llama_model_free(model);
llama_backend_free(); llama_backend_free();
return 0; return 0;

View file

@ -1526,7 +1526,7 @@ int main(int argc, char ** argv) {
// keep the same model between tests when possible // keep the same model between tests when possible
if (!lmodel || !prev_inst || !inst.equal_mparams(*prev_inst)) { if (!lmodel || !prev_inst || !inst.equal_mparams(*prev_inst)) {
if (lmodel) { if (lmodel) {
llama_free_model(lmodel); llama_model_free(lmodel);
} }
lmodel = llama_load_model_from_file(inst.model.c_str(), inst.to_llama_mparams()); lmodel = llama_load_model_from_file(inst.model.c_str(), inst.to_llama_mparams());
@ -1540,7 +1540,7 @@ int main(int argc, char ** argv) {
llama_context * ctx = llama_new_context_with_model(lmodel, inst.to_llama_cparams()); llama_context * ctx = llama_new_context_with_model(lmodel, inst.to_llama_cparams());
if (ctx == NULL) { if (ctx == NULL) {
fprintf(stderr, "%s: error: failed to create context with model '%s'\n", __func__, inst.model.c_str()); fprintf(stderr, "%s: error: failed to create context with model '%s'\n", __func__, inst.model.c_str());
llama_free_model(lmodel); llama_model_free(lmodel);
return 1; return 1;
} }
@ -1626,7 +1626,7 @@ int main(int argc, char ** argv) {
ggml_threadpool_free_fn(threadpool); ggml_threadpool_free_fn(threadpool);
} }
llama_free_model(lmodel); llama_model_free(lmodel);
if (p) { if (p) {
p->print_footer(); p->print_footer();

View file

@ -265,7 +265,7 @@ static void llava_free(struct llava_context * ctx_llava) {
} }
llama_free(ctx_llava->ctx_llama); llama_free(ctx_llava->ctx_llama);
llama_free_model(ctx_llava->model); llama_model_free(ctx_llava->model);
llama_backend_free(); llama_backend_free();
} }
@ -323,7 +323,7 @@ int main(int argc, char ** argv) {
} }
} }
llama_free_model(model); llama_model_free(model);
return 0; return 0;
} }

View file

@ -75,7 +75,7 @@ static void llava_free(struct llava_context * ctx_llava) {
} }
llama_free(ctx_llava->ctx_llama); llama_free(ctx_llava->ctx_llama);
llama_free_model(ctx_llava->model); llama_model_free(ctx_llava->model);
llama_backend_free(); llama_backend_free();
} }

View file

@ -354,7 +354,7 @@ static void llava_free(struct llava_context * ctx_llava) {
} }
llama_free(ctx_llava->ctx_llama); llama_free(ctx_llava->ctx_llama);
llama_free_model(ctx_llava->model); llama_model_free(ctx_llava->model);
llama_backend_free(); llama_backend_free();
} }
@ -575,7 +575,7 @@ int main(int argc, char ** argv) {
} }
} }
llama_free_model(model); llama_model_free(model);
return 0; return 0;
} }

View file

@ -266,7 +266,7 @@ int main(int argc, char ** argv) {
llama_batch_free(batch); llama_batch_free(batch);
llama_free(ctx); llama_free(ctx);
llama_free_model(model); llama_model_free(model);
llama_backend_free(); llama_backend_free();

View file

@ -323,7 +323,7 @@ int main(int argc, char ** argv) {
if (ctx == NULL) { if (ctx == NULL) {
fprintf(stderr, "%s: error: failed to create context with model '%s'\n", __func__, params.model.c_str()); fprintf(stderr, "%s: error: failed to create context with model '%s'\n", __func__, params.model.c_str());
llama_free_model(model); llama_model_free(model);
return 1; return 1;
} }
} }
@ -347,7 +347,7 @@ int main(int argc, char ** argv) {
fprintf(stderr, "%s: error: Quantization should be tested with a float model, " fprintf(stderr, "%s: error: Quantization should be tested with a float model, "
"this model contains already quantized layers (%s is type %d)\n", __func__, kv_tensor.first.c_str(), kv_tensor.second->type); "this model contains already quantized layers (%s is type %d)\n", __func__, kv_tensor.first.c_str(), kv_tensor.second->type);
llama_free(ctx); llama_free(ctx);
llama_free_model(model); llama_model_free(model);
return 1; return 1;
} }
included_layers++; included_layers++;
@ -409,7 +409,7 @@ int main(int argc, char ** argv) {
llama_free(ctx); llama_free(ctx);
llama_free_model(model); llama_model_free(model);
// report timing // report timing
{ {
const int64_t t_main_end_us = ggml_time_us(); const int64_t t_main_end_us = ggml_time_us();

View file

@ -194,7 +194,7 @@ int main(int argc, char ** argv) {
} }
llama_sampler_free(smpl); llama_sampler_free(smpl);
llama_free(ctx); llama_free(ctx);
llama_free_model(model); llama_model_free(model);
return 0; return 0;
} }

View file

@ -199,7 +199,7 @@ int main(int argc, char ** argv) {
llama_sampler_free(smpl); llama_sampler_free(smpl);
llama_free(ctx); llama_free(ctx);
llama_free_model(model); llama_model_free(model);
return 0; return 0;
} }

View file

@ -398,7 +398,7 @@ int main(int raw_argc, char ** raw_argv) {
} }
// silence valgrind // silence valgrind
llama_free(ctx); llama_free(ctx);
llama_free_model(model); llama_model_free(model);
return 0; return 0;
} }

View file

@ -9,7 +9,7 @@
#include "llama.h" #include "llama.h"
struct llama_model_deleter { struct llama_model_deleter {
void operator()(llama_model * model) { llama_free_model(model); } void operator()(llama_model * model) { llama_model_free(model); }
}; };
struct llama_context_deleter { struct llama_context_deleter {

View file

@ -417,8 +417,10 @@ extern "C" {
const char * path_model, const char * path_model,
struct llama_model_params params); struct llama_model_params params);
// TODO: rename to llama_model_free DEPRECATED(LLAMA_API void llama_free_model(struct llama_model * model),
LLAMA_API void llama_free_model(struct llama_model * model); "use llama_model_free instead");
LLAMA_API void llama_model_free(struct llama_model * model);
// TODO: rename to llama_init_from_model // TODO: rename to llama_init_from_model
LLAMA_API struct llama_context * llama_new_context_with_model( LLAMA_API struct llama_context * llama_new_context_with_model(

View file

@ -1976,6 +1976,10 @@ struct llama_model_params llama_model_default_params() {
} }
void llama_free_model(struct llama_model * model) { void llama_free_model(struct llama_model * model) {
llama_model_free(model);
}
void llama_model_free(struct llama_model * model) {
delete model; delete model;
} }

View file

@ -11494,7 +11494,7 @@ struct llama_model * llama_load_model_from_file(
ggml_backend_reg_t rpc_reg = ggml_backend_reg_by_name("RPC"); ggml_backend_reg_t rpc_reg = ggml_backend_reg_by_name("RPC");
if (!rpc_reg) { if (!rpc_reg) {
LLAMA_LOG_ERROR("%s: failed to find RPC backend\n", __func__); LLAMA_LOG_ERROR("%s: failed to find RPC backend\n", __func__);
llama_free_model(model); llama_model_free(model);
return nullptr; return nullptr;
} }
@ -11502,7 +11502,7 @@ struct llama_model * llama_load_model_from_file(
ggml_backend_rpc_add_device_t ggml_backend_rpc_add_device_fn = (ggml_backend_rpc_add_device_t) ggml_backend_reg_get_proc_address(rpc_reg, "ggml_backend_rpc_add_device"); ggml_backend_rpc_add_device_t ggml_backend_rpc_add_device_fn = (ggml_backend_rpc_add_device_t) ggml_backend_reg_get_proc_address(rpc_reg, "ggml_backend_rpc_add_device");
if (!ggml_backend_rpc_add_device_fn) { if (!ggml_backend_rpc_add_device_fn) {
LLAMA_LOG_ERROR("%s: failed to find RPC device add function\n", __func__); LLAMA_LOG_ERROR("%s: failed to find RPC device add function\n", __func__);
llama_free_model(model); llama_model_free(model);
return nullptr; return nullptr;
} }
@ -11512,7 +11512,7 @@ struct llama_model * llama_load_model_from_file(
model->devices.push_back(dev); model->devices.push_back(dev);
} else { } else {
LLAMA_LOG_ERROR("%s: failed to add RPC device for server '%s'\n", __func__, server.c_str()); LLAMA_LOG_ERROR("%s: failed to add RPC device for server '%s'\n", __func__, server.c_str());
llama_free_model(model); llama_model_free(model);
return nullptr; return nullptr;
} }
} }
@ -11544,7 +11544,7 @@ struct llama_model * llama_load_model_from_file(
if (params.split_mode == LLAMA_SPLIT_MODE_NONE) { if (params.split_mode == LLAMA_SPLIT_MODE_NONE) {
if (params.main_gpu < 0 || params.main_gpu >= (int)model->devices.size()) { if (params.main_gpu < 0 || params.main_gpu >= (int)model->devices.size()) {
LLAMA_LOG_ERROR("%s: invalid value for main_gpu: %d (available devices: %d)\n", __func__, params.main_gpu, (int)model->devices.size()); LLAMA_LOG_ERROR("%s: invalid value for main_gpu: %d (available devices: %d)\n", __func__, params.main_gpu, (int)model->devices.size());
llama_free_model(model); llama_model_free(model);
return nullptr; return nullptr;
} }
ggml_backend_dev_t main_gpu = model->devices[params.main_gpu]; ggml_backend_dev_t main_gpu = model->devices[params.main_gpu];
@ -11567,7 +11567,7 @@ struct llama_model * llama_load_model_from_file(
LLAMA_LOG_INFO("%s: cancelled model load\n", __func__); LLAMA_LOG_INFO("%s: cancelled model load\n", __func__);
} }
llama_free_model(model); llama_model_free(model);
return nullptr; return nullptr;
} }

View file

@ -16,7 +16,7 @@ int main(int argc, char ** argv) {
auto * model = llama_load_model_from_file(model_path, llama_model_default_params()); auto * model = llama_load_model_from_file(model_path, llama_model_default_params());
auto * ctx = llama_new_context_with_model(model, llama_context_default_params()); auto * ctx = llama_new_context_with_model(model, llama_context_default_params());
llama_free(ctx); llama_free(ctx);
llama_free_model(model); llama_model_free(model);
llama_backend_free(); llama_backend_free();
}).join(); }).join();

View file

@ -165,7 +165,7 @@ int main(int argc, char **argv) {
if (ctx == NULL) { if (ctx == NULL) {
fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str()); fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str());
llama_free_model(model); llama_model_free(model);
return 1; return 1;
} }
} }
@ -300,7 +300,7 @@ int main(int argc, char **argv) {
fprintf(stderr, "%s : tokens written to '%s'\n", __func__, (fname_text + ".tokcpp").c_str()); fprintf(stderr, "%s : tokens written to '%s'\n", __func__, (fname_text + ".tokcpp").c_str());
} }
llama_free_model(model); llama_model_free(model);
llama_free(ctx); llama_free(ctx);
llama_backend_free(); llama_backend_free();

View file

@ -59,7 +59,7 @@ int main(int argc, char **argv) {
if (ctx == NULL) { if (ctx == NULL) {
fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str()); fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str());
llama_free_model(model); llama_model_free(model);
return 1; return 1;
} }
} }
@ -143,7 +143,7 @@ int main(int argc, char **argv) {
} }
} }
llama_free_model(model); llama_model_free(model);
llama_free(ctx); llama_free(ctx);
llama_backend_free(); llama_backend_free();

View file

@ -47,7 +47,7 @@ int main(int argc, char ** argv) {
if (ctx == NULL) { if (ctx == NULL) {
fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str()); fprintf(stderr, "%s: error: failed to load vocab '%s'\n", __func__, fname.c_str());
llama_free_model(model); llama_model_free(model);
return 1; return 1;
} }
} }
@ -113,7 +113,7 @@ int main(int argc, char ** argv) {
} }
} }
llama_free_model(model); llama_model_free(model);
llama_free(ctx); llama_free(ctx);
llama_backend_free(); llama_backend_free();