Skip to content

Commit 663f749

Browse files
committed
handle rounding artifacts in alphas_cumprod
1 parent 9dcea94 commit 663f749

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

stable-diffusion.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,8 +716,24 @@ class StableDiffusionGGML {
716716

717717
auto comp_vis_denoiser = std::dynamic_pointer_cast<CompVisDenoiser>(denoiser);
718718
if (comp_vis_denoiser) {
719+
float* alphas_ptr = (float*)alphas_cumprod_tensor->data;
720+
bool needs_regen = false;
721+
722+
for (int i = 0; i < TIMESTEPS; ++i) {
723+
float alpha = alphas_ptr[i];
724+
if (!std::isfinite(alpha) || alpha <= 0.0f || alpha >= 1.0f) {
725+
needs_regen = true;
726+
break;
727+
}
728+
}
729+
730+
if (needs_regen) {
731+
calculate_alphas_cumprod(alphas_ptr);
732+
}
733+
719734
for (int i = 0; i < TIMESTEPS; i++) {
720-
comp_vis_denoiser->sigmas[i] = std::sqrt((1 - ((float*)alphas_cumprod_tensor->data)[i]) / ((float*)alphas_cumprod_tensor->data)[i]);
735+
float alpha = std::min(std::max(alphas_ptr[i], 1e-12f), 1.0f - 1e-12f);
736+
comp_vis_denoiser->sigmas[i] = std::sqrt((1.0f - alpha) / alpha);
721737
comp_vis_denoiser->log_sigmas[i] = std::log(comp_vis_denoiser->sigmas[i]);
722738
}
723739
}

0 commit comments

Comments
 (0)