-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.py
More file actions
194 lines (124 loc) · 4.44 KB
/
Copy pathdecoder.py
File metadata and controls
194 lines (124 loc) · 4.44 KB
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
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
# ============================================================
# Causal Mask
# ============================================================
def causal_mask(seq_len, device):
"""
Prevent attending to future tokens.
shape: (1, 1, seq_len, seq_len)
"""
mask = torch.tril(torch.ones(seq_len, seq_len, device=device))
mask = mask.unsqueeze(0).unsqueeze(0)
return mask
# ============================================================
# Multi-Head Self Attention
# ============================================================
class MultiHeadSelfAttention(nn.Module):
def __init__(self, d_model, n_heads):
super().__init__()
assert d_model % n_heads == 0
self.d_model = d_model
self.n_heads = n_heads
self.head_dim = d_model // n_heads
# combined projection (faster, used in real models)
self.qkv = nn.Linear(d_model, 3 * d_model)
self.out_proj = nn.Linear(d_model, d_model)
def forward(self, x, mask):
B, T, C = x.shape
qkv = self.qkv(x) # (B, T, 3C)
qkv = qkv.view(B, T, 3, self.n_heads, self.head_dim)
qkv = qkv.permute(2, 0, 3, 1, 4)
Q, K, V = qkv[0], qkv[1], qkv[2]
# attention scores
scores = (Q @ K.transpose(-2, -1)) / math.sqrt(self.head_dim)
scores = scores.masked_fill(mask[:, :, :T, :T] == 0, float('-inf'))
attn = torch.softmax(scores, dim=-1)
out = attn @ V
out = out.transpose(1, 2).contiguous().view(B, T, C)
out = self.out_proj(out)
return out
# ============================================================
# Feedforward Network
# ============================================================
class FeedForward(nn.Module):
def __init__(self, d_model, d_ff):
super().__init__()
self.net = nn.Sequential(
nn.Linear(d_model, d_ff),
nn.GELU(),
nn.Linear(d_ff, d_model)
)
def forward(self, x):
return self.net(x)
# ============================================================
# Decoder Block
# ============================================================
class DecoderBlock(nn.Module):
def __init__(self, d_model, n_heads, d_ff):
super().__init__()
self.ln1 = nn.LayerNorm(d_model)
self.attn = MultiHeadSelfAttention(d_model, n_heads)
self.ln2 = nn.LayerNorm(d_model)
self.ffn = FeedForward(d_model, d_ff)
def forward(self, x, mask):
# attention
x = x + self.attn(self.ln1(x), mask)
# feedforward
x = x + self.ffn(self.ln2(x))
return x
# ============================================================
# Full Decoder-Only Transformer
# ============================================================
class DecoderOnlyTransformer(nn.Module):
def __init__(
self,
vocab_size,
d_model=768,
n_heads=12,
n_layers=12,
d_ff=3072,
max_seq_len=1024
):
super().__init__()
self.token_embedding = nn.Embedding(vocab_size, d_model)
self.position_embedding = nn.Embedding(max_seq_len, d_model)
self.blocks = nn.ModuleList([
DecoderBlock(d_model, n_heads, d_ff)
for _ in range(n_layers)
])
self.ln_final = nn.LayerNorm(d_model)
self.lm_head = nn.Linear(d_model, vocab_size, bias=False)
self.max_seq_len = max_seq_len
def forward(self, tokens):
B, T = tokens.shape
device = tokens.device
pos = torch.arange(0, T, device=device)
pos = pos.unsqueeze(0)
x = self.token_embedding(tokens) + self.position_embedding(pos)
mask = causal_mask(self.max_seq_len, device)
for block in self.blocks:
x = block(x, mask)
x = self.ln_final(x)
logits = self.lm_head(x)
return logits
# ============================================================
# Test run
# ============================================================
if __name__ == "__main__":
model = DecoderOnlyTransformer(
vocab_size=50000,
d_model=512,
n_heads=8,
n_layers=6,
d_ff=2048,
max_seq_len=256
)
x = torch.randint(0, 50000, (2, 128))
logits = model(x)
print(logits.shape)
# expected:
# (batch, seq_len, vocab_size)
### code written by chat --- just a reference implementation