lint updates and diffusers installer

This commit is contained in:
Vladimir Mandic
2024-09-06 14:10:53 -04:00
parent 8a5722fc88
commit f2c5cbbb36
26 changed files with 75 additions and 83 deletions
+4 -5
View File
@@ -217,10 +217,9 @@ def control_run(units: List[unit.Unit] = [], inputs: List[Image.Image] = [], ini
debug(f'Control unit offload: model="{u.controlnet.model_id}" device={devices.cpu}')
sd_models.move_model(u.controlnet.model, devices.cpu)
continue
else:
if u.controlnet is not None and u.controlnet.model is not None:
debug(f'Control unit offload: model="{u.controlnet.model_id}" device={devices.device}')
sd_models.move_model(u.controlnet.model, devices.device)
if u.controlnet is not None and u.controlnet.model is not None:
debug(f'Control unit offload: model="{u.controlnet.model_id}" device={devices.device}')
sd_models.move_model(u.controlnet.model, devices.device)
if unit_type == 't2i adapter' and u.adapter.model is not None:
active_process.append(u.process)
active_model.append(u.adapter)
@@ -548,7 +547,7 @@ def control_run(units: List[unit.Unit] = [], inputs: List[Image.Image] = [], ini
elif unit_type == 'controlnet' and has_models:
if input_type == 0: # Control only
if shared.sd_model_type == 'f1':
p.task_args['control_image'] = p.init_images # TODO flux controlnet mandates this
p.task_args['control_image'] = p.init_images # flux controlnet mandates this
p.task_args['strength'] = p.denoising_strength
elif input_type == 1: # Init image same as control
p.task_args['control_image'] = p.init_images # switch image and control_image
+1 -1
View File
@@ -103,7 +103,7 @@ def process_batch(p, input_files, input_dir, output_dir, inpaint_mask_dir, args)
for k, v in items.items():
image.info[k] = v
images.save_image(image, path=output_dir, basename=basename, seed=None, prompt=None, extension=ext, info=geninfo, short_filename=True, no_prompt=True, grid=False, pnginfo_section_name="extras", existing_info=image.info, forced_filename=forced_filename)
proce = modules.scripts.scripts_img2img.after(p, proc, *args)
proc = modules.scripts.scripts_img2img.after(p, proc, *args)
shared.log.debug(f'Processed: images={len(batch_image_files)} memory={memory_stats()} batch')
+6 -6
View File
@@ -25,7 +25,7 @@ def get_quant(file_path):
return 'none'
def load_flux_quanto(checkpoint_info, diffusers_load_config):
def load_flux_quanto(checkpoint_info):
transformer, text_encoder_2 = None, None
from installer import install
install('optimum-quanto', quiet=True)
@@ -132,11 +132,11 @@ def load_transformer(file_path): # triggered by opts.sd_unet change
shared.log.info(f'Loading UNet: type=FLUX file="{file_path}" offload={shared.opts.diffusers_offload_mode} quant={quant} dtype={devices.dtype}')
if 'nf4' in quant:
from modules.model_flux_nf4 import load_flux_nf4
_transformer, _text_encoder_2 = load_flux_nf4(file_path, diffusers_load_config)
_transformer, _text_encoder_2 = load_flux_nf4(file_path)
if _transformer is not None:
transformer = _transformer
elif quant == 'qint8' or quant == 'qint4':
_transformer, _text_encoder_2 = load_flux_quanto(file_path, diffusers_load_config)
_transformer, _text_encoder_2 = load_flux_quanto(file_path)
if _transformer is not None:
transformer = _transformer
elif quant == 'fp8' or quant == 'fp4':
@@ -206,7 +206,7 @@ def load_flux(checkpoint_info, diffusers_load_config): # triggered by opts.sd_ch
if quant == 'nf4':
try:
from modules.model_flux_nf4 import load_flux_nf4
_transformer, _text_encoder = load_flux_nf4(checkpoint_info, diffusers_load_config)
_transformer, _text_encoder = load_flux_nf4(checkpoint_info)
if _transformer is not None:
transformer = _transformer
if _text_encoder is not None:
@@ -218,7 +218,7 @@ def load_flux(checkpoint_info, diffusers_load_config): # triggered by opts.sd_ch
errors.display(e, 'FLUX NF4:')
if quant == 'qint8' or quant == 'qint4':
try:
_transformer, _text_encoder = load_flux_quanto(checkpoint_info, diffusers_load_config)
_transformer, _text_encoder = load_flux_quanto(checkpoint_info)
if _transformer is not None:
transformer = _transformer
if _text_encoder is not None:
@@ -239,7 +239,7 @@ def load_flux(checkpoint_info, diffusers_load_config): # triggered by opts.sd_ch
components['vae'] = vae
shared.log.debug(f'Loading FLUX: preloaded={list(components)}')
if repo_id == 'sayakpaul/flux.1-dev-nf4':
repo_id = 'black-forest-labs/FLUX.1-dev' # TODO fix for since sayakpaul model is missing model_index.json
repo_id = 'black-forest-labs/FLUX.1-dev' # workaround since sayakpaul model is missing model_index.json
pipe = diffusers.FluxPipeline.from_pretrained(repo_id, cache_dir=shared.opts.diffusers_dir, **components, **diffusers_load_config)
try:
diffusers.pipelines.auto_pipeline.AUTO_TEXT2IMAGE_PIPELINES_MAPPING["flux"] = diffusers.FluxPipeline
+1 -1
View File
@@ -162,7 +162,7 @@ def create_quantized_param(
module._parameters[tensor_name] = new_value # pylint: disable=protected-access
def load_flux_nf4(checkpoint_info, diffusers_load_config):
def load_flux_nf4(checkpoint_info):
load_bnb()
transformer = None
text_encoder_2 = None
+1 -1
View File
@@ -78,7 +78,7 @@ def set_t5(pipe, module, t5=None, cache_dir=None):
return pipe
t5 = load_t5(t5=t5, cache_dir=cache_dir)
if module == "text_encoder_2" and t5 is None: # do not unload te2
return
return None
setattr(pipe, module, t5)
if shared.opts.diffusers_offload_mode == "sequential":
from accelerate import cpu_offload
+1 -1
View File
@@ -169,7 +169,7 @@ class StableDiffusionProcessing:
def setup_scripts(self):
self.scripts_setup_complete = True
self.scripts.setup_scrips(self, is_ui=not self.is_api)
self.scripts.setup_scripts()
def comment(self, text):
self.comments[text] = 1
-14
View File
@@ -26,20 +26,6 @@ def process_diffusers(p: processing.StableDiffusionProcessing):
return p.enable_hr and p.refiner_steps > 0 and p.refiner_start > 0 and p.refiner_start < 1 and shared.sd_refiner is not None
def update_pipeline(sd_model, p: processing.StableDiffusionProcessing):
"""
import diffusers
if p.sag_scale > 0 and is_txt2img():
update_sampler(shared.sd_model)
supported = ['DDIMScheduler', 'PNDMScheduler', 'DDPMScheduler', 'DEISMultistepScheduler', 'UniPCMultistepScheduler', 'DPMSolverMultistepScheduler', 'DPMSolverSinlgestepScheduler']
if hasattr(sd_model, 'sfast'):
shared.log.warning(f'SAG incompatible compile mode: backend={shared.opts.cuda_compile_backend}')
elif sd_model.scheduler.__class__.__name__ in supported:
sd_model = sd_models.switch_pipe(diffusers.StableDiffusionSAGPipeline, sd_model)
p.extra_generation_params["SAG scale"] = p.sag_scale
p.task_args['sag_scale'] = p.sag_scale
else:
shared.log.warning(f'SAG incompatible scheduler: current={sd_model.scheduler.__class__.__name__} supported={supported}')
"""
if sd_models.get_diffusers_task(sd_model) == sd_models.DiffusersTaskType.INPAINTING and getattr(p, 'image_mask', None) is None and p.task_args.get('image_mask', None) is None and getattr(p, 'mask', None) is None:
shared.log.warning('Processing: mode=inpaint mask=None')
sd_model = sd_models.set_diffuser_pipe(sd_model, sd_models.DiffusersTaskType.IMAGE_2_IMAGE)
+2 -3
View File
@@ -33,6 +33,7 @@ def create_latents(image, p, dtype=None, device=None):
def full_vae_decode(latents, model):
t0 = time.time()
base_device = None
if shared.opts.diffusers_move_unet and not getattr(model, 'has_accelerate', False):
base_device = sd_models.move_base(model, devices.cpu)
if shared.opts.diffusers_offload_mode == "balanced":
@@ -66,7 +67,7 @@ def full_vae_decode(latents, model):
decoded = model.vae.decode(latents, return_dict=False)[0]
# delete vae after OpenVINO compile
if shared.opts.cuda_compile and shared.opts.cuda_compile_backend == "openvino_fx" and shared.compiled_model_state.first_pass_vae:
if 'VAE' in shared.opts.cuda_compile and shared.opts.cuda_compile_backend == "openvino_fx" and shared.compiled_model_state.first_pass_vae:
shared.compiled_model_state.first_pass_vae = False
if not shared.opts.openvino_disable_memory_cleanup and hasattr(shared.sd_model, "vae"):
model.vae.apply(sd_models.convert_to_faketensors)
@@ -136,8 +137,6 @@ def vae_decode(latents, model, output_type='np', full_quality=True):
decoded = full_vae_decode(latents=latents, model=shared.sd_model)
else:
decoded = taesd_vae_decode(latents=latents)
# TODO validate decoded sample diffusers
# decoded = validate_sample(decoded)
if hasattr(model, 'image_processor'):
imgs = model.image_processor.postprocess(decoded, output_type=output_type)
else:
+3 -3
View File
@@ -679,12 +679,12 @@ def get_weighted_text_embeddings_sdxl_refiner(
for j in range(len(weight_tensor_2)):
if weight_tensor_2[j] != 1.0:
ow = weight_tensor_2[j] - 1
# ow = weight_tensor_2[j] - 1
# optional process
# To map number of (0,1) to (-1,1)
tanh_weight = (math.exp(ow) / (math.exp(ow) + 1) - 0.5) * 2
weight = 1 + tanh_weight
# tanh_weight = (math.exp(ow) / (math.exp(ow) + 1) - 0.5) * 2
# weight = 1 + tanh_weight
# add weight method 1:
# token_embedding[j] = token_embedding[j] * weight
+1
View File
@@ -491,6 +491,7 @@ class ScriptRunner:
if hasattr(script, 'run'):
processed = script.run(p, *parsed)
else:
processed = None
errors.log.error(f'Script: file="{script.filename}" no run function defined')
s.record(script.title())
s.report()
+2 -3
View File
@@ -1,7 +1,7 @@
from functools import cache, wraps
import torch
from diffusers.utils import USE_PEFT_BACKEND
from diffusers.utils import USE_PEFT_BACKEND # pylint: disable=unused-import
from modules import shared, devices
@@ -107,8 +107,7 @@ class DynamicAttnProcessorBMM:
based on AttnProcessor V1
"""
def __call__(self, attn, hidden_states: torch.Tensor, encoder_hidden_states=None, attention_mask=None,
temb=None, *args, **kwargs) -> torch.Tensor: # pylint: disable=too-many-statements, too-many-locals, too-many-branches
def __call__(self, attn, hidden_states: torch.Tensor, encoder_hidden_states=None, attention_mask=None, temb=None, *args, **kwargs) -> torch.Tensor: # pylint: disable=too-many-statements, too-many-locals, too-many-branches, keyword-arg-before-vararg
residual = hidden_states
+5 -5
View File
@@ -907,7 +907,7 @@ def move_model(model, device=None, force=False):
if hasattr(model, "prior_pipe"):
model.prior_pipe.to(device)
except Exception as e0:
if 'Cannot copy out of meta tensor' in str(e0) or 'must be Tensor, not NoneType':
if 'Cannot copy out of meta tensor' in str(e0) or 'must be Tensor, not NoneType' in str(e0):
if hasattr(model, "components"):
for _name, component in model.components.items():
if hasattr(component, 'modules'):
@@ -1316,7 +1316,7 @@ def load_diffuser(checkpoint_info=None, already_loaded_state_dict=None, timer=No
insert_parser_highjack(sd_model.__class__.__name__)
set_diffuser_options(sd_model, vae, op, offload=False)
if shared.opts.nncf_compress_weights and not (shared.opts.cuda_compile and shared.opts.cuda_compile_backend == "openvino_fx"):
if shared.opts.nncf_compress_weights and not ('Model' in shared.opts.cuda_compile and shared.opts.cuda_compile_backend == "openvino_fx"):
sd_model = sd_models_compile.nncf_compress_weights(sd_model) # run this before move model so it can be compressed in CPU
if shared.opts.optimum_quanto_weights:
sd_model = sd_models_compile.optimum_quanto_weights(sd_model) # run this before move model so it can be compressed in CPU
@@ -1337,7 +1337,7 @@ def load_diffuser(checkpoint_info=None, already_loaded_state_dict=None, timer=No
if shared.opts.ipex_optimize:
sd_model = sd_models_compile.ipex_optimize(sd_model)
if (shared.opts.cuda_compile and shared.opts.cuda_compile_backend != 'none'):
if ('Model' in shared.opts.cuda_compile and shared.opts.cuda_compile_backend != 'none'):
sd_model = sd_models_compile.compile_diffusers(sd_model)
timer.record("compile")
@@ -1488,7 +1488,7 @@ def set_diffuser_pipe(pipe, new_pipe_type):
return pipe
# skip specific pipelines
if n in ['StableDiffusionReferencePipeline', 'StableDiffusionAdapterPipeline', 'AnimateDiffPipeline', 'AnimateDiffSDXLPipeline', 'FluxControlNetPipeline']: # TODO flux does not have inpaint/img2img yet
if n in ['StableDiffusionReferencePipeline', 'StableDiffusionAdapterPipeline', 'AnimateDiffPipeline', 'AnimateDiffSDXLPipeline', 'FluxControlNetPipeline']:
return pipe
if 'Onnx' in pipe.__class__.__name__:
return pipe
@@ -1832,7 +1832,7 @@ def unload_model_weights(op='model'):
from modules import sd_hijack
move_model(model_data.sd_model, devices.cpu)
sd_hijack.model_hijack.undo_hijack(model_data.sd_model)
elif not (shared.opts.cuda_compile and shared.opts.cuda_compile_backend == "openvino_fx"):
elif not ('Model' in shared.opts.cuda_compile and shared.opts.cuda_compile_backend == "openvino_fx"):
disable_offload(model_data.sd_model)
move_model(model_data.sd_model, 'meta')
model_data.sd_model = None
+4 -4
View File
@@ -421,7 +421,7 @@ def compile_torch(sd_model):
except Exception as e:
shared.log.error(f"Torch inductor config error: {e}")
sd_model = apply_compile_to_model(sd_model, torch_compile_model, shared.opts.cuda_compile, op="compile")
sd_model = apply_compile_to_model(sd_model, function=torch_compile_model, options=shared.opts.cuda_compile, op="compile")
setup_logging() # compile messes with logging so reset is needed
if shared.opts.cuda_compile_precompile:
@@ -462,7 +462,7 @@ def compile_deepcache(sd_model):
def compile_diffusers(sd_model):
if not shared.opts.cuda_compile:
if 'Model' not in shared.opts.cuda_compile:
return sd_model
if shared.opts.cuda_compile_backend == 'none':
shared.log.warning('Model compile enabled but no backend specified')
@@ -518,7 +518,7 @@ def dynamic_quantization(sd_model):
def openvino_recompile_model(p, hires=False, refiner=False): # recompile if a parameter changes
if shared.opts.cuda_compile and shared.opts.cuda_compile_backend != 'none':
if 'Model' in shared.opts.cuda_compile and shared.opts.cuda_compile_backend != 'none':
if shared.opts.cuda_compile_backend == "openvino_fx":
compile_height = p.height if not hires and hasattr(p, 'height') else p.hr_upscale_to_y
compile_width = p.width if not hires and hasattr(p, 'width') else p.hr_upscale_to_x
@@ -541,7 +541,7 @@ def openvino_recompile_model(p, hires=False, refiner=False): # recompile if a pa
def openvino_post_compile(op="base"): # delete unet after OpenVINO compile
if shared.opts.cuda_compile and shared.opts.cuda_compile_backend == "openvino_fx":
if 'Model' in shared.opts.cuda_compile and shared.opts.cuda_compile_backend == "openvino_fx":
if shared.compiled_model_state.first_pass and op == "base":
shared.compiled_model_state.first_pass = False
if not shared.opts.openvino_disable_memory_cleanup and hasattr(shared.sd_model, "unet"):
+1 -1
View File
@@ -61,7 +61,7 @@ def single_sample_to_image(sample, approximation=None):
if approximation == 2: # TAESD
x_sample = sd_vae_taesd.decode(sample)
x_sample = (1.0 + x_sample) / 2.0 # preview requires smaller range
elif sd_cascade and not approximation == 3:
elif sd_cascade and approximation != 3:
x_sample = sd_vae_stablecascade.decode(sample)
elif approximation == 0: # Simple
x_sample = sd_vae_approx.cheap_approximation(sample) * 0.5 + 0.5
+1
View File
@@ -419,6 +419,7 @@ options_templates.update(options_section(('sd', "Execution & Models"), {
"sd_checkpoint_cache": OptionInfo(0, "Cached models", gr.Slider, {"minimum": 0, "maximum": 10, "step": 1, "visible": not native }),
"sd_vae_checkpoint_cache": OptionInfo(0, "Cached VAEs", gr.Slider, {"minimum": 0, "maximum": 10, "step": 1, "visible": False}),
"sd_disable_ckpt": OptionInfo(False, "Disallow models in ckpt format", gr.Checkbox, {"visible": False}),
"diffusers_version": OptionInfo("", "Diffusers version", gr.Textbox, {"visible": False}),
}))
options_templates.update(options_section(('cuda', "Compute Settings"), {
+12 -12
View File
@@ -23,12 +23,12 @@ def open_embeddings(filename):
"""
Load Embedding files from drive. Image embeddings not currently supported.
"""
if filename is None:
return
filenames = list(filename)
exts = [".SAFETENSORS", '.BIN', '.PT']
embeddings = []
skipped = []
if filename is None:
return embeddings, skipped
filenames = list(filename)
exts = [".SAFETENSORS", '.BIN', '.PT']
for _filename in filenames:
# debug(f'Embedding check: {filename}')
fullname = _filename
@@ -274,15 +274,15 @@ class EmbeddingDatabase:
"""
overwrite = bool(data)
if not shared.sd_loaded:
return 0
return
embeddings, skipped = open_embeddings(filename) or convert_bundled(data)
for skip in skipped:
self.skipped_embeddings[skip.name] = skipped
if not embeddings:
return 0
return
text_encoders, tokenizers, hiddensizes = get_text_encoders()
if not all([text_encoders, tokenizers, hiddensizes]):
return 0
return
for embedding in embeddings:
try:
embedding.vector_sizes = [v.shape[-1] for v in embedding.vec]
@@ -320,20 +320,20 @@ class EmbeddingDatabase:
if ext in ['.PNG', '.WEBP', '.JXL', '.AVIF']:
if '.preview' in filename.lower():
return None
return
embed_image = Image.open(path)
if hasattr(embed_image, 'text') and 'sd-ti-embedding' in embed_image.text:
data = embedding_from_b64(embed_image.text['sd-ti-embedding'])
else:
data = extract_image_data_embed(embed_image)
if not data: # if data is None, means this is not an embeding, just a preview image
return None
return
elif ext in ['.BIN', '.PT']:
data = torch.load(path, map_location="cpu")
elif ext in ['.SAFETENSORS']:
data = safetensors.torch.load_file(path, device="cpu")
else:
return None
return
# textual inversion embeddings
if 'string_to_param' in data:
@@ -345,7 +345,7 @@ class EmbeddingDatabase:
elif type(data) == dict and type(next(iter(data.values()))) == torch.Tensor:
if len(data.keys()) != 1:
self.skipped_embeddings[name] = Embedding(None, name=name, filename=path)
return None
return
emb = next(iter(data.values()))
if len(emb.shape) == 1:
emb = emb.unsqueeze(0)
@@ -353,7 +353,7 @@ class EmbeddingDatabase:
raise RuntimeError(f"Couldn't identify {filename} as textual inversion embedding")
if shared.native:
return emb
return
vec = emb.detach().to(devices.device, dtype=torch.float32)
# name = data.get('name', name)
+5 -3
View File
@@ -243,9 +243,11 @@ def create_ui(startup_timer = None):
if cmd_opts.use_directml:
directml_override_opts()
if cmd_opts.use_openvino:
if not shared.opts.cuda_compile:
shared.log.warning("OpenVINO: Enabling Torch Compile")
shared.opts.cuda_compile = True
shared.log.warning("OpenVINO: Enabling Torch Compile")
if "Model" not in shared.opts.cuda_compile:
shared.opts.cuda_compile.append("Model")
if "VAE" not in shared.opts.cuda_compile:
shared.opts.cuda_compile.append("VAE")
if shared.opts.cuda_compile_backend != "openvino_fx":
shared.log.warning("OpenVINO: Setting Torch Compiler backend to OpenVINO FX")
shared.opts.cuda_compile_backend = "openvino_fx"
+1 -1
View File
@@ -136,7 +136,7 @@ def create_ui(_blocks: gr.Blocks=None):
with gr.Row(variant='compact', elem_id="control_extra_networks", visible=False) as extra_networks_ui:
from modules import timer, ui_extra_networks
extra_networks_ui = ui_extra_networks.create_ui(extra_networks_ui, btn_extra, 'control', skip_indexing=shared.opts.extra_network_skip_indexing)
timer.startup.record('ui-en')
timer.startup.record('ui-networks')
with gr.Row(elem_id='control-inputs'):
with gr.Column(scale=9, elem_id='control-input-column', visible=True) as _column_input:
+1 -1
View File
@@ -44,7 +44,7 @@ def create_ui():
with gr.Row(variant='compact', elem_id="img2img_extra_networks", visible=False) as extra_networks_ui:
from modules import ui_extra_networks
extra_networks_ui_img2img = ui_extra_networks.create_ui(extra_networks_ui, img2img_extra_networks_button, 'img2img', skip_indexing=shared.opts.extra_network_skip_indexing)
timer.startup.record('ui-en')
timer.startup.record('ui-networks')
with gr.Row(elem_id="img2img_interface", equal_height=False):
with gr.Column(variant='compact', elem_id="img2img_settings"):
+1 -1
View File
@@ -29,7 +29,7 @@ def create_ui():
with gr.Row(variant='compact', elem_id="txt2img_extra_networks", visible=False) as extra_networks_ui:
from modules import ui_extra_networks
extra_networks_ui = ui_extra_networks.create_ui(extra_networks_ui, txt2img_extra_networks_button, 'txt2img', skip_indexing=shared.opts.extra_network_skip_indexing)
timer.startup.record('ui-en')
timer.startup.record('ui-networks')
with gr.Row(elem_id="txt2img_interface", equal_height=False):
with gr.Column(variant='compact', elem_id="txt2img_settings"):