Skip to content

[BugFix][Ops][310p]:fix the accuracy issue caused by MoEGatingTopkSoftmax - #11391

Merged
realliujiaxu merged 6 commits into
vllm-project:mainfrom
Tflowers-0129:7-3-moegating-planb
Jul 4, 2026
Merged

realliujiaxu merged 6 commits into
vllm-project:mainfrom
Tflowers-0129:7-3-moegating-planb

Conversation

@Tflowers-0129

@Tflowers-0129 Tflowers-0129 commented Jul 3, 2026 •

Copy link
Copy Markdown
Collaborator

What this PR does / why we need it?

Based on previous community issues, we had already noticed that Qwen3.5-MoE might have a hidden accuracy issue. The root cause has now been identified: when the input token dimension of the MoEGatingTopkSoftmax operator is 2048, UB overflow may occur, which can eventually produce INF values and lead to accuracy degradation.

Since the operator-level fix may not be externally available, this PR first introduces a workaround at the vllm-ascend level.

Does this PR introduce any user-facing change?

NA

How was this patch tested?

Local test

…tmax

Signed-off-by: Tflowers-0129 <2906339855@qq.com>
@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! Thank you for contributing to the vLLM Ascend project. The following points will speed up your PR merge:‌‌

  • A PR should do only one thing, smaller PRs enable faster reviews.
  • Every PR should include unit tests and end-to-end tests ‌to ensure it works and is not broken by other future PRs.
  • Write the commit message by fulfilling the PR description to help reviewer and future developers understand.

If CI fails, you can run linting and testing checks locally according Contributing and Testing.


Tip

💡 Consider Linking a Related Issue or RFC

Your PR title contains the [BugFix] tag, indicating a bug fix or new feature.

Linking a related issue or RFC in the PR description is strongly encouraged — it gives reviewers helpful context and speeds up the review. You can use any of these keywords:

  • Fixes #<issue_number>
  • Closes #<issue_number>
  • Resolves #<issue_number>
  • Refs #<rfc_or_issue_number> (for RFCs)

🙏 Thanks for helping us keep the project well-organized!

@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 addresses a hardware-specific limitation on 310P devices where the MoE gating softmax operation produces incorrect results for input batches exceeding 1024 tokens. By introducing a chunking mechanism, the logic now safely processes large inputs in smaller segments, ensuring numerical stability and correctness.

Highlights

  • MoE Gating Workaround: Implemented a chunking strategy for the MoE gating operation to avoid accuracy issues on 310P hardware when processing batches larger than 1024 tokens.
  • Test Coverage: Added a new unit test to verify that large token batches are correctly split and processed by the gating function.
New Features

🧠 You can now enable Memory (public preview) 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.

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 the 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 counterproductive. 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.

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. ↩

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

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.

Code Review

Suggested PR Title:

[Ops][BugFix] Chunk router logits for npu_moe_gating_top_k_softmax on 310P when tokens exceed 1024

Suggested PR Summary:

### What this PR does / why we need it?

On Ascend 310P, the `torch_npu.npu_moe_gating_top_k_softmax` operator returns invalid routing results when it receives more than 1024 tokens. This PR fixes this issue by chunking the `router_logits` along the token dimension (dim 0) into chunks of size 1024, processing each chunk individually, and then concatenating the results.

Additionally, the review feedback suggests ensuring that `router_logits` is contiguous before processing to prevent potential runtime errors or silent correctness issues with custom NPU operators.

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

A new unit test `test_select_experts_chunks_large_token_batch` has been added in `tests/ut/_310p/fused_moe/test_experts_selector_310.py` to verify the chunking behavior and output shapes for a batch of 2050 tokens.

Comment thread vllm_ascend/_310p/fused_moe/experts_selector.py
@Tflowers-0129

Tflowers-0129 commented Jul 3, 2026 •

Copy link
Copy Markdown
Collaborator Author

The accuracy results and performance impact are shown below:
Gsm8k accuracy:
image

TextVqa accuracy:
image

the Performance:
The performance impact is manageable. It mainly affects scenarios with Prefill requests, with an estimated regression of about 150–200 us per layer. Overall, the impact on performance is relatively small.

image

@Tflowers-0129

Tflowers-0129 commented Jul 3, 2026 •

Copy link
Copy Markdown
Collaborator Author

算子精度问题测试脚本:

import torch
import torch_npu

torch.manual_seed(0)
torch.npu.set_compile_mode(jit_compile=False)

# 2048 个 token 使用完全相同的 router logits,正确结果也应该完全相同。
router_logits = torch.randn(1, 256, dtype=torch.float16).repeat(2048, 1).npu()

# 原始调用:2048 个 token 一次送入算子。
full_weights, full_ids, _ = torch_npu.npu_moe_gating_top_k_softmax(
    router_logits, k=8
)

# 对照调用:相同输入按 1024 个 token 分块。
part1 = torch_npu.npu_moe_gating_top_k_softmax(router_logits[:1024], k=8)
part2 = torch_npu.npu_moe_gating_top_k_softmax(router_logits[1024:], k=8)
chunk_weights = torch.cat([part1[0], part2[0]], dim=0)
chunk_ids = torch.cat([part1[1], part2[1]], dim=0)

torch.npu.synchronize()

print("full batch 非有限权重数量:", (~torch.isfinite(full_weights)).sum().item())
print("chunked 非有限权重数量:", (~torch.isfinite(chunk_weights)).sum().item())
print("full batch 与 chunked 的 expert id 差异数量:", (full_ids != chunk_ids).sum().item())
print("full batch 与 chunked 的最大权重差:", (full_weights - chunk_weights).abs().max().item())

表现:

full batch 非有限权重数量: 1792
chunked 非有限权重数量: 0
full batch 与 chunked 的 expert id 差异数量: 16384
full batch 与 chunked 的最大权重差: 65504.0

@Tflowers-0129

Copy link
Copy Markdown
Collaborator Author

Fix this issue: #10892

@realliujiaxu
realliujiaxu merged commit 0dc58f8 into vllm-project:main Jul 4, 2026
34 checks passed
wangyichao1999 pushed a commit to wangyichao1999/vllm-ascend that referenced this pull request Jul 9, 2026
…tmax (vllm-project#11391)

### What this PR does / why we need it?

Based on previous community issues, we had already noticed that
Qwen3.5-MoE might have a hidden accuracy issue. The root cause has now
been identified: when the input token dimension of the
`MoEGatingTopkSoftmax` operator is 2048, UB overflow may occur, which
can eventually produce INF values and lead to accuracy degradation.

Since the operator-level fix may not be externally available, this PR
first introduces a workaround at the vllm-ascend level.

### Does this PR introduce _any_ user-facing change?

NA

### How was this patch tested?

Local test


- vLLM version: v0.23.0
- vLLM main:
vllm-project/vllm@b9a7cd4

Signed-off-by: Tflowers-0129 <2906339855@qq.com>
Alex-stack-hub pushed a commit to 0moyi0-2024/vllm-ascend_tp that referenced this pull request Jul 27, 2026
…tmax (vllm-project#11391)

### What this PR does / why we need it?

Based on previous community issues, we had already noticed that
Qwen3.5-MoE might have a hidden accuracy issue. The root cause has now
been identified: when the input token dimension of the
`MoEGatingTopkSoftmax` operator is 2048, UB overflow may occur, which
can eventually produce INF values and lead to accuracy degradation.

Since the operator-level fix may not be externally available, this PR
first introduces a workaround at the vllm-ascend level.

### Does this PR introduce _any_ user-facing change?

NA

### How was this patch tested?

Local test


- vLLM version: v0.23.0
- vLLM main:
vllm-project/vllm@b9a7cd4

Signed-off-by: Tflowers-0129 <2906339855@qq.com>
MmMmaru pushed a commit to jiaqi-lee/vllm-ascend that referenced this pull request Aug 19, 2026
…tmax (vllm-project#11391)

### What this PR does / why we need it?

Based on previous community issues, we had already noticed that
Qwen3.5-MoE might have a hidden accuracy issue. The root cause has now
been identified: when the input token dimension of the
`MoEGatingTopkSoftmax` operator is 2048, UB overflow may occur, which
can eventually produce INF values and lead to accuracy degradation.

Since the operator-level fix may not be externally available, this PR
first introduces a workaround at the vllm-ascend level.

### Does this PR introduce _any_ user-facing change?

NA

### How was this patch tested?

Local test


- vLLM version: v0.23.0
- vLLM main:
vllm-project/vllm@b9a7cd4

Signed-off-by: Tflowers-0129 <2906339855@qq.com>
shiqiangA pushed a commit to shiqiangA/vllm-ascend that referenced this pull request Aug 20, 2026
…tmax (vllm-project#11391)

### What this PR does / why we need it?

Based on previous community issues, we had already noticed that
Qwen3.5-MoE might have a hidden accuracy issue. The root cause has now
been identified: when the input token dimension of the
`MoEGatingTopkSoftmax` operator is 2048, UB overflow may occur, which
can eventually produce INF values and lead to accuracy degradation.

Since the operator-level fix may not be externally available, this PR
first introduces a workaround at the vllm-ascend level.

### Does this PR introduce _any_ user-facing change?

NA

### How was this patch tested?

Local test


- vLLM version: v0.23.0
- vLLM main:
vllm-project/vllm@b9a7cd4

Signed-off-by: Tflowers-0129 <2906339855@qq.com>
Leetrytry pushed a commit to Leetrytry/vllm-ascend that referenced this pull request Sep 11, 2026
…tmax (vllm-project#11391)

### What this PR does / why we need it?

Based on previous community issues, we had already noticed that
Qwen3.5-MoE might have a hidden accuracy issue. The root cause has now
been identified: when the input token dimension of the
`MoEGatingTopkSoftmax` operator is 2048, UB overflow may occur, which
can eventually produce INF values and lead to accuracy degradation.

Since the operator-level fix may not be externally available, this PR
first introduces a workaround at the vllm-ascend level.

### Does this PR introduce _any_ user-facing change?

NA

### How was this patch tested?

Local test


- vLLM version: v0.23.0
- vLLM main:
vllm-project/vllm@b9a7cd4

Signed-off-by: Tflowers-0129 <2906339855@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants