Sygil-Dev / stable-diffusion

GNU Affero General Public License v3.0
1.72k stars 149 forks source link

Installation Instructions for Mac #173

Closed johnhorsema closed 1 year ago

johnhorsema commented 1 year ago

Thanks for releaseing this stable diffusion model. Was wondering if there is equivalent webui.cmd for Mac?

tstrimple commented 1 year ago

There isn't one included. You could create a bash script which would work for both Linux and Mac. If you look at the webui.cmd file it's setting up some environment variables and the conda environment before invoking python scripts/relauncher.py. I already had the environment setup, so I just ran relauncher.py directly.

JoshuaKimsey commented 1 year ago

I already have the Linux Bash script made, but it was made explicitly with Linux in mind, not Mac. You'll have to do some work to make it work with a Mac. Further still, does Stable Diffusion even run correctly on an M1/M2 Mac? I thought that was still a planned feature sometime later on. Unless you have a Mac Pro, or an older Mac, which in that case you'll need to set it up to run with AMD hardware, which can be done.

fragmede commented 1 year ago

StableDiffusions itself works fine on Apple Silicon using the MPS backend.

JoshuaKimsey commented 1 year ago

Interesting! What's the performance like on Apple Silicon? It is able to make use of their AI Co-Processors or is it just using it like a normal CPU to render?

fragmede commented 1 year ago

Happy to run a specific benchmark on my M1, but it takes O(minutes) to run txt2img using their AI Co-processors (vs 10's of minutes using just the cpu).

rudolfolah commented 1 year ago

@fragmede did you have to modify the environment.yaml to remove the cudatoolkit package in order for the Conda environment creation to work? Was there another change you had to make to change the backend to use MPS?

fragmede commented 1 year ago

@rudolfolah I used environment-mac.yaml from https://github.com/fragmede/stable-diffusion/tree/mps_consistent_seed

rudolfolah commented 1 year ago

@fragmede awesome, thanks for the link, I tried out the branch and it ended up working with this: PYTORCH_ENABLE_MPS_FALLBACK=1 python scripts/orig_scripts/txt2img.py --prompt "turtle drinking wine"

It took over 30min to generate 6 samples, seemed like a lot of ops fall back to cpu? It's still very cool to see progress, especially since PyTorch's official support for M1s started recently: https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/

rudolfolah commented 1 year ago

someone posted instructions on their blog, which just made the frontpage of Hacker News: https://replicate.com/blog/run-stable-diffusion-on-m1-mac

the repo is here: ~wrong repo~ https://github.com/magnusviri/stable-diffusion

I haven't dug into it yet, so I'm not sure what changes they've made to ensure that MPS is used rather than CPU.

ideologysec commented 1 year ago

I think most of the work for this is happening in lstein's fork: https://github.com/lstein/stable-diffusion/

the main differences are making sure to use the pytorch nightly, and ensuring that you're passing PYTORCH_ENABLE_MPS_FALLBACK=1 because not all PyTorch operators are implemented for the MPS backend yet. (which is just the Apple GPU backend, not the sweet NPU/dedicated Neural Engine). There also needs to be some logic implemented for choosing the backend device (which is actually the hard part, since there are a few files that need changing).

I don't know how well it would play with the additional GAN filters; sourcing macOS/Apple Silicon specific versions might be necessary. waifu2x might be able to help here.

I would love to see some of that work ported here; the WebUI is awesome.

JoshuaKimsey commented 1 year ago

Imagine Stable Diffusion running on the Apple NPU (the thing I've been referring too as Apple's Co-Processor). That would be truly amazing too see. All the more so, given Emmad has talked about wanting to get the Model sizes down to a low as 100MB. That could open up doing stuff like this on mobile devices like the iPhone, or the Google Pixel 6 using it's Tensor Processors.

swyxio commented 1 year ago

just commenting to confirm that copying mac-environment.yml and then conda env create -f mac-environment.yaml worked for me. ( i also entered a venv first)

however i was unable to install gradio in conda, and all the gradio instructions are using pip install.

image

rudolfolah commented 1 year ago

the scripts need to be updated to check for MPS vs CUDA, the environments-mac.yaml looks alright, I also saw the same issue with gradio not being installed when running the webui script. The txt2img script in the other repo was updated to check for MPS: https://github.com/lstein/stable-diffusion/commit/bdb0651eb2120a5366dc64067add74fecf66db81

fourseven commented 1 year ago

just commenting to confirm that copying mac-environment.yml and then conda env create -f mac-environment.yaml worked for me. ( i also entered a venv first)

however i was unable to install gradio in conda, and all the gradio instructions are using pip install.

image

To solve this I added - defaults to the bottom of the channels section in the copied environment-mac.yaml from the other repo which then allowed gradio to install through conda.

As @rudolfolah said though, there's a lot of places which are hard-coded to cuda in this repo so it's a bit of work to switch it over at the moment.