Skip to content

fix: guard rescale_noise_cfg against zero std_cfg to prevent NaN propagation - #14334

Open
feiiiiii5 wants to merge 1 commit into
huggingface:mainfrom
feiiiiii5:fix/rescale-noise-cfg-zero-std-13425
Open

fix: guard rescale_noise_cfg against zero std_cfg to prevent NaN propagation#14334
feiiiiii5 wants to merge 1 commit into
huggingface:mainfrom
feiiiiii5:fix/rescale-noise-cfg-zero-std-13425

Conversation

@feiiiiii5

Copy link
Copy Markdown

Fixes #13425

Bug

rescale_noise_cfg performs unconditional division by std_cfg. When noise_cfg has zero variance, std_cfg=0 produces NaN/inf that silently corrupts diffusion output.

Fix

Wrap division in torch.where(std_cfg > 0, ..., noise_cfg) — mathematically correct since constant noise_cfg needs no rescaling. Applied to all 36 copies across pipelines and guiders.

Tests

6 regression tests in test_rescale_noise_cfg.py covering 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.

…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>
@github-actions github-actions Bot added size/L PR with diff > 200 LOC fixes-issue tests pipelines guiders and removed size/L PR with diff > 200 LOC labels Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Division by zero in rescale_noise_cfg can produce NaNs during inference

1 participant