Skip to content

Commit

Permalink
possible fix for empty list of optimizations #10605
Browse files Browse the repository at this point in the history
  • Loading branch information
AUTOMATIC1111 committed May 23, 2023
1 parent 3f50b7d commit b186045
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
21 changes: 15 additions & 6 deletions modules/sd_hijack.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def apply_optimizations():

undo_optimizations()

if len(optimizers) == 0:
# a script can access the model very early, and optimizations would not be filled by then
current_optimizer = None
return ''

ldm.modules.diffusionmodules.model.nonlinearity = silu
ldm.modules.diffusionmodules.openaimodel.th = sd_hijack_unet.th

Expand All @@ -67,8 +72,9 @@ def apply_optimizations():
matching_optimizer = optimizers[0]

if matching_optimizer is not None:
print(f"Applying optimization: {matching_optimizer.name}")
print(f"Applying optimization: {matching_optimizer.name}... ", end='')
matching_optimizer.apply()
print("done.")
current_optimizer = matching_optimizer
return current_optimizer.name
else:
Expand Down Expand Up @@ -149,6 +155,13 @@ class StableDiffusionModelHijack:
def __init__(self):
self.embedding_db.add_embedding_dir(cmd_opts.embeddings_dir)

def apply_optimizations(self):
try:
self.optimization_method = apply_optimizations()
except Exception as e:
errors.display(e, "applying cross attention optimization")
undo_optimizations()

def hijack(self, m):
if type(m.cond_stage_model) == xlmr.BertSeriesModelWithTransformation:
model_embeddings = m.cond_stage_model.roberta.embeddings
Expand All @@ -168,11 +181,7 @@ def hijack(self, m):
if m.cond_stage_key == "edit":
sd_hijack_unet.hijack_ddpm_edit()

try:
self.optimization_method = apply_optimizations()
except Exception as e:
errors.display(e, "applying cross attention optimization")
undo_optimizations()
self.apply_optimizations()

self.clip = m.cond_stage_model

Expand Down
17 changes: 14 additions & 3 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,20 @@ def initialize_rest(*, reload_script_modules=False):
modules.sd_hijack.list_optimizers()
startup_timer.record("scripts list_optimizers")

# load model in parallel to other startup stuff
# (when reloading, this does nothing)
Thread(target=lambda: shared.sd_model).start()
def load_model():
"""
Accesses shared.sd_model property to load model.
After it's available, if it has been loaded before this access by some extension,
its optimization may be None because the list of optimizaers has neet been filled
by that time, so we apply optimization again.
"""

shared.sd_model # noqa: B018

if modules.sd_hijack.current_optimizer is None:
modules.sd_hijack.apply_optimizations()

Thread(target=load_model).start()

shared.reload_hypernetworks()
startup_timer.record("reload hypernetworks")
Expand Down

1 comment on commit b186045

@vkage
Copy link
Contributor

@vkage vkage commented on b186045 May 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this error casued by this fix?

Launching Web UI with arguments: --lowvram --no-half --xformers --skip-torch-cuda-test
Warning: caught exception 'No CUDA GPUs are available', memory monitor disabled
Traceback (most recent call last):
File "D:\Personal\AI\stable-diffusion-webui\launch.py", line 38, in
main()
File "D:\Personal\AI\stable-diffusion-webui\launch.py", line 34, in main
start()
File "D:\Personal\AI\stable-diffusion-webui\modules\launch_utils.py", line 334, in start
webui.webui()
File "D:\Personal\AI\stable-diffusion-webui\webui.py", line 366, in webui
initialize()
File "D:\Personal\AI\stable-diffusion-webui\webui.py", line 251, in initialize
initialize_rest(reload_script_modules=False)
File "D:\Personal\AI\stable-diffusion-webui\webui.py", line 291, in initialize_rest
modules.sd_hijack.list_optimizers()
File "D:\Personal\AI\stable-diffusion-webui\modules\sd_hijack.py", line 38, in list_optimizers
new_optimizers = [x for x in new_optimizers if x.is_available()]
File "D:\Personal\AI\stable-diffusion-webui\modules\sd_hijack.py", line 38, in
new_optimizers = [x for x in new_optimizers if x.is_available()]
File "D:\Personal\AI\stable-diffusion-webui\modules\sd_hijack_optimizations.py", line 51, in is_available
return shared.cmd_opts.force_enable_xformers or (shared.xformers_available and torch.version.cuda and (6, 0) <= torch.cuda.get_device_capability(shared.device) <= (9, 0))
File "D:\Personal\AI\stable-diffusion-webui\venv\lib\site-packages\torch\cuda_init_.py", line 381, in get_device_capability
prop = get_device_properties(device)
File "D:\Personal\AI\stable-diffusion-webui\venv\lib\site-packages\torch\cuda_init_.py", line 395, in get_device_properties
_lazy_init() # will define get_device_properties
File "D:\Personal\AI\stable-diffusion-webui\venv\lib\site-packages\torch\cuda_init
.py", line 247, in _lazy_init
torch._C._cuda_init()
RuntimeError: No CUDA GPUs are available

Please sign in to comment.