invoke-ai / InvokeAI

Invoke is a leading creative engine for Stable Diffusion models, empowering professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry leading WebUI, and serves as the foundation for multiple commercial products.
https://invoke-ai.github.io/InvokeAI/
Apache License 2.0
23.44k stars 2.41k forks source link

Setting seed does not work on Mac #317

Closed yousifa closed 2 years ago

yousifa commented 2 years ago

Setting seed in dream.py command does not generate same image.

For example, running the below command multiple times will result in a different image every time.

"jackie chan as president of the united states in the year 2100" -s10 -W512 -H512 -C7.5 -Ak_lms -F -S85924511
asimilon commented 2 years ago

From the README-Mac-MPS.md:

How come --seed doesn't work?

First this:

Completely reproducible results are not guaranteed across PyTorch releases, individual commits, or different platforms. Furthermore, results may not be reproducible between CPU and GPU executions, even when using identical seeds.

lstein commented 2 years ago

I don't have a Mac to test on, but it sounds as though the user is complaining that within a single session (no differences in pytorch releases, commits, etc) the same seed is not resulting in the same image. @magnusviri could you check this out?

asimilon commented 2 years ago
image

"there is a fly in my soup" KLMS Sampler - 20 steps Seed 1234 for both images

I think I had misunderstand this

results may not be reproducible between CPU and GPU executions, even when using identical seeds

as meaning that each time it executes it may not be reproducible, but it probably means different results running on CPU and GPU.

yousifa commented 2 years ago

I don't have a Mac to test on, but it sounds as though the user is complaining that within a single session (no differences in pytorch releases, commits, etc) the same seed is not resulting in the same image. @magnusviri could you check this out?

Correct, within a single session the same seed + prompt always reproduces different images.

magnusviri commented 2 years ago

The seed doesn't work in dream.py. I looked at it yesterday. I don't know pytorch yet. But this is what someone came up with for txt2img. First, move seed_everything after the model is loaded.

 model = load_model_from_config(config, f"{opt.ckpt}")

seed_everything(opt.seed)

Then this

  if opt.fixed_code:
        shape = [opt.n_samples, opt.C, opt.H // opt.f, opt.W // opt.f]
        if device.type == 'mps':
            start_code = torch.randn(shape, device='cpu').to(device)
        else:
            torch.randn(shape, device=device)

torch.randn isn't in dream.py so I'm not sure how to apply this change to it.

yousifa commented 2 years ago

The seed doesn't work in dream.py. I looked at it yesterday. I don't know pytorch yet. But this is what someone came up with for txt2img. First, move seed_everything after the model is loaded.


 model = load_model_from_config(config, f"{opt.ckpt}")

seed_everything(opt.seed)

Then this


  if opt.fixed_code:

        shape = [opt.n_samples, opt.C, opt.H // opt.f, opt.W // opt.f]

        if device.type == 'mps':

            start_code = torch.randn(shape, device='cpu').to(device)

        else:

            torch.randn(shape, device=device)

torch.randn isn't in dream.py so I'm not sure how to apply this change to it.

I tried a similar change for this yesterday, but it made no difference. Seed_everything is run after model is loaded anyway, since model is loaded at start of dream.py

lstein commented 2 years ago

The seed_everyting() call before loading the model is a redundant piece of code. It can probably be removed entirely. The actual seed is set after the generation command is parsed and before the generation starts at line 334 in simplet2i.py (on development branch)

I don't observe this on Linux platforms. Has anyone seen this undesirable behavior on Windows systems? If not, it is a Mac-specific bug. If this is the case, then I'm afraid it must be a problem in the Mac version of the pytorch library, and needs to be reported upstream in some way.

i3oc9i commented 2 years ago

I have a Mac M1, and I got the good Anubis when I use the seed

So, I have a question, if seed work in txt2img.py but doesnot in dream.py, why this should be an issue with pytorch library ?

i3oc9i commented 2 years ago

The commit d66010410c4494e53634c8aeabea585e89e6e612 of the development branch solves this seed issue when using dream.py. (txt2img.py also give the good Anibus)

python scripts/dream.py --full_precision
dream> "a nice girl with flowers in the hairs, by paolo roversi" -s50 -W512 -H512 -C7.5 -Ak_lms -S12345678

000001 12345678

But curiosly if I quit dream.py , and then I start again reapting the same prompt I get a slight different image, but if I repeat one more time, then I get the same image of the previous run. This behaviour is reproduced at each new run of dream.py

000003 12345678

pytorch                   1.13.0.dev20220902         py3.9_0    pytorch-nightly
pytorch-lightning         1.6.5              pyhd8ed1ab_0    conda-forge
torch-fidelity            0.3.0                    pypi_0    pypi
torchdiffeq               0.2.3                    pypi_0    pypi
torchmetrics              0.9.3              pyhd8ed1ab_0    conda-forge
torchvision               0.14.0.dev20220902        py39_cpu    pytorch-nightly

I'm very happy you found a solution, thank you so much for this hard work

i3oc9i commented 2 years ago

SD-Dream Version 1.13 has exactly the same behaviour described in my previous comment

It is consistent but I get a different Image (note the nightly version of pytorch changed) Screen Shot 2022-09-03 at 19 01 41 PM

Indeed, when you start dream.py you get the SAME(Blue) version of the image, then when you repeat the prompt you get the SAME(Red) version of the image. If you look attentively there is a minor difference in the hand.

python scripts/dream.py --full_precision
dream> "a nice girl with flowers in the hairs, by paolo roversi" -s50 -W512 -H512 -C7.5 -Ak_lms -S12345678
pytorch                   1.13.0.dev20220903         py3.9_0    pytorch-nightly
pytorch-lightning         1.6.5              pyhd8ed1ab_0    conda-forge
torch-fidelity            0.3.0                    pypi_0    pypi
torchdiffeq               0.2.3                    pypi_0    pypi
torchmetrics              0.9.3              pyhd8ed1ab_0    conda-forge
torchvision               0.14.0.dev20220903        py39_cpu    pytorch-nightly

I'm happy with this, well done...

magnusviri commented 2 years ago

Yes, this is fixed in the development branch. img2img.py is not fixed because the code is a little bit more complex and I just didn't feel like cleaning it up.

XReyRobert-IBM commented 2 years ago

in fact the issue seems to be related to the sampler used . I was able to use seeding successfully with the k_lms sampler but failed for some of the others (Haven't done an extensive check on all of them)

tedd4u commented 2 years ago

in fact the issue seems to be related to the sampler used . I was able to use seeding successfully with the k_lms sampler but failed for some of the others (Haven't done an extensive check on all of them)

Really appreciate the fixes going in for this @magnusviri - thank you. I tested 4 samplers and I now get the same image for a given seed with sampler k_lms, k_euler, and ddim, but not with k_euler_a.

I'm using HEAD of development branch * main 4406fd1 [origin/development] Merge branch 'SebastianAigner-main' into development Add support for full CORS headers for dream server.

Hope this is helpful.

Any-Winter-4079 commented 2 years ago

This should be fixed in the development branch. If not, please reopen the issue.