Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undeterministic Output using Ancestral Types and Potential Fix #1329

Open
bmad4ever opened this issue Aug 25, 2023 · 2 comments
Open

Undeterministic Output using Ancestral Types and Potential Fix #1329

bmad4ever opened this issue Aug 25, 2023 · 2 comments

Comments

@bmad4ever
Copy link

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
Copy link
Author

bmad4ever commented Aug 28, 2023

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
Copy link
Author

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
...

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

No branches or pull requests

1 participant