stable-diffusion.cpp/src/generation_info.cpp
leejet 532d2c15c4
Some checks failed
CI / Build and push container images (push) Has been cancelled
CI / ubuntu-latest-cmake (push) Has been cancelled
CI / ubuntu-latest-cmake-vulkan (push) Has been cancelled
CI / macOS-latest-cmake (push) Has been cancelled
CI / windows-latest-cmake (cpu, -DGGML_NATIVE=OFF -DSD_BUILD_SHARED_LIBS=ON -DSD_BUILD_SHARED_GGML_LIB=ON -DGGML_BACKEND_DL=ON -DGGML_CPU_ALL_VARIANTS=ON) (push) Has been cancelled
CI / windows-latest-cmake (cuda12, -DSD_CUDA=ON -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_CUDA_COMPILER_LAUNCHER=ccache -DSD_BUILD_SHARED_LIBS=ON -DCMAKE_CUDA_ARCHITECTURES='61;70;75;80;86;89;90;100;120' -DCMAKE_CUDA_FL… (push) Has been cancelled
CI / windows-latest-cmake (vulkan, -DSD_VULKAN=ON -DSD_BUILD_SHARED_LIBS=ON -DGGML_NATIVE=OFF -DSD_BUILD_SHARED_GGML_LIB=ON -DGGML_BACKEND_DL=ON -DGGML_CPU_ALL_VARIANTS=ON) (push) Has been cancelled
CI / windows-latest-rocm (push) Has been cancelled
CI / ubuntu-latest-rocm (10.1.0, x64, gfx900;gfx906;gfx908;gfx90a;gfx90c;gfx942;gfx950;gfx1010;gfx1011;gfx1012;gfx1030;gfx1031;gfx1032;gfx1033;gfx1034;gfx1035;gfx1036;gfx1100;gfx1101;gfx1102;gfx1103;gfx1150;gfx1151;gfx1152;gfx1153;gfx1200;gfx1201) (push) Has been cancelled
CI / release (push) Has been cancelled
refactor(api): adopt opaque options, tensors, and results (#2365)
2026-11-15 18:33:53 +08:00

235 lines
12 KiB
C++

#include "generation_info.h"
#include "config.h"
#include "core/option_types.h"
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include "pipeline/request.h"
#include "runtime/guidance.h"
namespace sd {
using nlohmann::json;
static const char* text(const char* value) {
return value ? value : "";
}
static double shortest_f32_as_double(float value) {
if (!std::isfinite(value)) {
return static_cast<double>(value);
}
char buf[64];
buf[0] = '\0';
for (int prec = 1; prec <= 9; ++prec) {
std::snprintf(buf, sizeof(buf), "%.*g", prec, static_cast<double>(value));
if (std::strtof(buf, nullptr) == value) {
break;
}
}
return std::strtod(buf, nullptr);
}
static json sampling_info(const sd::SampleConfig& p, bool high_noise = false) {
const auto& g = p.guidance;
json info = {{"sample_steps", p.sample_steps}, {"guidance", {{"txt_cfg", g.txt_cfg}, {"distilled_guidance", g.distilled_guidance}}}};
if (!high_noise && p.shifted_timestep > 0)
info["shifted_timestep"] = p.shifted_timestep;
if (std::isfinite(g.img_cfg) && g.img_cfg != 1.f)
info["guidance"]["img_cfg"] = g.img_cfg;
if (p.sample_method != SAMPLE_METHOD_COUNT)
info["sample_method"] = sd_sample_method_name(p.sample_method);
if (!high_noise && p.scheduler != SCHEDULER_COUNT)
info["scheduler"] = sd_scheduler_name(p.scheduler);
if (std::isfinite(p.eta))
info["eta"] = p.eta;
if (!high_noise && std::isfinite(p.flow_shift))
info["flow_shift"] = p.flow_shift;
if (!high_noise && p.custom_sigmas && p.custom_sigmas_count > 0)
info["custom_sigmas"] = std::vector<float>(p.custom_sigmas, p.custom_sigmas + p.custom_sigmas_count);
if (p.extra_sample_args && *p.extra_sample_args)
info["extra_sample_args"] = p.extra_sample_args;
if (g.slg.layers && g.slg.layer_count > 0 &&
(g.slg.scale != 0.f || guidance::parse_skip_layer_guidance_uncond_arg(p.extra_sample_args)))
info["guidance"]["slg"] = {{"scale", g.slg.scale}, {"layer_start", g.slg.layer_start}, {"layer_end", g.slg.layer_end}, {"layers", std::vector<int>(g.slg.layers, g.slg.layers + g.slg.layer_count)}};
return info;
}
static json hires_info(const sd::HiresConfig& p) {
json info = {{"enabled", p.enabled}};
if (!p.enabled)
return info;
info.update({{"upscaler", sd_hires_upscaler_name(p.upscaler)}, {"scale", p.scale}, {"target_width", p.target_width}, {"target_height", p.target_height}, {"steps", p.steps}, {"denoising_strength", p.denoising_strength}});
if (p.upscaler == SD_HIRES_UPSCALER_MODEL) {
info["model_path"] = text(p.model_path);
info["upscale_tile_size"] = p.upscale_tile_size;
}
if (p.custom_sigmas && p.custom_sigmas_count > 0)
info["custom_sigmas"] = std::vector<float>(p.custom_sigmas, p.custom_sigmas + p.custom_sigmas_count);
return info;
}
static json cache_info(const sd::CacheConfig& p) {
static const char* modes[] = {"disabled", "easycache", "ucache", "dbcache", "taylorseer", "cache_dit", "spectrum"};
if (p.mode < SD_CACHE_DISABLED || p.mode > SD_CACHE_SPECTRUM)
return {{"mode", static_cast<int>(p.mode)}};
json info = {{"mode", modes[p.mode]}};
if (p.mode == SD_CACHE_DISABLED)
return info;
if (p.mode == SD_CACHE_EASYCACHE || p.mode == SD_CACHE_UCACHE) {
if (std::isfinite(p.reuse_threshold))
info["reuse_threshold"] = p.reuse_threshold;
info.update({{"start_percent", p.start_percent}, {"end_percent", p.end_percent}});
if (p.mode == SD_CACHE_UCACHE)
info.update({{"error_decay_rate", p.error_decay_rate}, {"use_relative_threshold", p.use_relative_threshold}, {"reset_error_on_compute", p.reset_error_on_compute}});
} else if (p.mode == SD_CACHE_DBCACHE || p.mode == SD_CACHE_TAYLORSEER || p.mode == SD_CACHE_CACHE_DIT) {
// TaylorSeer also uses the shared DBCache thresholds and limits.
info.update({{"Fn_compute_blocks", p.Fn_compute_blocks},
{"Bn_compute_blocks", p.Bn_compute_blocks},
{"residual_diff_threshold", p.residual_diff_threshold},
{"max_warmup_steps", p.max_warmup_steps},
{"max_cached_steps", p.max_cached_steps},
{"max_continuous_cached_steps", p.max_continuous_cached_steps}});
if (p.mode == SD_CACHE_TAYLORSEER || p.mode == SD_CACHE_CACHE_DIT)
info.update({{"taylorseer_n_derivatives", p.taylorseer_n_derivatives}, {"taylorseer_skip_interval", p.taylorseer_skip_interval}});
if (p.scm_mask && *p.scm_mask)
info.update({{"scm_mask", p.scm_mask}, {"scm_policy_dynamic", p.scm_policy_dynamic}});
} else if (p.mode == SD_CACHE_SPECTRUM) {
info.update({{"spectrum_w", p.spectrum_w},
{"spectrum_m", p.spectrum_m},
{"spectrum_lam", p.spectrum_lam},
{"spectrum_window_size", p.spectrum_window_size},
{"spectrum_flex_window", p.spectrum_flex_window},
{"spectrum_warmup_steps", p.spectrum_warmup_steps},
{"spectrum_stop_percent", p.spectrum_stop_percent}});
}
return info;
}
template <typename Params>
static json common_info(const Params& p, const char* kind) {
json info = {{"schema", "sdcpp.generation/v1"}, {"kind", kind}, {"prompt", text(p.prompt)}, {"negative_prompt", text(p.negative_prompt)}, {"width", p.width}, {"height", p.height}, {"clip_skip", p.clip_skip}, {"seed", std::to_string(p.seed)}, {"sample_params", sampling_info(p.sample_params)}};
if (p.hires.enabled)
info["hires"] = hires_info(p.hires);
if (p.cache.mode != SD_CACHE_DISABLED)
info["cache"] = cache_info(p.cache);
if (p.circular_x)
info["circular_x"] = true;
if (p.circular_y)
info["circular_y"] = true;
if (p.ref_image_args && *p.ref_image_args)
info["ref_image_args"] = p.ref_image_args;
if (p.image_preprocess.rules && *p.image_preprocess.rules)
info["image_preprocess"] = {{"rules", p.image_preprocess.rules}};
const auto& t = p.vae_tiling_params;
if (t.enabled || t.temporal_tiling) {
auto& tiling = info["vae_tiling_params"];
if (t.enabled)
tiling.update({{"enabled", true}, {"tile_size_w", t.tile_size_w}, {"tile_size_h", t.tile_size_h}, {"target_overlap", t.target_overlap}, {"rel_size_w", t.rel_size_w}, {"rel_size_h", t.rel_size_h}});
if (t.temporal_tiling)
tiling["temporal_tiling"] = true;
if (t.extra_tiling_args && *t.extra_tiling_args)
tiling["extra_tiling_args"] = t.extra_tiling_args;
}
if (p.lora_count > 0) {
info["loras"] = json::array();
for (uint32_t i = 0; i < p.lora_count; ++i) {
// nlohmann::json promotes float to double, so dump the shortest
// decimal that round-trips the float32 (e.g. 0.85f stays 0.85
// instead of 0.8500000238418579) to keep replay exact.
const double multiplier = shortest_f32_as_double(p.loras[i].multiplier);
info["loras"].push_back({{"path", text(p.loras[i].path)}, {"multiplier", multiplier}, {"is_high_noise", p.loras[i].is_high_noise}});
}
}
return info;
}
static json image_info(const sd::ImageConfig& p) {
auto info = common_info(p, "image");
if (p.init_image.data)
info["strength"] = p.strength;
if (p.control_image.data) {
info["control_strength"] = p.control_strength;
if (p.control_type != SD_CONTROL_TYPE_NONE)
info["control_type"] = sd_control_type_name(p.control_type);
}
if (p.ip_adapter_image.data)
info["ip_adapter_strength"] = p.ip_adapter_strength;
if (p.pm_params.id_images && p.pm_params.id_images_count > 0) {
info["pm_params"] = {{"style_strength", p.pm_params.style_strength}};
if (p.pm_params.id_embed_path && *p.pm_params.id_embed_path)
info["pm_params"]["id_embed_path"] = p.pm_params.id_embed_path;
}
if (p.pulid_params.id_embedding_path && *p.pulid_params.id_embedding_path)
info["pulid_params"] = {{"id_embedding_path", p.pulid_params.id_embedding_path}, {"id_weight", p.pulid_params.id_weight}};
return info;
}
template <typename Params>
static Params resolved_params(const pipeline::GenerationRequest& request, Params p) {
p.width = request.width;
p.height = request.height;
p.seed = request.seed;
p.clip_skip = request.clip_skip;
p.sample_params = request.sample_params;
p.hires = request.hires;
p.vae_tiling_params = request.vae_tiling_params;
return p;
}
json generation_info(const pipeline::GenerationRequest& request, const sd::ImageConfig& params) {
auto p = resolved_params(request, params);
p.qwen_image_layers = request.qwen_image_layers;
auto info = image_info(p);
if (!request.models.empty())
info["models"] = request.models;
if (request.is_layered_image)
info["qwen_image_layers"] = p.qwen_image_layers;
return info;
}
json generation_info(const pipeline::GenerationRequest& request, const sd::VideoConfig& params) {
const auto p = resolved_params(request, params);
auto info = common_info(p, "video");
if (!request.models.empty())
info["models"] = request.models;
info.update({{"video_frames", request.frames}, {"fps", request.fps}});
if (p.init_image.data || p.end_image.data)
info["strength"] = p.strength;
if (request.use_vace)
info["vace_strength"] = p.vace_strength;
if (request.use_high_noise)
info.update({{"moe_boundary", p.moe_boundary}, {"high_noise_sample_params", sampling_info(request.high_noise_sample_params, true)}});
if (p.ref_videos_count > 0) {
info["ref_videos"] = json::array();
for (int i = 0; i < p.ref_videos_count; ++i)
info["ref_videos"].push_back({{"fps", p.ref_videos[i].fps}});
}
return info;
}
json adetailer_generation_info(const sd::ADetailConfig& params, const sd::ImageConfig& inpaint_params) {
auto inpaint = image_info(inpaint_params);
inpaint["strength"] = inpaint_params.strength;
// ADetailer clears these inputs before generating each crop.
for (const auto* key : {"control_strength", "control_type", "pm_params", "pulid_params", "hires", "image_preprocess"})
inpaint.erase(key);
sd::ImageConfig defaults;
if (inpaint_params.qwen_image_layers != defaults.qwen_image_layers)
inpaint["qwen_image_layers"] = inpaint_params.qwen_image_layers;
json adetailer = json::object();
if (params.prompt && *params.prompt)
adetailer["prompt"] = params.prompt;
if (params.negative_prompt && *params.negative_prompt)
adetailer["negative_prompt"] = params.negative_prompt;
if (params.extra_ad_args && *params.extra_ad_args)
adetailer["extra_ad_args"] = params.extra_ad_args;
return {{"schema", "sdcpp.generation/v1"}, {"kind", "image"}, {"adetailer", adetailer}, {"inpaint_params", inpaint}};
}
json upscale_generation_info(uint32_t upscale_factor) {
return {{"schema", "sdcpp.generation/v1"}, {"kind", "image"}, {"upscale", {{"upscale_factor", upscale_factor}}}};
}
} // namespace sd