mirror of
https://github.com/vladmandic/automatic
synced 2026-09-18 08:44:33 +02:00
OpenVINO fix cache and Lora loading
This commit is contained in:
@@ -177,15 +177,12 @@ def load_networks(names, te_multipliers=None, unet_multipliers=None, dyn_dims=No
|
||||
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
|
||||
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
|
||||
shared.opts.nncf_compress_weights = []
|
||||
sd_models.reload_model_weights(op='model')
|
||||
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)):
|
||||
|
||||
@@ -135,7 +135,7 @@ def cached_model_name(model_hash_str, device, args, cache_root, reversed = False
|
||||
else:
|
||||
inputs_str += "_" + str(input_data.type()) + str(input_data.size())[11:-1].replace(" ", "")
|
||||
inputs_str = sha256(inputs_str.encode('utf-8')).hexdigest()
|
||||
file_name += inputs_str
|
||||
file_name += "_" + inputs_str
|
||||
|
||||
return file_name
|
||||
|
||||
@@ -436,14 +436,7 @@ def openvino_fx(subgraph, example_inputs):
|
||||
# Create a hash to be used for caching
|
||||
subgraph.apply(generate_subgraph_str)
|
||||
shared.compiled_model_state.model_hash_str = shared.compiled_model_state.model_hash_str + sha256(subgraph.code.encode('utf-8')).hexdigest()
|
||||
model_hash_str = sha256(shared.compiled_model_state.model_hash_str.encode('utf-8')).hexdigest()
|
||||
shared.compiled_model_state.model_hash_str = ""
|
||||
|
||||
if (shared.compiled_model_state.cn_model != [] and shared.compiled_model_state.partition_id == 0):
|
||||
shared.compiled_model_state.shared.compiled_model_state.model_hash_str = model_hash_str + str(shared.compiled_model_state.cn_model)
|
||||
|
||||
if (shared.compiled_model_state.lora_model != []):
|
||||
shared.compiled_model_state.model_hash_str = shared.compiled_model_state.model_hash_str + str(shared.compiled_model_state.lora_model)
|
||||
shared.compiled_model_state.model_hash_str = sha256(shared.compiled_model_state.model_hash_str.encode('utf-8')).hexdigest()
|
||||
|
||||
executor_parameters = {"model_hash_str": shared.compiled_model_state.model_hash_str}
|
||||
# Check if the model was fully supported and already cached
|
||||
|
||||
@@ -282,7 +282,7 @@ def process_diffusers(p: processing.StableDiffusionProcessing):
|
||||
debug(f'Diffusers pipeline args: {args}')
|
||||
return args
|
||||
|
||||
def recompile_model(hires=False):
|
||||
def recompile_model(hires=False): # recompile if a parameter changes
|
||||
if 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
|
||||
@@ -377,7 +377,7 @@ def process_diffusers(p: processing.StableDiffusionProcessing):
|
||||
if shared.opts.diffusers_move_base:
|
||||
sd_models.move_model(shared.sd_model, devices.device)
|
||||
|
||||
# recompile if a paramater chages
|
||||
# recompile if a parameter changes
|
||||
recompile_model()
|
||||
|
||||
# pipeline type is set earlier in processing, but check for sanity
|
||||
|
||||
@@ -1340,10 +1340,13 @@ def reload_model_weights(sd_model=None, info=None, reuse_dict=False, op='model')
|
||||
|
||||
|
||||
def convert_to_faketensors(tensor):
|
||||
fake_module = torch._subclasses.fake_tensor.FakeTensorMode(allow_non_fake_inputs=True) # pylint: disable=protected-access
|
||||
if hasattr(tensor, "weight"):
|
||||
tensor.weight = torch.nn.Parameter(fake_module.from_tensor(tensor.weight))
|
||||
return tensor
|
||||
try:
|
||||
fake_module = torch._subclasses.fake_tensor.FakeTensorMode(allow_non_fake_inputs=True) # pylint: disable=protected-access
|
||||
if hasattr(tensor, "weight"):
|
||||
tensor.weight = torch.nn.Parameter(fake_module.from_tensor(tensor.weight))
|
||||
return tensor
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def disable_offload(sd_model):
|
||||
@@ -1360,7 +1363,6 @@ def unload_model_weights(op='model'):
|
||||
if shared.compiled_model_state is not None:
|
||||
shared.compiled_model_state.compiled_cache.clear()
|
||||
shared.compiled_model_state.partitioned_modules.clear()
|
||||
shared.compiled_model_state = None
|
||||
if op == 'model' or op == 'dict':
|
||||
if model_data.sd_model:
|
||||
if shared.backend == shared.Backend.ORIGINAL:
|
||||
|
||||
@@ -97,7 +97,7 @@ def nncf_compress_weights(sd_model):
|
||||
shared.log.warning(f"Compress Weights: error: {e}")
|
||||
|
||||
|
||||
def optimize_openvino():
|
||||
def optimize_openvino(sd_model):
|
||||
try:
|
||||
from modules.intel.openvino import openvino_fx # pylint: disable=unused-import
|
||||
torch._dynamo.eval_frame.check_if_dynamo_supported = lambda: True # pylint: disable=protected-access
|
||||
@@ -109,8 +109,10 @@ def optimize_openvino():
|
||||
shared.compiled_model_state.first_pass = True if not shared.opts.cuda_compile_precompile else False
|
||||
shared.compiled_model_state.first_pass_vae = True if not shared.opts.cuda_compile_precompile else False
|
||||
shared.compiled_model_state.first_pass_refiner = True if not shared.opts.cuda_compile_precompile else False
|
||||
sd_model.has_accelerate = True
|
||||
except Exception as e:
|
||||
shared.log.warning(f"Model compile: task=OpenVINO: {e}")
|
||||
return sd_model
|
||||
|
||||
|
||||
def compile_stablefast(sd_model):
|
||||
@@ -157,7 +159,7 @@ def compile_torch(sd_model):
|
||||
torch._dynamo.reset() # pylint: disable=protected-access
|
||||
shared.log.debug(f"Model compile available backends: {torch._dynamo.list_backends()}") # pylint: disable=protected-access
|
||||
if shared.opts.cuda_compile_backend == "openvino_fx":
|
||||
optimize_openvino()
|
||||
sd_model = optimize_openvino(sd_model)
|
||||
elif shared.opts.cuda_compile_backend == "olive-ai":
|
||||
if shared.compiled_model_state is None:
|
||||
shared.compiled_model_state = CompiledModelState()
|
||||
|
||||
+1
-1
@@ -707,7 +707,7 @@ options_templates.update(options_section(('extra_networks', "Extra Networks"), {
|
||||
"lora_add_hashes_to_infotext": OptionInfo(True, "LoRA add hash info"),
|
||||
"lora_force_diffusers": OptionInfo(False if not cmd_opts.use_openvino else True, "LoRA use alternative loading method"),
|
||||
"lora_fuse_diffusers": OptionInfo(False if not cmd_opts.use_openvino else True, "LoRA use merge when using alternative method"),
|
||||
"lora_in_memory_limit": OptionInfo(1, "LoRA memory cache", gr.Slider, {"minimum": 0, "maximum": 24, "step": 1}),
|
||||
"lora_in_memory_limit": OptionInfo(1 if not cmd_opts.use_openvino else 0, "LoRA memory cache", gr.Slider, {"minimum": 0, "maximum": 24, "step": 1}),
|
||||
"lora_functional": OptionInfo(False, "Use Kohya method for handling multiple LoRA", gr.Checkbox, { "visible": False }),
|
||||
"sd_hypernetwork": OptionInfo("None", "Add hypernetwork to prompt", gr.Dropdown, { "choices": ["None"], "visible": False }),
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user