Comfy-Org / Comfy-Org/ComfyUI

No seed gets set in custom/advanced samplers when add_noise is disabled

Open
#2,833 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
133k
Forks
15.7k
Avg merge
1d 6h
Merged PRs (30d)
155

Description

the common sampler code has:

```python
if disable_noise:
noise = torch.zeros(latent_image.size(), dtype=latent_image.dtype, layout=latent_image.layout, device="cpu")
else:
batch_inds = latent["batch_index"] if "batch_index" in latent else None
noise = comfy.sample.prepare_noise(latent_image, seed, batch_inds)
```

`prepare_noise` is what sets the global RNG that various thing such as ancestral samplers use, so when `add_noise` is disabled this means no seed gets set before sampling and the RNG is an undefined state. in other words, if you turn off `add_noise` and use a sampler like `euler_a` it's impossible to reproduce generations.

it's easy to reproduce: just use an advanced or custom sampler, set a fixed seed, turn off `add_noise` and use `euler_a`. start generating and look at the preview. abort it at 50% or so and restart, you will get a completely new generation each time.

is there any reason not to just do `torch.manual_seed(seed)` in the `disable_noise` code path to ensure the RNG is in a defined state based on the seed the user set?

~~i made a simple work around ( https://github.com/blepping/ComfyUI-bleh#blehforceseedsampler ) but it would be nice if that wasn't necessary.~~ *edit: my workaround doesn't seem to work reliably*

see below for a visual demonstration of the issue. ignore the "Noisy Latent Image" node, it's not connected to anything. the seed comes from a `Seed Everywhere` node near the top right (set to 0)

## current behavior

![image](https://github.com/comfyanonymous/ComfyUI/assets/157360029/b26e81ac-7507-47b1-a0ea-e29e4b22dca2)

## with #2841

![image](https://github.com/comfyanonymous/ComfyUI/assets/157360029/7e274a99-a00f-485a-bbc6-98187f518371)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.