Repository navigation
Description
Git commit
master-1106-532d2c1 (current master), ggml submodule eeda9ec.
Operating System & Version
macOS 27.0.1, Apple M4 Max (64 GB)
GGML backends
Metal
Command-line arguments used
sd-cli -m Illustrious-XL-v2.0.safetensors --lora-model-dir loras \
-p "a lighthouse on a cliff, <lora:detail-tweaker-xl:1.0>" \
-W 512 -H 512 --steps 1 --seed 42
Steps to reproduce
- Build with
-DSD_METAL=ON. - Use any SDXL checkpoint with F16 weights. Tested with OnomaAIResearch/Illustrious-XL-v2.0.
- Add any SDXL LoRA. Tested with Detail Tweaker XL (civitai, v1.0).
- Run the command above.
--lora-apply-modeis left atauto, which picksimmediatelyhere.
What you expected to happen
The LoRA is merged into the weights and the image is generated.
What actually happened
The process aborts while the LoRA is merged into the text encoder:
[I] apply lora immediately --- diffusion_engine.cpp:1726
[I] apply_loras completed, taking 0.03s --- diffusion_engine.cpp:1875
[I] lora build cached graph cut plan done (taking 1 ms) --- ggml_graph_cut.cpp:1034
ggml/src/ggml-backend.cpp:1368: pre-allocated tensor (cond_stage_model.transformer.text_model.encoder.layers.0.mlp.fc1.weight (view)) in a buffer (MTL0) that cannot run the operation (ADD)
...
ggml_abort
ggml_backend_sched_backend_id_from_cur
ggml_backend_sched_split_graph
ggml_backend_sched_reserve_size
sd::ComputeWorkspace::measure
...
LoraModel::apply
ModelManager::apply_loras_to_params
...
sd_generate_image
Exit code 134 (SIGABRT).
Logs / error messages / stack trace
See above.
Additional context
Cause. In LoraModel (src/model/adapter/lora.hpp, around line 985), an F32 or F16 weight takes the else branch. That branch adds the LoRA delta (F32) to the weight in place:
if (model_tensor->type != GGML_TYPE_F32 && model_tensor->type != GGML_TYPE_F16) {
final_tensor = ggml_ext_cast_f32(&runner_ctx, model_tensor);
final_tensor = ggml_add_inplace(compute_ctx, final_tensor, diff);
final_tensor = ggml_cpy(compute_ctx, final_tensor, model_tensor);
} else {
final_tensor = ggml_add_inplace(compute_ctx, model_tensor, diff); // F16 += F32
}For an F16 weight this is ADD(F16, F32). The Metal backend supports ADD only when both sources have the same type, so ggml_backend_sched can't place the op on the backend that owns the weight's buffer, and it aborts. We have only seen it on Metal.
Fix. Cast the delta to the weight's type before the in-place add:
} else {
if (diff->type != model_tensor->type) {
diff = ggml_cast(compute_ctx, diff, model_tensor->type);
}
final_tensor = ggml_add_inplace(compute_ctx, model_tensor, diff);
}With this change the same command finishes (generate_image completed), and the LoRA changes the output. Branch: https://github.com/nitrobin/stable-diffusion.cpp/tree/fix/lora-diff-as-weight-type
The F32 weight case is unchanged (the delta is already F32). Rounding the delta to F16 before the add loses some precision compared with an F32 add. If that matters, the alternative is to always take the first branch (cast the weight to F32, add, copy back) for F16 too, at the cost of a temporary F32 copy.
I used AI tools to help draft this report.
Activity
wbruna commentedon Nov 18, 2026
Could you open a proper PR? Also, please apply the conversion on Metal only, to avoid the cost on backends that can handle it directly.
fix(lora): cast the LoRA diff to the weight's type where the backend …
fix(lora): cast the LoRA diff to the weight's type where the backend …
nitrobin commentedon Nov 19, 2026
Opened #2383 as a proper PR. The cast happens only when the backend can't run
ADD(F16, F32)(checked withggml_backend_supports_op), so in practice only on Metal; CUDA, Vulkan and CPU build the same graph as master. Test results are in the PR description.I used AI tools to help draft this comment.
fix(lora): cast the LoRA diff to the weight's type where the backend …