-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt.py
138 lines (93 loc) · 3.72 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
import torch
import torch.nn as nn
import torch.nn.functional as F
class AttentionHead(nn.Module):
def __init__(self, Config):
super().__init__()
self.block_size = Config.block_size
self.n_embed = Config.n_embed
self.head_size = Config.head_size
self.key = nn.Linear(self.n_embed, self.head_size, bias=False)
self.query = nn.Linear(self.n_embed, self.head_size, bias=False)
self.value = nn.Linear(self.n_embed, self.head_size, bias=False)
self.register_buffer(
'tril',
torch.tril(torch.ones(self.block_size,self.block_size))
)
self.dropout = nn.Dropout(Config.attn_dropout)
def forward(self, x):
B,T,C = x.shape
k = self.key(x)
q = self.query(x)
wei = [email protected](-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):
def __init__(self, Config):
super().__init__()
self.n_heads = Config.n_heads
self.head_size = Config.head_size
self.heads = nn.ModuleList([AttentionHead(Config) for _ in range(self.n_heads)])
self.projection = nn.Linear(Config.n_embed, Config.n_embed)
self.dropout = nn.Dropout(Config.attn_dropout)
def forward(self,x):
x = torch.cat([h(x) for h in self.heads],dim=-1)
x = self.projection(x)
x = self.dropout(x)
return x
class FeedForward(nn.Module):
def __init__(self, Config):
super().__init__()
self.net = nn.Sequential(
nn.Linear(Config.n_embed,Config.n_embed * 4),
nn.ReLU(),
nn.Linear(Config.n_embed * 4, Config.n_embed), # projection
nn.Dropout(Config.block_dropout)
)
def forward(self,x):
return self.net(x)
class TransformerBlock(nn.Module):
def __init__(self, Config):
super().__init__()
self.attn = MultiHeadAttention(Config)
self.ff = FeedForward(Config)
self.ln1 = nn.LayerNorm(Config.n_embed)
self.ln2 = nn.LayerNorm(Config.n_embed)
def forward(self,x):
x = x + self.attn(self.ln1(x))
x = x + self.ff(self.ln2(x))
return x
class ShakespeareGPT(nn.Module):
def __init__(self,Config):
super().__init__()
self.Config = Config
self.n_embed = Config.n_embed
self.block_size = Config.block_size
self.token_embedding_table = nn.Embedding(Config.vocab_size,self.n_embed)
self.pos_embedding_table = nn.Embedding(self.block_size, self.n_embed)
self.blocks = nn.Sequential(
*[TransformerBlock(Config)]*Config.n_layers,
nn.LayerNorm(self.n_embed)
)
self.lm_head = nn.Linear(self.n_embed,Config.vocab_size)
def forward(self,idx):
B,T = idx.shape
token_embs = self.token_embedding_table(idx)
pos_embs = self.pos_embedding_table(torch.arange(T,device=self.Config.device))
x = token_embs + pos_embs
x = self.blocks(x)
logits = self.lm_head(x)
return logits
def generate(self,idx,total):
for _ in range(total):
idx_cond = idx[:, -self.block_size:]
logits= 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