From 2c1d2375d6bc62b6095e0b8c1b31df1112ec2ea2 Mon Sep 17 00:00:00 2001 From: comfyanonymous Date: Fri, 23 Aug 2024 04:04:55 -0400 Subject: [PATCH 1/3] Fix. --- comfy/model_management.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/comfy/model_management.py b/comfy/model_management.py index c86b67e9f..fd3979020 100644 --- a/comfy/model_management.py +++ b/comfy/model_management.py @@ -44,11 +44,15 @@ cpu_state = CPUState.GPU total_vram = 0 -torch_version = torch.version.__version__ +xpu_available = False +try: + torch_version = torch.version.__version__ + xpu_available = int(torch_version[0]) < 2 or (int(torch_version[0]) == 2 and int(torch_version[2]) <= 4) + xpu_available = xpu_available and torch.xpu.is_available() +except: + pass lowvram_available = True -xpu_available = int(torch_version[0]) < 2 or (int(torch_version[0]) == 2 and int(torch_version[2]) <= 4) - if args.deterministic: logging.info("Using deterministic algorithms for pytorch") torch.use_deterministic_algorithms(True, warn_only=True) From 5d8bbb72816c263df1cff53c3c4a7835eeecc636 Mon Sep 17 00:00:00 2001 From: comfyanonymous Date: Fri, 23 Aug 2024 04:06:27 -0400 Subject: [PATCH 2/3] Cleanup. --- comfy/model_management.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/comfy/model_management.py b/comfy/model_management.py index fd3979020..91e692ba2 100644 --- a/comfy/model_management.py +++ b/comfy/model_management.py @@ -47,8 +47,7 @@ total_vram = 0 xpu_available = False try: torch_version = torch.version.__version__ - xpu_available = int(torch_version[0]) < 2 or (int(torch_version[0]) == 2 and int(torch_version[2]) <= 4) - xpu_available = xpu_available and torch.xpu.is_available() + xpu_available = (int(torch_version[0]) < 2 or (int(torch_version[0]) == 2 and int(torch_version[2]) <= 4)) and torch.xpu.is_available() except: pass From 7df42b9a2364bae6822fbd9e9fa10cea2e319ba3 Mon Sep 17 00:00:00 2001 From: comfyanonymous Date: Fri, 23 Aug 2024 04:58:59 -0400 Subject: [PATCH 3/3] Fix dora. --- comfy/lora.py | 20 ++++++++++++++++++++ comfy/model_patcher.py | 21 --------------------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/comfy/lora.py b/comfy/lora.py index 9d8a7908a..a3e7d9cc0 100644 --- a/comfy/lora.py +++ b/comfy/lora.py @@ -327,6 +327,26 @@ def model_lora_keys_unet(model, key_map={}): return key_map +def weight_decompose(dora_scale, weight, lora_diff, alpha, strength, intermediate_dtype): + dora_scale = comfy.model_management.cast_to_device(dora_scale, weight.device, intermediate_dtype) + lora_diff *= alpha + weight_calc = weight + lora_diff.type(weight.dtype) + weight_norm = ( + weight_calc.transpose(0, 1) + .reshape(weight_calc.shape[1], -1) + .norm(dim=1, keepdim=True) + .reshape(weight_calc.shape[1], *[1] * (weight_calc.dim() - 1)) + .transpose(0, 1) + ) + + weight_calc *= (dora_scale / weight_norm).type(weight.dtype) + if strength != 1.0: + weight_calc -= weight + weight += strength * (weight_calc) + else: + weight[:] = weight_calc + return weight + def calculate_weight(patches, weight, key, intermediate_dtype=torch.float32): for p in patches: strength = p[0] diff --git a/comfy/model_patcher.py b/comfy/model_patcher.py index 1d83ba7c2..1f8100698 100644 --- a/comfy/model_patcher.py +++ b/comfy/model_patcher.py @@ -31,27 +31,6 @@ import comfy.lora from comfy.types import UnetWrapperFunction -def weight_decompose(dora_scale, weight, lora_diff, alpha, strength, intermediate_dtype): - dora_scale = comfy.model_management.cast_to_device(dora_scale, weight.device, intermediate_dtype) - lora_diff *= alpha - weight_calc = weight + lora_diff.type(weight.dtype) - weight_norm = ( - weight_calc.transpose(0, 1) - .reshape(weight_calc.shape[1], -1) - .norm(dim=1, keepdim=True) - .reshape(weight_calc.shape[1], *[1] * (weight_calc.dim() - 1)) - .transpose(0, 1) - ) - - weight_calc *= (dora_scale / weight_norm).type(weight.dtype) - if strength != 1.0: - weight_calc -= weight - weight += strength * (weight_calc) - else: - weight[:] = weight_calc - return weight - - def set_model_options_patch_replace(model_options, patch, name, block_name, number, transformer_index=None): to = model_options["transformer_options"].copy()