mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2025-12-13 05:48:56 +00:00
Compare commits
5 Commits
4e2a0d513d
...
4023083f70
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4023083f70 | ||
|
|
b6c2244d9a | ||
|
|
fd693ac6a2 | ||
|
|
171b2222a5 | ||
|
|
567f9f14f0 |
@ -384,7 +384,6 @@ arguments:
|
||||
--pm-id-images-dir [DIR] path to PHOTOMAKER input id images dir
|
||||
--pm-id-embed-path [PATH] path to PHOTOMAKER v2 id embed
|
||||
--pm-style-strength strength for keeping PHOTOMAKER input identity (default: 20)
|
||||
--normalize-input normalize PHOTOMAKER input id images
|
||||
-v, --verbose print extra info
|
||||
```
|
||||
|
||||
|
||||
7
clip.hpp
7
clip.hpp
@ -553,12 +553,13 @@ protected:
|
||||
void init_params(struct ggml_context* ctx, const String2GGMLType& tensor_types = {}, const std::string prefix = "") {
|
||||
enum ggml_type token_wtype = GGML_TYPE_F32;
|
||||
if (!force_clip_f32) {
|
||||
auto tensor_type = tensor_types.find(prefix + "token_embedding.weight");
|
||||
if (tensor_type != tensor_types.end() && tensor_type->second == GGML_TYPE_F16) {
|
||||
auto tensor_type = tensor_types.find(prefix + "token_embedding.weight");
|
||||
std::set<ggml_type> allow_types = {GGML_TYPE_F16, GGML_TYPE_Q8_0, GGML_TYPE_Q5_1, GGML_TYPE_Q5_0, GGML_TYPE_Q4_1, GGML_TYPE_Q4_0};
|
||||
if (tensor_type != tensor_types.end() && allow_types.find(tensor_type->second) != allow_types.end()) {
|
||||
token_wtype = tensor_type->second;
|
||||
}
|
||||
}
|
||||
enum ggml_type position_wtype = GGML_TYPE_F32;
|
||||
enum ggml_type position_wtype = GGML_TYPE_F32;
|
||||
params["token_embedding.weight"] = ggml_new_tensor_2d(ctx, token_wtype, embed_dim, vocab_size);
|
||||
params["position_embedding.weight"] = ggml_new_tensor_2d(ctx, position_wtype, embed_dim, num_positions);
|
||||
}
|
||||
|
||||
@ -103,7 +103,6 @@ struct SDParams {
|
||||
bool verbose = false;
|
||||
bool offload_params_to_cpu = false;
|
||||
bool control_net_cpu = false;
|
||||
bool normalize_input = false;
|
||||
bool clip_on_cpu = false;
|
||||
bool vae_on_cpu = false;
|
||||
bool diffusion_flash_attn = false;
|
||||
@ -156,7 +155,6 @@ void print_params(SDParams params) {
|
||||
printf(" pm_id_images_dir: %s\n", params.pm_id_images_dir.c_str());
|
||||
printf(" pm_id_embed_path: %s\n", params.pm_id_embed_path.c_str());
|
||||
printf(" pm_style_strength: %.2f\n", params.pm_style_strength);
|
||||
printf(" normalize input image: %s\n", params.normalize_input ? "true" : "false");
|
||||
printf(" output_path: %s\n", params.output_path.c_str());
|
||||
printf(" init_image_path: %s\n", params.init_image_path.c_str());
|
||||
printf(" end_image_path: %s\n", params.end_image_path.c_str());
|
||||
@ -306,7 +304,6 @@ void print_usage(int argc, const char* argv[]) {
|
||||
printf(" --pm-id-images-dir [DIR] path to PHOTOMAKER input id images dir\n");
|
||||
printf(" --pm-id-embed-path [PATH] path to PHOTOMAKER v2 id embed\n");
|
||||
printf(" --pm-style-strength strength for keeping PHOTOMAKER input identity (default: 20)\n");
|
||||
printf(" --normalize-input normalize PHOTOMAKER input id images\n");
|
||||
printf(" -v, --verbose print extra info\n");
|
||||
}
|
||||
|
||||
@ -552,7 +549,6 @@ void parse_args(int argc, const char** argv, SDParams& params) {
|
||||
{"", "--vae-tiling", "", true, ¶ms.vae_tiling_params.enabled},
|
||||
{"", "--offload-to-cpu", "", true, ¶ms.offload_params_to_cpu},
|
||||
{"", "--control-net-cpu", "", true, ¶ms.control_net_cpu},
|
||||
{"", "--normalize-input", "", true, ¶ms.normalize_input},
|
||||
{"", "--clip-on-cpu", "", true, ¶ms.clip_on_cpu},
|
||||
{"", "--vae-on-cpu", "", true, ¶ms.vae_on_cpu},
|
||||
{"", "--diffusion-fa", "", true, ¶ms.diffusion_flash_attn},
|
||||
@ -1379,7 +1375,6 @@ int main(int argc, const char* argv[]) {
|
||||
params.batch_count,
|
||||
control_image,
|
||||
params.control_strength,
|
||||
params.normalize_input,
|
||||
{
|
||||
pmid_images.data(),
|
||||
(int)pmid_images.size(),
|
||||
|
||||
@ -2429,6 +2429,7 @@ bool ModelLoader::save_to_gguf_file(const std::string& file_path, ggml_type type
|
||||
|
||||
auto tensor_type_rules = parse_tensor_type_rules(tensor_type_rules_str);
|
||||
|
||||
std::mutex tensor_mutex;
|
||||
auto on_new_tensor_cb = [&](const TensorStorage& tensor_storage, ggml_tensor** dst_tensor) -> bool {
|
||||
const std::string& name = tensor_storage.name;
|
||||
ggml_type tensor_type = tensor_storage.type;
|
||||
@ -2446,6 +2447,7 @@ bool ModelLoader::save_to_gguf_file(const std::string& file_path, ggml_type type
|
||||
tensor_type = dst_type;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(tensor_mutex);
|
||||
ggml_tensor* tensor = ggml_new_tensor(ggml_ctx, tensor_type, tensor_storage.n_dims, tensor_storage.ne);
|
||||
if (tensor == NULL) {
|
||||
LOG_ERROR("ggml_new_tensor failed");
|
||||
|
||||
4
pmid.hpp
4
pmid.hpp
@ -599,7 +599,8 @@ struct PhotoMakerIDEmbed : public GGMLRunner {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool dry_run = true;
|
||||
bool dry_run = true;
|
||||
std::mutex tensor_mutex;
|
||||
auto on_new_tensor_cb = [&](const TensorStorage& tensor_storage, ggml_tensor** dst_tensor) -> bool {
|
||||
const std::string& name = tensor_storage.name;
|
||||
|
||||
@ -608,6 +609,7 @@ struct PhotoMakerIDEmbed : public GGMLRunner {
|
||||
return true;
|
||||
}
|
||||
if (dry_run) {
|
||||
std::lock_guard<std::mutex> lock(tensor_mutex);
|
||||
struct ggml_tensor* real = ggml_new_tensor(params_ctx,
|
||||
tensor_storage.type,
|
||||
tensor_storage.n_dims,
|
||||
|
||||
@ -443,6 +443,10 @@ public:
|
||||
diffusion_model->alloc_params_buffer();
|
||||
diffusion_model->get_param_tensors(tensors);
|
||||
|
||||
if (sd_version_is_unet_edit(version)) {
|
||||
vae_decode_only = false;
|
||||
}
|
||||
|
||||
if (high_noise_diffusion_model) {
|
||||
high_noise_diffusion_model->alloc_params_buffer();
|
||||
high_noise_diffusion_model->get_param_tensors(tensors);
|
||||
@ -748,15 +752,15 @@ public:
|
||||
denoiser->scheduler->version = version;
|
||||
break;
|
||||
case SGM_UNIFORM:
|
||||
LOG_INFO("Running with SGM Uniform schedule");
|
||||
denoiser->scheduler = std::make_shared<SGMUniformSchedule>();
|
||||
denoiser->scheduler->version = version;
|
||||
break;
|
||||
LOG_INFO("Running with SGM Uniform schedule");
|
||||
denoiser->scheduler = std::make_shared<SGMUniformSchedule>();
|
||||
denoiser->scheduler->version = version;
|
||||
break;
|
||||
case SIMPLE:
|
||||
LOG_INFO("Running with Simple schedule");
|
||||
denoiser->scheduler = std::make_shared<SimpleSchedule>();
|
||||
denoiser->scheduler->version = version;
|
||||
break;
|
||||
LOG_INFO("Running with Simple schedule");
|
||||
denoiser->scheduler = std::make_shared<SimpleSchedule>();
|
||||
denoiser->scheduler->version = version;
|
||||
break;
|
||||
case SMOOTHSTEP:
|
||||
LOG_INFO("Running with SmoothStep scheduler");
|
||||
denoiser->scheduler = std::make_shared<SmoothStepSchedule>();
|
||||
@ -1053,7 +1057,7 @@ public:
|
||||
ggml_tensor* denoise_mask = NULL,
|
||||
ggml_tensor* vace_context = NULL,
|
||||
float vace_strength = 1.f) {
|
||||
if (shifted_timestep > 0 && !sd_version_is_sdxl(version)) {
|
||||
if (shifted_timestep > 0 && !sd_version_is_sdxl(version)) {
|
||||
LOG_WARN("timestep shifting is only supported for SDXL models!");
|
||||
shifted_timestep = 0;
|
||||
}
|
||||
@ -1127,7 +1131,7 @@ public:
|
||||
} else {
|
||||
timesteps_vec.assign(1, t);
|
||||
}
|
||||
|
||||
|
||||
timesteps_vec = process_timesteps(timesteps_vec, init_latent, denoise_mask);
|
||||
auto timesteps = vector_to_ggml_tensor(work_ctx, timesteps_vec);
|
||||
std::vector<float> guidance_vec(1, guidance.distilled_guidance);
|
||||
@ -1790,7 +1794,6 @@ void sd_img_gen_params_init(sd_img_gen_params_t* sd_img_gen_params) {
|
||||
sd_img_gen_params->seed = -1;
|
||||
sd_img_gen_params->batch_count = 1;
|
||||
sd_img_gen_params->control_strength = 0.9f;
|
||||
sd_img_gen_params->normalize_input = false;
|
||||
sd_img_gen_params->pm_params = {nullptr, 0, nullptr, 20.f};
|
||||
sd_img_gen_params->vae_tiling_params = {false, 0, 0, 0.5f, 0.0f, 0.0f};
|
||||
}
|
||||
@ -1816,7 +1819,6 @@ char* sd_img_gen_params_to_str(const sd_img_gen_params_t* sd_img_gen_params) {
|
||||
"ref_images_count: %d\n"
|
||||
"increase_ref_index: %s\n"
|
||||
"control_strength: %.2f\n"
|
||||
"normalize_input: %s\n"
|
||||
"photo maker: {style_strength = %.2f, id_images_count = %d, id_embed_path = %s}\n"
|
||||
"VAE tiling: %s\n",
|
||||
SAFE_STR(sd_img_gen_params->prompt),
|
||||
@ -1831,7 +1833,6 @@ char* sd_img_gen_params_to_str(const sd_img_gen_params_t* sd_img_gen_params) {
|
||||
sd_img_gen_params->ref_images_count,
|
||||
BOOL_STR(sd_img_gen_params->increase_ref_index),
|
||||
sd_img_gen_params->control_strength,
|
||||
BOOL_STR(sd_img_gen_params->normalize_input),
|
||||
sd_img_gen_params->pm_params.style_strength,
|
||||
sd_img_gen_params->pm_params.id_images_count,
|
||||
SAFE_STR(sd_img_gen_params->pm_params.id_embed_path),
|
||||
@ -1915,7 +1916,6 @@ sd_image_t* generate_image_internal(sd_ctx_t* sd_ctx,
|
||||
int batch_count,
|
||||
sd_image_t control_image,
|
||||
float control_strength,
|
||||
bool normalize_input,
|
||||
sd_pm_params_t pm_params,
|
||||
std::vector<ggml_tensor*> ref_latents,
|
||||
bool increase_ref_index,
|
||||
@ -2387,19 +2387,35 @@ sd_image_t* generate_image(sd_ctx_t* sd_ctx, const sd_img_gen_params_t* sd_img_g
|
||||
init_latent = generate_init_latent(sd_ctx, work_ctx, width, height);
|
||||
}
|
||||
|
||||
if (sd_img_gen_params->ref_images_count > 0) {
|
||||
sd_guidance_params_t guidance = sd_img_gen_params->sample_params.guidance;
|
||||
std::vector<sd_image_t*> ref_images;
|
||||
for (int i = 0; i < sd_img_gen_params->ref_images_count; i++) {
|
||||
ref_images.push_back(&sd_img_gen_params->ref_images[i]);
|
||||
}
|
||||
|
||||
std::vector<uint8_t> empty_image_data;
|
||||
sd_image_t empty_image = {(uint32_t)width, (uint32_t)height, 3, nullptr};
|
||||
if (ref_images.empty() && sd_version_is_unet_edit(sd_ctx->sd->version)) {
|
||||
LOG_WARN("This model needs at least one reference image; using an empty reference");
|
||||
empty_image_data.resize(width * height * 3);
|
||||
ref_images.push_back(&empty_image);
|
||||
empty_image.data = empty_image_data.data();
|
||||
guidance.img_cfg = 0.f;
|
||||
}
|
||||
|
||||
if (ref_images.size() > 0) {
|
||||
LOG_INFO("EDIT mode");
|
||||
}
|
||||
|
||||
std::vector<ggml_tensor*> ref_latents;
|
||||
for (int i = 0; i < sd_img_gen_params->ref_images_count; i++) {
|
||||
for (int i = 0; i < ref_images.size(); i++) {
|
||||
ggml_tensor* img = ggml_new_tensor_4d(work_ctx,
|
||||
GGML_TYPE_F32,
|
||||
sd_img_gen_params->ref_images[i].width,
|
||||
sd_img_gen_params->ref_images[i].height,
|
||||
ref_images[i]->width,
|
||||
ref_images[i]->height,
|
||||
3,
|
||||
1);
|
||||
sd_image_to_tensor(sd_img_gen_params->ref_images[i], img);
|
||||
sd_image_to_tensor(*ref_images[i], img);
|
||||
|
||||
ggml_tensor* latent = NULL;
|
||||
if (sd_ctx->sd->use_tiny_autoencoder) {
|
||||
@ -2437,7 +2453,7 @@ sd_image_t* generate_image(sd_ctx_t* sd_ctx, const sd_img_gen_params_t* sd_img_g
|
||||
SAFE_STR(sd_img_gen_params->prompt),
|
||||
SAFE_STR(sd_img_gen_params->negative_prompt),
|
||||
sd_img_gen_params->clip_skip,
|
||||
sd_img_gen_params->sample_params.guidance,
|
||||
guidance,
|
||||
sd_img_gen_params->sample_params.eta,
|
||||
sd_img_gen_params->sample_params.shifted_timestep,
|
||||
width,
|
||||
@ -2448,7 +2464,6 @@ sd_image_t* generate_image(sd_ctx_t* sd_ctx, const sd_img_gen_params_t* sd_img_g
|
||||
sd_img_gen_params->batch_count,
|
||||
sd_img_gen_params->control_image,
|
||||
sd_img_gen_params->control_strength,
|
||||
sd_img_gen_params->normalize_input,
|
||||
sd_img_gen_params->pm_params,
|
||||
ref_latents,
|
||||
sd_img_gen_params->increase_ref_index,
|
||||
|
||||
@ -212,7 +212,6 @@ typedef struct {
|
||||
int batch_count;
|
||||
sd_image_t control_image;
|
||||
float control_strength;
|
||||
bool normalize_input;
|
||||
sd_pm_params_t pm_params;
|
||||
sd_tiling_params_t vae_tiling_params;
|
||||
} sd_img_gen_params_t;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user