[Perf] Restore torch.compile fusion for topk postprocessing#21771
Merged
Fridge003 merged 2 commits intosgl-project:mainfrom Apr 7, 2026
Merged
Conversation
PR sgl-project#16945 refactored topk postprocessing into `_post_process_topk_ids` but inlined the `topk_ids_logical_to_physical` and `_mask_topk_ids_padded_region` calls instead of delegating to the existing `@torch.compile`-decorated `_biased_grouped_topk_postprocess`. This caused those two operations to run as separate eager kernels instead of being fused by torch.compile, a regression for CUDA paths using expert-parallel / EPLB. Fix: call `_biased_grouped_topk_postprocess` (which already carries `@torch.compile(dynamic=True)`) from within `_post_process_topk_ids`, restoring the compiled kernel fusion. Ref: sgl-project#16945 (comment)
Collaborator
Author
|
/tag-and-rerun-ci |
Contributor
There was a problem hiding this comment.
Code Review
This pull request refactors the post-processing logic for top-k IDs in the MoE layer by replacing sequential calls to topk_ids_logical_to_physical and _mask_topk_ids_padded_region with a consolidated call to _biased_grouped_topk_postprocess when running on CUDA. I have no feedback to provide.
Fridge003
approved these changes
Apr 6, 2026
carlosfundora
pushed a commit
to carlosfundora/sglang-1-bit-turbo
that referenced
this pull request
Apr 8, 2026
…stprocessing (sgl-project#21771) Upstream SHA: 490fa9f Cherry-picked from sgl-project/sglang Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
yhyang201
pushed a commit
to yhyang201/sglang
that referenced
this pull request
Apr 22, 2026
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
PR #16945 reorganized topk logic into
_post_process_topk_idsbut inlinedtopk_ids_logical_to_physicaland_mask_topk_ids_padded_regioninstead of calling the existing@torch.compile-decorated_biased_grouped_topk_postprocess. This was flagged during review by @fzyzcjy (comment):The regression causes these two operations to run as separate eager kernels on CUDA instead of being fused via
torch.compile, impacting expert-parallel / EPLB paths.Current ToT:

This PR restores fusion present before PR #16945:

Modifications
Replace the two inlined calls in
_post_process_topk_idswith a call to the existing compiled_biased_grouped_topk_postprocess, restoring kernel fusion. The function was already defined with@torch.compile(dynamic=True, backend=get_compiler_backend())but had become dead code after #16945.Checklist
pre-commit run --all-files