diff --git a/examples/common/common.cpp b/examples/common/common.cpp index 059b4a33..45965855 100644 --- a/examples/common/common.cpp +++ b/examples/common/common.cpp @@ -583,6 +583,13 @@ ArgOptions SDContextParams::get_options() { "Accepts a single mode or per-module assignments, e.g. row or diffusion=row,te=layer", (int)',', &split_mode}, + {"", + "--multi-gpu", + "how --auto-fit uses several discrete GPUs of the same backend: auto (default; the extra GPUs are used only when the weights do not fit on the best one, " + "first for the text encoder and the VAE, then by splitting the diffusion model), on (also moves the text encoder and the VAE away from the diffusion GPU " + "when everything would fit on one) or off (one GPU). Modules given in --backend are never moved or split", + 0, + &multi_gpu}, {"", "--rpc-servers", "comma-separated list of RPC servers to connect to for offloading, in the format host:port, e.g. localhost:50052,192.168.1.3:50052", @@ -767,7 +774,8 @@ ArgOptions SDContextParams::get_options() { }}, {"", "--auto-fit", - "on|off (default: on). Preserve --backend (otherwise select one GPU) and place weights on the compute GPU, " + "on|off (default: on). Preserve --backend (otherwise select the GPU with the most memory, or several, see --multi-gpu) " + "and place weights on the compute GPU, " "RAM, another GPU, or disk in that order, according to available memory (--max-vram limits GPU budgets). " "With no GPU, each component stays in RAM if it fits in the available RAM and is read from disk otherwise. " "Disabled by explicit --params-backend; uses automatic graph segmentation when needed", @@ -910,6 +918,15 @@ bool SDContextParams::validate(SDMode mode, bool require_model) { return false; } + if (!multi_gpu.empty()) { + std::string mode = multi_gpu; + std::transform(mode.begin(), mode.end(), mode.begin(), [](unsigned char c) { return static_cast(std::tolower(c)); }); + if (mode != "auto" && mode != "on" && mode != "off") { + LOG_ERROR("error: --multi-gpu must be one of auto, on, off (got '%s')", multi_gpu.c_str()); + return false; + } + } + return true; } @@ -999,6 +1016,7 @@ std::string SDContextParams::to_string() const { << " backend: \"" << backend << "\",\n" << " params_backend: \"" << params_backend << "\",\n" << " split_mode: \"" << split_mode << "\",\n" + << " multi_gpu: \"" << multi_gpu << "\",\n" << " model_args: \"" << model_args << "\",\n" << " auto_fit: " << (auto_fit ? "true" : "false") << ",\n" << " enable_mmap: " << (enable_mmap ? "true" : "false") << ",\n" @@ -1084,6 +1102,7 @@ sd_ctx_params_t SDContextParams::to_sd_ctx_params_t(bool taesd_preview) { sd_ctx_params.backend = effective_backend.c_str(); sd_ctx_params.params_backend = effective_params_backend.c_str(); sd_ctx_params.split_mode = split_mode.c_str(); + sd_ctx_params.multi_gpu = multi_gpu.c_str(); sd_ctx_params.auto_fit = auto_fit; sd_ctx_params.rpc_servers = rpc_servers.c_str(); sd_ctx_params.model_args = model_args.empty() ? nullptr : model_args.c_str(); diff --git a/examples/common/common.h b/examples/common/common.h index 82979538..f6a565a2 100644 --- a/examples/common/common.h +++ b/examples/common/common.h @@ -167,6 +167,7 @@ struct SDContextParams { std::string backend; std::string params_backend; std::string split_mode; + std::string multi_gpu; std::string model_args; bool auto_fit = true; std::string rpc_servers; diff --git a/include/stable-diffusion.h b/include/stable-diffusion.h index 43261fdf..68c5de3f 100644 --- a/include/stable-diffusion.h +++ b/include/stable-diffusion.h @@ -253,6 +253,7 @@ typedef struct { int conditioning_cache_size; // Maximum cached conditioning entries per context; 0 disables caching (default: 4) int memory_guard; // Memory usage percent (device memory, or system RAM on CPU backends) that triggers pressure reduction (weight eviction, prefetch pause, segmented CPU graphs for weights read from disk); 0 disables (default). Auto-fit uses 90 for a CPU-only plan that reads weights from disk bool disable_backend_fallback; // Fail context creation instead of falling back to the CPU backend when the requested backend is unavailable + const char* multi_gpu; // How auto-fit uses several discrete GPUs of one backend: "auto" (default; extra GPUs only when the weights do not fit on the best one), "on" (also move the text encoder and the VAE away from the diffusion GPU) or "off" } sd_ctx_params_t; typedef struct { diff --git a/src/core/backend_fit.cpp b/src/core/backend_fit.cpp index bf4f38e1..df982a89 100644 --- a/src/core/backend_fit.cpp +++ b/src/core/backend_fit.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -42,6 +43,8 @@ namespace sd::backend_fit { struct Device { std::string name; std::string description; + std::string registry; + bool discrete = false; int64_t free_bytes = 0; int64_t budget_bytes = 0; }; @@ -56,11 +59,13 @@ namespace sd::backend_fit { struct Decision { ParamsLocation params_location = ParamsLocation::DISK; size_t params_device = SIZE_MAX; + std::vector shares; }; struct Runtime { std::string name; std::vector devices; + bool estimate_shares = false; }; struct Plan { @@ -140,6 +145,10 @@ namespace sd::backend_fit { Device device; device.name = ggml_backend_dev_name(dev); device.description = ggml_backend_dev_description(dev); + device.discrete = type == GGML_BACKEND_DEVICE_TYPE_GPU; + if (ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(dev)) { + device.registry = ggml_backend_reg_name(reg); + } size_t free_bytes = 0, total_bytes = 0; ggml_backend_dev_memory(dev, &free_bytes, &total_bytes); device.free_bytes = (int64_t)free_bytes; @@ -203,6 +212,32 @@ namespace sd::backend_fit { return main_device; } + static constexpr int64_t SPLIT_BOUNDARY_MARGIN = 256 * MiB; + static constexpr int64_t SPLIT_SLACK_PERCENT = 10; + + // The layer split fills the devices in order, each up to its capacity, so the first one takes as much as it can. + // The slack and the margin per boundary cover blocks that cannot be cut where the capacity ends. + static bool estimate_split_shares(int64_t params_bytes, + const std::vector& capacities, + std::vector& shares) { + int64_t total = 0; + for (int64_t capacity : capacities) { + total += capacity; + } + const int64_t boundaries = std::max((int64_t)capacities.size() - 1, 0); + const int64_t required = params_bytes + params_bytes * SPLIT_SLACK_PERCENT / 100 + boundaries * SPLIT_BOUNDARY_MARGIN; + shares.assign(capacities.size(), 0); + if (total < required) { + return false; + } + int64_t left = params_bytes; + for (size_t i = 0; i < capacities.size() && left > 0; ++i) { + shares[i] = std::min(left, capacities[i]); + left -= shares[i]; + } + return true; + } + static Plan compute_plan(const std::vector& components, const std::vector& devices, int64_t ram_budget_bytes, @@ -261,17 +296,28 @@ namespace sd::backend_fit { } const auto& runtime_devices = plan.runtimes[ci].devices; - const bool fits_runtime = !runtime_devices.empty() && - std::all_of(runtime_devices.begin(), runtime_devices.end(), [&](size_t di) { - const int64_t headroom = headroom_for(ci, di); - return headroom <= remaining[di] && comp.params_bytes <= remaining[di] - headroom; - }); + std::vector shares; + bool fits_runtime = false; + if (plan.runtimes[ci].estimate_shares) { + std::vector capacities; + for (size_t di : runtime_devices) { + capacities.push_back(std::max(remaining[di] - headroom_for(ci, di), 0)); + } + fits_runtime = !runtime_devices.empty() && estimate_split_shares(comp.params_bytes, capacities, shares); + } else { + fits_runtime = !runtime_devices.empty() && + std::all_of(runtime_devices.begin(), runtime_devices.end(), [&](size_t di) { + const int64_t headroom = headroom_for(ci, di); + return headroom <= remaining[di] && comp.params_bytes <= remaining[di] - headroom; + }); + } if (fits_runtime) { decision.params_location = ParamsLocation::MAIN_GPU; decision.params_device = runtime_devices.front(); + decision.shares = shares; // Exact split allocations are unavailable until the runners build their plans. - for (size_t di : runtime_devices) { - remaining[di] -= comp.params_bytes; + for (size_t i = 0; i < runtime_devices.size(); ++i) { + remaining[runtime_devices[i]] -= shares.empty() ? comp.params_bytes : shares[i]; } continue; } @@ -315,6 +361,12 @@ namespace sd::backend_fit { return "disk"; } + static std::string format_gib(int64_t bytes) { + char text[32]; + snprintf(text, sizeof(text), "%.1f GiB", (double)bytes / (1024.0 * MiB)); + return text; + } + static void print_plan(const Plan& plan, const std::vector& components, const std::vector& devices, @@ -346,6 +398,14 @@ namespace sd::backend_fit { LOG_INFO(" %-12s params %6lld MiB, compute reserve %5lld MiB -> compute %s, params %s", comp.name, (long long)(comp.params_bytes / MiB), (long long)(comp.reserve_bytes / MiB), plan.runtimes[ci].name.c_str(), params.c_str()); + const auto& shares = plan.decisions[ci].shares; + if (shares.size() > 1) { + std::string split; + for (size_t i = 0; i < shares.size(); ++i) { + split += (i == 0 ? "" : " + ") + format_gib(shares[i]) + " on " + devices[plan.runtimes[ci].devices[i]].name; + } + LOG_INFO(" %-12s split: %s", "", split.c_str()); + } } } @@ -370,10 +430,252 @@ namespace sd::backend_fit { return ""; } + static std::string lowercase(std::string text) { + std::transform(text.begin(), text.end(), text.begin(), [](unsigned char c) { return (char)std::tolower(c); }); + return text; + } + + bool parse_multi_gpu_mode(const std::string& text, MultiGpuMode& mode) { + const std::string value = lowercase(trim(text)); + if (value.empty() || value == "auto") { + mode = MultiGpuMode::AUTO; + } else if (value == "on") { + mode = MultiGpuMode::ON; + } else if (value == "off") { + mode = MultiGpuMode::OFF; + } else { + return false; + } + return true; + } + + static void set_assignment(std::string& spec, const char* key, const std::string& value) { + const std::string prefix = std::string(key) + "="; + std::string out; + bool replaced = false; + for (const std::string& part : split_string(spec, ',')) { + std::string token = trim(part); + if (token.empty()) { + continue; + } + if (!replaced && token.compare(0, prefix.size(), prefix) == 0) { + token = prefix + value; + replaced = true; + } + out += out.empty() ? "" : ","; + out += token; + } + if (!replaced) { + out += out.empty() ? "" : ","; + out += prefix + value; + } + spec = out; + } + + static bool splittable(ComponentKind kind) { + return kind == ComponentKind::DIT || kind == ComponentKind::CONDITIONER; + } + + // A module whose --split-mode is row is not split automatically: row split keeps its own weight distribution. + static std::vector split_allowed(const std::vector& components, const std::string& split_mode_spec) { + std::vector allowed(components.size(), true); + SDBackendAssignment assignment; + std::string error; + if (split_mode_spec.empty() || !sd_parse_backend_assignment(split_mode_spec, &assignment, &error)) { + return allowed; + } + const SDBackendModule modules[] = {SDBackendModule::DIFFUSION, SDBackendModule::TE, SDBackendModule::VAE}; + for (size_t ci = 0; ci < components.size(); ++ci) { + if (lowercase(trim(assignment.get(modules[int(components[ci].kind)]))) == "row") { + allowed[ci] = false; + } + } + return allowed; + } + + // The discrete GPUs of the backend that has the GPU with the most memory, largest budget first. Integrated + // GPUs share the system RAM and GPUs of another backend would need their own runtime, so neither is included. + static std::vector multi_gpu_group(const std::vector& devices) { + auto usable = [&](size_t di) { return devices[di].discrete && devices[di].budget_bytes > 0; }; + size_t best = SIZE_MAX; + for (size_t di = 0; di < devices.size(); ++di) { + if (usable(di) && (best == SIZE_MAX || devices[di].budget_bytes > devices[best].budget_bytes)) { + best = di; + } + } + std::vector group; + for (size_t di = 0; best != SIZE_MAX && di < devices.size(); ++di) { + if (usable(di) && devices[di].registry == devices[best].registry) { + group.push_back(di); + } + } + std::stable_sort(group.begin(), group.end(), [&](size_t a, size_t b) { + return devices[a].budget_bytes > devices[b].budget_bytes; + }); + return group; + } + + static std::string join_device_names(const std::vector& devices, const std::vector& indices, const char* separator) { + std::string out; + for (size_t di : indices) { + out += out.empty() ? "" : separator; + out += devices[di].name; + } + return out; + } + + // Places the modules the user did not assign, in priority order, where their weights stay resident: a module + // keeps the device it has unless it does not fit there next to the modules placed before it, and then it moves + // to the first GPU of the group that can hold it. The diffusion model and the text encoder are split across GPUs + // when no single one can hold them. With --multi-gpu on, the text encoder and the VAE also move away from the + // diffusion GPU when they would fit. Returns true when a runtime changed. + static bool plan_multi_gpu_runtimes(const std::vector& components, + const std::vector& devices, + int64_t ram_budget_bytes, + MultiGpuMode mode, + const std::vector& explicit_runtime, + const std::vector& allow_split, + std::vector& runtimes, + std::vector& notes) { + bool has_free_module = false; + for (size_t ci = 0; ci < components.size(); ++ci) { + has_free_module = has_free_module || (!explicit_runtime[ci] && components[ci].params_bytes > 0); + } + if (mode == MultiGpuMode::OFF || !has_free_module) { + return false; + } + + const std::vector group = multi_gpu_group(devices); + if (group.size() < 2) { + size_t discrete = 0; + for (const Device& device : devices) { + discrete += device.discrete && device.budget_bytes > 0 ? 1 : 0; + } + if (discrete >= 2) { + notes.push_back( + "several GPUs were found but they belong to different backends; only the best one is used " + "unless modules are assigned with --backend"); + } + return false; + } + + std::vector order(components.size()); + for (size_t ci = 0; ci < components.size(); ++ci) { + order[ci] = ci; + } + std::stable_sort(order.begin(), order.end(), [&](size_t a, size_t b) { + return components[a].kind < components[b].kind; + }); + + auto single = [&](size_t di) { return Runtime{devices[di].name, {di}}; }; + auto plan_now = [&]() { return compute_plan(components, devices, ram_budget_bytes, runtimes); }; + auto resident = [](const Plan& plan, size_t ci) { return plan.decisions[ci].params_location == ParamsLocation::MAIN_GPU; }; + + const std::vector original = runtimes; + std::vector planned_notes; + for (size_t ci : order) { + const Component& comp = components[ci]; + if (explicit_runtime[ci] || comp.params_bytes == 0) { + continue; + } + + const Runtime previous = runtimes[ci]; + const bool fits_before = resident(plan_now(), ci); + + std::vector candidates = {previous}; + for (size_t di : group) { + if (devices[di].name != previous.name) { + candidates.push_back(single(di)); + } + } + if (mode == MultiGpuMode::ON && comp.kind != ComponentKind::DIT) { + std::vector diffusion_devices; + for (size_t other = 0; other < components.size(); ++other) { + if (components[other].kind == ComponentKind::DIT && components[other].params_bytes > 0) { + diffusion_devices = runtimes[other].devices; + } + } + std::stable_partition(candidates.begin(), candidates.end(), [&](const Runtime& candidate) { + return std::none_of(candidate.devices.begin(), candidate.devices.end(), [&](size_t di) { + return std::find(diffusion_devices.begin(), diffusion_devices.end(), di) != diffusion_devices.end(); + }); + }); + } + if (splittable(comp.kind) && allow_split[ci]) { + for (size_t count = 2; count <= group.size(); ++count) { + Runtime split; + split.devices = std::vector(group.begin(), group.begin() + count); + split.name = join_device_names(devices, split.devices, "&"); + split.estimate_shares = true; + candidates.push_back(std::move(split)); + } + } + + Runtime chosen = previous; + for (const Runtime& candidate : candidates) { + runtimes[ci] = candidate; + if (resident(plan_now(), ci)) { + chosen = candidate; + break; + } + } + runtimes[ci] = chosen; + if (chosen.name == previous.name) { + continue; + } + + const std::string module = module_key(comp.kind); + if (chosen.devices.size() > 1) { + planned_notes.push_back(module + " is split across " + chosen.name + ": its weights (" + format_gib(comp.params_bytes) + + ") do not fit on " + previous.name + " next to the modules placed before it"); + } else if (fits_before) { + planned_notes.push_back(module + " runs on " + chosen.name + " (--multi-gpu on keeps " + previous.name + " for the diffusion model)"); + } else { + planned_notes.push_back(module + " runs on " + chosen.name + ": it does not fit on " + previous.name + " next to the modules placed before it"); + } + } + + bool changed = false; + for (size_t ci = 0; ci < components.size(); ++ci) { + changed = changed || runtimes[ci].name != original[ci].name; + } + const Plan final_plan = plan_now(); + + if (!changed && mode == MultiGpuMode::AUTO) { + bool all_resident = true; + std::vector idle = group; + std::vector hosts; + for (size_t ci = 0; ci < components.size(); ++ci) { + if (components[ci].params_bytes == 0) { + continue; + } + all_resident = all_resident && resident(final_plan, ci); + if (std::find(hosts.begin(), hosts.end(), runtimes[ci].name) == hosts.end()) { + hosts.push_back(runtimes[ci].name); + } + for (size_t di : runtimes[ci].devices) { + idle.erase(std::remove(idle.begin(), idle.end(), di), idle.end()); + } + } + if (all_resident && !idle.empty()) { + std::string host_names; + for (const std::string& host : hosts) { + host_names += (host_names.empty() ? "" : ", ") + host; + } + planned_notes.push_back(std::to_string(group.size()) + " GPUs found (" + join_device_names(devices, group, ", ") + + "); everything fits on " + host_names + ", so " + join_device_names(devices, idle, ", ") + + " will not be used (--multi-gpu on would put the text encoder and the VAE on the other GPUs)"); + } + } + notes.insert(notes.end(), planned_notes.begin(), planned_notes.end()); + return changed; + } + static bool resolve_runtimes(const std::vector& components, const std::vector& devices, std::string& runtime_spec, std::vector& runtimes, + std::vector& explicit_runtime, std::string& error) { SDBackendAssignment assignment; if (!sd_parse_backend_assignment(runtime_spec, &assignment, &error)) { @@ -381,8 +683,10 @@ namespace sd::backend_fit { } const size_t main_device = select_main_device(devices); const SDBackendModule modules[] = {SDBackendModule::DIFFUSION, SDBackendModule::TE, SDBackendModule::VAE}; - for (const Component& comp : components) { - std::string name = assignment.get(modules[int(comp.kind)]); + for (size_t ci = 0; ci < components.size(); ++ci) { + const Component& comp = components[ci]; + std::string name = assignment.get(modules[int(comp.kind)]); + explicit_runtime[ci] = !name.empty(); if (name.empty()) { name = main_device == SIZE_MAX ? "cpu" : devices[main_device].name; if (comp.params_bytes > 0) { @@ -420,7 +724,8 @@ namespace sd::backend_fit { sd::ggml_graph_cut::MaxVramAssignment& budgets, std::string& runtime_spec, std::string& params_spec, - int& memory_guard_percent) { + int& memory_guard_percent, + const Options& options) { std::string error; if (!budgets.canonicalize_backend_keys(&error)) { LOG_ERROR("%s", error.c_str()); @@ -432,7 +737,8 @@ namespace sd::backend_fit { const auto components = estimate_components(loader, override_wtype); const auto devices = enumerate_gpu_devices(budgets, !runtime_spec.empty()); std::vector runtimes; - if (!runtime_spec.empty() && !resolve_runtimes(components, devices, runtime_spec, runtimes, error)) { + std::vector explicit_runtime(components.size(), false); + if (!runtime_spec.empty() && !resolve_runtimes(components, devices, runtime_spec, runtimes, explicit_runtime, error)) { LOG_ERROR("%s", error.c_str()); return false; } @@ -455,7 +761,30 @@ namespace sd::backend_fit { } } const int64_t ram_budget = std::max(free_ram - std::max(2048 * MiB, free_ram / 10) - compute_reserve, 0); - const auto plan = compute_plan(components, devices, ram_budget, runtimes); + + const bool append_runtime_names = runtimes.empty(); + std::vector multi_gpu_notes; + if (!cpu_only) { + std::vector planned = runtimes; + if (planned.empty() && select_main_device(devices) != SIZE_MAX) { + const size_t main_device = select_main_device(devices); + planned.assign(components.size(), Runtime{devices[main_device].name, {main_device}}); + } + if (!planned.empty() && + plan_multi_gpu_runtimes(components, devices, ram_budget, options.multi_gpu, explicit_runtime, + split_allowed(components, options.split_mode_spec), planned, multi_gpu_notes)) { + if (!append_runtime_names) { + for (size_t ci = 0; ci < components.size(); ++ci) { + if (!explicit_runtime[ci] && components[ci].params_bytes > 0 && planned[ci].name != runtimes[ci].name) { + set_assignment(runtime_spec, module_key(components[ci].kind), planned[ci].name); + } + } + } + runtimes = std::move(planned); + } + } + + const auto plan = compute_plan(components, devices, ram_budget, runtimes); params_spec.clear(); if (!plan.valid) { if (devices.empty()) { @@ -468,6 +797,9 @@ namespace sd::backend_fit { } print_plan(plan, components, devices, free_ram, ram_budget); + for (const std::string& note : multi_gpu_notes) { + LOG_INFO("auto-fit: %s", note.c_str()); + } if (cpu_only) { const bool streams_from_disk = std::any_of(plan.decisions.begin(), plan.decisions.end(), [](const Decision& decision) { return decision.params_location == ParamsLocation::DISK; @@ -486,7 +818,7 @@ namespace sd::backend_fit { continue; } const char* key = module_key(components[ci].kind); - if (runtimes.empty()) { + if (append_runtime_names) { append_assignment(runtime_spec, key, plan.runtimes[ci].name); } if (plan.decisions[ci].params_location != ParamsLocation::MAIN_GPU) { diff --git a/src/core/backend_fit.h b/src/core/backend_fit.h index c50718a1..cf9b01f1 100644 --- a/src/core/backend_fit.h +++ b/src/core/backend_fit.h @@ -10,6 +10,20 @@ namespace sd::backend_fit { + enum class MultiGpuMode { + AUTO, + ON, + OFF, + }; + + struct Options { + MultiGpuMode multi_gpu = MultiGpuMode::AUTO; + std::string split_mode_spec; + }; + + // Accepts auto, on and off in any case; an empty string means auto. + bool parse_multi_gpu_mode(const std::string& text, MultiGpuMode& mode); + // memory_guard_percent is raised from 0 when the plan runs a component on the CPU with its // weights read from disk: without the guard nothing would release them between segments. bool derive_backend_specs(ModelLoader& loader, @@ -17,7 +31,8 @@ namespace sd::backend_fit { sd::ggml_graph_cut::MaxVramAssignment& budgets, std::string& runtime_spec, std::string& params_spec, - int& memory_guard_percent); + int& memory_guard_percent, + const Options& options); bool prepare_vae_decode_retry_tiling(sd_tiling_params_t& tiling_params, bool prefer_temporal_tiling, diff --git a/src/pipeline/diffusion_engine.cpp b/src/pipeline/diffusion_engine.cpp index 6b0c34f0..60be6d74 100644 --- a/src/pipeline/diffusion_engine.cpp +++ b/src/pipeline/diffusion_engine.cpp @@ -974,6 +974,7 @@ bool StableDiffusionGGML::init(const sd_ctx_params_t* sd_ctx_params) { backend_spec = SAFE_STR(sd_ctx_params->backend); params_backend_spec = SAFE_STR(sd_ctx_params->params_backend); split_mode_spec = SAFE_STR(sd_ctx_params->split_mode); + multi_gpu_spec = SAFE_STR(sd_ctx_params->multi_gpu); auto_fit_enabled = sd_ctx_params->auto_fit && params_backend_spec.empty(); backend_fallback_disabled = sd_ctx_params->disable_backend_fallback; max_vram_assignment.reset(0.f); @@ -1017,6 +1018,13 @@ bool StableDiffusionGGML::init(const sd_ctx_params_t* sd_ctx_params) { LOG_INFO("Version: %s ", model_version_to_str[version]); } + sd::backend_fit::Options fit_options; + if (!sd::backend_fit::parse_multi_gpu_mode(multi_gpu_spec, fit_options.multi_gpu)) { + LOG_ERROR("multi_gpu must be 'auto', 'on' or 'off' (got '%s')", multi_gpu_spec.c_str()); + return false; + } + fit_options.split_mode_spec = split_mode_spec; + if (auto_fit_enabled) { int memory_guard_percent = sd_ctx_params->memory_guard; if (!sd::backend_fit::derive_backend_specs(model_loader, @@ -1024,7 +1032,8 @@ bool StableDiffusionGGML::init(const sd_ctx_params_t* sd_ctx_params) { max_vram_assignment, backend_spec, params_backend_spec, - memory_guard_percent)) { + memory_guard_percent, + fit_options)) { return false; } if (memory_guard_percent != sd_ctx_params->memory_guard) { diff --git a/src/pipeline/diffusion_engine.h b/src/pipeline/diffusion_engine.h index 25b996f4..0812e4cd 100644 --- a/src/pipeline/diffusion_engine.h +++ b/src/pipeline/diffusion_engine.h @@ -100,6 +100,7 @@ public: std::string backend_spec; std::string params_backend_spec; std::string split_mode_spec; + std::string multi_gpu_spec; bool auto_fit_enabled = false; bool backend_fallback_disabled = false; @@ -141,6 +142,7 @@ public: &sd_ctx_params_t::pulid_weights_path, &sd_ctx_params_t::tensor_type_rules, &sd_ctx_params_t::max_vram, &sd_ctx_params_t::backend, &sd_ctx_params_t::params_backend, &sd_ctx_params_t::split_mode, + &sd_ctx_params_t::multi_gpu, &sd_ctx_params_t::rpc_servers, &sd_ctx_params_t::model_args}) { strings.emplace_back(SAFE_STR(initial.*member)); params.*member = strings.back().c_str(); diff --git a/src/stable-diffusion.cpp b/src/stable-diffusion.cpp index 30e71e27..b860fe8c 100644 --- a/src/stable-diffusion.cpp +++ b/src/stable-diffusion.cpp @@ -355,6 +355,7 @@ void sd_ctx_params_init(sd_ctx_params_t* sd_ctx_params) { sd_ctx_params->pulid_weights_path = nullptr; sd_ctx_params->memory_guard = 0; sd_ctx_params->disable_backend_fallback = false; + sd_ctx_params->multi_gpu = nullptr; } char* sd_ctx_params_to_str(const sd_ctx_params_t* sd_ctx_params) { @@ -398,6 +399,7 @@ char* sd_ctx_params_to_str(const sd_ctx_params_t* sd_ctx_params) { "backend: %s\n" "params_backend: %s\n" "split_mode: %s\n" + "multi_gpu: %s\n" "model_args: %s\n" "auto_fit: %s\n" "disable_backend_fallback: %s\n" @@ -441,6 +443,7 @@ char* sd_ctx_params_to_str(const sd_ctx_params_t* sd_ctx_params) { SAFE_STR(sd_ctx_params->backend), SAFE_STR(sd_ctx_params->params_backend), SAFE_STR(sd_ctx_params->split_mode), + SAFE_STR(sd_ctx_params->multi_gpu), SAFE_STR(sd_ctx_params->model_args), BOOL_STR(sd_ctx_params->auto_fit), BOOL_STR(sd_ctx_params->disable_backend_fallback),