Skip to content

Commit bae594e

Browse files
committed
made stable diffusin buildable
1 parent 0f52a87 commit bae594e

File tree

9 files changed

+198
-209
lines changed

9 files changed

+198
-209
lines changed

.DS_Store

0 Bytes
Binary file not shown.

stable-diffusion/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
HF_weights/
2+
models/
3+
outputs/
4+
15
# Byte-compiled / optimized / DLL files
26
__pycache__/
37
*.py[cod]

stable-diffusion/build.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pyinstaller txt2img.py --collect-all huggingface_hub --noconfirm --clean --collect-all tqdm --collect-all regex --collect-all requests --collect-all packaging --collect-all filelock --collect-all numpy --collect-all tokenizers --collect-all cv2
2+
3+
mkdir dist/txt2img/clip
4+
cp src/clip/clip/bpe_simple_vocab_16e6.txt.gz dist/txt2img/clip/bpe_simple_vocab_16e6.txt.gz
5+
cp -r configs dist/txt2img/configs
6+
cp -r HF_weights dist/txt2img/HF_weights
7+
mkdir dist/txt2img/models
8+
9+
echo "Right now you gotta copy the weights"

stable-diffusion/ldm/models/diffusion/ddim.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from ldm.modules.diffusionmodules.util import make_ddim_sampling_parameters, make_ddim_timesteps, noise_like, \
99
extract_into_tensor
10+
from ...utils.stdin_input import is_avail, get_input
1011

1112

1213
class DDIMSampler(object):
@@ -144,6 +145,13 @@ def ddim_sampling(self, cond, shape,
144145
iterator = tqdm(time_range, desc='DDIM Sampler', total=total_steps)
145146

146147
for i, step in enumerate(iterator):
148+
149+
if is_avail():
150+
if "__stop__" in get_input():
151+
return None , None
152+
else:
153+
print("boooo")
154+
147155
index = total_steps - i - 1
148156
ts = torch.full((b,), step, device=device, dtype=torch.long)
149157

stable-diffusion/ldm/models/diffusion/plms.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from functools import partial
77

88
from ldm.modules.diffusionmodules.util import make_ddim_sampling_parameters, make_ddim_timesteps, noise_like
9+
from ...utils.stdin_input import is_avail, get_input
910

1011

1112
class PLMSSampler(object):
@@ -146,6 +147,14 @@ def plms_sampling(self, cond, shape,
146147
old_eps = []
147148

148149
for i, step in enumerate(iterator):
150+
151+
if is_avail():
152+
if "__stop__" in get_input():
153+
return None , None
154+
else:
155+
print("boooo")
156+
157+
149158
index = total_steps - i - 1
150159
ts = torch.full((b,), step, device=device, dtype=torch.long)
151160
ts_next = torch.full((b,), time_range[min(i + 1, len(time_range) - 1)], device=device, dtype=torch.long)

stable-diffusion/ldm/modules/encoders/modules.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import clip
55
from einops import rearrange, repeat
66
from transformers import CLIPTokenizer, CLIPTextModel
7-
import kornia
8-
7+
# import kornia
8+
import os
99
from ldm.modules.x_transformer import Encoder, TransformerWrapper # TODO: can we directly rely on lucidrains code and simply add this as a reuirement? --> test
1010

1111

@@ -147,8 +147,21 @@ class FrozenCLIPEmbedder(AbstractEncoder):
147147
"""Uses the CLIP transformer encoder for text (from Hugging Face)"""
148148
def __init__(self, version="openai/clip-vit-large-patch14", device=get_default_device_type(), max_length=77):
149149
super().__init__()
150-
self.tokenizer = CLIPTokenizer.from_pretrained(version)
151-
self.transformer = CLIPTextModel.from_pretrained(version)
150+
151+
tokenizer_path = os.path.abspath(
152+
os.path.join(
153+
os.path.dirname(__file__),
154+
".." , '..' , '..' , 'HF_weights',
155+
"clip_tokenizer"))
156+
157+
transformer_path = os.path.abspath(
158+
os.path.join(
159+
os.path.dirname(__file__),
160+
".." , '..' , '..' , 'HF_weights',
161+
"clip_transformer"))
162+
163+
self.tokenizer = CLIPTokenizer.from_pretrained(tokenizer_path) #dg load from HF
164+
self.transformer = CLIPTextModel.from_pretrained(transformer_path) #dg load from HF
152165
self.device = device
153166
self.max_length = max_length
154167
self.freeze()

stable-diffusion/ldm/utils/__init__.py

Whitespace-only changes.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import threading
2+
import queue
3+
import sys
4+
import platform
5+
import select
6+
7+
8+
if platform.system() == "Windows":
9+
10+
# Due to limitations of windows, we spawn a new thread for stdin. The thread collects the stdin inputs and appends them in list.
11+
12+
input_queue = queue.Queue()
13+
evt = threading.Event()
14+
print("starting the input thread!")
15+
16+
def input_collector_worker(input_queue):
17+
while True:
18+
input_queue.put(input())
19+
evt.set()
20+
21+
input_thread = threading.Thread(
22+
target=input_collector_worker, args=(
23+
input_queue,))
24+
input_thread.daemon = True
25+
input_thread.start()
26+
27+
def is_avail():
28+
"""To check in a non blocking way if there is some input available.
29+
Returns:
30+
bool: If input is avaialble
31+
"""
32+
return not input_queue.empty()
33+
34+
def get_input():
35+
"""To get the input in a blocking way.
36+
Returns:
37+
str: The input
38+
"""
39+
evt.clear()
40+
41+
if is_avail():
42+
return input_queue.get()
43+
44+
evt.wait()
45+
46+
assert is_avail()
47+
return get_input()
48+
49+
50+
else:
51+
52+
# for unix we use select to see if stdin is there or not
53+
54+
print("non threaded input for unix systems!")
55+
56+
def get_input():
57+
return input()
58+
59+
def is_avail():
60+
if select.select([sys.stdin, ], [], [], 0.0)[0]:
61+
return True
62+
else:
63+
return False

0 commit comments

Comments
 (0)