-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
138 lines (108 loc) · 4.72 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
import torch
import torch.nn as nn
import torch.nn.functional as F
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 = nn.LayerNorm(dim)
self.ln_2 = nn.LayerNorm(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 = nn.LayerNorm(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}