Repository navigation
[FlyDSL] [Perf] gfx1250 MoE gather-reduce: routed scale + shared-expert add epilogue - #8429
Conversation
…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]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
…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]>
…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]>
…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]>
READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
…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]>
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 * scaleand then+ addendround it, so the bytes are those ops' bytes. The multiply is skipped atscale == 1.0.kernels/moe_gather_reduce.py: anaddend_scalebuild parameter and anaddendpointer 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 offused_moe:beforeruns right before the gather-reduce launch (e.g. joining the stream that producedaddend). Only the non-EP_grouped_a8w4_tdm_moepath takes the epilogue; every other path leavesconsumedFalse 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_factorand+ shared_outas 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 inmoe_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:
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 + addendinto its pass, throughflydsl_moe_gather_reduce(addend=, addend_scale=)or, forfused_moecallers, themoe_output_epiloguecontext manager. Without an addend the kernel and every caller are unchanged.Validation and unverified scope
GPU_ARCHS=gfx1250 python op_tests/test_flydsl_moe_aux.pypasses 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* scaleand+ addendas torch ops, eager and in a CUDA graph. model_dim 2050 covers the scalar tail path.fused_moe+moe_output_epiloguebit-exact againstfused_moethen* scaleand+ sharedas torch ops, eager and through Inductor, T = 64-256, and in a CUDA graph with the addend from a side stream joined viabefore(measured on the previous revision; the kernel is unchanged since).PR-specific question
test_gather_reducegainsaddend_scale; before this PR it fails at once, sincebuild_moe_gather_reduce_moduletakes noaddend_scaleand the kernel noaddendargument. 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 inmain()(48 cases), described above.🤖 Generated with Claude Code