Skip to content

Commit dcd9d6a

Browse files
committed
feat: add support for the eta parameter to ancestral samplers
1 parent 4105a77 commit dcd9d6a

5 files changed

Lines changed: 37 additions & 12 deletions

File tree

examples/cli/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,15 @@ Generation Options:
109109
medium
110110
--skip-layer-start <float> SLG enabling point (default: 0.01)
111111
--skip-layer-end <float> SLG disabling point (default: 0.2)
112-
--eta <float> eta in DDIM, only for DDIM and TCD (default: 0)
112+
--eta <float> noise multiplier (default: 0 for ddim_trailing, tcd, res_multistep and res_2s; 1 for euler_a and dpm++2s_a)
113113
--flow-shift <float> shift value for Flow models like SD3.x or WAN (default: auto)
114114
--high-noise-cfg-scale <float> (high noise) unconditional guidance scale: (default: 7.0)
115115
--high-noise-img-cfg-scale <float> (high noise) image guidance scale for inpaint or instruct-pix2pix models (default: same as --cfg-scale)
116116
--high-noise-guidance <float> (high noise) distilled guidance scale for models with guidance input (default: 3.5)
117117
--high-noise-slg-scale <float> (high noise) skip layer guidance (SLG) scale, only for DiT models: (default: 0)
118118
--high-noise-skip-layer-start <float> (high noise) SLG enabling point (default: 0.01)
119119
--high-noise-skip-layer-end <float> (high noise) SLG disabling point (default: 0.2)
120-
--high-noise-eta <float> (high noise) eta in DDIM, only for DDIM and TCD (default: 0)
120+
--high-noise-eta <float> (high noise) noise multiplier (default: 0 for ddim_trailing, tcd, res_multistep and res_2s; 1 for euler_a and dpm++2s_a)
121121
--strength <float> strength for noising/unnoising (default: 0.75)
122122
--pm-style-strength <float>
123123
--control-strength <float> strength to apply Control Net (default: 0.9). 1.0 corresponds to full destruction of information in init image

examples/common/common.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ struct SDGenerationParams {
11971197
&sample_params.guidance.slg.layer_end},
11981198
{"",
11991199
"--eta",
1200-
"eta in DDIM, only for DDIM and TCD (default: 0)",
1200+
"noise multiplier (default: 0 for ddim_trailing, tcd, res_multistep and res_2s; 1 for euler_a and dpm++2s_a)",
12011201
&sample_params.eta},
12021202
{"",
12031203
"--flow-shift",
@@ -1229,7 +1229,7 @@ struct SDGenerationParams {
12291229
&high_noise_sample_params.guidance.slg.layer_end},
12301230
{"",
12311231
"--high-noise-eta",
1232-
"(high noise) eta in DDIM, only for DDIM and TCD (default: 0)",
1232+
"(high noise) noise multiplier (default: 0 for ddim_trailing, tcd, res_multistep and res_2s; 1 for euler_a and dpm++2s_a)",
12331233
&high_noise_sample_params.eta},
12341234
{"",
12351235
"--strength",

examples/server/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,15 @@ Default Generation Options:
189189
medium
190190
--skip-layer-start <float> SLG enabling point (default: 0.01)
191191
--skip-layer-end <float> SLG disabling point (default: 0.2)
192-
--eta <float> eta in DDIM, only for DDIM and TCD (default: 0)
192+
--eta <float> noise multiplier (default: 0 for ddim_trailing, tcd, res_multistep and res_2s; 1 for euler_a and dpm++2s_a)
193193
--flow-shift <float> shift value for Flow models like SD3.x or WAN (default: auto)
194194
--high-noise-cfg-scale <float> (high noise) unconditional guidance scale: (default: 7.0)
195195
--high-noise-img-cfg-scale <float> (high noise) image guidance scale for inpaint or instruct-pix2pix models (default: same as --cfg-scale)
196196
--high-noise-guidance <float> (high noise) distilled guidance scale for models with guidance input (default: 3.5)
197197
--high-noise-slg-scale <float> (high noise) skip layer guidance (SLG) scale, only for DiT models: (default: 0)
198198
--high-noise-skip-layer-start <float> (high noise) SLG enabling point (default: 0.01)
199199
--high-noise-skip-layer-end <float> (high noise) SLG disabling point (default: 0.2)
200-
--high-noise-eta <float> (high noise) eta in DDIM, only for DDIM and TCD (default: 0)
200+
--high-noise-eta <float> (high noise) noise multiplier (default: 0 for ddim_trailing, tcd, res_multistep and res_2s; 1 for euler_a and dpm++2s_a)
201201
--strength <float> strength for noising/unnoising (default: 0.75)
202202
--pm-style-strength <float>
203203
--control-strength <float> strength to apply Control Net (default: 0.9). 1.0 corresponds to full destruction of information in init image

src/denoiser.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ static bool sample_k_diffusion(sample_method_t method,
818818

819819
// get_ancestral_step
820820
float sigma_up, sigma_down;
821-
generate_ancestral_step(sigma_up, sigma_down, sigmas[i], sigmas[i + 1]);
821+
generate_ancestral_step(sigma_up, sigma_down, sigmas[i], sigmas[i + 1], eta);
822822

823823
// Euler method
824824
float dt = sigma_down - sigmas[i];
@@ -832,7 +832,7 @@ static bool sample_k_diffusion(sample_method_t method,
832832
}
833833
}
834834

835-
if (sigmas[i + 1] > 0) {
835+
if (sigmas[i + 1] > 0 && sigma_up > 0.0f) {
836836
// x = x + noise_sampler(sigmas[i], sigmas[i + 1]) * s_noise * sigma_up
837837
ggml_ext_im_set_randn_f32(noise, rng);
838838
// noise = load_tensor_from_file(work_ctx, "./rand" + std::to_string(i+1) + ".bin");
@@ -1010,7 +1010,7 @@ static bool sample_k_diffusion(sample_method_t method,
10101010

10111011
// get_ancestral_step
10121012
float sigma_up, sigma_down;
1013-
generate_ancestral_step(sigma_up, sigma_down, sigmas[i], sigmas[i + 1]);
1013+
generate_ancestral_step(sigma_up, sigma_down, sigmas[i], sigmas[i + 1], eta);
10141014
auto t_fn = [](float sigma) -> float { return -log(sigma); };
10151015
auto sigma_fn = [](float t) -> float { return exp(-t); };
10161016

@@ -1053,7 +1053,7 @@ static bool sample_k_diffusion(sample_method_t method,
10531053
}
10541054

10551055
// Noise addition
1056-
if (sigmas[i + 1] > 0) {
1056+
if (sigmas[i + 1] > 0 && sigma_up > 0.0f) {
10571057
ggml_ext_im_set_randn_f32(noise, rng);
10581058
{
10591059
float* vec_x = (float*)x->data;

src/stable-diffusion.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2777,6 +2777,7 @@ void sd_sample_params_init(sd_sample_params_t* sample_params) {
27772777
sample_params->scheduler = SCHEDULER_COUNT;
27782778
sample_params->sample_method = SAMPLE_METHOD_COUNT;
27792779
sample_params->sample_steps = 20;
2780+
sample_params->eta = INFINITY;
27802781
sample_params->custom_sigmas = nullptr;
27812782
sample_params->custom_sigmas_count = 0;
27822783
sample_params->flow_shift = INFINITY;
@@ -2953,6 +2954,21 @@ enum sample_method_t sd_get_default_sample_method(const sd_ctx_t* sd_ctx) {
29532954
return EULER_A_SAMPLE_METHOD;
29542955
}
29552956

2957+
static float sd_get_default_eta(enum sample_method_t sample_method) {
2958+
switch(sample_method) {
2959+
case DDIM_TRAILING_SAMPLE_METHOD:
2960+
case TCD_SAMPLE_METHOD:
2961+
case RES_MULTISTEP_SAMPLE_METHOD:
2962+
case RES_2S_SAMPLE_METHOD:
2963+
return 0.0f;
2964+
case EULER_A_SAMPLE_METHOD:
2965+
case DPMPP2S_A_SAMPLE_METHOD:
2966+
return 1.0f;
2967+
default:
2968+
return INFINITY;
2969+
}
2970+
}
2971+
29562972
enum scheduler_t sd_get_default_scheduler(const sd_ctx_t* sd_ctx, enum sample_method_t sample_method) {
29572973
if (sd_ctx != nullptr && sd_ctx->sd != nullptr) {
29582974
auto edm_v_denoiser = std::dynamic_pointer_cast<EDMVDenoiser>(sd_ctx->sd->denoiser);
@@ -3331,7 +3347,16 @@ sd_image_t* generate_image(sd_ctx_t* sd_ctx, const sd_img_gen_params_t* sd_img_g
33313347
if (sample_method == SAMPLE_METHOD_COUNT) {
33323348
sample_method = sd_get_default_sample_method(sd_ctx);
33333349
}
3334-
LOG_INFO("sampling using %s method", sampling_methods_str[sample_method]);
3350+
float eta = sd_img_gen_params->sample_params.eta;
3351+
float default_eta = sd_get_default_eta(sample_method);
3352+
if (default_eta != INFINITY) {
3353+
if (eta == INFINITY) {
3354+
eta = default_eta;
3355+
}
3356+
LOG_INFO("sampling using %s method (eta %g)", sampling_methods_str[sample_method], eta);
3357+
} else {
3358+
LOG_INFO("sampling using %s method", sampling_methods_str[sample_method]);
3359+
}
33353360

33363361
int sample_steps = sd_img_gen_params->sample_params.sample_steps;
33373362
std::vector<float> sigmas;
@@ -3546,7 +3571,7 @@ sd_image_t* generate_image(sd_ctx_t* sd_ctx, const sd_img_gen_params_t* sd_img_g
35463571
SAFE_STR(sd_img_gen_params->negative_prompt),
35473572
sd_img_gen_params->clip_skip,
35483573
guidance,
3549-
sd_img_gen_params->sample_params.eta,
3574+
eta,
35503575
sd_img_gen_params->sample_params.shifted_timestep,
35513576
width,
35523577
height,

0 commit comments

Comments
 (0)