MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / export_lora

Function export_lora

examples/export-lora/export-lora.cpp:371–462  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

369}
370
371static void export_lora(struct export_lora_params * params) {
372 // load all loras
373 std::vector<struct lora_data *> loras;
374 for (size_t i = 0; i < params->lora.size(); ++i) {
375 struct lora_data * lora = load_lora(&params->lora[i]);
376 if (lora != NULL) {
377 loras.push_back(lora);
378 }
379 }
380 if (loras.size() == 0) {
381 fprintf(stderr, "warning: no lora adapters will be applied.\n");
382 }
383
384 // open input file
385 struct llama_file fin(params->fn_model_base.c_str(), "rb");
386 if (!fin.fp) {
387 die_fmt("Could not open file '%s'\n", params->fn_model_base.c_str());
388 }
389
390 // open base model gguf, read tensors without their data
391 struct ggml_context * ctx_in;
392 struct gguf_init_params params_gguf;
393 params_gguf.no_alloc = true;
394 params_gguf.ctx = &ctx_in;
395 struct gguf_context * gguf_in = gguf_init_from_file(params->fn_model_base.c_str(), params_gguf);
396
397 // create new gguf
398 struct gguf_context * gguf_out = gguf_init_empty();
399
400 // copy meta data from base model: kv and tensors
401 gguf_set_kv(gguf_out, gguf_in);
402 int n_tensors = gguf_get_n_tensors(gguf_in);
403 for (int i=0; i < n_tensors; ++i) {
404 const char * name = gguf_get_tensor_name(gguf_in, i);
405 struct ggml_tensor * tensor = ggml_get_tensor(ctx_in, name);
406 gguf_add_tensor(gguf_out, tensor);
407 }
408
409 // create output file
410 struct llama_file fout(params->fn_model_out.c_str(), "wb");
411 if (!fout.fp) {
412 die_fmt("Could not create file '%s'\n", params->fn_model_out.c_str());
413 }
414
415 // write gguf meta data
416 std::vector<uint8_t> meta;
417 meta.resize(gguf_get_meta_size(gguf_out));
418 gguf_get_meta_data(gguf_out, meta.data());
419 fout.write_raw(meta.data(), meta.size());
420
421 std::vector<uint8_t> data;
422 std::vector<uint8_t> padding;
423 for (int i=0; i < n_tensors; ++i) {
424 const char * name = gguf_get_tensor_name(gguf_in, i);
425 struct ggml_tensor * tensor = ggml_get_tensor(ctx_in, name);
426
427 // read tensor data
428 data.resize(ggml_nbytes(tensor));

Callers 1

mainFunction · 0.85

Calls 15

load_loraFunction · 0.85
fprintfFunction · 0.85
apply_loraFunction · 0.85
printfFunction · 0.85
free_loraFunction · 0.85
gguf_init_from_fileFunction · 0.50
gguf_init_emptyFunction · 0.50
gguf_set_kvFunction · 0.50
gguf_get_n_tensorsFunction · 0.50
gguf_get_tensor_nameFunction · 0.50
ggml_get_tensorFunction · 0.50
gguf_add_tensorFunction · 0.50

Tested by

no test coverage detected