fix: avoid issues when sigma_min is close to 0 (#1138)

This commit is contained in:
Daniele 2026-01-04 15:05:01 +01:00 committed by GitHub
parent 6eefd2d49a
commit a119a4da9a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -276,6 +276,10 @@ struct KarrasScheduler : SigmaScheduler {
// but does anybody ever bother to touch them? // but does anybody ever bother to touch them?
float rho = 7.f; float rho = 7.f;
if (sigma_min <= 1e-6f) {
sigma_min = 1e-6f;
}
std::vector<float> result(n + 1); std::vector<float> result(n + 1);
float min_inv_rho = pow(sigma_min, (1.f / rho)); float min_inv_rho = pow(sigma_min, (1.f / rho));
@ -347,7 +351,6 @@ struct SmoothStepScheduler : SigmaScheduler {
} }
}; };
// Implementation adapted from https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/15608
struct KLOptimalScheduler : SigmaScheduler { struct KLOptimalScheduler : SigmaScheduler {
std::vector<float> get_sigmas(uint32_t n, float sigma_min, float sigma_max, t_to_sigma_t t_to_sigma) override { std::vector<float> get_sigmas(uint32_t n, float sigma_min, float sigma_max, t_to_sigma_t t_to_sigma) override {
std::vector<float> sigmas; std::vector<float> sigmas;
@ -355,27 +358,28 @@ struct KLOptimalScheduler : SigmaScheduler {
if (n == 0) { if (n == 0) {
return sigmas; return sigmas;
} }
if (n == 1) { if (n == 1) {
sigmas.push_back(sigma_max); sigmas.push_back(sigma_max);
sigmas.push_back(0.0f); sigmas.push_back(0.0f);
return sigmas; return sigmas;
} }
if (sigma_min <= 1e-6f) {
sigma_min = 1e-6f;
}
sigmas.reserve(n + 1);
float alpha_min = std::atan(sigma_min); float alpha_min = std::atan(sigma_min);
float alpha_max = std::atan(sigma_max); float alpha_max = std::atan(sigma_max);
for (uint32_t i = 0; i < n; ++i) { for (uint32_t i = 0; i < n; ++i) {
// t goes from 0.0 to 1.0
float t = static_cast<float>(i) / static_cast<float>(n - 1); float t = static_cast<float>(i) / static_cast<float>(n - 1);
// Interpolate in the angle domain
float angle = t * alpha_min + (1.0f - t) * alpha_max; float angle = t * alpha_min + (1.0f - t) * alpha_max;
// Convert back to sigma
sigmas.push_back(std::tan(angle)); sigmas.push_back(std::tan(angle));
} }
// Append the final zero to sigma
sigmas.push_back(0.0f); sigmas.push_back(0.0f);
return sigmas; return sigmas;