FahimF / sd-gui

Clean and simple Stable Diffusion GUI for macOS, Windows, and Linux
MIT License
230 stars 17 forks source link

Seeds don't seem to work very well on Apple Silicon #4

Closed HugoSega closed 1 year ago

HugoSega commented 1 year ago

Thanks Fahim for putting all this together.

here is the fix for the seeds to work properly. Seeds don't seem to work very well on M1s, but you can re-generate an image by changing in txt2img.py

Change start_code = torch.randn([opt.n_samples, opt.C, opt.H // opt.f, opt.W // opt.f], device=device) to: start_code = torch.randn([opt.n_samples, opt.C, opt.H // opt.f, opt.W // opt.f], device="cpu").to(torch.device("mps"))

And then, move seed_everything(opt.seed) ``below model = load_model_from_config(config, f"{opt.ckpt}") below model = load_model_from_config(config, f"{opt.ckpt}")

For img2img.py, change

z_enc = sampler.stochastic_encode(init_latent, torch.tensor([t_enc]*batch_size).to(device)) to: z_enc = sampler.stochastic_encode(init_latent, torch.tensor([t_enc] * batch_size).to(device), noise=torch.randn_like(init_latent, device="cpu").to(device) if opt.fixed_code else None,)

Thanks again Fahim !

FahimF commented 1 year ago

Thank you for the instructions but the instructions you provided don't mean anything since the lines to be changed are not lines from my files/code 🙂 It doesn't tell me what needs to be fixed generally since how I generate the seed in my code is different.

Was this copied from somewhere? If so, can you please let me know the source that I can try to figure out what is needed?

HugoSega commented 1 year ago

When you want to regenerate exactly the same image using seed number it does not work on Apple silicon unless a few changes are made as mentioned above here is the link. I tried it and it works perfectly well.

Update #3 (Seeds / Generating same images) https://www.reddit.com/r/StableDiffusion/comments/wx0tkn/stablediffusion_runs_on_m1_chips/

FahimF commented 1 year ago

I assume when you say "I tried", you tried with the lstein code and not mine? The lstein code uses a different approach to mine - not all Stable Diffusion code is the same. I'll try to figure out what changed but it depends on what really changed for the other codebase. Will take a look and see.

HugoSega commented 1 year ago

That is correct I did not try it on your code. Can you guide me to where is the txt2img.py file located in your repo, I am not a programmer but like to experiment with things. Thank you in advance and Thank you Fahim for your time and for the great GUI.

FahimF commented 1 year ago

That's what I meant — my code is completely different to lstein's 🙂 There is no txt2img.py file because mine is not based off of one of the other SD repos out there. So I'd need to figure out what changes lstein made and why to get seeds working. I do know that the seeds aren't working correctly on Apple, but figured that the issue was something at the PyTorch end since their Apple support is still evolving ...

Thank you for letting me know about the lstein fix. I'll see if I can find a way to replicate it but it might not work at my end due to my approach being different. If I do manage to get it working and update the code, I'll ping you in this ticket so that you can update from the repo 🙂

HugoSega commented 1 year ago

Thank you very much.

Just found this also you might be interested. https://github.com/lkwq007/stablediffusion-infinity

FahimF commented 1 year ago

Oooh, that is nice 😄 I did want to implement outpainting (and inpainting) at some point but that was going to be down the line since I'm doing a re-write of the GUI to support prompt/image handling. That does help if their approach will mesh with mine. Will add to the to-do list ... So many things to do, but I guess we'll get there eventually 😛

HugoSega commented 1 year ago

Can't wait :) one of the best GUI's tried on Apple silicon. Good luck with everything!

FahimF commented 1 year ago

Thanks! And thank you for pointing out the random seed and the outpainting stuff — I've been head down coding when I have the time that I wasn't keeping up with other stuff going on. And once the new GUI is out, if you have suggestion/feature requests, let me know and I'll see what I can do.

I'm trying to create something which allows creative people to just make images rather than worry about the code ... But not sure if that's how it will come across 😄 Just creating something that works for me, and hopefully, works for other people too.

HugoSega commented 1 year ago

I am a big fan of your work, really well done and beautiful, especially for version 1 (really unbelievable job). If I find anything new or interesting or whatever I will let you know right away. Thanks and keep up the great work.

FahimF commented 1 year ago

Thank you 😄

FahimF commented 1 year ago

Just letting you know that the seed issue should be fixed 🙂 Apparently it's a PyTorch issue that they might or might not fix but there's a workaround and so far it seems to work correctly when I tested. Thanks for pointing me in the right direction!

HugoSega commented 1 year ago

I am really glad I was helpful, but you're the one who is doing the hard work, and it took you less than a day to fix it. Thank you.

HugoSega commented 1 year ago

just tested with Python 3.9.12, and everything works perfectly well

FahimF commented 1 year ago

Great! I'm glad that it went so smoothly 😄 I've had so many issues at my end getting things to work on anything other than Python 3.8.8 that I've been sticking to that since I don't want to spend time trying to struggle with Python.