Guard against zero std_cfg in rescale_noise_cfg (#13425)#13704
Open
jbbqqf wants to merge 1 commit intohuggingface:mainfrom
Open
Guard against zero std_cfg in rescale_noise_cfg (#13425)#13704jbbqqf wants to merge 1 commit intohuggingface:mainfrom
std_cfg in rescale_noise_cfg (#13425)#13704jbbqqf wants to merge 1 commit intohuggingface:mainfrom
Conversation
`rescale_noise_cfg` divides `std_text` by `std_cfg` without any numerical guard. When `noise_cfg` has zero variance (e.g. a constant or zero-valued guided prediction at the start of a schedule), `std_cfg` is `0`, the division produces `nan`/`inf`, and `noise_cfg * (std_text / 0)` silently corrupts the diffusion output (huggingface#13425). Clamp `std_cfg` to `torch.finfo(noise_cfg.dtype).eps` before dividing so the rescaling becomes a no-op in the degenerate case (`noise_cfg == 0` produces `0 * <finite> == 0`) instead of `nan`. Propagated to all 32 in-tree copies via `python utils/check_copies.py --fix_and_overwrite`.
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.
Summary
rescale_noise_cfgdividesstd_textbystd_cfgwithout any numerical guard:If
noise_cfghas zero variance (e.g. a constant or zero-valued guided prediction at the start of a schedule, or numerical edge cases),std_cfg == 0, the division producesnan/inf, and the silent corruption propagates through the rest of the diffusion process — no exception is raised.Fix
Clamp
std_cfgtotorch.finfo(noise_cfg.dtype).epsbefore the divide. In the degenerate case the rescaling becomes a no-op (0 * <finite> == 0) instead ofnan. In the non-degenerate case the clamp is below numerical noise, so the rescaling result is unchanged in practice.The fix was applied to the canonical copy in
pipelines/stable_diffusion/pipeline_stable_diffusion.pyand propagated to all 31 "# Copied from" duplicates viapython utils/check_copies.py --fix_and_overwrite.python utils/check_copies.py(no flags) is green.Reproduce BEFORE/AFTER yourself (copy-paste)
I confirmed both runs locally:
origin/main→any nan: Trueany nan: False, any inf: False, max abs: 0.0A second sanity check with a non-degenerate
noise_cfg = torch.randn(...)produces an unchanged result on this branch (the clamp is below numerical noise).Notes
🤖 Disclosure: Authored with assistance from Claude (Anthropic). Reproducer was run against both
origin/mainand this branch end-to-end.