Skip to content

[NVIDIA] Fix num_experts in modelopt_quant#8811

Merged
ch-wan merged 7 commits intosgl-project:mainfrom
wenscarl:fix_trtllm_moe_create_weights
Aug 6, 2025
Merged

[NVIDIA] Fix num_experts in modelopt_quant#8811
ch-wan merged 7 commits intosgl-project:mainfrom
wenscarl:fix_trtllm_moe_create_weights

Conversation

@wenscarl
Copy link
Copy Markdown
Collaborator

@wenscarl wenscarl commented Aug 5, 2025

A hotfix for #8779.
The trtllm_fp4_block_scale_moe API checks for the routing_logits dim to be consistent with num_experts(global). But previously at create_weight, the num_experts is overwritten by num_local_experts.
cc. @kushanam @zhyncs

Motivation

Modifications

Accuracy Test

With EP enabled:
Accuracy: 0.963
Invalid: 0.000
Latency: 697.093 s
Output throughput: 139.871 token/s

With TP enabled
Accuracy: 0.956
Invalid: 0.000
Latency: 636.827 s
Output throughput: 152.779 token/s

Benchmark & Profiling

With shared expert fusion
Accuracy: 0.963
Invalid: 0.000
Latency: 633.068 s
Output throughput: 154.200 token/s

Checklist

Copy link
Copy Markdown
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.

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 PR provides a hotfix to correctly set the num_experts attribute in the modelopt_quant module. Previously, layer.num_experts was being incorrectly set to the local number of experts, causing dimension inconsistencies with the trtllm_fp4_block_scale_moe API which expects the global number of experts. My change ensures layer.num_experts reflects the total global experts by multiplying the local expert count by the expert parallel world size.

Highlights

  • Corrected num_experts calculation: I've updated the create_weights function in modelopt_quant.py to correctly calculate layer.num_experts. Instead of assigning the local num_experts directly, it now multiplies it by the result of get_moe_expert_parallel_world_size() to reflect the global number of experts.
  • Dependency for parallel world size: To facilitate the correct calculation, I've added an import for get_moe_expert_parallel_world_size from sglang.srt.distributed.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 or fill out our survey to provide feedback.

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.

Copy link
Copy Markdown
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 provides a hotfix for an issue in modelopt_quant related to Mixture-of-Experts (MoE) models. The change correctly calculates the global number of experts by multiplying the number of local experts with the expert parallel world size. This resolves a dimension mismatch issue with the trtllm_fp4_block_scale_moe API, which expects the global number of experts. The fix is correct and well-targeted. I approve these changes.

@kaixih
Copy link
Copy Markdown
Collaborator

kaixih commented Aug 5, 2025

It is a simple change to enable EP fp4 moe, which might affect the original TP case. So can we have both EP and TP accuracy numbers after this change? @wenscarl

@pavanimajety
Copy link
Copy Markdown
Collaborator

@wenscarl May I ask you please fix these unnecessary prints that were merged with #8552:
The model loading is very verbose

@pavanimajety
Copy link
Copy Markdown
Collaborator

We can also perhaps test shared experts fusion with --enable-trtllm-moe and let the default behavior hold

Copy link
Copy Markdown
Collaborator

@ishandhanani ishandhanani left a comment

Choose a reason for hiding this comment

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

Tested with flashinfer 0.2.9 rc2. Lets remove those logs and get this in!

Copy link
Copy Markdown
Collaborator

@pavanimajety pavanimajety left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for making the changes!

Copy link
Copy Markdown
Collaborator

@kaixih kaixih left a comment

Choose a reason for hiding this comment

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

Noticed the accuracy results are already provided in the description. Approved. Thx.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

1 similar comment
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@kushanam kushanam enabled auto-merge (squash) August 6, 2025 05:17
@kushanam kushanam disabled auto-merge August 6, 2025 18:50
@wenscarl wenscarl force-pushed the fix_trtllm_moe_create_weights branch from 5ac4fcb to 85e68e2 Compare August 6, 2025 21:31
@ch-wan ch-wan merged commit 288ae41 into sgl-project:main Aug 6, 2025
6 of 57 checks passed
narutolhy pushed a commit to narutolhy/sglang that referenced this pull request Aug 17, 2025
MahmoudAshraf97 pushed a commit to MahmoudAshraf97/sglang that referenced this pull request Sep 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants