Skip to content

Commit

Permalink
settings option to disable reference models
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Mandic <[email protected]>
  • Loading branch information
vladmandic committed Jan 23, 2025
1 parent fef2be9 commit 06ba03c
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 42 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@
- **gallery**: add http fallback for slow/unreliable links
- **splash**: add legacy mode indicator on splash screen
- **network**: extract thumbnail from model metadata if present
- **network**: setting value to disable use of reference models
- **Refactor**:
- **upscale**: code refactor to unify latent, resize and model based upscalers
- **loader**: ability to run in-memory models
- **schedulers**: ability to create model-less schedulers
- **quantiation**: code refactor into dedicated module
- **Fixes**:
- non-full vae decode
- send-to image transfer
Expand Down
10 changes: 5 additions & 5 deletions modules/face/photomaker_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def encode_prompt_with_trigger_word(
# textual inversion: process multi-vector tokens if necessary
prompt_embeds_list = []
prompts = [prompt, prompt_2]
for prompt, tokenizer, text_encoder in zip(prompts, tokenizers, text_encoders):
for prompt, tokenizer, text_encoder in zip(prompts, tokenizers, text_encoders): # pylint: disable=redefined-argument-from-local
if isinstance(self, TextualInversionLoaderMixin):
prompt = self.maybe_convert_prompt(prompt, tokenizer)

Expand Down Expand Up @@ -367,7 +367,7 @@ def encode_prompt_with_trigger_word(
f"`negative_prompt` should be the same type to `prompt`, but got {type(negative_prompt)} !="
f" {type(prompt)}."
)
elif batch_size != len(negative_prompt):
if batch_size != len(negative_prompt):
raise ValueError(
f"`negative_prompt`: {negative_prompt} has batch size {len(negative_prompt)}, but `prompt`:"
f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"
Expand All @@ -377,7 +377,7 @@ def encode_prompt_with_trigger_word(
uncond_tokens = [negative_prompt, negative_prompt_2]

negative_prompt_embeds_list = []
for negative_prompt, tokenizer, text_encoder in zip(uncond_tokens, tokenizers, text_encoders):
for negative_prompt, tokenizer, text_encoder in zip(uncond_tokens, tokenizers, text_encoders): # pylint: disable=redefined-argument-from-local
if isinstance(self, TextualInversionLoaderMixin):
negative_prompt = self.maybe_convert_prompt(negative_prompt, tokenizer)

Expand Down Expand Up @@ -735,8 +735,8 @@ def __call__(
):
discrete_timestep_cutoff = int(
round(
self.scheduler.config.num_train_timesteps
- (self.denoising_end * self.scheduler.config.num_train_timesteps)
self.scheduler.config.num_train_timesteps # pylint: disable=no-member
- (self.denoising_end * self.scheduler.config.num_train_timesteps) # pylint: disable=no-member
)
)
num_inference_steps = len(list(filter(lambda ts: ts >= discrete_timestep_cutoff, timesteps)))
Expand Down
8 changes: 4 additions & 4 deletions modules/intel/openvino/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ def openvino_compile(gm: GraphModule, *example_inputs, model_hash_str: str = Non
for idx, _ in enumerate(example_inputs):
new_inputs.append(example_inputs[idx].detach().cpu().numpy())
new_inputs = [new_inputs]
if shared.opts.nncf_quant_mode == "INT8":
if shared.opts.nncf_quantize_mode == "INT8":
om = nncf.quantize(om, nncf.Dataset(new_inputs))
else:
om = nncf.quantize(om, nncf.Dataset(new_inputs), mode=getattr(nncf.QuantizationMode, shared.opts.nncf_quant_mode),
om = nncf.quantize(om, nncf.Dataset(new_inputs), mode=getattr(nncf.QuantizationMode, shared.opts.nncf_quantize_mode),
advanced_parameters=nncf.quantization.advanced_parameters.AdvancedQuantizationParameters(
overflow_fix=nncf.quantization.advanced_parameters.OverflowFix.DISABLE, backend_params=None))

Expand Down Expand Up @@ -281,10 +281,10 @@ def openvino_compile_cached_model(cached_model_path, *example_inputs):
for idx, _ in enumerate(example_inputs):
new_inputs.append(example_inputs[idx].detach().cpu().numpy())
new_inputs = [new_inputs]
if shared.opts.nncf_quant_mode == "INT8":
if shared.opts.nncf_quantize_mode == "INT8":
om = nncf.quantize(om, nncf.Dataset(new_inputs))
else:
om = nncf.quantize(om, nncf.Dataset(new_inputs), mode=getattr(nncf.QuantizationMode, shared.opts.nncf_quant_mode),
om = nncf.quantize(om, nncf.Dataset(new_inputs), mode=getattr(nncf.QuantizationMode, shared.opts.nncf_quantize_mode),
advanced_parameters=nncf.quantization.advanced_parameters.AdvancedQuantizationParameters(
overflow_fix=nncf.quantization.advanced_parameters.OverflowFix.DISABLE, backend_params=None))

Expand Down
14 changes: 7 additions & 7 deletions modules/merging/modules_sdxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ def load_base(override:str=None):
pipeline = diffusers.StableDiffusionXLPipeline.from_pretrained(fn, cache_dir=shared.opts.hfcache_dir, torch_dtype=recipe.dtype, add_watermarker=False)
else:
yield msg('base: not found')
return None
return
pipeline.vae.register_to_config(force_upcast = False)


def load_unet(pipe: diffusers.StableDiffusionXLPipeline, override:str=None):
if (recipe.unet is None or len(recipe.unet) == 0) and override is None:
return None
return
fn = override or recipe.unet
if not os.path.isabs(fn):
fn = os.path.join(shared.opts.unet_dir, fn)
Expand All @@ -92,7 +92,7 @@ def load_unet(pipe: diffusers.StableDiffusionXLPipeline, override:str=None):

def load_scheduler(pipe: diffusers.StableDiffusionXLPipeline, override:str=None):
if recipe.scheduler is None and override is None:
return None
return
config = pipe.scheduler.config.__dict__
scheduler = override or recipe.scheduler
yield msg(f'scheduler={scheduler}')
Expand All @@ -107,7 +107,7 @@ def load_scheduler(pipe: diffusers.StableDiffusionXLPipeline, override:str=None)

def load_vae(pipe: diffusers.StableDiffusionXLPipeline, override:str=None):
if (recipe.vae is None or len(recipe.vae) == 0)and override is None:
return None
return
fn = override or recipe.vae
if not os.path.isabs(fn):
fn = os.path.join(shared.opts.vae_dir, fn)
Expand All @@ -128,7 +128,7 @@ def load_vae(pipe: diffusers.StableDiffusionXLPipeline, override:str=None):

def load_te1(pipe: diffusers.StableDiffusionXLPipeline, override:str=None):
if (recipe.te1 is None or len(recipe.te1) == 0) and override is None:
return None
return
config = pipe.text_encoder.config.__dict__
pretrained_config = transformers.PretrainedConfig.from_dict(config)
fn = override or recipe.te1
Expand All @@ -149,7 +149,7 @@ def load_te1(pipe: diffusers.StableDiffusionXLPipeline, override:str=None):

def load_te2(pipe: diffusers.StableDiffusionXLPipeline, override:str=None):
if (recipe.te2 is None or len(recipe.te2) == 0) and override is None:
return None
return
config = pipe.text_encoder_2.config.__dict__
pretrained_config = transformers.PretrainedConfig.from_dict(config)
fn = override or recipe.te2
Expand All @@ -170,7 +170,7 @@ def load_te2(pipe: diffusers.StableDiffusionXLPipeline, override:str=None):

def load_lora(pipe: diffusers.StableDiffusionXLPipeline, override: dict=None, fuse: float=None):
if recipe.lora is None and override is None:
return None
return
names = []
pipe.unfuse_lora()
pipe.unload_lora_weights()
Expand Down
4 changes: 2 additions & 2 deletions modules/model_quant.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def nncf_compress_model(model, op=None, sd_model=None):
nncf_send_to_device(model, devices.device)
if hasattr(model, "set_input_embeddings") and backup_embeddings is not None:
model.set_input_embeddings(backup_embeddings)
if op is not None and shared.opts.quant_shuffle_weights:
if op is not None and shared.opts.nncf_quantize_shuffle_weights:
if quant_last_model_name is not None:
if "." in quant_last_model_name:
last_model_names = quant_last_model_name.split(".")
Expand Down Expand Up @@ -290,7 +290,7 @@ def optimum_quanto_model(model, op=None, sd_model=None, weights=None, activation
quanto.freeze(model)
if hasattr(model, "set_input_embeddings") and backup_embeddings is not None:
model.set_input_embeddings(backup_embeddings)
if op is not None and shared.opts.quant_shuffle_weights:
if op is not None and shared.opts.optimum_quanto_shuffle_weights:
if quant_last_model_name is not None:
if "." in quant_last_model_name:
last_model_names = quant_last_model_name.split(".")
Expand Down
6 changes: 3 additions & 3 deletions modules/perflow/pfode_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def solve(self,
timestep_cond = None
if unet.config.time_cond_proj_dim is not None:
guidance_scale_tensor = torch.tensor(guidance_scale - 1).repeat(bsz)
timestep_cond = self.get_guidance_scale_embedding(
timestep_cond = self.get_guidance_scale_embedding( # pylint: disable=no-member
guidance_scale_tensor, embedding_dim=unet.config.time_cond_proj_dim
).to(device=latents.device, dtype=latents.dtype)

Expand Down Expand Up @@ -124,7 +124,7 @@ def __init__(self, scheduler, t_initial=1, t_terminal=0,) -> None:
self.t_terminal = t_terminal
self.scheduler = scheduler

train_step_terminal = 0
train_step_terminal = 0
train_step_initial = train_step_terminal + self.scheduler.config.num_train_timesteps # 0+1000

self.stepsize = (t_terminal-t_initial) / (train_step_terminal - train_step_initial) #1/1000
Expand Down Expand Up @@ -186,7 +186,7 @@ def solve(self,
timestep_cond = None
if unet.config.time_cond_proj_dim is not None:
guidance_scale_tensor = torch.tensor(guidance_scale - 1).repeat(bsz)
timestep_cond = self.get_guidance_scale_embedding(
timestep_cond = self.get_guidance_scale_embedding( # pylint: disable=no-member
guidance_scale_tensor, embedding_dim=unet.config.time_cond_proj_dim
).to(device=latents.device, dtype=latents.dtype)

Expand Down
5 changes: 2 additions & 3 deletions modules/sd_models_compile.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import copy
import time
import logging
import torch
from modules import shared, devices, sd_models, model_quant
from installer import install, setup_logging
from modules import shared, devices, sd_models
from installer import setup_logging


#Used by OpenVINO, can be used with TensorRT or Olive
Expand Down
18 changes: 10 additions & 8 deletions modules/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ def get_default_modes():
}))

options_templates.update(options_section(('quantization', "Quantization Settings"), {
"bnb_sep": OptionInfo("<h2>BitsAndBytes</h2>", "", gr.HTML),
"bnb_quantization_sep": OptionInfo("<h2>BitsAndBytes</h2>", "", gr.HTML),
"bnb_quantization": OptionInfo([], "Quantization enabled", gr.CheckboxGroup, {"choices": ["Model", "VAE", "Text Encoder"], "visible": native}),
"bnb_quantization_type": OptionInfo("nf4", "Quantization type", gr.Dropdown, {"choices": ['nf4', 'fp8', 'fp4'], "visible": native}),
"bnb_quantization_storage": OptionInfo("uint8", "Backend storage", gr.Dropdown, {"choices": ["float16", "float32", "int8", "uint8", "float64", "bfloat16"], "visible": native}),
Expand All @@ -569,22 +569,23 @@ def get_default_modes():
"optimum_quanto_weights": OptionInfo([], "Quantization enabled", gr.CheckboxGroup, {"choices": ["Model", "VAE", "Text Encoder", "ControlNet"], "visible": native}),
"optimum_quanto_weights_type": OptionInfo("qint8", "Quantization weights type", gr.Dropdown, {"choices": ['qint8', 'qfloat8_e4m3fn', 'qfloat8_e5m2', 'qint4', 'qint2'], "visible": native}),
"optimum_quanto_activations_type": OptionInfo("none", "Quantization activations type ", gr.Dropdown, {"choices": ['none', 'qint8', 'qfloat8_e4m3fn', 'qfloat8_e5m2'], "visible": native}),
"optimum_quanto_shuffle_weights": OptionInfo(False, "Shuffle weights", gr.Checkbox, {"visible": native}),

"torchao_sep": OptionInfo("<h2>TorchAO</h2>", "", gr.HTML),
"torchao_quantization": OptionInfo([], "Quantization enabled", gr.CheckboxGroup, {"choices": ["Model", "VAE", "Text Encoder"], "visible": native}),
"torchao_quantization_mode": OptionInfo("pre", "Quantization mode", gr.Dropdown, {"choices": ['pre', 'post'], "visible": native}),
"torchao_quantization_type": OptionInfo("int8_weight_only", "Quantization type", gr.Dropdown, {"choices": ['int4_weight_only', 'int8_dynamic_activation_int4_weight', 'int8_weight_only', 'int8_dynamic_activation_int8_weight', 'float8_weight_only', 'float8_dynamic_activation_float8_weight', 'float8_static_activation_float8_weight'], "visible": native}),

"nncf_sep": OptionInfo("<h2>NNCF</h2>", "", gr.HTML),
"nncf_compress_sep": OptionInfo("<h2>NNCF</h2>", "", gr.HTML),
"nncf_compress_weights": OptionInfo([], "Quantization enabled", gr.CheckboxGroup, {"choices": ["Model", "VAE", "Text Encoder", "ControlNet"], "visible": native}),
"nncf_compress_weights_mode": OptionInfo("INT8", "Quantization type", gr.Dropdown, {"choices": ['INT8', 'INT8_SYM', 'INT4_ASYM', 'INT4_SYM', 'NF4'] if cmd_opts.use_openvino else ['INT8']}),
"nncf_compress_weights_raito": OptionInfo(0, "Compress ratio", gr.Slider, {"minimum": 0, "maximum": 1, "step": 0.01, "visible": cmd_opts.use_openvino}),
"nncf_compress_weights_group_size": OptionInfo(0, "Group size", gr.Slider, {"minimum": -1, "maximum": 512, "step": 1, "visible": cmd_opts.use_openvino}),
"nncf_quantize": OptionInfo([], "OpenVINO enabled", gr.CheckboxGroup, {"choices": ["Model", "VAE", "Text Encoder"], "visible": cmd_opts.use_openvino}),
"nncf_quant_mode": OptionInfo("INT8", "OpenVINO mode", gr.Dropdown, {"choices": ['INT8', 'FP8_E4M3', 'FP8_E5M2'], "visible": cmd_opts.use_openvino}),
"quant_shuffle_weights": OptionInfo(False, "Shuffle weights", gr.Checkbox, {"visible": native}),
"nncf_quantize_mode": OptionInfo("INT8", "OpenVINO mode", gr.Dropdown, {"choices": ['INT8', 'FP8_E4M3', 'FP8_E5M2'], "visible": cmd_opts.use_openvino}),
"nncf_quantize_shuffle_weights": OptionInfo(False, "Shuffle weights", gr.Checkbox, {"visible": native}),

"layerwise_sep": OptionInfo("<h2>Layerwise Casting</h2>", "", gr.HTML),
"layerwise_quantization_sep": OptionInfo("<h2>Layerwise Casting</h2>", "", gr.HTML),
"layerwise_quantization": OptionInfo([], "Layerwise casting enabled", gr.CheckboxGroup, {"choices": ["Model", "Transformer", "Text Encoder"], "visible": native}),
"layerwise_quantization_storage": OptionInfo("float8_e4m3fn", "Layerwise casting storage", gr.Dropdown, {"choices": ["float8_e4m3fn", "float8_e5m2"], "visible": native}),
"layerwise_quantization_nonblocking": OptionInfo(False, "Layerwise non-blocking operations", gr.Checkbox, {"visible": native}),
Expand Down Expand Up @@ -910,8 +911,9 @@ def get_default_modes():
"extra_networks_fetch": OptionInfo(True, "UI fetch network info on mouse-over"),
"extra_network_skip_indexing": OptionInfo(False, "Build info on first access", gr.Checkbox),

"extra_networks_model_sep": OptionInfo("<h2>Models</h2>", "", gr.HTML),
"extra_network_reference": OptionInfo(False, "Use reference values when available", gr.Checkbox),
"extra_networks_model_sep": OptionInfo("<h2>Rerefence models</h2>", "", gr.HTML),
"extra_network_reference_enable": OptionInfo(True, "Enable use of reference models", gr.Checkbox),
"extra_network_reference_values": OptionInfo(False, "Use reference values when available", gr.Checkbox),

"extra_networks_lora_sep": OptionInfo("<h2>LoRA</h2>", "", gr.HTML),
"extra_networks_default_multiplier": OptionInfo(1.0, "Default strength", gr.Slider, {"minimum": 0.0, "maximum": 2.0, "step": 0.01}),
Expand Down Expand Up @@ -1189,7 +1191,7 @@ def cast_value(self, key, value):
opts.data['diffusers_offload_mode'] = 'none'

prompt_styles = modules.styles.StyleDatabase(opts)
reference_models = readfile(os.path.join('html', 'reference.json'))
reference_models = readfile(os.path.join('html', 'reference.json')) if opts.extra_network_reference_enable else {}
cmd_opts.disable_extension_access = (cmd_opts.share or cmd_opts.listen or (cmd_opts.server_name or False)) and not cmd_opts.insecure
devices.args = cmd_opts
devices.opts = opts
Expand Down
2 changes: 1 addition & 1 deletion modules/styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def apply_styles_to_extra(p, style: Style):
'size',
]
reference_style = get_reference_style()
extra = infotext.parse(reference_style) if shared.opts.extra_network_reference else {}
extra = infotext.parse(reference_style) if shared.opts.extra_network_reference_values else {}

style_extra = apply_wildcards_to_prompt(style.extra, [style.wildcards], silent=True)
extra.update(infotext.parse(style_extra))
Expand Down
4 changes: 2 additions & 2 deletions modules/ui_extra_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def create_page(self, tabname, skip = False):
for parentdir, dirs in {d: files_cache.walk(d, cached=True, recurse=files_cache.not_hidden) for d in allowed_folders}.items():
for tgt in dirs:
tgt = tgt.path
if os.path.join(paths.models_path, 'Reference') in tgt:
if os.path.join(paths.models_path, 'Reference') in tgt and shared.opts.extra_network_reference_enable:
subdirs['Reference'] = 1
if shared.native and shared.opts.diffusers_dir in tgt:
subdirs[os.path.basename(shared.opts.diffusers_dir)] = 1
Expand All @@ -242,7 +242,7 @@ def create_page(self, tabname, skip = False):
subdirs[subdir] = 1
debug(f"Networks: page='{self.name}' subfolders={list(subdirs)}")
subdirs = OrderedDict(sorted(subdirs.items()))
if self.name == 'model':
if self.name == 'model' and shared.opts.extra_network_reference_enable:
subdirs['Reference'] = 1
subdirs[os.path.basename(shared.opts.diffusers_dir)] = 1
subdirs.move_to_end(os.path.basename(shared.opts.diffusers_dir))
Expand Down
2 changes: 1 addition & 1 deletion modules/ui_extra_networks_checkpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def refresh(self):
shared.refresh_checkpoints()

def list_reference(self): # pylint: disable=inconsistent-return-statements
if not shared.opts.sd_checkpoint_autodownload:
if not shared.opts.sd_checkpoint_autodownload or not shared.opts.extra_network_reference_enable:
return []
for k, v in shared.reference_models.items():
if not shared.native:
Expand Down
14 changes: 8 additions & 6 deletions modules/upscaler_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@ def __init__(self, dirname=None): # pylint: disable=unused-argument
def do_upscale(self, img: Image, selected_model=None):
if selected_model is None:
return img
if selected_model == "Resize Nearest":
elif selected_model == "Resize Nearest":
return img.resize((int(img.width * self.scale), int(img.height * self.scale)), resample=Image.Resampling.NEAREST)
if selected_model == "Resize Lanczos":
elif selected_model == "Resize Lanczos":
return img.resize((int(img.width * self.scale), int(img.height * self.scale)), resample=Image.Resampling.LANCZOS)
if selected_model == "Resize Bicubic":
elif selected_model == "Resize Bicubic":
return img.resize((int(img.width * self.scale), int(img.height * self.scale)), resample=Image.Resampling.BICUBIC)
if selected_model == "Resize Bilinear":
elif selected_model == "Resize Bilinear":
return img.resize((int(img.width * self.scale), int(img.height * self.scale)), resample=Image.Resampling.BILINEAR)
if selected_model == "Resize Hamming":
elif selected_model == "Resize Hamming":
return img.resize((int(img.width * self.scale), int(img.height * self.scale)), resample=Image.Resampling.HAMMING)
if selected_model == "Resize Box":
elif selected_model == "Resize Box":
return img.resize((int(img.width * self.scale), int(img.height * self.scale)), resample=Image.Resampling.BOX)
else:
return img


def load_model(self, _):
Expand Down

0 comments on commit 06ba03c

Please sign in to comment.