From 8bfc199d1fdad7fa252296141b6b39b305da5554 Mon Sep 17 00:00:00 2001 From: kijai <40791699+kijai@users.noreply.github.com> Date: Sun, 25 Aug 2024 15:24:15 +0300 Subject: [PATCH] Remove backwards compatibility attempt on patching possible causing issues, expect ComfyUI to be on latest version now --- nodes.py | 18 ++++----------- pyproject.toml | 2 +- utils/patches.py | 60 +----------------------------------------------- 3 files changed, 7 insertions(+), 73 deletions(-) diff --git a/nodes.py b/nodes.py index 751b8e2..ffdf814 100644 --- a/nodes.py +++ b/nodes.py @@ -7,7 +7,7 @@ import numpy as np import torch.nn.functional as F from comfy.utils import load_torch_file from .utils.convert_unet import convert_iclight_unet -from .utils.patches import calculate_weight_adjust_channel_old, calculate_weight_adjust_channel_new +from .utils.patches import calculate_weight_adjust_channel from .utils.image import generate_gradient_image, LightPosition from nodes import MAX_RESOLUTION from comfy.model_patcher import ModelPatcher @@ -69,18 +69,10 @@ Used with ICLightConditioning -node print("LoadAndApplyICLightUnet: Added LoadICLightUnet patches") #Patch ComfyUI's LoRA weight application to accept multi-channel inputs. Thanks @huchenlei - - if hasattr(ModelPatcher, 'calculate_weight'): - try: - ModelPatcher.calculate_weight = calculate_weight_adjust_channel_old(ModelPatcher.calculate_weight) - except: - raise Exception("IC-Light: Could not patch calculate_weight") - # the function was moved to lora module in commit https://github.com/comfyanonymous/ComfyUI/commit/c26ca272076262c8b21a8f2e094cf538d88b9e46 - else: - try: - lora.calculate_weight = calculate_weight_adjust_channel_new(lora.calculate_weight) - except: - raise Exception("IC-Light: Could not patch calculate_weight") + try: + lora.calculate_weight = calculate_weight_adjust_channel(lora.calculate_weight) + except: + raise Exception("IC-Light: Could not patch calculate_weight") # Mimic the existing IP2P class to enable extra_conds def bound_extra_conds(self, **kwargs): diff --git a/pyproject.toml b/pyproject.toml index 519f5f8..a48776f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "comfyui-ic-light" description = "ComfyUI native nodes for IC-Light" -version = "1.0.0" +version = "1.0.1" license = { text = "Apache License 2.0" } dependencies = ["opencv-python"] diff --git a/utils/patches.py b/utils/patches.py index 9be8a62..0c6dd97 100644 --- a/utils/patches.py +++ b/utils/patches.py @@ -7,65 +7,7 @@ import functools from comfy.model_patcher import ModelPatcher import comfy.model_management -def calculate_weight_adjust_channel_old(func): - """Patches ComfyUI's LoRA weight application to accept multi-channel inputs.""" - - @functools.wraps(func) - def calculate_weight( - self: ModelPatcher, patches, weight: torch.Tensor, key: str - ) -> torch.Tensor: - weight = func(self, patches, weight, key) - - for p in patches: - alpha = p[0] - v = p[1] - - # The recursion call should be handled in the main func call. - if isinstance(v, list): - continue - - if len(v) == 1: - patch_type = "diff" - elif len(v) == 2: - patch_type = v[0] - v = v[1] - - if patch_type == "diff": - w1 = v[0] - if all( - ( - alpha != 0.0, - w1.shape != weight.shape, - w1.ndim == weight.ndim == 4, - ) - ): - new_shape = [max(n, m) for n, m in zip(weight.shape, w1.shape)] - print( - f"IC-Light: Merged with {key} channel changed from {weight.shape} to {new_shape}" - ) - new_diff = alpha * comfy.model_management.cast_to_device( - w1, weight.device, weight.dtype - ) - new_weight = torch.zeros(size=new_shape).to(weight) - new_weight[ - : weight.shape[0], - : weight.shape[1], - : weight.shape[2], - : weight.shape[3], - ] = weight - new_weight[ - : new_diff.shape[0], - : new_diff.shape[1], - : new_diff.shape[2], - : new_diff.shape[3], - ] += new_diff - new_weight = new_weight.contiguous().clone() - weight = new_weight - return weight - - return calculate_weight - -def calculate_weight_adjust_channel_new(func): +def calculate_weight_adjust_channel(func): """Patches ComfyUI's LoRA weight application to accept multi-channel inputs.""" @functools.wraps(func)