undo sparse router removal
Browse files- modeling_myolmoe.py +38 -26
modeling_myolmoe.py
CHANGED
|
@@ -262,44 +262,56 @@ class OlmoeRotaryEmbedding(nn.Module):
|
|
| 262 |
|
| 263 |
return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype)
|
| 264 |
|
| 265 |
-
class
|
| 266 |
-
|
|
|
|
|
|
|
| 267 |
super().__init__()
|
| 268 |
self.num_experts = config.num_experts
|
| 269 |
self.top_k = config.num_experts_per_tok
|
|
|
|
|
|
|
| 270 |
self.norm_topk_prob = config.norm_topk_prob
|
| 271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
self.experts = nn.ModuleList([OlmoeMLP(config) for _ in range(self.num_experts)])
|
| 273 |
-
|
| 274 |
-
def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
|
| 275 |
-
batch_size,
|
| 276 |
-
hidden_states = hidden_states.view(-1,
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
routing_weights, selected_experts = torch.topk(routing_weights, self.top_k, dim=-1)
|
| 282 |
if self.norm_topk_prob:
|
| 283 |
-
routing_weights
|
| 284 |
-
|
| 285 |
-
routing_weights = routing_weights.to(hidden_states.dtype)
|
| 286 |
-
|
| 287 |
final_hidden_states = torch.zeros(
|
| 288 |
-
(batch_size *
|
|
|
|
|
|
|
| 289 |
)
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
|
|
|
| 294 |
for expert_idx in range(self.num_experts):
|
| 295 |
expert_layer = self.experts[expert_idx]
|
| 296 |
-
idx, top_x = torch.where(expert_mask[expert_idx])
|
| 297 |
-
|
| 298 |
-
|
|
|
|
|
|
|
|
|
|
| 299 |
current_hidden_states = expert_layer(current_state) * routing_weights[top_x, idx, None]
|
| 300 |
-
|
| 301 |
final_hidden_states.index_add_(0, top_x, current_hidden_states.to(hidden_states.dtype))
|
| 302 |
-
|
|
|
|
| 303 |
return final_hidden_states, router_logits
|
| 304 |
|
| 305 |
|
|
|
|
| 262 |
|
| 263 |
return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype)
|
| 264 |
|
| 265 |
+
class MyOLMoESparseMoeBlock(nn.Module):
|
| 266 |
+
"""Modified sparse MoE block with custom routing."""
|
| 267 |
+
|
| 268 |
+
def __init__(self, config: OlmoeConfig):
|
| 269 |
super().__init__()
|
| 270 |
self.num_experts = config.num_experts
|
| 271 |
self.top_k = config.num_experts_per_tok
|
| 272 |
+
self.hidden_size = config.hidden_size
|
| 273 |
+
self.intermediate_size = config.intermediate_size
|
| 274 |
self.norm_topk_prob = config.norm_topk_prob
|
| 275 |
+
|
| 276 |
+
# Custom routing mechanism
|
| 277 |
+
self.router = MyOLMoERouting(config)
|
| 278 |
+
|
| 279 |
+
# Expert networks
|
| 280 |
self.experts = nn.ModuleList([OlmoeMLP(config) for _ in range(self.num_experts)])
|
| 281 |
+
|
| 282 |
+
def forward(self, hidden_states: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 283 |
+
batch_size, seq_len, _ = hidden_states.shape
|
| 284 |
+
hidden_states = hidden_states.view(-1, self.hidden_size)
|
| 285 |
+
|
| 286 |
+
# Get routing weights and selected experts
|
| 287 |
+
routing_weights, selected_experts, router_logits = self.router(hidden_states)
|
| 288 |
+
|
|
|
|
| 289 |
if self.norm_topk_prob:
|
| 290 |
+
routing_weights = routing_weights / routing_weights.sum(dim=-1, keepdim=True)
|
| 291 |
+
|
|
|
|
|
|
|
| 292 |
final_hidden_states = torch.zeros(
|
| 293 |
+
(batch_size * seq_len, self.hidden_size),
|
| 294 |
+
dtype=hidden_states.dtype,
|
| 295 |
+
device=hidden_states.device
|
| 296 |
)
|
| 297 |
+
|
| 298 |
+
# One-hot expert mask
|
| 299 |
+
expert_mask = torch.nn.functional.one_hot(selected_experts, num_classes=self.num_experts)
|
| 300 |
+
|
| 301 |
+
# Dispatch to experts
|
| 302 |
for expert_idx in range(self.num_experts):
|
| 303 |
expert_layer = self.experts[expert_idx]
|
| 304 |
+
idx, top_x = torch.where(expert_mask[:, :, expert_idx])
|
| 305 |
+
|
| 306 |
+
if idx.shape[0] == 0:
|
| 307 |
+
continue
|
| 308 |
+
|
| 309 |
+
current_state = hidden_states[None, top_x].reshape(-1, self.hidden_size)
|
| 310 |
current_hidden_states = expert_layer(current_state) * routing_weights[top_x, idx, None]
|
| 311 |
+
|
| 312 |
final_hidden_states.index_add_(0, top_x, current_hidden_states.to(hidden_states.dtype))
|
| 313 |
+
|
| 314 |
+
final_hidden_states = final_hidden_states.reshape(batch_size, seq_len, self.hidden_size)
|
| 315 |
return final_hidden_states, router_logits
|
| 316 |
|
| 317 |
|