-
Notifications
You must be signed in to change notification settings - Fork 734
Fix moe topk select bug in cudagraph #7069
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
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 | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -245,16 +245,17 @@ def moe_topk_select( | |||||||||||||||||||||||||
| probs_for_choice.reshape([seq_length, n_group, -1]).topk(2, axis=-1)[0].sum(axis=-1) | ||||||||||||||||||||||||||
| ) # [seq_len, n_group] | ||||||||||||||||||||||||||
| group_idx = paddle.topk(group_scores, k=topk_group, axis=-1, sorted=True)[1] # [seq_len, topk_group] | ||||||||||||||||||||||||||
| group_mask = paddle.zeros_like(group_scores).put_along_axis( | ||||||||||||||||||||||||||
| group_idx, paddle.to_tensor(1.0, dtype=group_scores.dtype), axis=-1 | ||||||||||||||||||||||||||
| group_mask = paddle.sum( | ||||||||||||||||||||||||||
| paddle.nn.functional.one_hot(group_idx, num_classes=n_group).cast(group_scores.dtype), | ||||||||||||||||||||||||||
| axis=1, # Sum over topk_group dimension -> [seq_len, n_group] | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| score_mask = ( | ||||||||||||||||||||||||||
| group_mask.unsqueeze(-1).expand([seq_length, n_group, n_experts // n_group]).reshape([seq_length, -1]) | ||||||||||||||||||||||||||
| ) # [seq_len, n_experts] | ||||||||||||||||||||||||||
| probs_for_choice = probs_for_choice.masked_fill(~score_mask.astype(paddle.bool), float("-inf")) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| _, topk_ids = paddle.topk(probs_for_choice, top_k, axis=-1) | ||||||||||||||||||||||||||
| topk_weights = paddle.take_along_axis(gate_probs, topk_ids, axis=-1) | ||||||||||||||||||||||||||
| topk_weights = paddle.index_sample(gate_probs, topk_ids) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| topk_weights = paddle.index_sample(gate_probs, topk_ids) | |
| # Use paddle.index_sample with its 2-D [N, M] / [N, K] contract by flattening | |
| # all leading dimensions into a single batch dimension, and gather along the | |
| # last axis. This is equivalent to take_along_axis(..., axis=-1) but robust | |
| # to higher-rank gate_probs/topk_ids. | |
| last_expert_dim = gate_probs.shape[-1] | |
| flat_batch = paddle.numel(gate_probs) // last_expert_dim | |
| gate_probs_2d = gate_probs.reshape([flat_batch, last_expert_dim]) | |
| topk_last_dim = topk_ids.shape[-1] | |
| topk_ids_2d = topk_ids.reshape([flat_batch, topk_last_dim]) | |
| topk_weights_2d = paddle.index_sample(gate_probs_2d, topk_ids_2d) | |
| topk_weights = topk_weights_2d.reshape(topk_ids.shape) |
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.
Building
group_maskviaone_hot(group_idx, num_classes=n_group)materializes a dense tensor of shape[seq_len, topk_group, n_group], which can significantly increase memory traffic (and potentially hurt latency) for largeseq_len/n_group. Consider using a scatter/put-along-axis style update that writes ones directly into a[seq_len, n_group]buffer (e.g., initialize zeros then scatter indices with a ones tensor shaped likegroup_idx), which avoids the large intermediate.