-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_models.py
79 lines (61 loc) · 2.18 KB
/
base_models.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.transforms import CenterCrop
import torchvision
class Attention(nn.Module):
def __init__(self, emb, heads):
super().__init__()
assert emb % heads == 0, f'Embedding dimension ({emb}) should be divisible by nr. of heads ({heads})'
self.emb = emb
self.heads = heads
self.W_k = nn.Linear(emb, emb, bias=False)
self.W_q = nn.Linear(emb, emb, bias=False)
self.W_v = nn.Linear(emb, emb, bias=False)
self.W_u = nn.Linear(emb, emb)
def forward(self, X):
b, t, e = X.size()
h = self.heads
# chunksize of e, i.e. head dim
s = e // h
# query, key, value model
K = self.W_k(X)
Q = self.W_q(X)
V = self.W_v(X)
# split
K = K.view(b, t, h, s)
Q = Q.view(b, t, h, s)
V = V.view(b, t, h, s)
# prepare for dot product and scale (pbloem)
K = K.transpose(1,2).contiguous().view(b * h, t, s) / (e ** (1/4))
Q = Q.transpose(1,2).contiguous().view(b * h, t, s) / (e ** (1/4))
V = V.transpose(1,2).contiguous().view(b * h, t, s) / (e ** (1/4))
W = [email protected](1,2)
W = F.softmax(W, dim=2)
#assert W.size() == (b*h, t, t)
Y = W@V
Y = Y.view(b, h, t, s)
# re-arange and unify heads
Y = Y.transpose(1, 2).contiguous().view(b, t, s * h)
Y = self.W_u(Y)
return Y
class Transformer(nn.Module):
def __init__(self, emb=2048, heads=32,dropout=0.25,ff_hidden_mult=2):
super().__init__()
self.attention = Attention(emb, heads=heads)
self.norm1 = nn.LayerNorm(emb)
self.norm2 = nn.LayerNorm(emb)
self.ff = nn.Sequential(
nn.Linear(emb, ff_hidden_mult * emb),
nn.ReLU(inplace=True),
nn.Linear(ff_hidden_mult * emb, emb)
)
self.do = nn.Dropout(dropout)
def forward(self, x):
attended = self.attention(x)
x = self.norm1(attended + x)
x = self.do(x)
fedforward = self.ff(x)
x = self.norm2(fedforward + x)
x = self.do(x)
return x