Skip to content

[Performance] Use a plain tensor add for the MoE tail combine outside graph modes - #22969

Open
2luckydog wants to merge 1 commit into
vllm-project:mainfrom
2luckydog:feat/moe_tail_plain_add-main_official
Open

2luckydog wants to merge 1 commit into
vllm-project:mainfrom
2luckydog:feat/moe_tail_plain_add-main_official

Conversation

@2luckydog

@2luckydog 2luckydog commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does / why we need it?

MoE layers with both shared and routed experts merge the two outputs once per layer, at the end of the block. The Ascend runner mirrors upstream's MoERunner.forward and performs that combine with

result = torch._foreach_add([shared_output], [fused_output])[0]

Both operands are ordinary activation tensors, so this is a plain elementwise add. _foreach_add exists to amortise dispatch over a tensor list; with a one-element list there is nothing to amortise, while on NPU the multi-tensor path still launches a dedicated ForeachAddList kernel per call. That launch costs ~17 us regardless of tensor size (measured on Atlas 910B), once per MoE layer, on every decode step.

Profiling a 45-layer GLM-5.3-Flash decode instance shows 2835 calls (45 layers x 63 steps) at 16.7 us each: ~0.75 ms of pure launch overhead per decode step, sitting directly on the token-to-token critical path.

It can be replaced with:

result = shared_output + fused_output

The multi-tensor form is not arbitrary: the superkernel generation path only accepts this combine through torch._foreach_add, and a plain aclnnAdd disconnects the graph. So the combine keeps using whichever form the compilation mode requires:

  • ascend_compilation_config.enable_super_kernel or enable_static_kernel set: keep torch._foreach_add; behaviour is unchanged.
  • otherwise: shared_output + fused_output, which is bit-identical (same dtype, same operand order, elementwise add) and saves one op launch per MoE layer.

The mode is read once in AscendMoERunner.__init__ and cached on the instance, so the hot path never re-reads the config and no graph recompilation happens mid-run; runners built without an initialized Ascend config keep the graph-safe foreach path. No new configuration key, environment variable, or API is introduced.

Performance Test

Metric Before (ms) After (ms) Delta (ms)
ITL 37.1 36.2 -0.9
TPOT 14.84 14.48 -0.36

Setup: GLM-5.3-Flash W8A8, 1P1D PD disaggregation, prefill DP4/TP4/EP16, decode DP12/TP4/EP48, input length 80K, output length 1K, concurrency 12, Atlas 910C, superkernel and static kernel both disabled.

Accuracy Test

No numerical change: same operands, same order, same elementwise add. GPQA Diamond ~89.41 and GSM8K ~97.57 are unchanged before and after.

Does this PR introduce any user-facing change?

No. There is no new configuration key, environment variable, or API. Only the kernel performing the combine changes, and only when the superkernel/static-kernel graph modes are off; with either mode enabled the code path is exactly as before.

How was this patch tested?

Added tests/ut/ops/test_moe_tail_combine.py, hardware-agnostic (pure CPU + mocks):

  1. both branches produce bit-identical results for bfloat16 / float16 / float32 (rtol=0, atol=0);
  2. empty input (0, 8) keeps shape and dtype on both branches;
  3. the foreach branch dispatches torch._foreach_add exactly once, on a one-element list;
  4. the plain branch never dispatches torch._foreach_add (the spy raises);
  5. the four enable_super_kernel x enable_static_kernel combinations map to the expected branch;
  6. an uninitialized Ascend config falls back to the foreach path.

CI testing only: this machine has no NPU, so the suite was not executed locally and the service-level numbers above were not re-measured on this branch.


Testing

  • Boundary / edge-case coverage: empty input, and both branches of the mode decision.

  • Regression test: the mode-matrix and call-count assertions pin the selection logic; they fail if the plain branch is unreachable or the graph modes stop selecting foreach.

  • Error / failure path coverage: no exception, timeout or out-of-range path is introduced; the only error path is an uninitialized Ascend config, which is covered by a fallback test.

  • Meaningful & reliable tests: tests assert bit-exact tensor equality and exact dispatch counts, not "runs without error".

  • Performance impact: operator-level profiling and a service A/B are attached above; the change is limited to a path that is explicitly exempted from the graph modes that need the foreach form.

  • vLLM main: vllm-project/vllm@c410543

@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

This pull request optimizes the MoE tail combine step in AscendMoERunner by conditionally using a plain tensor addition instead of torch._foreach_add when graph modes (super kernel or static kernel) are disabled. This avoids unnecessary op launches per MoE layer outside of graph modes. The PR also introduces comprehensive unit tests in tests/ut/ops/test_moe_tail_combine.py to verify the correctness, shape preservation, and dispatch behavior of both branches. I have no feedback to provide as there are no review comments to evaluate.

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

🤖 How to trigger CI

CI runs automatically when the PR is opened or reopened. Pushing a new commit no longer re-runs the E2E CI (to save NPU runner resources), so use the comment commands below when needed. They are available to the PR author and users with triage permission or above:

  • /run — run the full PR CI gate on the latest commit.
  • /rerun — re-run the failed CI jobs (e.g. after a flaky failure).
  • /e2e — run selected E2E tests instead of the full gate.
  • /nightly / /presmoke / /weekly — run the corresponding large-scale tests (triage permission or above; blocked during the nightly maintenance window).
  • /cancel — cancel a running CI on this PR.

Note

Until the CI gate has run on a commit, the required ci-gate check stays pending and the PR cannot merge — remember to comment /run after pushing.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Last updated: 2026-11-26 09:29 UTC+8 for commit c683d0f.

ci-gate failed. Reason:

CODEOWNERS approval gate failed: at least one changed file has no approval from its owner(s). Open the 'CODEOWNERS' steps above to see which files are missing approvals and who can approve.
select-tests failed. Open the failed 'select-tests' job above to see which step failed (checkout/rebase, coverage config validation, or test selection).

See the workflow run for full logs. This comment is updated in place on each new failure and marked as passed once the gate passes (it is kept for history, not deleted).

Missing owner approvals. Every changed file needs one approval from its CODEOWNERS owner(s):

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Total Coverage Diff Coverage
Total 158773 lines 17 lines
Missing 45258 lines 0 lines
Coverage 71% 100%

More info see: Summary

MoE layers with both shared and routed experts merge the two outputs once per
layer at the end of the block. The Ascend runner mirrors upstream's
``MoERunner.forward`` and performs that combine with
``torch._foreach_add([shared_output], [fused_output])[0]``.

Both operands are ordinary activation tensors, so this is a plain elementwise
add. ``_foreach_add`` exists to amortise dispatch across a tensor list; with a
one-element list there is nothing to amortise, and on NPU the multi-tensor path
still launches a dedicated ``ForeachAddList`` kernel. That launch costs
~13-17us regardless of tensor size, once per MoE layer, on every decode step:
for a 45-layer MoE model this is ~0.75ms of pure launch overhead sitting on the
token-to-token critical path.

The multi-tensor form is still required in the graph modes: the superkernel
generation path only accepts this combine through ``torch._foreach_add``, and a
plain ``aclnnAdd`` disconnects the graph. So the combine uses whichever form
the mode requires:

* ``ascend_compilation_config.enable_super_kernel`` or
  ``enable_static_kernel`` set -> keep ``torch._foreach_add``, unchanged
  behaviour;
* otherwise -> ``shared_output + fused_output``, which is bit-identical (same
  dtype, same operand order, elementwise add) and saves one op launch per MoE
  layer.

No new configuration key or environment variable: the mode is read from the
existing compilation config once in ``__init__`` and cached on the instance, so
the hot path never re-reads it and no graph recompilation happens mid-run. A
runner built without an initialized Ascend config (unit tests) keeps the
graph-safe foreach path.

The combine is extracted into ``_combine_shared_and_routed_output`` so both
branches are directly unit-tested.

Co-authored-by: westbook1006 <1210973614@qq.com>
Co-authored-by: zmc1997 <742133686@qq.com>
Co-authored-by: mengguangxin <mgx0018@163.com>
Co-authored-by: liuwj-research <liuwj.2008@tsinghua.org.cn>
Co-authored-by: dailiang16 <dailiang0110@yeah.net>
Signed-off-by: 2luckydog <2981945956@qq.com>
@2luckydog
2luckydog force-pushed the feat/moe_tail_plain_add-main_official branch from 5c8203a to c683d0f Compare November 23, 2026 13:09
@2luckydog

2luckydog commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

/run
[Bot]: run command accepted. E2E has been queued on the latest commit c683d0fc10b981e84283fc61710909341c6cb740 of this PR, with whatever test-scope labels the PR currently carries. Track progress in PR checks.

@vllm-ascend-ci vllm-ascend-ci added the ci-run E2E requested manually on the latest commit label Nov 23, 2026
@Annarine Annarine added the ready-precise run selected e2e test for pr label Nov 26, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-run E2E requested manually on the latest commit module:ops module:tests ready-precise run selected e2e test for pr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants