Skip to content

[Performance][Ops] Optimize MoE counts cumsum on Ascend 910B - #19762

Closed
lumen183 wants to merge 4 commits into
vllm-project:mainfrom
lumen183:perf/moe-counts-cumsum
Closed

lumen183 wants to merge 4 commits into
vllm-project:mainfrom
lumen183:perf/moe-counts-cumsum

Conversation

@lumen183

@lumen183 lumen183 commented Oct 25, 2026 •

Copy link
Copy Markdown

What this PR does / why we need it?

This pull request introduces a specialized custom operator moe_counts_cumsum for Ascend NPU to optimize the cumulative sum of MoE expert counts. It implements an Ascend C kernel (moe_counts_cumsum_kernel), registers it as a PyTorch custom operator, and integrates it into cumsum_group_list to avoid unnecessary narrowing and keep the scan operation in the NPU stream.

Motivation

Profiling MoE inference on Ascend 910B2 shows that cumsum_group_list launches an AICPU task when converting INT64 per-expert token counts to cumulative group boundaries. These arrays are small: DeepSeek-V4-Flash uses 256 entries with EP disabled and 32 entries per rank with EP8.

In the tested CANN/torch_npu stack, the generic AI Core/vector Cumsum path does not support this INT64 input, so the native operation runs on AICPU. This is a limitation of the available Cumsum path, rather than a lack of INT64 scalar arithmetic on AI Core.

The workload is dominated by task and framework overhead. A device-side instrumentation experiment of TP8 workload measured:

Component Mean time
Task handling before and after the operator entry 14.52 µs
Framework and control flow inside the entry 13.06 µs
Original INT64 prefix-sum loop, including reads/writes 0.49 µs
Complete instrumented device task 28.08 µs

Implementation

Add an AscendC kernel specialized for small MoE counts-to-prefix conversion:

  • Launch one AIV block, copy counts to UB, compute the inclusive prefix sum with INT64 scalar arithmetic, and copy the result back to GM.
  • Use explicit MTE2→scalar and scalar→MTE3 dependencies, and enqueue on the current NPU stream through the existing custom-op adapter.
  • Dispatch from cumsum_group_list only for counts-to-prefix conversion with a one-dimensional NPU INT64 input. This specialized path assumes the small MoE counts array fits in UB. Retain the original path for other conversions and when the custom operator is unavailable or disabled.

Does this PR introduce any user-facing change?

Yes. This is a performance improvement for the eligible INT64 MoE counts-to-prefix path on Ascend 910B2, with no changes to the existing API.

How was this patch tested?

Validation environment

8 × Ascend 910B2; driver 25.2.1; CANN 9.2.0; torch 2.10.0+cpu; torch_npu 2.10.0.post7; Triton Ascend 3.6.0; Python 3.12.13. All measured tensors execute on NPU.

vllm-ascend base: e07bf18b76ce8543baffafa9ba868e86305d15b2; core vLLM: db9527a46873454610df6dbedf79a36d6bf1a7f6 (0.31.0+empty). Model: DeepSeek-V4-Flash W8A8, 43 layers, 256 experts, top-k 6; MTP disabled.

Operator performance

Latency was measured with NPU Events during graph replay, without profiling. Results report median latency per operation across repeated trials, measured over 64 operations per replay.

Input Native AIV Latency reduction
INT64[32] 23.823 µs 1.370 µs 94.25%
INT64[256] 24.405 µs 2.250 µs 90.78%
INT64[1024] 25.288 µs 5.326 µs 78.94%
INT64[4096] 28.856 µs 17.522 µs 39.28%

Correctness and model accuracy

Model-level validation used a fixed 256-question GSM8K subset with real prefill and normal greedy generation: seed 42, temperature 0, maximum 2048 generated tokens.

Run Correct Accuracy
Native repeat used for the comparison 247 / 256 96.48%
AIV cumsum 246 / 256 96.09%

The two runs agree on 255/256 final answers. An earlier native run scored 248/256, demonstrating same-route model-run variability. These result suggests that AIV cumsum implementation preserves model-level accuracy.

Model decode performance

The completed TP8 / EP-disabled experiment uses real model weights, synthetic initial KV. It measures client-observed token delivery within the inference engine, excluding HTTP transport and detokenization. Mean TPOT is (last_token_time - first_token_time) / 1023, excluding the first-token interval. Each configuration has native-pre → AIV → native-post stages, with three 1024-token runs per stage. The native result averages the pre/post means.

Configuration Native mean TPOT AIV mean TPOT Reduction P50 ITL, native → AIV
TP8, EP off, B1 29.805 ms 28.505 ms 1.301 ms / 4.36% 30.156 → 30.069 ms
TP8, EP off, B32 41.286 ms 39.950 ms 1.336 ms / 3.24% 40.211 → 40.020 ms
TP8 / EP8, B1 29.703 ms 28.381 ms 1.323 ms / 4.45% 30.230 → 30.079 ms
TP8 / EP8, B32 43.766 ms 42.597 ms 1.168 ms / 2.67% 41.367 → 40.432 ms

Reproduction

After applying the patch and building the custom extension in a matching Ascend environment, run from the vllm-ascend repository root:

python -m pytest -q tests/ut/ops/test_moe_counts_cumsum_dispatch.py
python -m pytest -q tests/e2e/nightly/single_node/ops/singlecard_ops/test_moe_counts_cumsum.py

Scope and remaining validation

This PR specializes the small INT64 MoE counts-to-prefix workload. Performance and device validation reported here cover Ascend 910B2.


Checklist

Testing

  • Boundary / edge-case coverage: I reviewed whether this change involves
    boundary conditions (e.g. empty input, single/zero element, min/max values,
    off-by-one, sequence-length limits, integer overflow). Where applicable, I
    added unit tests covering these edge cases in tests/ut/ or tests/e2e/.

  • Regression test: If this PR fixes a bug, I added a regression test
    that would fail without the fix.

  • Error / failure path coverage: I covered abnormal paths (exceptions,
    timeouts, out-of-range, division by zero) in addition to the happy path.

  • Meaningful & reliable tests: New tests assert real behavior and are
    not written merely to satisfy CI.

  • Performance impact: If this PR touches a performance-sensitive path,
    I attached benchmark results and added performance regression tests.

  • vLLM main: vllm-project/vllm@24420b7

@github-actions

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.

@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 introduces a performance optimization for Mixture of Experts (MoE) inference on Ascend 910B2. It replaces the slow AICPU-based prefix-sum operation for small INT64 token counts with a specialized AscendC AIV scalar kernel. This change achieves up to a 93.5% latency reduction for the cumsum operation without altering the user-facing API or compromising model accuracy.

Highlights

  • Performance Optimization: Accelerated the INT64 MoE group-list cumsum operation on Ascend 910B2 by implementing a specialized AIV scalar kernel, bypassing the slower AICPU path.
  • Kernel Implementation: Added a custom AscendC kernel in moe_counts_cumsum.cpp utilizing INT64 scalar arithmetic and explicit MTE dependencies.
  • Integration & Dispatch: Integrated the kernel with PyTorch via custom operators and updated cumsum_group_list to conditionally dispatch to the new kernel.
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][Feature] Add specialized moe_counts_cumsum custom operator for Ascend NPU

Suggested PR Summary:

### What this PR does / why we need it?
This pull request introduces a specialized custom operator `moe_counts_cumsum` for Ascend NPU to optimize the cumulative sum of MoE expert counts. It implements an Ascend C kernel (`moe_counts_cumsum_kernel`), registers it as a PyTorch custom operator, and integrates it into `cumsum_group_list` to avoid unnecessary narrowing and keep the scan operation in the NPU stream.

Feedback:
- In `moe_counts_cumsum.cpp`, `DataCopy` should be used instead of `DataCopyPad` when copying from Local Tensor to Global Tensor (UB -> GM).
- When copying from GM to UB with `DataCopyPad`, `isPad` should be set to `true` to handle unaligned memory access safely.
- A safety check should be added to limit the maximum number of elements (e.g., up to 4096) to prevent Unified Buffer (UB) capacity overflow and potential memory corruption.

### Does this PR introduce _any_ user-facing change?
No, this is an internal performance optimization for MoE operations on Ascend NPUs.

### How was this patch tested?
Added end-to-end tests in `test_moe_counts_cumsum.py` and unit tests in `test_moe_counts_cumsum_dispatch.py` to verify correctness, graph replay behavior, and input validation.

Comment thread csrc/kernels/moe_counts_cumsum.cpp Resolved
Comment thread csrc/kernels/moe_counts_cumsum.cpp Resolved
Comment thread vllm_ascend/ops/fused_moe/moe_utils.py Resolved
@lumen183
lumen183 marked this pull request as ready for review October 25, 2026 15:05
@lumen183 lumen183 changed the title [Performance] Accelerate INT64 MoE group-list cumsum with an AIV scalar kernel [Ops][Feature] Add specialized moe_counts_cumsum custom operator for Ascend NPU Oct 25, 2026
@lumen183 lumen183 changed the title [Ops][Feature] Add specialized moe_counts_cumsum custom operator for Ascend NPU [Ops][Feature] Add specialized moe_counts_cumsum custom operator for Ascend 910B Oct 25, 2026
@lumen183

Copy link
Copy Markdown
Author

@zzzzwwjj Could you take a look and help trigger CI? Thanks!

lumen183 commented Oct 26, 2026 •

Copy link
Copy Markdown
Author

I also tested a specialized custom AICPU implementation on Ascend 910B2 with CANN 9.2.1. It performs the full INT64 prefix sum through a direct entry taking input/output pointers and the element count, bypassing the generic NodeDef/Tensor/Context processing.

INT64 elements Native Cumsum Custom AICPU AIV scalar
32 26.220 µs 11.907 µs 1.294 µs
256 26.785 µs 12.386 µs 2.050 µs

These are NPU Event measurements without an active profiling session: the span of 64 captured nodes divided by 64, including inter-node gaps. Values are the median of six trial medians, with 40 measurements per trial after warmup and interleaved route orders. The AIV implementation tested was 3bc7f3bc7.

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@lumen183

Copy link
Copy Markdown
Author

Updated the dispatch to enable moe_counts_cumsum only for the Ascend 910B hardware profile. Other device families retain the existing cumsum path

@lumen183 lumen183 changed the title [Ops][Feature] Add specialized moe_counts_cumsum custom operator for Ascend 910B [Performance][Ops] Optimize MoE counts cumsum on Ascend 910B Oct 30, 2026
@lumen183
lumen183 force-pushed the perf/moe-counts-cumsum branch from 97e4546 to 93e0c5f Compare October 30, 2026 06:59
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

Signed-off-by: lumen183 <2024311525@stu.hit.edu.cn>
Signed-off-by: lumen183 <2024311525@stu.hit.edu.cn>
Signed-off-by: lumen183 <2024311525@stu.hit.edu.cn>
Enable the specialized kernel through the 910B hardware profile and retain the existing conversion on other device families.

Signed-off-by: lumen183 <2024311525@stu.hit.edu.cn>
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has conflicts, please resolve those before we can evaluate the pull request.

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.

1 participant