You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(lora): add in F32 when the backend rejects mixed-type addition - #2383
Metal rejects the mixed F16/F32 add used to merge LoRA into F16 weights. The fallback now adds in F32 and writes the result back in F16, preserving small sums that disappear when the delta is rounded first.
Summary
LoraModel::build_lora_graph probes the mixed-type add with ggml_backend_supports_op. If the backend rejects it, the graph casts the weight to a temporary F32 tensor, adds the F32 delta in place, and uses ggml_cpy to write back to the original F16 weight. When the backend supports the mixed-type add, the graph uses it directly.
On Apple M4 Max (macOS 27.0.1), I ran both merge graphs on ggml Metal (eeda9ec) and read back the updated F16 weight. Metal rejected the direct F16/F32 add.
F16 weight + F32 delta
Previous fallback
Revised fallback
1.0 + (-0.9999)
0
0.000100016593933
The same-type F16 control 1.0 + 0.5 used a single add node and returned 1.5.
SDXL LoRA regression
Re-ran the original case with Illustrious-XL-v2.0 (F16 SDXL) and Detail Tweaker XL (Civitai model version 135867). The checkpoint and LoRA SHA-256 matched on both machines.
sd-cli -m Illustrious-XL-v2.0.safetensors -W 512 -H 512 --steps 1 --seed 42 --lora-apply-mode immediately --lora-model-dir <loras> -p "a lighthouse on a cliff, <lora:detail-tweaker-xl:1.0>"
The master reference was 532d2c1. The Mac used an Apple M4 Max and macOS 27.0.1. The Windows PC used a Ryzen 5 3600 and RTX 2060 SUPER (driver 591.74).
Configuration
Result
macOS Metal
Master reproduced the unsupported ADD abort on MTL0 (exit 134). This revision applied the LoRA and saved the image.
macOS CPU (-t 10)
PNG byte-identical to master.
Windows CPU
PNG byte-identical to master.
Windows CUDA, default placement and text encoder weights in VRAM
PNG byte-identical to master in both cases.
Windows Vulkan, default placement and text encoder weights in VRAM
PNG byte-identical to master in both cases.
For the VRAM runs, --params-backend te=<backend>,diffusion=cpu,vae=cpu placed the text encoder weights on the GPU. For each default backend configuration, a separate no-LoRA run produced different decoded pixels with the same prompt and seed.
clang-format on the changed lines and git diff --check passed.
If this PR adds or changes model support, I have added or updated its usage guide and relevant model index/README entries following the model documentation requirements. (Not applicable: no model support changes.)
Summary by CodeRabbit
Bug Fixes
Improved compatibility for models using LoRA adapters when tensor types differ or a backend does not support the required operation. The adapter update is now applied through a compatible conversion path in those cases, while supported operations continue to use the direct path.
No actionable comments were generated in the recent review.
Walkthrough
The LoRA graph now checks whether the backend supports the in-place addition. If it does not, the graph adds the delta to an F32 copy of the model tensor and copies the result back.
When the backend does not support the mixed-type in-place addition, build_lora_graph converts the model tensor to F32, adds the delta, and copies the result back.
The change lets immediate LoRA merging into F16 weights complete on backends that reject mixed-type addition, such as Metal. Other paths are unchanged. No concrete merge-blocking risk was found. Backends outside CPU, CUDA, Vulkan and Metal were not tested by the author.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title names the change: a fallback that adds in F32 when the backend rejects mixed-type addition, which matches the raw summary and PR objectives. It uses the conventional fix(lora) prefix and is …
Linked Issues check
Objective [#2380]: merging an F32 LoRA delta into F16 weights must not abort on Metal. The whole-PR diff against merge base 532d2c1 changes only src/model/adapter/lora.hpp. In the else branch of build…
Out of Scope Changes check
The whole-PR diff changes only src/model/adapter/lora.hpp, adding 6 lines in the F16/F32 add path of LoraModel::build_lora_graph. Each added line supports the #2380 objective: the type check, the supp…
Description check
The description is thorough and covers the template's required sections. The Summary explains the change: LoraModel::build_lora_graph probes the mixed-type add with ggml_backend_supports_op, and on re…
Full details: Title check
Explanation
The title names the change: a fallback that adds in F32 when the backend rejects mixed-type addition, which matches the raw summary and PR objectives. It uses the conventional fix(lora) prefix and is specific. Note that the PR description and objectives describe a cast-to-weight-type fallback in one place and an F32 promotion in another; the title matches the F32 promotion that the code summary describes.
Full details: Linked Issues check
Explanation
Objective [#2380]: merging an F32 LoRA delta into F16 weights must not abort on Metal. The whole-PR diff against merge base 532d2c1 changes only src/model/adapter/lora.hpp. In the else branch of build_lora_graph, the code keeps ggml_add_inplace(model_tensor, diff). When diff->type differs from model_tensor->type and ggml_backend_supports_op(runtime_backend, final_tensor) returns false, the code casts model_tensor to F32, adds diff in F32, and copies the result back with ggml_cpy. This removes the unsupported ADD(F16, F32) on backends that reject it, which #2380 identifies as the cause. Backends that support the mixed-type add, and matching-type weights, keep the original graph. The fallback follows the F32-promotion approach that #2380 lists as an alternative. The current code does not match the PR description, which says the fallback casts the diff to the weight type. The code is the authoritative evidence here. The Metal and other-backend results are reported in the PR description and were not verified in this review. No automated tests are requested by #2380, and no non-coding requirement applies to the code change.
Full details: Out of Scope Changes check
Explanation
The whole-PR diff changes only src/model/adapter/lora.hpp, adding 6 lines in the F16/F32 add path of LoraModel::build_lora_graph. Each added line supports the #2380 objective: the type check, the supports_op capability check, the F32 promotion, the add, and the copy back. No unrelated file or refactoring appears in the diff. The PR description's benchmark tables, test notes, and the stale description of a diff-cast fallback are not code changes. The description mismatch does not make any code change out of scope.
Full details: Description check
Explanation
The description is thorough and covers the template's required sections. The Summary explains the change: LoraModel::build_lora_graph probes the mixed-type add with ggml_backend_supports_op, and on rejection it promotes the weight to a temporary F32 tensor, adds the delta, and writes back with ggml_cpy. The Related Issue section links #2380 and a review comment. The Additional Information section gives Metal reproduction results, an SDXL LoRA regression matrix across macOS, Windows CPU, CUDA, and Vulkan with the reference commit, and the clang-format and git diff --check results. The checklist is filled in, and the model documentation item is marked not applicable. The description does not state which backends or platforms went untested (ROCm/HIP, SYCL, OpenCL, MUSA, Linux), which the objectives mention. This is a minor gap.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: 1d99c1cc-3645-4338-951b-3e924ff891d1
Commits
Reviewing files that changed from the base of the PR and between bb36ac4 and d3227e8.
Files selected for processing: 1
src/model/adapter/lora.hpp
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
…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.
Fixesleejet#2380
The reason will be displayed to describe this comment to others. Learn more.
Could we cast the weight to F32 here, do the add, and use ggml_cpy to write it back as F16? I tried 1.0 + (-0.9999) in a small ggml test, and casting the delta first gave zero even though the sum fits in F16. The backend check looks good to me.
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.
nitrobin
changed the title
fix(lora): cast the LoRA diff to the weight's type where the backend can't add F32 to F16
fix(lora): add in F32 when the backend rejects mixed-type addition
Nov 23, 2026
Updated in d3227e8. The backend check stays in place. The fallback casts the weight to F32, adds the delta in F32, and writes the result back to F16 with ggml_cpy. On Metal, 1.0 + (-0.9999) now returns 0.000100016593933. The original SDXL LoRA case passed in all seven configurations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR
Metal rejects the mixed F16/F32 add used to merge LoRA into F16 weights. The fallback now adds in F32 and writes the result back in F16, preserving small sums that disappear when the delta is rounded first.
Summary
LoraModel::build_lora_graphprobes the mixed-type add withggml_backend_supports_op. If the backend rejects it, the graph casts the weight to a temporary F32 tensor, adds the F32 delta in place, and usesggml_cpyto write back to the original F16 weight. When the backend supports the mixed-type add, the graph uses it directly.Related Issue / Discussion
Fixes #2380. Review requesting the F32 fallback.
Additional Information
On Apple M4 Max (macOS 27.0.1), I ran both merge graphs on ggml Metal (
eeda9ec) and read back the updated F16 weight. Metal rejected the direct F16/F32 add.1.0 + (-0.9999)00.000100016593933The same-type F16 control
1.0 + 0.5used a single add node and returned1.5.SDXL LoRA regression
Re-ran the original case with Illustrious-XL-v2.0 (F16 SDXL) and Detail Tweaker XL (Civitai model version 135867). The checkpoint and LoRA SHA-256 matched on both machines.
The master reference was
532d2c1. The Mac used an Apple M4 Max and macOS 27.0.1. The Windows PC used a Ryzen 5 3600 and RTX 2060 SUPER (driver 591.74).ADDabort onMTL0(exit 134). This revision applied the LoRA and saved the image.-t 10)For the VRAM runs,
--params-backend te=<backend>,diffusion=cpu,vae=cpuplaced the text encoder weights on the GPU. For each default backend configuration, a separate no-LoRA run produced different decoded pixels with the same prompt and seed.clang-formaton the changed lines andgit diff --checkpassed.Checklist
Summary by CodeRabbit