-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattention.py
282 lines (243 loc) · 11.9 KB
/
attention.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import math
import numpy
import torch
import torch.nn as nn
import torch.nn.functional as F
class MultiHeadedAttention(nn.Module):
"""Multi-Head Attention layer
:param int n_head: the number of head s
:param int n_feat: the number of features
:param float dropout_rate: dropout rate
"""
def __init__(self, n_head, n_feat, dropout_rate):
super(MultiHeadedAttention, self).__init__()
assert n_feat % n_head == 0
# We assume d_v always equals d_k
self.d_k = n_feat // n_head
self.h = n_head
self.linear_q = nn.Linear(n_feat, n_feat)
self.linear_k = nn.Linear(n_feat, n_feat)
self.linear_v = nn.Linear(n_feat, n_feat)
self.linear_out = nn.Linear(n_feat, n_feat)
self.attn = None
self.dropout = nn.Dropout(p=dropout_rate)
def forward(self, query, key, value, mask):
"""Compute 'Scaled Dot Product Attention'
:param torch.Tensor query: (batch, time1, size)
:param torch.Tensor key: (batch, time2, size)
:param torch.Tensor value: (batch, time2, size)
:param torch.Tensor mask: (batch, time1, time2)
:param torch.nn.Dropout dropout:
:return torch.Tensor: attentined and transformed `value` (batch, time1, d_model)
weighted by the query dot key attention (batch, head, time1, time2)
"""
n_batch = query.size(0)
q = self.linear_q(query).view(n_batch, -1, self.h, self.d_k)
k = self.linear_k(key).view(n_batch, -1, self.h, self.d_k)
v = self.linear_v(value).view(n_batch, -1, self.h, self.d_k)
q = q.transpose(1, 2) # (batch, head, time1, d_k)
k = k.transpose(1, 2) # (batch, head, time2, d_k)
v = v.transpose(1, 2) # (batch, head, time2, d_k)
scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(self.d_k) # (batch, head, time1, time2)
if mask is not None:
mask = mask.unsqueeze(1).eq(0) # (batch, 1, time1, time2)
min_value = float(numpy.finfo(torch.tensor(0, dtype=scores.dtype).numpy().dtype).min)
scores = scores.masked_fill(mask, min_value)
self.attn = torch.softmax(scores, dim=-1).masked_fill(mask, 0.0) # (batch, head, time1, time2)
else:
self.attn = torch.softmax(scores, dim=-1) # (batch, head, time1, time2)
p_attn = self.dropout(self.attn)
x = torch.matmul(p_attn, v) # (batch, head, time1, d_k)
x = x.transpose(1, 2).contiguous().view(n_batch, -1, self.h * self.d_k) # (batch, time1, d_model)
return self.linear_out(x), self.attn # (batch, time1, d_model)
def chunkwise(xs, N_l, N_c, N_r):
"""Slice input frames chunk by chunk.
Args:
xs (FloatTensor): `[B, T, input_dim]`
N_l (int): number of frames for left context
N_c (int): number of frames for current context
N_r (int): number of frames for right context
Returns:
xs (FloatTensor): `[B * n_chunks, N_l + N_c + N_r, input_dim]`
where n_chunks = ceil(T / N_c)
"""
bs, xmax, idim = xs.size()
n_chunks = math.ceil(xmax / N_c)
c = N_l + N_c + N_r
s_index = torch.arange(0, xmax, N_c).unsqueeze(-1)
c_index = torch.arange(0, c)
index = s_index + c_index
xs_pad = torch.cat([xs.new_zeros(bs, N_l, idim),
xs,
xs.new_zeros(bs, N_c*n_chunks-xmax+N_r, idim)], dim=1)
xs_chunk = xs_pad[:, index].contiguous().view(bs * n_chunks, N_l + N_c + N_r, idim)
return xs_chunk
class MHLocalDenseSynthesizerAttention(nn.Module):
"""Multi-Head Local Dense Synthesizer attention layer
In this implementation, the calculation of multi-head mechanism is similar to that of self-attention,
but it takes more time for training. We provide an alternative multi-head mechanism implementation
that can achieve competitive results with less time.
:param int n_head: the number of heads
:param int n_feat: the dimension of features
:param float dropout_rate: dropout rate
:param int context_size: context size
:param bool use_bias: use bias term in linear layers
"""
def __init__(self, n_head, n_feat, dropout_rate, context_size=15, use_bias=False):
super().__init__()
assert n_feat % n_head == 0
# We assume d_v always equals d_k
self.d_k = n_feat // n_head
self.h = n_head
self.c = context_size
self.w1 = nn.Linear(n_feat, n_feat, bias=use_bias)
# self.w2 = nn.Linear(n_feat, n_head * self.c, bias=use_bias)
self.w2 = nn.Conv1d(in_channels=n_feat, out_channels=n_head * self.c, kernel_size=1,
groups=n_head)
self.w3 = nn.Linear(n_feat, n_feat, bias=use_bias)
self.w_out = nn.Linear(n_feat, n_feat, bias=use_bias)
self.attn = None
self.dropout = nn.Dropout(p=dropout_rate)
def forward(self, query, key, value, mask):
"""Forward pass.
:param torch.Tensor query: (batch, time, size)
:param torch.Tensor key: (batch, time, size) dummy
:param torch.Tensor value: (batch, time, size)
:param torch.Tensor mask: (batch, time, time) dummy
:return torch.Tensor: attentioned and transformed `value` (batch, time, d_model)
"""
bs, time = query.size()[: 2]
query = self.w1(query) # [B, T, d]
# [B, T, d] --> [B, d, T] --> [B, H*c, T]
weight = self.w2(torch.relu(query).transpose(1, 2))
# [B, H, c, T] --> [B, T, H, c] --> [B*T, H, 1, c]
weight = weight.view(bs, self.h, self.c, time).permute(0, 3, 1, 2) \
.contiguous().view(bs * time, self.h, 1, self.c)
value = self.w3(value) # [B, T, d]
# [B*T, c, d] --> [B*T, c, H, d_k] --> [B*T, H, c, d_k]
value_cw = chunkwise(value, (self.c - 1) // 2, 1, (self.c - 1) // 2) \
.view(bs * time, self.c, self.h, self.d_k).transpose(1, 2)
self.attn = torch.softmax(weight, dim=-1)
p_attn = self.dropout(self.attn)
x = torch.matmul(p_attn, value_cw)
x = x.contiguous().view(bs, -1, self.h * self.d_k) # [B, T, d]
x = self.w_out(x) # [B, T, d]
return x
class LocalDenseSynthesizerAttention(nn.Module):
"""Multi-Head Local Dense Synthesizer attention layer
This implementation has lower CPU usage, but requires additional GPU memory usage.
:param int n_head: the number of heads
:param int n_feat: the dimension of features
:param float dropout_rate: dropout rate
:param int context_size: context size
:param bool use_bias: use bias term in linear layers
"""
def __init__(self, n_head, n_feat, dropout_rate, context_size=15, use_bias=False):
super().__init__()
assert n_feat % n_head == 0
# We assume d_v always equals d_k
self.d_k = n_feat // n_head
self.h = n_head
self.c = context_size
self.w1 = nn.Linear(n_feat, n_feat, bias=use_bias)
self.w2 = nn.Linear(n_feat, n_head*self.c, bias=use_bias)
self.w3 = nn.Linear(n_feat, n_feat, bias=use_bias)
self.w_out = nn.Linear(n_feat, n_feat, bias=use_bias)
self.attn = None
self.dropout = nn.Dropout(p=dropout_rate)
def forward(self, query, key, value, mask):
"""Forward pass.
:param torch.Tensor query: (batch, time, size)
:param torch.Tensor key: (batch, time, size) dummy
:param torch.Tensor value: (batch, time, size)
:param torch.Tensor mask: (batch, time, time) dummy
:return torch.Tensor: attentioned and transformed `value` (batch, time, d_model)
"""
bs, time = query.size()[: 2]
query = self.w1(query) # [B, T, d]
# [B, T, H*c] --> [B*T, H, 1, c]
weight = self.w2(torch.relu(query)).view(bs*time, self.h, 1, self.c)
value = self.w3(value) # [B, T, d]
# [B*T, c, d] --> [B*T, c, H, d_k] --> [B*T, H, c, d_k]
value_cw = chunkwise(value, (self.c-1)//2, 1, (self.c-1)//2)\
.view(bs*time, self.c, self.h, self.d_k).transpose(1, 2)
self.attn = torch.softmax(weight, dim=-1)
p_attn = self.dropout(self.attn)
x = torch.matmul(p_attn, value_cw)
x = x.contiguous().view(bs, -1, self.h*self.d_k) # [B, T, d]
x = self.w_out(x) # [B, T, d]
return x
class LocalDenseSynthesizerAttention2(nn.Module):
"""Multi-Head Local Dense Synthesizer attention layer
This implementation has higher CPU usage, but no additional GPU memory usage.
based on https://github.com/pytorch/fairseq/tree/master/fairseq
https://github.com/espnet/espnet
:param int n_head: the number of heads
:param int n_feat: the dimension of features
:param float dropout_rate: dropout rate
:param int context_size: context size
:param bool use_bias: use bias term in linear layers
"""
def __init__(self, n_head, n_feat, dropout_rate, context_size=63, use_bias=False):
super().__init__()
assert n_feat % n_head == 0
# We assume d_v always equals d_k
self.d_k = n_feat // n_head
self.h = n_head
self.c = context_size
self.w1 = nn.Linear(n_feat, n_feat, bias=use_bias)
self.w2 = nn.Linear(n_feat, n_head*self.c, bias=use_bias)
self.w3 = nn.Linear(n_feat, n_feat, bias=use_bias)
self.w_out = nn.Linear(n_feat, n_feat, bias=use_bias)
self.attn = None
self.dropout = nn.Dropout(p=dropout_rate)
def forward(self, query, key, value, mask):
"""Forward pass.
:param torch.Tensor query: (batch, time, size)
:param torch.Tensor key: (batch, time, size) dummy
:param torch.Tensor value: (batch, time, size)
:param torch.Tensor mask: (batch, time, time) dummy
:return torch.Tensor: attentioned and transformed `value` (batch, time, d_model)
"""
bs, time = query.size()[: 2]
query = self.w1(query) # [B, T, d]
# [B, T, H*c] --> [B, T, H, c] --> [B, H, T, c]
weight = self.w2(torch.relu(query)).view(bs, time, self.h, self.c).transpose(1, 2).contiguous()
scores = torch.zeros(bs * self.h * time * (time + self.c - 1), dtype=weight.dtype)
scores = scores.view(bs, self.h, time, time + self.c - 1).fill_(float("-inf"))
scores = scores.to(query.device) # [B, H, T, T+c-1]
scores.as_strided(
(bs, self.h, time, self.c),
((time + self.c - 1) * time * self.h, (time + self.c - 1) * time, time + self.c, 1)
).copy_(weight)
scores = scores.narrow(-1, int((self.c - 1) / 2), time) # [B, H, T, T]
self.attn = torch.softmax(scores, dim=-1)
p_attn = self.dropout(self.attn)
value = self.w3(value).view(bs, time, self.h, self.d_k) # [B, T, H, d_k]
value = value.transpose(1, 2).contiguous() # [B, H, T, d_k]
x = torch.matmul(p_attn, value)
x = x.transpose(1, 2).contiguous().view(bs, time, self.h*self.d_k)
x = self.w_out(x) # [B, T, d]
return x
class HybirdAttention(nn.Module):
"""Combination of MHSA and LDSA
:param int n_head: the number of head s
:param int n_feat: the number of features
:param float dropout_rate: dropout rate
:param int context_size: context size
"""
def __init__(self, n_head, n_feat, dropout_rate, context_size=15):
super(HybirdAttention, self).__init__()
self.dot_att = MultiHeadedAttention(n_head, n_feat, dropout_rate)
self.ldsa_att = LocalDenseSynthesizerAttention(n_head, n_feat, dropout_rate, context_size)
def forward(self, query, key, value, mask):
"""
:param torch.Tensor query: (batch, time1, size)
:param torch.Tensor key: (batch, time2, size)
:param torch.Tensor value: (batch, time2, size)
:param torch.Tensor mask: (batch, time1, time2)
:return torch.Tensor: attentioned and transformed `value`
"""
x = self.ldsa_att(query, key, value, mask)
x = self.dot_att(x, x, x, mask)
return x