mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-11-28 19:16:33 +00:00
Brings in the 73 upstream commits since b13a482 (2026-10-20 to 2026-11-05):
cancellation of running server jobs (#2190), recipe based model management
and idempotent model loading (#2176, #2188, #2217, #2221), ADetailer in the
server (#2164), generation metadata returned with the core results (#2206,
which changes generate_image and generate_video), integrated GPUs in the
automatic memory planning (#2228), discrete GPUs preferred in auto-fit
(#2259), Vulkan capacity checks retried after reclaiming memory (#2256),
the layer split fix across CUDA devices (#2262), RPC devices in
--list-devices (#2182), new models and many speed-ups (FLUX.3, Fibo 2.0,
MiniMax-H3, P-H3-Image, MOSS, Z-Image control-net, INT8 CUDA fusions),
Node 24 and ccache in the workflows, a CUTLASS submodule for the CUDA build,
and the ggml sync to 135ce1e.
Conflicts resolved by hand in 25 files, keeping both sides:
- include/stable-diffusion.h, src/stable-diffusion.cpp,
src/core/ggml_runner.cpp, src/pipeline/diffusion_engine.cpp,
src/pipeline/image.cpp: upstream's result API stays (generate_image and
generate_video fill a result out-parameter, free_sd_image_generation_result
releases it); the fork's classified errors, exception barrier and busy
check wrap it, so a failed call still records its reason and clears the
result. memory_guard, disable_backend_fallback and multi_gpu follow
upstream's last sd_ctx_params_t field. A stop on request is logged as
information, not as an error.
- src/core/backend_fit.cpp: integrated GPUs are enumerated as upstream now
does; which device becomes the main one is still decided by the fork's
rules (discrete before integrated, a card seen by both CUDA and Vulkan
counts once), which cover the case of #2259 as well.
- src/core/ggml_extend_backend.cpp, CMakeLists.txt: the NVIDIA driver is
still loaded at run time (sd::cuda_driver; the toolkit provides headers
only), so upstream's cuda_compiled_arch asks the compute capability
through it and not through <cuda.h>; the CUTLASS include of upstream
stays.
- src/model_manager.cpp: the CPU memory guard and upstream's capacity
rechecks (#2183, #2256) both apply.
- examples/server (runtime, main, async_jobs, routes_*): the context keeper
is still the only owner of the context, and ServerRuntime has no context
or mutex of its own. Upstream's model catalog (--model-dir, the
/sdcpp/v1/models routes, recipes) asks the keeper to load and unload
(ContextKeeper::load, unload, has_model), so a model loaded from the
catalog gets the same recovery ladder as the one given with -m, built from
the options of the new model. ADetailer runs with the context and the
options of the profile in use. The server still starts without a model.
- Cancellation: every job has a CancelToken that the keeper registers only
while the job generates. POST /sdcpp/v1/jobs/{id}/cancel answers at once
when called without ?wait=, as upstream does (the job can still be
generating, with cancel_requested true); with ?wait=N it waits for the end
of the job and answers 200, or 202 if it is still ending, which is what
the fork's guardian expects. cancel_requested is always in the job JSON.
- examples/common/common.{h,cpp}: upstream's --list-devices with RPC devices
and --taesd-preview-only next to the fork's --profile, --memory-guard,
--multi-gpu and --disable-backend-fallback.
- .github/workflows/build.yml: upstream's Node 24, ccache and CUDA header
triggers next to the fork's CPU Docker variant, lowercase image names,
combined CUDA and Vulkan Windows zip and Italian release notes.
examples/cli and examples/server CMakeLists: profile.cpp and recovery.cpp
are added to upstream's source lists.
- docs/backend.md, docs/build.md, examples/server/api.md: the sections of
both sides are kept. The paragraph on how a layer split assigns blocks is
the fork's, which is what layer_split_partition.cpp does (the first device
takes blocks until its free memory is used up), not upstream's
"proportional" wording.
The ggml submodule moves from dc99877 to 135ce1e, the frontend submodule to
fa93429 and the new thirdparty/cutlass submodule is added, all at upstream's
pins. A checkout needs git submodule update --init --recursive.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC
963 lines
41 KiB
C++
963 lines
41 KiB
C++
#include "routes.h"
|
|
|
|
#include <algorithm>
|
|
#include <cctype>
|
|
#include <chrono>
|
|
#include <cmath>
|
|
#include <filesystem>
|
|
#include <limits>
|
|
#include <memory>
|
|
#include <new>
|
|
|
|
#include "async_jobs.h"
|
|
#include "common/common.h"
|
|
#include "common/media_io.h"
|
|
#include "common/resource_owners.hpp"
|
|
#include "model_catalog.h"
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
static constexpr uint32_t k_max_upscale_dimension = 8192;
|
|
|
|
static constexpr int k_max_cancel_wait_seconds = 60;
|
|
|
|
// Reads ?wait=: how long a cancellation may wait for the job to end. Longer waits are shortened to the maximum.
|
|
static bool parse_cancel_wait(const std::string& text, int& seconds) {
|
|
if (text.empty() || text.size() > 9 || !std::all_of(text.begin(), text.end(), [](unsigned char c) { return std::isdigit(c) != 0; })) {
|
|
return false;
|
|
}
|
|
seconds = std::min(std::stoi(text), k_max_cancel_wait_seconds);
|
|
return true;
|
|
}
|
|
|
|
static bool valid_upscale_dimensions(const sd_image_t& image, int factor, int repeats) {
|
|
if (image.width == 0 || image.height == 0 || factor < 1 || repeats < 1 || repeats > 4) {
|
|
return false;
|
|
}
|
|
uint32_t width = image.width;
|
|
uint32_t height = image.height;
|
|
for (int i = 0; i < repeats; ++i) {
|
|
if (width > k_max_upscale_dimension / factor || height > k_max_upscale_dimension / factor) {
|
|
return false;
|
|
}
|
|
width *= factor;
|
|
height *= factor;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static bool parse_cache_mode(const std::string& mode_str, sd_cache_mode_t& mode_out) {
|
|
if (mode_str == "disabled") {
|
|
mode_out = SD_CACHE_DISABLED;
|
|
return true;
|
|
}
|
|
if (mode_str == "easycache") {
|
|
mode_out = SD_CACHE_EASYCACHE;
|
|
return true;
|
|
}
|
|
if (mode_str == "ucache") {
|
|
mode_out = SD_CACHE_UCACHE;
|
|
return true;
|
|
}
|
|
if (mode_str == "dbcache") {
|
|
mode_out = SD_CACHE_DBCACHE;
|
|
return true;
|
|
}
|
|
if (mode_str == "taylorseer") {
|
|
mode_out = SD_CACHE_TAYLORSEER;
|
|
return true;
|
|
}
|
|
if (mode_str == "cache-dit") {
|
|
mode_out = SD_CACHE_CACHE_DIT;
|
|
return true;
|
|
}
|
|
if (mode_str == "spectrum") {
|
|
mode_out = SD_CACHE_SPECTRUM;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static json finite_number_or_null(float value) {
|
|
return std::isfinite(value) ? json(value) : json(nullptr);
|
|
}
|
|
|
|
static const char* capability_scheduler_name(enum scheduler_t scheduler) {
|
|
return scheduler < SCHEDULER_COUNT ? sd_scheduler_name(scheduler) : "default";
|
|
}
|
|
|
|
static const char* capability_sample_method_name(enum sample_method_t sample_method) {
|
|
return sample_method < SAMPLE_METHOD_COUNT ? sd_sample_method_name(sample_method) : "default";
|
|
}
|
|
|
|
static json make_vae_tiling_json(const sd_tiling_params_t& params) {
|
|
return {
|
|
{"enabled", params.enabled},
|
|
{"temporal_tiling", params.temporal_tiling},
|
|
{"tile_size_w", params.tile_size_w},
|
|
{"tile_size_h", params.tile_size_h},
|
|
{"target_overlap", params.target_overlap},
|
|
{"rel_size_w", params.rel_size_w},
|
|
{"rel_size_h", params.rel_size_h},
|
|
{"extra_tiling_args", params.extra_tiling_args ? params.extra_tiling_args : ""},
|
|
};
|
|
}
|
|
|
|
static fs::path resolve_display_model_path(const ServerRuntime& runtime) {
|
|
const auto& ctx = *runtime.ctx_params;
|
|
if (!ctx.model_path.empty()) {
|
|
return fs::u8path(ctx.model_path);
|
|
}
|
|
if (!ctx.diffusion_model_path.empty()) {
|
|
return fs::u8path(ctx.diffusion_model_path);
|
|
}
|
|
return {};
|
|
}
|
|
|
|
static json make_sample_params_json(const sd_sample_params_t& sample_params, const std::vector<int>& skip_layers, const std::vector<float>& custom_sigmas = {}) {
|
|
const auto& guidance = sample_params.guidance;
|
|
return {
|
|
{"scheduler", capability_scheduler_name(sample_params.scheduler)},
|
|
{"sample_method", capability_sample_method_name(sample_params.sample_method)},
|
|
{"sample_steps", sample_params.sample_steps},
|
|
{"custom_sigmas", custom_sigmas},
|
|
{"eta", finite_number_or_null(sample_params.eta)},
|
|
{"shifted_timestep", sample_params.shifted_timestep},
|
|
{"flow_shift", finite_number_or_null(sample_params.flow_shift)},
|
|
{"guidance",
|
|
{
|
|
{"txt_cfg", guidance.txt_cfg},
|
|
{"img_cfg", finite_number_or_null(guidance.img_cfg)},
|
|
{"distilled_guidance", guidance.distilled_guidance},
|
|
{"slg",
|
|
{
|
|
{"layers", skip_layers},
|
|
{"layer_start", guidance.slg.layer_start},
|
|
{"layer_end", guidance.slg.layer_end},
|
|
{"scale", guidance.slg.scale},
|
|
}},
|
|
}},
|
|
};
|
|
}
|
|
|
|
static json make_hires_json(const SDGenerationParams& defaults) {
|
|
return {
|
|
{"enabled", defaults.hires_enabled},
|
|
{"upscaler", defaults.hires_upscaler},
|
|
{"scale", defaults.hires_scale},
|
|
{"target_width", defaults.hires_width},
|
|
{"target_height", defaults.hires_height},
|
|
{"steps", defaults.hires_steps},
|
|
{"denoising_strength", defaults.hires_denoising_strength},
|
|
{"custom_sigmas", defaults.hires_custom_sigmas},
|
|
{"upscale_tile_size", defaults.hires_upscale_tile_size},
|
|
};
|
|
}
|
|
|
|
static json make_img_gen_defaults_json(const SDGenerationParams& defaults, const std::string& output_format) {
|
|
return {
|
|
{"prompt", defaults.prompt},
|
|
{"negative_prompt", defaults.negative_prompt},
|
|
{"clip_skip", defaults.clip_skip},
|
|
{"width", defaults.width > 0 ? defaults.width : 512},
|
|
{"height", defaults.height > 0 ? defaults.height : 512},
|
|
{"strength", defaults.strength},
|
|
{"seed", defaults.seed},
|
|
{"batch_count", defaults.batch_count},
|
|
{"qwen_image_layers", defaults.qwen_image_layers},
|
|
{"ref_image_args", defaults.ref_image_args},
|
|
{"image_preprocess", defaults.image_preprocess},
|
|
{"increase_ref_index", defaults.increase_ref_index},
|
|
{"control_strength", defaults.control_strength},
|
|
{"ip_adapter_strength", defaults.ip_adapter_strength},
|
|
{"sample_params", make_sample_params_json(defaults.sample_params, defaults.skip_layers, defaults.custom_sigmas)},
|
|
{"hires", make_hires_json(defaults)},
|
|
{"vae_tiling_params", make_vae_tiling_json(defaults.vae_tiling_params)},
|
|
{"cache_mode", defaults.cache_mode},
|
|
{"cache_option", defaults.cache_option},
|
|
{"scm_mask", defaults.scm_mask},
|
|
{"scm_policy_dynamic", defaults.scm_policy_dynamic},
|
|
{"output_format", output_format},
|
|
{"output_compression", 100},
|
|
};
|
|
}
|
|
|
|
static json make_vid_gen_defaults_json(const SDGenerationParams& defaults, const std::string& output_format) {
|
|
return {
|
|
{"prompt", defaults.prompt},
|
|
{"negative_prompt", defaults.negative_prompt},
|
|
{"clip_skip", defaults.clip_skip},
|
|
{"width", defaults.width > 0 ? defaults.width : 512},
|
|
{"height", defaults.height > 0 ? defaults.height : 512},
|
|
{"strength", defaults.strength},
|
|
{"seed", defaults.seed},
|
|
{"video_frames", defaults.video_frames},
|
|
{"image_preprocess", defaults.image_preprocess},
|
|
{"ref_image_args", defaults.ref_image_args},
|
|
{"fps", defaults.fps},
|
|
{"moe_boundary", defaults.moe_boundary},
|
|
{"vace_strength", defaults.vace_strength},
|
|
{"sample_params", make_sample_params_json(defaults.sample_params, defaults.skip_layers, defaults.custom_sigmas)},
|
|
{"high_noise_sample_params", make_sample_params_json(defaults.high_noise_sample_params, defaults.high_noise_skip_layers)},
|
|
{"hires", make_hires_json(defaults)},
|
|
{"vae_tiling_params", make_vae_tiling_json(defaults.vae_tiling_params)},
|
|
{"cache_mode", defaults.cache_mode},
|
|
{"cache_option", defaults.cache_option},
|
|
{"scm_mask", defaults.scm_mask},
|
|
{"scm_policy_dynamic", defaults.scm_policy_dynamic},
|
|
{"output_format", output_format},
|
|
{"output_compression", 100},
|
|
};
|
|
}
|
|
|
|
static json make_img_gen_features_json() {
|
|
return {
|
|
{"init_image", true},
|
|
{"mask_image", true},
|
|
{"control_image", true},
|
|
{"ip_adapter_image", true},
|
|
{"ref_images", true},
|
|
{"lora", true},
|
|
{"vae_tiling", true},
|
|
{"hires", true},
|
|
{"cache", true},
|
|
{"cancel_queued", true},
|
|
{"cancel_generating", true},
|
|
{"preview", true},
|
|
};
|
|
}
|
|
|
|
/** Builds the default feature set advertised for vid_gen requests. */
|
|
static json make_vid_gen_features_json() {
|
|
return {
|
|
{"init_image", true},
|
|
{"end_image", true},
|
|
{"control_frames", true},
|
|
{"ref_videos", true},
|
|
{"ref_audios", true},
|
|
{"ref_video_audios", true},
|
|
{"high_noise_sample_params", true},
|
|
{"lora", true},
|
|
{"vae_tiling", true},
|
|
{"cache", true},
|
|
{"cancel_queued", true},
|
|
{"cancel_generating", true},
|
|
{"preview", true},
|
|
};
|
|
}
|
|
|
|
static json make_preview_modes_json() {
|
|
json modes = json::array();
|
|
for (int i = 0; i < PREVIEW_COUNT; ++i) {
|
|
modes.push_back(sd_preview_name((preview_t)i));
|
|
}
|
|
return modes;
|
|
}
|
|
|
|
/** Builds the /capabilities payload: model, defaults, limits and feature maps. */
|
|
static json make_capabilities_json(ServerRuntime& runtime) {
|
|
refresh_lora_cache(runtime);
|
|
refresh_upscaler_cache(runtime);
|
|
|
|
AsyncJobManager& manager = *runtime.async_job_manager;
|
|
const auto& defaults = *runtime.default_gen_params;
|
|
const fs::path model_path = resolve_display_model_path(runtime);
|
|
const bool supports_img = runtime_supports_generation_mode(runtime, IMG_GEN);
|
|
const bool supports_vid = runtime_supports_generation_mode(runtime, VID_GEN);
|
|
json samplers = json::array();
|
|
json schedulers = json::array();
|
|
json image_output_formats = supported_img_output_formats();
|
|
json video_output_formats = supported_vid_output_formats();
|
|
json available_loras = json::array();
|
|
json available_upscalers = json::array();
|
|
json supported_modes = json::array();
|
|
|
|
for (int i = 0; i < SAMPLE_METHOD_COUNT; ++i) {
|
|
samplers.push_back(sd_sample_method_name((sample_method_t)i));
|
|
}
|
|
|
|
for (int i = 0; i < SCHEDULER_COUNT; ++i) {
|
|
schedulers.push_back(sd_scheduler_name((scheduler_t)i));
|
|
if (i == DISCRETE_SCHEDULER) {
|
|
schedulers.push_back("normal");
|
|
}
|
|
}
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock(*runtime.lora_mutex);
|
|
for (const auto& entry : *runtime.lora_cache) {
|
|
available_loras.push_back({
|
|
{"name", entry.name},
|
|
{"path", entry.path},
|
|
{"display_path", entry.display_path},
|
|
});
|
|
}
|
|
}
|
|
|
|
available_upscalers.push_back({
|
|
{"name", "None"},
|
|
{"model", false},
|
|
{"image_upscale", false},
|
|
});
|
|
available_upscalers.push_back({
|
|
{"name", "Lanczos"},
|
|
{"model", false},
|
|
{"image_upscale", false},
|
|
});
|
|
available_upscalers.push_back({
|
|
{"name", "Nearest"},
|
|
{"model", false},
|
|
{"image_upscale", false},
|
|
});
|
|
available_upscalers.push_back({
|
|
{"name", "Latent"},
|
|
{"model", false},
|
|
{"image_upscale", false},
|
|
});
|
|
available_upscalers.push_back({
|
|
{"name", "Latent (nearest)"},
|
|
{"model", false},
|
|
{"image_upscale", false},
|
|
});
|
|
available_upscalers.push_back({
|
|
{"name", "Latent (nearest-exact)"},
|
|
{"model", false},
|
|
{"image_upscale", false},
|
|
});
|
|
available_upscalers.push_back({
|
|
{"name", "Latent (antialiased)"},
|
|
{"model", false},
|
|
{"image_upscale", false},
|
|
});
|
|
available_upscalers.push_back({
|
|
{"name", "Latent (bicubic)"},
|
|
{"model", false},
|
|
{"image_upscale", false},
|
|
});
|
|
available_upscalers.push_back({
|
|
{"name", "Latent (bicubic antialiased)"},
|
|
{"model", false},
|
|
{"image_upscale", false},
|
|
});
|
|
bool have_upscaler_models = false;
|
|
{
|
|
std::lock_guard<std::mutex> lock(*runtime.upscaler_mutex);
|
|
for (const auto& entry : *runtime.upscaler_cache) {
|
|
available_upscalers.push_back({
|
|
{"name", entry.name},
|
|
{"model", true},
|
|
{"image_upscale", entry.image_upscale_factor > 0},
|
|
});
|
|
have_upscaler_models = have_upscaler_models || entry.image_upscale_factor > 0;
|
|
}
|
|
}
|
|
|
|
if (supports_img) {
|
|
supported_modes.push_back("img_gen");
|
|
}
|
|
if (supports_vid) {
|
|
supported_modes.push_back("vid_gen");
|
|
}
|
|
|
|
std::string default_img_output_format = "png";
|
|
std::string default_vid_output_format = "avi";
|
|
if (!image_output_formats.empty()) {
|
|
default_img_output_format = image_output_formats[0].get<std::string>();
|
|
}
|
|
if (!video_output_formats.empty()) {
|
|
default_vid_output_format = video_output_formats[0].get<std::string>();
|
|
}
|
|
|
|
const json generation_features = model_generation_features(runtime);
|
|
auto recipe_features = [&](json features, bool video) {
|
|
features["init_image"] = generation_features.at(video ? "init_image" : "img2img");
|
|
for (const char* key : {"mask_image", "ref_images", "end_image", "control_frames", "ref_videos", "ref_audios", "ref_video_audios", "lora", "vae_tiling", "cache", "preview"}) {
|
|
if (features.contains(key) || std::string(key) == "ref_images" ||
|
|
(video && (std::string(key) == "ref_videos" || std::string(key) == "ref_audios" ||
|
|
std::string(key) == "ref_video_audios")))
|
|
features[key] = generation_features.at(key);
|
|
}
|
|
if (video)
|
|
features["high_noise_sample_params"] = generation_features.at("high_noise");
|
|
else {
|
|
features["mask_image"] = generation_features.at("mask_image").get<bool>() && generation_features.at("img2img").get<bool>();
|
|
features["control_image"] = generation_features.at("controlnet").get<bool>() || generation_features.at("control_image").get<bool>();
|
|
features["ip_adapter_image"] = generation_features.at("ip_adapter");
|
|
}
|
|
return features;
|
|
};
|
|
json defaults_by_mode = json::object();
|
|
json output_formats_by_mode = json::object();
|
|
json features_by_mode = json::object();
|
|
if (supports_img) {
|
|
defaults_by_mode["img_gen"] = make_img_gen_defaults_json(defaults, default_img_output_format);
|
|
output_formats_by_mode["img_gen"] = image_output_formats;
|
|
features_by_mode["img_gen"] = recipe_features(make_img_gen_features_json(), false);
|
|
}
|
|
if (supports_vid) {
|
|
defaults_by_mode["vid_gen"] = make_vid_gen_defaults_json(defaults, default_vid_output_format);
|
|
output_formats_by_mode["vid_gen"] = video_output_formats;
|
|
features_by_mode["vid_gen"] = recipe_features(make_vid_gen_features_json(), true);
|
|
}
|
|
|
|
json top_level_defaults = json::object();
|
|
json top_level_output_formats = json::array();
|
|
json top_level_features = {
|
|
{"cancel_queued", true},
|
|
{"cancel_generating", true},
|
|
};
|
|
std::string current_mode = "";
|
|
if (supports_img) {
|
|
current_mode = "img_gen";
|
|
top_level_defaults = defaults_by_mode["img_gen"];
|
|
top_level_output_formats = output_formats_by_mode["img_gen"];
|
|
top_level_features = features_by_mode["img_gen"];
|
|
} else if (supports_vid) {
|
|
current_mode = "vid_gen";
|
|
top_level_defaults = defaults_by_mode["vid_gen"];
|
|
top_level_output_formats = output_formats_by_mode["vid_gen"];
|
|
top_level_features = features_by_mode["vid_gen"];
|
|
}
|
|
|
|
json result;
|
|
result["model"] = {
|
|
{"name", model_path.filename().u8string()},
|
|
{"stem", model_path.stem().u8string()},
|
|
{"path", model_path.u8string()},
|
|
};
|
|
result["generation_features"] = generation_features;
|
|
result["runtime"] = model_status(runtime);
|
|
result["current_mode"] = current_mode;
|
|
result["supported_modes"] = supported_modes;
|
|
result["defaults"] = top_level_defaults;
|
|
result["defaults_by_mode"] = defaults_by_mode;
|
|
result["limits"] = {
|
|
{"min_width", 64},
|
|
{"max_width", k_max_generation_dimension},
|
|
{"min_height", 64},
|
|
{"max_height", k_max_generation_dimension},
|
|
{"max_batch_count", 8},
|
|
{"max_queue_size", manager.max_pending_jobs},
|
|
{"max_upscale_width", k_max_upscale_dimension},
|
|
{"max_upscale_height", k_max_upscale_dimension},
|
|
};
|
|
result["samplers"] = samplers;
|
|
result["schedulers"] = schedulers;
|
|
result["output_formats"] = top_level_output_formats;
|
|
result["output_formats_by_mode"] = output_formats_by_mode;
|
|
result["features"] = top_level_features;
|
|
result["features_by_mode"] = features_by_mode;
|
|
result["loras"] = available_loras;
|
|
result["upscalers"] = available_upscalers;
|
|
result["upscale"] = have_upscaler_models;
|
|
result["preview_modes"] = make_preview_modes_json();
|
|
return result;
|
|
}
|
|
|
|
static bool parse_img_gen_request(const json& body,
|
|
ServerRuntime& runtime,
|
|
ImgGenJobRequest& request,
|
|
std::string& error_message) {
|
|
request.gen_params = *runtime.default_gen_params;
|
|
|
|
refresh_lora_cache(runtime);
|
|
if (!request.gen_params.from_json_str(body.dump(), [&](const std::string& path) {
|
|
return get_lora_full_path(runtime, path);
|
|
})) {
|
|
error_message = "invalid generation parameters";
|
|
return false;
|
|
}
|
|
|
|
std::string output_format = body.value("output_format", "png");
|
|
int output_compression = body.value("output_compression", 100);
|
|
if (!assign_output_options(request, output_format, output_compression, true, error_message)) {
|
|
return false;
|
|
}
|
|
request.preview_mode = body.value("preview", std::string("none"));
|
|
request.preview_interval = body.value("preview_interval", 1);
|
|
// Intentionally disable prompt-embedded LoRA tag parsing for server APIs.
|
|
if (!request.gen_params.resolve_and_validate(IMG_GEN, "", runtime.ctx_params->hires_upscalers_dir, true)) {
|
|
error_message = "invalid generation parameters";
|
|
return false;
|
|
}
|
|
return validate_server_img_gen_request(runtime, request, error_message);
|
|
}
|
|
|
|
static bool parse_vid_gen_request(const json& body,
|
|
ServerRuntime& runtime,
|
|
VidGenJobRequest& request,
|
|
std::string& error_message) {
|
|
request.gen_params = *runtime.default_gen_params;
|
|
|
|
refresh_lora_cache(runtime);
|
|
if (!request.gen_params.from_json_str(body.dump(), [&](const std::string& path) {
|
|
return get_lora_full_path(runtime, path);
|
|
})) {
|
|
error_message = "invalid generation parameters";
|
|
return false;
|
|
}
|
|
|
|
std::string output_format = body.value("output_format", "webm");
|
|
int output_compression = body.value("output_compression", 100);
|
|
if (!assign_output_options(request, output_format, output_compression, error_message)) {
|
|
return false;
|
|
}
|
|
request.preview_mode = body.value("preview", std::string("none"));
|
|
request.preview_interval = body.value("preview_interval", 1);
|
|
// Intentionally disable prompt-embedded LoRA tag parsing for server APIs.
|
|
if (!request.gen_params.resolve_and_validate(VID_GEN, "", runtime.ctx_params->hires_upscalers_dir, true)) {
|
|
error_message = "invalid generation parameters";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void register_sdcpp_api_endpoints(httplib::Server& svr, ServerRuntime& rt) {
|
|
ServerRuntime* runtime = &rt;
|
|
RuntimeRoutes routes(svr, rt);
|
|
|
|
routes.get("/sdcpp/v1/capabilities", [runtime](const httplib::Request&, httplib::Response& res) {
|
|
res.status = 200;
|
|
res.set_content(make_capabilities_json(*runtime).dump(), "application/json");
|
|
});
|
|
|
|
// Health and resources stay outside RuntimeRoutes: they must answer while a model is loading or the server is
|
|
// recovering, which is when a supervisor needs them most.
|
|
svr.Get("/sdcpp/v1/health", [runtime](const httplib::Request&, httplib::Response& res) {
|
|
json body = runtime->keeper->health_json();
|
|
{
|
|
AsyncJobManager& manager = *runtime->async_job_manager;
|
|
std::lock_guard<std::mutex> lock(manager.mutex);
|
|
body["queue"] = {
|
|
{"pending", count_pending_jobs(manager)},
|
|
{"waiting", manager.queue.size()},
|
|
{"max", manager.max_pending_jobs},
|
|
};
|
|
}
|
|
res.status = body.value("status", std::string()) == "unavailable" ? 503 : 200;
|
|
res.set_content(safe_json_dump(body), "application/json");
|
|
});
|
|
|
|
svr.Get("/sdcpp/v1/resources", [runtime](const httplib::Request&, httplib::Response& res) {
|
|
json body;
|
|
sd_system_memory_t memory;
|
|
if (sd_get_system_memory(&memory)) {
|
|
body["system_memory"] = {
|
|
{"total_bytes", memory.total_bytes},
|
|
{"available_bytes", memory.available_bytes},
|
|
{"process_bytes", memory.process_bytes},
|
|
};
|
|
} else {
|
|
body["system_memory"] = nullptr;
|
|
}
|
|
|
|
json devices = json::array();
|
|
for (size_t i = 0; i < sd_get_device_count(); ++i) {
|
|
sd_device_info_t info;
|
|
if (!sd_get_device_info(i, &info)) {
|
|
continue;
|
|
}
|
|
devices.push_back({
|
|
{"name", info.name},
|
|
{"description", info.description},
|
|
{"type", sd_device_type_name(info.type)},
|
|
{"free_memory_bytes", info.free_memory_bytes},
|
|
{"total_memory_bytes", info.total_memory_bytes},
|
|
});
|
|
}
|
|
body["devices"] = std::move(devices);
|
|
res.status = 200;
|
|
res.set_content(safe_json_dump(body), "application/json");
|
|
});
|
|
|
|
routes.post("/sdcpp/v1/upscale", [runtime](const httplib::Request& req, httplib::Response& res) {
|
|
try {
|
|
if (req.body.empty()) {
|
|
res.status = 400;
|
|
res.set_content(R"({"error":"empty body"})", "application/json");
|
|
return;
|
|
}
|
|
json body = json::parse(req.body);
|
|
if (!body.is_object()) {
|
|
res.status = 400;
|
|
res.set_content(R"({"error":"body must be an object"})", "application/json");
|
|
return;
|
|
}
|
|
for (const char* key : {"repeats", "tile_size", "output_compression"}) {
|
|
if (!body.contains(key)) {
|
|
continue;
|
|
}
|
|
const auto& value = body[key];
|
|
const bool valid = value.is_number_unsigned()
|
|
? value.get<uint64_t>() <= static_cast<uint64_t>(std::numeric_limits<int>::max())
|
|
: value.is_number_integer() && value.get<int64_t>() >= std::numeric_limits<int>::min() &&
|
|
value.get<int64_t>() <= std::numeric_limits<int>::max();
|
|
if (!valid) {
|
|
res.status = 400;
|
|
res.set_content(json({{"error", std::string(key) + " must be a 32-bit integer"}}).dump(), "application/json");
|
|
return;
|
|
}
|
|
}
|
|
ImgGenJobRequest output_options;
|
|
std::string error_message;
|
|
if (!assign_output_options(output_options,
|
|
body.value("output_format", std::string("png")),
|
|
body.value("output_compression", 100),
|
|
true,
|
|
error_message)) {
|
|
res.status = 400;
|
|
res.set_content(json({{"error", error_message}}).dump(), "application/json");
|
|
return;
|
|
}
|
|
const int tile_size = std::max(32, body.value("tile_size", runtime->default_gen_params->upscale_tile_size));
|
|
const int repeats = std::clamp(body.value("repeats", 1), 1, 4);
|
|
const std::string wanted = body.value("upscaler", std::string());
|
|
|
|
const std::string encoded = body.value("image", std::string());
|
|
if (encoded.empty()) {
|
|
res.status = 400;
|
|
res.set_content(R"({"error":"image is required"})", "application/json");
|
|
return;
|
|
}
|
|
SDImageOwner input;
|
|
if (!decode_base64_image(encoded, 3, 0, 0, input) || input.get().data == nullptr) {
|
|
res.status = 400;
|
|
res.set_content(R"({"error":"image could not be read"})", "application/json");
|
|
return;
|
|
}
|
|
|
|
refresh_upscaler_cache(*runtime);
|
|
int model_scale = 0;
|
|
std::string model_path;
|
|
std::string used_name;
|
|
{
|
|
std::lock_guard<std::mutex> lock(*runtime->upscaler_mutex);
|
|
for (const auto& entry : *runtime->upscaler_cache) {
|
|
if (entry.image_upscale_factor > 0 && (wanted.empty() || entry.name == wanted)) {
|
|
model_path = entry.fullpath;
|
|
used_name = entry.name;
|
|
model_scale = entry.image_upscale_factor;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (model_path.empty()) {
|
|
res.status = 400;
|
|
res.set_content(json({{"error", wanted.empty()
|
|
? std::string("no RGB ESRGAN upscaler models are available; "
|
|
"start the server with --hires-upscalers-dir")
|
|
: "no compatible image upscaler called " + wanted}})
|
|
.dump(),
|
|
"application/json");
|
|
return;
|
|
}
|
|
|
|
if (!valid_upscale_dimensions(input.get(), model_scale, repeats)) {
|
|
res.status = 400;
|
|
res.set_content(R"({"error":"upscaled dimensions must not exceed 8192 x 8192"})", "application/json");
|
|
return;
|
|
}
|
|
|
|
// One GPU: an upscale must not run while a generation is using it. The upscaler follows the
|
|
// settings the server is running with, which are lighter after it stepped down.
|
|
std::lock_guard<std::mutex> ctx_lock(runtime->keeper->mutex());
|
|
const ContextKeeper::BackendChoice choice = runtime->keeper->backend_choice();
|
|
UpscalerCtxPtr upscaler_ctx(new_upscaler_ctx(model_path.c_str(),
|
|
choice.diffusion_conv_direct,
|
|
choice.n_threads,
|
|
tile_size,
|
|
choice.backend.c_str(),
|
|
choice.params_backend.c_str()));
|
|
if (upscaler_ctx == nullptr) {
|
|
res.status = 500;
|
|
res.set_content(R"({"error":"the upscaler model could not be loaded"})", "application/json");
|
|
return;
|
|
}
|
|
const int factor = get_upscale_factor(upscaler_ctx.get());
|
|
// The model file may have changed since its metadata was cached.
|
|
if (!valid_upscale_dimensions(input.get(), factor, repeats)) {
|
|
res.status = 400;
|
|
res.set_content(R"({"error":"upscaled dimensions must not exceed 8192 x 8192"})", "application/json");
|
|
return;
|
|
}
|
|
|
|
SDImageOwner current(input.release());
|
|
std::string generation_info;
|
|
for (int i = 0; i < repeats; ++i) {
|
|
sd_image_generation_result_t* generated = nullptr;
|
|
if (!upscale(upscaler_ctx.get(), current.get(), (uint32_t)factor, &generated) ||
|
|
!generated || generated->count <= 0 || generated->items[0].image.data == nullptr) {
|
|
free_sd_image_generation_result(generated);
|
|
res.status = 500;
|
|
res.set_content(R"({"error":"upscale failed"})", "application/json");
|
|
return;
|
|
}
|
|
generation_info = merge_generation_info(generation_info, generated->items[0].generation_info);
|
|
sd_image_t produced = generated->items[0].image;
|
|
generated->items[0].image = {};
|
|
free_sd_image_generation_result(generated);
|
|
current.reset(produced);
|
|
}
|
|
|
|
const std::string& format = output_options.output_format;
|
|
const int compression = output_options.output_compression;
|
|
const sd_image_t result = current.get();
|
|
auto image_bytes = encode_image_to_vector(format == "jpeg" ? EncodedImageFormat::JPEG
|
|
: format == "webp" ? EncodedImageFormat::WEBP
|
|
: EncodedImageFormat::PNG,
|
|
result.data,
|
|
result.width,
|
|
result.height,
|
|
result.channel,
|
|
generation_info,
|
|
compression);
|
|
if (image_bytes.empty()) {
|
|
res.status = 500;
|
|
res.set_content(R"({"error":"the result could not be encoded"})", "application/json");
|
|
return;
|
|
}
|
|
|
|
json out;
|
|
out["generation_info"] = json::parse(generation_info);
|
|
out["upscaler"] = used_name;
|
|
out["scale"] = factor;
|
|
out["repeats"] = repeats;
|
|
out["width"] = result.width;
|
|
out["height"] = result.height;
|
|
out["output_format"] = format;
|
|
json images = json::array();
|
|
images.push_back({{"index", 0}, {"b64_json", base64_encode(image_bytes)}});
|
|
out["images"] = std::move(images);
|
|
res.set_content(out.dump(), "application/json");
|
|
res.status = 200;
|
|
} catch (const json::exception& e) {
|
|
res.status = 400;
|
|
res.set_content(json({{"error", "invalid request"}, {"message", e.what()}}).dump(), "application/json");
|
|
} catch (const std::bad_alloc&) {
|
|
send_generation_failure(res, out_of_memory_failure());
|
|
} catch (const std::exception& e) {
|
|
res.status = 500;
|
|
res.set_content(safe_json_dump(json({{"error", std::string("server_error: ") + e.what()}})), "application/json");
|
|
}
|
|
});
|
|
|
|
routes.post("/sdcpp/v1/img_gen", [runtime](const httplib::Request& req, httplib::Response& res) {
|
|
try {
|
|
if (req.body.empty()) {
|
|
res.status = 400;
|
|
res.set_content(R"({"error":"empty body"})", "application/json");
|
|
return;
|
|
}
|
|
if (!runtime_supports_generation_mode(*runtime, IMG_GEN)) {
|
|
res.status = 400;
|
|
res.set_content(json({{"error", unsupported_generation_mode_error(*runtime, IMG_GEN)}}).dump(), "application/json");
|
|
return;
|
|
}
|
|
|
|
json body = json::parse(req.body);
|
|
ImgGenJobRequest request;
|
|
std::string error_message;
|
|
if (!parse_img_gen_request(body, *runtime, request, error_message)) {
|
|
res.status = 400;
|
|
res.set_content(json({{"error", error_message}}).dump(), "application/json");
|
|
return;
|
|
}
|
|
|
|
AsyncJobManager& manager = *runtime->async_job_manager;
|
|
std::shared_ptr<AsyncGenerationJob> job = std::make_shared<AsyncGenerationJob>();
|
|
job->kind = AsyncJobKind::ImgGen;
|
|
job->status = AsyncJobStatus::Queued;
|
|
job->created_at = unix_timestamp_now_ms();
|
|
job->img_gen = std::move(request);
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock(manager.mutex);
|
|
purge_expired_jobs(manager);
|
|
if (count_pending_jobs(manager) >= manager.max_pending_jobs) {
|
|
res.status = 429;
|
|
res.set_header("Retry-After", "10");
|
|
res.set_content(R"({"error":"job queue is full"})", "application/json");
|
|
return;
|
|
}
|
|
job->id = make_async_job_id(manager);
|
|
manager.jobs[job->id] = job;
|
|
manager.queue.push_back(job->id);
|
|
}
|
|
|
|
manager.cv.notify_one();
|
|
|
|
json out;
|
|
out["id"] = job->id;
|
|
out["kind"] = async_job_kind_name(job->kind);
|
|
out["status"] = async_job_status_name(job->status);
|
|
out["created"] = job->created_at;
|
|
out["poll_url"] = "/sdcpp/v1/jobs/" + job->id;
|
|
|
|
res.status = 202;
|
|
res.set_content(out.dump(), "application/json");
|
|
} catch (const json::parse_error& e) {
|
|
res.status = 400;
|
|
res.set_content(json({{"error", "invalid json"}, {"message", e.what()}}).dump(), "application/json");
|
|
} catch (const std::exception& e) {
|
|
res.status = 500;
|
|
res.set_content(json({{"error", "server_error"}, {"message", e.what()}}).dump(), "application/json");
|
|
}
|
|
});
|
|
|
|
routes.post("/sdcpp/v1/vid_gen", [runtime](const httplib::Request& req, httplib::Response& res) {
|
|
try {
|
|
if (req.body.empty()) {
|
|
res.status = 400;
|
|
res.set_content(R"({"error":"empty body"})", "application/json");
|
|
return;
|
|
}
|
|
if (!runtime_supports_generation_mode(*runtime, VID_GEN)) {
|
|
res.status = 400;
|
|
res.set_content(json({{"error", unsupported_generation_mode_error(*runtime, VID_GEN)}}).dump(), "application/json");
|
|
return;
|
|
}
|
|
|
|
json body = json::parse(req.body);
|
|
VidGenJobRequest request;
|
|
std::string error_message;
|
|
if (!parse_vid_gen_request(body, *runtime, request, error_message)) {
|
|
res.status = 400;
|
|
res.set_content(json({{"error", error_message}}).dump(), "application/json");
|
|
return;
|
|
}
|
|
|
|
AsyncJobManager& manager = *runtime->async_job_manager;
|
|
std::shared_ptr<AsyncGenerationJob> job = std::make_shared<AsyncGenerationJob>();
|
|
job->kind = AsyncJobKind::VidGen;
|
|
job->status = AsyncJobStatus::Queued;
|
|
job->created_at = unix_timestamp_now_ms();
|
|
job->vid_gen = std::move(request);
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock(manager.mutex);
|
|
purge_expired_jobs(manager);
|
|
if (count_pending_jobs(manager) >= manager.max_pending_jobs) {
|
|
res.status = 429;
|
|
res.set_header("Retry-After", "10");
|
|
res.set_content(R"({"error":"job queue is full"})", "application/json");
|
|
return;
|
|
}
|
|
job->id = make_async_job_id(manager);
|
|
manager.jobs[job->id] = job;
|
|
manager.queue.push_back(job->id);
|
|
}
|
|
|
|
manager.cv.notify_one();
|
|
|
|
json out;
|
|
out["id"] = job->id;
|
|
out["kind"] = async_job_kind_name(job->kind);
|
|
out["status"] = async_job_status_name(job->status);
|
|
out["created"] = job->created_at;
|
|
out["poll_url"] = "/sdcpp/v1/jobs/" + job->id;
|
|
|
|
res.status = 202;
|
|
res.set_content(out.dump(), "application/json");
|
|
} catch (const json::parse_error& e) {
|
|
res.status = 400;
|
|
res.set_content(json({{"error", "invalid json"}, {"message", e.what()}}).dump(), "application/json");
|
|
} catch (const std::exception& e) {
|
|
res.status = 500;
|
|
res.set_content(json({{"error", "server_error"}, {"message", e.what()}}).dump(), "application/json");
|
|
}
|
|
});
|
|
|
|
routes.get(R"(/sdcpp/v1/jobs/([A-Za-z0-9_\-]+))", [runtime](const httplib::Request& req, httplib::Response& res) {
|
|
AsyncJobManager& manager = *runtime->async_job_manager;
|
|
std::lock_guard<std::mutex> lock(manager.mutex);
|
|
purge_expired_jobs(manager);
|
|
|
|
std::string job_id = req.matches[1];
|
|
auto it = manager.jobs.find(job_id);
|
|
if (it == manager.jobs.end()) {
|
|
if (manager.expired_jobs.find(job_id) != manager.expired_jobs.end()) {
|
|
res.status = 410;
|
|
res.set_content(R"({"error":"job expired"})", "application/json");
|
|
} else {
|
|
res.status = 404;
|
|
res.set_content(R"({"error":"job not found"})", "application/json");
|
|
}
|
|
return;
|
|
}
|
|
|
|
res.status = 200;
|
|
res.set_content(make_async_job_json(manager, *it->second).dump(), "application/json");
|
|
});
|
|
|
|
// Without ?wait= the route answers at once, like the upstream server: the stop is requested and the client
|
|
// polls the job to see it end. With ?wait=N it waits up to N seconds for the job to end and answers 202 if it did not.
|
|
//
|
|
// The route takes the body through a content reader because it has none: a plain handler is called only after the
|
|
// HTTP library has read the body, and for a POST without Content-Length (what `curl -X POST` sends) it waits for
|
|
// its read timeout of 5 seconds to be sure there is none. RuntimeRoutes would not add anything here: it leaves
|
|
// the job routes alone.
|
|
svr.Post(R"(/sdcpp/v1/jobs/([A-Za-z0-9_\-]+)/cancel)", [runtime](const httplib::Request& req, httplib::Response& res, const httplib::ContentReader& body) {
|
|
if (req.has_header("Content-Length") || req.has_header("Transfer-Encoding")) {
|
|
body([](const char*, size_t) { return true; });
|
|
}
|
|
|
|
const bool wait_requested = req.has_param("wait");
|
|
int wait_seconds = 0;
|
|
if (wait_requested && !parse_cancel_wait(req.get_param_value("wait"), wait_seconds)) {
|
|
res.status = 400;
|
|
res.set_content(R"({"error":"wait must be a whole number of seconds, 0 or more"})", "application/json");
|
|
return;
|
|
}
|
|
|
|
AsyncJobManager& manager = *runtime->async_job_manager;
|
|
std::shared_ptr<AsyncGenerationJob> generating;
|
|
std::unique_lock<std::mutex> lock(manager.mutex);
|
|
purge_expired_jobs(manager);
|
|
|
|
std::string job_id = req.matches[1];
|
|
auto it = manager.jobs.find(job_id);
|
|
if (it == manager.jobs.end()) {
|
|
if (manager.expired_jobs.find(job_id) != manager.expired_jobs.end()) {
|
|
res.status = 410;
|
|
res.set_content(R"({"error":"job expired"})", "application/json");
|
|
} else {
|
|
res.status = 404;
|
|
res.set_content(R"({"error":"job not found"})", "application/json");
|
|
}
|
|
return;
|
|
}
|
|
|
|
auto& job = *it->second;
|
|
if (job.status == AsyncJobStatus::Queued) {
|
|
if (!cancel_queued_job(manager, job)) {
|
|
res.status = 409;
|
|
res.set_content(R"({"error":"job queue state changed before cancellation"})", "application/json");
|
|
return;
|
|
}
|
|
res.status = 200;
|
|
res.set_content(make_async_job_json(manager, job).dump(), "application/json");
|
|
return;
|
|
}
|
|
|
|
if (job.status != AsyncJobStatus::Generating) {
|
|
res.status = 200;
|
|
res.set_content(make_async_job_json(manager, job).dump(), "application/json");
|
|
return;
|
|
}
|
|
|
|
if (!wait_requested) {
|
|
runtime->keeper->cancel(job.cancel);
|
|
res.status = 200;
|
|
res.set_content(make_async_job_json(manager, job).dump(), "application/json");
|
|
return;
|
|
}
|
|
|
|
generating = it->second;
|
|
lock.unlock();
|
|
|
|
const bool ended = cancel_generating_job(*runtime, generating, std::chrono::seconds(wait_seconds));
|
|
lock.lock();
|
|
res.status = ended ? 200 : 202;
|
|
res.set_content(make_async_job_json(manager, *generating).dump(), "application/json");
|
|
});
|
|
}
|