-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpt.py
194 lines (129 loc) · 3.94 KB
/
gpt.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
# Generatively Pretrained Transformer
#%%
with open('input.txt', 'r', encoding='utf-8') as f:
text = f.read()
print(len(text))
# %%
chars = sorted(list(set(text)))
vocab_size = len(chars)
print(''.join(chars))
print(len(chars))
# %%
# map of chars to integers
ctoi = { ch:i for i,ch in enumerate(chars)}
itoc = { i:ch for i,ch in enumerate(chars)}
encode = lambda s: [ ctoi[c] for c in s ]
decode = lambda l: ''.join([ itoc[i] for i in l ])
print(encode("hello"))
print(decode([46, 43, 50, 50, 53]))
# %%
import tiktoken
enc = tiktoken.get_encoding("gpt2")
enc.encode(".")
enc.decode([31373,13])
# %%
import torch
data = torch.tensor(encode(text), dtype=torch.int16)
print(decode(data[:100].tolist()))
# %%
# train val split
n = int(0.9 * len(data))
train_data = data[:n]
val_data = data[n:]
# %%
# chunks to feed into transformer
block_size = 8
data[:block_size+1]
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 context is:{context}, the tartget is:{target}")
# %%
torch.manual_seed(4242)
batch_size = 4 # how mnay sequences will we process in parallel
block_size = 8 # max length of a sequence to fit into context (used for predicting the target)
def get_batch(split):
data = train_data if split == 'train' else val_data
ix = torch.randint(len(data) - block_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
xb, yb = get_batch('train')
print('inputs:')
print(xb.shape)
print(xb)
print('targets:')
print(yb.shape)
print(yb)
for b in range(batch_size): # batch dimention
for t in range(block_size): # time dimension
context = xb[b : t+1 ]
target = yb[b,t]
print(f"when context is {context.tolist()}, the target is {target}")
# %%
print(xb)
# %%
import torch
import torch.nn as nn
from torch.nn import functional as F
torch.manual_seed(4242)
class BigramLanguageModel(nn.Module):
def __init__(self, vocab_size):
super().__init__()
self.token_embedding_table = nn.Embedding(vocab_size,vocab_size)
def forward(self,idx,targets=None):
logits = self.token_embedding_table(idx)
if targets is None:
loss = None
else:
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):
for _ in range(max_new_tokens):
# get predictions (logits)
logits , loss = self(idx)
# focus on last step (what comes next)
logits = logits[:,-1,:] #(B,C)
# logits to probs
probs = F.softmax(logits, dim=-1)
# sample probs and get 1
idx_next = torch.multinomial(probs, num_samples=1) # (B,1)
# appened sampled index to the running sequence
idx = torch.cat((idx, idx_next), dim=1) # (B, T+1)
return idx
m = BigramLanguageModel(vocab_size)
logits, loss = m(xb,yb)
print(out.shape)
print(loss)
idx = torch.zeros((1,1),dtype=torch.int16)
print(decode(m.generate(idx, max_new_tokens=100)[0].tolist()))
# %%
# device = torch.device("cpu")
device = torch.device('mps')
import time
t1 = time.time()
model = BigramLanguageModel(vocab_size)
m = model.to(device)
optimizer = torch.optim.AdamW(m.parameters(), lr=1e-3)
batch_size = 32
for steps in range(5000):
xb, yb = get_batch('train')
logits, loss = m(xb,yb)
optimizer.zero_grad(set_to_none=True)
loss.backward()
optimizer.step()
print(loss.item())
print(int(time.time() - t1),'s')
# %%timeit
idx = torch.zeros((1,1),dtype=torch.int16)
print(decode(m.generate(idx, max_new_tokens=100)[0].tolist()))
# %%
# %%
print(torch.__version__)
# %%