-
Notifications
You must be signed in to change notification settings - Fork 2
/
transformer.py
313 lines (219 loc) · 9.69 KB
/
transformer.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
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
def gumbel_max(logits, dim=-1):
eps = torch.finfo(logits.dtype).tiny
gumbels = -(torch.empty_like(logits).exponential_() + eps).log()
gumbels = logits + gumbels
return gumbels.argmax(dim)
def gumbel_softmax(logits, tau=1., hard=False, dim=-1):
eps = torch.finfo(logits.dtype).tiny
gumbels = -(torch.empty_like(logits).exponential_() + eps).log()
gumbels = (logits + gumbels) / tau
y_soft = F.softmax(gumbels, dim)
if hard:
index = y_soft.argmax(dim, keepdim=True)
y_hard = torch.zeros_like(logits).scatter_(dim, index, 1.)
return y_hard - y_soft.detach() + y_soft
else:
return y_soft
def log_prob_gaussian(value, mean, std):
var = std ** 2
if isinstance(var, float):
return -0.5 * (((value - mean) ** 2) / var + math.log(var) + math.log(2 * math.pi))
else:
return -0.5 * (((value - mean) ** 2) / var + var.log() + math.log(2 * math.pi))
def conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0,
dilation=1, groups=1, bias=True, padding_mode='zeros',
weight_init='xavier'):
m = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding,
dilation, groups, bias, padding_mode)
if weight_init == 'kaiming':
nn.init.kaiming_uniform_(m.weight, nonlinearity='relu')
else:
nn.init.xavier_uniform_(m.weight)
if bias:
nn.init.zeros_(m.bias)
return m
class Conv2dBlock(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0):
super().__init__()
self.m = conv2d(in_channels, out_channels, kernel_size, stride, padding,
bias=False, weight_init='kaiming')
self.weight = nn.Parameter(torch.ones(out_channels))
self.bias = nn.Parameter(torch.zeros(out_channels))
def forward(self, x):
x = self.m(x)
return F.relu(F.group_norm(x, 1, self.weight, self.bias))
def linear(in_features, out_features, bias=True, weight_init='xavier', gain=1.):
m = nn.Linear(in_features, out_features, bias)
if weight_init == 'kaiming':
nn.init.kaiming_uniform_(m.weight, nonlinearity='relu')
else:
nn.init.xavier_uniform_(m.weight, gain)
if bias:
nn.init.zeros_(m.bias)
return m
def gru_cell(input_size, hidden_size, bias=True):
m = nn.GRUCell(input_size, hidden_size, bias)
nn.init.xavier_uniform_(m.weight_ih)
nn.init.orthogonal_(m.weight_hh)
if bias:
nn.init.zeros_(m.bias_ih)
nn.init.zeros_(m.bias_hh)
return m
class MultiHeadAttention(nn.Module):
def __init__(self, d_model, num_heads, dropout=0., gain=1.):
super().__init__()
assert d_model % num_heads == 0, "d_model must be divisible by num_heads"
self.d_model = d_model
self.num_heads = num_heads
self.attn_dropout = nn.Dropout(dropout)
self.output_dropout = nn.Dropout(dropout)
self.proj_q = linear(d_model, d_model, bias=False)
self.proj_k = linear(d_model, d_model, bias=False)
self.proj_v = linear(d_model, d_model, bias=False)
self.proj_o = linear(d_model, d_model, bias=False, gain=gain)
def forward(self, q, k, v, attn_mask=None):
"""
q: batch_size x target_len x d_model
k: batch_size x source_len x d_model
v: batch_size x source_len x d_model
attn_mask: target_len x source_len
return: batch_size x target_len x d_model
"""
B, T, _ = q.shape
_, S, _ = k.shape
q = self.proj_q(q).view(B, T, self.num_heads, -1).transpose(1, 2)
k = self.proj_k(k).view(B, S, self.num_heads, -1).transpose(1, 2)
v = self.proj_v(v).view(B, S, self.num_heads, -1).transpose(1, 2)
q = q * (q.shape[-1] ** (-0.5))
attn = torch.matmul(q, k.transpose(-1, -2))
if attn_mask is not None:
attn = attn.masked_fill(attn_mask, float('-inf'))
attn = F.softmax(attn, dim=-1)
attn = self.attn_dropout(attn)
output = torch.matmul(attn, v).transpose(1, 2).reshape(B, T, -1)
output = self.proj_o(output)
output = self.output_dropout(output)
return output
class PositionalEncoding(nn.Module):
def __init__(self, max_len, d_model, dropout=0.1):
super().__init__()
self.dropout = nn.Dropout(dropout)
self.pe = nn.Parameter(torch.zeros(1, max_len, d_model), requires_grad=True)
nn.init.trunc_normal_(self.pe)
def forward(self, input):
"""
input: batch_size x seq_len x d_model
return: batch_size x seq_len x d_model
"""
T = input.shape[1]
return self.dropout(input + self.pe[:, :T])
class TransformerEncoderBlock(nn.Module):
def __init__(self, d_model, num_heads, dropout=0., gain=1., is_first=False):
super().__init__()
self.is_first = is_first
self.attn_layer_norm = nn.LayerNorm(d_model)
self.attn = MultiHeadAttention(d_model, num_heads, dropout, gain)
self.ffn_layer_norm = nn.LayerNorm(d_model)
self.ffn = nn.Sequential(
linear(d_model, 4 * d_model, weight_init='kaiming'),
nn.ReLU(),
linear(4 * d_model, d_model, gain=gain),
nn.Dropout(dropout))
def forward(self, input):
"""
input: batch_size x source_len x d_model
return: batch_size x source_len x d_model
"""
if self.is_first:
input = self.attn_layer_norm(input)
x = self.attn(input, input, input)
input = input + x
else:
x = self.attn_layer_norm(input)
x = self.attn(x, x, x)
input = input + x
x = self.ffn_layer_norm(input)
x = self.ffn(x)
return input + x
class TransformerEncoder(nn.Module):
def __init__(self, num_blocks, d_model, num_heads, dropout=0.):
super().__init__()
if num_blocks > 0:
gain = (2 * num_blocks) ** (-0.5)
self.blocks = nn.ModuleList(
[TransformerEncoderBlock(d_model, num_heads, dropout, gain, is_first=True)] +
[TransformerEncoderBlock(d_model, num_heads, dropout, gain, is_first=False)
for _ in range(num_blocks - 1)])
else:
self.blocks = nn.ModuleList()
self.layer_norm = nn.LayerNorm(d_model)
def forward(self, input):
"""
input: batch_size x source_len x d_model
return: batch_size x source_len x d_model
"""
for block in self.blocks:
input = block(input)
return self.layer_norm(input)
class TransformerDecoderBlock(nn.Module):
def __init__(self, max_len, d_model, num_heads, dropout=0., gain=1., is_first=False):
super().__init__()
self.is_first = is_first
self.self_attn_layer_norm = nn.LayerNorm(d_model)
self.self_attn = MultiHeadAttention(d_model, num_heads, dropout, gain)
mask = torch.triu(torch.ones((max_len, max_len), dtype=torch.bool), diagonal=1)
self.self_attn_mask = nn.Parameter(mask, requires_grad=False)
self.encoder_decoder_attn_layer_norm = nn.LayerNorm(d_model)
self.encoder_decoder_attn = MultiHeadAttention(d_model, num_heads, dropout, gain)
self.ffn_layer_norm = nn.LayerNorm(d_model)
self.ffn = nn.Sequential(
linear(d_model, 4 * d_model, weight_init='kaiming'),
nn.ReLU(),
linear(4 * d_model, d_model, gain=gain),
nn.Dropout(dropout))
def forward(self, input, encoder_output):
"""
input: batch_size x target_len x d_model
encoder_output: batch_size x source_len x d_model
return: batch_size x target_len x d_model
"""
T = input.shape[1]
if self.is_first:
input = self.self_attn_layer_norm(input)
x = self.self_attn(input, input, input, self.self_attn_mask[:T, :T])
input = input + x
else:
x = self.self_attn_layer_norm(input)
x = self.self_attn(x, x, x, self.self_attn_mask[:T, :T])
input = input + x
x = self.encoder_decoder_attn_layer_norm(input)
x = self.encoder_decoder_attn(x, encoder_output, encoder_output)
input = input + x
x = self.ffn_layer_norm(input)
x = self.ffn(x)
return input + x
class TransformerDecoder(nn.Module):
def __init__(self, num_blocks, max_len, d_model, num_heads, dropout=0.):
super().__init__()
if num_blocks > 0:
gain = (3 * num_blocks) ** (-0.5)
self.blocks = nn.ModuleList(
[TransformerDecoderBlock(max_len, d_model, num_heads, dropout, gain, is_first=True)] +
[TransformerDecoderBlock(max_len, d_model, num_heads, dropout, gain, is_first=False)
for _ in range(num_blocks - 1)])
else:
self.blocks = nn.ModuleList()
self.layer_norm = nn.LayerNorm(d_model)
def forward(self, input, encoder_output):
"""
input: batch_size x target_len x d_model
encoder_output: batch_size x source_len x d_model
return: batch_size x target_len x d_model
"""
for block in self.blocks:
input = block(input, encoder_output)
return self.layer_norm(input)