-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformer.py
437 lines (369 loc) · 14.3 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
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
# Pytorch transformers from scratch (Aladdin Persson)
# https://www.youtube.com/watch?v=U0s0f995w14&t=2735s
# questions, uncertain stuffs
# 1. Why transformer block takes the same x as k q v
# 2. Why tgt_mask has different shape from src_mask
# 3. What's the shape of each tensor in each stage (Tensor shape analysis)
# 4. is it the same logic to implement tgt_pad_idx logic, as it is not implemented
# Some Basics
# torch.nn building blocks
# https://pytorch.org/docs/stable/nn.html
# Containers: Module, ModuleList, Sequential
# Convolution Layers: Conv2d
# Pooling Layers: MaxPool2d
# Activations: ReLU
# Normalization Layers: LayerNorm
# Linear Layer: Linear
# Dropout Layers: Dropout
# Sparse Layers: Embedding
# https://pytorch.org/docs/stable/torch.html
# Tensors:
# numel()
# Creation Ops:
# tensor(), arange(), randn(), ones(), zeros(), from_numpy()
# Indexing, Slicing, Joining, Mutating Ops:
# cat(), squeeze(), unsqueeze(), transpose(), stack()
# BLAS and LAPACK Operations:
# mm(), bmm(), matmul()
# Other Operations:
# tril(), triu(), einsum()
# torch.sparse.softmax()
#torch.Tensor
# descriptors
# shape, numel(), size(), type(), dim()
# shaping
# reshape(), squeeze(), unsqueeze(), transpose(), select(), expand(), permute()
# conversion
# to(), numpy()
# stats
# sum()
# misc
# masked_fill()
#pil conversions
# torchvision.transforms.functional
# to_pil_image(), to_tensor()
# Class dependencies
#
# Transformer
# @Encoder, @Decoder
# / \
# Encoder / Decoder
# @TransformerBlock x N @DecoderBlock x N
# Embedding, Dropout Embedding, Linear, Dropout
# | |
# | DecoderBlock
# | / @SelfAttention, @TransformerBlock
# | /------/ LayerNorm, dropout
# | / |
# TransformerBlock |
# @SelfAttention |
# LayerNorm, Linear, Dropout, ReLU /
# \ /
# \ /
# SelfAttention
# Linear
import torch
import torch.nn as nn
class SelfAttention(nn.Module):
def __init__(self, embed_size, heads):
super(SelfAttention, self).__init__()
self.embed_size = embed_size
self.heads = heads
self.head_dim = embed_size // heads
assert (self.head_dim * heads == embed_size), "Embed size need to be div by heads"
# torch.nn.Linear Documentation
# bias – If set to False, the layer will not learn an additive bias
# y = x * A.transpose
# https://pytorch.org/docs/stable/generated/torch.nn.Linear.html
self.values = nn.Linear(self.head_dim, self.head_dim, bias=False)
self.keys = nn.Linear(self.head_dim, self.head_dim, bias=False)
self.queries = nn.Linear(self.head_dim, self.head_dim, bias=False)
self.fc_out = nn.Linear(heads*self.head_dim, embed_size)
def forward(self, values, keys, queries, mask):
N = queries.shape[0] # How many example to send in the same time
value_len, key_len, query_len = values.shape[1], keys.shape[1], queries.shape[1]
# values, keys: [N, src_len, embed_size]
# queries: [N, src_len/tgt_len, embed_size]
# mask: [N, 1, 1, src_len] or [N, 1, tgt_len, tgt_len]
# Split embedding into self.heads pieces
values = values.reshape(N, value_len, self.heads, self.head_dim)
keys = keys.reshape(N, key_len, self.heads, self.head_dim)
queries = queries.reshape(N, query_len, self.heads, self.head_dim)
# Einsum is all you need (Aladdin Persson)
# https://www.youtube.com/watch?v=pkVwUVEHmfI
# Exact explanations of einsum
# Flatten, Reshape, and Squeeze Explained (deeplizard)
# https://www.youtube.com/watch?v=fCVuiW9AFzY&t=84s
# Pytorch permute函数 (胡孟)
# https://zhuanlan.zhihu.com/p/76583143
values = self.values(values)
keys = self.keys(keys)
queries = self.queries(queries)
# values, keys: [N, src_len, heads, head_dim]
# queries: [N, src_len/tgt_len, heads, head_dim]
# notice in following matrix multiply(bmm)
# shape of q != shape of v/k (not necessarily)
# 此处energy就是公式里的 $QK^T$
# energy calculation
# queries shape: (N, query_len, heads, head_dim)
# keys shape: (N, query_len, heads, head_dim)
# energy shape: (N, heads, query_len, key_len)
energy = torch.einsum("nqhd,nkhd->nhqk", [queries, keys])
# 等价于 torch.matmul(queries, keys.transpose(-2, -1))
# How to code The Transformer in Pytorch 中的 score有多种意思, 这个变量被反复重定义了
# 在最外层的scores说的是attention计算得到的值
# https://towardsdatascience.com/how-to-code-the-transformer-in-pytorch-24db27c8f9ec
if mask is not None:
# Here, mask is a tensor with the same shape as energy
# masked_fill method, explains how it works
# https://programmersought.com/article/68273730436/
# replace mask==0, where element is True, to -1e20
energy = energy.masked_fill(mask==0, float("-1e20"))
# $Attention(Q, K, V)=softmax(\frac{QK^T}{\sqrt{d_k}})V$
# normalizes values along axis 3 (head_dim), within each head
attention = torch.softmax(energy / (self.embed_size ** (1/2)), dim=3)
out = torch.einsum("nhql,nlhd->nqhd",[attention, values]).reshape(
N, query_len, self.heads*self.head_dim
)
# attention shape: (N, heads, query_len, key_len)
# values shape: (N, value_len, heads, head_dim)
# (N, query_len, heads, head_dim)
# after einsum, then flattern last 2 dim
out = self.fc_out(out)
return out
class TransformerBlock(nn.Module):
def __init__(self, embed_size, heads, dropout, forward_expansion):
super(TransformerBlock, self).__init__()
self.attention = SelfAttention(embed_size, heads)
# https://pytorch.org/docs/stable/generated/torch.nn.LayerNorm.html
# https://arxiv.org/pdf/1607.06450.pdf
# $ y = \frac{x-E[x]}{\sqrt{Var[x]+\epsilon }} * \gamma + \beta $
# Normalization Techniques in Deep Neural Networks (Aakash Bindal)
# https://medium.com/techspace-usict/normalization-techniques-in-deep-neural-networks-9121bf100d8
#
# refers to an image of summary of all normalization techniques
# https://miro.medium.com/max/3000/1*r0HM4TvZvvceXcJIpDJmDQ.png
self.norm1 = nn.LayerNorm(embed_size)
self.norm2 = nn.LayerNorm(embed_size)
# feed_forward is a simple magnify and shrink process
# with a relu activation in the middle
# 本例中forward_expansion 4倍
self.feed_forward = nn.Sequential(
nn.Linear(embed_size, forward_expansion*embed_size),
nn.ReLU(),
nn.Linear(forward_expansion*embed_size, embed_size)
)
self.dropout = nn.Dropout(dropout)
def forward(self, value, key, query, mask):
# value, key: [N, src_len, embed_size]
# query: [N, src_len/tgt_len, embed_size]
# mask: [N, 1, 1, src_len]
attention = self.attention(value, key, query, mask)
# attention: [N, src_len/tgt_len, embed_size]
x = self.dropout(self.norm1(attention + query))
# x: [N, src_len/tgt_len, embed_size]
forward = self.feed_forward(x)
# forward: [N, src_len/tgt_len, embed_size]
out = self.dropout(self.norm2(forward+x))
# out: [N, src_len/tgt_len, embed_size]
return out
class Encoder(nn.Module):
def __init__(
self,
src_vocab_size,
embed_size,
num_layers,
heads,
device,
forward_expansion,
dropout,
max_length
):
super(Encoder, self).__init__()
self.embed_size = embed_size
self.device = device
self.word_embedding = nn.Embedding(src_vocab_size, embed_size)
self.position_embedding = nn.Embedding(max_length, embed_size)
self.layers = nn.ModuleList(
[
TransformerBlock(
embed_size,
heads,
dropout=dropout,
forward_expansion=forward_expansion
) for _ in range(num_layers)
]
)
self.dropout = nn.Dropout(dropout)
def forward(self, x, mask):
# x: [N, src_len]
# mask: [N, 1, 1, src_len]
N, seq_length = x.shape
# here, just a naive implementation for position embedding
# create [0, ... , seq_length] x N, then look up in the self.position_embedding
# https://pytorch.org/docs/stable/generated/torch.nn.Embedding.html
# As the position_embedding.weight requires_grad == True (by default)
# it will be updated along with the training process
positions = torch.arange(0, seq_length).expand(N, seq_length).to(self.device)
# positions: [N, src_len]
# in original paper, using $PE_{(pos, 2i+1)} = cos(pos/10000^(2i/d_model))$
# https://towardsdatascience.com/how-to-code-the-transformer-in-pytorch-24db27c8f9ec
# pe = torch.zeros(max_seq_len, d_model)
# for pos in range(max_seq_len):
# for i in range(0, d_model, 2):
# pe[pos, i] = math.sin(pos / (10000 ** ((2 * i)/d_model)))
# pe[pos, i + 1] = math.cos(pos / (10000 ** ((2 * (i + 1))/d_model)))
# pe = pe.unsqueeze(0)
# in forward(), actually, make embeddings relatively larger
# x = x * math.sqrt(self.d_model)
out = self.dropout(self.word_embedding(x) + self.position_embedding(positions))
# out: [N, src_len, embed_size])
for layer in self.layers:
out = layer(out, out, out, mask)
# out: [N, src_len, embed_size])
return out
class DecoderBlock(nn.Module):
def __init__(
self,
embed_size,
heads,
forward_expansion,
dropout,
device
):
super(DecoderBlock, self).__init__()
self.attention = SelfAttention(embed_size, heads)
self.norm = nn.LayerNorm(embed_size)
self.transformer_block = TransformerBlock(
embed_size, heads, dropout, forward_expansion
)
self.dropout = nn.Dropout(dropout)
def forward(self, x, value ,key, src_mask, tgt_mask):
# x: [N, tgt_len, embed_size]), src_mask: [N, 1, 1, src_len]
# value, key: [N, src_len, embed_size]
# tgt_mask: [N, 1, tgt_len, tgt_len]
attention = self.attention(x, x, x, tgt_mask)
query = self.dropout(self.norm(attention +x))
out = self.transformer_block(value, key, query, src_mask)
# attention, query, out: [N, tgt_len, embed_size]
return out
class Decoder(nn.Module):
def __init__(self,
tgt_vocab_size,
embed_size,
num_layers,
heads,
forward_expansion,
dropout,
device,
max_length):
super(Decoder, self).__init__()
self.device = device
self.word_embedding = nn.Embedding(tgt_vocab_size, embed_size)
self.position_embedding = nn.Embedding(max_length, embed_size)
self.layers = nn.ModuleList(
[
DecoderBlock(embed_size, heads, forward_expansion, dropout, device)
for _ in range(num_layers)
]
)
self.fc_out = nn.Linear(embed_size, tgt_vocab_size)
self.dropout = nn.Dropout(dropout)
def forward(self, x, enc_out, src_mask, tgt_mask):
N, seq_length = x.shape
positions = torch.arange(0, seq_length).expand(N, seq_length).to(self.device)
# positions: [N, tgt_len]
x = self.dropout(self.word_embedding(x) + self.position_embedding(positions))
# x: [N, tgt_len, embed_size]
for layer in self.layers:
x = layer(x, enc_out, enc_out, src_mask, tgt_mask)
# x: [N, tgt_len, embed_size]
out = self.fc_out(x)
# out: [N, tgt_len, tgt_vocab_size]
return out
class Transformer(nn.Module):
def __init__(self,
src_vocab_size,
tgt_vocab_size,
src_pad_idx,
tgt_pad_idx,
embed_size=256,
num_layers = 6,
forward_expansion=4,
heads=8,
dropout=0,
device="cpu",
max_length=100
):
super(Transformer, self).__init__()
self.encoder = Encoder(
src_vocab_size,
embed_size,
num_layers,
heads,
device,
forward_expansion,
dropout,
max_length
)
self.decoder = Decoder(
tgt_vocab_size,
embed_size,
num_layers,
heads,
forward_expansion,
dropout,
device,
max_length
)
self.src_pad_idx = src_pad_idx
self.tgt_pad_idx = tgt_pad_idx # this is never used ...
self.device = device
def make_src_mask(self, src):
src_mask = (src!= self.src_pad_idx).unsqueeze(1).unsqueeze(2)
# (N, 1, 1, src_len)
return src_mask.to(self.device)
def make_tgt_mask(self, tgt):
N, tgt_len = tgt.shape
# lower triangular
# https://pytorch.org/docs/stable/generated/torch.tril.html
# [tgt_len, tgt_len] => expand => [N, 1, tgt_len, tgt_len]
tgt_mask = torch.tril(torch.ones((tgt_len, tgt_len))).expand(
N, 1, tgt_len, tgt_len
)
# (N, 1, tgt_len, tgt_len)
# Transformer in 5 minutes (Blue Season)
# https://blue-season.github.io/transformer-in-5-minutes/
# target mask is also called "Look-ahead Mask"
# To ensure causality, a mask is used to prevent the future leaks into the past
# Following image explains how it works, so basically it is a lower triangular matrix structure
# up/right is the future index direction
# https://blue-season.github.io/images/2019-09-08/causal-mask.png
return tgt_mask.to(self.device)
def forward(self, src, tgt):
# src: [N, src_len], target: [N, tgt_len]
src_mask = self.make_src_mask(src)
tgt_mask = self.make_tgt_mask(tgt)
# src_mask [2, 1, 1, src_len], tgt_mask: [N, 1, tgt_len, tgt_len]
enc_src = self.encoder(src, src_mask)
out = self.decoder(tgt, enc_src, src_mask, tgt_mask)
# enc_src: [N, src_len, embed_size]
# out: [N, tgt_len, tgt_vocab_size]
return out
if __name__ == "__main__":
device = "cpu"
x = torch.tensor([
[1,5,6,4,3,9,5,2,0],
[1,8,7,3,4,5,6,7,2],
]).to(device)
tgt = torch.tensor([
[1,7,4,3,5,9,2,0],
[1,5,6,2,4,7,6,2],
]).to(device)
src_pad_idx =0
tgt_pad_idx =0
src_vocab_size=10
tgt_vocab_size=10
model = Transformer(src_vocab_size, tgt_vocab_size, src_pad_idx, tgt_pad_idx).to(device)
out = model(x, tgt[:,:-1])
print(out.shape)