mirror of
https://github.com/vladmandic/automatic
synced 2026-09-19 09:14:35 +02:00
Unify compile Upscaler compile command
This commit is contained in:
@@ -27,12 +27,12 @@ class CompiledModelState:
|
||||
deepcache_worker = None
|
||||
|
||||
|
||||
def ipex_optimize(sd_model):
|
||||
def ipex_optimize(sd_model, apply_to_components=True, op="Model"):
|
||||
try:
|
||||
t0 = time.time()
|
||||
import intel_extension_for_pytorch as ipex # pylint: disable=import-error, unused-import
|
||||
|
||||
def ipex_optimize_model(model, op=None, sd_model=None): # pylint: disable=unused-argument
|
||||
import intel_extension_for_pytorch as ipex # pylint: disable=import-error, unused-import
|
||||
model.eval()
|
||||
model.training = False
|
||||
if model.device.type != "meta":
|
||||
@@ -51,27 +51,31 @@ def ipex_optimize(sd_model):
|
||||
devices.torch_gc()
|
||||
return model
|
||||
|
||||
sd_model = sd_models.apply_function_to_model(sd_model, ipex_optimize_model, shared.opts.ipex_optimize, op="ipex")
|
||||
if apply_to_components:
|
||||
sd_model = sd_models.apply_function_to_model(sd_model, ipex_optimize_model, shared.opts.ipex_optimize, op="ipex")
|
||||
else:
|
||||
sd_model = ipex_optimize_model(sd_model, op=op)
|
||||
|
||||
t1 = time.time()
|
||||
shared.log.info(f"IPEX Optimize: time={t1-t0:.2f}")
|
||||
shared.log.info(f"{op} IPEX Optimize: time={t1-t0:.2f}")
|
||||
except Exception as e:
|
||||
shared.log.warning(f"IPEX Optimize: error: {e}")
|
||||
shared.log.warning(f"{op} IPEX Optimize: error: {e}")
|
||||
return sd_model
|
||||
|
||||
|
||||
def optimize_openvino(sd_model):
|
||||
def optimize_openvino(sd_model, clear_cache=True):
|
||||
try:
|
||||
from modules.intel.openvino import openvino_fx # pylint: disable=unused-import
|
||||
if shared.compiled_model_state is not None:
|
||||
if clear_cache and shared.compiled_model_state is not None:
|
||||
shared.compiled_model_state.compiled_cache.clear()
|
||||
shared.compiled_model_state.req_cache.clear()
|
||||
shared.compiled_model_state.partitioned_modules.clear()
|
||||
shared.compiled_model_state = CompiledModelState()
|
||||
shared.compiled_model_state.is_compiled = True
|
||||
shared.compiled_model_state.first_pass = 'precompile' not in shared.opts.cuda_compile_options
|
||||
shared.compiled_model_state.first_pass_vae = 'precompile' not in shared.opts.cuda_compile_options
|
||||
shared.compiled_model_state.first_pass_refiner = 'precompile' not in shared.opts.cuda_compile_options
|
||||
if clear_cache or shared.compiled_model_state is None:
|
||||
shared.compiled_model_state = CompiledModelState()
|
||||
shared.compiled_model_state.is_compiled = True
|
||||
shared.compiled_model_state.first_pass = 'precompile' not in shared.opts.cuda_compile_options
|
||||
shared.compiled_model_state.first_pass_vae = 'precompile' not in shared.opts.cuda_compile_options
|
||||
shared.compiled_model_state.first_pass_refiner = 'precompile' not in shared.opts.cuda_compile_options
|
||||
sd_models.set_accelerate(sd_model)
|
||||
except Exception as e:
|
||||
shared.log.warning(f"Model compile: task=OpenVINO: {e}")
|
||||
@@ -152,12 +156,12 @@ def compile_stablefast(sd_model):
|
||||
return sd_model
|
||||
|
||||
|
||||
def compile_torch(sd_model):
|
||||
def compile_torch(sd_model, apply_to_components=True, op="Model"):
|
||||
try:
|
||||
t0 = time.time()
|
||||
import torch._dynamo # pylint: disable=unused-import,redefined-outer-name
|
||||
torch._dynamo.reset() # pylint: disable=protected-access
|
||||
shared.log.debug(f"Model compile: task=torch backends={torch._dynamo.list_backends()}") # pylint: disable=protected-access
|
||||
shared.log.debug(f"{op} compile: task=torch backends={torch._dynamo.list_backends()}") # pylint: disable=protected-access
|
||||
|
||||
def torch_compile_model(model, op=None, sd_model=None): # pylint: disable=unused-argument
|
||||
if hasattr(model, 'compile_repeated_blocks') and 'repeated' in shared.opts.cuda_compile_options:
|
||||
@@ -186,7 +190,7 @@ def compile_torch(sd_model):
|
||||
return model
|
||||
|
||||
if shared.opts.cuda_compile_backend == "openvino_fx":
|
||||
sd_model = optimize_openvino(sd_model)
|
||||
sd_model = optimize_openvino(sd_model, clear_cache=apply_to_components)
|
||||
elif shared.opts.cuda_compile_backend == "olive-ai":
|
||||
if shared.compiled_model_state is None:
|
||||
shared.compiled_model_state = CompiledModelState()
|
||||
@@ -207,21 +211,24 @@ def compile_torch(sd_model):
|
||||
torch._inductor.config.use_mixed_mm = True # pylint: disable=protected-access
|
||||
# torch._inductor.config.force_fuse_int_mm_with_mul = True # pylint: disable=protected-access
|
||||
except Exception as e:
|
||||
shared.log.error(f"Model compile: torch inductor config error: {e}")
|
||||
shared.log.error(f"{op} compile: torch inductor config error: {e}")
|
||||
|
||||
sd_model = sd_models.apply_function_to_model(sd_model, function=torch_compile_model, options=shared.opts.cuda_compile, op="compile")
|
||||
if apply_to_components:
|
||||
sd_model = sd_models.apply_function_to_model(sd_model, function=torch_compile_model, options=shared.opts.cuda_compile, op="compile")
|
||||
else:
|
||||
sd_model = torch_compile_model(sd_model)
|
||||
|
||||
setup_logging() # compile messes with logging so reset is needed
|
||||
if 'precompile' in shared.opts.cuda_compile_options:
|
||||
if apply_to_components and 'precompile' in shared.opts.cuda_compile_options:
|
||||
try:
|
||||
shared.log.debug("Model compile: task=torch precompile")
|
||||
shared.log.debug(f"{op} compile: task=torch precompile")
|
||||
sd_model("dummy prompt")
|
||||
except Exception:
|
||||
pass
|
||||
t1 = time.time()
|
||||
shared.log.info(f"Model compile: task=torch time={t1-t0:.2f}")
|
||||
shared.log.info(f"{op} compile: task=torch time={t1-t0:.2f}")
|
||||
except Exception as e:
|
||||
shared.log.warning(f"Model compile: task=torch {e}")
|
||||
shared.log.warning(f"{op} compile: task=torch {e}")
|
||||
errors.display(e, 'Compile')
|
||||
return sd_model
|
||||
|
||||
@@ -254,13 +261,11 @@ def compile_deepcache(sd_model):
|
||||
return sd_model
|
||||
|
||||
|
||||
def compile_diffusers(sd_model):
|
||||
if 'Model' not in shared.opts.cuda_compile:
|
||||
return sd_model
|
||||
def compile_diffusers(sd_model, apply_to_components=True, op="Model"):
|
||||
if shared.opts.cuda_compile_backend == 'none':
|
||||
shared.log.warning('Model compile enabled but no backend specified')
|
||||
shared.log.warning(f'{op} compile enabled but no backend specified')
|
||||
return sd_model
|
||||
shared.log.info(f"Model compile: pipeline={sd_model.__class__.__name__} mode={shared.opts.cuda_compile_mode} backend={shared.opts.cuda_compile_backend} options={shared.opts.cuda_compile_options} compile={shared.opts.cuda_compile}")
|
||||
shared.log.info(f"{op} compile: pipeline={sd_model.__class__.__name__} mode={shared.opts.cuda_compile_mode} backend={shared.opts.cuda_compile_backend} options={shared.opts.cuda_compile_options} compile={shared.opts.cuda_compile}")
|
||||
if shared.opts.cuda_compile_backend == 'onediff':
|
||||
sd_model = compile_onediff(sd_model)
|
||||
elif shared.opts.cuda_compile_backend == 'stable-fast':
|
||||
@@ -269,7 +274,7 @@ def compile_diffusers(sd_model):
|
||||
sd_model = compile_deepcache(sd_model)
|
||||
else:
|
||||
check_deepcache(False)
|
||||
sd_model = compile_torch(sd_model)
|
||||
sd_model = compile_torch(sd_model, apply_to_components=apply_to_components, op=op)
|
||||
return sd_model
|
||||
|
||||
|
||||
|
||||
+12
-54
@@ -164,59 +164,17 @@ class UpscalerData:
|
||||
|
||||
|
||||
def compile_upscaler(model):
|
||||
try:
|
||||
if shared.opts.ipex_optimize and "Upscaler" in shared.opts.ipex_optimize:
|
||||
t0 = time.time()
|
||||
import intel_extension_for_pytorch as ipex # pylint: disable=import-error, unused-import
|
||||
model.eval()
|
||||
model.training = False
|
||||
model = ipex.optimize(model, dtype=devices.dtype, inplace=True, weights_prepack=False) # pylint: disable=attribute-defined-outside-init
|
||||
t1 = time.time()
|
||||
shared.log.info(f"Upscaler IPEX Optimize: time={t1-t0:.2f}")
|
||||
except Exception as e:
|
||||
shared.log.warning(f"Upscaler IPEX Optimize: error: {e}")
|
||||
if "Upscaler" in shared.opts.ipex_optimize:
|
||||
try:
|
||||
from modules.sd_models_compile import ipex_optimize
|
||||
model = ipex_optimize(model, apply_to_components=False, op="Upscaler")
|
||||
except Exception as e:
|
||||
shared.log.warning(f"Upscaler IPEX Optimize: error: {e}")
|
||||
|
||||
try:
|
||||
if "Upscaler" in shared.opts.cuda_compile and shared.opts.cuda_compile_backend != 'none':
|
||||
import torch._dynamo # pylint: disable=unused-import,redefined-outer-name
|
||||
if shared.opts.cuda_compile_backend not in torch._dynamo.list_backends(): # pylint: disable=protected-access
|
||||
shared.log.warning(f"Upscaler compile not available: backend={shared.opts.cuda_compile_backend} available={torch._dynamo.list_backends()}") # pylint: disable=protected-access
|
||||
return model
|
||||
else:
|
||||
shared.log.info(f"Upscaler compile: backend={shared.opts.cuda_compile_backend} available={torch._dynamo.list_backends()}") # pylint: disable=protected-access
|
||||
|
||||
if shared.opts.cuda_compile_backend == "openvino_fx":
|
||||
from modules.intel.openvino import openvino_fx # pylint: disable=unused-import
|
||||
if shared.compiled_model_state is None:
|
||||
from modules.sd_models_compile import CompiledModelState
|
||||
shared.compiled_model_state = CompiledModelState()
|
||||
|
||||
log_level = logging.WARNING if 'verbose' in shared.opts.cuda_compile_options else logging.CRITICAL # pylint: disable=protected-access
|
||||
if hasattr(torch, '_logging'):
|
||||
torch._logging.set_logs(dynamo=log_level, aot=log_level, inductor=log_level) # pylint: disable=protected-access
|
||||
torch._dynamo.config.verbose = 'verbose' in shared.opts.cuda_compile_options # pylint: disable=protected-access
|
||||
torch._dynamo.config.suppress_errors = 'verbose' not in shared.opts.cuda_compile_options # pylint: disable=protected-access
|
||||
|
||||
try:
|
||||
torch._inductor.config.conv_1x1_as_mm = True # pylint: disable=protected-access
|
||||
torch._inductor.config.coordinate_descent_tuning = True # pylint: disable=protected-access
|
||||
torch._inductor.config.epilogue_fusion = False # pylint: disable=protected-access
|
||||
torch._inductor.config.coordinate_descent_check_all_directions = True # pylint: disable=protected-access
|
||||
torch._inductor.config.use_mixed_mm = True # pylint: disable=protected-access
|
||||
# torch._inductor.config.force_fuse_int_mm_with_mul = True # pylint: disable=protected-access
|
||||
except Exception as e:
|
||||
shared.log.error(f"Torch inductor config error: {e}")
|
||||
|
||||
t0 = time.time()
|
||||
model = torch.compile(model,
|
||||
mode=shared.opts.cuda_compile_mode,
|
||||
backend=shared.opts.cuda_compile_backend,
|
||||
fullgraph='fullgraph' in shared.opts.cuda_compile_options,
|
||||
dynamic='dynamic' in shared.opts.cuda_compile_options,
|
||||
) # pylint: disable=attribute-defined-outside-init
|
||||
setup_logging() # compile messes with logging so reset is needed
|
||||
t1 = time.time()
|
||||
shared.log.info(f"Upscaler compile: time={t1-t0:.2f}")
|
||||
except Exception as e:
|
||||
shared.log.warning(f"Upscaler compile error: {e}")
|
||||
if "Upscaler" in shared.opts.cuda_compile:
|
||||
try:
|
||||
from modules.sd_models_compile import compile_torch
|
||||
model = compile_torch(model, apply_to_components=False, op="Upscaler")
|
||||
except Exception as e:
|
||||
shared.log.warning(f"Upscaler compile error: {e}")
|
||||
return model
|
||||
|
||||
Reference in New Issue
Block a user