huggingface / huggingface/diffusers

Division by zero in rescale_noise_cfg can produce NaNs during inference

Open Beginner friendly
#13,425 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
34.5k
Forks
7.3k
Avg merge
3d 3h
Merged PRs (30d)
91

Description

Describe the bug

Bug: Division by zero in rescale_noise_cfg can produce NaNs

Description

The function rescale_noise_cfg performs a division by std_cfg without any numerical stability guard:

noise_pred_rescaled = noise_cfg * (std_text / std_cfg)

If std_cfg becomes zero, this leads to NaN or inf values, which can silently corrupt the diffusion process and produce invalid outputs.

This can happen in edge cases where noise_cfg has zero variance.

Reproduction
import torch

noise_cfg = torch.zeros(1, 4, 64, 64)  # std = 0
noise_pred_text = torch.randn_like(noise_cfg)

std_text = noise_pred_text.std(dim=list(range(1, noise_pred_text.ndim)), keepdim=True)
std_cfg = noise_cfg.std(dim=list(range(1, noise_cfg.ndim)), keepdim=True)

result = noise_cfg * (std_text / std_cfg)
print(result)
Logs

System Info
  • Diffusers: main
  • PyTorch: any
  • OS: any
Who can help?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Locate the rescale_noise_cfg entry point in the Diffusers source and run the supplied PyTorch reproduction with a zero-variance noise_cfg. Trace the existing handling of std_cfg and add coverage for this edge case. Done means the zero-variance path no longer produces NaN or infinite values during inference.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.