-
Notifications
You must be signed in to change notification settings - Fork 55
Support router replay #353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -76,7 +76,7 @@ def __init__( | |||||||||||||||||||||||||||||||||||||||
| **kwargs, | ||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| def forward(self, hidden_states): | ||||||||||||||||||||||||||||||||||||||||
| def forward(self, hidden_states, forced_expert_ids: Optional[torch.Tensor] = None): | ||||||||||||||||||||||||||||||||||||||||
| # hidden_states: [num_tokens, H] | ||||||||||||||||||||||||||||||||||||||||
| # DP input: gather peer ranks' shards so gating/dispatch see the full token set. | ||||||||||||||||||||||||||||||||||||||||
| if self.dp_input and self.ep_size > 1: | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -87,8 +87,17 @@ def forward(self, hidden_states): | |||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||
| dist.all_gather_into_tensor(full, hidden_states.contiguous(), group=self.ep_group) | ||||||||||||||||||||||||||||||||||||||||
| hidden_states = full | ||||||||||||||||||||||||||||||||||||||||
| if forced_expert_ids is not None: | ||||||||||||||||||||||||||||||||||||||||
| full_forced_expert_ids = torch.empty( | ||||||||||||||||||||||||||||||||||||||||
| local_tokens * self.ep_size, | ||||||||||||||||||||||||||||||||||||||||
| *forced_expert_ids.shape[1:], | ||||||||||||||||||||||||||||||||||||||||
| dtype=forced_expert_ids.dtype, | ||||||||||||||||||||||||||||||||||||||||
| device=forced_expert_ids.device, | ||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||
| dist.all_gather_into_tensor(full_forced_expert_ids, forced_expert_ids.contiguous(), group=self.ep_group) | ||||||||||||||||||||||||||||||||||||||||
| forced_expert_ids = full_forced_expert_ids | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| top_k_indices, top_k_gates = self.gating(hidden_states) | ||||||||||||||||||||||||||||||||||||||||
| top_k_indices, top_k_gates = self.gating(hidden_states, forced_expert_ids=forced_expert_ids) | ||||||||||||||||||||||||||||||||||||||||
| # top_k_indices, top_k_gates: [num_tokens, top_k] | ||||||||||||||||||||||||||||||||||||||||
| sorted_hidden_states, tokens_per_expert, sorted_gates, token_indices = self.dispatch( | ||||||||||||||||||||||||||||||||||||||||
| hidden_states, top_k_gates, top_k_indices | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -227,7 +236,7 @@ def __init__( | |||||||||||||||||||||||||||||||||||||||
| **kwargs, | ||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| def forward(self, hidden_states): | ||||||||||||||||||||||||||||||||||||||||
| def forward(self, hidden_states, forced_expert_ids: Optional[torch.Tensor] = None): | ||||||||||||||||||||||||||||||||||||||||
| # DP input: gather peer ranks' shards so gating/dispatch see the full token set. | ||||||||||||||||||||||||||||||||||||||||
| if self.dp_input and self.ep_size > 1: | ||||||||||||||||||||||||||||||||||||||||
| local_tokens = hidden_states.shape[0] | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -237,8 +246,17 @@ def forward(self, hidden_states): | |||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||
| dist.all_gather_into_tensor(full, hidden_states.contiguous(), group=self.ep_group) | ||||||||||||||||||||||||||||||||||||||||
| hidden_states = full | ||||||||||||||||||||||||||||||||||||||||
| if forced_expert_ids is not None: | ||||||||||||||||||||||||||||||||||||||||
| full_forced_expert_ids = torch.empty( | ||||||||||||||||||||||||||||||||||||||||
| local_tokens * self.ep_size, | ||||||||||||||||||||||||||||||||||||||||
| *forced_expert_ids.shape[1:], | ||||||||||||||||||||||||||||||||||||||||
| dtype=forced_expert_ids.dtype, | ||||||||||||||||||||||||||||||||||||||||
| device=forced_expert_ids.device, | ||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||
| dist.all_gather_into_tensor(full_forced_expert_ids, forced_expert_ids.contiguous(), group=self.ep_group) | ||||||||||||||||||||||||||||||||||||||||
| forced_expert_ids = full_forced_expert_ids | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+249
to
+257
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If To prevent this, ensure
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| top_k_indices, top_k_gates = self.gating(hidden_states) | ||||||||||||||||||||||||||||||||||||||||
| top_k_indices, top_k_gates = self.gating(hidden_states, forced_expert_ids=forced_expert_ids) | ||||||||||||||||||||||||||||||||||||||||
| sorted_hidden_states, tokens_per_expert, sorted_gates, token_indices = self.dispatch( | ||||||||||||||||||||||||||||||||||||||||
| hidden_states, | ||||||||||||||||||||||||||||||||||||||||
| top_k_gates, | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -299,12 +317,14 @@ def __init__( | |||||||||||||||||||||||||||||||||||||||
| def forward( | ||||||||||||||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||||||||||||||
| hidden_states: torch.Tensor, | ||||||||||||||||||||||||||||||||||||||||
| forced_expert_ids: Optional[torch.Tensor] = None, | ||||||||||||||||||||||||||||||||||||||||
| ) -> Tuple[torch.Tensor, torch.Tensor]: | ||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||
| Forward pass for MoE Gating operator. | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| Input: | ||||||||||||||||||||||||||||||||||||||||
| - hidden_states (torch.Tensor): Input tensor of shape [num_tokens, hidden_size]. | ||||||||||||||||||||||||||||||||||||||||
| - forced_expert_ids (Optional[torch.Tensor]): Expert ids of shape [num_tokens, top_k]. | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| Output: | ||||||||||||||||||||||||||||||||||||||||
| - top_k_indices (torch.Tensor): Output tensor of shape [num_tokens, top_k]. | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -314,6 +334,16 @@ def forward( | |||||||||||||||||||||||||||||||||||||||
| gate_logits = torch.matmul(hidden_states.float(), self.gate_weight) | ||||||||||||||||||||||||||||||||||||||||
| gate_logits = torch.softmax(gate_logits, dim=-1) | ||||||||||||||||||||||||||||||||||||||||
| top_k_logits, top_k_indices = torch.topk(gate_logits, self.top_k, dim=-1) | ||||||||||||||||||||||||||||||||||||||||
| if forced_expert_ids is not None: | ||||||||||||||||||||||||||||||||||||||||
| expected_shape = (hidden_states.shape[0], self.top_k) | ||||||||||||||||||||||||||||||||||||||||
| if tuple(forced_expert_ids.shape) != expected_shape: | ||||||||||||||||||||||||||||||||||||||||
| raise ValueError( | ||||||||||||||||||||||||||||||||||||||||
| f"forced_expert_ids must have shape {expected_shape}, got {tuple(forced_expert_ids.shape)}." | ||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||
| forced_expert_ids = forced_expert_ids.to(device=hidden_states.device, dtype=torch.int64) | ||||||||||||||||||||||||||||||||||||||||
| forced_expert_mask = forced_expert_ids >= 0 | ||||||||||||||||||||||||||||||||||||||||
| top_k_indices = torch.where(forced_expert_mask, forced_expert_ids, top_k_indices) | ||||||||||||||||||||||||||||||||||||||||
| top_k_logits = torch.gather(gate_logits, dim=-1, index=top_k_indices) | ||||||||||||||||||||||||||||||||||||||||
| top_k_gates = top_k_logits / torch.sum(top_k_logits, dim=-1, keepdim=True) | ||||||||||||||||||||||||||||||||||||||||
| return top_k_indices.to(torch.int32), top_k_gates | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
forced_expert_idsis on CPU (or a different device thanhidden_states), creatingfull_forced_expert_idsonforced_expert_ids.deviceand callingdist.all_gather_into_tensorwill result in a runtime error (especially when using the NCCL backend, which requires CUDA tensors).To prevent this, ensure
forced_expert_idsis moved tohidden_states.devicebefore allocating the empty tensor and performing the collective operation.