Fix division by zero in rescale_noise_cfg causing NaNs during inference#13426
Open
Akash504-ai wants to merge 2 commits intohuggingface:mainfrom
Open
Fix division by zero in rescale_noise_cfg causing NaNs during inference#13426Akash504-ai wants to merge 2 commits intohuggingface:mainfrom
Akash504-ai wants to merge 2 commits intohuggingface:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
This PR fixes a numerical stability issue in
rescale_noise_cfgthat can lead to division by zero and produce NaN/inf values during inference.Problem
The current implementation performs:
noise_pred_rescaled = noise_cfg * (std_text / std_cfg)
If
std_cfgbecomes zero (e.g., whennoise_cfghas zero variance), this results in division by zero, producing NaNs that can silently corrupt the diffusion process.Solution
A small epsilon value is added to the denominator to ensure numerical stability:
noise_pred_rescaled = noise_cfg * (std_text / (std_cfg + 1e-6))
Scope
This fix is applied consistently across:
src/diffusers/guiders/guider_utils.pysrc/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.pysrc/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.pyImpact
std_cfg > 0Fixes #13425
@sayakpaul @yiyixuxu