-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt.py
61 lines (53 loc) · 2.38 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
from attention import CausalSelfAttentionBlock
import torch
from torch import nn
from torch.nn import functional as F
class MinGPT(nn.Module):
def __init__(self, vocab_size, block_size, n_embd, n_head, n_layer, dropout, device):
super().__init__()
self.block_size = block_size
self.device = device
self.token_embedding_table = nn.Embedding(vocab_size, n_embd)
self.position_embedding_table = nn.Embedding(block_size, n_embd)
self.blocks = nn.Sequential(*[CausalSelfAttentionBlock(block_size, n_embd, n_head, dropout) for _ in range(n_layer)])
self.ln_f = nn.LayerNorm(n_embd)
self.lm_head = nn.Linear(n_embd, vocab_size)
def forward(self, x, targets=None):
B, T = x.shape
tok_emb = self.token_embedding_table(x) # (B, T, C)
pos_emb = self.position_embedding_table(torch.arange(T, device=self.device)) # (T, C)
x = tok_emb + pos_emb
x = self.blocks(x)
x = self.ln_f(x)
if targets is not None:
# if we are given some desired targets also calculate the loss
logits = self.lm_head(x)
loss = F.cross_entropy(logits.view(-1, logits.size(-1)), targets.view(-1), ignore_index=-1)
else:
# inference-time mini-optimization: only forward the lm_head on the very last position
logits = self.lm_head(x[:, [-1], :]) # note: using list [-1] to preserve the time dim
loss = None
return logits, loss
@torch.no_grad()
def generate(self, idx, max_new_tokens: int):
# idx: (B, T) array of indices in the current context
for _ in range(max_new_tokens):
# crop idx to last block_size tokens
# (B, T) where T = min(T, block_size)
idx_cond = idx[:, -self.block_size:]
# get predictions
# (B, block_size, C)
pred, _ = self(idx_cond)
# focus on last time step
# (B, C)
logits = pred[:, -1,:]
# softmax over C to get probability distribution
# (B, C) still
probs = nn.functional.softmax(logits, dim=-1)
# sample from distribution
# (B, 1)
idx_next = torch.multinomial(probs, num_samples=1)
# append sample
# (B, T) + (B, 1) = (B, T+1)
idx = torch.cat((idx, idx_next), dim=1)
return idx