[TRITON] moe routing support expert_map for expert parallelism#3348
Open
amd-ruitang3 wants to merge 3 commits into
Open
[TRITON] moe routing support expert_map for expert parallelism#3348amd-ruitang3 wants to merge 3 commits into
amd-ruitang3 wants to merge 3 commits into
Conversation
Contributor
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR extends the Triton fused “topk → routing data” path to support expert-parallel setups via an optional expert_map that remaps global expert IDs to local expert IDs, and adds a corresponding correctness test.
Changes:
- Add
expert_map: Optional[torch.Tensor]tofused_routing_from_topkand plumb it into the Triton histogram/place kernels. - Implement expert-map remapping + invalid-expert masking (redirect to expert 0, weight=0) inside the fused Triton kernels.
- Extend the torch reference path and add a new test covering
expert_mapbehavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| op_tests/triton_tests/fusions/test_fused_routing_from_topk.py | Adds expert-map-aware reference logic and a new test case for remapped routing. |
| aiter/ops/triton/fusions/fused_routing_from_topk.py | Extends the Python wrapper API to accept expert_map and pass it to Triton kernels. |
| aiter/ops/triton/_triton_kernels/fusions/fused_routing_from_topk.py | Adds expert-map remapping logic to the Triton histogram and placement kernels. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+45
to
+51
| topk_ids: ``[n_tokens, n_expts_act]`` selected expert ids; values | ||
| in ``[0, n_expts_tot)``. | ||
| n_expts_tot: Total number of routed experts (= ``E``). | ||
| expert_map: Optional global→local expert map. When provided, | ||
| ``topk_ids`` are treated as global ids and remapped inside fused | ||
| kernels. Entries mapped to ``< 0`` are masked to zero weight and | ||
| redirected to local expert ``0`` for routing safety. |
| expert_map_numel = 0 | ||
| expert_map_flat = topk_ids_flat | ||
| has_expert_map = expert_map is not None | ||
| if has_expert_map: |
| ) | ||
| # Match reference semantics: invalid experts are redirected to bucket 0 | ||
| # and later zeroed in gate_scal. | ||
| expt = tl.where(local_expt >= 0, local_expt, 0) |
| local_expt = tl.load(expert_map_ptr + safe_global_expt, mask=map_mask, other=-1).to( | ||
| tl.int32 | ||
| ) | ||
| invalid = local_expt < 0 |
| """ | ||
| if expert_map is not None: | ||
| local_ids = expert_map[topk_ids.long()] | ||
| invalid = local_ids < 0 |
Comment on lines
+298
to
+309
| test_hist, test_topk_indx, test_gate_indx, test_gate_scal = fused_routing_from_topk( | ||
| topk_weights, topk_ids, n_expts_tot, expert_map=expert_map | ||
| ) | ||
| _check_routing_invariants( | ||
| test_hist, | ||
| test_topk_indx, | ||
| test_gate_indx, | ||
| test_gate_scal, | ||
| topk_ids, | ||
| n_expts_tot, | ||
| bucket_unsorted_layout=False, | ||
| ) |
k50112113
reviewed
May 26, 2026
k50112113
reviewed
May 26, 2026
k50112113
reviewed
May 26, 2026
k50112113
reviewed
May 26, 2026
k50112113
requested changes
May 26, 2026
k50112113
approved these changes
May 26, 2026
Contributor
k50112113
left a comment
There was a problem hiding this comment.
LGTM, Thanks for the addition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Technical Details
Test Plan
Test Result
Submission Checklist