Repository navigation
[FlyDSL] MoE v2 stage 2: skip the inter_dim padding - #7917
cpersson-amd wants to merge 7 commits into
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
The v2 layout GEMM2 ran every K tile up to the padded inter_dim, and fused_moe never told it about intermediate_pad. With the padding passed down (D_INTER_VALID), K tiles past the unpadded width are not run; in the last partial tile, lanes whose 32-wide K block is padding load A as zero and use unit scales, so their products are exactly zero whatever the padding holds. Since stage 2 no longer reads the padding, stage 1 can skip the padding tile when it feeds a v2 layout stage 2 (stage2_skips_inter_pad, set only by fused_moe next to the stage-2 padding). The a8w4 test data helper takes inter_pad and activation so the e2e test can choose its padding. DSv4.1 TP4 a8w4 fused_moe (inter 576 padded to 640, real routing, MI355X): -7.5% at 256-1024 tokens, -5% at 3000-4096, -4.5% at 16384; flat at 1-16. Reduce rows are bit-exact against intermediate_pad=0. Co-authored-by: Claude Opus 5.5 <[email protected]> Signed-off-by: Carl Persson <[email protected]>
74a52c0 to
08f6dc3
Compare
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 |
|
This PR and the NaN reports in #8382 and #8051 overlap, so here is what we measured with this change applied. Setup. We used the #8382 shape on one MI355X:
Results.
With this PR's stage-1 flag, stage 1 skips those columns on purpose. Because of that, this run cannot show whether the underlying stage-1 problem is fixed. Cause. The cause of #8382 is in the xcd4 stage-1 remap, not in stage 2:
This PR does not change that count. Any caller that launches the xcd4 stage 1 with a pad but without the new flag still gets the unwritten block; per the description, that includes the tuner and FHMoE. We did not test those paths with this PR. Fix. The one-term remap fix is in #8512. With it, on the same setup:
With that fix in, this PR is not needed for #8382. Its stage-2 skip still makes sense on its own merits (speed, and defence in depth against bad padding), but it should not be treated as the fix for the stage-1 bug. Not tested:
|
There was a problem hiding this comment.
ROCm/aiter PR #7917 — [FlyDSL] MoE v2 stage 2: skip the inter_dim padding
This PR makes the DSv4.1 MoE layer 3 to 8% faster on MI355X: the v2 down-projection stops at the real inter_dim, and stage 1 stops writing the padding.
Review (advisory): HIGH RISK
Validation (deterministic): NOT RUN — auto-validation is disabled (REVIEW_AUTO_VALIDATE=0); a manual replay on MI355X at 7625b76 is cited in the first two findings
Perf (advisory): NOT RUN — target exposes no benchmark entry point (no --scenario bench, no perftest/@benchmark harness)
Size: 340 lines / 7 files
Design (advisory): make stage 2 tolerate any bytes in the inter_dim padding so that stage 1 may stop writing it
Human: @fanxingran, as the author of the fused_moe_run_stage2 per-slot and a2 override paths (#7135), could you confirm that their consumers tolerate the stage-1 padding this PR leaves unwritten?
The kernel change looks correct to me. In the last partial K tile, padding lanes get A = 0, unit scales and the last valid weights, so their products are exactly zero. Your test file passes 95/95 on MI355X at 7625b76 in my run.
-
High [verified]: An
a2/w2override of a new width infused_moe_run_stage2keeps the captured pad, so stage 2 silently drops real K columns. With inter 512, pad 64 and an unpadded inter-256 override, rel-L2 is 0.499 against the direct run, and 3.2e-3 with pad 0 (MI355X, 7625b76). Details inline. -
Medium [verified]: The per-slot path multiplies padding that stage 1 no longer writes. It is correct today only because that API admits fp4 activations: 0xFF padding gave 0 NaN, but an fp8-activation kernel through the same wrapper gave 40960 of 40960 NaN. Details inline.
-
Medium [verified]: The diff no longer applies to main 057d818.
git applyfails inaiter/fused_moe.pyat 4286 and inaiter/ops/flydsl/kernels/mxmoe_dispatcher.pyat 808, and the body's 95/95 result names 08f6dc3. Rebase and re-runop_tests/flydsl_tests/test_flydsl_moe.pyon the rebased head. -
Medium [verified]:
get_2stage_cfgschanges every model that pairs aflydsl_moe2_layout_*kernel withintermediate_pad > 0, but the downstream vLLM, SGLang and ATOM jobs were skipped. I suggestci:alland a sign-off from @valarLip before merge. -
Note [inferred]: The "11% wasted K" in Motivation does not apply to stage 2 at DSv4.1 TP4. Split the 3 to 8% gain between stage 1 and stage 2 in the body. Details inline.
| model_dim=model_dim, | ||
| inter_dim=inter_dim, | ||
| num_experts=expert, | ||
| inter_dim_pad=intermediate_pad, |
There was a problem hiding this comment.
This pad stays fixed when an a2/w2 override changes the width, so D_INTER_VALID drops real columns. When a2 is replaced, drop the pad and use the new width.
There was a problem hiding this comment.
Fixed in ec26b80. A replaced a2 now runs with no pad, so stage 2 reads all of it. test_split_intermediate_gather pads one rank and checks the summed output, which fails without the fix.
| activation=activation, | ||
| inter_dim_pad=intermediate_pad, | ||
| model_dim_pad=hidden_pad, | ||
| stage2_skips_inter_pad=is_flydsl2_layout, |
There was a problem hiding this comment.
Stage 1 now skips the padding, but _flydsl_v2_stage2_per_slot reads full K. Apply the pad here only when a2 is not replaced, so test_split_intermediate_gather still passes.
There was a problem hiding this comment.
Instead of giving the per-slot path the pad, I kept the padding written. When stage 2 runs through the staged API (fused_moe_prepare_stage2), stage 1 no longer skips the padding columns, so per-slot reading full K sees zeros, as on main. fused_moe itself still skips them.
The pad can't be applied to per-slot because callers like vllm-project/vllm#64461 replace a2 with one gathered across TP ranks. The config's pad describes one rank's slice, not the gathered width. The only cost is that the staged stage 1 doesn't get the one-tile saving.
| if K_VALID is None: | ||
| K_VALID = INTER_MAX | ||
| assert K_VALID % 32 == 0 and 0 < K_VALID <= INTER_MAX, K_VALID | ||
| K_TILES_MAX = (K_VALID + BK - 1) // BK |
There was a problem hiding this comment.
At inter 640, BK is 128, so this stays 5 tiles, as before. Please split the 3 to 8% gain between stage 1 and stage 2.
There was a problem hiding this comment.
Most of it is stage 1: it launches 9 instead of 10 N tiles, saving 7–8% of its time. Stage 2 gains about 5% on the atomic rows (64–1024 tokens) and nothing on the reduce rows, since, as you say, it stays at 5 K tiles.
|
@anhminhnguyenhoang @cpersson-amd Thanks for the ping. This is from reading the diff at 7625b76; I have not rerun it. The only consumer of
On the two findings:
One request on the fix. vllm-project/vllm#64461 widens |
The FlyDSL wrappers moved to aiter/moev2/segments/flydsl.py on main; port stage2_skips_inter_pad and inter_dim_pad / D_INTER_VALID there.
fused_moe_run_stage2 with a replacing a2 dropped the last inter_dim_pad columns of it: the padding of the config describes stage 1's a2, not the replacement. Run stage 2 over all of a replacing a2. The per-slot path and a replaced a2 read stage 1's padding columns, so the staged API's stage 1 keeps writing them; only fused_moe skips them. Tests: split-intermediate gather with a padded rank (per-slot and summed), and the stage-1 flag on both paths.
|
@anhminhnguyenhoang @fanxingran thanks for the reviews! Both findings are fixed in ec26b80. I've answered the inline threads separately. @fanxingran, on your request: a replaced a2 now drops the pad, so stage 2 reads all of it, and no width from get_2stage_cfgs reaches the per-slot path. For the per-slot finding, the staged API's stage 1 now keeps writing the padding, so per-slot reads of full K see zeros as on main. vllm-project/vllm#64461 (pad 0, per_slot=True) behaves as before. test_split_intermediate_gather now also covers a padded rank, per-slot and summed. |
Summary
The FlyDSL v2 layout GEMM2 (
flydsl_moe2_layout_*) now skips the zero padding ofinter_diminstead of multiplying through it, and stage 1 stops writing the padding tile when it feeds such a stage 2. On the DeepSeek-V4.1 TP4 a8w4 rows (inter 576 padded to 640)fused_moeis 5.5–7.5% faster from 64 tokens up; most of it is stage 1. It also removes the reason vLLM forcesintermediate_pad=0for DSv4.1: stage 2 no longer reads the padding at all, so unwritten padding cannot produce NaN.Motivation
fused_moenever passedintermediate_padto the v2 stage 2, so it ran every K tile up to the padded width: 11% wasted K at 576/640. Stage 1 also spent a tile column on padding. vLLM currently declares no padding for DSv4.1 and other bias-less W4A16 models because an unwritten stage-1 padding tile could reach this GEMM2 as NaN (#6598).Changes
mxfp4_moe_gemm2(..., D_INTER_VALID=None)→gemm2_body_v2(K_VALID). K tiles pastK_VALIDare not compiled. In the last partial tile, lanes whose 32-wide K block is padding:Their products are exactly 0 whatever the padding holds, including NaN.
K_VALIDis part of the kernel tag and cache key; the default (None) is byte-identical to before._flydsl_v2_stage2_wrapper(inter_dim_pad=0)passesD_INTER_VALID = inter_dim - inter_dim_pad // 32 * 32.Stage 1
stage2_skips_inter_pad(v2 output layout only): launches just enough tiles for the unpadded width rounded up to 32, which is exactly what the stage 2 above reads.fused_moe.get_2stage_cfgssets it only together with the stage-2 padding. Direct callers (the tuner, FHMoE) keep the old behavior.Staged API (
fused_moe_prepare_stage2/fused_moe_run_stage2):a2read it;a2is read in full: the config's padding describes stage 1'sa2, not the replacement (e.g. ana2all-gathered across TP ranks).Without
per_slotor a replaceda2, its stage 2 skips the padding likefused_moe.The wrappers moved to
aiter/moev2/segments/flydsl.pyon main; the wrapper changes are there.Performance
MI355X,
fused_moeat the DSv4.1 TP4 shape (E=384, top-6, model_dim 5120, inter 576 padded to 640) through the shippeddsv41_fp8_tuned_fmoe.csvrows (fp8 activations, MXFP4 weights). Random weights, uniform routing, L2/MALL flushed.intermediate_pad0 (today) vs 64. Total: median of 50 iterations, two alternating rounds averaged. Stage split: device time per kernel from the profiler (20 iterations).Per row:
flydsl_moe2_afp8_wfp4_*), which this PR does not change. At 1 and 16 tokens the call is launch-bound.dsv41_fp4_tuned_fmoe.csv,_mxfp4_a4w4_stage1_fw/_mxfp4_a4w4_stage2_fw) do not take this path. They are 9–16% slower at pad 64 than at pad 0 (64–512 tokens), identically on main and with this PR.End-to-end (measured before the latest main merge): DeepSeek-V4.1-Flash served by vLLM on MI355X (TP4, FP8 KV cache, dspark speculative decoding). Random data, mean of 2 runs per point.
Testing
op_tests/flydsl_tests/test_flydsl_moe.py:test_flydsl_v2_stage2_a8w4_skips_inter_pad: NaN values and NaN scales in the padding. The output must equal the full-K kernel over zero padding exactly. Covers BK 128/256, partial, half-empty and fully skipped last tiles, BM 16/32, atomic and reduce.test_flydsl_e2e_a8w4_stage2_skips_inter_pad: stage 1 into a NaN-filled buffer with the flag set, then the v2 stage 2 through_flydsl_v2_stage2_wrapper. Checks that the skipped columns are untouched and that the output matches the reference._generate_a8w4_gui_datatakesinter_padandactivationfor it.op_tests/test_moe_stage_api.py:test_split_intermediate_gathergets a padded rank 0 (pad 0 and 64) and checks the summed output as well as the per-slot one.test_staged_stage1_writes_the_padding: stage 1 getsstage2_skips_inter_padinfused_moeand not in the staged API.On the merged branch (ec26b80):
test_moe_stage_api.py20/20 andtest_flydsl_moe.py95/95 pass.Mutation testing kills all eight mutants:
a2(summed gather at pad 64 fails, rel. error 0.26), and the staged stage 1 skipping the padding.Production rows (T = 1 to 16384, weights and routing captured from vLLM), with the caching allocator filled with 0xFF bytes (NaN as fp8 and e8m0) before every call: no NaN at pad 0 or 64. Reduce rows are bit-exact against pad 0. Atomic rows, including the v1 stage-2 rows, differ from pad 0 as much as pad 0 differs from a second pad-0 run (rel-L2 ≤ 4e-3).
End-to-end accuracy: DeepSeek-V4.1-Flash on MI355X, vLLM TP4, FP8 KV cache, dspark speculative decoding. Base runs vLLM declaring no padding (today). This PR runs vLLM forwarding
intermediate_pad=64: the DSv4.1 override and the bias-less W4A16 workaround for #6598 are removed. gsm8k ran three times, alternating the arms' GPU sets. The staged-API fixes since then do not changefused_moe's output, so these were not rerun.The gsm8k spread is run-to-run noise: base alone ranges over a point, and run 3 goes the other way. Over the three runs, 94 questions lean to base and 70 to this PR (sign test p = 0.07). gsm8k decodes greedily, and the draft acceptance on it is the same in both arms (46.7%).
PR Self-Review
Reviewed revision
ec26b80
Behavior and intent
When
fused_moeruns a v2 layout stage 2 (flydsl_moe2_layout_*) withintermediate_pad > 0, it now passes the padding through. GEMM2 stops at the unpadded K, and stage 1 does not compute or write the padding columns. Before, the pad was never passed to the v2 stage 2, so it multiplied through the padded K (11% of K for DSv4.1 at 576/640) and read whatever stage 1 left in the padding. An unwritten padding tile could therefore turn into NaN (#6598). That is why vLLM declares no padding for DSv4.1 and the bias-less W4A16 models. The staged API keeps stage 1 writing the padding and reads a replacinga2in full, so its per-slot and replaced-operand paths are unchanged. Withintermediate_pad == 0, or with other stage-2 kernels, nothing changes: the new arguments default to the old behavior, and the default kernel is byte-identical.Validation and unverified scope
All on MI355X (gfx950), on the merged branch unless noted:
pytest op_tests/test_moe_stage_api.py op_tests/flydsl_tests/test_flydsl_moe.py: 115/115 pass.fused_moekernel timings with the stage split: see Performance.Unverified:
PR-specific question
Before this PR, the kernel tests fail. Neither
mxfp4_moe_gemm2(D_INTER_VALID=...)nor stage 1'sstage2_skips_inter_padexists on main, and_flydsl_v2_stage2_wrapperignoresinter_dim_pad. More to the point, the behavior they check is absent. A full-K GEMM2 multiplies the NaN fp8 values and NaN e8m0 scales planted in the padding, so the output is NaN. Stage 1 also overwrites the padding columns that the end-to-end test requires to stay untouched. The padded gather test passes on main, where nothing skips the padding, and fails on this PR without thea2fix; the flag test checks this PR's flag. The mutants show that each part is needed for the tests to pass.No existing canonical test covers the skip.
test_flydsl_v2_stage2_a8w4_full_tileandtest_flydsl_v2_stage2_scatter_ldsparun the v2 GEMM2 only at full K, without a padding.test_flydsl_stage2_a8w4_guiandtest_flydsl_e2e_a8w4_guipass the padding only to the v1flydsl_moe_stage2, which this PR does not change. None of them put non-zero data in the padding, and zero padding gives the same result whether or not it is skipped.Changed op tests
op_tests/flydsl_tests/test_flydsl_moe.py:test_flydsl_v2_stage2_a8w4_skips_inter_padcovers the GEMM2 skip on its own. It plants NaN values and scales in the padding and requires exactly the same output as the full-K kernel over zero padding. Parametrized over BK 128/256, last tiles that are partial, half empty or fully skipped, BM 16/32, and atomic and reduce epilogues. It is the only test that exercises the partial-tile lane masking and unit scales.test_flydsl_e2e_a8w4_stage2_skips_inter_padcovers the handoff between the stages: stage 1 with the flag writes only the columns that stage 2 reads, and_flydsl_v2_stage2_wrapperturnsinter_dim_padinto the right K bound (pads 32/64/96, stage-1 tiles 64 and 128 wide). Its result is checked against the fp32 reference. The GEMM2 test above calls the kernel directly, so it would not catch a wrong bound computed in the wrapper or a stage-1 tile count that doesn't match it._generate_a8w4_gui_datatakes keyword-onlyinter_padandactivationfor the end-to-end test. Their defaults reproduce the old data, so existing callers are unchanged.op_tests/test_moe_stage_api.py:test_split_intermediate_gather(pad 0/64): rank 0 is padded, and the gathereda2replaces its own. The summed check catches a stage 2 that applies rank 0's padding to the widera2; the per-slot path ignores the padding.test_staged_stage1_writes_the_paddingchecks the flag rather than the memory. Whether a skip leaves columns unwritten depends on the stage-1 tile: at inter 512, pad 64 and tile_n 128, both grids cover all columns.Documentation
Dependencies
Stands alone on main.
get_2stage_cfgs(it drops the pad); whichever lands second reconciles the two.Breaking Changes
None. New arguments default to the previous behavior.
AI assistance
Developed with AI assistance (Claude). I reviewed every change and ran the tests and benchmarks on MI355X.