Skip to content

Commit 4ffdf73

Browse files
committed
Merge branch 'development' into main
This merge adds the following major features: * Support for image variations. * Security fix for webGUI (binds to localhost by default, use --host=0.0.0.0 to allow access from external interface. * Scalable configs/models.yaml configuration file for adding more models as they become available. * More tuning and exception handling for M1 hardware running MPS. * Various documentation fixes.
2 parents 3393643 + 9130ad7 commit 4ffdf73

File tree

16 files changed

+820
-107
lines changed

16 files changed

+820
-107
lines changed

README-Mac-MPS.md

Lines changed: 127 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,81 @@
1-
# Apple Silicon Mac Users
1+
# macOS Instructions
22

3-
Several people have gotten Stable Diffusion to work on Apple Silicon
4-
Macs using Anaconda, miniforge, etc. I've gathered up most of their instructions and
5-
put them in this fork (and readme). Things have moved really fast and so these
6-
instructions change often. Hopefully things will settle down a little.
3+
Requirements
74

8-
There's several places where people are discussing Apple
9-
MPS functionality: [the original CompVis
10-
issue](https://github.com/CompVis/stable-diffusion/issues/25), and generally on
11-
[lstein's fork](https://github.com/lstein/stable-diffusion/).
5+
- macOS 12.3 Monterey or later
6+
- Python
7+
- Patience
8+
- Apple Silicon*
129

13-
You have to have macOS 12.3 Monterey or later. Anything earlier than that won't work.
10+
*I haven't tested any of this on Intel Macs but I have read that one person got
11+
it to work, so Apple Silicon might not be requried.
1412

15-
Tested on a 2022 Macbook M2 Air with 10-core GPU and 24 GB unified memory.
13+
Things have moved really fast and so these instructions change often and are
14+
often out-of-date. One of the problems is that there are so many different ways to
15+
run this.
1616

17-
How to:
17+
We are trying to build a testing setup so that when we make changes it doesn't
18+
always break.
19+
20+
How to (this hasn't been 100% tested yet):
21+
22+
First get the weights checkpoint download started - it's big:
23+
24+
1. Sign up at https://huggingface.co
25+
2. Go to the [Stable diffusion diffusion model page](https://huggingface.co/CompVis/stable-diffusion-v-1-4-original)
26+
3. Accept the terms and click Access Repository:
27+
4. Download [sd-v1-4.ckpt (4.27 GB)](https://huggingface.co/CompVis/stable-diffusion-v-1-4-original/blob/main/sd-v1-4.ckpt) and note where you have saved it (probably the Downloads folder)
28+
29+
While that is downloading, open Terminal and run the following commands one at a time.
1830

1931
```
32+
# install brew (and Xcode command line tools):
33+
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
34+
35+
# install python 3, git, cmake, protobuf:
36+
brew install cmake protobuf rust
37+
38+
# install miniconda (M1 arm64 version):
39+
curl https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh -o Miniconda3-latest-MacOSX-arm64.sh
40+
/bin/bash Miniconda3-latest-MacOSX-arm64.sh
41+
42+
# clone the repo
2043
git clone https://github.com/lstein/stable-diffusion.git
2144
cd stable-diffusion
2245
46+
#
47+
# wait until the checkpoint file has downloaded, then proceed
48+
#
49+
50+
# create symlink to checkpoint
2351
mkdir -p models/ldm/stable-diffusion-v1/
24-
PATH_TO_CKPT="$HOME/Documents/stable-diffusion-v-1-4-original" # or wherever yours is.
52+
53+
PATH_TO_CKPT="$HOME/Downloads" # or wherever you saved sd-v1-4.ckpt
54+
2555
ln -s "$PATH_TO_CKPT/sd-v1-4.ckpt" models/ldm/stable-diffusion-v1/model.ckpt
2656
27-
CONDA_SUBDIR=osx-arm64 conda env create -f environment-mac.yaml
57+
# install packages
58+
PIP_EXISTS_ACTION=w CONDA_SUBDIR=osx-arm64 conda env create -f environment-mac.yaml
2859
conda activate ldm
2960
61+
# only need to do this once
3062
python scripts/preload_models.py
63+
64+
# run SD!
3165
python scripts/dream.py --full_precision # half-precision requires autocast and won't work
3266
```
3367

34-
After you follow all the instructions and run dream.py you might get several errors. Here's the errors I've seen and found solutions for.
68+
The original scripts should work as well.
69+
70+
```
71+
python scripts/orig_scripts/txt2img.py --prompt "a photograph of an astronaut riding a horse" --plms
72+
```
73+
74+
Note, `export PIP_EXISTS_ACTION=w` is a precaution to fix `conda env create -f environment-mac.yaml`
75+
never finishing in some situations. So it isn't required but wont hurt.
76+
77+
After you follow all the instructions and run dream.py you might get several
78+
errors. Here's the errors I've seen and found solutions for.
3579

3680
### Is it slow?
3781

@@ -52,27 +96,37 @@ One debugging step is to update to the latest version of PyTorch nightly.
5296

5397
conda install pytorch torchvision torchaudio -c pytorch-nightly
5498

55-
Or you can clean everything up.
99+
If `conda env create -f environment-mac.yaml` takes forever run this.
100+
101+
git clean -f
102+
103+
And run this.
56104

57105
conda clean --yes --all
58106

59-
Or you can reset Anaconda.
107+
Or you could reset Anaconda.
60108

61109
conda update --force-reinstall -y -n base -c defaults conda
62110

63-
### "No module named cv2" (or some other module)
111+
### "No module named cv2", torch, 'ldm', 'transformers', 'taming', etc.
112+
113+
There are several causes of these errors.
64114

65-
Did you remember to `conda activate ldm`? If your terminal prompt
115+
First, did you remember to `conda activate ldm`? If your terminal prompt
66116
begins with "(ldm)" then you activated it. If it begins with "(base)"
67117
or something else you haven't.
68118

69-
If it says you're missing taming you need to rebuild your virtual
119+
Second, you might've run `./scripts/preload_models.py` or `./scripts/dream.py`
120+
instead of `python ./scripts/preload_models.py` or `python ./scripts/dream.py`.
121+
The cause of this error is long so it's below.
122+
123+
Third, if it says you're missing taming you need to rebuild your virtual
70124
environment.
71125

72126
conda env remove -n ldm
73127
conda env create -f environment-mac.yaml
74128

75-
If you have activated the ldm virtual environment and tried rebuilding
129+
Fourth, If you have activated the ldm virtual environment and tried rebuilding
76130
it, maybe the problem could be that I have something installed that
77131
you don't and you'll just need to manually install it. Make sure you
78132
activate the virtual environment so it installs there instead of
@@ -83,6 +137,56 @@ globally.
83137

84138
You might also need to install Rust (I mention this again below).
85139

140+
### How many snakes are living in your computer?
141+
142+
Here's the reason why you have to specify which python to use.
143+
There are several versions of python on macOS and the computer is
144+
picking the wrong one. More specifically, preload_models.py and dream.py says to
145+
find the first `python3` in the path environment variable. You can see which one
146+
it is picking with `which python3`. These are the mostly likely paths you'll see.
147+
148+
% which python3
149+
/usr/bin/python3
150+
151+
The above path is part of the OS. However, that path is a stub that asks you if
152+
you want to install Xcode. If you have Xcode installed already,
153+
/usr/bin/python3 will execute /Library/Developer/CommandLineTools/usr/bin/python3 or
154+
/Applications/Xcode.app/Contents/Developer/usr/bin/python3 (depending on which
155+
Xcode you've selected with `xcode-select`).
156+
157+
% which python3
158+
/opt/homebrew/bin/python3
159+
160+
If you installed python3 with Homebrew and you've modified your path to search
161+
for Homebrew binaries before system ones, you'll see the above path.
162+
163+
% which python
164+
/opt/anaconda3/bin/python
165+
166+
If you drop the "3" you get an entirely different python. Note: starting in
167+
macOS 12.3, /usr/bin/python no longer exists (it was python 2 anyway).
168+
169+
If you have Anaconda installed, this is what you'll see. There is a
170+
/opt/anaconda3/bin/python3 also.
171+
172+
(ldm) % which python
173+
/Users/name/miniforge3/envs/ldm/bin/python
174+
175+
This is what you'll see if you have miniforge and you've correctly activated
176+
the ldm environment. This is the goal.
177+
178+
It's all a mess and you should know [how to modify the path environment variable](https://support.apple.com/guide/terminal/use-environment-variables-apd382cc5fa-4f58-4449-b20a-41c53c006f8f/mac)
179+
if you want to fix it. Here's a brief hint of all the ways you can modify it
180+
(don't really have the time to explain it all here).
181+
182+
- ~/.zshrc
183+
- ~/.bash_profile
184+
- ~/.bashrc
185+
- /etc/paths.d
186+
- /etc/path
187+
188+
Which one you use will depend on what you have installed except putting a file
189+
in /etc/paths.d is what I prefer to do.
86190

87191
### Debugging?
88192

@@ -139,7 +243,7 @@ the environment variable `CONDA_SUBDIR=osx-arm64`, like so:
139243
This error happens with Anaconda on Macs when the Intel-only `mkl` is pulled in by
140244
a dependency. [nomkl](https://stackoverflow.com/questions/66224879/what-is-the-nomkl-python-package-used-for)
141245
is a metapackage designed to prevent this, by making it impossible to install
142-
`mkl`, but if your environment is already broken it may not work.
246+
`mkl`, but if your environment is already broken it may not work.
143247

144248
Do *not* use `os.environ['KMP_DUPLICATE_LIB_OK']='True'` or equivalents as this
145249
masks the underlying issue of using Intel packages.
@@ -207,7 +311,7 @@ change instead. This is a 32-bit vs 16-bit problem.
207311
What? Intel? On an Apple Silicon?
208312

209313
Intel MKL FATAL ERROR: This system does not meet the minimum requirements for use of the Intel(R) Math Kernel Library.
210-
The processor must support the Intel(R) Supplemental Streaming SIMD Extensions 3 (Intel(R) SSSE3) instructions.██████████████| 50/50 [02:25<00:00, 2.53s/it]
314+
The processor must support the Intel(R) Supplemental Streaming SIMD Extensions 3 (Intel(R) SSSE3) instructions.
211315
The processor must support the Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) instructions.
212316
The processor must support the Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.
213317

README.md

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,21 @@ text-to-image generator. This fork supports:
2121
2. A basic Web interface that allows you to run a local web server for
2222
generating images in your browser.
2323

24-
3. A notebook for running the code on Google Colab.
25-
26-
4. Support for img2img in which you provide a seed image to guide the
24+
3. Support for img2img in which you provide a seed image to guide the
2725
image creation. (inpainting & masking coming soon)
2826

27+
4. A notebook for running the code on Google Colab.
28+
2929
5. Upscaling and face fixing using the optional ESRGAN and GFPGAN
3030
packages.
3131

3232
6. Weighted subprompts for prompt tuning.
3333

34-
7. Textual inversion for customization of the prompt language and images.
34+
7. [Image variations](VARIATIONS.md) which allow you to systematically
35+
generate variations of an image you like and combine two or more
36+
images together to combine the best features of both.
37+
38+
8. Textual inversion for customization of the prompt language and images.
3539

3640
8. ...and more!
3741

@@ -42,8 +46,11 @@ improvements and bug fixes.
4246
# Table of Contents
4347

4448
1. [Major Features](#features)
45-
2. [Changelog](#latest)
49+
2. [Changelog](#latest-changes)
4650
3. [Installation](#installation)
51+
1. [Linux](#linux)
52+
1. [Windows](#windows)
53+
1. [MacOS](README-Mac-MPS.md)
4754
4. [Troubleshooting](#troubleshooting)
4855
5. [Contributing](#contributing)
4956
6. [Support](#support)
@@ -331,8 +338,10 @@ and introducing a new vocabulary to the fixed model.
331338

332339
To train, prepare a folder that contains images sized at 512x512 and execute the following:
333340

341+
342+
WINDOWS: As the default backend is not available on Windows, if you're using that platform, set the environment variable `PL_TORCH_DISTRIBUTED_BACKEND=gloo`
343+
334344
```
335-
# As the default backend is not available on Windows, if you're using that platform, execute SET PL_TORCH_DISTRIBUTED_BACKEND=gloo
336345
(ldm) ~/stable-diffusion$ python3 ./main.py --base ./configs/stable-diffusion/v1-finetune.yaml \
337346
-t \
338347
--actual_resume ./models/ldm/stable-diffusion-v1/model.ckpt \
@@ -505,6 +514,30 @@ This will bring your local copy into sync with the remote one.
505514

506515
## Windows
507516

517+
### Notebook install (semi-automated)
518+
519+
We have a
520+
[Jupyter notebook](https://github.com/lstein/stable-diffusion/blob/main/Stable-Diffusion-local-Windows.ipynb)
521+
with cell-by-cell installation steps. It will download the code in this repo as
522+
one of the steps, so instead of cloning this repo, simply download the notebook
523+
from the link above and load it up in VSCode (with the
524+
appropriate extensions installed)/Jupyter/JupyterLab and start running the cells one-by-one.
525+
526+
Note that you will need NVIDIA drivers, Python 3.10, and Git installed
527+
beforehand - simplified
528+
[step-by-step instructions](https://github.com/lstein/stable-diffusion/wiki/Easy-peasy-Windows-install)
529+
are available in the wiki (you'll only need steps 1, 2, & 3 ).
530+
531+
### Manual installs
532+
533+
#### pip
534+
535+
See
536+
[Easy-peasy Windows install](https://github.com/lstein/stable-diffusion/wiki/Easy-peasy-Windows-install)
537+
in the wiki
538+
539+
#### Conda
540+
508541
1. Install Anaconda3 (miniconda3 version) from here: https://docs.anaconda.com/anaconda/install/windows/
509542

510543
2. Install Git from here: https://git-scm.com/download/win

0 commit comments

Comments
 (0)