forked from G-370/ComfyUI-SD3-Powerlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
171 lines (133 loc) · 5.5 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from typing import NamedTuple
from torch import Tensor
import torchvision.transforms as T
import torch
import matplotlib.pyplot as plt
to_tensor = T.ToTensor()
to_img = T.ToPILImage()
BACKBONE_ENUM = {
'text': 'context_block',
'latent': 'x_block'
}
def colormap_tensor(colormap, tensor: Tensor):
if (colormap == 'none'):
return tensor.unsqueeze(-1).repeat(1, 1, 3).unsqueeze(0)
# Normalize the tensor to the range [0, 1]
tensor_min = tensor.min()
tensor_max = tensor.max()
tensor_normalized = (tensor - tensor_min) / (tensor_max - tensor_min)
tensor_np = tensor_normalized.numpy()
colormap = plt.get_cmap(colormap)
tensor_colored_np = colormap(tensor_np)
tensor_colored = torch.from_numpy(tensor_colored_np)#tensor_colored_np[:, :, :3])
return tensor_colored
matplotlib_colormaps = plt.colormaps()
class RenderAttentionSpot:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"sd3_model": ("MODEL",),
"joint_block": ("INT", {"default": 0, "max": 23}),
"backbone": (["text", "latent"],),
"view": (["query", "key", "value", "all-stacked", "all-interposed"],),
"colormap": (["none", *matplotlib_colormaps],)
}
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "render"
CATEGORY = "SD3 Power Lab/Visualize"
def render(self, sd3_model, joint_block, backbone, view, colormap):
km = sd3_model.model_state_dict()
tensor_location = f'joint_blocks.{joint_block}.{BACKBONE_ENUM[backbone]}.attn.qkv.weight'
attention_tensor: Tensor = None
for k in km:
if tensor_location in k:
attention_tensor = km[k]
if (attention_tensor is None):
raise f"Could not locate attention tensor {tensor_location}"
pre_image_tensor: Tensor = None
q,k,v = (None,None,None)
if (view == 'all-stacked'):
pre_image_tensor: Tensor = attention_tensor
elif (view == 'all-interposed'):
pre_image_tensor= attention_tensor.view(1536, 1536, 3)
else:
q,k,v = torch.split(attention_tensor, 1536)
if (view == 'query'):
pre_image_tensor = q
elif (view == 'key'):
pre_image_tensor = k
elif (view == 'value'):
pre_image_tensor = v
if len(pre_image_tensor.shape) == 3:
return (pre_image_tensor.unsqueeze(0), )
else:
return colormap_tensor(colormap, pre_image_tensor)
def calculate_modified_tensor(tensor_area: Tensor, tensor_mask: Tensor, operation, operation_value):
modified_area = operation(tensor_area.clone()[tensor_mask], operation_value)
class AttentionToImage:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"sd3_model": ("MODEL",),
"joint_block": ("INT", {"default": 0, "max": 23}),
"backbone": (["text", "latent"],),
}
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "render"
CATEGORY = "SD3 Power Lab/Hack"
def render(self, sd3_model, joint_block, backbone):
km = sd3_model.model_state_dict()
tensor_location = f'joint_blocks.{joint_block}.{BACKBONE_ENUM[backbone]}.attn.qkv.weight'
attention_tensor: Tensor = None
for k in km:
if tensor_location in k:
attention_tensor = km[k]
if (attention_tensor is None):
raise f"Could not locate attention tensor {tensor_location}"
return (attention_tensor.clone().view(1536, 1536, 3).unsqueeze(0),)
class ImageToAttention:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"sd3_model": ("MODEL",),
"joint_block": ("INT", {"default": 0, "max": 23}),
"backbone": (["text", "latent"],),
"attention_image": ("IMAGE",),
"patch_strength": ("FLOAT", {"default": 1.0, "max": 1.0, "min": 0.0}),
"model_strength": ("FLOAT", {"default": 0.0, "max": 1.0, "min": 0.0})
}
}
RETURN_TYPES = ("MODEL",)
FUNCTION = "patch"
CATEGORY = "SD3 Power Lab/Hack"
def patch(self, sd3_model, joint_block, backbone, attention_image, patch_strength, model_strength):
m = sd3_model.clone()
km = sd3_model.model_state_dict()
tensor_location = f'joint_blocks.{joint_block}.{BACKBONE_ENUM[backbone]}.attn.qkv.weight'
attention_tensor: Tensor = None
key_to_patch = None
for k in km:
if tensor_location in k:
attention_tensor = km[k]
key_to_patch = k
if (attention_tensor is None):
raise f"Could not locate attention tensor {tensor_location}"
modified_attention = attention_image.clone().squeeze(0).view(4608,1536)
m.add_patches({key_to_patch: (modified_attention,)}, patch_strength, model_strength)
return (m,)
NODE_CLASS_MAPPINGS = {
"G370SD3PowerLab_RenderAttention": RenderAttentionSpot,
"G370SD3PowerLab_AttentionToImage": AttentionToImage,
"G370SD3PowerLab_ImageIntoAttention": ImageToAttention
}
# A dictionary that contains the friendly/humanly readable titles for the nodes
NODE_DISPLAY_NAME_MAPPINGS = {
"G370SD3PowerLab_RenderAttention": "Render SD3 Attention",
"G370SD3PowerLab_AttentionToImage": "SD3 Attention To Image",
"G370SD3PowerLab_ImageIntoAttention": "SD3 Image Into Attention"
}