-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathmodel.py
More file actions
244 lines (203 loc) · 9.71 KB
/
model.py
File metadata and controls
244 lines (203 loc) · 9.71 KB
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
"""
GPT model:
- the initial stem consists of a combination of token encoding and a positional encoding
- the meat of it is a uniform sequence of Transformer blocks
- the final decoder is a linear projection into a Softmax classifier
"""
import math
import logging
import torch
import torch.nn as nn
from torch.nn import functional as F
logger = logging.getLogger(__name__)
class GPTConfig:
""" base GPT config, params common to all GPT versions """
embd_pdrop = 0.1
resid_pdrop = 0.1
attn_pdrop = 0.1
def __init__(self, vocab_size, block_size, **kwargs):
self.vocab_size = vocab_size
self.block_size = block_size
for k,v in kwargs.items():
setattr(self, k, v)
class GPT1Config(GPTConfig):
n_layer = 12
n_head = 12
n_embd = 768
class CausalSelfAttention(nn.Module):
"""
multi-head masked self-attention layer with projection
"""
def __init__(self, config):
super().__init__()
assert config.n_embd % config.n_head == 0
# key, query, value projections for all heads
self.key = nn.Linear(config.n_embd, config.n_embd)
self.query = nn.Linear(config.n_embd, config.n_embd)
self.value = nn.Linear(config.n_embd, config.n_embd)
# regularization
self.attn_drop = nn.Dropout(config.attn_pdrop)
self.resid_drop = nn.Dropout(config.resid_pdrop)
self.proj = nn.Linear(config.n_embd, config.n_embd)
if config.num_props:
if config.scaffold_maxlen:
num = int(bool(config.num_props)) + int(config.scaffold_maxlen)
else:
num = int(bool(config.num_props))
else:
num = 0
self.register_buffer("mask", torch.tril(torch.ones(config.block_size + num, config.block_size + num))
.view(1, 1, config.block_size + num, config.block_size + num))
self.n_head = config.n_head
def forward(self, x, layer_past=None):
B, T, C = x.size()
k = self.key(x).view(B, T, self.n_head, C // self.n_head).transpose(1, 2) # (B, nh, T, hs)
q = self.query(x).view(B, T, self.n_head, C // self.n_head).transpose(1, 2) # (B, nh, T, hs)
v = self.value(x).view(B, T, self.n_head, C // self.n_head).transpose(1, 2) # (B, nh, T, hs)
# causal self-attention; Self-attend: (B, nh, T, hs) x (B, nh, hs, T) -> (B, nh, T, T)
att = (q @ k.transpose(-2, -1)) * (1.0 / math.sqrt(k.size(-1)))
att = att.masked_fill(self.mask[:,:,:T,:T] == 0, float('-inf'))
att = F.softmax(att, dim=-1)
attn_save = att
att = self.attn_drop(att)
y = att @ v # (B, nh, T, T) x (B, nh, T, hs) -> (B, nh, T, hs)
y = y.transpose(1, 2).contiguous().view(B, T, C) # re-assemble all head outputs side by side
# output projection
y = self.resid_drop(self.proj(y))
return y, attn_save
class Block(nn.Module):
""" an unassuming Transformer block """
def __init__(self, config):
super().__init__()
self.ln1 = nn.LayerNorm(config.n_embd)
self.ln2 = nn.LayerNorm(config.n_embd)
self.attn = CausalSelfAttention(config)
self.mlp = nn.Sequential(
nn.Linear(config.n_embd, 4 * config.n_embd),
nn.GELU(),
nn.Linear(4 * config.n_embd, config.n_embd),
nn.Dropout(config.resid_pdrop),
)
def forward(self, x):
y, attn = self.attn(self.ln1(x))
x = x + y
x = x + self.mlp(self.ln2(x))
return x, attn
class GPT(nn.Module):
""" the full GPT language model, with a context size of block_size """
def __init__(self, config):
super().__init__()
# input embedding stem
self.config = config
self.padding_token_id = 0
self.tok_emb = nn.Embedding(config.vocab_size, config.n_embd)
self.type_emb = nn.Embedding(2, config.n_embd)
if config.num_props:
self.prop_nn = nn.Linear(config.num_props, config.n_embd)
self.pos_emb = nn.Parameter(torch.zeros(1, config.block_size, config.n_embd))
self.drop = nn.Dropout(config.embd_pdrop)
# transformer
self.blocks = nn.Sequential(*[Block(config) for _ in range(config.n_layer)])
# decoder head
self.ln_f = nn.LayerNorm(config.n_embd)
self.head = nn.Linear(config.n_embd, config.vocab_size, bias=False)
self.block_size = config.block_size
self.isconditional = config.isconditional
if config.lstm:
self.lstm = nn.LSTM(input_size = config.n_embd, hidden_size = config.n_embd, num_layers = config.lstm_layers, dropout = 0.3, bidirectional = False)
self.apply(self._init_weights)
logger.info("number of parameters: %e", sum(p.numel() for p in self.parameters()))
def get_block_size(self):
return self.block_size
def _init_weights(self, module):
if isinstance(module, (nn.Linear, nn.Embedding)):
module.weight.data.normal_(mean=0.0, std=0.02)
if isinstance(module, nn.Linear) and module.bias is not None:
module.bias.data.zero_()
elif isinstance(module, nn.LayerNorm):
module.bias.data.zero_()
module.weight.data.fill_(1.0)
def configure_optimizers(self, train_config):
"""
We are separating out all parameters of the model into two buckets: those that will experience
weight decay for regularization and those that won't (biases, and layernorm/embedding weights).
We are then returning the PyTorch optimizer object.
"""
decay = set()
no_decay = set()
whitelist_weight_modules = (torch.nn.Linear, torch.nn.LSTM)
blacklist_weight_modules = (torch.nn.LayerNorm, torch.nn.Embedding)
for mn, m in self.named_modules():
for pn, p in m.named_parameters():
fpn = '%s.%s' % (mn, pn) if mn else pn # full param name
if pn.endswith('bias') or ('bias' in pn):
no_decay.add(fpn)
elif (pn.endswith('weight') or ('weight' in pn)) and isinstance(m, whitelist_weight_modules):
decay.add(fpn)
elif pn.endswith('weight') and isinstance(m, blacklist_weight_modules):
no_decay.add(fpn)
no_decay.add('pos_emb')
param_dict = {pn: p for pn, p in self.named_parameters()}
inter_params = decay & no_decay
union_params = decay | no_decay
assert len(inter_params) == 0, "parameters %s made it into both decay/no_decay sets!" % (str(inter_params), )
assert len(param_dict.keys() - union_params) == 0, "parameters %s were not separated into either decay/no_decay set!" \
% (str(param_dict.keys() - union_params), )
optim_groups = [
{"params": [param_dict[pn] for pn in sorted(list(decay))], "weight_decay": train_config.weight_decay},
{"params": [param_dict[pn] for pn in sorted(list(no_decay))], "weight_decay": 0.0},
]
optimizer = torch.optim.AdamW(optim_groups, lr=train_config.learning_rate, betas=train_config.betas)
return optimizer
def forward(self, idx, targets=None, condition_split_id=None, prop = None, scaffold = None):
b, t = idx.size()
assert t <= self.block_size, "Cannot forward, model block size is exhausted."
if self.config.num_props:
assert prop.size(-1) == self.config.num_props, "Num_props should be equal to last dim of property vector"
# forward the GPT model
token_embeddings = self.tok_emb(idx)
position_embeddings = self.pos_emb[:, :t, :]
type_embeddings = self.type_emb(torch.ones((b,t), dtype = torch.long, device = idx.device))
x = self.drop(token_embeddings + position_embeddings + type_embeddings)
if self.config.num_props:
type_embd = self.type_emb(torch.zeros((b, 1), dtype = torch.long, device = idx.device))
if prop.ndim == 2:
p = self.prop_nn(prop.unsqueeze(1))
else:
p = self.prop_nn(prop)
p += type_embd
x = torch.cat([p, x], 1)
if self.config.scaffold:
type_embd = self.type_emb(torch.zeros((b, 1), dtype = torch.long, device = idx.device))
scaffold_embeds = self.tok_emb(scaffold)
if self.config.lstm:
scaffold_embeds = self.lstm(scaffold_embeds.permute(1,0,2))[1][0]
scaffold_embeds = scaffold_embeds.permute(1,0,2)
scaffold_embeds += type_embd
x = torch.cat([scaffold_embeds, x], 1)
attn_maps = []
for layer in self.blocks:
x, attn = layer(x)
attn_maps.append(attn)
x = self.ln_f(x)
logits = self.head(x)
if self.config.num_props and self.config.scaffold:
num = int(bool(self.config.num_props)) + int(self.config.scaffold_maxlen)
elif self.config.num_props:
num = int(bool(self.config.num_props))
elif self.config.scaffold:
num = int(self.config.scaffold_maxlen)
else:
num = 0
logits = logits[:, num:, :]
loss = None
if targets is not None:
mask = targets != self.padding_token_id
if self.isconditional:
range_tensor = torch.arange(t, device=mask.device).expand(b, -1)
expanded_split_id = condition_split_id.unsqueeze(1).expand(-1, t)
cond_mask = range_tensor < expanded_split_id
mask[cond_mask] = False
loss = F.cross_entropy(logits.view(-1, logits.size(-1)), targets.view(-1), reduction='none')
loss = (loss * mask.view(-1)).sum() / mask.sum()
return logits, loss