Repository navigation
[FlyDSL] [gfx1250] MegaMoE: fold the shared expert output into the fused combine reduce - #7661
Conversation
…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]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
WAITING ON AUTHORPR Self-ReviewRevision 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
Edit the Missing or invalid answers
Shadow mode reports what would block but keeps the status successful. Set |
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]>
…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]>
Summary
MegaMoEGfx1250.forwardcan take the shared expert's bf16 output (shared_add) and an optionalshared_readyevent. The fused scatter combine addsshared_addto each token's sum inside its reduce, so the caller no longer needs a separaterouted + sharedadd 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). Whenshared_readyis 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 advertiseadds_sharedget it in their reduce; any other stage adds it afterfinish(), so callers see the same result either way.combine.py/stages/combine_fused.py: the fused reduce has a second compiled variant keyed byadd_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.shared_addnothing changes.The result is
bf16(bf16(sum) + shared), the same as adding the two bf16 tensors after the combine.Validation
op_tests/multigpu_tests/test_mega_moe_gfx1250.pyimport cleanly.🤖 Generated with Claude Code