-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv4.py
275 lines (221 loc) · 10.4 KB
/
v4.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
import os
import torch
import torch.nn as nn
from torch.nn import functional as F
# v4: Add multi-head self-attention. Add ffwd
# Build a character-level Biagram language model (our baseline for LLM)
# hyperparameters
batch_size = 32 # B, how many independent sequences will we process in parallel?
block_size = 8 # T, what is the maximum context length for predictions?
max_iters = 3000
eval_interval = 300
learning_rate = 1e-3 # self attention can't tolerate too high LR. reduce it to 1e-3
device = 'cuda' if torch.cuda.is_available() else 'cpu'
eval_iters = 200
n_embed = 32
# ------------
torch.manual_seed(1337)
# Load the data
with open('input.txt', 'r', encoding='utf-8') as f:
text = f.read()
print(len(text)) # ~1M characters in total
# here are all the unique characters that occur in this text
chars = sorted(list(set(text)))
vocab_size = len(chars) # C = 65
# print(''.join(chars))
# print(vocab_size) # 65 unique characters
# Encode the data
# create a mapping from characters to integers and vice versa
stoi = {ch: i for i, ch in enumerate(chars)} # ch to int
itos = {i: ch for i, ch in enumerate(chars)} # int to ch
# encoder: take a string, output a list of integers
encode = lambda s: [stoi[ch] for ch in s]
decode = lambda l: ''.join([itos[i] for i in l])
print(encode("hii there"), decode(encode("hii there")))
# Prep the data, split into train and validation sets
data = torch.tensor(encode(text), dtype = torch.long)
print(data.shape, data.dtype)
n = int(0.9 * len(data))
train_data = data[:n]
val_data = data[n:]
# spot-check the data
# example: enumerate all possible hidden chunks in the training set.
# block_size = 8
# x = train_data[:block_size]
# y = train_data[1:block_size + 1]
# for t in range(block_size):
# context = x[:t + 1]
# target = y[t]
# print(f"when input is {context} the target: {target}")
# Build data loader
def get_batch(split):
# generate a small batch of data of inputs x and targets y
data = train_data if split == 'train' else val_data
# batch_size is hardcoded to 4
# random int between 0 to (1115394 - 8), of size (4, 1)
ix = torch.randint(low = 0, high = len(data) - block_size, size=(batch_size,))
# print(ix) # tensor([ 76049, 234249, 934904, 560986])
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]) # merely shift by one from x
return x, y
# unit test the get_batch() function
# xb, yb = get_batch('train') # batch_size is hardcoded to 4 from global variable
# print('inputs:')
# print(xb.shape) # [4, 8] = [B, T]
# print(xb)
# print('targets:')
# print(yb.shape) # [4, 8] = [B, T]
# print(yb)
# print('----spill them out----')
# for b in range(batch_size): # batch dimension. 4 sequences
# for t in range(block_size): # time dimension, add 1 char at a time, until 8 chars
# context = xb[b, :t+1]
# target = yb[b,t]
# # ie., 8 predictions in total for a sequence. and we have 4 sequences in 1 batch, so 4 * 8
# print(f"when input is {context.tolist()} the target: {target}")
@torch.no_grad()
def estimate_loss():
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 MultiHeadAttention(nn.Module):
""" multiple heads of self-attention in parallel """
def __init__(self, num_heads, head_size):
super().__init__()
self.heads = nn.ModuleList([Head(head_size) for _ in range(num_heads)])
def forward(self, x):
out = torch.cat([h(x) for h in self.heads], dim=-1)
return out
class Head(nn.Module):
""" one head of self-attention """
def __init__(self, head_size):
super().__init__()
self.key = nn.Linear(n_embed, head_size, bias=False)
self.query = nn.Linear(n_embed, head_size, bias=False)
self.value = nn.Linear(n_embed, head_size, bias=False)
# tril variable. it is not a param and will not be updated via backprop.
self.register_buffer('tril', torch.tril(torch.ones(block_size, block_size)))
def forward(self, x):
B,T,C = x.shape
k = self.key(x) # (B,T,C)
q = self.query(x) # (B,T,C)
# compute attention scores ("affinities")
wei = q @ k.transpose(-2,-1) * C**-0.5 # (B, T, C) @ (B, C, T) -> (B, T, T)
wei = wei.masked_fill(self.tril[:T, :T] == 0, float('-inf')) # (B, T, T)
wei = F.softmax(wei, dim=-1) # (B, T, T)
# perform the weighted aggregation of the values
v = self.value(x) # (B,T,C)
out = wei @ v # (B, T, T) @ (B, T, C) -> (B, T, C)
return out
# just a MLP
class FeedFoward(nn.Module):
""" a simple linear layer followed by a non-linearity """
def __init__(self, n_embed):
super().__init__()
self.net = nn.Sequential(
nn.Linear(n_embed, n_embed),
nn.ReLU(),
)
def forward(self, x):
return self.net(x)
# build the model
# Bigram model: even though we feed in a sequence of 8 chars, we only use the current char, to predict the next char
class BigramLanguageModel(nn.Module):
def __init__(self, vocab_size):
super().__init__()
# holds token (ie., identity) information. What is this char? is it A? B? C?
self.token_embedding_table = nn.Embedding(vocab_size, n_embed) # (65, 65) embedding table
# plug in a Linear layer between head and input, size (n_embed, vocab_size)
self.lm_head = nn.Linear(n_embed, vocab_size)
# holds positional info. Where was it? Was it a position 0, 1, 2, T - 1?
self.position_embedding_table = nn.Embedding(block_size, n_embed)
# self-attention head
# because we have 4 heads (communication channels), we want 8 dimensional self attentions
# so that when we concatenate these 4 self-attentions, we get 32 dimensional output
self.sa_heads = MultiHeadAttention(4, n_embed // 4)
self.ffwd = FeedFoward(n_embed)
def forward(self, idx, targets=None):
B, T = idx.shape # (batch_size, context_length)
# idx and targets are both (B,T) tensor of integers
# B: batch, T: Time (context length), C: Channels (vocab_size)
tok_emb = self.token_embedding_table(idx) # (B,T,C) = (4,8,65)
pos_input = torch.arange(T, device = device) # [0, 1, 2, ..., T - 1]
pos_emb = self.position_embedding_table(pos_input) # maps pos_input to (T, C)
x = tok_emb + pos_emb # (B, T, C)
x = self.sa_heads(x) # apply 4-head of self-attention. (B, T, C)
x = self.ffwd(x)
logits = self.lm_head(x) # (B, T, C)
if targets is None: # inference time
loss = None
else: # training time
# pytorch cross entropy expects (M,N) as inputs
# also, we are only using bigram, so we reshape logits to flatten 4x8x65 to a 32x65
# ie., now we see it as 32 independent predictions, each of which is a 65-way classification problem
# in xb = [24, 43, 58, 5, 57, 1, 46, 43],
# yb = [43, 58, 5, 57, 1, 46, 43, 39],
# when input is [24] the target: 43
# when input is [43] the target: 58 (even though the context given is [24, 43] we only use [43])
# when input is [43] the target: 39 (same idea)
# same for the rest 3 sequences. in total, 32 predictions
B, T, C = logits.shape
logits = logits.view(B*T, C)
targets = targets.view(B*T)
loss = F.cross_entropy(logits, targets)
return logits, loss
def generate(self, idx, max_new_tokens):
# idx is (B, T) array of indices in the current context
# in xb = [24, 43, 58, 5, 57, 1, 46, 43],
# eg., [24, 43, 58] and we are predicting the next target [5]
# we first take only (B, C) so the [58], and we apply softmax
# and we get a 65 dim array, and we sample from it to get the next chart, eg., [8] (ie., wrong, not 5)
# and we concat it to be [24, 43, 58, 8]
# next run we are predicting [57]. Given [24, 43, 58, 8]
# generate until max_new_tokens = 100 times and stop to avoid infinite loop
for _ in range(max_new_tokens):
# note: because we have positional embedding which has size at most T,
# we have to ensure idx not exceed T (up to block_size)
# crop idx to the last block_size tokens
idx_crop = idx[:, -block_size:]
# get the predictions
logits, loss = self(idx_crop)
# we are using a bigram model. it only take one char. So focus only on the last time step
logits = logits[:, -1, :] # (B,T,C) becomes (B, C). We take the current char
# apply softmax to get probabilities
probs = F.softmax(logits, dim=-1) # (B, C), the prob of the next char among those 65 possible chars
# convert the (B, C) to a single prediction. Sample from the distribution
idx_next = torch.multinomial(probs, num_samples=1) # (B, 1)
# append sampled index to the running sequence
idx = torch.cat((idx, idx_next), dim=1) # (B, T+1)
return idx
# See what this totally random model will generate
# given batch_size = 1, time = 1, holding a 0.
# we kick off the generation by this [0], and we generate 100 chars
# .reshape(-1) get rid of batch_size dimension. can also use [0] instead of .reshape(-1)
model = BigramLanguageModel(vocab_size)
m = model.to(device)
print(decode(m.generate(idx = torch.zeros((1, 1), dtype=torch.long), max_new_tokens=100).reshape(-1).tolist()))
# Actually train the model
optimizer = torch.optim.AdamW(m.parameters(), lr=1e-3)
for iter in range(max_iters):
# every once in a while evaluate the loss on train and val sets
if iter % eval_interval == 0:
losses = estimate_loss()
print(f"step {iter}: train loss {losses['train']:.4f}, val loss {losses['val']:.4f}")
# sample a batch of data
xb, yb = get_batch('train')
# evaluate the loss
logits, loss = model(xb, yb)
optimizer.zero_grad(set_to_none=True)
loss.backward()
optimizer.step()
# generate from the model
context = torch.zeros((1, 1), dtype=torch.long, device=device)
print(decode(m.generate(context, max_new_tokens=500)[0].tolist()))