-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeformable_attn.py
236 lines (186 loc) · 7.87 KB
/
deformable_attn.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
from typing import List, Optional
import torch
import torch.nn.modules as nn
import torch.nn.functional as F
def generate_ref_points(width: int,
height: int):
grid_y, grid_x = torch.meshgrid(torch.arange(0, height), torch.arange(0, width))
grid_y = grid_y / (height - 1)
grid_x = grid_x / (width - 1)
grid = torch.stack((grid_x, grid_y), 2).float()
grid.requires_grad = False
return grid
def restore_scale(width: int,
height: int,
ref_point: torch.Tensor):
new_point = ref_point.clone().detach()
c = new_point[..., 0]
d = [[1,2,3],[1,2,3]]
new_point[..., 0] = new_point[..., 0] * (width - 1)
new_point[..., 1] = new_point[..., 1] * (height - 1)
return new_point
class DeformableHeadAttention(nn.Module):
def __init__(self, h,
d_model,
k,
last_feat_height,
last_feat_width,
scales=1,
dropout=0.1,
need_attn=False):
"""
:param h: number of self attention head
:param d_model: dimension of model
:param dropout:
:param k: number of keys
"""
super(DeformableHeadAttention, self).__init__()
# assert h == 8 # currently header is fixed 8 in paper
assert d_model % h == 0
# We assume d_v always equals d_k, d_q = d_k = d_v = d_m / h
self.d_k = int(d_model / h)
self.h = h
# self.q_proj = nn.Linear(d_model, d_model)
# self.k_proj = nn.Linear(d_model, d_model)
self.scales_hw = []
for i in range(scales):
self.scales_hw.append([last_feat_height * 2 ** i,
last_feat_width * 2 ** i])
self.dropout = None
if self.dropout:
self.dropout = nn.Dropout(p=dropout)
self.k = k
self.scales = scales
self.last_feat_height = last_feat_height
self.last_feat_width = last_feat_width
self.offset_dims = 2 * self.h * self.k * self.scales
self.A_dims = self.h * self.k * self.scales
# 2MLK for offsets MLK for A_mlqk
self.offset_proj = nn.Linear(d_model, self.offset_dims)
self.A_proj = nn.Linear(d_model, self.A_dims)
self.wm_proj = nn.Linear(d_model, d_model)
self.need_attn = need_attn
self.reset_parameters()
def reset_parameters(self):
torch.nn.init.constant_(self.offset_proj.weight, 0.0)
torch.nn.init.constant_(self.A_proj.weight, 0.0)
torch.nn.init.constant_(self.A_proj.bias, 1 / (self.scales * self.k))
def init_xy(bias, x, y):
torch.nn.init.constant_(bias[:, 0], float(x))
torch.nn.init.constant_(bias[:, 1], float(y))
# caution: offset layout will be M, L, K, 2
bias = self.offset_proj.bias.view(self.h, self.scales, self.k, 2)
init_xy(bias[0], x=-self.k, y=-self.k)
init_xy(bias[1], x=-self.k, y=0)
init_xy(bias[2], x=-self.k, y=self.k)
init_xy(bias[3], x=0, y=-self.k)
init_xy(bias[4], x=0, y=self.k)
init_xy(bias[5], x=self.k, y=-self.k)
init_xy(bias[6], x=self.k, y=0)
init_xy(bias[7], x=self.k, y=self.k)
def forward(self,
query: torch.Tensor,
keys: List[torch.Tensor],
ref_point: torch.Tensor,
query_mask: torch.Tensor = None,
key_masks: Optional[torch.Tensor] = None,
):
"""
:param key_masks:
:param query_mask:
:param query: B, H, W, C
:param keys: List[B, H, W, C]
:param ref_point: B, H, W, 2
:return:
"""
if key_masks is None:
key_masks = [None] * len(keys)
assert len(keys) == self.scales
attns = {'attns': None, 'offsets': None}
nbatches, query_height, query_width, _ = query.shape
# B, H, W, C
# query = self.q_proj(query)
# B, H, W, 2MLK
offset = self.offset_proj(query)
# B, H, W, M, 2LK
offset = offset.view(nbatches, query_height, query_width, self.h, -1)
# B, H, W, MLK
A = self.A_proj(query)
# B, H, W, M, LK
A = A.view(nbatches, query_height, query_width, self.h, -1)
A = F.softmax(A, dim=-1)
offset = offset.view(nbatches, query_height, query_width, self.h, self.scales, self.k, 2)
offset = offset.permute(0, 3, 4, 5, 1, 2, 6).contiguous()
# B*M, L, K, H, W, 2
offset = offset.view(nbatches * self.h, self.scales, self.k, query_height, query_width, 2)
A = A.permute(0, 3, 1, 2, 4).contiguous()
# B*M, H*W, LK
A = A.view(nbatches * self.h, query_height * query_width, -1)
scale_features = []
for l in range(self.scales):
feat_map = keys[l]
_, h, w, _ = feat_map.shape
key_mask = key_masks[l]
# B, H, W, 2
reversed_ref_point = restore_scale(height=h, width=w, ref_point=ref_point)
# B, H, W, 2 -> B*M, H, W, 2
reversed_ref_point = reversed_ref_point.repeat(self.h, 1, 1, 1)
# B, h, w, M, C_v
# scale_feature = self.k_proj(feat_map).view(nbatches, h, w, self.h, self.d_k)
scale_feature = feat_map.view(nbatches, h, w, self.h, self.d_k)
if key_mask is not None:
# B, h, w, 1, 1
key_mask = key_mask.unsqueeze(dim=-1).unsqueeze(dim=-1)
key_mask = key_mask.expand(nbatches, h, w, self.h, self.d_k)
scale_feature = torch.masked_fill(scale_feature, mask=key_mask, value=0)
# B, M, C_v, h, w
scale_feature = scale_feature.permute(0, 3, 4, 1, 2).contiguous()
# B*M, C_v, h, w
scale_feature = scale_feature.view(-1, self.d_k, h, w)
k_features = []
for k in range(self.k):
points = reversed_ref_point + offset[:, l, k, :, :, :]
vgrid_x = 2.0 * points[:, :, :, 0] / max(w - 1, 1) - 1.0
vgrid_y = 2.0 * points[:, :, :, 1] / max(h - 1, 1) - 1.0
vgrid_scaled = torch.stack((vgrid_x, vgrid_y), dim=3)
# B*M, C_v, H, W
feat = F.grid_sample(scale_feature, vgrid_scaled, mode='bilinear', padding_mode='zeros')
k_features.append(feat)
# B*M, k, C_v, H, W
k_features = torch.stack(k_features, dim=1)
scale_features.append(k_features)
# B*M, L, K, C_v, H, W
scale_features = torch.stack(scale_features, dim=1)
# B*M, H*W, C_v, LK
scale_features = scale_features.permute(0, 4, 5, 3, 1, 2).contiguous()
scale_features = scale_features.view(nbatches * self.h, query_height * query_width, self.d_k, -1)
# B*M, H*W, C_v
feat = torch.einsum('nlds, nls -> nld', scale_features, A)
# B*M, H*W, C_v -> B, M, H, W, C_v
feat = feat.view(nbatches, self.h, query_height, query_width, self.d_k)
# B, M, H, W, C_v -> B, H, W, M, C_v
feat = feat.permute(0, 2, 3, 1, 4).contiguous()
# B, H, W, M, C_v -> B, H, W, M * C_v
feat = feat.view(nbatches, query_height, query_width, self.d_k * self.h)
feat = self.wm_proj(feat)
if self.dropout:
feat = self.dropout(feat)
return feat, attns
if __name__ =="__main__":
DA = DeformableHeadAttention(1,
256,
4,
16,
16,
scales=1,
dropout=0.1,
need_attn=False)
a = torch.randn(3, 16, 16, 256)
b = [a]
ref_point = generate_ref_points(width=16,
height=16)
ref_point = ref_point.type_as(a)
# H, W, 2 -> B, H, W, 2
ref_point = ref_point.unsqueeze(0).repeat(3, 1, 1, 1)
a = DA(torch.randn(3,16,16,256),b,ref_point)
print()