Skip to content

Fix division by zero in rescale_noise_cfg causing NaNs during inference#13426

Open
Akash504-ai wants to merge 2 commits intohuggingface:mainfrom
Akash504-ai:fix-rescale-noise-nan
Open

Fix division by zero in rescale_noise_cfg causing NaNs during inference#13426
Akash504-ai wants to merge 2 commits intohuggingface:mainfrom
Akash504-ai:fix-rescale-noise-nan

Conversation

@Akash504-ai
Copy link
Copy Markdown

What does this PR do?

This PR fixes a numerical stability issue in rescale_noise_cfg that 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_cfg becomes zero (e.g., when noise_cfg has 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.py
  • src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py
  • src/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py

Impact

  • Prevents NaN/inf propagation during inference
  • Improves numerical stability in edge cases
  • No impact on normal behavior when std_cfg > 0

Fixes #13425

@sayakpaul @yiyixuxu

@sayakpaul sayakpaul requested a review from yiyixuxu April 6, 2026 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

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