From 3c3b3b92912ba6600ba26735439724d0f68a7d15 Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Sat, 16 Sep 2023 13:46:22 -0400 Subject: [PATCH] simplify ti loading --- modules/processing.py | 6 ++++-- modules/processing_diffusers.py | 9 +++------ modules/sd_models.py | 6 +++--- modules/sd_samplers_compvis.py | 4 ---- modules/shared.py | 2 +- modules/textual_inversion/textual_inversion.py | 5 ----- 6 files changed, 11 insertions(+), 21 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index 2acca730f..d4d635497 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -686,8 +686,8 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: p.all_subseeds = subseed else: p.all_subseeds = [int(subseed) + x for x in range(len(p.all_prompts))] - if os.path.exists(shared.opts.embeddings_dir) and not p.do_not_reload_embeddings: - modules.sd_hijack.model_hijack.embedding_db.load_textual_inversion_embeddings() + if os.path.exists(shared.opts.embeddings_dir) and not p.do_not_reload_embeddings and shared.backend == shared.Backend.ORIGINAL: + modules.sd_hijack.model_hijack.embedding_db.load_textual_inversion_embeddings(force_reload=False) if p.scripts is not None: p.scripts.process(p) infotexts = [] @@ -990,6 +990,8 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): modules.sd_models.set_diffuser_pipe(self.sd_model, modules.sd_models.DiffusersTaskType.TEXT_2_IMAGE) latent_scale_mode = shared.latent_upscale_modes.get(self.hr_upscaler, None) if self.hr_upscaler is not None else shared.latent_upscale_modes.get(shared.latent_upscale_default_mode, "None") + if latent_scale_mode is not None: + self.hr_force = False # no need to force anything if self.enable_hr and (latent_scale_mode is None or self.hr_force): if len([x for x in shared.sd_upscalers if x.name == self.hr_upscaler]) == 0: shared.log.warning(f"Cannot find upscaler for hires: {self.hr_upscaler}") diff --git a/modules/processing_diffusers.py b/modules/processing_diffusers.py index c52193e5b..4c64c79a1 100644 --- a/modules/processing_diffusers.py +++ b/modules/processing_diffusers.py @@ -3,6 +3,7 @@ import inspect import typing import torch import torchvision.transforms.functional as TF +import diffusers import modules.devices as devices import modules.shared as shared import modules.sd_samplers as sd_samplers @@ -15,12 +16,6 @@ from modules.processing import StableDiffusionProcessing import modules.prompt_parser_diffusers as prompt_parser_diffusers -try: - import diffusers -except Exception as ex: - shared.log.error(f'Failed to import diffusers: {ex}') - - def process_diffusers(p: StableDiffusionProcessing, seeds, prompts, negative_prompts): results = [] if p.enable_hr and p.hr_upscaler != 'None' and p.denoising_strength > 0 and len(getattr(p, 'init_images', [])) == 0: @@ -171,6 +166,8 @@ def process_diffusers(p: StableDiffusionProcessing, seeds, prompts, negative_pro return prompts, negative_prompts, prompts_2, negative_prompts_2 def set_pipeline_args(model, prompts: list, negative_prompts: list, prompts_2: typing.Optional[list]=None, negative_prompts_2: typing.Optional[list]=None, desc:str='', **kwargs): + if hasattr(model, 'embedding_db'): + del model.embedding_db try: is_refiner = model.text_encoder.__class__.__name__ != 'CLIPTextModel' except Exception: diff --git a/modules/sd_models.py b/modules/sd_models.py index 36f00d0af..95203739b 100644 --- a/modules/sd_models.py +++ b/modules/sd_models.py @@ -780,7 +780,7 @@ def load_diffuser(checkpoint_info=None, already_loaded_state_dict=None, timer=No shared.log.error(f'Failed loading {op}: {checkpoint_info.path} {e}') return else: - diffusers_load_config["local_files_only "] = True + diffusers_load_config["local_files_only"] = True diffusers_load_config["extract_ema"] = shared.opts.diffusers_extract_ema pipeline, model_type = detect_pipeline(checkpoint_info.path, op) if pipeline is None: @@ -873,10 +873,10 @@ def load_diffuser(checkpoint_info=None, already_loaded_state_dict=None, timer=No sd_model.vae = vae if shared.opts.diffusers_vae_upcast != 'default': if shared.opts.diffusers_vae_upcast == 'true': - sd_model.vae.config["force_upcast"] = True + # sd_model.vae.config["force_upcast"] = True sd_model.vae.config.force_upcast = True else: - sd_model.vae.config["force_upcast"] = False + # sd_model.vae.config["force_upcast"] = False sd_model.vae.config.force_upcast = False if shared.opts.no_half_vae: devices.dtype_vae = torch.float32 diff --git a/modules/sd_samplers_compvis.py b/modules/sd_samplers_compvis.py index 19fe750a1..bd3e95c70 100644 --- a/modules/sd_samplers_compvis.py +++ b/modules/sd_samplers_compvis.py @@ -45,7 +45,6 @@ class VanillaStableDiffusionSampler: def launch_sampling(self, steps, func): state.sampling_steps = steps state.sampling_step = 0 - try: return func() except sd_samplers_common.InterruptedException: @@ -53,11 +52,8 @@ class VanillaStableDiffusionSampler: def p_sample_ddim_hook(self, x_dec, cond, ts, unconditional_conditioning, *args, **kwargs): x_dec, ts, cond, unconditional_conditioning = self.before_sample(x_dec, ts, cond, unconditional_conditioning) - res = self.orig_p_sample_ddim(x_dec, cond, ts, *args, unconditional_conditioning=unconditional_conditioning, **kwargs) - x_dec, ts, cond, unconditional_conditioning, res = self.after_sample(x_dec, ts, cond, unconditional_conditioning, res) - return res def before_sample(self, x, ts, cond, unconditional_conditioning): diff --git a/modules/shared.py b/modules/shared.py index 616ea76d9..7ee44b668 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -9,7 +9,6 @@ import urllib.request from urllib.parse import urlparse from enum import Enum import gradio as gr -import tqdm import fasteners from rich.console import Console from modules import errors, ui_components, shared_items, cmd_args @@ -887,6 +886,7 @@ def reload_gradio_theme(theme_name=None): class TotalTQDM: # compatibility with previous global-tqdm + # import tqdm def __init__(self): pass def reset(self): diff --git a/modules/textual_inversion/textual_inversion.py b/modules/textual_inversion/textual_inversion.py index a9e200822..b959034ef 100644 --- a/modules/textual_inversion/textual_inversion.py +++ b/modules/textual_inversion/textual_inversion.py @@ -159,11 +159,6 @@ class EmbeddingDatabase: self.register_embedding(embedding, shared.sd_model) except Exception: self.skipped_embeddings[name] = embedding - try: - text_inv_tokens = pipe.tokenizer.added_tokens_encoder.keys() - text_inv_tokens = [t for t in text_inv_tokens if not (len(t.split("_")) > 1 and t.split("_")[-1].isdigit())] - except Exception: - text_inv_tokens = [] def load_from_file(self, path, filename): name, ext = os.path.splitext(filename)