mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-11-28 19:16:33 +00:00
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.
This commit is contained in:
parent
bb36ac493c
commit
d3227e83e4
1 changed files with 4 additions and 5 deletions
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue