Skip to content

Commit 0ec2b58

Browse files
committed
reject merging subset
1 parent 1f96573 commit 0ec2b58

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

examples/export-lora/export-lora.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,19 @@ struct lora_merge_ctx {
195195
// output is forced to f16 for now
196196
gguf_set_val_u32(ctx_out, "general.file_type", LLAMA_FTYPE_MOSTLY_F16);
197197

198+
// check if all lora adapters have the same tensors
199+
// TODO: remove this when we can support merging subset of adapters. Ref: https://github.com/ggerganov/llama.cpp/pull/8607#discussion_r1686027777
200+
if (adapters.size() > 1) {
201+
auto & base_adapter = adapters[0];
202+
for (size_t i = 1; i < adapters.size(); ++i) {
203+
for (auto & it : base_adapter->tensors) {
204+
if (base_adapter->get_tensor(it.first) == nullptr) {
205+
throw std::runtime_error("Input adapters do not have the same list of tensors. This is not yet supported. Please merge the adapter one-by-one instead of merging all at once.");
206+
}
207+
}
208+
}
209+
}
210+
198211
// if true, this tensor can be lora-merged. if false, we skip merging and just copy data to outfile
199212
std::vector<std::pair<struct ggml_tensor *, bool>> base_tensors;
200213
for (auto & it : base_model.tensors) {
@@ -319,8 +332,8 @@ struct lora_merge_ctx {
319332
const float scale = alpha ? adapters[i]->scale * alpha / rank : adapters[i]->scale;
320333
delta = ggml_scale(ctx0, delta, scale);
321334
cur = ggml_add(ctx0, cur, delta);
322-
printf("%s : merging from adapter[%ld]\n", __func__, i);
323-
printf("%s : input_scale=%f calculated_scale=%f rank=%d\n", __func__, adapters[i]->scale, scale, (int) inp_b[i]->ne[0]);
335+
printf("%s : + merging from adapter[%ld]\n", __func__, i);
336+
printf("%s : input_scale=%f calculated_scale=%f rank=%d\n", __func__, adapters[i]->scale, scale, (int) inp_b[i]->ne[0]);
324337
}
325338
cur = ggml_cast(ctx0, cur, get_out_tensor_type(base));
326339
ggml_build_forward_expand(gf, cur);

0 commit comments

Comments
 (0)