From bb36ac493c87f1d544e74cc084bf7a895cb5dcf9 Mon Sep 17 00:00:00 2001 From: Eugene Veretennikov Date: Wed, 18 Nov 2026 23:38:02 +0300 Subject: [PATCH 1/2] fix(lora): cast the LoRA diff to the weight's type where the backend can't add F32 to F16 Merging a LoRA into F16 weights on Metal aborts: the in-place ADD(F16, F32) can't run on MTL0, because Metal adds only tensors of the same type. When the backend doesn't support that ADD, cast the diff to the weight's type first. Backends that support it keep the F32 diff. Fixes #2380 --- src/model/adapter/lora.hpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/model/adapter/lora.hpp b/src/model/adapter/lora.hpp index 7d71b34f60..c30703a8fc 100644 --- a/src/model/adapter/lora.hpp +++ b/src/model/adapter/lora.hpp @@ -988,6 +988,13 @@ struct LoraModel : public GGMLRunner { final_tensor = ggml_cpy(compute_ctx, final_tensor, model_tensor); } else { final_tensor = ggml_add_inplace(compute_ctx, model_tensor, diff); + // Some backends (e.g. Metal) can't add an F32 diff to an F16 weight: + // cast the diff to the weight's type there. supports_op() asks about a built op, + // so the add is built first and rebuilt with the cast diff if it is refused. + if (diff->type != model_tensor->type && !ggml_backend_supports_op(runtime_backend, final_tensor)) { + diff = ggml_cast(compute_ctx, diff, model_tensor->type); + final_tensor = ggml_add_inplace(compute_ctx, model_tensor, diff); + } } ggml_build_forward_expand(gf, final_tensor); if (!sd_backend_is_cpu(runtime_backend) && ggml_backend_buffer_is_host(original_tensor->buffer)) { From d3227e83e475abb40b052cd55cb0aa37d5473f15 Mon Sep 17 00:00:00 2001 From: Eugene Veretennikov Date: Mon, 23 Nov 2026 23:04:33 +0300 Subject: [PATCH 2/2] fix(lora): add in F32 when the backend rejects mixed-type addition Rounding the F32 delta before adding can erase a representable F16 result. When the backend rejects the mixed-type add, promote the weight to F32 and copy the sum back to the original weight. --- src/model/adapter/lora.hpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/model/adapter/lora.hpp b/src/model/adapter/lora.hpp index c30703a8fc..b9c93461b4 100644 --- a/src/model/adapter/lora.hpp +++ b/src/model/adapter/lora.hpp @@ -988,12 +988,11 @@ struct LoraModel : public GGMLRunner { final_tensor = ggml_cpy(compute_ctx, final_tensor, model_tensor); } else { final_tensor = ggml_add_inplace(compute_ctx, model_tensor, diff); - // Some backends (e.g. Metal) can't add an F32 diff to an F16 weight: - // cast the diff to the weight's type there. supports_op() asks about a built op, - // so the add is built first and rebuilt with the cast diff if it is refused. if (diff->type != model_tensor->type && !ggml_backend_supports_op(runtime_backend, final_tensor)) { - diff = ggml_cast(compute_ctx, diff, model_tensor->type); - final_tensor = ggml_add_inplace(compute_ctx, model_tensor, diff); + // Rounding the delta before adding can erase a representable result. + final_tensor = ggml_cast(compute_ctx, model_tensor, GGML_TYPE_F32); + final_tensor = ggml_add_inplace(compute_ctx, final_tensor, diff); + final_tensor = ggml_cpy(compute_ctx, final_tensor, model_tensor); } } ggml_build_forward_expand(gf, final_tensor);