mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-11-11 03:24:30 +00:00
refactor: centralize model factories and defaults (#2198)
This commit is contained in:
1 parent
baf45b49e2
commit
cd7f986058
50 files changed
+2233
-943
No files matched your search
@@ -818,7 +818,7 @@ ArgOptions SDContextParams::get_options() {
|
||||
on_sampler_rng_arg},
|
||||
{"",
|
||||
"--prediction",
|
||||
"prediction type override, one of [eps, v, edm_v, sd3_flow, flux_flow, sefi_flow]",
|
||||
"prediction type override, one of [eps, v, edm_v, sd3_flow, flux_flow, sefi_flow, minit2i_flow, sensenova_u1_flow, flux3_flow, h3_av_flow, ming_image_flow]",
|
||||
on_prediction_arg},
|
||||
{"",
|
||||
"--lora-apply-mode",
|
||||
|
||||
@@ -447,10 +447,6 @@ image establishes the generation dimensions if `size` is omitted. Input
|
||||
geometry follows `image_preprocess`: references preserve their dimensions by
|
||||
default, while init and mask use the generation canvas preset.
|
||||
|
||||
When the request starts without preconfigured reference images, the first uploaded image becomes the img2img init image if the loaded model does not support reference-image conditioning, the final reference list is empty, a mask is present, or an explicit numeric `strength` override is provided. Otherwise, only the final reference list is used for image conditioning, without initializing the img2img latent. Requests with preconfigured references retain their existing init-image selection.
|
||||
|
||||
Routing uses the reference list and mask after applying `sd_cpp_extra_args`. Both `ref_images: []` and `ref_images: null` clear the reference list; `mask_image: null` clears an uploaded mask. An explicit `init_image` in `sd_cpp_extra_args`, including `null`, takes precedence over the implicit init-image selection.
|
||||
|
||||
Reference encoding then follows model presets and `ref_image_args`. To skip
|
||||
input geometry for references and disable resizing before VAE encoding, include
|
||||
this in `prompt`:
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
#ifndef __SD_EXAMPLES_SERVER_OPENAI_EDIT_POLICY_H__
|
||||
#define __SD_EXAMPLES_SERVER_OPENAI_EDIT_POLICY_H__
|
||||
|
||||
#include "common/resource_owners.hpp"
|
||||
|
||||
enum class OpenAIEditInitImageAction {
|
||||
KeepExisting,
|
||||
UseUploaded,
|
||||
Clear,
|
||||
};
|
||||
|
||||
inline bool openai_edit_uses_uploaded_image_as_init(bool supports_ref_image_conditioning,
|
||||
bool has_ref_images,
|
||||
bool has_mask,
|
||||
bool has_explicit_strength_override) {
|
||||
return !supports_ref_image_conditioning || !has_ref_images || has_mask || has_explicit_strength_override;
|
||||
}
|
||||
|
||||
inline OpenAIEditInitImageAction apply_openai_edit_init_image_policy(
|
||||
SDImageOwner& init_image,
|
||||
SDImageOwner& uploaded_image,
|
||||
bool uploaded_image_was_first_reference,
|
||||
bool supports_ref_image_conditioning,
|
||||
bool has_ref_images,
|
||||
bool has_mask,
|
||||
bool has_extra_init_image_override,
|
||||
bool has_explicit_strength_override) {
|
||||
if (!uploaded_image_was_first_reference || has_extra_init_image_override) {
|
||||
return OpenAIEditInitImageAction::KeepExisting;
|
||||
}
|
||||
|
||||
if (openai_edit_uses_uploaded_image_as_init(supports_ref_image_conditioning,
|
||||
has_ref_images,
|
||||
has_mask,
|
||||
has_explicit_strength_override)) {
|
||||
init_image = std::move(uploaded_image);
|
||||
return OpenAIEditInitImageAction::UseUploaded;
|
||||
}
|
||||
|
||||
init_image.reset();
|
||||
return OpenAIEditInitImageAction::Clear;
|
||||
}
|
||||
|
||||
#endif // __SD_EXAMPLES_SERVER_OPENAI_EDIT_POLICY_H__
|
||||
@@ -7,7 +7,6 @@
|
||||
#include "common/common.h"
|
||||
#include "common/media_io.h"
|
||||
#include "common/resource_owners.hpp"
|
||||
#include "openai_edit_policy.h"
|
||||
|
||||
static std::string extract_and_remove_sd_cpp_extra_args(std::string& text) {
|
||||
std::regex re("<sd_cpp_extra_args>(.*?)</sd_cpp_extra_args>");
|
||||
@@ -158,24 +157,7 @@ static bool build_openai_edit_request(const httplib::Request& req,
|
||||
request.gen_params.height = height;
|
||||
request.gen_params.batch_count = n;
|
||||
|
||||
std::string sd_cpp_extra_args_str = extract_and_remove_sd_cpp_extra_args(request.gen_params.prompt);
|
||||
const json extra_args = sd_cpp_extra_args_str.empty()
|
||||
? json::object()
|
||||
: json::parse(sd_cpp_extra_args_str, nullptr, false);
|
||||
const bool has_extra_init_image_override = extra_args.is_object() && extra_args.contains("init_image");
|
||||
const bool has_extra_ref_images_override = extra_args.is_object() && extra_args.contains("ref_images");
|
||||
const bool has_extra_strength_override = extra_args.is_object() &&
|
||||
extra_args.contains("strength") &&
|
||||
extra_args["strength"].is_number();
|
||||
const bool has_extra_mask_image = extra_args.is_object() &&
|
||||
extra_args.contains("mask_image") &&
|
||||
!extra_args["mask_image"].is_null();
|
||||
const bool supports_ref_image_conditioning = sd_ctx_supports_ref_image_conditioning(runtime.sd_ctx);
|
||||
const bool may_need_uploaded_init_image = !has_extra_init_image_override &&
|
||||
(!supports_ref_image_conditioning || !mask_bytes.empty() ||
|
||||
has_extra_mask_image || has_extra_strength_override || has_extra_ref_images_override);
|
||||
SDImageOwner uploaded_image_for_init;
|
||||
bool uploaded_image_was_first_reference = false;
|
||||
std::string sd_cpp_extra_args_str = extract_and_remove_sd_cpp_extra_args(request.gen_params.prompt);
|
||||
for (auto& bytes : images_bytes) {
|
||||
int img_w = 0;
|
||||
int img_h = 0;
|
||||
@@ -190,17 +172,9 @@ static bool build_openai_edit_request(const httplib::Request& req,
|
||||
continue;
|
||||
}
|
||||
|
||||
const bool is_first_ref_image = request.gen_params.ref_images.empty();
|
||||
SDImageOwner image_owner({(uint32_t)img_w, (uint32_t)img_h, (uint32_t)resolved_channel, raw_pixels});
|
||||
request.gen_params.set_width_and_height_if_unset(image_owner.get().width, image_owner.get().height);
|
||||
|
||||
if (is_first_ref_image) {
|
||||
uploaded_image_was_first_reference = true;
|
||||
if (may_need_uploaded_init_image) {
|
||||
uploaded_image_for_init = image_owner;
|
||||
}
|
||||
}
|
||||
|
||||
request.gen_params.ref_images.push_back(std::move(image_owner));
|
||||
}
|
||||
|
||||
@@ -231,21 +205,6 @@ static bool build_openai_edit_request(const httplib::Request& req,
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto init_image_action = apply_openai_edit_init_image_policy(
|
||||
request.gen_params.init_image,
|
||||
uploaded_image_for_init,
|
||||
uploaded_image_was_first_reference,
|
||||
supports_ref_image_conditioning,
|
||||
!request.gen_params.ref_images.empty(),
|
||||
request.gen_params.mask_image.get().data != nullptr,
|
||||
has_extra_init_image_override,
|
||||
has_extra_strength_override);
|
||||
if (init_image_action == OpenAIEditInitImageAction::UseUploaded &&
|
||||
request.gen_params.init_image.get().data == nullptr) {
|
||||
error_message = "could not allocate init image";
|
||||
return false;
|
||||
}
|
||||
|
||||
// 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 params";
|
||||
|
||||
Reference in new issue
Block a user