fix: guard rescale_noise_cfg against zero std_cfg to prevent NaN propagation - #14334
Open
feiiiiii5 wants to merge 1 commit into
Open
fix: guard rescale_noise_cfg against zero std_cfg to prevent NaN propagation#14334feiiiiii5 wants to merge 1 commit into
feiiiiii5 wants to merge 1 commit into
Conversation
…gingface#13425) The rescale_noise_cfg function performs an unconditional division by std_cfg: noise_pred_rescaled = noise_cfg * (std_text / std_cfg) When noise_cfg has zero variance (e.g. all-zero tensor), std_cfg becomes 0, causing division by zero that produces NaN/inf values which silently corrupt the diffusion process. Fix: wrap the division in torch.where to fall back to the original noise_cfg when std_cfg is zero. This is mathematically correct: when noise_cfg is constant, there is nothing to rescale. Applied to all 36 copies of the function across pipelines and guiders. Fixes huggingface#13425 Signed-off-by: fei <204683769+feiiiiii5@users.noreply.github.com>
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.
Fixes #13425
Bug
rescale_noise_cfgperforms unconditional division bystd_cfg. Whennoise_cfghas zero variance,std_cfg=0produces NaN/inf that silently corrupts diffusion output.Fix
Wrap division in
torch.where(std_cfg > 0, ..., noise_cfg)— mathematically correct since constantnoise_cfgneeds no rescaling. Applied to all 36 copies across pipelines and guiders.Tests
6 regression tests in
test_rescale_noise_cfg.pycovering normal inputs, zero-variance, mixed batches, and source guard verification. All passing.AI Disclosure
This PR was developed with assistance from an AI coding agent.