Charlie81 commited on
Commit
6e3bd26
·
1 Parent(s): dcf107e

consistent dense moe routers

Browse files
Files changed (1) hide show
  1. modeling_myolmoe.py +11 -4
modeling_myolmoe.py CHANGED
@@ -231,11 +231,18 @@ class MyOLMoERouting(nn.Module):
231
  self.gumbel_noise = getattr(config, "gumbel_noise", 0.1)
232
 
233
  def forward(self, hidden_states: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
234
- """Compute routing weights and expert selection."""
235
- batch_size, seq_len, _ = hidden_states.shape
236
- hidden_states = hidden_states.view(-1, self.hidden_size)
237
-
238
  router_logits = self.gate(hidden_states)
 
 
 
 
 
 
 
 
 
239
 
240
  if self.routing_type == "dense":
241
  # Dense routing - use all experts equally
 
231
  self.gumbel_noise = getattr(config, "gumbel_noise", 0.1)
232
 
233
  def forward(self, hidden_states: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
234
+ batch_size, sequence_length, hidden_dim = hidden_states.shape
235
+ hidden_states = hidden_states.view(-1, hidden_dim)
 
 
236
  router_logits = self.gate(hidden_states)
237
+
238
+ # Always use softmax, even for "dense" routing
239
+ routing_weights = F.softmax(router_logits, dim=1, dtype=torch.float)
240
+ routing_weights, selected_experts = torch.topk(routing_weights, self.top_k, dim=-1)
241
+
242
+ if self.norm_topk_prob:
243
+ routing_weights /= routing_weights.sum(dim=-1, keepdim=True)
244
+
245
+ routing_weights = routing_weights.to(hidden_states.dtype)
246
 
247
  if self.routing_type == "dense":
248
  # Dense routing - use all experts equally