Skip to content

Commit 98e67c0

Browse files
committed
apg: add experimental threshold smoothing parameter
1 parent 316937b commit 98e67c0

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

examples/cli/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ struct SDParams {
109109
float apg_eta = 1.0f;
110110
float apg_momentum = 0.0f;
111111
float apg_norm_threshold = 0.0f;
112+
float apg_norm_smoothing = 0.0f;
112113

113114
bool chroma_use_dit_mask = true;
114115
bool chroma_use_t5_mask = false;
@@ -211,6 +212,8 @@ void print_usage(int argc, const char* argv[]) {
211212
printf(" --apg-eta VALUE parallel projected guidance scale for APG (default: 1.0, recommended: between 0 and 1)\n");
212213
printf(" --apg-momentum VALUE CFG update direction momentum for APG (default: 0, recommended: around -0.5)\n");
213214
printf(" --apg-nt, --apg-rescale VALUE CFG update direction norm threshold for APG (default: 0 = disabled, recommended: 4-15)\n");
215+
printf(" --apg-nt-smoothing VALUE EXPERIMENTAL! Norm threshold smoothing for APG (default: 0 = disabled)\n");
216+
printf(" (replaces saturation with a smooth approximation)\n");
214217
printf(" --slg-scale SCALE skip layer guidance (SLG) scale, only for DiT models: (default: 0)\n");
215218
printf(" 0 means disabled, a value of 2.5 is nice for sd3.5 medium\n");
216219
printf(" --eta SCALE eta in DDIM, only for DDIM and TCD: (default: 0)\n");
@@ -675,6 +678,9 @@ std::string get_image_params(SDParams params, int64_t seed) {
675678
}
676679
if (params.apg_norm_threshold != 0) {
677680
parameter_string += "CFG normalization threshold: " + std::to_string(params.apg_norm_threshold) + ", ";
681+
if (params.apg_norm_smoothing != 0) {
682+
parameter_string += "CFG normalization threshold: " + std::to_string(params.apg_norm_smoothing) + ", ";
683+
}
678684
}
679685
if (params.slg_scale != 0 && params.skip_layers.size() != 0) {
680686
parameter_string += "SLG scale: " + std::to_string(params.cfg_scale) + ", ";

stable-diffusion.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ class StableDiffusionGGML {
848848
int start_merge_step,
849849
SDCondition id_cond,
850850
std::vector<ggml_tensor*> ref_latents = {},
851-
sd_apg_params_t apg_params = {1, 0, 0},
851+
sd_apg_params_t apg_params = {1, 0, 0, 0},
852852
ggml_tensor* denoise_mask = nullptr) {
853853
std::vector<int> skip_layers(guidance.slg.layers, guidance.slg.layers + guidance.slg.layer_count);
854854

@@ -1068,8 +1068,14 @@ class StableDiffusionGGML {
10681068
deltas[i] = delta;
10691069
}
10701070
if (apg_params.norm_treshold > 0) {
1071-
diff_norm = sqrtf(diff_norm);
1072-
apg_scale_factor = std::min(1.0f, apg_params.norm_treshold / diff_norm);
1071+
diff_norm = sqrtf(diff_norm);
1072+
if (apg_params.norm_treshold_smoothing <= 0) {
1073+
apg_scale_factor = std::min(1.0f, apg_params.norm_treshold / diff_norm);
1074+
} else {
1075+
// Experimental: smooth saturate
1076+
float x = apg_params.norm_treshold / diff_norm;
1077+
apg_scale_factor = x / std::pow(1 + std::pow(x, 1.0 / apg_params.norm_treshold_smoothing), apg_params.norm_treshold_smoothing);
1078+
}
10731079
}
10741080
if (apg_params.eta != 1.0f) {
10751081
dot *= apg_scale_factor;

stable-diffusion.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ typedef struct {
158158
float eta;
159159
float momentum;
160160
float norm_treshold;
161+
float norm_treshold_smoothing;
161162
} sd_apg_params_t;
162163

163164
typedef struct {

0 commit comments

Comments
 (0)