mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-11-28 19:16:33 +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
166 lines
5.8 KiB
C++
166 lines
5.8 KiB
C++
#pragma once
|
|
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <json.hpp>
|
|
#include "common/common.h"
|
|
#include "common/resource_owners.hpp"
|
|
#include "stable-diffusion.h"
|
|
|
|
using json = nlohmann::json;
|
|
|
|
struct ArgOptions;
|
|
struct SDContextParams;
|
|
struct AsyncJobManager;
|
|
class ContextKeeper;
|
|
class Guardian;
|
|
struct ServerModels;
|
|
|
|
struct SDSvrParams {
|
|
std::string listen_ip = "127.0.0.1";
|
|
int listen_port = 1234;
|
|
int max_pending_jobs = 64;
|
|
int completed_ttl_seconds = 600;
|
|
int weight_staging_ttl = -1;
|
|
int weight_residency_ttl = -1;
|
|
std::string serve_html_path;
|
|
std::string api_key; // empty = no authentication
|
|
std::string cors_origins; // comma-separated allowlist; empty = reflect any origin
|
|
bool disable_degradation = false;
|
|
int degradation_cooldown = 300; // seconds before the server tries a heavier profile again; 0 = never
|
|
bool guardian = true; // stop all work when device health leaves nothing to run on (--guardian)
|
|
std::string dgl_dir = "dgl";
|
|
std::vector<std::string> model_dirs;
|
|
bool normal_exit = false;
|
|
sd_log_level_t log_level = SD_LOG_INFO;
|
|
bool color = false;
|
|
|
|
ArgOptions get_options();
|
|
bool validate();
|
|
bool resolve_and_validate();
|
|
std::string to_string() const;
|
|
};
|
|
|
|
struct LoraEntry {
|
|
std::string name;
|
|
std::string path;
|
|
std::string fullpath;
|
|
std::string display_path;
|
|
std::filesystem::path root;
|
|
};
|
|
|
|
struct UpscalerEntry {
|
|
std::string name;
|
|
std::string path;
|
|
std::string fullpath;
|
|
std::string model_name;
|
|
int scale = 4;
|
|
uintmax_t file_size = 0;
|
|
std::filesystem::file_time_type last_modified;
|
|
};
|
|
|
|
struct ServerRuntime {
|
|
ContextKeeper* keeper;
|
|
// Held across a model switch and the generation that follows, and by every other user of the context.
|
|
std::mutex* sd_ctx_mutex;
|
|
const SDSvrParams* svr_params;
|
|
SDContextParams* ctx_params;
|
|
SDGenerationParams* default_gen_params;
|
|
std::vector<LoraEntry>* lora_cache;
|
|
std::mutex* lora_mutex;
|
|
std::vector<UpscalerEntry>* upscaler_cache;
|
|
std::mutex* upscaler_mutex;
|
|
AsyncJobManager* async_job_manager;
|
|
ServerModels* models;
|
|
Guardian* guardian = nullptr;
|
|
};
|
|
|
|
struct ServerADetailerPass {
|
|
std::string prompt;
|
|
std::string negative_prompt;
|
|
std::string extra_args;
|
|
std::optional<int> width;
|
|
std::optional<int> height;
|
|
std::optional<float> strength;
|
|
std::optional<int> clip_skip;
|
|
bool automatic_scheduler = false;
|
|
};
|
|
|
|
struct ImgGenJobRequest {
|
|
SDGenerationParams gen_params;
|
|
std::string output_format = "png";
|
|
int output_compression = 100;
|
|
std::string preview_mode = "none";
|
|
int preview_interval = 1;
|
|
bool adetailer_enabled = true;
|
|
std::vector<ServerADetailerPass> adetailer_passes;
|
|
|
|
SDOptPtr to_image_opt() { return gen_params.to_image_opt(); }
|
|
};
|
|
|
|
class PromptImageCallbackGuard {
|
|
sd_ctx_t* ctx_;
|
|
const SDGenerationParams& params_;
|
|
SDTensorPtr image_tensor_;
|
|
|
|
static bool load_image(const char* source, void* data, const sd_tensor_t** image_out);
|
|
|
|
public:
|
|
PromptImageCallbackGuard(sd_ctx_t* ctx, const SDGenerationParams& params);
|
|
~PromptImageCallbackGuard();
|
|
PromptImageCallbackGuard(const PromptImageCallbackGuard&) = delete;
|
|
PromptImageCallbackGuard& operator=(const PromptImageCallbackGuard&) = delete;
|
|
};
|
|
|
|
constexpr int k_max_generation_dimension = 4096;
|
|
constexpr int k_max_adetailer_steps = 100;
|
|
|
|
bool validate_server_img_gen_request(const ServerRuntime& runtime,
|
|
const ImgGenJobRequest& request,
|
|
std::string& error_message);
|
|
|
|
struct VidGenJobRequest {
|
|
SDGenerationParams gen_params;
|
|
std::string output_format = "webm";
|
|
int output_compression = 100;
|
|
std::string preview_mode = "none";
|
|
int preview_interval = 1;
|
|
|
|
SDOptPtr to_video_opt() { return gen_params.to_video_opt(); }
|
|
};
|
|
|
|
std::string base64_encode(const std::vector<uint8_t>& bytes);
|
|
std::string base64_encode(const void* bytes, size_t size);
|
|
std::string normalize_output_format(std::string output_format);
|
|
std::vector<std::string> supported_img_output_formats(bool allow_webp = true);
|
|
std::vector<std::string> supported_vid_output_formats();
|
|
bool assign_output_options(ImgGenJobRequest& request,
|
|
std::string output_format,
|
|
int output_compression,
|
|
bool allow_webp,
|
|
std::string& error_message);
|
|
bool assign_output_options(VidGenJobRequest& request,
|
|
std::string output_format,
|
|
int output_compression,
|
|
std::string& error_message);
|
|
std::string video_mime_type(const std::string& output_format);
|
|
bool runtime_supports_generation_mode(const ServerRuntime& runtime, SDMode mode);
|
|
bool has_current_model(const ServerRuntime& runtime);
|
|
// Decides where the contexts may read and write files; apply_server_file_access() sets it on each context.
|
|
void configure_server_file_access(ServerModels& models, const SDSvrParams& params, const SDContextParams& config);
|
|
bool apply_server_file_access(ServerModels& models, sd_ctx_t* ctx);
|
|
bool generate_server_images(sd_ctx_t* ctx,
|
|
ImgGenJobRequest& request,
|
|
SDImageVec& results,
|
|
std::string& error_message,
|
|
bool force_vae_tiling = false);
|
|
std::string unsupported_generation_mode_error(const ServerRuntime& runtime, SDMode mode);
|
|
void refresh_lora_cache(ServerRuntime& rt);
|
|
std::string get_lora_full_path(ServerRuntime& rt, const std::string& path);
|
|
void refresh_upscaler_cache(ServerRuntime& rt);
|
|
int64_t unix_timestamp_now_ms();
|