mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-11-27 07:40:36 +00:00
The engine keeps, for the whole process, a record of how each device,
the RAM and the disk are doing (src/core/device_health.*). A component
under strain offers less of its capacity, one step per level (85%, 70%,
55%); an excluded one offers nothing and is never computed on. A level
rises by one for a compute that fails, a backend that does not start,
the memory guard that cannot reach its threshold or a read of the
weights that fails (events less than 10 s apart count once), and comes
down one step after each quiet period of 300 s. Outside watchers report
signals with a source and a duration; a signal reaches the other
listings of the same card, a failure of one backend does not.
- C API, additions only: sd_device_health_t, sd_get_device_health(),
sd_report_device_pressure() and sd_resume_device_health().
- The layer split gives each device its share of capacity, the run plan
and the multi-GPU planner their share of memory, and an excluded
device is never the default one. When the record changes, the next
generation releases the cached models and places them again.
- The engine refuses to compute on an excluded device (a backend error
at the start of the segment), computes on the CPU with 25% fewer
threads per level and does not fall back to an excluded CPU. The RAM
is never cut below the share of level 3; with the disk excluded,
weights that would be read from it during the run are refused as out
of memory.
- sd-server: the context keeper skips excluded devices and does not
climb back to them. A guardian (examples/server/guardian.*) stops all
work when a component is reported critical or every compute device is
excluded: the generation in progress ends with the code halted, new
work is refused with 503 halted and Retry-After, queued jobs wait, and
/sdcpp/v1/health reports the guardian and every component. It resumes
when the cause ends, or with POST /sdcpp/v1/guardian/resume; while it
is halted the keeper neither loads the model again nor steps down.
--guardian on|off, on by default.
- POST /sdcpp/v1/devices/{device}/pressure takes the reports of outside
watchers, and termico.py osserva --segnala sends the temperature band
of an NVIDIA GPU as the level of its device.
The values are provisional. This is a layer of the software, not a
safety system, and the docs say so: examples/server/api.md (Device
health and the guardian), the server README, the README of
scripts/diagnostica_termica, docs/agenti_indipendenti.md (section 9)
and the fork history.
test: device_health_test and server_guardian_test are new, and the
recovery, layer split, planner and termico tests cover the new rules.
server_guardian_e2e.py runs the guardian against a real sd-server: 59
checks, three runs in a row, with a synthetic SD 1.5 model. Every
program of test/ passes, the server tests under ThreadSanitizer too,
and broken versions of the registry, guardian, keeper and termico code
make them fail. Not tried: real GPUs, a layer split between two
devices, Windows.
Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC
136 lines
7.9 KiB
C++
136 lines
7.9 KiB
C++
// Test of how the layer split fills the devices (src/core/layer_split_partition.*, --layer-split-fill):
|
|
// proportional ranges, the first device filled first, the choice auto makes for the kinds of devices, and the
|
|
// placement the planner and the split share. No GPU and no model are needed.
|
|
// Build (from the repository root, after building the static library into build/):
|
|
// c++ -std=c++17 -DGGML_MAX_NAME=160 -Isrc -Isrc/core -Iinclude -Ithirdparty -Iggml/include \
|
|
// test/layer_split_fill_test.cpp build/libstable-diffusion.a build/ggml/src/libggml.a \
|
|
// build/ggml/src/libggml-cpu.a build/ggml/src/libggml-base.a -ldl -lm -fopenmp -pthread \
|
|
// -o layer_split_fill_test
|
|
// Run: ./layer_split_fill_test ; exit code 0 = all checks passed.
|
|
#include <cstdio>
|
|
#include <numeric>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "core/layer_split_partition.h"
|
|
|
|
static int failures = 0;
|
|
|
|
static void check(bool condition, const char* what) {
|
|
std::printf("%s %s\n", condition ? "ok: " : "FAIL:", what);
|
|
failures += condition ? 0 : 1;
|
|
}
|
|
|
|
static constexpr int64_t GiB = 1024ll * 1024 * 1024;
|
|
|
|
static std::vector<int64_t> pieces(int64_t total, int count) {
|
|
std::vector<int64_t> result(count, total / count);
|
|
result.back() += total % count;
|
|
return result;
|
|
}
|
|
|
|
static std::vector<int64_t> place(sd::LayerSplitFill fill, const std::vector<int64_t>& segments, const std::vector<int64_t>& capacities) {
|
|
std::vector<int64_t> bytes;
|
|
if (!sd::layer_split_place(segments, sd::layer_split_fill_limits(fill, segments, capacities), bytes)) {
|
|
return {};
|
|
}
|
|
return bytes;
|
|
}
|
|
|
|
static bool near(int64_t value, int64_t expected, int64_t tolerance) {
|
|
return value >= expected - tolerance && value <= expected + tolerance;
|
|
}
|
|
|
|
int main() {
|
|
using sd::LayerSplitFill;
|
|
|
|
LayerSplitFill fill = LayerSplitFill::FILL_FIRST;
|
|
check(sd::parse_layer_split_fill("", fill) && fill == LayerSplitFill::AUTO, "an empty value means auto");
|
|
check(sd::parse_layer_split_fill("Proportional", fill) && fill == LayerSplitFill::PROPORTIONAL,
|
|
"proportional is accepted in any case");
|
|
check(sd::parse_layer_split_fill("fill-first", fill) && fill == LayerSplitFill::FILL_FIRST, "fill-first is accepted");
|
|
check(!sd::parse_layer_split_fill("sideways", fill), "anything else is refused");
|
|
check(std::string(sd::layer_split_fill_name(LayerSplitFill::FILL_FIRST)) == "fill-first", "the names round-trip");
|
|
|
|
const std::vector<int64_t> segments = pieces(8 * GiB, 64);
|
|
{
|
|
const auto bytes = place(LayerSplitFill::FILL_FIRST, segments, {10 * GiB, 10 * GiB});
|
|
check(bytes.size() == 2 && bytes[0] == 8 * GiB && bytes[1] == 0, "fill-first keeps everything on the first device while it fits");
|
|
}
|
|
{
|
|
const auto bytes = place(LayerSplitFill::PROPORTIONAL, segments, {10 * GiB, 10 * GiB});
|
|
check(bytes.size() == 2 && near(bytes[0], 4 * GiB, segments[0]) && near(bytes[1], 4 * GiB, segments[0]),
|
|
"proportional halves the weights between two equal devices");
|
|
}
|
|
{
|
|
const auto bytes = place(LayerSplitFill::PROPORTIONAL, segments, {12 * GiB, 4 * GiB});
|
|
check(bytes.size() == 2 && near(bytes[0], 6 * GiB, segments[0]) && near(bytes[1], 2 * GiB, segments[0]),
|
|
"proportional follows the capacities, 3 to 1");
|
|
}
|
|
{
|
|
const auto fill_first = place(LayerSplitFill::FILL_FIRST, segments, {6 * GiB, 2 * GiB + segments[0]});
|
|
const auto proportional = place(LayerSplitFill::PROPORTIONAL, segments, {6 * GiB, 2 * GiB + segments[0]});
|
|
check(!fill_first.empty() && fill_first == proportional,
|
|
"when the weights barely fit, both fill the devices the same way");
|
|
}
|
|
{
|
|
const auto bytes = place(LayerSplitFill::PROPORTIONAL, segments, {3 * GiB, 3 * GiB});
|
|
check(bytes.empty(), "weights larger than all the devices together are not placed");
|
|
}
|
|
for (LayerSplitFill mode : {LayerSplitFill::FILL_FIRST, LayerSplitFill::PROPORTIONAL}) {
|
|
const auto bytes = place(mode, segments, {7 * GiB, 5 * GiB, 3 * GiB});
|
|
check(bytes.size() == 3 && std::accumulate(bytes.begin(), bytes.end(), int64_t(0)) == 8 * GiB &&
|
|
bytes[0] <= 7 * GiB && bytes[1] <= 5 * GiB && bytes[2] <= 3 * GiB,
|
|
"every byte is placed and no device gets more than its capacity");
|
|
}
|
|
|
|
{
|
|
const std::vector<int64_t> capacities = {10 * GiB, 10 * GiB};
|
|
check(sd::layer_split_apply_shares(capacities, {1.0, 1.0}) == capacities, "healthy devices offer all of their capacity");
|
|
const auto strained = sd::layer_split_apply_shares(capacities, {0.70, 1.0});
|
|
check(strained[0] == 7 * GiB && strained[1] == 10 * GiB, "a device under strain at 70% offers 70% of its capacity");
|
|
|
|
const auto fill_first = place(LayerSplitFill::FILL_FIRST, segments, strained);
|
|
check(fill_first.size() == 2 && near(fill_first[0], 7 * GiB, segments[0]) && near(fill_first[1], GiB, segments[0]),
|
|
"fill-first: the next device takes what the device under strain gives up");
|
|
const auto healthy = place(LayerSplitFill::PROPORTIONAL, segments, capacities);
|
|
const auto proportional = place(LayerSplitFill::PROPORTIONAL, segments, strained);
|
|
check(proportional.size() == 2 && near(proportional[0], 8 * GiB * 7 / 17, segments[0]) &&
|
|
near(proportional[1], 8 * GiB * 10 / 17, segments[0]),
|
|
"proportional: what it gives up is spread in proportion to the capacities");
|
|
check(!healthy.empty() && !proportional.empty() && proportional[0] < healthy[0],
|
|
"so the device under strain holds less than when it was healthy");
|
|
|
|
const auto excluded = place(LayerSplitFill::PROPORTIONAL, segments, sd::layer_split_apply_shares(capacities, {0.0, 1.0}));
|
|
check(excluded.size() == 2 && excluded[0] == 0 && excluded[1] == 8 * GiB, "an excluded device takes nothing");
|
|
check(place(LayerSplitFill::FILL_FIRST, segments, sd::layer_split_apply_shares({10 * GiB, 5 * GiB}, {0.0, 1.0})).empty(),
|
|
"when the others cannot hold what an excluded device leaves, nothing is placed");
|
|
check(place(LayerSplitFill::PROPORTIONAL, segments, sd::layer_split_apply_shares(capacities, {1.0, 1.0})) == healthy,
|
|
"when the device recovers, its share comes back");
|
|
|
|
const auto unknown = sd::layer_split_apply_shares({sd::layer_split_unknown_capacity, 10 * GiB}, {0.85, 1.0});
|
|
check(unknown[0] == sd::layer_split_unknown_capacity, "a capacity the device does not report stays unknown");
|
|
check(sd::layer_split_apply_shares({sd::layer_split_unknown_capacity}, {0.0})[0] == 0, "unless the device is excluded");
|
|
}
|
|
|
|
const auto gpu = GGML_BACKEND_DEVICE_TYPE_GPU;
|
|
const auto igpu = GGML_BACKEND_DEVICE_TYPE_IGPU;
|
|
const auto cpu = GGML_BACKEND_DEVICE_TYPE_CPU;
|
|
check(sd::resolve_layer_split_fill(LayerSplitFill::AUTO, {gpu, gpu}) == LayerSplitFill::PROPORTIONAL,
|
|
"auto spreads the weights over discrete GPUs");
|
|
check(sd::resolve_layer_split_fill(LayerSplitFill::AUTO, {gpu, igpu}) == LayerSplitFill::FILL_FIRST,
|
|
"auto fills the discrete GPU first when an integrated GPU takes part");
|
|
check(sd::resolve_layer_split_fill(LayerSplitFill::AUTO, {gpu, cpu}) == LayerSplitFill::FILL_FIRST,
|
|
"and when the CPU takes part");
|
|
check(sd::resolve_layer_split_fill(LayerSplitFill::PROPORTIONAL, {gpu, igpu}) == LayerSplitFill::PROPORTIONAL &&
|
|
sd::resolve_layer_split_fill(LayerSplitFill::FILL_FIRST, {gpu, gpu}) == LayerSplitFill::FILL_FIRST,
|
|
"a choice made by the user is kept");
|
|
|
|
check(sd::layer_split_fill() == LayerSplitFill::AUTO, "auto until a context chooses");
|
|
sd::set_layer_split_fill(LayerSplitFill::FILL_FIRST);
|
|
check(sd::layer_split_fill() == LayerSplitFill::FILL_FIRST, "the choice of the context is kept");
|
|
sd::set_layer_split_fill(LayerSplitFill::AUTO);
|
|
|
|
std::printf("\n%s (%d failures)\n", failures == 0 ? "ALL TESTS PASSED" : "TESTS FAILED", failures);
|
|
return failures == 0 ? 0 : 1;
|
|
}
|