-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgpt.py
More file actions
158 lines (116 loc) · 4.5 KB
/
gpt.py
File metadata and controls
158 lines (116 loc) · 4.5 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
## dumbed down version of karpathy/mingpt
import math
import torch as th
import torch.nn as nn
import torch.nn.functional as F
from torch import tensor, as_tensor, from_numpy
from numpy.random import rand, randint, randn, normal
import numpy as np
class Attend(nn.Module):
def __init__(self, nheads, nembd, ntoks, pdrop=0.0):
super().__init__()
self.key = nn.Linear(nembd, nembd)
self.query = nn.Linear(nembd, nembd)
self.value = nn.Linear(nembd, nembd)
self.attn_drop = nn.Dropout(pdrop)
self.head_drop = nn.Dropout(pdrop)
self.head = nn.Linear(nembd, nembd)
self.nheads = nheads
self.register_buffer("mask", th.tril(th.ones(1, 1, ntoks, ntoks)) == 0)
def forward(self, x):
nbatch, ntoks, nembd = x.size()
# (nbatch, nheads, ntoks, nembd)
k = self.key(x).view(nbatch, ntoks, self.nheads, nembd // self.nheads).transpose(1, 2)
q = self.query(x).view(nbatch, ntoks, self.nheads, nembd // self.nheads).transpose(1, 2)
v = self.value(x).view(nbatch, ntoks, self.nheads, nembd // self.nheads).transpose(1, 2)
att = q @ k.transpose(-2, -1) / math.sqrt(k.size(-1))
att = att.masked_fill(self.mask[:, :, :ntoks, :ntoks], -np.inf)
att = F.softmax(att, -1)
att = self.attn_drop(att)
out = att @ v
out = out.transpose(1, 2).contiguous().view(nbatch, ntoks, nembd)
return self.head_drop(self.head(out))
class AttendClose(nn.Module):
def __init__(self, nheads, nembd, ntoks, pdrop=0.1):
super().__init__()
self.key = nn.Linear(nembd, nembd)
self.query = nn.Linear(nembd, nembd)
self.value = nn.Linear(nembd, nembd)
self.register_buffer("mask", th.tril(th.ones(ntoks, ntoks)) == 0)
self.attention = nn.MultiheadAttention(nembd, nheads, dropout=pdrop, bias=False)
self.head = nn.Linear(nembd, nembd)
self.head_drop = nn.Dropout(pdrop)
def forward(self, x):
ntoks, nbatch, nembd = x.size()
k = self.key(x)
q = self.query(x)
v = self.value(x)
out, _ = self.attention(q, k, v, attn_mask=self.mask[:ntoks, :ntoks])
return self.head_drop(self.head(out))
class Block(nn.Module):
def __init__(self, nheads, nembd, ntoks, pdrop=0.0):
super().__init__()
self.ln1 = nn.LayerNorm(nembd)
self.ln2 = nn.LayerNorm(nembd)
self.att = Attend(nheads=nheads, nembd=nembd, ntoks=ntoks)
# self.att = AttendClose(nheads=nheads, nembd=nembd, ntoks=ntoks)
self.head = nn.Sequential(
nn.Linear(nembd, 4 * nembd),
nn.GELU(),
nn.Linear(4 * nembd, nembd),
nn.Dropout(pdrop)
)
def forward(self, x):
x = x + self.att(self.ln1(x))
x = x + self.head(self.ln2(x))
return x
class YOGPT(nn.Module):
def __init__(self, vocabsize, nheads, nembd, ntoks, nlayers, pdrop=0.0):
super().__init__()
self.vocabsize = vocabsize
self.nheads = nheads
self.ntoks = ntoks
self.nembd = nembd
self.tok_emb = nn.Embedding(self.vocabsize, self.nembd)
self.pos_emb = nn.Parameter(th.zeros(1, self.ntoks, self.nembd))
self.drop = nn.Dropout(pdrop)
self.blocks = nn.Sequential(*[Block(nheads=nheads, nembd=nembd, ntoks=ntoks, pdrop=pdrop) for _ in range(nlayers)])
self.ln = nn.LayerNorm(nembd)
self.head = nn.Linear(nembd, vocabsize, bias=False)
print(f'{sum(p.numel() for p in self.parameters()) / 2**20:.2f}M params')
def forward(self, x):
bsize, ntoks = x.size()
embs = self.tok_emb(x)
positions = self.pos_emb[:, :ntoks, :]
x = self.drop(embs + positions)
out = self.blocks(x)
# logits = self.head(self.ln(out))
logits = self.ln(out)
return logits
def gsample(model, xs, ngrow):
model.eval()
for _ in range(ngrow):
logits = model(xs)
conts = logits[:, -1, :].argmax(-1).unsqueeze(-1)
xs = th.cat((xs, conts), dim=1)
return xs
def pprint(d: dict):
for k,v in d.items():
print(f'{k}: {v}')
def sample(ps):
if ps[0] < 0:
ps = np.exp(ps)
ps /= ps.sum()
cdf = ps.cumsum(-1)
x = rand()
for i in range(len(ps)):
if cdf[i] > x:
return i
return len(ps)-1
def bar(ts, label=None):
if ts.requires_grad:
ts = ts.detach()
ts = ts.flatten()
if label is None:
label = np.arange(len(ts))
pyplot.bar(label[:len(ts)], ts)