Skip to content

Commit ff67c30

Browse files
committed
add uncond slg variant
fix default slg params
1 parent 98e67c0 commit ff67c30

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

examples/cli/main.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ struct SDParams {
105105
float slg_scale = 0.0f;
106106
float skip_layer_start = 0.01f;
107107
float skip_layer_end = 0.2f;
108+
bool slg_uncond = false;
108109

109110
float apg_eta = 1.0f;
110111
float apg_momentum = 0.0f;
@@ -216,11 +217,14 @@ void print_usage(int argc, const char* argv[]) {
216217
printf(" (replaces saturation with a smooth approximation)\n");
217218
printf(" --slg-scale SCALE skip layer guidance (SLG) scale, only for DiT models: (default: 0)\n");
218219
printf(" 0 means disabled, a value of 2.5 is nice for sd3.5 medium\n");
219-
printf(" --eta SCALE eta in DDIM, only for DDIM and TCD: (default: 0)\n");
220+
printf(" --slg-uncond Use CFG's forward pass for SLG instead of a separate pass, only for DiT models\n");
221+
printf(" To use this, it's recommended to keep slg-scale to 0, both for performance and quality reasons\n");
222+
printf(" This should be slightly faster than normal cfg when cfg_scale != 1.\n");
220223
printf(" --skip-layers LAYERS Layers to skip for SLG steps: (default: [7,8,9])\n");
221224
printf(" --skip-layer-start START SLG enabling point: (default: 0.01)\n");
222225
printf(" --skip-layer-end END SLG disabling point: (default: 0.2)\n");
223226
printf(" SLG will be enabled at step int([STEPS]*[START]) and disabled at int([STEPS]*[END])\n");
227+
printf(" --eta SCALE eta in DDIM, only for DDIM and TCD: (default: 0)\n");
224228
printf(" --strength STRENGTH strength for noising/unnoising (default: 0.75)\n");
225229
printf(" --style-ratio STYLE-RATIO strength for keeping input identity (default: 20)\n");
226230
printf(" --control-strength STRENGTH strength to apply Control Net (default: 0.9)\n");
@@ -683,6 +687,7 @@ std::string get_image_params(SDParams params, int64_t seed) {
683687
}
684688
}
685689
if (params.slg_scale != 0 && params.skip_layers.size() != 0) {
690+
parameter_string += "Unconditional SLG: " + std::string(params.slg_uncond ? "True" : "False") + ", ";
686691
parameter_string += "SLG scale: " + std::to_string(params.cfg_scale) + ", ";
687692
parameter_string += "Skip layers: [";
688693
for (const auto& layer : params.skip_layers) {

stable-diffusion.cpp

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ class StableDiffusionGGML {
886886

887887
bool has_unconditioned = img_cfg_scale != 1.0 && uncond.c_crossattn != NULL;
888888
bool has_img_cond = cfg_scale != img_cfg_scale && img_cond.c_crossattn != NULL;
889-
bool has_skiplayer = slg_scale != 0.0 && skip_layers.size() > 0;
889+
bool has_skiplayer = (slg_scale != 0.0 || guidance.slg.uncond) && skip_layers.size() > 0;
890890

891891
// denoise wrapper
892892
struct ggml_tensor* out_cond = ggml_dup_tensor(work_ctx, x);
@@ -899,7 +899,9 @@ class StableDiffusionGGML {
899899
}
900900
if (has_skiplayer) {
901901
if (sd_version_is_dit(version)) {
902-
out_skip = ggml_dup_tensor(work_ctx, x);
902+
if (slg_scale != 0.0) {
903+
out_skip = ggml_dup_tensor(work_ctx, x);
904+
}
903905
} else {
904906
has_skiplayer = false;
905907
LOG_WARN("SLG is incompatible with %s models", model_version_to_str[version]);
@@ -973,6 +975,8 @@ class StableDiffusionGGML {
973975
control_strength,
974976
&out_cond);
975977
}
978+
int step_count = sigmas.size();
979+
bool is_skiplayer_step = has_skiplayer && step > (int)(guidance.slg.layer_start * step_count) && step < (int)(guidance.slg.layer_end * step_count);
976980

977981
float* negative_data = NULL;
978982
if (has_unconditioned) {
@@ -981,18 +985,36 @@ class StableDiffusionGGML {
981985
control_net->compute(n_threads, noised_input, control_hint, timesteps, uncond.c_crossattn, uncond.c_vector);
982986
controls = control_net->controls;
983987
}
984-
diffusion_model->compute(n_threads,
985-
noised_input,
986-
timesteps,
987-
uncond.c_crossattn,
988-
uncond.c_concat,
989-
uncond.c_vector,
990-
guidance_tensor,
991-
ref_latents,
992-
-1,
993-
controls,
994-
control_strength,
995-
&out_uncond);
988+
if (is_skiplayer_step && guidance.slg.uncond) {
989+
LOG_DEBUG("Skipping layers at uncond step %d\n", step);
990+
diffusion_model->compute(n_threads,
991+
noised_input,
992+
timesteps,
993+
uncond.c_crossattn,
994+
uncond.c_concat,
995+
uncond.c_vector,
996+
guidance_tensor,
997+
ref_latents,
998+
-1,
999+
controls,
1000+
control_strength,
1001+
&out_uncond,
1002+
NULL,
1003+
skip_layers);
1004+
} else {
1005+
diffusion_model->compute(n_threads,
1006+
noised_input,
1007+
timesteps,
1008+
uncond.c_crossattn,
1009+
uncond.c_concat,
1010+
uncond.c_vector,
1011+
guidance_tensor,
1012+
ref_latents,
1013+
-1,
1014+
controls,
1015+
control_strength,
1016+
&out_uncond);
1017+
}
9961018
negative_data = (float*)out_uncond->data;
9971019
}
9981020

@@ -1013,10 +1035,8 @@ class StableDiffusionGGML {
10131035
img_cond_data = (float*)out_img_cond->data;
10141036
}
10151037

1016-
int step_count = sigmas.size();
1017-
bool is_skiplayer_step = has_skiplayer && step > (int)(guidance.slg.layer_start * step_count) && step < (int)(guidance.slg.layer_end * step_count);
10181038
float* skip_layer_data = NULL;
1019-
if (is_skiplayer_step) {
1039+
if (is_skiplayer_step && slg_scale != 0.0) {
10201040
LOG_DEBUG("Skipping layers at step %d\n", step);
10211041
// skip layer (same as conditionned)
10221042
diffusion_model->compute(n_threads,
@@ -1106,7 +1126,7 @@ class StableDiffusionGGML {
11061126
} else {
11071127
float delta = deltas[i];
11081128

1109-
if(cfg_scale != 1) {
1129+
if (cfg_scale != 1) {
11101130
latent_result = positive_data[i] + (cfg_scale - 1) * delta;
11111131
} else if (has_img_cond) {
11121132
latent_result = positive_data[i] + (img_cfg_scale - 1) * delta;
@@ -1116,7 +1136,7 @@ class StableDiffusionGGML {
11161136
// img_cfg_scale == 1
11171137
latent_result = img_cond_data[i] + cfg_scale * (positive_data[i] - img_cond_data[i]);
11181138
}
1119-
if (is_skiplayer_step) {
1139+
if (is_skiplayer_step && slg_scale != 0.0) {
11201140
latent_result = latent_result + (positive_data[i] - skip_layer_data[i]) * slg_scale;
11211141
}
11221142
// v = latent_result, eps = latent_result

stable-diffusion.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ typedef struct {
152152
float layer_start;
153153
float layer_end;
154154
float scale;
155+
bool uncond;
155156
} sd_slg_params_t;
156157

157158
typedef struct {

0 commit comments

Comments
 (0)