-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
170 lines (130 loc) · 5.15 KB
/
model.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
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
from dataclasses import dataclass, asdict
import torch
import torch.nn as nn
from torch.nn import functional as F
def initialize_weight_bias(layer: nn.Module, mean: float, std: float) -> None:
layer.weight.data.normal_(mean, std)
if hasattr(layer, "bias") and layer.bias is not None:
layer.bias.data.zero_()
def _validate_config(config: dict) -> bool:
# very naive way of validating
required_keys = [
"embed_size",
"context_length",
"n_layer",
"n_head",
"vocab_size",
]
for _key in required_keys:
if _key not in config:
raise KeyError(f"key {_key} not found in config")
assert config[_key] > 0, f"{_key} should be positive"
# TODO: Add more validations
return True
@dataclass
class KPTConfig:
context_length: int
vocab_size: int
n_layer: int
n_head: int
embed_size: int
class ReLUSquared(nn.Module):
# https://arxiv.org/pdf/2109.08668
def forward(self, x):
F.relu_(x)
return torch.square(x)
class FeedForward(nn.Module):
def __init__(self, config: KPTConfig) -> None:
super().__init__()
self.linear1 = nn.Linear(config.embed_size, config.embed_size * 4)
self.squared_relu = ReLUSquared()
self.linear2 = nn.Linear(config.embed_size * 4, config.embed_size)
# initialization
std = 0.02 * (2 * config.n_layer) ** -0.5
initialize_weight_bias(self.linear1, 0.0, std)
initialize_weight_bias(self.linear2, 0.0, std)
def forward(self, x):
x = self.linear1(x)
# squared ReLU
x = self.squared_relu(x)
x = self.linear2(x)
return x
class CausalSelfAttention(nn.Module):
def __init__(self, config: KPTConfig) -> None:
super().__init__()
self.embed_size = config.embed_size
self.n_head = config.n_head
# key, query, value projections
self.attention_layer = nn.Linear(self.embed_size, 3 * self.embed_size)
# output projection
self.out_proj = nn.Linear(self.embed_size, self.embed_size)
# initialization
std = 0.02 * (2 * config.n_layer) ** -0.5
initialize_weight_bias(self.attention_layer, 0.0, std)
initialize_weight_bias(self.out_proj, 0.0, std)
def forward(self, x):
batch_size, context_length, embed_size = x.shape
qkv = self.attention_layer(x)
q, k, v = qkv.split(self.embed_size, dim=2)
head_size = embed_size // self.n_head
# B, nheads, context_length, head_size
k = k.view(batch_size, context_length, self.n_head, head_size).transpose(1, 2)
q = q.view(batch_size, context_length, self.n_head, head_size).transpose(1, 2)
v = v.view(batch_size, context_length, self.n_head, head_size).transpose(1, 2)
# flash attention
out = F.scaled_dot_product_attention(q, k, v, is_causal=True)
out = (
out.transpose(1, 2)
.contiguous()
.view(batch_size, context_length, embed_size)
)
return self.out_proj(out)
class DecoderLayer(nn.Module):
def __init__(self, config: KPTConfig) -> None:
super().__init__()
self.layer_norm1 = nn.LayerNorm(config.embed_size)
self.attention_block = CausalSelfAttention(config)
self.layer_norm2 = nn.LayerNorm(config.embed_size)
self.feed_forward_block = FeedForward(config)
def forward(self, x):
# pre-norm
x = x + self.attention_block(self.layer_norm1(x))
x = x + self.feed_forward_block(self.layer_norm2(x))
return x
class KPT(nn.Module):
def __init__(self, config: KPTConfig):
super().__init__()
_validate_config(asdict(config))
self.config = config
self.token_embeddings = nn.Embedding(config.vocab_size, config.embed_size)
self.position_embeddings = nn.Embedding(
config.context_length, config.embed_size
)
self.decoder_layers = nn.ModuleList(
DecoderLayer(self.config) for _ in range(config.n_layer)
)
self.layer_norm = nn.LayerNorm(config.embed_size)
self.output_head = nn.Linear(config.embed_size, config.vocab_size, bias=False)
# weight sharing
self.token_embeddings.weight = self.output_head.weight
# initialization
std = 0.02
initialize_weight_bias(self.token_embeddings, 0.0, std)
initialize_weight_bias(self.position_embeddings, 0.0, std)
initialize_weight_bias(self.output_head, 0.0, std)
def forward(self, x):
batch_size, context_length = x.size()
assert context_length <= self.config.context_length
# position embeddings
pos = torch.arange(0, context_length, dtype=torch.long, device=x.device)
# context_length, embed_size
pos_emb = self.position_embeddings(pos)
# batch_size, context_length, embed_size
tok_emb = self.token_embeddings(x)
x = tok_emb + pos_emb
for decoder_layer in self.decoder_layers:
x = decoder_layer(x)
x = self.layer_norm(x)
# batch_size, context_length, vocab_size
logits = self.output_head(x)
return logits