Skip to content

[FlyDSL] [gfx1250] MegaMoE: fold the shared expert output into the fused combine reduce - #7661

Merged
valarLip merged 2 commits into
ROCm:mainfrom
yhl-amd:hyi/mega-moe-combine-shared-add
Nov 11, 2026
Merged

valarLip merged 2 commits into
ROCm:mainfrom
yhl-amd:hyi/mega-moe-combine-shared-add

Conversation

@yhl-amd

@yhl-amd yhl-amd commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Summary

MegaMoEGfx1250.forward can take the shared expert's bf16 output (shared_add) and an optional shared_ready event. The fused scatter combine adds shared_add to each token's sum inside its reduce, so the caller no longer needs a separate routed + shared add after the combine.

Motivation

On the gfx1250 MegaMoE EP decode path, the shared expert runs on a side stream and its output is added to the combined routed output afterwards. That add is one more kernel on the main stream which reads both tensors and writes the result back. The fused combine reduce already reads every token's top-k slot rows; reading one more row there is almost free.

Changes

  • mega_moe.py: forward(..., shared_add=None, shared_ready=None). When shared_ready is given, the current stream waits on it just before the combine, so the shared expert can keep running beside the dispatch and the expert GEMMs.
  • transport.py: combine(..., shared_add=None). Stages that advertise adds_shared get it in their reduce; any other stage adds it after finish(), so callers see the same result either way.
  • combine.py / stages/combine_fused.py: the fused reduce has a second compiled variant keyed by add_shared. The shared row load is issued first so it overlaps the top-k sum; padding tokens past the rank's count read the last real row and are dropped by the store's row clamp.
  • Without shared_add nothing changes.

The result is bf16(bf16(sum) + shared), the same as adding the two bf16 tensors after the combine.

Validation

  • Bit-exact against the previous reduce followed by a torch add, on gfx1250: top-k 6 and top-k 3 shapes, full / partial / single / zero token counts, slot tails and out-of-range regions poisoned with NaN, and edge inputs (sums overflowing to inf, denormals, NaN / inf / near-max values in the shared tensor). All outputs identical, including NaN bit patterns; rows past the token count are left untouched.
  • Also identical to an independent torch reference (fp32 sum in k order, round to bf16, add shared, round to bf16).
  • DeepSeek-V4.1-Flash EP4 (MegaMoE) on gfx1250, served end to end with the caller wired to pass the shared output; GSM8K in line with the baseline.
  • black and ruff clean on the changed files; the modules and op_tests/multigpu_tests/test_mega_moe_gfx1250.py import cleanly.

🤖 Generated with Claude Code

…output

MegaMoEGfx1250.forward takes an optional bf16 [tokens, hidden] shared_add
(e.g. the shared expert's output) and an optional shared_ready event. The
fused scatter combine adds shared_add to each token's sum inside its reduce,
so the caller's separate add kernel and its round trip through memory go
away; any other combine stage adds it after finish(). shared_ready, when
given, is waited on by the current stream just before the combine, so the
shared expert can run on a side stream beside the dispatch and the expert
GEMMs.

The result is bf16(bf16(sum) + shared), bit-identical to adding the bf16
tensors afterwards. Without shared_add nothing changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@yhl-amd
yhl-amd requested a review from a team November 9, 2026 15:07
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter standard tests on MI35X
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:mi300x Run Aiter standard tests on MI300X in PRs; main branch always runs them
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the Radeon R9700 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 7661 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

Reviews:
A reviewer is usually assigned within one working day after the PR is ready for review (see Code Review Process). If nobody has responded for about a week, mention the area contact listed in CONTACTS.md. Want to help review? See REVIEWERS.md.

@github-actions github-actions Bot added the FlyDSL label Nov 9, 2026
@aiter-bot

aiter-bot commented Nov 9, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision 40c98eb092f1 · mode shadow

Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision.

Questions to answer in the PR description

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. What is the most important boundary or failure mode introduced by this change, and what evidence covers it?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — missing ### Reviewed revision in the PR description
  • FAIL — missing ### Behavior and intent in the PR description
  • FAIL — missing ### Validation and unverified scope in the PR description
  • FAIL — missing ### PR-specific question in the PR description
  • FAIL — missing ### Changed op tests in the PR description
  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA
  • FAIL — Behavior and intent has no substantive answer
  • FAIL — Validation and unverified scope has no substantive answer
  • FAIL — PR-specific question has no substantive answer

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

@valarLip
valarLip merged commit 023a9d1 into ROCm:main Nov 11, 2026
yhl-amd added a commit to yhl-amd/ATOM that referenced this pull request Nov 12, 2026
Address review on the shared-in-combine path:

- Drop the inspect.signature probe of MegaMoE.forward and the
  PendingSharedAdd.consumed fallback. aiter main's MegaMoEGfx1250.forward
  takes shared_add / shared_ready (ROCm/aiter#7661), so
  MoriV2ModularKernel always passes them to MegaMoE; its Triton-experts
  branch, which drives MegaMoE's internals instead of its forward, adds
  the shared output after its combine.
- Drop the with_shared_add / pending_shared_add attribute side channel.
  FusedMoE.forward_maybe_comm_fused now hands shared_partial down its
  non-comm-fused branch when the caller passes shared_ready:
  forward_impl -> _run_experts -> Mxfp4MoEMethod.apply -> the modular
  kernel, as explicit arguments. That call goes to forward_impl directly,
  as the comm-fused backend does, because the moe_forward custom op cannot
  carry the event; the caller is already inside its opaque dual-stream op.
  combine_accepts_shared also refuses the steps where a later reduction
  would count the shared output again (SP MegaMoE, PCP merge, simulated
  DP, reduce_results), and an expert path without such a combine raises
  instead of dropping the shared output.
- Drop _dual_stream_shared_in_combine. dual_stream_moe_forward branches
  on combine_accepts_shared and keeps routing through
  routed_expert_forward(x, shared_partial=shared, shared_ready=...), so
  the V4.1 override (fixed-order router) still applies; that override
  forwards shared_ready.

Tests: the dual-stream handoff, forward_maybe_comm_fused with and without
shared_ready, the combine_accepts_shared refusals, and both
MoriV2ModularKernel branches.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
valarLip pushed a commit to ROCm/ATOM that referenced this pull request Nov 13, 2026
…he mori v2 EP path (#3132)

* perf(dsv41): add the shared expert output in the MegaMoE combine on the mori v2 EP path

On the MegaMoE (mori v2 modular kernel) EP path the shared expert output
was joined after the routed combine and added with a separate elementwise
add. It is now handed to MegaMoE as a PendingSharedAdd; the combine waits
on its event right before the reduce and adds it there. The router GEMM is
issued before the shared expert so the routed chain keeps the launch
stream under graph replay. When the installed aiter MegaMoE does not take
shared_add/shared_ready, or the comm-fused backend takes the step, the
output is added as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* refactor(moe): FusedMoE owns the shared-output handoff to the combine; test it

- FusedMoE.combine_accepts_shared(rows) answers whether the routed combine
  can add a shared-expert output on a step (MegaMoE behind the mori v2
  modular kernel, off the comm-fused path); FusedMoE.with_shared_add(pending)
  hands it over for one forward and always clears it, and
  pending_shared_add() is what the quant method reads. PendingSharedAdd
  moves next to FusedMoE. The DeepSeek-V4 MoE no longer reaches into the
  experts' private state.
- CPU tests: the shared output is added exactly once whether the combine
  consumes it or leaves it to the caller, the handoff is cleared afterwards,
  and combine_accepts_shared refuses without the mori v2 kernel and on
  comm-fused steps.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* refactor(moe): pass the shared output to the MegaMoE combine explicitly

Address review on the shared-in-combine path:

- Drop the inspect.signature probe of MegaMoE.forward and the
  PendingSharedAdd.consumed fallback. aiter main's MegaMoEGfx1250.forward
  takes shared_add / shared_ready (ROCm/aiter#7661), so
  MoriV2ModularKernel always passes them to MegaMoE; its Triton-experts
  branch, which drives MegaMoE's internals instead of its forward, adds
  the shared output after its combine.
- Drop the with_shared_add / pending_shared_add attribute side channel.
  FusedMoE.forward_maybe_comm_fused now hands shared_partial down its
  non-comm-fused branch when the caller passes shared_ready:
  forward_impl -> _run_experts -> Mxfp4MoEMethod.apply -> the modular
  kernel, as explicit arguments. That call goes to forward_impl directly,
  as the comm-fused backend does, because the moe_forward custom op cannot
  carry the event; the caller is already inside its opaque dual-stream op.
  combine_accepts_shared also refuses the steps where a later reduction
  would count the shared output again (SP MegaMoE, PCP merge, simulated
  DP, reduce_results), and an expert path without such a combine raises
  instead of dropping the shared output.
- Drop _dual_stream_shared_in_combine. dual_stream_moe_forward branches
  on combine_accepts_shared and keeps routing through
  routed_expert_forward(x, shared_partial=shared, shared_ready=...), so
  the V4.1 override (fixed-order router) still applies; that override
  forwards shared_ready.

Tests: the dual-stream handoff, forward_maybe_comm_fused with and without
shared_ready, the combine_accepts_shared refusals, and both
MoriV2ModularKernel branches.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(moe): drive the shared-output handoff through the real forward_impl

The handoff test mocked forward_impl, so a hop that stopped passing
shared_add/shared_ready on the way to the quant method would go unnoticed.
Run the real forward_maybe_comm_fused -> forward_impl (-> forward_impl_graph
on several DP ranks) -> _run_experts chain against a recording quant method,
check the ordinary forward hands it no shared output, and cover the
Mxfp4MoEMethod.apply guard for paths without a combine that adds it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* [MoE] Describe both shared-add branches; wait on the event unconditionally

The MoriV2ModularKernel.forward docstring now says where the shared
output is added on each branch: inside MegaMoE's combine on its own
forward, and after the combine on the triton_mega_moe branch, which
drives MegaMoE's internals. On that branch shared_ready always comes
with shared_add (forward_maybe_comm_fused sends neither or both), so the
wait no longer checks for None and a future caller cannot get an
unsynchronized add.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(moe): Mxfp4MoEMethod.apply hands the shared output to fused_experts

The last hop of the shared-output handoff: on the modular-kernel path
apply passes shared_add and shared_ready on to fused_experts, and passes
neither when there is no shared output. A recording fused_experts checks
both cases.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

4 participants