comfyanonymous / ComfyUI

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface.
https://www.comfy.org/
GNU General Public License v3.0
52.62k stars 5.56k forks source link

Undeterministic Output using Ancestral Types and Potential Fix #1329

Open bmad4ever opened 1 year ago

bmad4ever commented 1 year ago

I noticed when using ancestral samplers with KSampler (Advanced) and w/ noise disabled that the output was nondetermistic.

Would using the Ksampler node seed, passed in extra_args, be a “legit” way to solve this for ancestral samplers? The default_noise_sampler method could have it has an optional arg to not mess with other methods that use it and do not receive the seed.

But I don't know if there is some intended/planned way to go about the problem, possibly to output the same as other tools, or, since there is an argument for an alternative noise sampler, maybe this would be something to be solved in the future.

The potential solution:

def default_noise_sampler(x, seed=None):
    if seed is not None:
        torch.manual_seed(seed)
    return lambda sigma, sigma_next: torch.randn_like(x)
noise_sampler = default_noise_sampler(x, extra_args["seed"]) if noise_sampler is None else noise_sampler
bmad4ever commented 1 year ago

On second thought, and having read some previously closed issues, maybe this would be preferable:

class CPU_DefaultNoiseSampler:
    def __init__(self, x, seed=0):
        self.noise_seed_offset = -1
        self.seed = seed
        self.shape = x.shape

    def sampler(self, sigma, sigma_next):
        self.noise_seed_offset += 1
        return torch.randn(*self.shape,
                           device='cpu',
                           generator=torch.manual_seed(self.seed + self.noise_seed_offset)
                           ).to(device=comfy.model_management.get_torch_device())

I'm still not sure this fits within the intended design, otherwise, I would have made a pull request. But may be of use to people who need consistent outputs in ancestral samplers.

bmad4ever commented 1 year ago

After some testing, I noticed that on some workflows (img2img and the like) the previous solution generates noisy results that previously were clean (both when using the source solution or using the first fix suggestion).

I tried a generator instead of incrementing the seed 1 by 1, which I conjectured would be the source of some bias/likeness in the generated noise, but the behavior persisted. I don't know why, when using a generator, this approach consistently gives me noisy outputs on img2img-like workflows. Can someone shed some light on this?

...
        self.seed_generator = torch.Generator(device='cpu')
        self.seed_generator.manual_seed(seed)

    def sampler(self, sigma, sigma_next):
        return torch.randn(*self.shape,
                           device='cpu',
                           generator=self.seed_generator
...