--profile max|balanced|lite|auto sets, in one option, what a small
machine needs: balanced turns the memory guard on at 90; lite also
puts the text encoder on the CPU, tiles the VAE, runs CFG unbatched
and lowers the guard to 85. An option given on the command line always
wins over the profile, including --memory-guard 0. A profile does not
touch --params-backend or --backend, so --auto-fit keeps deciding
where the weights live, and it never changes the size or the steps of
an image.
auto compares the size of the model files plus a working-memory
reserve with the free memory of the discrete GPU with the most, or
with the available RAM without one, and logs the figures it used. It
leaves the defaults alone when the sizes or the memory are unknown.
parse_options() now records which options were given, through
option_was_given(). The levels below lite (weights streamed from disk,
everything on the CPU) cannot be chosen from the command line; they
are the steps a server takes when a request runs out of memory.
The thresholds of auto are heuristics and have not been tuned on a real
model or a GPU. Checked with the synthetic SD 1.5 model on the CPU: the
three profiles give the same image, and with 3.3 GiB held by another
process auto picks balanced.
Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC
new_sd_ctx(), generate_image() and generate_video() now record the
reason of a failure, and sd_get_last_error() returns it with a code:
out of memory, model file, backend, invalid argument, busy or
cancelled. An application can tell "try again with less memory" from
"the file is damaged" instead of reading the log. The state belongs to
the calling thread; errors logged by the model loader threads reach the
thread that reports the failure. C++ exceptions, std::bad_alloc
included, no longer cross the C API.
A test with a limited address space showed the process ending with
std::terminate when a loader thread could not be created while others
were already running. The loader now uses fewer threads, or the
calling thread when none could be started, and the loader threads
report exceptions as a failed load.
sd_get_system_memory() returns total, available and process RAM. The
free memory of the CPU device in sd_get_device_info() is the available
RAM, because ggml reports the total on Linux and macOS.
Checked with a synthetic 2.1 GB SD 1.5 model under a limited address
space: the standard configuration fails with out_of_memory, the same
context generates once the limit is lifted, and weights on disk let
the same request succeed. The Windows branch of system_memory.cpp was
only syntax-checked; macOS was not built.
Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC
Without a GPU, auto-fit now compares the available RAM with the size of
each component and leaves the ones that do not fit on disk. When it does,
it turns the memory guard on at 90% unless --memory-guard is set.
On CPU backends the memory guard now counts the weights a graph still has
to read from disk before they are loaded. When that total would pass the
threshold and some weights can be released, the graph runs in segments, as
it already does on a GPU, so the peak stays well below the size of the
model. Splitting is skipped when the guard is off or when every weight is
resident, because it could not lower the peak.
Checked with a synthetic SD 1.5 model (128x128, 3 steps, 2 threads, Linux):
with 3.3 GiB of RAM held by another process and a 60% guard, the UNet runs
in 22 segments with a 732 MiB peak instead of 1722 MiB and the image is
identical pixel for pixel. Resident weights and runs without pressure or
without the guard behave as before. Not tested on Windows or with real
models.
Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC
The guard returned an empty sample for CPU backends, so a CPU-only run
could fill system RAM without the guard ever reacting. On the CPU, measure
system RAM instead: total minus the memory available without swapping,
plus the allocation the graph is about to make, against total physical
memory.
Under pressure the guard releases unpinned weights that can be re-read
from the model file (--params-backend disk). It never fails a graph on the
CPU, since system RAM has no managed capacity, and CPU pressure does not
suspend prefetch for GPU backends.
Expose available_ram_bytes() from backend_fit for the probe, and update
the CLI help, the public header comment and the docs.
Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC
Brings in the nine upstream commits since 228c707, including the VAE
decode tile halving on OOM retry and the demotion of resident params to
disk residency when memory reclamation fails.
The only conflict was in ModelManager::ensure_compute_backend_capacity:
both sides added a block before the final capacity report. Keep both,
and apply the memory guard's "never fail a graph that still fits"
fallback before and after the demotion step.
Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC