Skip to content

Commit da95169

Browse files
committed
fix: fix schedulers when sigma_min gets too close to zero
1 parent d377b87 commit da95169

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

denoiser.hpp

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,11 @@ struct SmoothStepScheduler : SigmaScheduler {
347347
}
348348
};
349349

350-
// KL Optimal: https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/15608
350+
/*
351+
* KL Optimal:
352+
* Original work from https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/15608.
353+
* Implemented using https://github.com/comfyanonymous/ComfyUI/pull/6206 as a reference.
354+
*/
351355
struct KLOptimalScheduler : SigmaScheduler {
352356
std::vector<float> get_sigmas(uint32_t n, float sigma_min, float sigma_max, t_to_sigma_t t_to_sigma) override {
353357
std::vector<float> sigmas;
@@ -367,39 +371,15 @@ struct KLOptimalScheduler : SigmaScheduler {
367371
float alpha_min = std::atan(sigma_min);
368372
float alpha_max = std::atan(sigma_max);
369373

370-
if (sigma_min <= 1e-6f) {
371-
/* sigma_min = 0:
372-
* implemented using
373-
* https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/15608
374-
* as a reference.
375-
*/
376-
377-
LOG_INFO("KL Optimal with sigma_min close to zero");
378-
for (uint32_t i = 0; i <= n; ++i) {
379-
380-
float t = static_cast<float>(i) / static_cast<float>(n);
381-
float angle = t * alpha_min + (1.0f - t) * alpha_max;
382-
sigmas.push_back(std::tan(angle));
383-
}
384-
385-
} else {
386-
/* sigma_min != 0:
387-
* implemented using
388-
* https://github.com/comfyanonymous/ComfyUI/pull/6206
389-
* as a reference.
390-
*/
391-
392-
LOG_INFO("KL Optimal with sigma_min greater than zero");
393-
for (uint32_t i = 0; i < n; ++i) {
394-
395-
float t = static_cast<float>(i) / static_cast<float>(n - 1);
396-
float angle = t * alpha_min + (1.0f - t) * alpha_max;
397-
sigmas.push_back(std::tan(angle));
398-
}
374+
for (uint32_t i = 0; i < n; ++i) {
399375

400-
sigmas.push_back(0.0f);
376+
float t = static_cast<float>(i) / static_cast<float>(n - 1);
377+
float angle = t * alpha_min + (1.0f - t) * alpha_max;
378+
sigmas.push_back(std::tan(angle));
401379
}
402380

381+
sigmas.push_back(0.0f);
382+
403383
return sigmas;
404384
}
405385
};
@@ -481,6 +461,10 @@ struct CompVisDenoiser : public Denoiser {
481461
}
482462

483463
float sigma_to_t(float sigma) override {
464+
if (sigma <= 1e-6f) {
465+
return (float)(TIMESTEPS - 1);
466+
}
467+
484468
float log_sigma = std::log(sigma);
485469
std::vector<float> dists;
486470
dists.reserve(TIMESTEPS);
@@ -756,8 +740,12 @@ static bool sample_k_diffusion(sample_method_t method,
756740
float* vec_x = (float*)x->data;
757741
float* vec_denoised = (float*)denoised->data;
758742

759-
for (int i = 0; i < ggml_nelements(d); i++) {
760-
vec_d[i] = (vec_x[i] - vec_denoised[i]) / sigma;
743+
if (sigma < 1e-6f) {
744+
ggml_set_f32(d, 0.0f);
745+
} else {
746+
for (int i = 0; i < ggml_nelements(d); i++) {
747+
vec_d[i] = (vec_x[i] - vec_denoised[i]) / sigma;
748+
}
761749
}
762750
}
763751

0 commit comments

Comments
 (0)