refactor: reorganize moe ops and kernels#243
Open
zhenhuang12 wants to merge 3 commits into
Open
Conversation
- Merge fused_router, permute, unpermute... into a single moe_utils.py file - Rename token_permute/token_unpermute to moe_permute/moe_unpermute - Update all references in token_dispatcher.py and test files
- Merge fused_router, multihot_to_indices, tokens_per_expert_to_mask into moe_utils.py - Rename token_permute/token_unpermute to moe_permute/moe_unpermute
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors MoE-related ops by consolidating previously separate router / indices conversion / permutation / tokens-per-expert utilities into shared moe_utils modules (PyTorch + Triton), and updates call sites/tests to the new API names and import paths.
Changes:
- Move Triton MoE kernels (router, indices↔multihot, tokens-per-expert mask) into
primus_turbo/triton/moe/moe_utils.py. - Consolidate PyTorch MoE ops into
primus_turbo/pytorch/ops/moe/moe_utils.py, renamingtoken_permute/unpermute→moe_permute/unpermute. - Update tests and module call sites to new names/imports and simplify MoE package exports.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| tests/pytorch/ops/test_tokens_per_expert_to_mask.py | Updates import to new consolidated MoE utils location. |
| tests/pytorch/ops/test_permutation.py | Updates permutation API calls to moe_permute / moe_unpermute. |
| primus_turbo/triton/moe/tokens_per_expert_to_mask_kernel.py | Deleted; kernel moved into Triton moe_utils. |
| primus_turbo/triton/moe/multihot_to_indices.py | Deleted; kernels moved into Triton moe_utils. |
| primus_turbo/triton/moe/moe_utils.py | Becomes the consolidated Triton MoE kernel module. |
| primus_turbo/pytorch/ops/moe/tokens_per_expert_to_mask.py | Deleted; op moved into PyTorch moe_utils. |
| primus_turbo/pytorch/ops/moe/moe_utils.py | New single entry point for router, indices conversion, permute/unpermute, and tokens-per-expert mask. |
| primus_turbo/pytorch/ops/moe/indices_converter.py | Deleted; functionality moved into PyTorch moe_utils. |
| primus_turbo/pytorch/ops/moe/fused_moe_router.py | Deleted; functionality moved into PyTorch moe_utils. |
| primus_turbo/pytorch/ops/moe/init.py | Re-exports MoE API via moe_dispatch_combine + moe_utils. |
| primus_turbo/pytorch/modules/moe/token_dispatcher.py | Updates call sites to renamed moe_permute/unpermute API. |
| primus_turbo/pytorch/kernels/moe/tokens_per_expert_to_mask_impl.py | Updates kernel import to consolidated Triton moe_utils. |
| primus_turbo/pytorch/kernels/moe/fused_moe_router_impl.py | Updates kernel import to consolidated Triton moe_utils. |
Comments suppressed due to low confidence (7)
primus_turbo/pytorch/ops/moe/moe_utils.py:100
torch.zeros(..., device="cuda")hard-codes GPU 0; this will break on multi-GPU iflogitsis on a non-default CUDA device. Allocate onlogits.device(and similarly avoid hard-coded "cuda" elsewhere in this module) so the op is device-correct.
primus_turbo/pytorch/ops/moe/moe_utils.py:190- These outputs are always allocated on
device="cuda", which forces CUDA:0 even ifindicesis on another CUDA device. Allocatemultihot_indices,probs_in_multihot, andposition_maponindices.deviceto keep the op multi-GPU safe.
primus_turbo/pytorch/ops/moe/moe_utils.py:236 grad_probs_indicesis allocated ondevice="cuda", which can put the gradient tensor on the wrong GPU when running on CUDA:1+. Allocate it ongrad_probs_in_multihot.device(orposition_map.device) instead.
primus_turbo/pytorch/ops/moe/moe_utils.py:255IndicesToMultihot.forwardtakes 3 inputs, butbackwardreturns 4 gradients. PyTorch will error with “returned an invalid number of gradients”. Return exactly 3 items (one per forward input).
primus_turbo/pytorch/ops/moe/moe_utils.py:560- The return type annotation for
moe_permutedoesn’t match actual returns: this function returns 4 values (both in fused and non-fused paths), but the annotation declares only 3. Update the signature typing to reflect the real return tuple shape to avoid misleading callers and type-checkers.
primus_turbo/pytorch/ops/moe/moe_utils.py:592 - Use
is not None/is NoneforNonechecks instead of!= Noneto avoid surprising behavior with overloaded__eq__and to match the rest of this module’s style.
primus_turbo/pytorch/ops/moe/moe_utils.py:100 - In this autograd Function,
g_probscan beNonewhen backpropagating only throughoutput_scores(e.g., callingoutput_scores.backward(...)without touchingoutput_probs). Multiplyingg_probsbefore checking it will raise a runtime error; guard the multiplication (or treat missing grads as zeros) before using it.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
No description provided.