Skip to content

Commit 0e16188

Browse files
committed
add metadata check
1 parent 41ced24 commit 0e16188

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/llama.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ enum llm_kv {
371371
LLM_KV_TOKENIZER_SUFFIX_ID,
372372
LLM_KV_TOKENIZER_MIDDLE_ID,
373373
LLM_KV_TOKENIZER_EOT_ID,
374+
375+
LLM_KV_TRAINING_TYPE,
374376
};
375377

376378
static const std::map<llm_kv, const char *> LLM_KV_NAMES = {
@@ -464,6 +466,8 @@ static const std::map<llm_kv, const char *> LLM_KV_NAMES = {
464466
{ LLM_KV_TOKENIZER_SUFFIX_ID, "tokenizer.ggml.suffix_token_id" },
465467
{ LLM_KV_TOKENIZER_MIDDLE_ID, "tokenizer.ggml.middle_token_id" },
466468
{ LLM_KV_TOKENIZER_EOT_ID, "tokenizer.ggml.eot_token_id" },
469+
470+
{ LLM_KV_TRAINING_TYPE, "training.type" },
467471
};
468472

469473
struct LLM_KV {
@@ -18519,8 +18523,6 @@ static void llama_lora_adapter_init_internal(struct llama_model * model, const c
1851918523
static const int n_out_tensors = 5; // see llama_model
1852018524
LLAMA_LOG_INFO("%s: applying lora adapter from '%s' - please wait ...\n", __func__, path_lora);
1852118525

18522-
// TODO: check lora base model arch
18523-
1852418526
ggml_context * ctx = nullptr;
1852518527
struct gguf_init_params meta_gguf_params = {
1852618528
/* .no_alloc = */ false,
@@ -18532,6 +18534,25 @@ static void llama_lora_adapter_init_internal(struct llama_model * model, const c
1853218534
throw std::exception();
1853318535
}
1853418536

18537+
// check metadata
18538+
{
18539+
auto get_kv_str = [&](std::string key) -> std::string {
18540+
std::vector<char> str_buf(32, 0); // we only get the arch, so no need big buffer here
18541+
int id = gguf_find_key(ctx_gguf, key.c_str());
18542+
return id < 0 ? "" : std::string(gguf_get_val_str(ctx_gguf, id));
18543+
};
18544+
LLM_KV llm_kv = LLM_KV(LLM_ARCH_UNKNOWN);
18545+
auto lora_arch_name = get_kv_str(llm_kv(LLM_KV_GENERAL_ARCHITECTURE));
18546+
auto lora_arch = llm_arch_from_string(lora_arch_name);
18547+
if (lora_arch != model->arch) {
18548+
throw std::runtime_error("model arch and LoRA arch mismatch");
18549+
}
18550+
auto train_type = get_kv_str(llm_kv(LLM_KV_TRAINING_TYPE));
18551+
if (train_type != "finetune_lora") {
18552+
throw std::runtime_error("expect training.type to be finetune_lora, but got: " + train_type);
18553+
}
18554+
}
18555+
1853518556
// calculate n_tensors_per_layer
1853618557
int n_tensors_per_layer = 0;
1853718558
{
@@ -18542,7 +18563,6 @@ static void llama_lora_adapter_init_internal(struct llama_model * model, const c
1854218563
if (il == 0) n_tensors_per_layer++;
1854318564
}
1854418565
}
18545-
// printf("n_tensors_per_layer %d\n", n_tensors_per_layer);
1854618566

1854718567
// count layer buffer types
1854818568
std::map<ggml_backend_buffer_type_t, int> buft_tensor_count;

0 commit comments

Comments
 (0)