Spaces:
Sleeping
Sleeping
File size: 7,920 Bytes
64cc34d |
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
import os
import math
import numpy as np
import matplotlib.pyplot as plt
import torch
class Conceptrol:
def __init__(self, config):
if "name" not in config:
raise KeyError("name has to be provided as 'conceptrol' or 'ominicontrol'")
name = config["name"]
if name not in ["conceptrol", "ominicontrol"]:
raise ValueError(
f"Name must be one of ['conceptrol', 'ominicontrol'], got {name}"
)
try:
log_attn_map = config["log_attn_map"]
except KeyError:
log_attn_map = False
# static
self.NUM_BLOCKS = 19 # this is fixed for FLUX
self.M = 512 # num of text tokens, fixed for FLUX
self.N = 1024 # num of latent / image condtion tokens, fixed for FLUX
self.EP = -10e6
self.CONCEPT_BLOCK_IDX = 18
# fixed during one generation
self.name = name
# variable during one generation
self.textual_concept_mask = None
self.forward_count = 0
# log out for visualization
if log_attn_map:
self.attn_maps = {"latent_to_concept": [], "latent_to_image": []}
def __call__(
self,
query: torch.FloatTensor,
key: torch.FloatTensor,
attention_mask: torch.Tensor,
c_factor: float = 1.0,
) -> torch.Tensor:
if not hasattr(self, "textual_concept_idx"):
raise AttributeError(
"textual_concept_idx must be registered before calling Conceptrol"
)
# Skip computation for ominicontrol
if self.name == "ominicontrol":
scale_factor = 1 / math.sqrt(query.size(-1))
attention_weight = (
query @ key.transpose(-2, -1) * scale_factor + attention_mask
)
attention_probs = torch.softmax(
attention_weight, dim=-1
) # [B, H, M+2N, M+2N]
return attention_probs
if not self.textual_concept_idx[0] < self.textual_concept_idx[1]:
raise ValueError(
f"register_idx[0] must be less than register_idx[1], "
f"got {self.textual_concept_idx[0]} >= {self.textual_concept_idx[1]}"
)
### Reset attention mask predefined in ominicontrol
attention_mask = torch.zeros_like(attention_mask)
bias = torch.log(c_factor[0])
# attention of image condition to latent
attention_mask[-self.N :, self.M : -self.N] = bias
# attention of latent to image condition
attention_mask[self.M : -self.N, -self.N :] = bias
# attention of textual concept to image condition
attention_mask[
self.textual_concept_idx[0] : self.textual_concept_idx[1], -self.N :
] = bias
# attention of other words to image condition (set as negative inf)
attention_mask[: self.textual_concept_idx[0], -self.N :] = self.EP
attention_mask[self.textual_concept_idx[1] : self.M, -self.N :] = self.EP
# If there is no textual_concept_mask, it means currently in layers previous to the first concept-specific block
if self.textual_concept_mask is None:
self.textual_concept_mask = (
torch.zeros_like(attention_mask).unsqueeze(0).unsqueeze(0)
)
### Compute attention
scale_factor = 1 / math.sqrt(query.size(-1))
attention_weight = (
query @ key.transpose(-2, -1) * scale_factor
+ attention_mask
+ self.textual_concept_mask
)
# [B, H, M+2N, M+2N]
attention_probs = torch.softmax(attention_weight, dim=-1)
### Extract textual concept mask if it's concept-specific block
is_concept_block = (
self.forward_count % self.NUM_BLOCKS == self.CONCEPT_BLOCK_IDX
)
if is_concept_block:
# Shape: [B, H, N, S], where S is the token numbers of the subject
textual_concept_mask_local = attention_probs[
:,
:,
self.M : -self.N,
self.textual_concept_idx[0] : self.textual_concept_idx[1],
]
# Consider the ratio within context of text
textual_concept_mask_local = textual_concept_mask_local / torch.sum(
attention_probs[:, :, self.M : -self.N, : self.M], dim=-1, keepdim=True
)
# Average over words and head, Shape: [B, 1, N, 1]
textual_concept_mask_local = torch.mean(
textual_concept_mask_local, dim=(-1, 1), keepdim=True
)
# Normalize to average as 1
textual_concept_mask_local = textual_concept_mask_local / torch.mean(
textual_concept_mask_local, dim=-2, keepdim=True
)
self.textual_concept_mask = (
torch.zeros_like(attention_mask).unsqueeze(0).unsqueeze(0)
)
# log(A) in the paper
self.textual_concept_mask[:, :, self.M : -self.N, -self.N :] = torch.log(
textual_concept_mask_local
)
self.forward_count += 1
return attention_probs
def register(self, textual_concept_idx):
self.textual_concept_idx = textual_concept_idx
def visualize_attn_map(self, config_name: str, subject: str):
global global_concept_mask
global forward_count
save_dir = f"attn_maps/{config_name}/{subject}"
if not os.path.exists(save_dir):
os.makedirs(save_dir)
for attn_map_name, attn_maps in self.attn_maps.items():
if "token_to_token" in attn_map_name:
continue
plt.figure()
rows, cols = 8, 19
fig, axes = plt.subplots(
rows, cols, figsize=(64 * cols / 100, 64 * rows / 100)
)
fig.subplots_adjust(
wspace=0.1, hspace=0.1
) # Adjust spacing between subplots
# Plot each array in the list on the grid
for i, ax in enumerate(axes.flatten()):
if i < len(attn_maps): # Only plot existing arrays
attn_map = attn_maps[i] / np.amax(attn_maps[i])
ax.imshow(attn_map, cmap="viridis")
ax.axis("off") # Turn off axes for clarity
else:
ax.axis("off") # Turn off unused subplots
fig.set_size_inches(64 * cols / 100, 64 * rows / 100)
save_path = os.path.join(save_dir, f"{attn_map_name}.jpg")
plt.savefig(save_path)
plt.close()
for attn_map_name, attn_maps in self.attn_maps.items():
if "token_to_token" not in attn_map_name:
continue
plt.figure()
rows, cols = 8, 19
fig, axes = plt.subplots(
rows, cols, figsize=(2560 * cols / 100, 2560 * rows / 100)
)
fig.subplots_adjust(
wspace=0.1, hspace=0.1
) # Adjust spacing between subplots
# Plot each array in the list on the grid
for i, ax in enumerate(axes.flatten()):
if i < len(attn_maps): # Only plot existing arrays
attn_map = attn_maps[i] / np.amax(attn_maps[i])
ax.imshow(attn_map, cmap="viridis")
ax.axis("off") # Turn off axes for clarity
else:
ax.axis("off") # Turn off unused subplots
fig.set_size_inches(64 * cols / 100, 64 * rows / 100)
save_path = os.path.join(save_dir, f"{attn_map_name}.jpg")
plt.savefig(save_path)
plt.close()
for attn_map_name in self.attn_maps.keys():
self.attn_maps[attn_map_name] = []
global_concept_mask = None
forward_count = 0
|