Skip to content

Commit 7c00e69

Browse files
committed
some updates
1 parent 865d2af commit 7c00e69

File tree

16 files changed

+248
-64
lines changed

16 files changed

+248
-64
lines changed

backends/model_converter/tdict.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ def init_read(self):
124124
weights_json_start = extra_head[4]
125125
weights_json_l = extra_head[5] - extra_head[4]
126126

127+
self.ctdict_version = int(extra_head[3])
128+
127129
self.in_file.seek(weights_json_start)
128130
self.keys_info = json.loads( self.in_file.read(weights_json_l).decode('ascii') )
129131

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
build
2+
build/
3+
build/*
4+
5+
dist
6+
dist/
7+
dist/*
8+
9+
# Byte-compiled / optimized / DLL files
10+
__pycache__/
11+
*.py[cod]
12+
*$py.class
13+
14+
# C extensions
15+
*.so
16+
17+
# Distribution / packaging
18+
.Python
19+
build/
20+
develop-eggs/
21+
dist/
22+
downloads/
23+
eggs/
24+
.eggs/
25+
lib/
26+
lib64/
27+
parts/
28+
sdist/
29+
var/
30+
wheels/
31+
share/python-wheels/
32+
*.egg-info/
33+
.installed.cfg
34+
*.egg
35+
MANIFEST
36+
37+
# PyInstaller
38+
# Usually these files are written by a python script from a template
39+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
40+
*.manifest
41+
*.spec
42+
43+
# Installer logs
44+
pip-log.txt
45+
pip-delete-this-directory.txt
46+
47+
# Unit test / coverage reports
48+
htmlcov/
49+
.tox/
50+
.nox/
51+
.coverage
52+
.coverage.*
53+
.cache
54+
nosetests.xml
55+
coverage.xml
56+
*.cover
57+
*.py,cover
58+
.hypothesis/
59+
.pytest_cache/
60+
cover/
61+
62+
# Translations
63+
*.mo
64+
*.pot
65+
66+
# Django stuff:
67+
*.log
68+
local_settings.py
69+
db.sqlite3
70+
db.sqlite3-journal
71+
72+
# Flask stuff:
73+
instance/
74+
.webassets-cache
75+
76+
# Scrapy stuff:
77+
.scrapy
78+
79+
# Sphinx documentation
80+
docs/_build/
81+
82+
# PyBuilder
83+
.pybuilder/
84+
target/
85+
86+
# Jupyter Notebook
87+
.ipynb_checkpoints
88+
89+
# IPython
90+
profile_default/
91+
ipython_config.py
92+
93+
# pyenv
94+
# For a library or package, you might want to ignore these files since the code is
95+
# intended to run in multiple environments; otherwise, check them in:
96+
# .python-version
97+
98+
# pipenv
99+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
100+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
101+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
102+
# install all needed dependencies.
103+
#Pipfile.lock
104+
105+
# poetry
106+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
107+
# This is especially recommended for binary packages to ensure reproducibility, and is more
108+
# commonly ignored for libraries.
109+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
110+
#poetry.lock
111+
112+
# pdm
113+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
114+
#pdm.lock
115+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
116+
# in version control.
117+
# https://pdm.fming.dev/#use-with-ide
118+
.pdm.toml
119+
120+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
121+
__pypackages__/
122+
123+
# Celery stuff
124+
celerybeat-schedule
125+
celerybeat.pid
126+
127+
# SageMath parsed files
128+
*.sage.py
129+
130+
# Environments
131+
.env
132+
.venv
133+
env/
134+
venv/
135+
ENV/
136+
env.bak/
137+
venv.bak/
138+
139+
# Spyder project settings
140+
.spyderproject
141+
.spyproject
142+
143+
# Rope project settings
144+
.ropeproject
145+
146+
# mkdocs documentation
147+
/site
148+
149+
# mypy
150+
.mypy_cache/
151+
.dmypy.json
152+
dmypy.json
153+
154+
# Pyre type checker
155+
.pyre/
156+
157+
# pytype static type analyzer
158+
.pytype/
159+
160+
# Cython debug symbols
161+
cython_debug/
162+
163+
# PyCharm
164+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
165+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
166+
# and can be added to the global gitignore or merged into this file. For a more nuclear
167+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
168+
#.idea/
169+
170+

backends/stable_diffusion/clip_tokenizer/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ def default_bpe():
1616
p = os.path.join(
1717
os.path.dirname(os.path.abspath(__file__)), "bpe_simple_vocab_16e6.txt.gz"
1818
)
19+
# print("checking " , p )
1920
if os.path.exists(p):
2021
return p
2122
else:
2223
p2 = os.path.join(
23-
os.path.dirname(os.path.abspath(__file__)), ".." , ".." ,"bpe_simple_vocab_16e6.txt.gz"
24+
os.path.dirname(os.path.abspath(__file__)), ".." ,"bpe_simple_vocab_16e6.txt.gz"
2425
)
2526
p2 = os.path.abspath(p2)
27+
# print("checking " , p2 )
2628
if os.path.exists(p2):
2729
return p2
2830
assert False

backends/stable_diffusion/diffusionbee_backend.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
print("backend")
1+
print("starting backend")
22

33
from stdin_input import is_avail, get_input
44
import argparse
@@ -12,18 +12,35 @@
1212
import math
1313
import time
1414
import traceback
15-
from stable_diffusion import StableDiffusion
1615
import os
16+
from pathlib import Path
17+
import importlib
18+
1719
# b2py t2im {"prompt": "sun glasses" , "W":640 , "H" : 640 , "num_imgs" : 10 , "input_image":"/Users/divamgupta/Downloads/inn.png" , "mask_image" : "/Users/divamgupta/Downloads/maa.png" , "is_inpaint":true }
1820

21+
if not ( getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS')):
22+
print("Adding sys paths")
23+
dir_path = os.path.dirname(os.path.realpath(__file__))
24+
sys.path.append(os.path.join(dir_path , "../model_converter"))
25+
26+
model_interface_path = os.environ.get('MODEL_INTERFACE_PATH') or "../stable_diffusion_tf_models"
27+
sys.path.append( os.path.join(dir_path , model_interface_path) )
28+
else:
29+
print("not adding sys paths")
30+
1931

20-
dir_path = os.path.dirname(os.path.realpath(__file__))
21-
sys.path.append(os.path.join(dir_path , "../model_converter"))
2232
from convert_model import convert_model
33+
from stable_diffusion import StableDiffusion
34+
35+
# get the model interface form the environ
36+
USE_DUMMY_INTERFACE = False
37+
if USE_DUMMY_INTERFACE :
38+
from fake_interface import ModelInterface
39+
else:
40+
from interface import ModelInterface
41+
2342

2443

25-
from pathlib import Path
26-
import os
2744

2845
home_path = Path.home()
2946

@@ -68,13 +85,20 @@ def download_weights():
6885
is_downloaded = False
6986
for _ in range(10):
7087
try:
71-
print("sdbk mltl Downloading Model 1/2")
72-
73-
p_14 = "/Users/divamgupta/Downloads/sd-v1-4.tdict"
7488

75-
print("sdbk mltl Downloading Model 2/2")
89+
p_14 = ProgressBarDownloader(title="Downloading Model 1/2").download(
90+
url="https://huggingface.co/divamgupta/stable_diffusion_mps/resolve/main/sd-v1-4_fp16.tdict",
91+
md5_checksum="9f1fc1e94821d000b811e3bb6e7686b2",
92+
verify_ssl=False,
93+
extract_zip=False,
94+
)
7695

77-
p_14_np = "/Users/divamgupta/Downloads/sd-v1-5-inpainting.tdict"
96+
p_14_np = ProgressBarDownloader(title="Downloading Model 2/2").download(
97+
url="https://huggingface.co/divamgupta/stable_diffusion_mps/resolve/main/sd-v1-5-inpainting_fp16.tdict",
98+
md5_checksum="68303f49cca00968c39abddc20b622a6",
99+
verify_ssl=False,
100+
extract_zip=False,
101+
)
78102

79103
is_downloaded = True
80104
break
@@ -169,7 +193,7 @@ def callback(state="" , progress=-1):
169193
if "__stop__" in get_input():
170194
return "stop"
171195

172-
generator = StableDiffusion( p_14 , model_name="sd_1x", callback=callback)
196+
generator = StableDiffusion( ModelInterface , p_14 , model_name="sd_1x", callback=callback)
173197

174198

175199
default_d = { "W" : 512 , "H" : 512, "num_imgs":1 , "ddim_steps" : 25 ,

backends/stable_diffusion/schedulers/scheduling_lms_discrete.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from typing import Optional, Tuple, Union
1919

2020
import numpy as np
21-
import torch
2221

2322
from scipy import integrate
2423

@@ -141,9 +140,9 @@ def set_timesteps(self, num_inference_steps: int):
141140

142141
def step(
143142
self,
144-
model_output: Union[torch.FloatTensor, np.ndarray],
143+
model_output: Union[ np.ndarray],
145144
timestep: int,
146-
sample: Union[torch.FloatTensor, np.ndarray],
145+
sample: Union[ np.ndarray],
147146
seed=None,
148147
order: int = 4,
149148
return_dict: bool = True,
@@ -190,10 +189,10 @@ def step(
190189

191190
def add_noise(
192191
self,
193-
original_samples: Union[torch.FloatTensor, np.ndarray],
194-
noise: Union[torch.FloatTensor, np.ndarray],
195-
timesteps: Union[torch.IntTensor, np.ndarray],
196-
) -> Union[torch.FloatTensor, np.ndarray]:
192+
original_samples: Union[ np.ndarray],
193+
noise: Union[ np.ndarray],
194+
timesteps: Union[np.ndarray],
195+
) -> Union[ np.ndarray]:
197196
sigmas = self.match_shape(self.sigmas[timesteps], noise)
198197
noisy_samples = original_samples + noise * sigmas
199198

backends/stable_diffusion/stable_diffusion.py

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,15 @@
1111
import io
1212
from dataclasses import dataclass
1313
import os
14-
import importlib
1514
import sys
1615

1716
MAX_TEXT_LEN = 77
1817

19-
USE_DUMMY_INTERFACE = False
2018

2119
dir_path = os.path.dirname(os.path.realpath(__file__))
2220

23-
sys.path.append(os.path.join(dir_path , "../model_converter"))
2421
from tdict import TDict
2522

26-
# get the model interface form the environ
27-
if not USE_DUMMY_INTERFACE :
28-
model_interface_path = os.environ.get('MODEL_INTERFACE_PATH') or "../stable_diffusion_tf_models"
29-
30-
print("model_interface_path", model_interface_path )
31-
32-
if model_interface_path[-1] == "/":
33-
model_interface_path = model_interface_path[:-1]
34-
35-
module_name = model_interface_path.split("/")[-1]
36-
module_path = "/".join(model_interface_path.split("/")[:-1])
37-
38-
39-
sys.path.append( os.path.join(dir_path , module_path) )
40-
41-
42-
ModelInterface = importlib.import_module( module_name + ".interface").ModelInterface
43-
else:
44-
from fake_interface import ModelInterface
45-
4623
from schedulers.scheduling_ddim import DDIMScheduler
4724
from schedulers.scheduling_lms_discrete import LMSDiscreteScheduler
4825
from schedulers.scheduling_pndm import PNDMScheduler
@@ -145,8 +122,9 @@ def dummy_callback(state="" , progress=-1):
145122
pass
146123

147124
class StableDiffusion:
148-
def __init__(self , tdict_path , model_name="sd_1x", callback=None ):
125+
def __init__(self , ModelInterfaceClass , tdict_path , model_name="sd_1x", callback=None ):
149126

127+
self.ModelInterfaceClass = ModelInterfaceClass
150128

151129
if callback is None:
152130
callback = dummy_callback
@@ -158,9 +136,9 @@ def __init__(self , tdict_path , model_name="sd_1x", callback=None ):
158136

159137
self.current_model_name = model_name
160138
self.current_tdict_path = tdict_path
161-
self.current_dtype = ModelInterface.default_float_type
139+
self.current_dtype = self.ModelInterfaceClass.default_float_type
162140

163-
self.model = ModelInterface( TDict(self.current_tdict_path ), dtype=self.current_dtype, model_name=self.current_model_name )
141+
self.model = self.ModelInterfaceClass( TDict(self.current_tdict_path ), dtype=self.current_dtype, model_name=self.current_model_name )
164142

165143

166144
def prepare_model_interface(self , sd_run=None ):
@@ -177,7 +155,7 @@ def prepare_model_interface(self , sd_run=None ):
177155
print("Creating model interface")
178156
assert tdict_path is not None
179157
self.model.destroy()
180-
self.model = ModelInterface(TDict(tdict_path ) , dtype=dtype, model_name=model_name )
158+
self.model = self.ModelInterfaceClass(TDict(tdict_path ) , dtype=dtype, model_name=model_name )
181159
self.current_tdict_path = tdict_path
182160
self.current_dtype = dtype
183161
self.current_model_name = model_name

backends/stable_diffusion_tf_models/__init__.py

Whitespace-only changes.

backends/stable_diffusion_tf_models/autoencoder_kl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import tensorflow as tf
2-
from .group_norm import GroupNormalization
2+
from group_norm import GroupNormalization
33

4-
from .layers import apply_seq, PaddedConv2D
4+
from layers import apply_seq, PaddedConv2D
55

66

77
class AttentionBlock(tf.keras.layers.Layer):

0 commit comments

Comments
 (0)