-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
144 lines (112 loc) · 4.21 KB
/
Copy pathmodel.py
File metadata and controls
144 lines (112 loc) · 4.21 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
import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Dict
# embedding layer
class EmbeddingLayer(nn.Module):
def __init__(self, vocab_size, max_context, embed_dim):
super().__init__()
self.semantic_embedding = nn.Embedding(vocab_size, embed_dim)
self.positional_embedding = nn.Embedding(max_context, embed_dim)
def forward(self, x):
# indices are of shape (B, T)
x1 = self.semantic_embedding(x)
# positions a list of ints from 0 to block_size-1
_, max_context = x.shape
positions = torch.arange(max_context, device=x.device)
x2 = self.positional_embedding(positions)
# output are of shape (B, T, embed_dim)
x = x1 + x2
return x
# self-attention layer
class SelfAttentionLayer(nn.Module):
def __init__(self, embed_dim, head_size, dropout):
super().__init__()
self.query = nn.Linear(embed_dim, head_size, bias=False)
self.key = nn.Linear(embed_dim, head_size, bias=False)
self.value = nn.Linear(embed_dim, head_size, bias=False)
self.dropout = nn.Dropout(dropout)
def forward(self, x):
# compute q, k, and v
q = self.query(x)
k = self.key(x)
v = self.value(x)
# construct attention scores matrix
scores = q @ k.transpose(1, 2)
# get shape and scale scores
_, T, A = q.shape
scores = scores / (A ** 0.5)
# causal masking (decoder only)
mask = torch.ones(T, T, device=x.device)
mask = torch.tril(mask)
mask = mask == 0
scores = scores.masked_fill(mask, float('-inf'))
# generate output of shape (B, T, head_size)
scores = F.softmax(scores, dim=-1)
scores = self.dropout(scores)
x = scores @ v
return x
# multi-head attention
class MHA(nn.Module):
def __init__(self, embed_dim, attention_dim, num_heads, dropout):
super().__init__()
head_size = attention_dim // num_heads
self.mha = nn.ModuleList()
for _ in range(num_heads):
self.mha.append(SelfAttentionLayer(embed_dim, head_size, dropout))
self.projection = nn.Linear(head_size * num_heads, embed_dim)
self.dropout = nn.Dropout(dropout)
def forward(self, x):
results = []
for head in self.mha:
results.append(head(x))
x = torch.cat(results, dim=-1)
x = self.projection(x)
x = self.dropout(x)
return x
# feed-forward network
class FFN(nn.Module):
def __init__(self, embed_dim, ffn_dim, dropout):
super().__init__()
self.mlp = nn.Sequential(
nn.Linear(embed_dim, ffn_dim),
nn.ReLU()
)
self.projection = nn.Linear(ffn_dim, embed_dim)
self.dropout = nn.Dropout(dropout)
def forward(self, x):
x = self.mlp(x)
x = self.projection(x)
x = self.dropout(x)
return x
# transformer block
class TransformerBlock(nn.Module):
def __init__(self, embed_dim, attention_dim, num_heads, ffn_dim, dropout):
super().__init__()
self.mha = MHA(embed_dim, attention_dim, num_heads, dropout)
self.ffn = FFN(embed_dim, ffn_dim, dropout)
self.ln1 = nn.LayerNorm(embed_dim)
self.ln2 = nn.LayerNorm(embed_dim)
def forward(self, x):
# residual connections
x = x + self.mha(self.ln1(x))
x = x + self.ffn(self.ln2(x))
return x
# GPT model
class GPT(nn.Module):
def __init__(self, vocab_size, max_context, embed_dim, attention_dim, num_heads, ffn_dim, dropout, num_blocks):
super().__init__()
self.embed = EmbeddingLayer(vocab_size, max_context, embed_dim)
self.blocks = nn.Sequential()
for _ in range(num_blocks):
self.blocks.append(TransformerBlock(embed_dim, attention_dim, num_heads, ffn_dim, dropout))
self.ln = nn.LayerNorm(embed_dim)
self.projection = nn.Linear(embed_dim, vocab_size)
self.max_context = max_context
def forward(self, x):
x = self.embed(x)
x = self.blocks(x)
x = self.ln(x)
x = self.projection(x)
# return logits (not probabilities)
return x