From 2a0fa74b0bfd5b3e667fcf67a8f595d23919f060 Mon Sep 17 00:00:00 2001 From: Jasper Blues Date: Thu, 26 Nov 2026 14:35:43 +1000 Subject: [PATCH 1/2] fix(lora): apply LoRAs at runtime for fp8 weights in auto mode --- src/dgl/models/lora.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/dgl/models/lora.cpp b/src/dgl/models/lora.cpp index 039244e35..29d942606 100644 --- a/src/dgl/models/lora.cpp +++ b/src/dgl/models/lora.cpp @@ -19,7 +19,9 @@ namespace sd::dgl::models { const auto& tensors = loader.get_tensor_storage_map(); const bool quantized = std::any_of(tensors.begin(), tensors.end(), [](const auto& entry) { const auto& tensor = entry.second; - return tensor.w4_convrot_kind || + // Merging into fp8 weights needs a CPY to fp8, which Vulkan cannot run. + const ggml_type type = tensor.expected_type < GGML_TYPE_COUNT ? tensor.expected_type : tensor.type; + return tensor.w4_convrot_kind || type == GGML_TYPE_F8_E4M3 || type == GGML_TYPE_F8_E5M2 || (!tensor.is_int8_tensorwise && (ggml_is_quantized(tensor.type) || (tensor.expected_type < GGML_TYPE_COUNT && ggml_is_quantized(tensor.expected_type)))); From bb5586b1a2fe10bce3eb7f570e5ea923205f64ac Mon Sep 17 00:00:00 2001 From: Jasper Blues Date: Sat, 28 Nov 2026 07:28:08 +1000 Subject: [PATCH 2/2] fix(lora): base the fp8 fallback on the weight type and the backend Keep the immediate merge for F8_E4M3 where the runtime backend can CPY F32 back to it, fall back to at_runtime where it can't (Vulkan), and always use at_runtime for F8_E5M2. Guard the FP8 checks with SD_USE_UPSTREAM_GGML, as the safetensors loader does. --- src/dgl/models/lora.cpp | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/dgl/models/lora.cpp b/src/dgl/models/lora.cpp index 29d942606..1592c647d 100644 --- a/src/dgl/models/lora.cpp +++ b/src/dgl/models/lora.cpp @@ -8,6 +8,24 @@ namespace sd::dgl::models { +#ifndef SD_USE_UPSTREAM_GGML + // Merging a LoRA into a weight that isn't F32 or F16 adds in F32 and writes the sum back with a CPY to the + // weight's type. Not every backend has that CPY for fp8 (Vulkan has none). + static bool supports_writeback(ggml_backend_t backend, ggml_type type) { + if (!backend) + return false; + ggml_init_params params = {ggml_tensor_overhead() * 4, nullptr, true}; + ggml_context* ctx = ggml_init(params); + if (!ctx) + return false; + ggml_tensor* sum = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 256); + ggml_tensor* weight = ggml_new_tensor_1d(ctx, type, 256); + const bool supported = ggml_backend_supports_op(backend, ggml_cpy(ctx, sum, weight)); + ggml_free(ctx); + return supported; + } +#endif + lora_apply_mode_t resolve_lora_mode(lora_apply_mode_t mode, const ModelLoader& loader, SDBackendManager& backends, SDBackendModule module, bool row_split) { if (mode != LORA_APPLY_AUTO) return mode; @@ -16,12 +34,20 @@ namespace sd::dgl::models { backends.params_backend(module) != backends.runtime_backend(module)) return LORA_APPLY_AT_RUNTIME; - const auto& tensors = loader.get_tensor_storage_map(); - const bool quantized = std::any_of(tensors.begin(), tensors.end(), [](const auto& entry) { + const auto& tensors = loader.get_tensor_storage_map(); +#ifndef SD_USE_UPSTREAM_GGML + const bool e4m3_writeback = supports_writeback(backends.runtime_backend(module), GGML_TYPE_F8_E4M3); +#endif + const bool quantized = std::any_of(tensors.begin(), tensors.end(), [&](const auto& entry) { const auto& tensor = entry.second; - // Merging into fp8 weights needs a CPY to fp8, which Vulkan cannot run. +#ifndef SD_USE_UPSTREAM_GGML + // fp8 weights merge only where the backend can write F32 back to them: E4M3 where it has that CPY, + // E5M2 never. (Upstream ggml loads fp8 as F16, so this doesn't arise there.) const ggml_type type = tensor.expected_type < GGML_TYPE_COUNT ? tensor.expected_type : tensor.type; - return tensor.w4_convrot_kind || type == GGML_TYPE_F8_E4M3 || type == GGML_TYPE_F8_E5M2 || + if (type == GGML_TYPE_F8_E5M2 || (type == GGML_TYPE_F8_E4M3 && !e4m3_writeback)) + return true; +#endif + return tensor.w4_convrot_kind || (!tensor.is_int8_tensorwise && (ggml_is_quantized(tensor.type) || (tensor.expected_type < GGML_TYPE_COUNT && ggml_is_quantized(tensor.expected_type))));