-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
202 lines (157 loc) · 6.56 KB
/
model.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class RMSNorm(nn.Module):
def __init__(self, d, p=-1., eps=1e-8, bias=False):
# https://github.com/bzhangGo/rmsnorm/blob/master/rmsnorm_torch.py
"""
Root Mean Square Layer Normalization
:param d: model size
:param p: partial RMSNorm, valid value [0, 1], default -1.0 (disabled)
:param eps: epsilon value, default 1e-8
:param bias: whether use bias term for RMSNorm, disabled by
default because RMSNorm doesn't enforce re-centering invariance.
"""
super(RMSNorm, self).__init__()
self.eps = eps
self.d = d
self.p = p
self.bias = bias
self.scale = nn.Parameter(torch.ones(d))
self.register_parameter("scale", self.scale)
if self.bias:
self.offset = nn.Parameter(torch.zeros(d))
self.register_parameter("offset", self.offset)
def forward(self, x):
if self.p < 0. or self.p > 1.:
norm_x = x.norm(2, dim=-1, keepdim=True)
d_x = self.d
else:
partial_size = int(self.d * self.p)
partial_x, _ = torch.split(x, [partial_size, self.d - partial_size], dim=-1)
norm_x = partial_x.norm(2, dim=-1, keepdim=True)
d_x = partial_size
rms_x = norm_x * d_x ** (-1. / 2)
x_normed = x / (rms_x + self.eps)
if self.bias:
return self.scale * x_normed + self.offset
return self.scale * x_normed
class MultiheadAttention(nn.Module):
def __init__(self, dim, n_heads, dropout=0.):
super().__init__()
self.dim = dim
self.n_heads = n_heads
assert dim % n_heads == 0, 'dim should be div by n_heads'
self.head_dim = self.dim // self.n_heads
self.in_proj = nn.Linear(dim,dim*3,bias=False)
self.attn_dropout = nn.Dropout(dropout)
self.scale = self.head_dim ** -0.5
self.out_proj = nn.Linear(dim,dim)
def forward(self,x,mask=None):
b,t,c = x.shape
q,k,v = self.in_proj(x).chunk(3,dim=-1)
q = q.view(b,t,self.n_heads,self.head_dim).permute(0,2,1,3)
k = k.view(b,t,self.n_heads,self.head_dim).permute(0,2,1,3)
v = v.view(b,t,self.n_heads,self.head_dim).permute(0,2,1,3)
qkT = torch.matmul(q,k.transpose(-1,-2)) * self.scale
qkT = self.attn_dropout(qkT)
if mask is not None:
mask = mask.to(dtype=qkT.dtype,device=qkT.device)
qkT = qkT.masked_fill(mask==0,float('-inf'))
qkT = F.softmax(qkT,dim=-1)
attn = torch.matmul(qkT,v)
attn = attn.permute(0,2,1,3).contiguous().view(b,t,c)
out = self.out_proj(attn)
return out
class FeedForward(nn.Module):
def __init__(self,dim,dropout=0.):
super().__init__()
self.feed_forward = nn.Sequential(
nn.Linear(dim,dim*4),
nn.Dropout(dropout),
nn.GELU(),
nn.Linear(dim*4,dim)
)
def forward(self, x):
return self.feed_forward(x)
class EncoderBlock(nn.Module):
def __init__(self, dim, n_heads, attn_dropout=0., mlp_dropout=0.):
super().__init__()
self.attn = MultiheadAttention(dim,n_heads,attn_dropout)
self.ffd = FeedForward(dim,mlp_dropout)
self.ln_1 = RMSNorm(dim)
self.ln_2 = RMSNorm(dim)
def forward(self,x,mask=None):
x = self.ln_1(x)
x = x + self.attn(x,mask)
x = self.ln_2(x)
x = x + self.ffd(x)
return x
class Embedding(nn.Module):
def __init__(self,vocab_size,max_len,dim):
super().__init__()
self.max_len = max_len
self.class_embedding = nn.Embedding(vocab_size,dim)
self.pos_embedding = nn.Embedding(max_len,dim)
def forward(self,x):
x = self.class_embedding(x)
pos = torch.arange(0,x.size(1),device=x.device)
x = x + self.pos_embedding(pos)
return x
class MLMBERT(nn.Module):
def __init__(self, config):
super().__init__()
self.embedding = Embedding(config['vocab_size'],config['max_len'],config['dim'])
self.depth = config['depth']
self.encoders = nn.ModuleList([
EncoderBlock(
dim=config['dim'],
n_heads=config['n_heads'],
attn_dropout=config['attn_dropout'],
mlp_dropout=config['mlp_dropout']
) for _ in range(self.depth)
])
self.ln_f = RMSNorm(config['dim'])
self.mlm_head = nn.Linear(config['dim'],config['vocab_size'],bias=False)
self.embedding.class_embedding.weight = self.mlm_head.weight # weight tying
self.pad_token_id = config['pad_token_id']
self.mask_token_id = config['mask_token_id']
self.apply(self._init_weights)
def _init_weights(self, module):
if isinstance(module, nn.Linear):
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
if module.bias is not None:
torch.nn.init.zeros_(module.bias)
elif isinstance(module, nn.Embedding):
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
def create_src_mask(self,src):
return (src != self.pad_token_id).unsqueeze(1).unsqueeze(2) # N, 1, 1, src_len
def forward(self,input_ids,labels=None):
src_mask = self.create_src_mask(input_ids)
enc_out = self.embedding(input_ids)
for layer in self.encoders:
enc_out = layer(enc_out,mask=src_mask)
enc_out = self.ln_f(enc_out)
logits = self.mlm_head(enc_out)
if labels is not None:
loss = F.cross_entropy(logits.view(-1,logits.size(-1)),labels.view(-1))
return {'loss': loss, 'logits': logits}
else:
# assuming inference input_ids only have 1 [MASK] token
mask_idx = (input_ids==self.mask_token_id).flatten().nonzero().item()
mask_preds = F.softmax(logits[:,mask_idx,:],dim=-1).argmax(dim=-1)
return {'mask_predictions':mask_preds}
if __name__ == '__main__':
config = {
'dim': 256,
'n_heads': 8,
'attn_dropout': 0.1,
'mlp_dropout': 0.1,
'depth': 6,
'vocab_size': 8192,
'max_len': 128,
'pad_token_id': 1,
'mask_token_id': 2
}
model = MLMBERT(config)
print('trainable parameters:',sum([p.numel() for p in model.parameters() if p.requires_grad]))