Skip to content

fix(lora): add in F32 when the backend rejects mixed-type addition - #2383

Open
nitrobin wants to merge 2 commits into
leejet:masterfrom
nitrobin:fix/lora-diff-as-weight-type
Open

nitrobin wants to merge 2 commits into
leejet:masterfrom
nitrobin:fix/lora-diff-as-weight-type

Conversation

@nitrobin

@nitrobin nitrobin commented Nov 19, 2026 •

Copy link
Copy Markdown

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_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.

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.

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.

Checklist

  • I have read and confirmed this PR follows the contribution guidelines.
  • 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.

@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

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.

Changes: LoRA addition

Layer / File(s) Summary
Backend-compatible addition
src/model/adapter/lora.hpp
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.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: leejet

Merge Risk: Minimal · up to d3227

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: Passed 4

Check name Status Explanation
Title check Passed 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 Passed 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 Passed 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 Passed 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.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • 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.

Fixes leejet#2380

@leejet leejet left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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 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
@nitrobin

Copy link
Copy Markdown
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Metal: merging a LoRA into F16 weights aborts — ADD(F16, F32) can't run on MTL0

2 participants