-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigram_language_model.py
286 lines (230 loc) · 8.04 KB
/
bigram_language_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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#%%
import datasets
import torch
from torch import nn
from torch.nn import functional as F
from torch.nn.functional import softmax
# hyperparameters
batch_size = 4 # B
block_size = 25 # T
max_iters = 3000
eval_interval = 300
learning_rate = 1e-3
device = "cuda" if torch.cuda.is_available() else "cpu"
eval_iters = 200
n_embd = 32
n_head = 4
n_layer = 3
dropout = 0.2
assert n_embd / n_head == n_embd // n_head
head_size = (
10 # TODO: I think there's some theoretical bound on what this val should be
)
# -----------------
torch.manual_seed(42)
# pull in shakespeare data
d = datasets.load_dataset(path="tiny_shakespeare", name="shakespeare")
train = d["train"]["text"][0]
test = d["test"]["text"][0]
val = d["validation"]["text"][0]
text = train + test + val
# create tokenizer
chars = sorted(list(set(text)))
vocab_size = len(chars)
# token encoder and decoder dictionaries
toke = {t: i for i, t in enumerate(chars)}
tokd = {i: t for i, t in enumerate(chars)}
encode = lambda sentence: [toke[t] for t in sentence]
decode = lambda integers: "".join([tokd[i] for i in integers])
# train and test splits
data = torch.tensor(encode(text), dtype=torch.long)
def get_batch(data, batch_size=4, block_size=8):
if not isinstance(data, torch.Tensor):
data = torch.tensor(data, dtype=torch.long)
max_idx = len(data) - block_size
ix = torch.randint(max_idx, size=(batch_size,))
X = torch.stack([data[i : i + block_size] for i in ix])
y = torch.stack([data[i + 1 : i + block_size + 1] for i in ix])
X, y = X.to(device), y.to(device)
return X, y
@torch.no_grad()
def estimate_loss(model):
out = {}
model.eval()
for split in [train, val]:
losses = torch.zeros(eval_iters)
for k in range(eval_iters):
X, Y = get_batch(split)
logits, loss = model(X, Y)
losses[k] = loss.item()
out[split] = losses.mean()
model.train()
return out
class AttentionHead(nn.Module):
"""
B --> batch size
T --> number of tokens in each block
D --> embedding dimension (equal to the vocab size, C?)
karpathy:
- block_size = n_embd = T
- n_embd = head_size = C
"""
def __init__(self, head_size, mask=True):
super().__init__()
# matrices and attributes
self.K = nn.Linear(n_embd, head_size, bias=False)
self.Q = nn.Linear(n_embd, head_size, bias=False)
self.V = nn.Linear(n_embd, head_size, bias=False)
self.register_buffer("tril", torch.tril(torch.ones(block_size, block_size)))
# hyperparameters
self.mask = mask
def forward(self, x):
B, T, D = x.shape
k = self.K(x) # (B, T, D)
q = self.Q(x) # (B, T, D)
# compute attention
attn_matrix = (
q @ k.transpose(-2, -1)
) * D**-0.5 # (B, T, D) @ (B, D, T) --> (B, T, T)
attn_matrix = attn_matrix.masked_fill(
self.tril[:T, :T] == 0, float("-inf")
) # (B, T, T)
attn_matrix = softmax(attn_matrix, dim=-1)
v = self.V(x) # (B, T, T) @ (B, T, D) --> (B, T, D)
out = attn_matrix @ v # (B, T, D)
return out
class MultiHeadedAttention(nn.Module):
"""
Make a bunch of attention heads,
calculate attention on all of them,
concatenate them together,
then run the result through a feedforward layer
"""
def __init__(self, n_head, head_size):
super().__init__()
self.heads = nn.ModuleList([AttentionHead(head_size) for _ in range(n_head)])
self.proj = nn.Linear(n_embd, n_embd)
def forward(self, x):
B, T, D = x.shape
out = torch.cat([head(x) for head in self.heads], axis=-1) # (B, T, D*n_embd)
return self.proj(out)
class FeedForward(nn.Module):
def __init__(self, n_embd):
super().__init__()
self.net = nn.Sequential(
nn.Linear(n_embd, 4 * n_embd),
nn.ReLU(),
nn.Linear(4 * n_embd, n_embd),
nn.Dropout(p=dropout)
)
def forward(self, x):
return self.net(x)
class Block(nn.Module):
def __init__(self, n_embd, n_head):
super().__init__()
head_size = n_embd // n_head
self.sa_heads = MultiHeadedAttention(n_head, head_size)
self.ffn = FeedForward(n_embd)
self.ln1 = nn.LayerNorm(n_embd)
self.ln2 = nn.LayerNorm(n_embd)
def forward(self, x):
x = x + self.sa_heads(self.ln1(x))
x = x + self.ffn(self.ln2(x))
return x
class BigramLanguageModel(nn.Module):
"""
T --> block_size
C --> vocab length
D --> n_embd
B --> batch size
"""
def __init__(self):
super().__init__()
self.token_embedding_table = nn.Embedding(vocab_size, n_embd)
self.position_embedding_table = nn.Embedding(block_size, n_embd)
self.blocks = nn.Sequential(*[Block(n_embd, n_head) for _ in range(n_layer)])
self.lm_head = nn.Linear(n_embd, vocab_size)
def forward(self, X, y=None):
"""
takes a batch, returns the loss and target for that batch
"""
# x \in (batch_size, block_size) == (B, T)
B, T = X.shape
tok_emb = self.token_embedding_table(X) # (B, T, D)
pos_emb = self.position_embedding_table(
torch.arange(T, device=device)
) # (T, D)
x = tok_emb + pos_emb # (B, T, D)
x = self.blocks(x)
logits = self.lm_head(x) # (B, T, vocab_size)
if y is None:
loss = None
else:
B, T, D = logits.shape
logits = logits.view(B * T, D)
y = y.view(B * T)
# where D is n_classes, N is batch_size
# cross_entropy requires shape (D) or shape (N, D),
# but our data is (N, block_size, D)
loss = F.cross_entropy(logits, y)
return logits, loss
def generate(self, idx, max_new_tokens):
"""
for everything up to max_new_tokens,
do a forward pass,
softmax the logits,
sample multinomial from the resulting distribution,
append sample to running sequence
"""
for _ in range(max_new_tokens):
# idx is (B, T)
idx_cond = idx[:, -block_size:]
logits, _ = self(idx_cond)
# focus only on the last timestep
logits = logits[:, -1, :] # becomes (B, D)
probs = F.softmax(logits, dim=-1) # (B, D)
idx_next = torch.multinomial(probs, num_samples=1) # (B, 1)
idx = torch.cat((idx, idx_next), dim=1) # (B, T+1)
return idx
X, y = get_batch(data, batch_size=batch_size, block_size=block_size)
blm = BigramLanguageModel()
logits, loss = blm(X, y)
# get a generation with a zero initiation and 100 new tokens
generation = blm.generate(
idx=torch.zeros((1, 1), dtype=torch.long), max_new_tokens=100
)[0].tolist()
# train
# Create an optimization object (which eats the parameters of a model)
# then, with a batch size of 32,
# loop through 100 training steps.
# at each one, sample a batch of data,
# get the loss, do a backward step,
# and then step forward on the optimizer
optimizer = torch.optim.AdamW(blm.parameters(), lr=1e-3)
for step in range(10000):
xb, yb = get_batch(data, batch_size=batch_size, block_size=block_size)
logits, loss = blm(xb, yb)
optimizer.zero_grad(set_to_none=True)
loss.backward()
optimizer.step()
print(loss.item())
with torch.no_grad():
idx = blm.generate(idx=torch.zeros((1, 1), dtype=torch.long), max_new_tokens=100)[
0
].tolist()
print(decode(idx))
#%% [markdown]
# ## The mathematical trick in self-attention
A = torch.tril(torch.ones(3, 3, dtype=torch.float64))
A /= A.sum(dim=1, keepdim=True) # each row is divided by its sum (count of 1s)
B = torch.arange(3 * 3, dtype=torch.float64).reshape(3, 3) + 1
torch.manual_seed(42)
B, T, C = 4, 8, 2
x = torch.randn(B, T, C)
# create x, random batch of shape (B, T, C)
xbow = torch.zeros((B, T, C))
for b in range(B):
for t in range(T):
xprev = x[b, : t + 1]
xbow[b, t] = torch.mean(xprev, 0)
# make x[b, t] = mean_{i<=r} x[b, i]