-
Notifications
You must be signed in to change notification settings - Fork 0
/
attn_utils.py
195 lines (158 loc) · 8.29 KB
/
attn_utils.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
from typing import Optional, Union, Tuple, List, Callable, Dict
from diffusers import StableDiffusionPipeline
import numpy as np
import torch
import abc
import generate_utils as g_utils
import seq_aligner
class AttentionControl(abc.ABC):
def step_callback(self, x_t):
return x_t
def between_steps(self):
return
@property
def num_uncond_att_layers(self):
return 0
@abc.abstractmethod
def forward (self, attn, is_cross: bool, place_in_unet: str,erase_attn):
raise NotImplementedError
def __call__(self, attn, is_cross: bool, place_in_unet: str,erase_attn):
if self.cur_att_layer >= self.num_uncond_att_layers:
h = attn.shape[0]
attn[:h // 2] = self.forward(attn[:h // 2], is_cross, place_in_unet,erase_attn[:h // 2])
self.cur_att_layer += 1
if self.cur_att_layer == self.num_att_layers + self.num_uncond_att_layers:
self.cur_att_layer = 0
self.cur_step += 1
self.between_steps()
return attn
def reset(self):
self.cur_step = 0
self.cur_att_layer = 0
def __init__(self):
self.cur_step = 0
self.num_att_layers = -1
self.cur_att_layer = 0
class EmptyControl(AttentionControl):
def forward (self, attn, is_cross: bool, place_in_unet: str,erase_attn):
return attn
class AttentionStore(AttentionControl):
@staticmethod
def get_empty_store():
return {"down_cross": [], "mid_cross": [], "up_cross": [],
"down_self": [], "mid_self": [], "up_self": []}
def forward(self, attn, is_cross: bool, place_in_unet: str, erase_attn):
key = f"{place_in_unet}_{'cross' if is_cross else 'self'}"
if attn.shape[1] <= 32 ** 2:
self.step_store[key].append(attn)
return attn
def between_steps(self):
if len(self.attention_store) == 0:
self.attention_store = self.step_store
else:
for key in self.attention_store:
for i in range(len(self.attention_store[key])):
self.attention_store[key][i] += self.step_store[key][i]
self.step_store = self.get_empty_store()
def get_average_attention(self):
average_attention = {}
for key in self.attention_store:
attention_list = self.attention_store[key]
average_attention[key] = []
for item in attention_list:
average_attention[key].append(item / self.cur_step)
return average_attention
def reset(self):
super(AttentionStore, self).reset()
self.step_store = self.get_empty_store()
self.attention_store = {}
def __init__(self):
super(AttentionStore, self).__init__()
self.step_store = self.get_empty_store()
self.attention_store = {}
class AttentionControlEdit(AttentionStore, abc.ABC):
def step_callback(self, x_t):
return x_t
def replace_self_attention(self, attn_base, att_replace):
if att_replace.shape[2] <= 16 ** 2:
return attn_base.unsqueeze(0).expand(att_replace.shape[0], *attn_base.shape)
else:
return att_replace
@abc.abstractmethod
def replace_cross_attention(self, attn_base, att_replace,erase_attn):
raise NotImplementedError
def forward(self, attn, is_cross: bool, place_in_unet: str,erase_attn):
super(AttentionControlEdit, self).forward(attn, is_cross, place_in_unet,erase_attn)
if is_cross or (self.num_self_replace[0] <= self.cur_step < self.num_self_replace[1]):
h = attn.shape[0] // (self.batch_size)
attn = attn.reshape(self.batch_size, h, *attn.shape[1:])
erase_attn = erase_attn.reshape(self.batch_size, h, *erase_attn.shape[1:])
attn_base, attn_repalce = attn[0], attn[1:]
erase_attn_base=erase_attn[1:]
if is_cross:
alpha_words = self.cross_replace_alpha[self.cur_step]
attn_repalce_new = self.replace_cross_attention(attn_base, attn_repalce,erase_attn_base) * alpha_words + (1 - alpha_words) * attn_repalce
attn[1:] = attn_repalce_new
else:
attn[1:] = self.replace_self_attention(attn_base, attn_repalce)
attn = attn.reshape(self.batch_size * h, *attn.shape[2:])
return attn
def __init__(self, prompts, erase_targets, num_steps: int,
cross_replace_steps: Union[float, Tuple[float, float], Dict[str, Tuple[float, float]]],
self_replace_steps: Union[float, Tuple[float, float]],tokenizer,device):
super(AttentionControlEdit, self).__init__()
self.batch_size = len(prompts)
self.cross_replace_alpha = g_utils.get_time_words_attention_alpha(prompts, num_steps, cross_replace_steps, tokenizer).to(device)
if type(self_replace_steps) is float:
self_replace_steps = 0, self_replace_steps
self.num_self_replace = int(num_steps * self_replace_steps[0]), int(num_steps * self_replace_steps[1])
class AttentionInhibit(AttentionControlEdit):
def replace_cross_attention(self, attn_base, att_replace, erase_attn):
erase_attn_supress = erase_attn[:, :, :, self.erase_mapper[0]] * self.equalizer[:, None, None, :]
attn_base_replace = attn_base[:, :, self.mapper].permute(2, 0, 1, 3)
attn_replace = attn_base_replace * self.alphas + erase_attn_supress * (1 - self.alphas)
return attn_replace
def __init__(self, prompts, erase_targets, num_steps: int, cross_replace_steps: float, self_replace_steps: float, equalizer, tokenizer, device):
super(AttentionInhibit, self).__init__(prompts, erase_targets, num_steps, cross_replace_steps, self_replace_steps,tokenizer,device)
self.equalizer = equalizer.to(device)
self.mapper, erase_mapper, alphas = seq_aligner.get_refinement_mapper(prompts, tokenizer)
self.mapper, alphas = self.mapper.to(device), alphas.to(device)
self.erase_mapper = erase_mapper.to(device)
self.alphas = alphas.reshape(alphas.shape[0], 1, 1, alphas.shape[1])
class AttentionErase(AttentionInhibit):
def forward(self, attn, is_cross: bool, place_in_unet: str,erase_attn):
super(AttentionControlEdit, self).forward(attn, is_cross, place_in_unet,erase_attn)
if is_cross or (self.num_self_replace[0] <= self.cur_step < self.num_self_replace[1]):
h = attn.shape[0] // (self.batch_size)
attn = attn.reshape(self.batch_size, h, *attn.shape[1:])
erase_attn = erase_attn.reshape(self.batch_size, h, *erase_attn.shape[1:])
attn_base, attn_repalce = attn[0], attn[1:]
erase_attn_base=erase_attn[1:]
if is_cross:
self.cross_step.append(erase_attn_base[:,:,:,1:self.extract-1])
alpha_words = self.cross_replace_alpha[self.cur_step]
attn_repalce_new = self.replace_cross_attention(attn_base, attn_repalce,erase_attn_base) * alpha_words + (1 - alpha_words) * attn_repalce
attn[1:] = attn_repalce_new
else:
attn[1:] = self.replace_self_attention(attn_base, attn_repalce)
attn = attn.reshape(self.batch_size * h, *attn.shape[2:])
return attn
def __call__(self, attn, is_cross: bool, place_in_unet: str,erase_attn):
if self.cur_att_layer >= self.num_uncond_att_layers:
h = attn.shape[0]
attn[:h // 2] = self.forward(attn[:h // 2], is_cross, place_in_unet,erase_attn[:h // 2])
self.cur_att_layer += 1
if self.cur_att_layer == self.num_att_layers + self.num_uncond_att_layers:
self.cur_att_layer = 0
self.cur_step += 1
self.data.append(self.cross_step)
self.cross_step=[]
self.between_steps()
return attn
def __init__(self, prompts, erase_targets, num_steps: int, cross_replace_steps: float, self_replace_steps: float, equalizer, tokenizer, device):
super(AttentionErase, self).__init__(prompts, erase_targets, num_steps, cross_replace_steps, self_replace_steps, equalizer, tokenizer, device)
self.cross_step=[]
self.data=[]
join_result = " ".join(erase_targets)
tokens_prompt = tokenizer.encode(join_result)
self.extract = len(tokens_prompt)