diff --git a/extensions-builtin/Lora/networks.py b/extensions-builtin/Lora/networks.py index b0a34b06d..5518e2527 100644 --- a/extensions-builtin/Lora/networks.py +++ b/extensions-builtin/Lora/networks.py @@ -157,8 +157,10 @@ def load_networks(names, te_multipliers=None, unet_multipliers=None, dyn_dims=No list_available_networks() networks_on_disk = [available_network_aliases.get(name, None) for name in names] failed_to_load_networks = [] + recompile_model = False - if shared.opts.cuda_compile and shared.opts.cuda_compile_backend == "openvino_fx": + if ((shared.opts.cuda_compile and shared.opts.cuda_compile_backend == "openvino_fx") or + shared.opts.nncf_compress_weights or shared.opts.nncf_compress_text_encoder_weights): if len(names) == len(shared.compiled_model_state.lora_model): for i, name in enumerate(names): if shared.compiled_model_state.lora_model[i] != f"{name}:{te_multipliers[i] if te_multipliers else 1.0}": @@ -173,11 +175,19 @@ def load_networks(names, te_multipliers=None, unet_multipliers=None, dyn_dims=No recompile_model = True shared.compiled_model_state.lora_model = [] if recompile_model: + backup_cuda_compile = shared.opts.cuda_compile + backup_nncf_compress_weights = shared.opts.nncf_compress_weights + backup_nncf_compress_text_encoder_weights = shared.opts.nncf_compress_text_encoder_weights shared.compiled_model_state.lora_compile = True sd_models.unload_model_weights(op='model') shared.opts.cuda_compile = False + shared.opts.nncf_compress_weights = False + shared.opts.nncf_compress_text_encoder_weights = False sd_models.reload_model_weights(op='model') - shared.opts.cuda_compile = True + shared.opts.cuda_compile = backup_cuda_compile + shared.opts.nncf_compress_weights = backup_nncf_compress_weights + shared.opts.nncf_compress_text_encoder_weights = backup_nncf_compress_text_encoder_weights + loaded_networks.clear() for i, (network_on_disk, name) in enumerate(zip(networks_on_disk, names)): net = None diff --git a/modules/sd_models_compile.py b/modules/sd_models_compile.py index 9b6d3242e..31508ec9c 100644 --- a/modules/sd_models_compile.py +++ b/modules/sd_models_compile.py @@ -34,12 +34,24 @@ def ipex_optimize(sd_model): sd_model.unet = ipex.optimize(sd_model.unet, dtype=devices.dtype_unet, inplace=True, weights_prepack=False) # pylint: disable=attribute-defined-outside-init else: shared.log.warning('IPEX Optimize enabled but model has no Unet') - if hasattr(sd_model, 'vae'): - sd_model.vae.training = False - sd_model.vae = ipex.optimize(sd_model.vae, dtype=devices.dtype_vae, inplace=True, weights_prepack=False) # pylint: disable=attribute-defined-outside-init - if hasattr(sd_model, 'movq'): - sd_model.movq.training = False - sd_model.movq = ipex.optimize(sd_model.movq, dtype=devices.dtype_vae, inplace=True, weights_prepack=False) # pylint: disable=attribute-defined-outside-init + if shared.opts.ipex_optimize_vae: + if hasattr(sd_model, 'vae'): + sd_model.vae.training = False + sd_model.vae = ipex.optimize(sd_model.vae, dtype=devices.dtype_vae, inplace=True, weights_prepack=False) # pylint: disable=attribute-defined-outside-init + elif hasattr(sd_model, 'movq'): + sd_model.movq.training = False + sd_model.movq = ipex.optimize(sd_model.movq, dtype=devices.dtype_vae, inplace=True, weights_prepack=False) # pylint: disable=attribute-defined-outside-init + else: + shared.log.warning('Compress VAE Weights enabled but model has no VAE') + if shared.opts.ipex_optimize_text_encoder: + if hasattr(sd_model, 'text_encoder'): + sd_model.text_encoder.training = False + sd_model.text_encoder = ipex.optimize(sd_model.text_encoder, dtype=devices.dtype_unet, inplace=True, weights_prepack=False) # pylint: disable=attribute-defined-outside-init + if hasattr(sd_model, 'text_encoder_2'): + sd_model.text_encoder_2.training = False + sd_model.text_encoder_2 = ipex.optimize(sd_model.text_encoder_2, dtype=devices.dtype_unet, inplace=True, weights_prepack=False) # pylint: disable=attribute-defined-outside-init + else: + shared.log.warning('IPEX Optimize Text Encoder Weights enabled but model has no Text Encoder') t1 = time.time() shared.log.info(f"IPEX Optimize: time={t1-t0:.2f}") return sd_model @@ -50,15 +62,36 @@ def nncf_compress_weights(sd_model): try: t0 = time.time() import nncf - if hasattr(sd_model, 'unet'): - sd_model.unet = nncf.compress_weights(sd_model.unet) + if shared.compiled_model_state is None: + shared.compiled_model_state = CompiledModelState() else: - shared.log.warning('Compress Weights enabled but model has no Unet') + shared.compiled_model_state.compiled_cache.clear() + shared.compiled_model_state.partitioned_modules.clear() + backup_lora_model = [] + if shared.compiled_model_state.lora_compile: + backup_lora_model = shared.compiled_model_state.lora_model + shared.compiled_model_state = CompiledModelState() + shared.compiled_model_state.lora_model = backup_lora_model + + if shared.opts.nncf_compress_weights: + if hasattr(sd_model, 'unet'): + sd_model.unet = nncf.compress_weights(sd_model.unet) + else: + shared.log.warning('Compress Weights enabled but model has no Unet') if shared.opts.nncf_compress_vae_weights: if hasattr(sd_model, 'vae'): sd_model.vae = nncf.compress_weights(sd_model.vae) - if hasattr(sd_model, 'movq'): + elif hasattr(sd_model, 'movq'): sd_model.movq = nncf.compress_weights(sd_model.movq) + else: + shared.log.warning('Compress VAE Weights enabled but model has no VAE') + if shared.opts.nncf_compress_text_encoder_weights: + if hasattr(sd_model, 'text_encoder'): + sd_model.text_encoder = nncf.compress_weights(sd_model.text_encoder) + if hasattr(sd_model, 'text_encoder_2'): + sd_model.text_encoder_2 = nncf.compress_weights(sd_model.text_encoder_2) + else: + shared.log.warning('Compress VAE Text Encoder Weights enabled but model has no Text Encoder') t1 = time.time() shared.log.info(f"Compress Weights: time={t1-t0:.2f}") return sd_model @@ -181,7 +214,7 @@ def compile_torch(sd_model): def compile_diffusers(sd_model): if shared.opts.ipex_optimize: sd_model = ipex_optimize(sd_model) - if shared.opts.nncf_compress_weights and not (shared.opts.cuda_compile and shared.opts.cuda_compile_backend == "openvino_fx"): + if not (shared.opts.cuda_compile and shared.opts.cuda_compile_backend == "openvino_fx"): sd_model = nncf_compress_weights(sd_model) if not (shared.opts.cuda_compile or shared.opts.cuda_compile_vae or shared.opts.cuda_compile_upscaler): return sd_model diff --git a/modules/shared.py b/modules/shared.py index f5f721f41..762f729f5 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -345,14 +345,23 @@ options_templates.update(options_section(('cuda', "Compute Settings"), { "cuda_compile_verbose": OptionInfo(False, "Model compile verbose mode"), "cuda_compile_errors": OptionInfo(True, "Model compile suppress errors"), "diffusers_quantization": OptionInfo(False, "Enable dynamic quantization with torchao"), + + "nncf_sep": OptionInfo("

NNCF

", "", gr.HTML), "nncf_compress_weights": OptionInfo(False, "Compress Model weights with NNCF"), "nncf_compress_vae_weights": OptionInfo(False, "Compress VAE weights with NNCF"), + "nncf_compress_text_encoder_weights": OptionInfo(False, "Compress Text Encoder weights with NNCF"), - "ipex_sep": OptionInfo("

IPEX, DirectML and OpenVINO

", "", gr.HTML), - "ipex_optimize": OptionInfo(False if not devices.backend == "ipex" else True, "Enable IPEX Optimize for Intel GPUs"), - "ipex_optimize_upscaler": OptionInfo(False if not devices.backend == "ipex" else True, "Enable IPEX Optimize for Intel GPUs with Upscalers"), + "directml_sep": OptionInfo("

DirectML

", "", gr.HTML), "directml_memory_provider": OptionInfo(default_memory_provider, 'DirectML memory stats provider', gr.Radio, {"choices": memory_providers}), "directml_catch_nan": OptionInfo(False, "DirectML retry specific operation when NaN is produced if possible. (makes generation slower)"), + + "ipex_sep": OptionInfo("

IPEX

", "", gr.HTML), + "ipex_optimize": OptionInfo(False if not devices.backend == "ipex" else True, "Enable IPEX Optimize for Intel GPUs with UNet"), + "ipex_optimize_vae": OptionInfo(False if not devices.backend == "ipex" else True, "Enable IPEX Optimize for Intel GPUs with VAE"), + "ipex_optimize_text_encoder": OptionInfo(False if not devices.backend == "ipex" else True, "Enable IPEX Optimize for Intel GPUs with Text Encoder"), + "ipex_optimize_upscaler": OptionInfo(False if not devices.backend == "ipex" else True, "Enable IPEX Optimize for Intel GPUs with Upscalers"), + + "openvino_sep": OptionInfo("

OpenVINO

", "", gr.HTML), "openvino_disable_model_caching": OptionInfo(False, "OpenVINO disable model caching"), "openvino_hetero_gpu": OptionInfo(False, "OpenVINO use Hetero Device for single inference with multiple devices"), "openvino_remove_cpu_from_hetero": OptionInfo(False, "OpenVINO remove CPU from Hetero Device"),