-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
177 lines (141 loc) · 6.33 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
import math
import inspect
from dataclasses import dataclass
import torch
import torch.nn as nn
from torch.nn import functional as F
class LayerNorm(nn.Module):
def __init__(self, ndim, bias):
super().__init__()
self.weight = nn.Parameter(torch.ones(ndim))
self.bias = nn.Parameter(torch.zeros(ndim)) if bias else None
def forward(self, input):
return F.layer_norm(input, self.weight.shape, self.weight, self.bias, 1e-5)
class SelfAttention(nn.Module):
def __init__(self, n_embd, n_head, block_size, dropout, bias):
super().__init__()
# key, query, value projections
self.c_attn_q = nn.Linear(n_embd, n_embd, bias=bias)
self.c_attn_v = nn.Linear(n_embd, n_embd, bias=bias)
self.c_attn_k = nn.Linear(n_embd, n_embd, bias=bias)
# output projection
self.c_proj = nn.Linear(n_embd, n_embd, bias=bias)
# regularization
self.attn_dropout = nn.Dropout(dropout)
self.resid_dropout = nn.Dropout(dropout)
self.n_head = n_head
self.n_embd = n_embd
self.dropout = dropout
self.register_buffer("bias", torch.tril(torch.ones(block_size, block_size))
.view(1, 1, block_size, block_size))
def forward(self, x):
B, T, C = x.shape
# calculate query, key, values for all heads in batch and move head forward to be the batch dim
q, k, v = self.c_attn_q(x), self.c_attn_k(x), self.c_attn_v(x)
k = k.view(B, T, self.n_head, C // self.n_head).transpose(1, 2)
q = q.view(B, T, self.n_head, C // self.n_head).transpose(1, 2)
v = v.view(B, T, self.n_head, C // self.n_head).transpose(1, 2)
att = (q @ k.transpose(-2, -1)) * (1.0 / math.sqrt(k.size(-1)))
att = att.masked_fill(self.bias[:,:,:T,:T] == 0, float('-inf'))
att = F.softmax(att, dim=-1)
att = self.attn_dropout(att)
y = att @ v
y = y.transpose(1, 2).contiguous().view(B, T, C)
# output projection
y = self.resid_dropout(self.c_proj(y))
return y
class MLP(nn.Module):
def __init__(self, n_embd, bias, dropout):
super().__init__()
self.mlp = nn.Sequential(
nn.Linear(n_embd, 4 * n_embd, bias=bias),
nn.GELU(),
nn.Linear(4 * n_embd, n_embd, bias=bias),
nn.Dropout(dropout)
)
def forward(self, x):
return self.mlp(x)
class Block(nn.Module):
def __init__(self, block_size, vocab_size, n_layer, n_head, n_embd, dropout, bias):
super().__init__()
self.ln_1 = LayerNorm(n_embd, bias=bias)
self.attn = SelfAttention(n_embd, n_head, block_size, dropout, bias)
self.ln_2 = LayerNorm(n_embd, bias=bias)
self.mlp = MLP(n_embd, bias, dropout)
def forward(self, x):
residual = x
x = self.attn(self.ln_1(x))
x = x + residual
residual = x
x = self.mlp(self.ln_2(x))
x = x + residual
return x
class GPT(nn.Module):
def __init__(self, block_size=1024, vocab_size=50304, n_layer=12, n_head=12, n_embd=768,
dropout=0, bias=True):
super().__init__()
self.block_size = block_size
self.vocab_size = vocab_size
self.n_layer = n_layer
self.n_embd = n_embd
self.dropout = dropout
self.bias = bias
self.transformer = nn.ModuleDict(dict(
wte = nn.Embedding(vocab_size, n_embd),
wpe = nn.Embedding(block_size, n_embd),
drop = nn.Dropout(dropout),
h = nn.ModuleList([Block(block_size, vocab_size, n_layer, n_head, n_embd, dropout, bias) for _ in range(n_layer)]),
ln_f = LayerNorm(n_embd, bias=bias),
))
self.lm_head = nn.Linear(n_embd, vocab_size, bias=False)
# Weight Tying
self.transformer.wte.weight = self.lm_head.weight
# weight initialisation
self.apply(self._init_weights)
# Scaled initialisation from GPT paper
for pn, p in self.named_parameters():
if pn.endswith('c_proj.weight'):
torch.nn.init.normal_(p, mean=0.0, std=0.02/math.sqrt(2 * n_layer))
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 forward(self, idx, targets=None, return_all_logits=False):
device = idx.device
b, t = idx.shape
pos = torch.arange(0, t, dtype=torch.long, device=device)
tok_emb = self.transformer.wte(idx)
pos_emb = self.transformer.wpe(pos)
out = self.transformer.drop(tok_emb + pos_emb)
for block in self.transformer.h:
out = block(out)
out = self.transformer.ln_f(out)
if targets is not None:
# Return cross entropy loss during training.
logits = self.lm_head(out)
loss = F.cross_entropy(logits.view(-1, logits.size(-1)), targets.view(-1), ignore_index=-1)
else:
if return_all_logits:
# For fine-tuning tasks return all logits and compute custom loss.
logits = self.lm_head(x)
else:
# Return just the last timestep during inference
logits = self.lm_head(out[:, [-1], :])
loss = None
return logits, loss
def configure_optimizers(self, weight_decay, learning_rate, betas, device_type):
param_dict = {pn: p for pn, p in self.named_parameters() if p.requires_grad}
# create optim groups. Any parameters that is 2D will be weight decayed, otherwise no.
# i.e. all weight tensors in matmuls + embeddings decay, all biases and layernorms don't.
decay_params = [p for n, p in param_dict.items() if p.dim() >= 2]
nodecay_params = [p for n, p in param_dict.items() if p.dim() < 2]
optim_groups = [
{'params': decay_params, 'weight_decay': weight_decay},
{'params': nodecay_params, 'weight_decay': 0.0}
]
kwargs = dict(fused=True)
optimizer = torch.optim.AdamW(optim_groups, lr=learning_rate, betas=betas, **kwargs)
return optimizer