Spaces:
Running
on
L4
Running
on
L4
Update MagicQuill/brushnet_nodes.py
Browse files- MagicQuill/brushnet_nodes.py +39 -21
MagicQuill/brushnet_nodes.py
CHANGED
|
@@ -149,7 +149,13 @@ class PowerPaintCLIPLoader:
|
|
| 149 |
|
| 150 |
print('PowerPaint base CLIP file: ', base_CLIP_file)
|
| 151 |
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
pp_text_encoder = pp_clip.patcher.model.clip_l.transformer
|
| 154 |
|
| 155 |
add_tokens(
|
|
@@ -164,7 +170,8 @@ class PowerPaintCLIPLoader:
|
|
| 164 |
|
| 165 |
print('PowerPaint CLIP file: ', pp_CLIP_file)
|
| 166 |
|
| 167 |
-
|
|
|
|
| 168 |
pp_clip.patcher.model.clip_l.transformer = pp_text_encoder
|
| 169 |
|
| 170 |
return (pp_clip,)
|
|
@@ -941,6 +948,20 @@ def brushnet_inference(x, timesteps, transformer_options, debug):
|
|
| 941 |
debug=debug,
|
| 942 |
)
|
| 943 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 944 |
|
| 945 |
# This is main patch function
|
| 946 |
def add_brushnet_patch(model, brushnet, torch_dtype, conditioning_latents,
|
|
@@ -952,7 +973,7 @@ def add_brushnet_patch(model, brushnet, torch_dtype, conditioning_latents,
|
|
| 952 |
is_SDXL = isinstance(model.model.model_config, comfy.supported_models.SDXL)
|
| 953 |
|
| 954 |
if is_SDXL:
|
| 955 |
-
input_blocks = [[0, comfy.ops.
|
| 956 |
[1, comfy.ldm.modules.diffusionmodules.openaimodel.ResBlock],
|
| 957 |
[2, comfy.ldm.modules.diffusionmodules.openaimodel.ResBlock],
|
| 958 |
[3, comfy.ldm.modules.diffusionmodules.openaimodel.Downsample],
|
|
@@ -974,7 +995,7 @@ def add_brushnet_patch(model, brushnet, torch_dtype, conditioning_latents,
|
|
| 974 |
[7, comfy.ldm.modules.diffusionmodules.openaimodel.ResBlock],
|
| 975 |
[8, comfy.ldm.modules.diffusionmodules.openaimodel.ResBlock]]
|
| 976 |
else:
|
| 977 |
-
input_blocks = [[0, comfy.ops.
|
| 978 |
[1, comfy.ldm.modules.attention.SpatialTransformer],
|
| 979 |
[2, comfy.ldm.modules.attention.SpatialTransformer],
|
| 980 |
[3, comfy.ldm.modules.diffusionmodules.openaimodel.Downsample],
|
|
@@ -1059,36 +1080,33 @@ def add_brushnet_patch(model, brushnet, torch_dtype, conditioning_latents,
|
|
| 1059 |
bo['latent_id'] = 0
|
| 1060 |
|
| 1061 |
# patch layers `forward` so we can apply brushnet
|
| 1062 |
-
|
| 1063 |
-
h = self.original_forward(x, *args, **kwargs)
|
| 1064 |
-
if hasattr(self, 'add_sample_after') and type(self):
|
| 1065 |
-
to_add = self.add_sample_after
|
| 1066 |
-
if torch.is_tensor(to_add):
|
| 1067 |
-
# interpolate due to RAUNet
|
| 1068 |
-
if h.shape[2] != to_add.shape[2] or h.shape[3] != to_add.shape[3]:
|
| 1069 |
-
to_add = torch.nn.functional.interpolate(to_add, size=(h.shape[2], h.shape[3]), mode='bicubic')
|
| 1070 |
-
h += to_add.to(h.dtype).to(h.device)
|
| 1071 |
-
else:
|
| 1072 |
-
h += self.add_sample_after
|
| 1073 |
-
self.add_sample_after = 0
|
| 1074 |
-
return h
|
| 1075 |
-
|
| 1076 |
for i, block in enumerate(model.model.diffusion_model.input_blocks):
|
| 1077 |
for j, layer in enumerate(block):
|
| 1078 |
if not hasattr(layer, 'original_forward'):
|
| 1079 |
layer.original_forward = layer.forward
|
| 1080 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1081 |
layer.add_sample_after = 0
|
| 1082 |
|
| 1083 |
for j, layer in enumerate(model.model.diffusion_model.middle_block):
|
| 1084 |
if not hasattr(layer, 'original_forward'):
|
| 1085 |
layer.original_forward = layer.forward
|
| 1086 |
-
|
|
|
|
|
|
|
|
|
|
| 1087 |
layer.add_sample_after = 0
|
| 1088 |
|
| 1089 |
for i, block in enumerate(model.model.diffusion_model.output_blocks):
|
| 1090 |
for j, layer in enumerate(block):
|
| 1091 |
if not hasattr(layer, 'original_forward'):
|
| 1092 |
layer.original_forward = layer.forward
|
| 1093 |
-
|
|
|
|
|
|
|
|
|
|
| 1094 |
layer.add_sample_after = 0
|
|
|
|
| 149 |
|
| 150 |
print('PowerPaint base CLIP file: ', base_CLIP_file)
|
| 151 |
|
| 152 |
+
# Reuse TokenizerWrapper if already wrapped
|
| 153 |
+
if isinstance(pp_clip.tokenizer.clip_l.tokenizer, TokenizerWrapper):
|
| 154 |
+
pp_tokenizer = pp_clip.tokenizer.clip_l.tokenizer
|
| 155 |
+
else:
|
| 156 |
+
pp_tokenizer = TokenizerWrapper(pp_clip.tokenizer.clip_l.tokenizer)
|
| 157 |
+
pp_clip.tokenizer.clip_l.tokenizer = pp_tokenizer
|
| 158 |
+
|
| 159 |
pp_text_encoder = pp_clip.patcher.model.clip_l.transformer
|
| 160 |
|
| 161 |
add_tokens(
|
|
|
|
| 170 |
|
| 171 |
print('PowerPaint CLIP file: ', pp_CLIP_file)
|
| 172 |
|
| 173 |
+
# Already assigned above if new, or reused if existing
|
| 174 |
+
# pp_clip.tokenizer.clip_l.tokenizer = pp_tokenizer
|
| 175 |
pp_clip.patcher.model.clip_l.transformer = pp_text_encoder
|
| 176 |
|
| 177 |
return (pp_clip,)
|
|
|
|
| 948 |
debug=debug,
|
| 949 |
)
|
| 950 |
|
| 951 |
+
def forward_patched_by_brushnet(self, x, *args, **kwargs):
|
| 952 |
+
h = self.original_forward(x, *args, **kwargs)
|
| 953 |
+
if hasattr(self, 'add_sample_after') and type(self):
|
| 954 |
+
to_add = self.add_sample_after
|
| 955 |
+
if torch.is_tensor(to_add):
|
| 956 |
+
# interpolate due to RAUNet
|
| 957 |
+
if h.shape[2] != to_add.shape[2] or h.shape[3] != to_add.shape[3]:
|
| 958 |
+
to_add = torch.nn.functional.interpolate(to_add, size=(h.shape[2], h.shape[3]), mode='bicubic')
|
| 959 |
+
h += to_add.to(h.dtype).to(h.device)
|
| 960 |
+
else:
|
| 961 |
+
h += self.add_sample_after
|
| 962 |
+
self.add_sample_after = 0
|
| 963 |
+
return h
|
| 964 |
+
|
| 965 |
|
| 966 |
# This is main patch function
|
| 967 |
def add_brushnet_patch(model, brushnet, torch_dtype, conditioning_latents,
|
|
|
|
| 973 |
is_SDXL = isinstance(model.model.model_config, comfy.supported_models.SDXL)
|
| 974 |
|
| 975 |
if is_SDXL:
|
| 976 |
+
input_blocks = [[0, comfy.ops.manual_cast.Conv2d],
|
| 977 |
[1, comfy.ldm.modules.diffusionmodules.openaimodel.ResBlock],
|
| 978 |
[2, comfy.ldm.modules.diffusionmodules.openaimodel.ResBlock],
|
| 979 |
[3, comfy.ldm.modules.diffusionmodules.openaimodel.Downsample],
|
|
|
|
| 995 |
[7, comfy.ldm.modules.diffusionmodules.openaimodel.ResBlock],
|
| 996 |
[8, comfy.ldm.modules.diffusionmodules.openaimodel.ResBlock]]
|
| 997 |
else:
|
| 998 |
+
input_blocks = [[0, comfy.ops.manual_cast.Conv2d],
|
| 999 |
[1, comfy.ldm.modules.attention.SpatialTransformer],
|
| 1000 |
[2, comfy.ldm.modules.attention.SpatialTransformer],
|
| 1001 |
[3, comfy.ldm.modules.diffusionmodules.openaimodel.Downsample],
|
|
|
|
| 1080 |
bo['latent_id'] = 0
|
| 1081 |
|
| 1082 |
# patch layers `forward` so we can apply brushnet
|
| 1083 |
+
# Modified to prevent re-patching leak and closure creation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1084 |
for i, block in enumerate(model.model.diffusion_model.input_blocks):
|
| 1085 |
for j, layer in enumerate(block):
|
| 1086 |
if not hasattr(layer, 'original_forward'):
|
| 1087 |
layer.original_forward = layer.forward
|
| 1088 |
+
|
| 1089 |
+
# Only patch if not already patched by us
|
| 1090 |
+
if getattr(layer.forward, '__func__', None) != forward_patched_by_brushnet:
|
| 1091 |
+
layer.forward = types.MethodType(forward_patched_by_brushnet, layer)
|
| 1092 |
+
|
| 1093 |
layer.add_sample_after = 0
|
| 1094 |
|
| 1095 |
for j, layer in enumerate(model.model.diffusion_model.middle_block):
|
| 1096 |
if not hasattr(layer, 'original_forward'):
|
| 1097 |
layer.original_forward = layer.forward
|
| 1098 |
+
|
| 1099 |
+
if getattr(layer.forward, '__func__', None) != forward_patched_by_brushnet:
|
| 1100 |
+
layer.forward = types.MethodType(forward_patched_by_brushnet, layer)
|
| 1101 |
+
|
| 1102 |
layer.add_sample_after = 0
|
| 1103 |
|
| 1104 |
for i, block in enumerate(model.model.diffusion_model.output_blocks):
|
| 1105 |
for j, layer in enumerate(block):
|
| 1106 |
if not hasattr(layer, 'original_forward'):
|
| 1107 |
layer.original_forward = layer.forward
|
| 1108 |
+
|
| 1109 |
+
if getattr(layer.forward, '__func__', None) != forward_patched_by_brushnet:
|
| 1110 |
+
layer.forward = types.MethodType(forward_patched_by_brushnet, layer)
|
| 1111 |
+
|
| 1112 |
layer.add_sample_after = 0
|