Commit 8bd8e0c for stable-diffusion.cpp

commit 8bd8e0c68d4eba72366e5e22bcd7c4ad20844396
Author: leejet <leejet714@gmail.com>
Date:   Wed Dec 2 00:31:29 2026 +0800

    fix: prefer CUDA for GPUs shared with Vulkan in auto-fit (#2478)

diff --git a/docs/guides/backend.md b/docs/guides/backend.md
index 53397fc..1a0433e 100644
--- a/docs/guides/backend.md
+++ b/docs/guides/backend.md
@@ -194,7 +194,7 @@ other than `diffusion` move to another GPU, starting from the lowest priority in
 budget holds all of its weights and compute reserve, and it then runs there and
 keeps its weights there. Components with an explicit `--backend` assignment are
 not moved. A GPU exposed by several backends, such as CUDA and Vulkan, counts
-once.
+once. Auto-fit prefers CUDA when both CUDA and Vulkan expose the same GPU.

 Hires model upscaling shares the runtime budget and can reclaim idle weight
 copies from other components. Weights used by active computation or pending
diff --git a/src/core/backend_fit.cpp b/src/core/backend_fit.cpp
index 35e32f2..1401030 100644
--- a/src/core/backend_fit.cpp
+++ b/src/core/backend_fit.cpp
@@ -5,9 +5,9 @@
 #include <cctype>
 #include <cstddef>
 #include <cstdint>
+#include <cstring>
 #include <fstream>
 #include <map>
-#include <set>
 #include <vector>

 #if defined(_WIN32)
@@ -30,12 +30,15 @@ namespace sd::backend_fit {
     struct Device {
         ggml_backend_dev_t device = nullptr;
         std::string name;
-        std::string description;
-        int64_t free_bytes              = 0;
         int64_t budget_bytes            = 0;
         enum ggml_backend_dev_type type = GGML_BACKEND_DEVICE_TYPE_GPU;
     };

+    static bool uses_backend(ggml_backend_dev_t device, const char* name) {
+        const auto reg = ggml_backend_dev_backend_reg(device);
+        return reg != nullptr && std::strcmp(ggml_backend_reg_name(reg), name) == 0;
+    }
+
     static std::string budget_key(std::string name) {
         std::transform(name.begin(), name.end(), name.begin(), [](unsigned char c) { return (char)std::tolower(c); });
         return name;
@@ -51,15 +54,14 @@ namespace sd::backend_fit {
                 continue;
             }
             Device device;
-            device.device      = dev;
-            device.name        = ggml_backend_dev_name(dev);
-            device.description = ggml_backend_dev_description(dev);
-            device.type        = type;
+            device.device     = dev;
+            device.name       = ggml_backend_dev_name(dev);
+            device.type       = type;
             size_t free_bytes = 0, total_bytes = 0;
             ggml_backend_dev_memory(dev, &free_bytes, &total_bytes);
             if (available_vram)
                 free_bytes = available_vram(dev, free_bytes, total_bytes);
-            device.free_bytes = (int64_t)free_bytes;
+            const auto available_bytes = (int64_t)free_bytes;

             float gib = budgets.default_gib;
             auto it   = budgets.backend_gib.find(budget_key(device.name));
@@ -67,11 +69,11 @@ namespace sd::backend_fit {
                 gib = it->second;
             }
             if (gib > 0.f) {
-                device.budget_bytes = (int64_t)std::min(gib * 1024.0 * MiB, (double)device.free_bytes);
+                device.budget_bytes = (int64_t)std::min(gib * 1024.0 * MiB, (double)available_bytes);
             } else if (gib < 0.f) {
-                device.budget_bytes = (int64_t)std::max<double>(device.free_bytes + gib * 1024.0 * MiB, 0);
+                device.budget_bytes = (int64_t)std::max<double>(available_bytes + gib * 1024.0 * MiB, 0);
             } else {
-                device.budget_bytes = std::max<int64_t>(device.free_bytes - 512 * MiB, 0);
+                device.budget_bytes = std::max<int64_t>(available_bytes - 512 * MiB, 0);
             }
             out.push_back(std::move(device));
         }
@@ -140,17 +142,24 @@ namespace sd::backend_fit {
         return SIZE_MAX;
     }

-    // Backends of one card share its memory, so the tightest --max-vram cap applies to all of them.
     static std::vector<Device> physical_budgets(const sd::ggml_graph_cut::MaxVramAssignment& budgets, const AvailableVRAM& available_vram) {
-        auto devices = enumerate_gpu_devices(budgets, available_vram);
-        std::map<std::string, int64_t> tightest;
-        for (const auto& device : devices) {
-            const auto added = tightest.emplace(physical_device_key(device.device), device.budget_bytes);
-            if (!added.second)
-                added.first->second = std::min(added.first->second, device.budget_bytes);
+        std::vector<Device> devices;
+        std::map<std::string, size_t> indices;
+        for (auto& device : enumerate_gpu_devices(budgets, available_vram)) {
+            const auto [it, inserted] = indices.emplace(physical_device_key(device.device), devices.size());
+            if (inserted) {
+                devices.push_back(std::move(device));
+                continue;
+            }
+            auto& current = devices[it->second];
+            // Backends of one card share its memory, including any --max-vram cap.
+            const auto budget = std::min(current.budget_bytes, device.budget_bytes);
+            if (device.type != current.type ? device.type == GGML_BACKEND_DEVICE_TYPE_GPU
+                                            : uses_backend(device.device, "CUDA") && !uses_backend(current.device, "CUDA")) {
+                current = std::move(device);
+            }
+            current.budget_bytes = budget;
         }
-        for (auto& device : devices)
-            device.budget_bytes = tightest.at(physical_device_key(device.device));
         return devices;
     }

@@ -161,22 +170,42 @@ namespace sd::backend_fit {
         return selected == SIZE_MAX ? fallback : devices[selected].name;
     }

+    static ggml_backend_dev_t find_cuda_alias(ggml_backend_dev_t device, const char* description) {
+        ggml_backend_dev_t cuda = nullptr;
+        for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
+            const auto other = ggml_backend_dev_get(i);
+            if (other == device)
+                continue;
+            const bool is_cuda = uses_backend(other, "CUDA");
+            if ((!is_cuda && !uses_backend(other, "Vulkan")) ||
+                std::strcmp(ggml_backend_dev_description(other), description) != 0)
+                continue;
+            if (!is_cuda || cuda != nullptr)
+                return nullptr;
+            cuda = other;
+        }
+        return cuda;
+    }
+
     std::string physical_device_key(ggml_backend_dev_t device) {
         if (device == nullptr)
             return {};
         ggml_backend_dev_props props{};
         ggml_backend_dev_get_props(device, &props);
-        return props.device_id != nullptr && props.device_id[0] ? std::string("pci:") + props.device_id
-                                                                : std::string("dev:") + ggml_backend_dev_name(device);
+        if (props.device_id != nullptr && props.device_id[0])
+            return std::string("pci:") + props.device_id;
+        // Vulkan can lack a PCI ID. Only use the model name when it is unique in both backends.
+        if (props.description != nullptr && props.description[0] && uses_backend(device, "Vulkan")) {
+            if (const auto cuda = find_cuda_alias(device, props.description))
+                return physical_device_key(cuda);
+        }
+        return std::string("dev:") + ggml_backend_dev_name(device);
     }

     std::vector<DeviceBudget> gpu_device_budgets(const sd::ggml_graph_cut::MaxVramAssignment& budgets, const AvailableVRAM& available_vram) {
         std::vector<DeviceBudget> result;
-        std::set<std::string> seen;
-        for (const auto& device : physical_budgets(budgets, available_vram)) {
-            if (seen.insert(physical_device_key(device.device)).second)
-                result.push_back({device.device, device.name, static_cast<size_t>(std::max<int64_t>(device.budget_bytes, 0))});
-        }
+        for (auto& device : physical_budgets(budgets, available_vram))
+            result.push_back({device.device, std::move(device.name), static_cast<size_t>(std::max<int64_t>(device.budget_bytes, 0))});
         return result;
     }

diff --git a/src/core/backend_fit.h b/src/core/backend_fit.h
index 4ce405c..40d9b09 100644
--- a/src/core/backend_fit.h
+++ b/src/core/backend_fit.h
@@ -31,7 +31,7 @@ namespace sd::backend_fit {
     std::string physical_device_key(ggml_backend_dev_t device);

     // Weight and compute budget of each physical GPU after --max-vram and reclaimable weights.
-    // A card exposed by several backends is listed once, under its first device.
+    // A card exposed by several backends is listed once, preferring CUDA when available.
     std::vector<DeviceBudget> gpu_device_budgets(const sd::ggml_graph_cut::MaxVramAssignment& budgets, const AvailableVRAM& available_vram = {});

     bool prepare_vae_decode_retry_tiling(sd::TilingConfig& tiling_params,