|
|
|
|
|
"""gpt-dev.ipynb |
|
|
|
|
|
Automatically generated by Colab. |
|
|
|
|
|
Original file is located at |
|
|
https://colab.research.google.com/drive/1JMLa53HDuA-i7ZBmqV7ZnA3c_fvtXnx- |
|
|
|
|
|
## Building a GPT |
|
|
|
|
|
Companion notebook to the [Zero To Hero](https://karpathy.ai/zero-to-hero.html) video on GPT. |
|
|
""" |
|
|
|
|
|
|
|
|
!wget https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt |
|
|
|
|
|
|
|
|
with open('input.txt', 'r', encoding='utf-8') as f: |
|
|
text = f.read() |
|
|
|
|
|
print("length of dataset in characters: ", len(text)) |
|
|
|
|
|
|
|
|
print(text[:1000]) |
|
|
|
|
|
|
|
|
chars = sorted(list(set(text))) |
|
|
vocab_size = len(chars) |
|
|
print(''.join(chars)) |
|
|
print(vocab_size) |
|
|
|
|
|
|
|
|
stoi = { ch:i for i,ch in enumerate(chars) } |
|
|
itos = { i:ch for i,ch in enumerate(chars) } |
|
|
encode = lambda s: [stoi[c] for c in s] |
|
|
decode = lambda l: ''.join([itos[i] for i in l]) |
|
|
|
|
|
print(encode("hii there")) |
|
|
print(decode(encode("hii there"))) |
|
|
|
|
|
|
|
|
import torch |
|
|
data = torch.tensor(encode(text), dtype=torch.long) |
|
|
print(data.shape, data.dtype) |
|
|
print(data[:1000]) |
|
|
|
|
|
|
|
|
n = int(0.9*len(data)) |
|
|
train_data = data[:n] |
|
|
val_data = data[n:] |
|
|
|
|
|
block_size = 8 |
|
|
train_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 input is {context} the target: {target}") |
|
|
|
|
|
torch.manual_seed(1337) |
|
|
batch_size = 4 |
|
|
block_size = 8 |
|
|
|
|
|
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]) |
|
|
return x, y |
|
|
|
|
|
xb, yb = get_batch('train') |
|
|
print('inputs:') |
|
|
print(xb.shape) |
|
|
print(xb) |
|
|
print('targets:') |
|
|
print(yb.shape) |
|
|
print(yb) |
|
|
|
|
|
print('----') |
|
|
|
|
|
for b in range(batch_size): |
|
|
for t in range(block_size): |
|
|
context = xb[b, :t+1] |
|
|
target = yb[b,t] |
|
|
print(f"when input is {context.tolist()} the target: {target}") |
|
|
|
|
|
print(xb) |
|
|
|
|
|
import torch |
|
|
import torch.nn as nn |
|
|
from torch.nn import functional as F |
|
|
torch.manual_seed(1337) |
|
|
|
|
|
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): |
|
|
|
|
|
logits, loss = self(idx) |
|
|
|
|
|
logits = logits[:, -1, :] |
|
|
|
|
|
probs = F.softmax(logits, dim=-1) |
|
|
|
|
|
idx_next = torch.multinomial(probs, num_samples=1) |
|
|
|
|
|
idx = torch.cat((idx, idx_next), dim=1) |
|
|
return idx |
|
|
|
|
|
m = BigramLanguageModel(vocab_size) |
|
|
logits, loss = m(xb, yb) |
|
|
print(logits.shape) |
|
|
print(loss) |
|
|
|
|
|
print(decode(m.generate(idx = torch.zeros((1, 1), dtype=torch.long), max_new_tokens=100)[0].tolist())) |
|
|
|
|
|
|
|
|
optimizer = torch.optim.AdamW(m.parameters(), lr=1e-3) |
|
|
|
|
|
batch_size = 32 |
|
|
for steps in range(100): |
|
|
|
|
|
|
|
|
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(decode(m.generate(idx = torch.zeros((1, 1), dtype=torch.long), max_new_tokens=500)[0].tolist())) |
|
|
|
|
|
"""## The mathematical trick in self-attention""" |
|
|
|
|
|
|
|
|
torch.manual_seed(42) |
|
|
a = torch.tril(torch.ones(3, 3)) |
|
|
a = a / torch.sum(a, 1, keepdim=True) |
|
|
b = torch.randint(0,10,(3,2)).float() |
|
|
c = a @ b |
|
|
print('a=') |
|
|
print(a) |
|
|
print('--') |
|
|
print('b=') |
|
|
print(b) |
|
|
print('--') |
|
|
print('c=') |
|
|
print(c) |
|
|
|
|
|
|
|
|
|
|
|
torch.manual_seed(1337) |
|
|
B,T,C = 4,8,2 |
|
|
x = torch.randn(B,T,C) |
|
|
x.shape |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
wei = torch.tril(torch.ones(T, T)) |
|
|
wei = wei / wei.sum(1, keepdim=True) |
|
|
xbow2 = wei @ x |
|
|
torch.allclose(xbow, xbow2) |
|
|
|
|
|
|
|
|
tril = torch.tril(torch.ones(T, T)) |
|
|
wei = torch.zeros((T,T)) |
|
|
wei = wei.masked_fill(tril == 0, float('-inf')) |
|
|
wei = F.softmax(wei, dim=-1) |
|
|
xbow3 = wei @ x |
|
|
torch.allclose(xbow, xbow3) |
|
|
|
|
|
|
|
|
torch.manual_seed(1337) |
|
|
B,T,C = 4,8,32 |
|
|
x = torch.randn(B,T,C) |
|
|
|
|
|
|
|
|
head_size = 16 |
|
|
key = nn.Linear(C, head_size, bias=False) |
|
|
query = nn.Linear(C, head_size, bias=False) |
|
|
value = nn.Linear(C, head_size, bias=False) |
|
|
k = key(x) |
|
|
q = query(x) |
|
|
wei = q @ k.transpose(-2, -1) |
|
|
|
|
|
tril = torch.tril(torch.ones(T, T)) |
|
|
|
|
|
wei = wei.masked_fill(tril == 0, float('-inf')) |
|
|
wei = F.softmax(wei, dim=-1) |
|
|
|
|
|
v = value(x) |
|
|
out = wei @ v |
|
|
|
|
|
|
|
|
out.shape |
|
|
|
|
|
wei[0] |
|
|
|
|
|
"""Notes: |
|
|
- Attention is a **communication mechanism**. Can be seen as nodes in a directed graph looking at each other and aggregating information with a weighted sum from all nodes that point to them, with data-dependent weights. |
|
|
- There is no notion of space. Attention simply acts over a set of vectors. This is why we need to positionally encode tokens. |
|
|
- Each example across batch dimension is of course processed completely independently and never "talk" to each other |
|
|
- In an "encoder" attention block just delete the single line that does masking with `tril`, allowing all tokens to communicate. This block here is called a "decoder" attention block because it has triangular masking, and is usually used in autoregressive settings, like language modeling. |
|
|
- "self-attention" just means that the keys and values are produced from the same source as queries. In "cross-attention", the queries still get produced from x, but the keys and values come from some other, external source (e.g. an encoder module) |
|
|
- "Scaled" attention additional divides `wei` by 1/sqrt(head_size). This makes it so when input Q,K are unit variance, wei will be unit variance too and Softmax will stay diffuse and not saturate too much. Illustration below |
|
|
""" |
|
|
|
|
|
k = torch.randn(B,T,head_size) |
|
|
q = torch.randn(B,T,head_size) |
|
|
wei = q @ k.transpose(-2, -1) * head_size**-0.5 |
|
|
|
|
|
k.var() |
|
|
|
|
|
q.var() |
|
|
|
|
|
wei.var() |
|
|
|
|
|
torch.softmax(torch.tensor([0.1, -0.2, 0.3, -0.2, 0.5]), dim=-1) |
|
|
|
|
|
torch.softmax(torch.tensor([0.1, -0.2, 0.3, -0.2, 0.5])*8, dim=-1) |
|
|
|
|
|
class LayerNorm1d: |
|
|
|
|
|
def __init__(self, dim, eps=1e-5, momentum=0.1): |
|
|
self.eps = eps |
|
|
self.gamma = torch.ones(dim) |
|
|
self.beta = torch.zeros(dim) |
|
|
|
|
|
def __call__(self, x): |
|
|
|
|
|
xmean = x.mean(1, keepdim=True) |
|
|
xvar = x.var(1, keepdim=True) |
|
|
xhat = (x - xmean) / torch.sqrt(xvar + self.eps) |
|
|
self.out = self.gamma * xhat + self.beta |
|
|
return self.out |
|
|
|
|
|
def parameters(self): |
|
|
return [self.gamma, self.beta] |
|
|
|
|
|
torch.manual_seed(1337) |
|
|
module = LayerNorm1d(100) |
|
|
x = torch.randn(32, 100) |
|
|
x = module(x) |
|
|
x.shape |
|
|
|
|
|
x[:,0].mean(), x[:,0].std() |
|
|
|
|
|
x[0,:].mean(), x[0,:].std() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""### Full finished code, for reference |
|
|
|
|
|
You may want to refer directly to the git repo instead though. |
|
|
""" |
|
|
|
|
|
import torch |
|
|
import torch.nn as nn |
|
|
from torch.nn import functional as F |
|
|
|
|
|
|
|
|
batch_size = 16 |
|
|
block_size = 32 |
|
|
max_iters = 5000 |
|
|
eval_interval = 100 |
|
|
learning_rate = 1e-3 |
|
|
device = 'cuda' if torch.cuda.is_available() else 'cpu' |
|
|
eval_iters = 200 |
|
|
n_embd = 64 |
|
|
n_head = 4 |
|
|
n_layer = 4 |
|
|
dropout = 0.0 |
|
|
|
|
|
|
|
|
torch.manual_seed(1337) |
|
|
|
|
|
|
|
|
with open('input.txt', 'r', encoding='utf-8') as f: |
|
|
text = f.read() |
|
|
|
|
|
|
|
|
chars = sorted(list(set(text))) |
|
|
vocab_size = len(chars) |
|
|
|
|
|
stoi = { ch:i for i,ch in enumerate(chars) } |
|
|
itos = { i:ch for i,ch in enumerate(chars) } |
|
|
encode = lambda s: [stoi[c] for c in s] |
|
|
decode = lambda l: ''.join([itos[i] for i in l]) |
|
|
|
|
|
|
|
|
data = torch.tensor(encode(text), dtype=torch.long) |
|
|
n = int(0.9*len(data)) |
|
|
train_data = data[:n] |
|
|
val_data = data[n:] |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
@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 Head(nn.Module): |
|
|
""" one head of self-attention """ |
|
|
|
|
|
def __init__(self, head_size): |
|
|
super().__init__() |
|
|
self.key = nn.Linear(n_embd, head_size, bias=False) |
|
|
self.query = nn.Linear(n_embd, head_size, bias=False) |
|
|
self.value = nn.Linear(n_embd, head_size, bias=False) |
|
|
self.register_buffer('tril', torch.tril(torch.ones(block_size, block_size))) |
|
|
|
|
|
self.dropout = nn.Dropout(dropout) |
|
|
|
|
|
def forward(self, x): |
|
|
B,T,C = x.shape |
|
|
k = self.key(x) |
|
|
q = self.query(x) |
|
|
|
|
|
wei = q @ k.transpose(-2,-1) * C**-0.5 |
|
|
wei = wei.masked_fill(self.tril[:T, :T] == 0, float('-inf')) |
|
|
wei = F.softmax(wei, dim=-1) |
|
|
wei = self.dropout(wei) |
|
|
|
|
|
v = self.value(x) |
|
|
out = wei @ v |
|
|
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)]) |
|
|
self.proj = nn.Linear(n_embd, n_embd) |
|
|
self.dropout = nn.Dropout(dropout) |
|
|
|
|
|
def forward(self, x): |
|
|
out = torch.cat([h(x) for h in self.heads], dim=-1) |
|
|
out = self.dropout(self.proj(out)) |
|
|
return out |
|
|
|
|
|
class FeedFoward(nn.Module): |
|
|
""" a simple linear layer followed by a non-linearity """ |
|
|
|
|
|
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(dropout), |
|
|
) |
|
|
|
|
|
def forward(self, x): |
|
|
return self.net(x) |
|
|
|
|
|
class Block(nn.Module): |
|
|
""" Transformer block: communication followed by computation """ |
|
|
|
|
|
def __init__(self, n_embd, n_head): |
|
|
|
|
|
super().__init__() |
|
|
head_size = n_embd // n_head |
|
|
self.sa = MultiHeadAttention(n_head, head_size) |
|
|
self.ffwd = FeedFoward(n_embd) |
|
|
self.ln1 = nn.LayerNorm(n_embd) |
|
|
self.ln2 = nn.LayerNorm(n_embd) |
|
|
|
|
|
def forward(self, x): |
|
|
x = x + self.sa(self.ln1(x)) |
|
|
x = x + self.ffwd(self.ln2(x)) |
|
|
return x |
|
|
|
|
|
|
|
|
class BigramLanguageModel(nn.Module): |
|
|
|
|
|
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=n_head) for _ in range(n_layer)]) |
|
|
self.ln_f = nn.LayerNorm(n_embd) |
|
|
self.lm_head = nn.Linear(n_embd, vocab_size) |
|
|
|
|
|
def forward(self, idx, targets=None): |
|
|
B, T = idx.shape |
|
|
|
|
|
|
|
|
tok_emb = self.token_embedding_table(idx) |
|
|
pos_emb = self.position_embedding_table(torch.arange(T, device=device)) |
|
|
x = tok_emb + pos_emb |
|
|
x = self.blocks(x) |
|
|
x = self.ln_f(x) |
|
|
logits = self.lm_head(x) |
|
|
|
|
|
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): |
|
|
|
|
|
idx_cond = idx[:, -block_size:] |
|
|
|
|
|
logits, loss = self(idx_cond) |
|
|
|
|
|
logits = logits[:, -1, :] |
|
|
|
|
|
probs = F.softmax(logits, dim=-1) |
|
|
|
|
|
idx_next = torch.multinomial(probs, num_samples=1) |
|
|
|
|
|
idx = torch.cat((idx, idx_next), dim=1) |
|
|
return idx |
|
|
|
|
|
model = BigramLanguageModel() |
|
|
m = model.to(device) |
|
|
|
|
|
print(sum(p.numel() for p in m.parameters())/1e6, 'M parameters') |
|
|
|
|
|
|
|
|
optimizer = torch.optim.AdamW(model.parameters(), lr=learning_rate) |
|
|
|
|
|
for iter in range(max_iters): |
|
|
|
|
|
|
|
|
if iter % eval_interval == 0 or iter == max_iters - 1: |
|
|
losses = estimate_loss() |
|
|
print(f"step {iter}: train loss {losses['train']:.4f}, val loss {losses['val']:.4f}") |
|
|
|
|
|
|
|
|
xb, yb = get_batch('train') |
|
|
|
|
|
|
|
|
logits, loss = model(xb, yb) |
|
|
optimizer.zero_grad(set_to_none=True) |
|
|
loss.backward() |
|
|
optimizer.step() |
|
|
|
|
|
|
|
|
context = torch.zeros((1, 1), dtype=torch.long, device=device) |
|
|
print(decode(m.generate(context, max_new_tokens=2000)[0].tolist())) |
|
|
|
|
|
|