-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodules.py
More file actions
325 lines (256 loc) · 13.5 KB
/
Copy pathmodules.py
File metadata and controls
325 lines (256 loc) · 13.5 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
from torch import nn
import torch
from transformers import PretrainedConfig
import torch.nn.functional as F
from einops import einsum, rearrange
import math
from copy import deepcopy
from time import time
class MLP(nn.Module):
"""Standard MLP module"""
def __init__(self, config):
super().__init__()
self.mlp = nn.Sequential(
nn.Linear(config.hidden_size, config.intermediate_size, bias=config.bias),
nn.ReLU(),
nn.Linear(config.intermediate_size, config.hidden_size, bias=config.bias),
)
def forward(self, x):
return self.mlp(x)
class Router(nn.Module):
"""Router module for mixture models"""
def __init__(self, config):
super().__init__()
self.config = config
self.num_experts_per_token = config.num_experts_per_token
self.hidden_size = config.hidden_size
self.num_experts = config.num_experts
self.expert_embeddings = nn.Linear(self.hidden_size, self.num_experts)
torch.nn.init.xavier_uniform_(self.expert_embeddings.weight)
def forward(self, x):
routing_weights = self.expert_embeddings(x)
routing_weights = F.softmax(routing_weights, dim=-1)
return routing_weights
class MoE(nn.Module):
"""Mixture-of-Experts feed forward"""
def __init__(self, config):
super().__init__()
self.config = config
self.num_experts = config.num_experts
self.hidden_size = config.hidden_size
self.intermediate_size = config.intermediate_size
self.num_experts_per_token = config.num_experts_per_token
self.capacity_factor = config.capacity_factor
self.experts_inter = nn.Parameter(torch.randn(self.num_experts, self.hidden_size, self.intermediate_size))
self.experts_out = nn.Parameter(torch.randn(self.num_experts, self.intermediate_size, self.hidden_size))
self.expert_activation = nn.ReLU()
self.router = Router(config)
def forward(self, x):
batch_size, seq_len, hidden_size = x.shape
expert_capacity = math.ceil(batch_size * seq_len / self.num_experts * self.capacity_factor)
routing_weights = self.router(x)
x_flat = x.view(-1, hidden_size)
weights_flat = routing_weights.view(-1, self.num_experts)
# Get top-k experts for each token
non_zero_experts = torch.topk(weights_flat, self.num_experts_per_token, dim=-1)
topk_flat_weights = torch.zeros_like(weights_flat).scatter_(-1, non_zero_experts.indices, non_zero_experts.values)
routing_mask = (topk_flat_weights > 0).long()
# Assign capacity tokens to experts
sorted_mask, sorted_indices = torch.sort(routing_mask, dim=0, descending=True)
sorted_mask = sorted_mask[:expert_capacity]
sorted_indices = sorted_indices[:expert_capacity]
used_tokens_indices = sorted_indices * sorted_mask # zero indices for zero-weights tokens
used_tokens_indices = used_tokens_indices.flatten()
inputs_tokens = torch.index_select(x_flat, 0, used_tokens_indices) * sorted_mask.flatten().unsqueeze(-1)
inputs_tokens = inputs_tokens.view(expert_capacity, self.num_experts, hidden_size)
inputs_weights = torch.gather(topk_flat_weights, 0, sorted_indices)
# Compute expert outputs
out = einsum(inputs_tokens, self.experts_inter, 'c e h, e h i -> c e i')
out = self.expert_activation(out)
out = einsum(out, self.experts_out, 'c e i, e i h -> c e h')
out = out * inputs_weights.unsqueeze(-1)
out = out.reshape(-1, hidden_size)
# Collect expert token embeddings back to original shape
expert_embeddings = torch.zeros_like(x_flat, dtype=out.dtype).index_add_(0, used_tokens_indices, out)
expert_embeddings = expert_embeddings.view(batch_size, seq_len, hidden_size)
return expert_embeddings
class MoDE(nn.Module):
"""Integrated Mixture-of-Depths-and-Experts feed forward"""
def __init__(self, config):
super().__init__()
self.config = config
self.num_experts = config.num_experts # includes one no-op expert as the last expert
self.hidden_size = config.hidden_size
self.intermediate_size = config.intermediate_size
self.num_experts_per_token = config.num_experts_per_token
self.capacity_factor = config.capacity_factor
self.experts_inter = nn.Parameter(torch.randn(self.num_experts-1, self.hidden_size, self.intermediate_size))
self.experts_out = nn.Parameter(torch.randn(self.num_experts-1, self.intermediate_size, self.hidden_size))
self.expert_activation = nn.ReLU()
self.router = Router(config)
def forward(self, x):
batch_size, seq_len, hidden_size = x.shape
expert_capacity = math.ceil(batch_size * seq_len / self.num_experts * self.capacity_factor)
routing_weights = self.router(x) # [batch_size, seq_len, num_experts]
x_flat = x.view(-1, hidden_size) # [batch_size * seq_len, hidden_size]
weights_flat = routing_weights.view(-1, self.num_experts) # [batch_size * seq_len, num_experts]
# Get top-k experts for each token
non_zero_experts = torch.topk(weights_flat, self.num_experts_per_token, dim=-1)
topk_flat_weights = torch.zeros_like(weights_flat).scatter_(-1, non_zero_experts.indices, non_zero_experts.values)
# Get no-op tokens
noop_flat_weights = topk_flat_weights[:, -1:] # no-op expert, [batch_size*seq_len, 1]
noop_routed = x_flat * noop_flat_weights # zeros for non-routed tokens
# Assign capacity tokens to experts
experts_flat_weights = topk_flat_weights[:, :-1] # exclude no-op expert, [batch_size*seq_len, (num_experts-1)]
experts_routing_mask = (experts_flat_weights > 0).long()
sorted_mask, sorted_indices = torch.sort(experts_routing_mask, dim=0, descending=True) # stable=False by default
sorted_mask = sorted_mask[:expert_capacity] # [expert_capacity, num_experts-1]
sorted_indices = sorted_indices[:expert_capacity] # [expert_capacity, num_experts-1]
used_tokens_indices = sorted_indices * sorted_mask # zero indices for zero-weights tokens
used_tokens_indices = used_tokens_indices.flatten()
inputs_tokens = torch.index_select(x_flat, 0, used_tokens_indices) * sorted_mask.flatten().unsqueeze(-1)
inputs_tokens = inputs_tokens.view(expert_capacity, self.num_experts-1, hidden_size)
inputs_weights = torch.gather(experts_flat_weights, 0, sorted_indices)
# Compute expert outputs
out = einsum(inputs_tokens, self.experts_inter, 'c e h, e h i -> c e i')
out = self.expert_activation(out)
out = einsum(out, self.experts_out, 'c e i, e i h -> c e h')
out = out * inputs_weights.unsqueeze(-1)
out = out.reshape(-1, hidden_size)
# Collect expert token embeddings back to original shape
expert_embeddings = torch.zeros_like(x_flat, dtype=out.dtype).index_add_(0, used_tokens_indices, out) # add expert embeddings
expert_embeddings += noop_routed
expert_embeddings = expert_embeddings.view(batch_size, seq_len, hidden_size)
return expert_embeddings
class MHMixtureWrapper(nn.Module):
"""Multi-Head wrapper for Mixture models"""
def __init__(self, config):
super().__init__()
self.config = config
self.hidden_size = config.hidden_size
self.num_heads = 4
self.mh_input_layer = nn.Linear(self.hidden_size, self.hidden_size)
self.mh_output_layer = nn.Linear(self.hidden_size, self.hidden_size)
self.activation = nn.ReLU()
ff_config = deepcopy(config)
ff_config.hidden_size = self.hidden_size // self.num_heads
self.ff = config.ff_cls(ff_config)
def forward(self, x):
batch_size, seq_len, hidden_size = x.shape
x = self.mh_input_layer(x)
x = self.activation(x)
# Split tokens into subtokens
x = x.view(batch_size, seq_len, self.num_heads, hidden_size // self.num_heads)
x = x.reshape(batch_size, -1, hidden_size // self.num_heads) # [batch_size, seq_len*num_heads, hidden_size//num_heads]
# Apply FF to subtokens
x = self.ff(x)
# Merge subtokens back to tokens
x = x.view(batch_size, seq_len, self.num_heads, hidden_size // self.num_heads)
x = x.reshape(batch_size, seq_len, hidden_size)
x = self.mh_output_layer(x)
x = self.activation(x)
return x
class Embedding(nn.Module):
def __init__(self, config):
super(Embedding, self).__init__()
self.word_embed = nn.Embedding(config.vocab_size, config.hidden_size)
self.pos_embed = nn.Embedding(config.max_position_embeddings, config.hidden_size)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
def forward(self, x):
batch_size, seq_length = x.shape
device = x.device
positions = torch.arange(0, seq_length).expand(
batch_size, seq_length).to(device)
embedding = self.word_embed(x) + self.pos_embed(positions)
return self.dropout(embedding)
class MHSelfAttention(nn.Module):
def __init__(self, config: PretrainedConfig):
super(MHSelfAttention, self).__init__()
self.num_attention_heads = config.num_attention_heads
self.hidden_size = config.hidden_size
self.head_size = self.hidden_size // self.num_attention_heads
self.num_attention_heads = config.num_attention_heads
self.qkv = nn.Linear(self.hidden_size, 3 * self.hidden_size, bias=False)
def forward(self, embeddings):
batch_size, seq_length, hidden_size = embeddings.size()
result = self.qkv(embeddings)
q, k, v = rearrange(result, 'b s (qkv nah hdsz) -> qkv b nah s hdsz', nah=self.num_attention_heads, qkv=3).unbind(0)
attention_scores = torch.matmul(q, k.transpose(-1, -2))
attention_scores = attention_scores / math.sqrt(hidden_size)
attention_probs = nn.Softmax(dim=-1)(attention_scores)
contextualized_layer = torch.matmul(attention_probs, v)
outputs = rearrange(contextualized_layer, 'b nah s hdsz -> b s (nah hdsz)')
return outputs
class TransformerBlock(nn.Module):
def __init__(self, config):
super().__init__()
self.attention = MHSelfAttention(config)
self.norm1 = nn.LayerNorm(config.hidden_size)
self.norm2 = nn.LayerNorm(config.hidden_size)
if config.mh_moe:
self.intermediate = MHMixtureWrapper(config)
else:
self.intermediate = config.ff_cls(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
def forward(self, x):
x = x + self.norm1(self.dropout(self.attention(x)))
x = x + self.norm2(self.dropout(self.intermediate(x)))
return x
class TransformerClassifier(nn.Module):
def __init__(self, config):
super().__init__()
self.embeddings = Embedding(config)
self.layer = nn.Sequential(*[TransformerBlock(config) for _ in range(config.num_hidden_layers)])
self.classifier = nn.Linear(config.hidden_size, config.num_labels)
def forward(self, input_ids, labels=None):
embedding_output = self.embeddings(input_ids)
encoding = self.layer(embedding_output)
pooled_encoding = encoding.mean(dim=1)
logits = self.classifier(pooled_encoding)
loss = F.cross_entropy(logits, labels) if labels is not None else None
return {
'loss': loss,
'logits': logits,
}
class TransformerForMaskedLanguageModeling(nn.Module):
def __init__(self, config):
super().__init__()
self.config = config
self.embeddings = Embedding(config)
self.layer = nn.Sequential(*[TransformerBlock(config) for _ in range(config.num_hidden_layers)])
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size) # Predict vocabulary tokens
def forward(self, input_ids, attention_mask=None, labels=None):
embedding_output = self.embeddings(input_ids)
encoding = self.layer(embedding_output)
logits = self.lm_head(encoding)
loss = None
if labels is not None:
# Mask the logits and labels for MLM loss calculation
active_loss = attention_mask.view(-1) == 1 # Apply attention mask to calculate loss only on masked positions
active_logits = logits.view(-1, self.config.vocab_size)[active_loss]
active_labels = labels.view(-1)[active_loss]
loss = F.cross_entropy(active_logits, active_labels)
return {
'loss': loss,
'logits': logits,
}
class TransformerForCausalLanguageModeling(nn.Module):
def __init__(self, config):
super().__init__()
self.embeddings = Embedding(config)
self.layer = nn.Sequential(*[TransformerBlock(config) for _ in range(config.num_hidden_layers)])
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size) # Predict vocabulary tokens
def forward(self, input_ids, labels=None):
embedding_output = self.embeddings(input_ids)
encoding = self.layer(embedding_output)
logits = self.lm_head(encoding)
loss = None
if labels is not None:
# Shift the logits and labels for CLM loss calculation
shift_logits = logits[..., :-1, :].contiguous()
shift_labels = labels[..., 1:].contiguous()
loss = F.cross_entropy(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
return {
'loss': loss,
'logits': logits,
}