Skip to content

[FlyDSL] [Perf] gfx1250 MoE gather-reduce: routed scale + shared-expert add epilogue - #8429

Merged
XiaobingSuper merged 4 commits into
ROCm:mainfrom
XiaobingSuper:xiaobingsuper/gfx1250-moe-gather-reduce-epilogue
Nov 26, 2026
Merged

XiaobingSuper merged 4 commits into
ROCm:mainfrom
XiaobingSuper:xiaobingsuper/gfx1250-moe-gather-reduce-epilogue

Conversation

@XiaobingSuper

@XiaobingSuper XiaobingSuper commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

What

An optional output epilogue for the gfx1250 grouped MoE gather-reduce (moe_gather_reduce, called from _grouped_a8w4_tdm_moe):

out = bf16(bf16(bf16(sum_k w[t,k] * rows) * scale) + addend)

Each step is rounded to the output dtype as the separate torch ops moe_out * scale and then + addend round it, so the bytes are those ops' bytes. The multiply is skipped at scale == 1.0.

  • kernels/moe_gather_reduce.py: an addend_scale build parameter and an addend pointer argument (null when unused).
  • flydsl_moe_gather_reduce(..., addend=None, addend_scale=1.0).
  • moe_output_epilogue(addend, scale, before=None), a context manager for callers of fused_moe:
with moe_output_epilogue(shared_out, routed_scaling_factor, before=wait_for_side_stream) as epi:
    routed = fused_moe(...)          # the gfx1250 grouped A8W4 path takes it
if not epi.consumed:
    routed = routed * routed_scaling_factor + shared_out   # any other path

before runs right before the gather-reduce launch (e.g. joining the stream that produced addend). Only the non-EP _grouped_a8w4_tdm_moe path takes the epilogue; every other path leaves consumed False and the caller applies it, so existing callers are unaffected. The state is thread-local Python state: for eager callers (an opaque custom op), not code traced by torch.compile.

Why

MoE models with a shared expert and a routed scaling factor run the gather-reduce, * routed_scaling_factor and + shared_out as three launches; at decode token counts each is mostly fixed cost.

User: ROCm/ATOM#3680 (MiniMax-M3's MoE block, _routed_plus_shared) wraps the routed MoE in moe_output_epilogue(shared_output, routed_scale, before=<wait for the shared-expert stream>), so the routed scale and the shared expert's add run in the gather-reduce and the wait lands right before that launch.

Performance

hidden 6144, E128 top-4, bf16 out, scale 2.0, MI455X, us:

T gather-reduce + mul + add (eager) same under Inductor fused gather-reduce
64 5.41 3.48 1.84
128 5.61 3.67 1.91
192 6.09 4.56 2.19
256 6.50 4.55 2.21

The plain gather-reduce alone is 1.81-2.18 us at these sizes, so the epilogue costs at most about 0.1 us.

PR Self-Review

Reviewed revision

5e30af6

Behavior and intent

The gfx1250 grouped MoE gather-reduce can fold out * scale + addend into its pass, through flydsl_moe_gather_reduce(addend=, addend_scale=) or, for fused_moe callers, the moe_output_epilogue context manager. Without an addend the kernel and every caller are unchanged.

Validation and unverified scope

  • 1x MI455X (gfx1250), this head: GPU_ARCHS=gfx1250 python op_tests/test_flydsl_moe_aux.py passes all 18 sections. The new "moe_gather_reduce + addend" section: 48 cases (bf16 / f16, scale 1.0 / 0.5, T 1 / 24 / 512, model_dim 512 / 2050, vec 2 / 4), every one bit-exact against the plain reduce followed by * scale and + addend as torch ops, eager and in a CUDA graph. model_dim 2050 covers the scalar tail path.
  • fused_moe + moe_output_epilogue bit-exact against fused_moe then * scale and + shared as torch ops, eager and through Inductor, T = 64-256, and in a CUDA graph with the addend from a side stream joined via before (measured on the previous revision; the kernel is unchanged since).
  • Unverified: EP calls (they leave the epilogue to the caller by design) and gfx950 / gfx942 (this kernel is gfx1250's grouped MoE path).

PR-specific question

test_gather_reduce gains addend_scale; before this PR it fails at once, since build_moe_gather_reduce_module takes no addend_scale and the kernel no addend argument. No existing test covers the epilogue: the gather-reduce cases check the plain reduce against an fp32 reference within tolerance, while the new cases check that the fused scale and add give exactly the bytes of the unfused torch ops, on both the vector and the scalar tail path.

Changed op tests

op_tests/test_flydsl_moe_aux.py: test_gather_reduce(..., addend_scale=None) and one more summary section in main() (48 cases), described above.

🤖 Generated with Claude Code

…shared-expert add

Add an optional output epilogue to the grouped MoE gather-reduce:
out = bf16(bf16(bf16(sum_k w * rows) * scale) + addend), each step rounded
like the separate torch ops it replaces (the multiply is skipped at scale
1.0), so the bytes are unchanged.

moe_output_epilogue(addend, scale, before=None) is a context manager that
hands the pending epilogue to the next _grouped_a8w4_tdm_moe call in the
block; `before` runs right before the gather-reduce launch (e.g. a wait for
the stream that produced addend) and `consumed` tells the caller whether a
gather-reduce took it. EP calls and other MoE paths leave it unconsumed and
the caller applies the scale and add itself. Python-side state: for eager
callers, not torch.compile tracing.

MiniMax-M3.1 TP4 shape (hidden 6144, E128 top-4, bf16), 4x MI455X, us:
  T                       64    128   192   256
  gather-reduce+mul+add  5.41  5.61  6.09  6.50
  fused gather-reduce    1.84  1.91  2.19  2.21
Output bitwise identical to the separate ops (eager and Inductor).

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

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

XiaobingSuper added a commit to XiaobingSuper/aiter that referenced this pull request Nov 23, 2026
XiaobingSuper added a commit to XiaobingSuper/aiter that referenced this pull request Nov 23, 2026
ROCm#8456 (k5 decode AR)

# Conflicts:
#	aiter/ops/flydsl/grouped_moe_gfx1250.py
#	aiter/ops/flydsl/moe_kernels.py
XiaobingSuper added a commit to XiaobingSuper/ATOM that referenced this pull request Nov 23, 2026
…e MoE gather-reduce

With an aiter that has moe_output_epilogue (ROCm/aiter#8429), MiniMax-M3's MoE
with an MXFP4 routed path and a separate shared expert hands the routed
scaling factor and the shared output to the routed MoE's gather-reduce: one
launch in place of gather-reduce + mul + add, bitwise the same output. The
dual-stream path joins the shared stream right before that launch. Where aiter
does not take the epilogue (any other MoE path), the separate scale and add
run as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
XiaobingSuper added a commit to XiaobingSuper/ATOM that referenced this pull request Nov 23, 2026
…e MoE gather-reduce

With an aiter that has moe_output_epilogue (ROCm/aiter#8429), MiniMax-M3's MoE
with an MXFP4 routed path and a separate shared expert hands the routed
scaling factor and the shared output to the routed MoE's gather-reduce: one
launch in place of gather-reduce + mul + add, bitwise the same output. The
dual-stream path joins the shared stream right before that launch. Where aiter
does not take the epilogue (any other MoE path), the separate scale and add
run as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
XiaobingSuper added a commit to XiaobingSuper/aiter that referenced this pull request Nov 23, 2026
ROCm#8456 (k5 decode AR)

# Conflicts:
#	aiter/ops/flydsl/grouped_moe_gfx1250.py
#	aiter/ops/flydsl/moe_kernels.py
XiaobingSuper and others added 3 commits November 23, 2026 10:00
…t an epilogue

`ptr_arg(None)` raised AttributeError on the plain (no addend) path and in
test_gather_reduce. Pass a 0-element tensor, as num_valid_tokens already does.
Also sort MoeOutputEpilogue.__slots__ (ruff RUF023).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
test_gather_reduce takes addend_scale: the epilogue's output must be the
bytes of the plain reduce followed by `* scale` and `+ addend` as torch ops,
at scale 1.0 and 0.5, model_dim 512 and 2050 (vector and tail paths).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@XiaobingSuper
XiaobingSuper marked this pull request as ready for review November 26, 2026 05:32
@XiaobingSuper
XiaobingSuper requested review from a team and a balanced review from Copilot November 26, 2026 05:32

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@aiter-bot

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

Revision 5e30af60c469 · mode shadow

Author self-review accepted. Readiness report:

Author answers

  • PASS — current-SHA answers are present and structurally specific
Show the author's self-review answers

Behavior and intent

The gfx1250 grouped MoE gather-reduce can fold `out * scale + addend` into its pass, through `flydsl_moe_gather_reduce(addend=, addend_scale=)` or, for `fused_moe` callers, the `moe_output_epilogue` context manager. Without an addend the kernel and every caller are unchanged.

Validation and unverified scope

- 1x MI455X (gfx1250), this head: `GPU_ARCHS=gfx1250 python op_tests/test_flydsl_moe_aux.py` passes all 18 sections. The new "moe_gather_reduce + addend" section: 48 cases (bf16 / f16, scale 1.0 / 0.5, T 1 / 24 / 512, model_dim 512 / 2050, vec 2 / 4), every one bit-exact against the plain reduce followed by `* scale` and `+ addend` as torch ops, eager and in a CUDA graph. model_dim 2050 covers the scalar tail path.
- `fused_moe` + `moe_output_epilogue` bit-exact against `fused_moe` then `* scale` and `+ shared` as torch ops, eager and through Inductor, T = 64-256, and in a CUDA graph with the addend from a side stream joined via `before` (measured on the previous revision; the kernel is unchanged since).
- Unverified: EP calls (they leave the epilogue to the caller by design) and gfx950 / gfx942 (this kernel is gfx1250's grouped MoE path).

PR-specific question

`test_gather_reduce` gains `addend_scale`; before this PR it fails at once, since `build_moe_gather_reduce_module` takes no `addend_scale` and the kernel no `addend` argument. No existing test covers the epilogue: the gather-reduce cases check the plain reduce against an fp32 reference within tolerance, while the new cases check that the fused scale and add give exactly the bytes of the unfused torch ops, on both the vector and the scalar tail path.

Changed op tests

`op_tests/test_flydsl_moe_aux.py`: `test_gather_reduce(..., addend_scale=None)` and one more summary section in `main()` (48 cases), described above.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply
  • ADVISORY — Copilot could not review the current head (for example, quota or service failure)

Code and op-test hygiene

  • PASS — no committed artifact or conflict marker detected

Required CI

  • PASS — Black: passed
  • PASS — Ruff: passed
  • PASS — Repository dependency: passed
  • PASS — Aiter Test Gate: passed

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

@XiaobingSuper
XiaobingSuper merged commit a5927ba into ROCm:main Nov 26, 2026
106 checks passed
XiaobingSuper added a commit to XiaobingSuper/ATOM that referenced this pull request Nov 30, 2026
…e MoE gather-reduce

With an aiter that has moe_output_epilogue (ROCm/aiter#8429), MiniMax-M3's
dual-stream MoE hands the routed scaling factor and the shared output to
the routed MoE's gather-reduce: one launch in place of gather-reduce + mul
+ add, the same bytes. The join with the shared stream moves to right
before that launch. Where aiter does not take the epilogue (any other MoE
path, or an older aiter) the separate scale and add run as before.

The epilogue is Python-side state, so only the eager paths inside the
dual-stream custom op use it.

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