forked from ai-dawang/PlugNPlay-Modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRMSA.py
263 lines (210 loc) · 10.2 KB
/
CRMSA.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import torch
import torch.nn as nn
import numpy as np
import math
#Cross-region Multi-head Self-attention跨区域多头自注意力
def region_partition(x, region_size):
"""
Args:
x: (B, H, W, C)
region_size (int): region size
Returns:
regions: (num_regions*B, region_size, region_size, C)
"""
B, H, W, C = x.shape
x = x.view(B, H // region_size, region_size, W // region_size, region_size, C)
regions = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, region_size, region_size, C)
return regions
def region_reverse(regions, region_size, H, W):
"""
Args:
regions: (num_regions*B, region_size, region_size, C)
region_size (int): Window size
H (int): Height of image
W (int): Width of image
Returns:
x: (B, H, W, C)
"""
B = int(regions.shape[0] / (H * W / region_size / region_size))
x = regions.view(B, H // region_size, W // region_size, region_size, region_size, -1)
x = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(B, H, W, -1)
return x
class InnerAttention(nn.Module):
def __init__(self, dim, head_dim=None, num_heads=8, qkv_bias=True, qk_scale=None, attn_drop=0., proj_drop=0.,
epeg=True, epeg_k=15, epeg_2d=False, epeg_bias=True, epeg_type='attn'):
super().__init__()
self.dim = dim
self.num_heads = num_heads
if head_dim is None:
head_dim = dim // num_heads
self.head_dim = head_dim
self.scale = qk_scale or head_dim ** -0.5
self.qkv = nn.Linear(dim, head_dim * num_heads * 3, bias=qkv_bias)
self.attn_drop = nn.Dropout(attn_drop)
self.proj = nn.Linear(head_dim * num_heads, dim)
self.proj_drop = nn.Dropout(proj_drop)
self.epeg_2d = epeg_2d
self.epeg_type = epeg_type
if epeg:
padding = epeg_k // 2
if epeg_2d:
if epeg_type == 'attn':
self.pe = nn.Conv2d(num_heads, num_heads, epeg_k, padding=padding, groups=num_heads, bias=epeg_bias)
else:
self.pe = nn.Conv2d(head_dim * num_heads, head_dim * num_heads, epeg_k, padding=padding,
groups=head_dim * num_heads, bias=epeg_bias)
else:
if epeg_type == 'attn':
self.pe = nn.Conv2d(num_heads, num_heads, (epeg_k, 1), padding=(padding, 0), groups=num_heads,
bias=epeg_bias)
else:
self.pe = nn.Conv2d(head_dim * num_heads, head_dim * num_heads, (epeg_k, 1), padding=(padding, 0),
groups=head_dim * num_heads, bias=epeg_bias)
else:
self.pe = None
self.softmax = nn.Softmax(dim=-1)
def forward(self, x):
"""
Args:
x: input features with shape of (num_regions*B, N, C)
"""
B_, N, C = x.shape
# x = self.pe(x)
qkv = self.qkv(x).reshape(B_, N, 3, self.num_heads, self.head_dim).permute(2, 0, 3, 1, 4)
q, k, v = qkv[0], qkv[1], qkv[2] # make torchscript happy (cannot use tensor as tuple)
q = q * self.scale
attn = (q @ k.transpose(-2, -1))
if self.pe is not None and self.epeg_type == 'attn':
pe = self.pe(attn)
attn = attn + pe
attn = self.softmax(attn)
attn = self.attn_drop(attn)
if self.pe is not None and self.epeg_type == 'value_bf':
# B,H,N,C -> B,HC,N-0.5,N-0.5
pe = self.pe(v.permute(0, 3, 1, 2).reshape(B_, C, int(np.ceil(np.sqrt(N))), int(np.ceil(np.sqrt(N)))))
# pe = torch.einsum('ahbd->abhd',pe).flatten(-2,-1)
v = v + pe.reshape(B_, self.num_heads, self.head_dim, N).permute(0, 1, 3, 2)
# print(v.size())
x = (attn @ v).transpose(1, 2).reshape(B_, N, self.num_heads * self.head_dim)
if self.pe is not None and self.epeg_type == 'value_af':
# print(v.size())
pe = self.pe(v.permute(0, 3, 1, 2).reshape(B_, C, int(np.ceil(np.sqrt(N))), int(np.ceil(np.sqrt(N)))))
# print(pe.size())
# print(v.size())
x = x + pe.reshape(B_, self.num_heads * self.head_dim, N).transpose(-1, -2)
x = self.proj(x)
x = self.proj_drop(x)
return x
def extra_repr(self) -> str:
return f'dim={self.dim}, region_size={self.region_size}, num_heads={self.num_heads}'
def flops(self, N):
# calculate flops for 1 region with token length of N
flops = 0
# qkv = self.qkv(x)
flops += N * self.dim * 3 * self.dim
# attn = (q @ k.transpose(-2, -1))
flops += self.num_heads * N * (self.dim // self.num_heads) * N
# x = (attn @ v)
flops += self.num_heads * N * N * (self.dim // self.num_heads)
# x = self.proj(x)
flops += N * self.dim * self.dim
return flops
class CrossRegionAttntion(nn.Module):
def __init__(self, dim, head_dim=None, num_heads=8, region_size=0, qkv_bias=True, qk_scale=None, drop=0.,
attn_drop=0., region_num=8, epeg=False, min_region_num=0, min_region_ratio=0., crmsa_k=3,
crmsa_mlp=False, region_attn='native', **kawrgs):
super().__init__()
self.dim = dim
self.num_heads = num_heads
self.region_size = region_size if region_size > 0 else None
self.region_num = region_num
self.min_region_num = min_region_num
self.min_region_ratio = min_region_ratio
self.attn = InnerAttention(
dim, head_dim=head_dim, num_heads=num_heads,
qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop, epeg=epeg, **kawrgs)
self.crmsa_mlp = crmsa_mlp
if crmsa_mlp:
self.phi = [nn.Linear(self.dim, self.dim // 4, bias=False)]
self.phi += [nn.Tanh()]
self.phi += [nn.Linear(self.dim // 4, crmsa_k, bias=False)]
self.phi = nn.Sequential(*self.phi)
else:
self.phi = nn.Parameter(
torch.empty(
(self.dim, crmsa_k),
)
)
nn.init.kaiming_uniform_(self.phi, a=math.sqrt(5))
def padding(self, x):
B, L, C = x.shape
if self.region_size is not None:
H, W = int(np.ceil(np.sqrt(L))), int(np.ceil(np.sqrt(L)))
_n = -H % self.region_size
H, W = H + _n, W + _n
region_num = int(H // self.region_size)
region_size = self.region_size
else:
H, W = int(np.ceil(np.sqrt(L))), int(np.ceil(np.sqrt(L)))
_n = -H % self.region_num
H, W = H + _n, W + _n
region_size = int(H // self.region_num)
region_num = self.region_num
add_length = H * W - L
# if padding much,i will give up region attention. only for ablation
if (add_length > L / (self.min_region_ratio + 1e-8) or L < self.min_region_num):
H, W = int(np.ceil(np.sqrt(L))), int(np.ceil(np.sqrt(L)))
_n = -H % 2
H, W = H + _n, W + _n
add_length = H * W - L
region_size = H
if add_length > 0:
x = torch.cat([x, torch.zeros((B, add_length, C), device=x.device)], dim=1)
return x, H, W, add_length, region_num, region_size
def forward(self, x, return_attn=False):
B, L, C = x.shape
# padding
x, H, W, add_length, region_num, region_size = self.padding(x)
x = x.view(B, H, W, C)
# partition regions
x_regions = region_partition(x, region_size) # nW*B, region_size, region_size, C
x_regions = x_regions.view(-1, region_size * region_size, C) # nW*B, region_size*region_size, C
# CR-MSA
if self.crmsa_mlp:
logits = self.phi(x_regions).transpose(1, 2) # W*B, sW, region_size*region_size
else:
logits = torch.einsum("w p c, c n -> w p n", x_regions, self.phi).transpose(1,
2) # nW*B, sW, region_size*region_size
dispatch_weights = logits.softmax(dim=-1)
combine_weights = logits.softmax(dim=1)
logits_min, _ = logits.min(dim=-1)
logits_max, _ = logits.max(dim=-1)
dispatch_weights_1 = (logits - logits_min.unsqueeze(-1)) / (
logits_max.unsqueeze(-1) - logits_min.unsqueeze(-1) + 1e-8)
attn_regions = torch.einsum("w p c, w n p -> w n p c", x_regions, dispatch_weights).sum(dim=-2).transpose(0,
1) # sW, nW, C
if return_attn:
attn_regions, _attn = self.attn(attn_regions, return_attn) # sW, nW, C
attn_regions = attn_regions.transpose(0, 1) # nW, sW, C
else:
attn_regions = self.attn(attn_regions).transpose(0, 1) # nW, sW, C
attn_regions = torch.einsum("w n c, w n p -> w n p c", attn_regions,
dispatch_weights_1) # nW, sW, region_size*region_size, C
attn_regions = torch.einsum("w n p c, w n p -> w n p c", attn_regions, combine_weights).sum(
dim=1) # nW, region_size*region_size, C
# merge regions
attn_regions = attn_regions.view(-1, region_size, region_size, C)
x = region_reverse(attn_regions, region_size, H, W) # B H' W' C
x = x.view(B, H * W, C)
if add_length > 0:
x = x[:, :-add_length]
return x
if __name__ == '__main__':
# 输入张量的形状为 (batch_size, sequence_length, embedding_dim)
input = torch.randn(1, 64, 1024) # 示例输入张量,批大小为 2,序列长度为 64,嵌入维度为 128
block = CrossRegionAttntion(dim=1024, num_heads=8, region_size=16)
# 将输入张量输入模块,获取输出张量
output = block(input)
# 打印输入和输出张量的形状
print(input.size())
print( output.size())