Skip to content

Fix fallback to default tactic (flashinfer autotuner) with trtllm_fp4_block_scale_moe#19189

Merged
Fridge003 merged 3 commits intosgl-project:mainfrom
wenscarl:fix_trtllm_moe_autotune
Mar 6, 2026
Merged

Fix fallback to default tactic (flashinfer autotuner) with trtllm_fp4_block_scale_moe#19189
Fridge003 merged 3 commits intosgl-project:mainfrom
wenscarl:fix_trtllm_moe_autotune

Conversation

@wenscarl
Copy link
Collaborator

@wenscarl wenscarl commented Feb 23, 2026

The flashinfer autotuner expects the first dimension of the MoE tensors to be num_tokens.

Following the changes in vllm-project/vllm#35088.

Credits to @danisereb. Depends on flashinfer fix: flashinfer-ai/flashinfer#2617
cc. @Fridge003 @nvpohanh

Motivation

Modifications

Accuracy Tests

Benchmarking and Profiling

Checklist

Review Process

  1. Ping Merge Oncalls to start the PR flow. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • /tag-run-ci-label, /rerun-failed-ci, /tag-and-rerun-ci
  4. After green CI and required approvals, ask Merge Oncalls to merge.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @wenscarl, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical issue affecting the flashinfer autotuner when used with trtllm_fp4_block_scale_moe. The core problem stemmed from an incorrect reshaping of the hidden_states_scale tensor, leading to a dimension mismatch that caused the autotuner to fail and potentially fall back to less optimized execution paths. By adjusting the reshape calls across relevant files, this change ensures that the tensor dimensions align with the autotuner's expectations, thereby restoring proper functionality and performance for MoE operations.

Highlights

  • Tensor Reshaping Correction: Corrected the reshape operation for hidden_states_scale to ensure compatibility with the trtllm_fp4_block_scale_moe function, preventing incorrect tensor dimensions.
  • FlashInfer Autotuner Compatibility: Addressed an issue where the flashinfer autotuner expected the first dimension of MoE tensors to be num_tokens, which was not being met due to incorrect scaling factor reshaping.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • python/sglang/srt/layers/moe/fused_moe_triton/layer.py
    • Modified the hidden_states_scale argument in the trtllm_fp4_block_scale_moe call to use reshape(*hs_scale_linear.shape[:-1], -1) instead of view(torch.float8_e4m3fn).
  • python/sglang/srt/layers/moe/moe_runner/flashinfer_trtllm.py
    • Updated the hidden_states_scale argument in the fused_experts_none_to_flashinfer_trtllm_fp4 function to use reshape(*hs_scale_linear.shape[:,-1], -1) instead of flatten().
  • python/sglang/srt/layers/quantization/compressed_tensors/schemes/compressed_tensors_w4a4_nvfp4_moe.py
    • Changed the hs_scale assignment to use reshape(*hs_sf_bytes.shape[:,-1], -1) instead of reshape(-1).
Activity
  • No activity has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions bot added the blackwell SM100/SM120 label Feb 23, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request aims to fix an issue with the flashinfer autotuner by ensuring the hidden_states_scale tensor is 2D instead of being flattened. The changes across the three files are consistent with this goal. However, there are critical syntax errors in two of the files (python/sglang/srt/layers/moe/moe_runner/flashinfer_trtllm.py and python/sglang/srt/layers/quantization/compressed_tensors/schemes/compressed_tensors_w4a4_nvfp4_moe.py) where an invalid tuple slicing [:,-1] is used. This should be corrected to [:-1] to avoid a SyntaxError.

@wenscarl wenscarl force-pushed the fix_trtllm_moe_autotune branch from babc680 to 62234ac Compare February 23, 2026 18:58
@nvpohanh
Copy link
Collaborator

@wenscarl Should we mark this PR as DRAFT until FlashInfer PR has been merged and SGLang has upgraded to a new FlashInfer version containing the fix? Thanks!

@wenscarl wenscarl marked this pull request as draft February 25, 2026 01:32
@wenscarl wenscarl marked this pull request as ready for review February 25, 2026 13:04
@wenscarl
Copy link
Collaborator Author

@nvpohanh Actually this PR doesn't rely on flashinfer fix or update which add coverage to the cases when the first dim is not power of 2.

@nvpohanh
Copy link
Collaborator

nvpohanh commented Mar 2, 2026

Got it, then I think this PR is ready to merge.

@nvpohanh
Copy link
Collaborator

nvpohanh commented Mar 2, 2026

@wenscarl Could you update the PR description?

Credits to @danisereb. Depends on flashinfer fix: flashinfer-ai/flashinfer#2617

This line makes me wonder if this is blocked by FlashInfer PR. Thanks!

@nvpohanh
Copy link
Collaborator

nvpohanh commented Mar 2, 2026

@Fridge003 Could you assign this to the MoE expert for review? This is a small change. Thanks!

@Fridge003
Copy link
Collaborator

/tag-and-rerun-ci

@github-actions github-actions bot added the run-ci label Mar 6, 2026
@Fridge003 Fridge003 merged commit 61de303 into sgl-project:main Mar 6, 2026
147 of 159 checks passed
Wangzheee pushed a commit to Wangzheee/sglang that referenced this pull request Mar 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants