-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
132 lines (99 loc) · 4.13 KB
/
model.py
File metadata and controls
132 lines (99 loc) · 4.13 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
import torch
import torch.nn as nn
import torch.nn.functional as F
class ModelConfig:
vocab_size = 32000
d_model = 348
n_layer = 4
n_head = 6
d_ff = 1392
max_len = 256
dropout = 0.3
class CausalSelfAttention(nn.Module):
def __init__(self, config):
super().__init__()
assert config.d_model % config.n_head == 0
self.c_attn = nn.Linear(config.d_model, 3 * config.d_model)
self.c_proj = nn.Linear(config.d_model, config.d_model)
self.n_head = config.n_head
self.d_model = config.d_model
self.max_len = config.max_len
self.dropout = nn.Dropout(config.dropout)
self.register_buffer("bias", torch.tril(torch.ones(config.max_len, config.max_len))
.view(1, 1, config.max_len, config.max_len))
def forward(self, x):
B, T, C = x.size()
qkv = self.c_attn(x)
q, k, v = qkv.split(self.d_model, dim=2)
k = k.view(B, T, self.n_head, C // self.n_head).transpose(1, 2)
q = q.view(B, T, self.n_head, C // self.n_head).transpose(1, 2)
v = v.view(B, T, self.n_head, C // self.n_head).transpose(1, 2)
#att = (q @ k.transpose(-2, -1)) * (1.0 / k.size(-1)**2)
att = (q @ k.transpose(-2, -1)) * (1.0 / (k.size(-1)**0.5))
att = att.masked_fill(self.bias[:, :, :T, :T] == 0, float('-inf'))
att = F.softmax(att, dim=-1)
att = self.dropout(att)
y = att @ v
y = y.transpose(1, 2).contiguous().view(B, T, C)
return self.c_proj(y)
class FeedForward(nn.Module):
def __init__(self, config):
super().__init__()
self.net = nn.Sequential(
nn.Linear(config.d_model, config.d_ff),
nn.GELU(),
nn.Linear(config.d_ff, config.d_model),
nn.Dropout(config.dropout),
)
def forward(self, x):
return self.net(x)
class TransformerBlock(nn.Module):
def __init__(self, config):
super().__init__()
self.ln1 = nn.LayerNorm(config.d_model)
self.attn = CausalSelfAttention(config)
self.ln2 = nn.LayerNorm(config.d_model)
self.mlp = FeedForward(config)
def forward(self, x):
x = x + self.attn(self.ln1(x))
x = x + self.mlp(self.ln2(x))
return x
class CodeCompletionTransformer(nn.Module):
def __init__(self, config):
super().__init__()
self.config = config
self.token_embedding = nn.Embedding(config.vocab_size, config.d_model)
self.position_embedding = nn.Embedding(config.max_len, config.d_model)
self.dropout = nn.Dropout(config.dropout)
self.blocks = nn.Sequential(*[TransformerBlock(config) for _ in range(config.n_layer)])
self.ln_f = nn.LayerNorm(config.d_model)
self.lm_head = nn.Linear(config.d_model, config.vocab_size, bias=False)
self.token_embedding.weight = self.lm_head.weight
self.apply(self._init_weights)
def _init_weights(self, module):
if isinstance(module, nn.Linear):
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
if module.bias is not None:
torch.nn.init.zeros_(module.bias)
elif isinstance(module, nn.Embedding):
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
elif isinstance(module, nn.LayerNorm):
torch.nn.init.zeros_(module.bias)
torch.nn.init.ones_(module.weight)
def forward(self, idx):
B, T = idx.size()
assert T <= self.config.max_len, f"Sequence length {T} exceeds model max_len {self.config.max_len}"
pos = torch.arange(0, T, dtype=torch.long, device=idx.device)
tok_emb = self.token_embedding(idx)
pos_emb = self.position_embedding(pos)
x = self.dropout(tok_emb + pos_emb)
x = self.blocks(x)
x = self.ln_f(x)
logits = self.lm_head(x)
return logits
if __name__ == "__main__":
# Quick sanity check: run a dummy forward pass
config = ModelConfig()
model = CodeCompletionTransformer(config)
dummy_input = torch.randint(0, config.vocab_size, (4, config.max_len))
print(model(dummy_input).shape)