Commit b853093 for stable-diffusion.cpp
commit b853093c2796b10a2faceef90daeb9630d013e05
Author: leejet <leejet714@gmail.com>
Date: Sun Dec 6 13:09:18 2026 +0800
feat: support --max-ram in CLI and server generation (#2538)
diff --git a/docs/guides/performance.md b/docs/guides/performance.md
index ee36980..f610e39 100644
--- a/docs/guides/performance.md
+++ b/docs/guides/performance.md
@@ -171,6 +171,7 @@ buffers, or 1 GiB on Windows CUDA. A graph that reuses its resident weights and
workspace still checks the managed budget.
- `--max-vram <GiB>` optionally lowers the live-memory limit. A positive value is a managed per-device budget, `0` uses the device's current free memory without an explicit budget, and a negative value snapshots free memory at startup while reserving that many GiB (`--max-vram -1` reserves about 1 GiB). Driver contexts and unrelated external allocations remain outside the managed budget.
+- `--max-ram <GiB>` caps host memory used by managed weight buffers in `sd-cli` and `sd-server`, including DGL runs. Use a nonnegative value. `0` disables the explicit cap. Temporary compute buffers are outside this budget.
- `--disable-prefetch` disables asynchronous next-segment prefetch while retaining synchronous loading, eviction, and segmented execution.
- `--disable-segmented-compute` forces monolithic graph execution for diagnostics or compatibility, even when the automatic memory check would select segments.
diff --git a/examples/cli/dgl.cpp b/examples/cli/dgl.cpp
index e7e89d7..64d8087 100644
--- a/examples/cli/dgl.cpp
+++ b/examples/cli/dgl.cpp
@@ -27,7 +27,6 @@ ArgOptions DGLModeParams::get_options() {
{"", "--script", "path to the .dgl program", 0, &script_path},
{"", "--write-builtins", "write the DGL built-in catalog to a file", 0, &write_builtins_path},
{"", "--check-builtins", "verify a file against the DGL built-in catalog", 0, &check_builtins_path},
- {"", "--max-ram", "limit DGL context weight residency in GiB", 0, &max_ram},
};
options.bool_options = {
{"", "--check", "check the DGL script without loading models", true, &check_only},
@@ -73,7 +72,7 @@ ArgOptions DGLModeParams::get_options() {
bool DGLModeParams::has_options() const {
return !script_path.empty() || !write_builtins_path.empty() || !check_builtins_path.empty() ||
- !tensor_paths.empty() || !script_args.empty() || !max_ram.empty() || check_only || run_once.has_value() ||
+ !tensor_paths.empty() || !script_args.empty() || check_only || run_once.has_value() ||
list_builtins || download.has_options();
}
@@ -87,8 +86,8 @@ bool DGLModeParams::validate() const {
LOG_ERROR("DGL mode requires exactly one of --script, --list-builtins, --write-builtins and --check-builtins");
return false;
}
- if (script_path.empty() && (check_only || run_once.has_value() || !tensor_paths.empty() || !max_ram.empty())) {
- LOG_ERROR("--check, --run-once, --tensor and --max-ram require --script");
+ if (script_path.empty() && (check_only || run_once.has_value() || !tensor_paths.empty())) {
+ LOG_ERROR("--check, --run-once and --tensor require --script");
return false;
}
if (script_path.empty() && download.has_options()) {
@@ -304,8 +303,6 @@ int DGLModeParams::run(const SDContextParams& options,
check_dgl(sd_dgl_ctx_set_progress_callback(context.get(), callback, userdata, &progress) == SD_STATUS_OK);
}
check_dgl(sd_dgl_ctx_set(context.get(), "embed_metadata", embed_metadata ? "on" : "off"));
- if (!max_ram.empty())
- check_dgl(sd_dgl_ctx_set(context.get(), "max_ram", max_ram.c_str()));
sd_dgl_result_t* raw_result = nullptr;
const auto status = sd_dgl_run(context.get(), source.c_str(), nullptr, 0, &raw_result);
log_callback.flush();
diff --git a/examples/cli/dgl.h b/examples/cli/dgl.h
index 5aa485e..451b34c 100644
--- a/examples/cli/dgl.h
+++ b/examples/cli/dgl.h
@@ -23,7 +23,6 @@ struct DGLModeParams {
std::string script_path;
std::string write_builtins_path;
std::string check_builtins_path;
- std::string max_ram;
std::vector<std::pair<std::string, std::string>> tensor_paths;
DGLScriptArgs script_args;
bool check_only = false;
diff --git a/examples/cli/main.cpp b/examples/cli/main.cpp
index e8b31bc..8259917 100644
--- a/examples/cli/main.cpp
+++ b/examples/cli/main.cpp
@@ -768,7 +768,7 @@ int main(int argc, const char* argv[]) {
if (sd_ctx == nullptr) {
log_callback.flush();
- LOG_INFO("sd_ctx_create failed");
+ LOG_ERROR("sd_ctx_create failed: %s", sd_get_last_error());
return 1;
}
diff --git a/examples/common/common.cpp b/examples/common/common.cpp
index 9d0bbdd..48d97fe 100644
--- a/examples/common/common.cpp
+++ b/examples/common/common.cpp
@@ -542,6 +542,11 @@ ArgOptions SDContextParams::get_options() {
"optional per-device budget in GiB for managed weights, runner buffers, and idle backend buffers. Accepts a single value or assignments by backend/device, e.g. 6 or cuda0=6,vulkan0=4. 0 uses live free VRAM without an explicit budget; a negative value reserves that much free VRAM",
0,
&max_vram},
+ {"",
+ "--max-ram",
+ "optional host memory budget in GiB for managed weight buffers. 0 disables the explicit limit",
+ 0,
+ &max_ram},
};
options.int_options = {
@@ -948,6 +953,7 @@ std::string SDContextParams::to_string() const {
<< " sampler_rng_type: " << sampler_rng_type << ",\n"
<< " offload_params_to_cpu: " << (offload_params_to_cpu ? "true" : "false") << ",\n"
<< " max_vram: \"" << max_vram << "\",\n"
+ << " max_ram: \"" << max_ram << "\",\n"
<< " disable_prefetch: " << disable_prefetch << ",\n"
<< " disable_segmented_compute: " << disable_segmented_compute << ",\n"
<< " batched_cfg: " << batched_cfg << ",\n"
@@ -982,6 +988,7 @@ bool SDContextParams::configure_dgl(sd_dgl_ctx_t* ctx) const {
return !option || sd_dgl_ctx_set(ctx, key, std::to_string(*option).c_str());
};
return (!max_vram || sd_dgl_ctx_set(ctx, "max_vram", max_vram->c_str())) &&
+ (!max_ram || max_ram->empty() || sd_dgl_ctx_set(ctx, "max_ram", max_ram->c_str())) &&
(!linear_type || sd_dgl_ctx_set(ctx, "linear_type", linear_type->c_str())) &&
set("auto_fit", auto_fit) &&
set("conditioning_cache_size", conditioning_cache_size) &&
@@ -1052,6 +1059,7 @@ SDOptPtr SDContextParams::to_context_opt() {
opt.set_bool("force_sdxl_vae_conv_scale", force_sdxl_vae_conv_scale);
opt.set_string("vae_format", vae_format);
opt.set_string("max_vram", max_vram);
+ opt.set_string("max_ram", max_ram);
opt.set_bool("disable_prefetch", disable_prefetch);
opt.set_bool("disable_segmented_compute", disable_segmented_compute);
opt.set_bool("batched_cfg", batched_cfg);
diff --git a/examples/common/common.h b/examples/common/common.h
index 2935955..b629427 100644
--- a/examples/common/common.h
+++ b/examples/common/common.h
@@ -162,6 +162,7 @@ struct SDContextParams {
std::optional<std::string> sampler_rng_type;
bool offload_params_to_cpu = false;
std::optional<std::string> max_vram;
+ std::optional<std::string> max_ram;
std::optional<bool> disable_prefetch;
std::optional<bool> disable_segmented_compute;
std::optional<bool> eager_load;