Skip to content

Commit e6b2c15

Browse files
committed
Merge branch 'main' into fit-init-img
add a --fit option to limit the size of the initial image to the maximum boundaries specified by width and height.
2 parents 28fe841 + 9e99fcb commit e6b2c15

File tree

5 files changed

+90
-69
lines changed

5 files changed

+90
-69
lines changed

README-Mac-MPS.md

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ issue](https://github.com/CompVis/stable-diffusion/issues/25), and generally on
1212

1313
You have to have macOS 12.3 Monterey or later. Anything earlier than that won't work.
1414

15-
BTW, I haven't tested any of this on Intel Macs but I have read that one person
16-
got it to work.
15+
Tested on a 2022 Macbook M2 Air with 10-core GPU and 24 GB unified memory.
1716

1817
How to:
1918

@@ -22,24 +21,23 @@ git clone https://github.com/lstein/stable-diffusion.git
2221
cd stable-diffusion
2322
2423
mkdir -p models/ldm/stable-diffusion-v1/
25-
ln -s /path/to/ckpt/sd-v1-1.ckpt models/ldm/stable-diffusion-v1/model.ckpt
24+
PATH_TO_CKPT="$HOME/Documents/stable-diffusion-v-1-4-original" # or wherever yours is.
25+
ln -s "$PATH_TO_CKPT/sd-v1-4.ckpt" models/ldm/stable-diffusion-v1/model.ckpt
2626
27-
conda env create -f environment-mac.yaml
27+
CONDA_SUBDIR=osx-arm64 conda env create -f environment-mac.yaml
2828
conda activate ldm
2929
3030
python scripts/preload_models.py
31-
python scripts/orig_scripts/txt2img.py --prompt "a photograph of an astronaut riding a horse" --plms
31+
python scripts/dream.py --full_precision # half-precision requires autocast and won't work
3232
```
3333

34-
We have not gotten lstein's dream.py to work yet.
35-
36-
After you follow all the instructions and run txt2img.py you might get several errors. Here's the errors I've seen and found solutions for.
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.
3735

3836
### Is it slow?
3937

4038
Be sure to specify 1 sample and 1 iteration.
4139

42-
python ./scripts/txt2img.py --prompt "ocean" --ddim_steps 5 --n_samples 1 --n_iter 1
40+
python ./scripts/orig_scripts/txt2img.py --prompt "ocean" --ddim_steps 5 --n_samples 1 --n_iter 1
4341

4442
### Doesn't work anymore?
4543

@@ -94,10 +92,6 @@ get quick feedback.
9492

9593
python ./scripts/txt2img.py --prompt "ocean" --ddim_steps 5 --n_samples 1 --n_iter 1
9694

97-
### MAC: torch._C' has no attribute '_cuda_resetPeakMemoryStats' #234
98-
99-
We haven't fixed gotten dream.py to work on Mac yet.
100-
10195
### OSError: Can't load tokenizer for 'openai/clip-vit-large-patch14'...
10296

10397
python scripts/preload_models.py
@@ -108,7 +102,7 @@ Example error.
108102

109103
```
110104
...
111-
NotImplementedError: The operator 'aten::index.Tensor' is not current implemented for the MPS device. If you want this op to be added in priority during the prototype phase of this feature, please comment on [https://github.com/pytorch/pytorch/issues/77764](https://github.com/pytorch/pytorch/issues/77764). As a temporary fix, you can set the environment variable `PYTORCH_ENABLE_MPS_FALLBACK=1` to use the CPU as a fallback for this op. WARNING: this will be slower than running natively on MPS.
105+
NotImplementedError: The operator 'aten::_index_put_impl_' is not current implemented for the MPS device. If you want this op to be added in priority during the prototype phase of this feature, please comment on [https://github.com/pytorch/pytorch/issues/77764](https://github.com/pytorch/pytorch/issues/77764). As a temporary fix, you can set the environment variable `PYTORCH_ENABLE_MPS_FALLBACK=1` to use the CPU as a fallback for this op. WARNING: this will be slower than running natively on MPS.
112106
```
113107

114108
The lstein branch includes this fix in [environment-mac.yaml](https://github.com/lstein/stable-diffusion/blob/main/environment-mac.yaml).
@@ -137,27 +131,18 @@ still working on it.
137131

138132
OMP: Error #15: Initializing libiomp5.dylib, but found libomp.dylib already initialized.
139133

140-
There are several things you can do. First, you could use something
141-
besides Anaconda like miniforge. I read a lot of things online telling
142-
people to use something else, but I am stuck with Anaconda for other
143-
reasons.
144-
145-
Or you can try this.
146-
147-
export KMP_DUPLICATE_LIB_OK=True
148-
149-
Or this (which takes forever on my computer and didn't work anyway).
134+
You are likely using an Intel package by mistake. Be sure to run conda with
135+
the environment variable `CONDA_SUBDIR=osx-arm64`, like so:
150136

151-
conda install nomkl
137+
`CONDA_SUBDIR=osx-arm64 conda install ...`
152138

153-
This error happens with Anaconda on Macs, and
154-
[nomkl](https://stackoverflow.com/questions/66224879/what-is-the-nomkl-python-package-used-for)
155-
is supposed to fix the issue (it isn't a module but a fix of some
156-
sort). [There's more
157-
suggestions](https://stackoverflow.com/questions/53014306/error-15-initializing-libiomp5-dylib-but-found-libiomp5-dylib-already-initial),
158-
like uninstalling tensorflow and reinstalling. I haven't tried them.
139+
This error happens with Anaconda on Macs when the Intel-only `mkl` is pulled in by
140+
a dependency. [nomkl](https://stackoverflow.com/questions/66224879/what-is-the-nomkl-python-package-used-for)
141+
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.
159143

160-
Since I switched to miniforge I haven't seen the error.
144+
Do *not* use `os.environ['KMP_DUPLICATE_LIB_OK']='True'` or equivalents as this
145+
masks the underlying issue of using Intel packages.
161146

162147
### Not enough memory.
163148

@@ -226,4 +211,8 @@ What? Intel? On an Apple Silicon?
226211
The processor must support the Intel(R) Streaming SIMD Extensions 4.2 (Intel(R) SSE4.2) instructions.
227212
The processor must support the Intel(R) Advanced Vector Extensions (Intel(R) AVX) instructions.
228213

229-
This was actually the issue that I couldn't solve until I switched to miniforge.
214+
This is due to the Intel `mkl` package getting picked up when you try to install
215+
something that depends on it-- Rosetta can translate some Intel instructions but
216+
not the specialized ones here. To avoid this, make sure to use the environment
217+
variable `CONDA_SUBDIR=osx-arm64`, which restricts the Conda environment to only
218+
use ARM packages, and use `nomkl` as described above.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ and [Tesseract Cat](https://github.com/TesseractCat)
740740

741741
Original portions of the software are Copyright (c) 2020 Lincoln D. Stein (https://github.com/lstein)
742742

743-
#Further Reading
743+
# Further Reading
744744

745745
Please see the original README for more information on this software
746-
and underlying algorithm, located in the file README-CompViz.md.
746+
and underlying algorithm, located in the file [README-CompViz.md](README-CompViz.md).

environment-mac.yaml

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,57 @@
11
name: ldm
22
channels:
3-
- apple
4-
- conda-forge
53
- pytorch-nightly
6-
- defaults
4+
- conda-forge
75
dependencies:
8-
- python=3.10.4
9-
- pip=22.1.2
6+
- python==3.9.13
7+
- pip==22.2.2
8+
9+
# pytorch-nightly, left unpinned
1010
- pytorch
11+
- torchmetrics
1112
- torchvision
12-
- numpy=1.23.1
13+
14+
# I suggest to keep the other deps sorted for convenience.
15+
# If you wish to upgrade to 3.10, try to run this:
16+
#
17+
# ```shell
18+
# CONDA_CMD=conda
19+
# sed -E 's/python==3.9.13/python==3.10.5/;s/ldm/ldm-3.10/;21,99s/- ([^=]+)==.+/- \1/' environment-mac.yaml > /tmp/environment-mac-updated.yml
20+
# CONDA_SUBDIR=osx-arm64 $CONDA_CMD env create -f /tmp/environment-mac-updated.yml && $CONDA_CMD list -n ldm-3.10 | awk ' {print " - " $1 "==" $2;} '
21+
# ```
22+
#
23+
# Unfortunately, as of 2022-08-31, this fails at the pip stage.
24+
- albumentations==1.2.1
25+
- coloredlogs==15.0.1
26+
- einops==0.4.1
27+
- grpcio==1.46.4
28+
- humanfriendly
29+
- imageio-ffmpeg==0.4.7
30+
- imageio==2.21.2
31+
- imgaug==0.4.0
32+
- kornia==0.6.7
33+
- mpmath==1.2.1
34+
- nomkl
35+
- numpy==1.23.2
36+
- omegaconf==2.1.1
37+
- onnx==1.12.0
38+
- onnxruntime==1.12.1
39+
- opencv==4.6.0
40+
- pudb==2022.1
41+
- pytorch-lightning==1.6.5
42+
- scipy==1.9.1
43+
- streamlit==1.12.2
44+
- sympy==1.10.1
45+
- tensorboard==2.9.0
46+
- transformers==4.21.2
1347
- pip:
14-
- albumentations==0.4.6
15-
- opencv-python==4.6.0.66
16-
- pudb==2019.2
17-
- imageio==2.9.0
18-
- imageio-ffmpeg==0.4.2
19-
- pytorch-lightning==1.4.2
20-
- omegaconf==2.1.1
21-
- test-tube>=0.7.5
22-
- streamlit==1.12.0
23-
- pillow==9.2.0
24-
- einops==0.3.0
25-
- torch-fidelity==0.3.0
26-
- transformers==4.19.2
27-
- torchmetrics==0.6.0
28-
- kornia==0.6.0
29-
- -e git+https://github.com/openai/CLIP.git@main#egg=clip
48+
- invisible-watermark
49+
- test-tube
50+
- tokenizers
51+
- torch-fidelity
52+
- -e git+https://github.com/huggingface/[email protected]#egg=diffusers
3053
- -e git+https://github.com/CompVis/taming-transformers.git@master#egg=taming-transformers
54+
- -e git+https://github.com/openai/CLIP.git@main#egg=clip
3155
- -e git+https://github.com/lstein/k-diffusion.git@master#egg=k-diffusion
3256
- -e .
3357
variables:

ldm/simplet2i.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from ldm.models.diffusion.plms import PLMSSampler
2828
from ldm.models.diffusion.ksampler import KSampler
2929
from ldm.dream.pngwriter import PngWriter
30-
from ldm.dream.image_util import InitImageResizer
3130
from ldm.dream.devices import choose_torch_device
3231

3332
"""Simplified text to image API for stable diffusion/latent diffusion
@@ -159,7 +158,7 @@ def __init__(
159158

160159
# for VRAM usage statistics
161160
self.session_peakmem = torch.cuda.max_memory_allocated() if self.device == 'cuda' else None
162-
161+
163162
if seed is None:
164163
self.seed = self._new_seed()
165164
else:
@@ -178,7 +177,8 @@ def prompt2png(self, prompt, outdir, **kwargs):
178177
outputs = []
179178
for image, seed in results:
180179
name = f'{prefix}.{seed}.png'
181-
path = pngwriter.save_image_and_prompt_to_png(image, f'{prompt} -S{seed}', name)
180+
path = pngwriter.save_image_and_prompt_to_png(
181+
image, f'{prompt} -S{seed}', name)
182182
outputs.append([path, seed])
183183
return outputs
184184

@@ -276,7 +276,8 @@ def process_image(image,seed):
276276
self._set_sampler()
277277

278278
tic = time.time()
279-
torch.cuda.torch.cuda.reset_peak_memory_stats()
279+
if torch.cuda.is_available():
280+
torch.cuda.reset_peak_memory_stats()
280281
results = list()
281282

282283
try:
@@ -487,7 +488,8 @@ def _get_uc_and_c(self, prompt, skip_normalize):
487488
uc = self.model.get_learned_conditioning([''])
488489

489490
# get weighted sub-prompts
490-
weighted_subprompts = T2I._split_weighted_subprompts(prompt, skip_normalize)
491+
weighted_subprompts = T2I._split_weighted_subprompts(
492+
prompt, skip_normalize)
491493

492494
if len(weighted_subprompts) > 1:
493495
# i dont know if this is correct.. but it works
@@ -530,7 +532,7 @@ def load_model(self):
530532
if self.model is None:
531533
seed_everything(self.seed)
532534
try:
533-
config = OmegaConf.load(self.config)
535+
config = OmegaConf.load(self.config)
534536
self.device = self._get_device()
535537
model = self._load_model_from_config(config, self.weights)
536538
if self.embedding_path is not None:
@@ -673,18 +675,20 @@ def _split_weighted_subprompts(text, skip_normalize=False):
673675
$ # else, if no ':' then match end of line
674676
) # end non-capture group
675677
""", re.VERBOSE)
676-
parsed_prompts = [(match.group("prompt").replace("\\:", ":"), float(match.group("weight") or 1)) for match in re.finditer(prompt_parser, text)]
678+
parsed_prompts = [(match.group("prompt").replace("\\:", ":"), float(
679+
match.group("weight") or 1)) for match in re.finditer(prompt_parser, text)]
677680
if skip_normalize:
678681
return parsed_prompts
679682
weight_sum = sum(map(lambda x: x[1], parsed_prompts))
680683
if weight_sum == 0:
681-
print("Warning: Subprompt weights add up to zero. Discarding and using even weights instead.")
684+
print(
685+
"Warning: Subprompt weights add up to zero. Discarding and using even weights instead.")
682686
equal_weight = 1 / len(parsed_prompts)
683687
return [(x[0], equal_weight) for x in parsed_prompts]
684688
return [(x[0], x[1] / weight_sum) for x in parsed_prompts]
685-
686-
# shows how the prompt is tokenized
687-
# usually tokens have '</w>' to indicate end-of-word,
689+
690+
# shows how the prompt is tokenized
691+
# usually tokens have '</w>' to indicate end-of-word,
688692
# but for readability it has been replaced with ' '
689693
def _log_tokenization(self, text):
690694
if not self.log_tokenization:
@@ -721,4 +725,8 @@ def _resolution_check(self, width, height, log=False):
721725
height = h
722726
width = w
723727
resize_needed = True
728+
729+
if (width * height) > (self.width * self.height):
730+
print(">> This input is larger than your defaults. If you run out of memory, please use a smaller image.")
731+
724732
return width, height, resize_needed

scripts/orig_scripts/img2img.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def main():
200200
config = OmegaConf.load(f"{opt.config}")
201201
model = load_model_from_config(config, f"{opt.ckpt}")
202202

203-
device = choose_torch_device()
203+
device = torch.device(choose_torch_device())
204204
model = model.to(device)
205205

206206
if opt.plms:

0 commit comments

Comments
 (0)