diff --git a/docs/guides/backend.md b/docs/guides/backend.md index 53397fc4b..1a0433ef6 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 35e32f2b0..140103080 100644 --- a/src/core/backend_fit.cpp +++ b/src/core/backend_fit.cpp @@ -5,9 +5,9 @@ #include #include #include +#include #include #include -#include #include #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(device.free_bytes + gib * 1024.0 * MiB, 0); + device.budget_bytes = (int64_t)std::max(available_bytes + gib * 1024.0 * MiB, 0); } else { - device.budget_bytes = std::max(device.free_bytes - 512 * MiB, 0); + device.budget_bytes = std::max(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 physical_budgets(const sd::ggml_graph_cut::MaxVramAssignment& budgets, const AvailableVRAM& available_vram) { - auto devices = enumerate_gpu_devices(budgets, available_vram); - std::map 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 devices; + std::map 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 gpu_device_budgets(const sd::ggml_graph_cut::MaxVramAssignment& budgets, const AvailableVRAM& available_vram) { std::vector result; - std::set 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(std::max(device.budget_bytes, 0))}); - } + for (auto& device : physical_budgets(budgets, available_vram)) + result.push_back({device.device, std::move(device.name), static_cast(std::max(device.budget_bytes, 0))}); return result; } diff --git a/src/core/backend_fit.h b/src/core/backend_fit.h index 4ce405cf2..40d9b093a 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 gpu_device_budgets(const sd::ggml_graph_cut::MaxVramAssignment& budgets, const AvailableVRAM& available_vram = {}); bool prepare_vae_decode_retry_tiling(sd::TilingConfig& tiling_params,