-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.py
486 lines (409 loc) · 17.3 KB
/
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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
"""
Self-attention layers and other utilities from self-attentive-parser
https://github.com/nikitakit/self-attentive-parser
"""
"""
MIT License
Copyright (c) 2017-2018 Nikita Kitaev
Copyright (c) 2017 Victor Huang
Copyright (c) 2017 Mitchell Stern
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import torch
import torch.nn as nn
import numpy as np
from numpy.typing import ArrayLike
from typing import List, Optional, Tuple, Any, Union
class BatchIndices:
"""
Batch indices container class (used to implement packed batches)
"""
def __init__(self, batch_idxs_arr: Union[ArrayLike, torch.Tensor]) -> None:
if torch.is_tensor(batch_idxs_arr): # type: ignore
self.batch_idxs_np = batch_idxs_arr.cpu().numpy() # type: ignore
self.batch_idxs_torch = batch_idxs_arr
else:
self.batch_idxs_np = batch_idxs_arr
self.batch_idxs_torch = torch.from_numpy(batch_idxs_arr)
self.batch_size = int(1 + np.max(self.batch_idxs_np))
batch_idxs_np_extra = np.concatenate([[-1], self.batch_idxs_np, [-1]])
self.boundaries_np = np.nonzero(
batch_idxs_np_extra[1:] != batch_idxs_np_extra[:-1]
)[0]
self.seq_lens_np = self.boundaries_np[1:] - self.boundaries_np[:-1]
assert len(self.seq_lens_np) == self.batch_size
self.max_len = int(np.max(self.boundaries_np[1:] - self.boundaries_np[:-1]))
@staticmethod
def from_lens(lens: List[int]) -> "BatchIndices":
batch_idxs = np.zeros(np.sum(lens), dtype=np.int64)
base = 0
for i, l in enumerate(lens):
batch_idxs[base : base + l] = i
base += l
return BatchIndices(batch_idxs)
def inflate(self, flattened: torch.Tensor, fill_value: float = 0.0) -> torch.Tensor:
padded = flattened.new_full(
(len(self.seq_lens_np), self.max_len, flattened.size(-1)),
fill_value=fill_value,
)
base = 0
for i, l in enumerate(self.seq_lens_np):
padded[i, :l] = flattened[base : base + l]
base += l
return padded
class FeatureDropoutFunction(torch.autograd.function.InplaceFunction):
@classmethod
def forward( # type: ignore
cls: Any,
ctx: Any,
input: torch.Tensor,
batch_idxs: BatchIndices,
p: float = 0.5,
train: bool = False,
inplace: bool = False,
) -> torch.Tensor:
if p < 0 or p > 1:
raise ValueError(
"dropout probability has to be between 0 and 1, " "but got {}".format(p)
)
ctx.p = p
ctx.train = train
ctx.inplace = inplace
if ctx.inplace:
ctx.mark_dirty(input)
output = input
else:
output = input.clone()
if ctx.p > 0 and ctx.train:
ctx.noise = input.new().resize_(batch_idxs.batch_size, input.size(1))
if ctx.p == 1:
ctx.noise.fill_(0)
else:
ctx.noise.bernoulli_(1 - ctx.p).div_(1 - ctx.p)
ctx.noise = ctx.noise[batch_idxs.batch_idxs_torch, :]
output.mul_(ctx.noise)
return output
@staticmethod
def backward(ctx: Any, grad_output: Any) -> Any: # type: ignore
if ctx.p > 0 and ctx.train:
return grad_output.mul(ctx.noise), None, None, None, None
else:
return grad_output, None, None, None, None
class FeatureDropout(nn.Module):
"""
Feature-level dropout: takes an input of size len x num_features and drops
each feature with probabibility p. A feature is dropped across the full
portion of the input that corresponds to a single batch element.
"""
def __init__(self, p: float = 0.5, inplace: bool = False) -> None:
super().__init__()
if p < 0 or p > 1:
raise ValueError(
"dropout probability has to be between 0 and 1, " "but got {}".format(p)
)
self.p = p
self.inplace = inplace
def forward(self, input: torch.Tensor, batch_idxs: BatchIndices) -> torch.Tensor:
return FeatureDropoutFunction.apply( # type: ignore
input, batch_idxs, self.p, self.training, self.inplace
)
class LayerNormalization(nn.Module):
def __init__(self, d_hid: int, eps: float = 1e-3, affine: bool = True) -> None:
super(LayerNormalization, self).__init__()
self.eps = eps
self.affine = affine
if self.affine:
self.a_2 = nn.Parameter(torch.ones(d_hid), requires_grad=True)
self.b_2 = nn.Parameter(torch.zeros(d_hid), requires_grad=True)
def forward(self, z: torch.Tensor) -> torch.Tensor:
if z.size(-1) == 1:
return z
mu = torch.mean(z, keepdim=True, dim=-1)
sigma = torch.std(z, keepdim=True, dim=-1)
ln_out = (z - mu.expand_as(z)) / (sigma.expand_as(z) + self.eps)
if self.affine:
ln_out = ln_out * self.a_2.expand_as(ln_out) + self.b_2.expand_as(ln_out)
# NOTE(nikita): the t2t code does the following instead, with eps=1e-6
# However, I currently have no reason to believe that this difference in
# implementation matters.
# mu = torch.mean(z, keepdim=True, dim=-1)
# variance = torch.mean((z - mu.expand_as(z))**2, keepdim=True, dim=-1)
# ln_out = (z - mu.expand_as(z)) * torch.rsqrt(variance + self.eps).expand_as(z)
# ln_out = ln_out * self.a_2.expand_as(ln_out) + self.b_2.expand_as(ln_out)
return ln_out
class ScaledDotProductAttention(nn.Module):
def __init__(self, d_model: int, attention_dropout: float = 0.1) -> None:
super(ScaledDotProductAttention, self).__init__()
self.temper = d_model**0.5
self.dropout = nn.Dropout(attention_dropout)
self.softmax = nn.Softmax(dim=-1)
def forward(
self,
q: torch.Tensor,
k: torch.Tensor,
v: torch.Tensor,
attn_mask: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor, torch.Tensor]:
# q: [batch, slot, feat]
# k: [batch, slot, feat]
# v: [batch, slot, feat]
attn = torch.bmm(q, k.transpose(1, 2)) / self.temper
if attn_mask is not None:
assert attn_mask.size() == attn.size(), (
"Attention mask shape {} mismatch "
"with Attention logit tensor shape "
"{}.".format(attn_mask.size(), attn.size())
)
attn.data.masked_fill_(attn_mask, -float("inf"))
attn = self.softmax(attn)
# Note that this makes the distribution not sum to 1. At some point it
# may be worth researching whether this is the right way to apply
# dropout to the attention.
# Note that the t2t code also applies dropout in this manner
attn = self.dropout(attn)
output = torch.bmm(attn, v)
return output, attn
class MultiHeadAttention(nn.Module):
"""
Multi-head attention module
"""
def __init__(
self,
n_head: int,
d_model: int,
d_k: int,
d_v: int,
residual_dropout: float = 0.1,
attention_dropout: float = 0.1,
d_positional: Optional[int] = None,
) -> None:
super(MultiHeadAttention, self).__init__()
self.n_head = n_head
self.d_k = d_k
self.d_v = d_v
if d_positional is None:
self.partitioned = False
self.w_qs = nn.Parameter(torch.FloatTensor(n_head, d_model, d_k))
self.w_ks = nn.Parameter(torch.FloatTensor(n_head, d_model, d_k))
self.w_vs = nn.Parameter(torch.FloatTensor(n_head, d_model, d_v))
nn.init.xavier_normal_(self.w_qs)
nn.init.xavier_normal_(self.w_ks)
nn.init.xavier_normal_(self.w_vs)
else:
self.partitioned = True
self.d_content = d_model - d_positional
self.d_positional = d_positional
self.w_qs1 = nn.Parameter(
torch.FloatTensor(n_head, self.d_content, d_k // 2)
)
self.w_ks1 = nn.Parameter(
torch.FloatTensor(n_head, self.d_content, d_k // 2)
)
self.w_vs1 = nn.Parameter(
torch.FloatTensor(n_head, self.d_content, d_v // 2)
)
self.w_qs2 = nn.Parameter(
torch.FloatTensor(n_head, self.d_positional, d_k // 2)
)
self.w_ks2 = nn.Parameter(
torch.FloatTensor(n_head, self.d_positional, d_k // 2)
)
self.w_vs2 = nn.Parameter(
torch.FloatTensor(n_head, self.d_positional, d_v // 2)
)
nn.init.xavier_normal_(self.w_qs1)
nn.init.xavier_normal_(self.w_ks1)
nn.init.xavier_normal_(self.w_vs1)
nn.init.xavier_normal_(self.w_qs2)
nn.init.xavier_normal_(self.w_ks2)
nn.init.xavier_normal_(self.w_vs2)
self.attention = ScaledDotProductAttention(
d_model, attention_dropout=attention_dropout
)
self.layer_norm = LayerNormalization(d_model)
if not self.partitioned:
# The lack of a bias term here is consistent with the t2t code, though
# in my experiments I have never observed this making a difference.
self.proj = nn.Linear(n_head * d_v, d_model, bias=False)
else:
self.proj1 = nn.Linear(n_head * (d_v // 2), self.d_content, bias=False)
self.proj2 = nn.Linear(n_head * (d_v // 2), self.d_positional, bias=False)
self.residual_dropout = FeatureDropout(residual_dropout)
def split_qkv_packed(
self, inp: torch.Tensor, qk_inp: Optional[torch.Tensor] = None
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
v_inp_repeated = inp.repeat(self.n_head, 1).view(
self.n_head, -1, inp.size(-1)
) # n_head x len_inp x d_model
if qk_inp is None:
qk_inp_repeated = v_inp_repeated
else:
qk_inp_repeated = qk_inp.repeat(self.n_head, 1).view(
self.n_head, -1, qk_inp.size(-1)
)
if not self.partitioned:
# n_head x len_inp x d_k
q_s = torch.bmm(qk_inp_repeated, self.w_qs)
# n_head x len_inp x d_k
k_s = torch.bmm(qk_inp_repeated, self.w_ks)
# n_head x len_inp x d_v
v_s = torch.bmm(v_inp_repeated, self.w_vs)
else:
q_s = torch.cat(
[
torch.bmm(qk_inp_repeated[:, :, : self.d_content], self.w_qs1),
torch.bmm(qk_inp_repeated[:, :, self.d_content :], self.w_qs2),
],
-1,
)
k_s = torch.cat(
[
torch.bmm(qk_inp_repeated[:, :, : self.d_content], self.w_ks1),
torch.bmm(qk_inp_repeated[:, :, self.d_content :], self.w_ks2),
],
-1,
)
v_s = torch.cat(
[
torch.bmm(v_inp_repeated[:, :, : self.d_content], self.w_vs1),
torch.bmm(v_inp_repeated[:, :, self.d_content :], self.w_vs2),
],
-1,
)
return q_s, k_s, v_s
def pad_and_rearrange(
self,
q_s: torch.Tensor,
k_s: torch.Tensor,
v_s: torch.Tensor,
batch_idxs: BatchIndices,
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
# Input is padded representation: n_head x len_inp x d
# Output is packed representation: (n_head * mb_size) x len_padded x d
# (along with masks for the attention and output)
n_head = self.n_head
d_k, d_v = self.d_k, self.d_v
len_padded = batch_idxs.max_len
mb_size = batch_idxs.batch_size
q_padded = q_s.new_zeros((n_head, mb_size, len_padded, d_k))
k_padded = k_s.new_zeros((n_head, mb_size, len_padded, d_k))
v_padded = v_s.new_zeros((n_head, mb_size, len_padded, d_v))
invalid_mask = q_s.new_ones((mb_size, len_padded), dtype=torch.bool)
for i, (start, end) in enumerate(
zip(batch_idxs.boundaries_np[:-1], batch_idxs.boundaries_np[1:])
):
q_padded[:, i, : end - start, :] = q_s[:, start:end, :]
k_padded[:, i, : end - start, :] = k_s[:, start:end, :]
v_padded[:, i, : end - start, :] = v_s[:, start:end, :]
invalid_mask[i, : end - start].fill_(False)
return (
q_padded.view(-1, len_padded, d_k),
k_padded.view(-1, len_padded, d_k),
v_padded.view(-1, len_padded, d_v),
invalid_mask.unsqueeze(1)
.expand(mb_size, len_padded, len_padded)
.repeat(n_head, 1, 1),
(~invalid_mask).repeat(n_head, 1),
)
def combine_v(self, outputs: torch.Tensor) -> torch.Tensor:
# Combine attention information from the different heads
n_head = self.n_head
outputs = outputs.view(n_head, -1, self.d_v)
if not self.partitioned:
# Switch from n_head x len_inp x d_v to len_inp x (n_head * d_v)
outputs = (
torch.transpose(outputs, 0, 1).contiguous().view(-1, n_head * self.d_v)
)
# Project back to residual size
outputs = self.proj(outputs)
else:
d_v1 = self.d_v // 2
outputs1 = outputs[:, :, :d_v1]
outputs2 = outputs[:, :, d_v1:]
outputs1 = (
torch.transpose(outputs1, 0, 1).contiguous().view(-1, n_head * d_v1)
)
outputs2 = (
torch.transpose(outputs2, 0, 1).contiguous().view(-1, n_head * d_v1)
)
outputs = torch.cat(
[
self.proj1(outputs1),
self.proj2(outputs2),
],
-1,
)
return outputs
def forward(
self,
inp: torch.Tensor,
batch_idxs: BatchIndices,
qk_inp: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor, torch.Tensor]:
residual = inp
# While still using a packed representation, project to obtain the
# query/key/value for each head
q_s, k_s, v_s = self.split_qkv_packed(inp, qk_inp=qk_inp)
# Switch to padded representation, perform attention, then switch back
q_padded, k_padded, v_padded, attn_mask, output_mask = self.pad_and_rearrange(
q_s, k_s, v_s, batch_idxs
)
outputs_padded, attns_padded = self.attention(
q_padded,
k_padded,
v_padded,
attn_mask=attn_mask,
)
outputs = outputs_padded[output_mask]
outputs = self.combine_v(outputs)
outputs = self.residual_dropout(outputs, batch_idxs)
return self.layer_norm(outputs + residual), attns_padded
class PartitionedPositionwiseFeedForward(nn.Module):
def __init__(
self,
d_hid: int,
d_ff: int,
d_positional: int,
relu_dropout: float = 0.1,
residual_dropout: float = 0.1,
) -> None:
super().__init__()
self.d_content = d_hid - d_positional
self.w_1c = nn.Linear(self.d_content, d_ff // 2)
self.w_1p = nn.Linear(d_positional, d_ff // 2)
self.w_2c = nn.Linear(d_ff // 2, self.d_content)
self.w_2p = nn.Linear(d_ff // 2, d_positional)
self.layer_norm = LayerNormalization(d_hid)
# The t2t code on github uses relu dropout, even though the transformer
# paper describes residual dropout only. We implement relu dropout
# because we always have the option to set it to zero.
self.relu_dropout = FeatureDropout(relu_dropout)
self.residual_dropout = FeatureDropout(residual_dropout)
self.relu = nn.ReLU()
def forward(self, x: torch.Tensor, batch_idxs: BatchIndices) -> torch.Tensor:
residual = x
xc = x[:, : self.d_content]
xp = x[:, self.d_content :]
outputc = self.w_1c(xc)
outputc = self.relu_dropout(self.relu(outputc), batch_idxs)
outputc = self.w_2c(outputc)
outputp = self.w_1p(xp)
outputp = self.relu_dropout(self.relu(outputp), batch_idxs)
outputp = self.w_2p(outputp)
output = torch.cat([outputc, outputp], -1)
output = self.residual_dropout(output, batch_idxs)
return self.layer_norm(output + residual) # type: ignore