Skip to content

Fix qwen3.5 mtp eplb related issues#19767

Merged
ispobock merged 2 commits intosgl-project:mainfrom
luoyuyan:qwen35-deepep-eplb-mtp
Mar 9, 2026
Merged

Fix qwen3.5 mtp eplb related issues#19767
ispobock merged 2 commits intosgl-project:mainfrom
luoyuyan:qwen35-deepep-eplb-mtp

Conversation

@luoyuyan
Copy link
Copy Markdown
Contributor

@luoyuyan luoyuyan commented Mar 3, 2026

Motivation

support eplb with mtp for Qwen3.5 & Qwen3-Next

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
Copy Markdown
Contributor

Summary of Changes

Hello, 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 enhances the SGLang framework by integrating Expert Parallelism Load Balancing (EPLB) capabilities for Qwen3.5 and Qwen3-Next models, particularly in Model Tensor Parallelism (MTP) environments. The changes involve introducing a specific flag to differentiate model behaviors, optimizing expert weight loading, and carefully managing expert distribution recording during the model's forward pass to ensure compatibility and efficiency within the distributed inference system.

Highlights

  • Qwen3.5 and Qwen3-Next EPLB Support: Enabled Expert Parallelism Load Balancing (EPLB) for Qwen3.5 and Qwen3-Next models, specifically when used with Model Tensor Parallelism (MTP).
  • Conditional Expert Dispatch: Introduced an is_nextn flag to conditionally manage ExpertLocationDispatchInfo initialization, allowing for different EPLB behaviors for Qwen3-Next models.
  • Lazy Expert Weight Loading: Implemented lazy loading for routed expert weights in Qwen3.5 models using LazyValue to optimize resource usage.
  • Expert Distribution Recorder Control: Wrapped the model's forward pass in MTP implementations for Qwen3.5 and Qwen3-Next to disable global expert distribution recording during this phase, ensuring correct behavior in parallel setups.
  • Model Configuration for EPLB: Added get_model_config_for_expert_location methods to relevant model classes to provide necessary configuration for EPLB.

🧠 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/models/qwen2_moe.py
    • Added is_nextn parameter to the Qwen2MoeSparseMoeBlock constructor.
    • Stored the is_nextn flag as an instance variable.
    • Modified _forward_deepep to conditionally initialize expert_location_dispatch_info based on the is_nextn flag.
  • python/sglang/srt/models/qwen3_5.py
    • Imported LazyValue for deferred computation.
    • Added is_nextn parameter to the __init__ methods of Qwen3_5DecoderLayer, Qwen3_5Model, and Qwen3_5ForCausalLM.
    • Passed the is_nextn flag to the Qwen2MoeSparseMoeBlock constructor.
    • Introduced a get_model_config_for_expert_location class method in Qwen3_5MoeModel.
    • Implemented lazy loading for _routed_experts_weights_of_layer using LazyValue and exposed it via a property.
  • python/sglang/srt/models/qwen3_5_mtp.py
    • Imported get_global_expert_distribution_recorder and ModelConfigForExpertLocation.
    • Passed is_nextn=True to the Qwen3_5Model constructor.
    • Added a get_model_config_for_expert_location class method.
    • Wrapped the model's forward pass within a context that disables global expert distribution recording.
  • python/sglang/srt/models/qwen3_next.py
    • Added is_nextn parameter to the __init__ methods of Qwen3NextDecoderLayer, Qwen3NextModel, and Qwen3NextForCausalLM.
    • Passed the is_nextn flag to the Qwen2MoeSparseMoeBlock constructor.
  • python/sglang/srt/models/qwen3_next_mtp.py
    • Imported get_global_expert_distribution_recorder.
    • Passed is_nextn=True to the Qwen3NextModel constructor.
    • Wrapped the model's forward pass within a context that disables global expert distribution recording.
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.

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 introduces support for EPLB (Expert Placement and Load Balancing) with MTP (Multi-Turn Prefill) for Qwen3.5 and Qwen3-Next models. The changes primarily involve adding an is_nextn flag to differentiate model variants and control expert location dispatching. Additionally, expert distribution recording is disabled for MTP models to avoid unwanted side effects.

The implementation is consistent across the affected model files. I have one suggestion to refactor a duplicated helper method to improve code maintainability. Overall, the changes look good and align with the PR's objectives.

Comment on lines +90 to +97
@classmethod
def get_model_config_for_expert_location(cls, config):
text_config = getattr(config, "text_config", config)
return ModelConfigForExpertLocation(
num_layers=text_config.num_hidden_layers,
num_logical_experts=text_config.num_experts,
num_groups=None,
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

This get_model_config_for_expert_location method is identical to the one in Qwen3_5MoeForConditionalGeneration (in python/sglang/srt/models/qwen3_5.py). To improve maintainability and reduce code duplication, consider extracting this logic into a shared helper function or a mixin class.

For example, you could create a helper function in a utility module:

from sglang.srt.eplb.expert_location import ModelConfigForExpertLocation

def get_model_config_for_expert_location_with_text_config(config):
    text_config = getattr(config, "text_config", config)
    return ModelConfigForExpertLocation(
        num_layers=text_config.num_hidden_layers,
        num_logical_experts=text_config.num_experts,
        num_groups=None,
    )

And then call this helper from both places.

@luoyuyan luoyuyan force-pushed the qwen35-deepep-eplb-mtp branch from 43d1b85 to 8d901cb Compare March 3, 2026 09:40
@yizhang2077
Copy link
Copy Markdown
Collaborator

/tag-run-ci-label

@github-actions github-actions Bot added the run-ci label Mar 5, 2026
@yizhang2077
Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@ispobock ispobock merged commit cabe171 into sgl-project:main Mar 9, 2026
203 of 221 checks passed
@luoyuyan luoyuyan deleted the qwen35-deepep-eplb-mtp branch March 9, 2026 10:08
scottgl9 pushed a commit to scottgl9/sglang that referenced this pull request Mar 10, 2026
liubiyongge pushed a commit to liubiyongge/sglang that referenced this pull request Mar 13, 2026
scottgl9 pushed a commit to scottgl9/sglang that referenced this pull request Mar 14, 2026
scottgl9 pushed a commit to scottgl9/sglang that referenced this pull request Mar 18, 2026
Wangzheee pushed a commit to Wangzheee/sglang that referenced this pull request Mar 21, 2026
scottgl9 pushed a commit to scottgl9/sglang that referenced this pull request Mar 22, 2026
JustinTong0323 pushed a commit to JustinTong0323/sglang that referenced this pull request Apr 7, 2026
vroomfondel added a commit to vroomfondel/dgxarley that referenced this pull request Apr 7, 2026
**Upstream status** as of 2026-04-06:
- Qwen3.5: fixed via [PR #19767](sgl-project/sglang#19767) (merged 2026-03-09, included in v0.5.10)
- Qwen3: [PR #21461](sgl-project/sglang#21461) — closed without merge 2026-03-30 (CI failure), superseded by #21822
- Qwen3: [PR #21822](sgl-project/sglang#21822) — new fix opened 2026-03-26, addresses `AttributeError: 'LazyValue' object has no attribute 'keys'` in `eplb_manager.py` for Qwen3 MoE. Code review 2026-04-04 by `Fridge003` and `Evgueni-Petrov-aka-espetrov`. Alternative `LazyValue.__getattr__` approach proposed (avoids modifying the model class). **Approved** by `Fridge003` on 2026-04-06, CI rerun triggered — awaiting merge. (Duplicate [PR #21820](sgl-project/sglang#21820) was closed same day in favour of #21822.) Not in v0.5.10

When `--enable-eplb` is active with EP, the `EPLBManager` crashes after its first rebalance interval (default: 1000 forward passes):
- SGLang PR #17137 — non-Marlin WNA16MoE port (does not fix EP bug)
- SGLang #14158 — update_weights_from_tensor for WNA16MoE (unrelated)
- SGLang [PR #13715](sgl-project/sglang#13715) — fix EPLB + FP4 weight tensor filtering (merged, different issue)
- SGLang [PR #20963](sgl-project/sglang#20963) — Nvidia modelopt refactoring (1/N). Under active review: reviewer `Edwardf0t1` asked for end-to-end verification 2026-03-31, author `wenscarl` responded 2026-04-01 and posted 3 further inline review responses 2026-04-06. Not stalled but awaiting approval. Migrates the NVFP4 code as-is — expected vehicle for EP-awareness fixes (#20869, #21630). Watch this PR for resolution of the NVFP4 input_scale and CutlassMoEParams bugs
- SGLang [PR #21822](sgl-project/sglang#21822) — new EPLB/Qwen3 fix (opened 2026-03-26). Addresses `LazyValue.keys()` AttributeError. Code review 2026-04-04 by `Fridge003` and `Evgueni-Petrov-aka-espetrov`. Alternative `LazyValue.__getattr__` approach proposed. **Approved** by `Fridge003` on 2026-04-06, CI rerun triggered — awaiting merge

"Good code is like humor: when you have to explain it, it’s bad." - Cory House
P.S.: Code reviews and approvals are crucial for maintaining high-quality software.
yhyang201 pushed a commit to yhyang201/sglang that referenced this pull request Apr 22, 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