Update moep_llama.py
Browse files- moep_llama.py +42 -14
moep_llama.py
CHANGED
|
@@ -19,6 +19,12 @@ from transformers.modeling_outputs import (
|
|
| 19 |
@dataclass
|
| 20 |
class BaseModelOutputWithAux(BaseModelOutput):
|
| 21 |
aux_loss: Optional[torch.Tensor] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
def create_causal_mask(seq_len: int, device=None, dtype=torch.float32):
|
|
@@ -183,7 +189,9 @@ class Top_k_Router(nn.Module):
|
|
| 183 |
f_mean = one_hot.float().mean(dim=0) / float(self.k)
|
| 184 |
aux_loss = (E * (p_mean * f_mean).sum())
|
| 185 |
|
| 186 |
-
|
|
|
|
|
|
|
| 187 |
|
| 188 |
|
| 189 |
class MoE_Block(nn.Module):
|
|
@@ -201,7 +209,7 @@ class MoE_Block(nn.Module):
|
|
| 201 |
T = B * S
|
| 202 |
x_flat = x.view(T, D)
|
| 203 |
|
| 204 |
-
topk_idx, topk_prob, aux_loss = self.router(x_flat) # [T,k], [T,k]
|
| 205 |
y_flat = x_flat.new_zeros((T, self.dim_out))
|
| 206 |
|
| 207 |
for slot in range(self.k):
|
|
@@ -214,7 +222,7 @@ class MoE_Block(nn.Module):
|
|
| 214 |
y_e = self.experts[e](x_flat.index_select(0, idx)) # [Ne, dim_out]
|
| 215 |
y_flat.index_add_(0, idx, y_e * e_prob.index_select(0, idx))
|
| 216 |
|
| 217 |
-
return y_flat.view(B, S, self.dim_out), aux_loss
|
| 218 |
|
| 219 |
|
| 220 |
class ParallelLayer(nn.Module):
|
|
@@ -233,13 +241,13 @@ class ParallelLayer(nn.Module):
|
|
| 233 |
Y = torch.stack([y.view(T, D) for y in outs], dim=1) # [T, n_blocks, D]
|
| 234 |
|
| 235 |
x_flat = x.view(T, D)
|
| 236 |
-
topk_idx, topk_prob, aux_loss = self.router(x_flat) # [T,k], [T,k]
|
| 237 |
|
| 238 |
gather_idx = topk_idx.unsqueeze(-1).expand(-1, -1, D) # [T,k,D]
|
| 239 |
Y_topk = torch.gather(Y, dim=1, index=gather_idx) # [T,k,D]
|
| 240 |
y_flat = (Y_topk * topk_prob.unsqueeze(-1)).sum(dim=1) # [T,D]
|
| 241 |
|
| 242 |
-
return y_flat.view(B, S, D), aux_loss
|
| 243 |
|
| 244 |
|
| 245 |
class MoEPllamaLMConfig(PretrainedConfig):
|
|
@@ -401,6 +409,7 @@ class MoEPllamaModel(PreTrainedModel):
|
|
| 401 |
causal = create_causal_mask(S, device=device, dtype=mask_dtype)
|
| 402 |
|
| 403 |
aux_total = None
|
|
|
|
| 404 |
|
| 405 |
freqs = self.freqs
|
| 406 |
|
|
@@ -408,26 +417,34 @@ class MoEPllamaModel(PreTrainedModel):
|
|
| 408 |
x = self.layers[0](x, causal_mask=causal, key_padding_mask=key_padding, freqs=freqs)
|
| 409 |
|
| 410 |
# MoE shrink
|
| 411 |
-
x, aux = self.layers[1](x)
|
| 412 |
|
| 413 |
if aux is not None:
|
| 414 |
aux_total = aux
|
| 415 |
|
|
|
|
|
|
|
|
|
|
| 416 |
freqs_p = self.freqs_p
|
| 417 |
|
| 418 |
|
| 419 |
idx = 2
|
| 420 |
for i in range(self.config.n_layers - 2):
|
| 421 |
-
x, aux = self.layers[idx](x, causal_mask=causal, key_padding_mask=key_padding, freqs=freqs_p)
|
| 422 |
if aux is not None:
|
| 423 |
aux_total += aux
|
|
|
|
|
|
|
| 424 |
idx += 1
|
| 425 |
|
| 426 |
# MoE grow
|
| 427 |
-
x, aux = self.layers[idx](x)
|
| 428 |
|
| 429 |
if aux is not None:
|
| 430 |
aux_total += aux
|
|
|
|
|
|
|
|
|
|
| 431 |
|
| 432 |
idx += 1
|
| 433 |
|
|
@@ -445,6 +462,7 @@ class MoEPllamaModel(PreTrainedModel):
|
|
| 445 |
hidden_states=None,
|
| 446 |
attentions=None,
|
| 447 |
aux_loss=aux_total,
|
|
|
|
| 448 |
)
|
| 449 |
else:
|
| 450 |
return BaseModelOutput(
|
|
@@ -525,12 +543,22 @@ class MoEPllamaForCausalLM(PreTrainedModel):
|
|
| 525 |
if not return_dict:
|
| 526 |
return (loss, logits) if loss is not None else (logits,)
|
| 527 |
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
|
| 535 |
AutoConfig.register("moep_llama_lm", MoEPllamaLMConfig)
|
| 536 |
AutoModelForCausalLM.register(MoEPllamaLMConfig, MoEPllamaForCausalLM)
|
|
|
|
| 19 |
@dataclass
|
| 20 |
class BaseModelOutputWithAux(BaseModelOutput):
|
| 21 |
aux_loss: Optional[torch.Tensor] = None
|
| 22 |
+
one_hot: Optional[torch.Tensor] = None
|
| 23 |
+
|
| 24 |
+
@dataclass
|
| 25 |
+
class CausalLMOutputWithAux(CausalLMOutput):
|
| 26 |
+
aux_loss: Optional[torch.Tensor] = None
|
| 27 |
+
one_hot: Optional[torch.Tensor] = None
|
| 28 |
|
| 29 |
|
| 30 |
def create_causal_mask(seq_len: int, device=None, dtype=torch.float32):
|
|
|
|
| 189 |
f_mean = one_hot.float().mean(dim=0) / float(self.k)
|
| 190 |
aux_loss = (E * (p_mean * f_mean).sum())
|
| 191 |
|
| 192 |
+
one_hot = one_hot.view(B, S, E)
|
| 193 |
+
|
| 194 |
+
return topk_idx, topk_prob, aux_loss, one_hot
|
| 195 |
|
| 196 |
|
| 197 |
class MoE_Block(nn.Module):
|
|
|
|
| 209 |
T = B * S
|
| 210 |
x_flat = x.view(T, D)
|
| 211 |
|
| 212 |
+
topk_idx, topk_prob, aux_loss, one_hot = self.router(x_flat) # [T,k], [T,k]
|
| 213 |
y_flat = x_flat.new_zeros((T, self.dim_out))
|
| 214 |
|
| 215 |
for slot in range(self.k):
|
|
|
|
| 222 |
y_e = self.experts[e](x_flat.index_select(0, idx)) # [Ne, dim_out]
|
| 223 |
y_flat.index_add_(0, idx, y_e * e_prob.index_select(0, idx))
|
| 224 |
|
| 225 |
+
return y_flat.view(B, S, self.dim_out), aux_loss, one_hot
|
| 226 |
|
| 227 |
|
| 228 |
class ParallelLayer(nn.Module):
|
|
|
|
| 241 |
Y = torch.stack([y.view(T, D) for y in outs], dim=1) # [T, n_blocks, D]
|
| 242 |
|
| 243 |
x_flat = x.view(T, D)
|
| 244 |
+
topk_idx, topk_prob, aux_loss, one_hot = self.router(x_flat) # [T,k], [T,k]
|
| 245 |
|
| 246 |
gather_idx = topk_idx.unsqueeze(-1).expand(-1, -1, D) # [T,k,D]
|
| 247 |
Y_topk = torch.gather(Y, dim=1, index=gather_idx) # [T,k,D]
|
| 248 |
y_flat = (Y_topk * topk_prob.unsqueeze(-1)).sum(dim=1) # [T,D]
|
| 249 |
|
| 250 |
+
return y_flat.view(B, S, D), aux_loss, one_hot
|
| 251 |
|
| 252 |
|
| 253 |
class MoEPllamaLMConfig(PretrainedConfig):
|
|
|
|
| 409 |
causal = create_causal_mask(S, device=device, dtype=mask_dtype)
|
| 410 |
|
| 411 |
aux_total = None
|
| 412 |
+
all_one_hots = []
|
| 413 |
|
| 414 |
freqs = self.freqs
|
| 415 |
|
|
|
|
| 417 |
x = self.layers[0](x, causal_mask=causal, key_padding_mask=key_padding, freqs=freqs)
|
| 418 |
|
| 419 |
# MoE shrink
|
| 420 |
+
x, aux, one_hot = self.layers[1](x)
|
| 421 |
|
| 422 |
if aux is not None:
|
| 423 |
aux_total = aux
|
| 424 |
|
| 425 |
+
if one_hot is not None:
|
| 426 |
+
all_one_hots.append(one_hot)
|
| 427 |
+
|
| 428 |
freqs_p = self.freqs_p
|
| 429 |
|
| 430 |
|
| 431 |
idx = 2
|
| 432 |
for i in range(self.config.n_layers - 2):
|
| 433 |
+
x, aux, one_hot = self.layers[idx](x, causal_mask=causal, key_padding_mask=key_padding, freqs=freqs_p)
|
| 434 |
if aux is not None:
|
| 435 |
aux_total += aux
|
| 436 |
+
if one_hot is not None:
|
| 437 |
+
all_one_hots.append(one_hot)
|
| 438 |
idx += 1
|
| 439 |
|
| 440 |
# MoE grow
|
| 441 |
+
x, aux, one_hot = self.layers[idx](x)
|
| 442 |
|
| 443 |
if aux is not None:
|
| 444 |
aux_total += aux
|
| 445 |
+
|
| 446 |
+
if one_hot is not None:
|
| 447 |
+
all_one_hots.append(one_hot)
|
| 448 |
|
| 449 |
idx += 1
|
| 450 |
|
|
|
|
| 462 |
hidden_states=None,
|
| 463 |
attentions=None,
|
| 464 |
aux_loss=aux_total,
|
| 465 |
+
one_hot=all_one_hots,
|
| 466 |
)
|
| 467 |
else:
|
| 468 |
return BaseModelOutput(
|
|
|
|
| 543 |
if not return_dict:
|
| 544 |
return (loss, logits) if loss is not None else (logits,)
|
| 545 |
|
| 546 |
+
if return_aux:
|
| 547 |
+
return CausalLMOutputWithAux(
|
| 548 |
+
loss=loss,
|
| 549 |
+
logits=logits,
|
| 550 |
+
hidden_states=base_out.hidden_states,
|
| 551 |
+
attentions=base_out.attentions,
|
| 552 |
+
aux_loss=getattr(base_out, "aux_loss", None),
|
| 553 |
+
one_hot=getattr(base_out, "one_hot", None),
|
| 554 |
+
)
|
| 555 |
+
else:
|
| 556 |
+
return CausalLMOutput(
|
| 557 |
+
loss=loss,
|
| 558 |
+
logits=logits,
|
| 559 |
+
hidden_states=base_out.hidden_states,
|
| 560 |
+
attentions=base_out.attentions,
|
| 561 |
+
)
|
| 562 |
|
| 563 |
AutoConfig.register("moep_llama_lm", MoEPllamaLMConfig)
|
| 564 |
AutoModelForCausalLM.register(MoEPllamaLMConfig, MoEPllamaForCausalLM)
|