mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-11-28 19:16:33 +00:00
Brings in the Diffusion Graph Language runtime (#2396) and the commits after it: MOSS-Image edit LoRAs (#2203), runtime LoRA counts (#2400), immediate LoRA merges for INT8 weights (#2403), one server context with on-demand model loading (#2404), Qwen Image 2.1 prefix caching (#2405) and idle TTLs for model weights (#2406). The server changes of #2404 and #2406 come in the same merge so that sd-server is ported once. src/pipeline and src/stable-diffusion.cpp are gone; the fork's additions move into the graph runtime: - CPU fallback: ModelRuntime::configure_backends() falls back to the cpu backend, with a warning, when a module's backend cannot be initialized; disable_backend_fallback (also a DGL context option) makes it fail with SD_ERROR_BACKEND instead. - memory_guard is a DGL context option for the context's ModelManager. ModelManager::ResourceHandle, the residency manager the models use, forwards the guard hooks and honours the prefetch suspension. - layer_split_fill is applied when the context is created; multi_gpu is checked there. The multi-GPU planner, which upstream no longer calls, picks the device of each module when auto-fit is on, more than one GPU is present and no backend was given (planned_backend_spec() in src/api/generation_program.cpp); the run plan places the weights. - The SD2 v-parameterization probe reports a failed compute instead of aborting. - sd_ctx_create, sd_ctx_set_model_options*, sd_generate_image and sd_generate_video record the error code again; a load failure with no deeper cause is model_file. sd-server keeps the ContextKeeper as the owner of the context: - requests pick their model as upstream does (recipe and models in the body); the switch goes through ContextKeeper::load(), so the profile ladder and the recovery cover it too; - sd_ctx_mutex serializes model switches, generations and DGL scripts; a DGL script runs on the keeper's context, or on a temporary one while no model is loaded (ContextKeeper::with_context()); the idle TTL release does not wait for a running generation (try_with_context()); - every context the keeper creates gets the server's file-access policy (create_context_from_params() + apply_server_file_access()); - /sdcpp/v1/models/load and /unload stay, rebuilt on resolve_model_selection(), as do /sdcpp/v1/health and /resources; - /sdcpp/v1/upscale and the server-side ADetailer go, as upstream did: both are DGL scripts now. Tests: memory_guard_engine_test follows the new capacity hook; server_recovery_test checks with_context() and try_with_context() (red with both behaviours broken); api_robustness expects a missing model file at the first generation, where the weights load now; server_cancel_e2e.py uses 100 steps, the server's maximum. docs: the C API section of docs/integrazione_progetti.md uses the opaque options; backend.md says what the multi-GPU planner decides with DGL; api.md documents the kept load/unload routes. Verified with a CPU build: the library, sd-cli and sd-server build; all nine unit-test programs pass, server_recovery_test eight times in a row and twice under TSan with no warnings; with a synthetic SD 1.5 model (random weights) sd-cli generates, falls back to the cpu backend for --backend vulkan0 and fails as backend with --disable-backend-fallback, and test/server_cancel_e2e.py passes its 35 checks against a real sd-server. A scratch smoke test of the server (20 checks: no model, DGL jobs with and without a model, models/load and unload, a job that picks its model, the idle release) passes too. Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC
140 lines
6.7 KiB
C++
140 lines
6.7 KiB
C++
// Test of the memory guard inside the execution engine (src/core/execution_engine.cpp): ensure_capacity frees
|
|
// weight blocks until a request both fits and keeps the usage under the guard threshold, and still runs a request
|
|
// that fits when the threshold cannot be reached. The residency manager is a scripted fake, so no model is 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/memory_guard_engine_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 memory_guard_engine_test
|
|
// Run: ./memory_guard_engine_test ; exit code 0 = all checks passed.
|
|
#include <cstdio>
|
|
#include <vector>
|
|
|
|
#include "core/execution_engine.h"
|
|
#include "device_residency_manager.h"
|
|
#include "ggml-cpu.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;
|
|
}
|
|
|
|
// Every eviction frees one block. The capacity fits once fits_after blocks are gone and the guard is satisfied
|
|
// once guard_after blocks are gone (never when guard_after is negative).
|
|
struct ScriptedManager : DeviceResidencyManager {
|
|
int blocks = 4;
|
|
int evicted = 0;
|
|
int fits_after = 0;
|
|
int guard_after = 0;
|
|
int unreachable_calls = 0;
|
|
|
|
bool segmented_compute_enabled() const override { return true; }
|
|
bool prefetch_enabled() const override { return false; }
|
|
void register_runtime_owner(uintptr_t) override {}
|
|
void remove_runtime_owner(uintptr_t) override {}
|
|
bool fits_compute_backend_capacity(const DeviceMemoryRequest&, const std::vector<ggml_tensor*>&, bool) const override {
|
|
return true;
|
|
}
|
|
bool assign_compute_backend(const std::vector<ggml_tensor*>&, ggml_backend_t) override { return true; }
|
|
bool prepare_params(const std::vector<ggml_tensor*>&) override { return true; }
|
|
void release_compute_backend_params(const std::vector<ggml_tensor*>&) override {}
|
|
void evict_compute_backend_params(const std::vector<ggml_tensor*>&) override {}
|
|
WeightResidencyInfo inspect_compute_backend_params(const std::vector<ggml_tensor*>&) const override { return {}; }
|
|
void update_runtime_residency(uintptr_t, ggml_backend_t, size_t) override {}
|
|
bool ensure_compute_backend_capacity(const DeviceMemoryRequest& request,
|
|
const std::vector<ggml_tensor*>& required_params,
|
|
const std::vector<std::vector<ggml_tensor*>>& preferred_eviction_order,
|
|
const std::vector<ggml_tensor*>& protected_params) override {
|
|
return execution_engine()->ensure_capacity(*this, request, required_params, preferred_eviction_order, protected_params);
|
|
}
|
|
DeviceMemoryCapacity inspect_compute_backend_capacity(const DeviceMemoryRequest&,
|
|
const std::vector<ggml_tensor*>&,
|
|
bool) const override {
|
|
DeviceMemoryCapacity capacity;
|
|
capacity.required_device_bytes = 100;
|
|
capacity.available_device_bytes = evicted >= fits_after ? 100 : 50;
|
|
return capacity;
|
|
}
|
|
std::vector<WeightResidencyBlock> evictable_compute_backend_blocks(ggml_backend_t backend,
|
|
const std::vector<ggml_tensor*>&) const override {
|
|
std::vector<WeightResidencyBlock> result;
|
|
for (int i = evicted; i < blocks; ++i) {
|
|
WeightResidencyBlock block;
|
|
block.backend = backend;
|
|
block.bytes = 10;
|
|
block.last_use_epoch = static_cast<uint64_t>(i);
|
|
result.push_back(block);
|
|
}
|
|
return result;
|
|
}
|
|
bool evict_compute_backend_block(const WeightResidencyBlock&) override {
|
|
++evicted;
|
|
return true;
|
|
}
|
|
bool memory_guard_ok(const DeviceMemoryRequest&, const std::vector<ggml_tensor*>&) override {
|
|
return guard_after >= 0 && evicted >= guard_after;
|
|
}
|
|
void memory_guard_unreachable(const DeviceMemoryRequest&, const std::vector<ggml_tensor*>&) override {
|
|
++unreachable_calls;
|
|
}
|
|
WeightPrefetchResult prefetch_params(uintptr_t, const std::vector<ggml_tensor*>&) override {
|
|
return WeightPrefetchResult::Unsupported;
|
|
}
|
|
bool activate_prefetched_params(uintptr_t, const std::vector<ggml_tensor*>&) override { return true; }
|
|
void clear_prefetched_params(uintptr_t) override {}
|
|
};
|
|
|
|
static bool run(ScriptedManager& manager, ggml_backend_t backend) {
|
|
DeviceMemoryRequest request;
|
|
request.compute_backend = backend;
|
|
request.owner_id = 1;
|
|
return manager.ensure_compute_backend_capacity(request, {}, {}, {});
|
|
}
|
|
|
|
int main() {
|
|
ggml_backend_t backend = ggml_backend_cpu_init();
|
|
if (backend == nullptr) {
|
|
std::printf("FAIL: no CPU backend\n");
|
|
return 1;
|
|
}
|
|
{
|
|
ScriptedManager manager;
|
|
const bool ok = run(manager, backend);
|
|
check(ok && manager.evicted == 0 && manager.unreachable_calls == 0,
|
|
"a request that fits under the guard threshold frees nothing");
|
|
}
|
|
{
|
|
ScriptedManager manager;
|
|
manager.guard_after = 2;
|
|
const bool ok = run(manager, backend);
|
|
check(ok && manager.evicted == 2 && manager.unreachable_calls == 0,
|
|
"above the guard threshold, blocks are freed until the usage is under it");
|
|
}
|
|
{
|
|
ScriptedManager manager;
|
|
manager.fits_after = 1;
|
|
manager.guard_after = 3;
|
|
const bool ok = run(manager, backend);
|
|
check(ok && manager.evicted == 3 && manager.unreachable_calls == 0,
|
|
"freeing goes on after the request fits, until the guard is satisfied too");
|
|
}
|
|
{
|
|
ScriptedManager manager;
|
|
manager.guard_after = -1;
|
|
const bool ok = run(manager, backend);
|
|
check(ok && manager.evicted == manager.blocks && manager.unreachable_calls == 1,
|
|
"when the guard target is out of reach, a request that fits still runs and the guard is told once");
|
|
}
|
|
{
|
|
ScriptedManager manager;
|
|
manager.fits_after = 99;
|
|
const bool ok = run(manager, backend);
|
|
check(!ok && manager.unreachable_calls == 0,
|
|
"a request that cannot fit fails as before, without a guard warning");
|
|
}
|
|
ggml_backend_free(backend);
|
|
std::printf("\n%s (%d failures)\n", failures == 0 ? "ALL TESTS PASSED" : "TESTS FAILED", failures);
|
|
return failures == 0 ? 0 : 1;
|
|
}
|