-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
87 lines (72 loc) · 3.05 KB
/
Copy pathmodel.py
File metadata and controls
87 lines (72 loc) · 3.05 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
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
class Attention(nn.Module):
def __init__(self, dim, head_dim, dropout):
super().__init__()
self.head_dim = head_dim
self.w_q = nn.Linear(dim, head_dim)
self.w_k = nn.Linear(dim, head_dim)
self.w_v = nn.Linear(dim, head_dim)
self.attn_dropout = nn.Dropout(dropout)
def forward(self, x):
q = self.w_q(x)
k = self.w_k(x)
v = self.w_v(x)
attn_scores = q @ k.transpose(-1, -2) / math.sqrt(self.head_dim)
mask = torch.triu(torch.full((x.shape[1], x.shape[1]), float('-inf'), device=x.device), diagonal=1)
attn_scores = attn_scores + mask
attn_w = self.attn_dropout(F.softmax(attn_scores, dim=-1))
return attn_w @ v
class MultiHeadAttention(nn.Module):
def __init__(self, num_heads, dim, dropout):
super().__init__()
self.num_heads = num_heads
self.head_dim = dim // num_heads
self.heads = nn.ModuleList([Attention(dim, self.head_dim, dropout) for _ in range(num_heads)])
self.multihead_proj = nn.Linear(dim, dim)
self.proj_dropout = nn.Dropout(dropout)
def forward(self, x):
head_outs = [head(x) for head in self.heads]
multihead_out = torch.cat(head_outs, dim=-1)
return self.proj_dropout(self.multihead_proj(multihead_out))
class FFN(nn.Module):
def __init__(self, dim, expansion, dropout):
super().__init__()
self.fc1 = nn.Linear(dim, dim * expansion)
self.fc2 = nn.Linear(dim * expansion, dim)
self.gelu = nn.GELU()
self.dropout = nn.Dropout(dropout)
def forward(self, x):
return self.dropout(self.fc2(self.gelu(self.fc1(x))))
class TransformerBlock(nn.Module):
def __init__(self, dim, num_heads, expansion, dropout):
super().__init__()
self.ln1 = nn.LayerNorm(dim)
self.multihead_attn = MultiHeadAttention(num_heads, dim, dropout)
self.ln2 = nn.LayerNorm(dim)
self.ffn = FFN(dim, expansion, dropout)
def forward(self, x):
x = x + self.multihead_attn(self.ln1(x))
x = x + self.ffn(self.ln2(x))
return x
class GPT(nn.Module):
def __init__(self, vocab_size, dim, num_heads, expansion, num_layers, context_length, dropout=0.1):
super().__init__()
self.token_embedding = nn.Embedding(vocab_size, dim)
self.pos_embedding = nn.Embedding(context_length, dim)
self.emb_dropout = nn.Dropout(dropout)
self.transformer_blocks = nn.ModuleList([
TransformerBlock(dim, num_heads, expansion, dropout) for _ in range(num_layers)
])
self.ln_head = nn.LayerNorm(dim)
self.linear_head = nn.Linear(dim, vocab_size)
def forward(self, x):
tok_emb = self.token_embedding(x)
pos_emb = self.pos_embedding(torch.arange(x.shape[1], device=x.device))
x = self.emb_dropout(tok_emb + pos_emb)
for block in self.transformer_blocks:
x = block(x)
x = self.ln_head(x)
return self.linear_head(x)