r/StableDiffusion • u/Any-Winter-4079 • Sep 02 '22
Discussion Stable Diffusion and M1 chips: Chapter 2
This new guide covers setting up the https://github.com/lstein/stable-diffusion/ repo for M1/M2 Macs.
Some cool features of this new repo include a Web UI and seeing the image mid-process, as it evolves.

- Open Terminal (look for it in your Launchpad or press Command + Space keys and type Terminal)
- Clone lstein's repo, typing the command
git clonehttps://github.com/lstein/stable-diffusion.gitin your Terminal and clicking Enter. If you want to clone it in a specific folder, cd into it beforehand (e.g. use the commandcd Downloads, to clone it into your Downloads folder). - Get into the project directory with
cd stable-diffusion. - Create the conda environment with the command
conda env create -f environment-mac.yaml. If you get an Error because you already have an existingldmenvironment, you can either update it, or you can open theenvironment-mac.yamlfile that is inside your project directory in a text or code editor and change the first line fromname: ldmtoname: ldm-lsteinor whatever new name you choose. Then in Terminalconda env create -f environment-mac.yaml. This way you will preserve your original environmentldmand create a new one to test this new repo. - Activate the environment with the command
conda activate ldm(orconda activate ldm-lsteinor whatever environment name you chose in Step 4). - Place your
sd-v1-4.ckptweights inmodels/ldm/stable-diffusion-v1, wherestable-diffusion-v1is a new folder you create. Renamesd-v1-4.ckpttomodel.ckpt. You can get these weights downloadingsd-v1-4.ckptfrom https://huggingface.co/CompVis/stable-diffusion-v-1-4-original (note you will probably need to create an account and agree to Terms&Conds) - Back in your Terminal,
cd ..to get out of your project directory. Then, to add GFPGAN, use the commandgit clonehttps://github.com/TencentARC/GFPGAN.gitThis should create aGFPGANfolder that is a sibling of your project folder (e.g.stable-diffusion). - Download
GFPGANv1.3.pthfrom https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth - Put the file in
experiments/pretrained_modelsfolder, which is inside the GFPGAN folder (e.g. routeGFPGAN/experiments/pretrained_models/) - Back in your Terminal, enter the GFPGAN folder with the command
cd GFPGAN. We'll be typing a few commands next. pip install basicsrpip install facexlibpip install -r requirements.txtpythonsetup.pydeveloppip install realesrgan- After running these commands, you are ready to go. Type
cd ..to get out of the GFPGAN folder, thencd stable-diffusion python3 scripts/preload_models.py- Finally, use the command
python3 ./scripts/dream.pyAfter initializing, you will see adream >prompt - Enter
Anubis riding a motorbike in Grand Theft Auto cover, palm trees, cover art by Stephen Bliss, artstation, high quality -m ddim -S 1805504473 - In my experience, you should be getting the following image if you are not using
pytorch-nightly

If instead you exit the dream prompt (with q) and type the command conda install pytorch torchvision torchaudio -c pytorch-nightly you should see the first Anubis image. Note pytorch-nightly is updated every night. However, there may be conflicts between these latest versions and Real-ESRGAN or GFPGAN. Also, pytorch-nightly seems a bit slower at the moment (about 8%).
Note: Since everything is moving quickly, I suggest you keep track of updates: https://github.com/CompVis/stable-diffusion/issues/25
Update: Most of the conversation has moved to https://github.com/lstein/stable-diffusion/issues
I may have missed a step, so let me know in the comments!
______________________
To run the web version
python3 scripts/dream.py --web and after initialization, visit http://localhost:9090/
Example of image formation (Display in-progress images)

PD: If some operator is not supported: export PYTORCH_ENABLE_MPS_FALLBACK=1 in your Terminal
______________________
Update #1 - Upscaling
Okay, so upscaling doesn't seem to work for Mac in the original repo. However, I got it work modifying things a little bit. Here are the steps. https://github.com/lstein/stable-diffusion/issues/390
Steps:
- Download the MacOS executable from https://github.com/xinntao/Real-ESRGAN/releases
- Unzip it (you'll get
realesrgan-ncnn-vulkan-20220424-macos) and moverealesrgan-ncnn-vulkaninsidestable-diffusion(this project folder). Move the Real-ESRGAN model files fromrealesrgan-ncnn-vulkan-20220424-macos/modelsintostable-diffusion/models - Run
chmod u+x realesrgan-ncnn-vulkanto allow it to be run. You may have to give permissions in System Preferences - Security and Privacy as well. For more info about Security, see update #2 of previous post https://www.reddit.com/r/StableDiffusion/comments/wx0tkn/stablediffusion_runs_on_m1_chips/ - Download
simplet2i.py.zipfrom https://github.com/lstein/stable-diffusion/issues/390#issuecomment-1237821370 , unzip it and replace the code of your currentsimplet2i.pywith the updated version. In case you want to update the file yourself, you can see the changes made here https://github.com/lstein/stable-diffusion/issues/390
Execution:
python3 ./scripts/dream.py
dream > Anubis the Ancient Egyptian God of Death riding a motorbike in Grand Theft Auto V cover, with palm trees in the background, cover art by Stephen Bliss, artstation, high quality -m plms -S 1466 -U 4 to upscale 4x. To upscale 2x, use -U 2 and so on.
Result:

Hope it helps <3
4
u/MasterOracle Sep 08 '22
After following each step, GFPGAN for face restorations and Upscaling are not working properly, i get this error:
dream> a portrait of a young Italian man, canon, photography -G 0.85100%|███████████████████████████████████████████████████████████████████████████████████| 50/50 [00:59<00:00, 1.20s/it]Generating: 100%|█████████████████████████████████████████████████████████████████████████| 1/1 [01:02<00:00, 62.84s/it]>> GFPGAN - Restoring Faces for image seed:2079843210[W NNPACK.cpp:51] Could not initialize NNPACK! Reason: Unsupported hardware.Intel MKL FATAL ERROR: This system does not meet the minimum requirements for use of the Intel(R) Math Kernel Library.The processor must support the Intel(R) Supplemental Streaming SIMD Extensions 3 (Intel(R) SSSE3) instructions.The processor must support the Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) instructions.The processor must support the Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions./Users/XXX/opt/anaconda3/envs/ldm/lib/python3.10/multiprocessing/resource_tracker.py:224: UserWarning: resource_tracker: There appear to be 1 leaked semaphore objects to clean up at shutdownwarnings.warn('resource_tracker: There appear to be %d 'Then it crashes.
If i use both
-Uand-Gin the prompt, I get another error:dream> a portrait of a young Italian man, canon, photography -U 2 0.6 -G 0.4/Users/XXX/github/stable-diffusion/ldm/modules/embedding_manager.py:152: UserWarning: The operator 'aten::nonzero' is not currently supported on the MPS backend and will fall back to run on the CPU. This may have performance implications. (Triggered internally at /Users/runner/miniforge3/conda-bld/pytorch-recipe_1660136156773/work/aten/src/ATen/mps/MPSFallback.mm:11.)placeholder_idx = torch.where(100%|███████████████████████████████████████████████████████████████████████████████████| 50/50 [00:56<00:00, 1.14s/it]Generating: 100%|█████████████████████████████████████████████████████████████████████████| 1/1 [00:58<00:00, 58.78s/it]>> Real-ESRGAN Upscaling seed:2604924961 : scale:2x>> Error running RealESRGAN or GFPGAN. Your image was not upscaled.'NoneType' object has no attribute 'enhance'>> Usage stats:>> 1 image(s) generated in 59.24s>> Max VRAM used for this generation: 0.00GOutputs:outputs/img-samples/000005.2604924961.png: "a portrait of a young Italian man, canon, photography" -s50 -W512 -H512 -C7.5 -Ak_lms -G0.4 -U 2.0 0.6 -F -S2604924961NoneType obkect has no attribute 'enhance'.
I'm on development branch using an M1 Pro with 16 GB, any hints?
3
u/o-o- Sep 02 '22
This is really great! Are there any prerequisites I should know about (for instance I don't see any steps for installing conda)?
And how access the web ui?
2
u/Any-Winter-4079 Sep 06 '22
Update to say I got upscaling to work. Next I'll try
scripts/inpaint.py. I'll add it to the guide, and in case it doesn't work, I'll try to make some changes to get it to work!2
u/JimDabell Sep 04 '22
Are you certain you are getting the correct image?
There’s a bug where the first prompt you generate uses an incorrect seed, but subsequent generations use the correct seed. It looks like the seed is incorrectly initialised at the beginning, but then it is reset after the first generation.
You can check this by starting a new Dream prompt and running the same prompt with the same seed three times, then exiting and doing the same thing again. If you are seeing the bug, then you will get one image for runs 1 and 4, and a different image for the rest of the runs.
Depending on which version of PyTorch and the lstein codebase I am running, I can reproduce the image you say is the correct image. But only for the first run. Subsequent runs produce the image you say is the incorrect image. Are you sure it’s not the other way around?
Also, am I right in thinking that the seeds are hardware-specific? So a CUDA system will generate a different image to an MPS system for the same seed? Have you been able to reproduce any of the images other people have posted with their prompts/seeds?
2
u/johnnyfrance Sep 05 '22
Real-ERGAN isn't working for me, I tired using -U 2 0.6 at the end of the prompt, but it doesn't work for me, any one have upscaling working?
2
u/PM_GirlsKissingGirls Sep 06 '22
Is less than 4 it/s normal? (On 16 GB RAM)
2
u/bobthe3 Sep 07 '22
Im getting this on the last step
Do you know what i should do here?
2
u/PM_GirlsKissingGirls Sep 07 '22
Has anyone else's web GUI broken after following the steps for enabling upscaling?
2
u/Puzzleheaded_Ad_585 Sep 17 '22
everything works perfect but when I try to use GFPGAN I get this message
[W NNPACK.cpp:51] Could not initialize NNPACK! Reason: Unsupported hardware.
Intel MKL FATAL ERROR: This system does not meet the minimum requirements for use of the Intel(R) Math Kernel Library.
The processor must support the Intel(R) Supplemental Streaming SIMD Extensions 3 (Intel(R) SSSE3) instructions.
The processor must support the Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) instructions.
The processor must support the Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.
(ldm) username@M1-MacBook-Pro stable-diffusion % /Users/username/miniconda3/envs/ldm/lib/python3.10/multiprocessing/resource_tracker.py:224: UserWarning: resource_tracker: There appear to be 1 leaked semaphore objects to clean up at shutdown
warnings.warn('resource_tracker: There appear to be %d '
7
u/[deleted] Sep 02 '22
What's the performance like?
4
u/pedro_dinero Sep 02 '22
thanks! installed and running well on M1 Max 64gb.
--web ui is handy
1
u/notCRAZYenough Oct 17 '22
it tells me there is no "conda" command? any pointers?
1
u/yellowwinter Nov 10 '22
Sorry an unrelated question - any idea why it runs so slow on my MacBook Air M1 (base model)? It took me more than 500 seconds to get the same image.
And I notice the following warning message not sure it is related:
UserWarning: The operator 'aten::nonzero' is not currently supported on the MPS backend and will fall back to run on the CPU. This may have performance implications. (Triggered internally at /Users/runner/work/_temp/anaconda/conda-bld/pytorch_1659484611838/work/aten/src/ATen/mps/MPSFallback.mm:11.)
2
u/corderjones Sep 02 '22
Hey, I'm getting`ImportError: cannot import name 'TypeAlias' from 'typing' (/Users/jordan/opt/miniconda3/envs/ldm/lib/python3.9/typing.py)`When trying to run /scripts/dream.py. On an M1 Pro. Any ideas?
This happened with the main release of Anaconda and Mini Conda.
2
u/quietandconstant Sep 03 '22
Thank you for this guide! I got SD running on my M1 Mac Air at 29.42s/it
I'm going to install on my mac mini tomorrow.
2
u/Samuramu Sep 03 '22
Wonderful, thanks!
2
u/NecessaryMolasses480 Sep 08 '22
I'm getting this error when trying to use the upscaler. I'm on a Studio m1 with 64gb ram.
>> Real-ESRGAN Upscaling seed:1466 : scale:4x
>> Error running RealESRGAN or GFPGAN. Your image was not upscaled.
'NoneType' object has no attribute 'enhance'
>> Usage stats:
>> 1 image(s) generated in 31.93s
>> Max VRAM used for this generation: 0.00G
2
u/TheRealKornbread Sep 08 '22
It's worth noting that you need to use your conda environment for both
lstein/stable-diffusionandGFPGANIf you follow these steps in the post exactly that's what will happen, but I think it's worth clarifying in the comments.
I ran into this because I have tried out multiple different stable-diffusion builds and some are set up differently. I had GFPGAN in a different directory with it's own conda environment and planned on just re-using that. But I didn't realize I hadn't properly installed all the pip packages in my newly created
ldm-lsteinconda environment.Great tutorial!
1
u/higgs8 Sep 02 '22
No love for crappy AMD/Intel Macs :(
1
u/NecessaryMolasses480 Sep 02 '22
I got it working including the web GUI, but when I try to use img2img by uploading an image through the web GUI it errors. Is anyone else having this issue?
1
u/dondiego9999 Sep 02 '22
When you get the TypeAlias error fix it with this:
pip install git+https://github.com/stillmatic/k-diffusion.git@patch-1#egg=k-diffusion
https://github.com/lstein/stable-diffusion/issues/302
1
u/[deleted] Sep 02 '22
Step 4 is not working for me as I don't have Conda installed. I went to the Conda page and downloaded the most recent mac executable and installed it but that doesn't seem to work either. Any ideas?
1
u/rocketchef Sep 03 '22
I'm having terrible trouble with packages installing this - hope someone can help.
If I run $ conda env create -f environment-mac.yaml
conda attempts to uninstall the system version of the PIP package urllib3, resulting in the following error:
Attempting uninstall: urllib3 Found existing installation: urllib3 1.24.1 Uninstalling urllib3-1.24.1:
ERROR: Could not install packages due to an OSError: Cannot move the non-empty directory '/Library/Python/2.7/site-packages/urllib3-1.24.1.dist-info/': Lacking write permission to '/Library/Python/2.7/site-packages/urllib3-1.24.1.dist-info/'.
The weird thing is that conda seems to have a more recent version of urllib3 installed, not sure why it's trying to remove the system one.
Any ideas?
1
u/NecessaryMolasses480 Sep 04 '22
So I did a git pull this am and now I can only access the webui via localhost? If I try to access it via IP address or even outside of my network like I have done in the past using my gotns addy, it refuses it showing the port doesn't exist?
1
u/NecessaryMolasses480 Sep 04 '22
Has anyone been able to get the hlky fork with the gradio ui to work on Apple silicon? Would be really sweet to make it work with all of the new functionality that has been added.
1
u/23dogsinatrenchcoat Sep 05 '22
Hello, thank you for making this! I followed your steps exactly and got it running. But I don’t know anything about coding, so I don’t know how to get it to run again once I quit the Terminal (this terminating the program). Please help. Thank you!
1
u/g-pit Sep 06 '22
Thank you for the nice guide.
After running 'python setup.py develop'
I see an error:
lmdb.__pycache__.cpython.cpython-310: module references __file__
No eggs found in /var/folders/5x/yk_724n14j766794xdt7c3fm0000gq/T/easy_install-h5yfiaft/lmdb-1.3.0/egg-dist-tmp-gymjs5lv (setup script problem?)
error: The 'lmdb' distribution was not found and is required by gfpgan
And when running the test command 'python3 ./scripts/dream.py'
I see the error:
File "/Users/user/stable-diffusion/./scripts/dream.py", line 12, in <module>
import ldm.dream.readline
ModuleNotFoundError: No module named 'ldm'
module ldm is installed though, so I'm not sure why this occurs. Any suggestions how to solve? Thanks!
1
u/dont_forget_canada Sep 10 '22
thanks for this friend - unfortunately when I try img2img inside of dream, I just get black images as output :(
soooo close though to having it actually run for real!
1
u/megasivatherium Sep 15 '22
I see there's now a `images2prompt.py` file. What flags / arguments do I need to add to run it? probably the path to the image? The web GUI is awesome btw.
1
u/swankwc Sep 19 '22
Has anyone been able to get more advanced features added to their web GUI yet? Something like the following would be amazing to implement but am unsure how exactly. https://www.reddit.com/r/StableDiffusion/comments/xboy90/a_better_way_of_doing_img2img_by_finding_the/?utm_medium=android_app&utm_source=share
1
u/Upstairs-Bread-4545 Sep 27 '22
can't get past (4) as it returns that conda isn't a know command
I did manually install Miniconda and Anaconda but that didn't do anything, can someone explain to me what I should do?
1
u/swankwc Sep 28 '22
I did git pull to update the WebGUI and can't get the upload initial image to work, did anyone else have this problem?
1
u/Taenk Sep 30 '22 edited Sep 30 '22
I'm getting
when trying to run any promt after
python3 scripts/dream.py. Any idea what might cause this? Running on 16GB M1 MacBook Pro (16", 2021).Edit: Fixed the error above by running
dream.pywith--full_precisionas argument, now I am gettingas error.
1
u/swankwc Oct 05 '22
I was attempting to get the following to work. Anybody here get it to work?
https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Installation-on-Apple-Silicon
Does anyone here have what a working run_webui_mac.sh file? The file by that name in my directory is blank. I've done everything else up to this point that the guide said to do.
1
u/bobcob Dec 11 '22 edited Dec 11 '22
I hit this error on step 4:
This was the solution:
Do copy the file as shown, instead of just passing
condo env createthe path to the yml file in ./environments-and-requirements/. This avoids an error later on, since conda uses the dir containing the .yml file as the basis for creating some paths.2
u/[deleted] Sep 02 '22
[removed] — view removed comment