-
-
Notifications
You must be signed in to change notification settings - Fork 20.1k
[ROCM] Optimize the fused_topk_bias to use aiter instead of fallback torch ops. #36253
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 1 commit
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 |
|---|---|---|
|
|
@@ -108,6 +108,42 @@ def fused_topk_bias( | |
| return topk_weights, topk_ids | ||
| else: | ||
| raise ValueError(f"Unsupported scoring function: {scoring_func}") | ||
| elif rocm_aiter_ops.is_fused_moe_enabled() and scoring_func == "sigmoid": | ||
| # aiter only have sigmoid bias grouped topk now. | ||
| M = hidden_states.size(0) | ||
| num_experts = gating_output.shape[-1] | ||
| _AITER_MAX_EXPERTS_PER_GROUP = 32 | ||
| num_expert_group = max( | ||
| 1, -(-num_experts // _AITER_MAX_EXPERTS_PER_GROUP) | ||
| ) # ceil division | ||
| while num_experts % num_expert_group != 0: | ||
| num_expert_group += 1 | ||
| topk_weights = torch.empty( | ||
| M, topk, dtype=torch.float32, device=hidden_states.device | ||
| ) | ||
| assert num_experts % num_expert_group == 0, ( | ||
| f"{num_experts=} not divisible by {num_expert_group=}" | ||
| ) | ||
| assert num_experts // num_expert_group <= _AITER_MAX_EXPERTS_PER_GROUP, ( | ||
| f"group size {num_experts // num_expert_group} \ | ||
| exceeds limit {_AITER_MAX_EXPERTS_PER_GROUP}" | ||
| ) | ||
| topk_ids = torch.empty( | ||
| M, | ||
| topk, | ||
| dtype=torch.int32 if indices_type is None else indices_type, | ||
| device=hidden_states.device, | ||
| ) | ||
| rocm_aiter_ops.biased_grouped_topk( | ||
| gating_output, | ||
| e_score_correction_bias.to(gating_output.dtype), | ||
| topk_weights, | ||
| topk_ids, | ||
| num_expert_group=num_expert_group, | ||
| topk_group=num_expert_group, | ||
| need_renorm=renormalize, | ||
| ) | ||
|
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. The code added in the PR introduces a fast path for ROCm using the
Contributor
Author
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. Fixed in new commit. Added checks and fallback to original path when checks don't meet. |
||
| return topk_weights, topk_ids | ||
|
|
||
| n_routed_experts = gating_output.shape[-1] | ||
| if scoring_func == "softmax": | ||
|
|
||
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.
The calculation of
num_expert_groupis performed on every forward pass. Thewhileloop can be inefficient ifnum_expertsis a large number with no small divisors (e.g., a prime number). For instance, ifnum_expertsis a prime like 59, this loop will iterate many times on every call. This could introduce a performance overhead in what is supposed to be a fast path.Since
num_expertsis constant for a given model, this value should be computed once and cached. The best approach would be to compute it during model initialization and pass it to this function. As a self-contained alternative, you can cache the result within this function usingfunctools.lru_cacheas shown in the suggestion.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.
Fixed in new commit, added the LRU cache to the function.