Skip to content

[FlyDSL] MoE v2 stage 2: skip the inter_dim padding - #7917

Open
cpersson-amd wants to merge 7 commits into
ROCm:mainfrom
cpersson-amd:moe-v2-kpad
Open

cpersson-amd wants to merge 7 commits into
ROCm:mainfrom
cpersson-amd:moe-v2-kpad

Conversation

@cpersson-amd

@cpersson-amd cpersson-amd commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The FlyDSL v2 layout GEMM2 (flydsl_moe2_layout_*) now skips the zero padding of inter_dim instead 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_moe is 5.5–7.5% faster from 64 tokens up; most of it is stage 1. It also removes the reason vLLM forces intermediate_pad=0 for DSv4.1: stage 2 no longer reads the padding at all, so unwritten padding cannot produce NaN.

Motivation

fused_moe never passed intermediate_pad to 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 past K_VALID are not compiled. In the last partial tile, lanes whose 32-wide K block is padding:

    • load A as zero (an out-of-bounds offset in the A→LDS DMA);
    • use unit e8m0 scales for A and B;
    • re-read the last valid block's weight bytes, so there is no extra traffic.

    Their products are exactly 0 whatever the padding holds, including NaN. K_VALID is part of the kernel tag and cache key; the default (None) is byte-identical to before.

  • _flydsl_v2_stage2_wrapper(inter_dim_pad=0) passes D_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_cfgs sets 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):

    • its stage 1 keeps writing the padding, because the per-slot path and a replaced a2 read it;
    • a replacing a2 is read in full: the config's padding describes stage 1's a2, not the replacement (e.g. an a2 all-gathered across TP ranks).

    Without per_slot or a replaced a2, its stage 2 skips the padding like fused_moe.

The wrappers moved to aiter/moev2/segments/flydsl.py on main; the wrapper changes are there.

Performance

MI355X, fused_moe at the DSv4.1 TP4 shape (E=384, top-6, model_dim 5120, inter 576 padded to 640) through the shipped dsv41_fp8_tuned_fmoe.csv rows (fp8 activations, MXFP4 weights). Random weights, uniform routing, L2/MALL flushed. intermediate_pad 0 (today) vs 64. Total: median of 50 iterations, two alternating rounds averaged. Stage split: device time per kernel from the profiler (20 iterations).

Tokens Total Stage 1 Stage 2
64–1024 (atomic stage 2) −6.9 to −7.5% −15 to −23 µs about −7 µs
3000–16384 (reduce stage 2) −5.5 to −6.1% −19 to −74 µs about 0
2048 (v1 stage 2) unchanged
  • Stage 1 gives most of the gain: it launches 9 instead of 10 N tiles (gate/up interleaved, tile_n 128) and saves 7–8% of its time.
  • Stage 2 is 5% faster on the atomic rows and unchanged on the reduce rows. With BK 128, 576 columns still take 5 K tiles, the same as 640, so stage 2 saves only the padding half of the last tile.

Per row:

Tokens pad 0 (µs) pad 64 (µs) Change Stage 1 (µs) Stage 2 (µs)
1 143.7 143.3 noise -0.2 +0.1
16 146.7 146.8 noise -3.7 -2.3
64 284.3 263.0 -7.5% -14.6 -6.5
256 417.0 388.4 -6.9% -20.8 -7.7
512 426.3 396.9 -6.9% -21.9 -6.6
1024 442.0 410.3 -7.2% -23.4 -7.0
2048 521.4 521.7 v1 stage 2 +0.3 +8.0
3000 560.9 530.3 -5.5% -19.1 -4.6
4096 714.5 671.0 -6.1% -29.7 -2.8
8192 1022.3 960.9 -6.0% -29.4 +1.5
16384 1736.3 1641.0 -5.5% -73.8 +3.1
  • The 2048 row uses the v1 stage 2 (flydsl_moe2_afp8_wfp4_*), which this PR does not change. At 1 and 16 tokens the call is launch-bound.
  • The opt-in a4w4 rows (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.

Workload Metric Base This PR Change
8k in / 1 out, conc 64 mean TTFT (ms) 4557 4480 -1.7%
60k in / 1 out, conc 16 mean TTFT (ms) 8201 8073 -1.6%
8k in / 1k out, conc 16 median TPOT (ms) 8.02 8.05 noise
8k in / 1k out, conc 64 median TPOT (ms) 15.50 15.67 noise
8k in / 1k out, conc 256 median TPOT (ms) 37.06 36.83 noise

Testing

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X

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_data takes inter_pad and activation for it.

op_tests/test_moe_stage_api.py:

  • test_split_intermediate_gather gets 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 gets stage2_skips_inter_pad in fused_moe and not in the staged API.

On the merged branch (ec26b80): test_moe_stage_api.py 20/20 and test_flydsl_moe.py 95/95 pass.

Mutation testing kills all eight mutants:

  • six for the kernels: no unit scale, no A zero-fill, off-by-one A bound, wrong scale half, stage 2 not given the padding, stage 1 flag dropped;
  • two for the staged API: the pad kept for a replaced 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 change fused_moe's output, so these were not rerun.

Task Base This PR
gsm8k, 5-shot, all 1319 (runs 1 / 2 / 3) 92.95 / 92.80 / 91.96 91.28 / 92.04 / 92.12
niah_single_2, 32k / 64k 1.00 / 1.00 1.00 / 1.00

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_moe runs a v2 layout stage 2 (flydsl_moe2_layout_*) with intermediate_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 replacing a2 in full, so its per-slot and replaced-operand paths are unchanged. With intermediate_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.
  • Mutation testing: all eight mutants are killed (six kernel, two staged API; see Testing).
  • fused_moe kernel timings with the stage split: see Performance.
  • Before the merge: the 0xFF-allocator NaN check on the production rows, end-to-end vLLM gsm8k (full 1319, three runs) and niah_single_2 32k/64k.

Unverified:

  • MI250X and MI300X were not run; the new tests are gfx950-only.
  • Only the DSv4.1 TP4 shape was benchmarked. Other models whose tuned rows pair a v2 stage 2 with a padding were not run.
  • Direct callers of the kernels (the tuner, FHMoE) keep the old full-K behavior and were not exercised with the new arguments.
  • The staged API with a pad and an xcd-swizzled stage 1 still leaves the last N block unwritten until [FlyDSL] Count tile2_pad in the stage-1 XCD remap grid #8512 lands, as on main.
  • The end-to-end gain also needs vLLM to forward the padding again; that change is separate from this PR.

PR-specific question

Before this PR, the kernel tests fail. Neither mxfp4_moe_gemm2(D_INTER_VALID=...) nor stage 1's stage2_skips_inter_pad exists on main, and _flydsl_v2_stage2_wrapper ignores inter_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 the a2 fix; 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_tile and test_flydsl_v2_stage2_scatter_ldspa run the v2 GEMM2 only at full K, without a padding. test_flydsl_stage2_a8w4_gui and test_flydsl_e2e_a8w4_gui pass the padding only to the v1 flydsl_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_pad covers 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_pad covers the handoff between the stages: stage 1 with the flag writes only the columns that stage 2 reads, and _flydsl_v2_stage2_wrapper turns inter_dim_pad into 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_data takes keyword-only inter_pad and activation for 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 gathered a2 replaces its own. The summed check catches a stage 2 that applies rank 0's padding to the wider a2; the per-slot path ignores the padding.
  • test_staged_stage1_writes_the_padding checks 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

  • Docstrings updated

Dependencies

  • No new third-party dependencies added

Stands alone on main.

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.

@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 7917 --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.

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]>
@cpersson-amd
cpersson-amd marked this pull request as ready for review November 18, 2026 14:59
@cpersson-amd
cpersson-amd requested a review from a team November 18, 2026 14:59
@aiter-bot

aiter-bot commented Nov 18, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision ec26b805bae2 · 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 behavior would each changed op test fail on before this PR, and why is that coverage not already provided by an existing canonical test?
  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 — Reviewed revision must contain at least the first 7 characters of the current head SHA
  • FAIL — changed op test not explained in Changed op tests: op_tests/test_moe_stage_api.py

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

@peymanr

peymanr commented Nov 23, 2026

Copy link
Copy Markdown

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:

  • 5120 / 640 (576 real + 64 pad), 384 experts, top-6, random MXFP4 weights and routing.
  • intermediate_pad=64.
  • Stage 1 forced to flydsl_moe1_afp8_wfp4_bf16_t64x128x256_w4_bnt0_gui_xcd4_fp8, with the v2 layout stage 2.
  • Every buffer allocated inside fused_moe filled with 0xFF before the call, so unwritten storage shows up as NaN.

Results.

  • Unpatched, every call (32 of 32) returns non-finite rows.
  • With this PR, the output is finite: 0 of 32 calls fail.
  • With this PR, stage-1 columns 576-639 are still unwritten on 32 of 32 calls.

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:

  • compile_mixed_moe_gemm1_common launches enough N blocks to cover tile2_pad.
  • The XCD remap recounts grid_x without tile2_pad.
  • At inter 640, pad 64, the launch has 10 N blocks but the remap spreads only 9, so the last N block is never computed.

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:

  • 0 of 32 calls fail.
  • No call leaves columns unwritten.
  • The xcd4 output is bit-identical to the non-swizzled kernel's.

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:

  • intermediate_pad=0: we did not reproduce a failure there, and we don't know which pad the reporter used.
  • GPUs other than MI355X (gfx950).
  • The reporter's ROCm 7.2.4 / torch 2.11 stack; we only used ROCm 10 / torch 2.13 images.
  • How often NaN appears under the normal allocator without the 0xFF fill.
  • End-to-end vLLM/SGLang serving, real DeepSeek-V4.1 weights, and multi-GPU tensor parallelism.
  • The gate-only and separate gate/up stage-1 paths, tile_k other than 256, atomic stage 2 and split-K.
  • Kernel timing with the fix.

@anhminhnguyenhoang anhminhnguyenhoang 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.

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/w2 override of a new width in fused_moe_run_stage2 keeps 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 apply fails in aiter/fused_moe.py at 4286 and in aiter/ops/flydsl/kernels/mxmoe_dispatcher.py at 808, and the body's 95/95 result names 08f6dc3. Rebase and re-run op_tests/flydsl_tests/test_flydsl_moe.py on the rebased head.

  • Medium [verified]: get_2stage_cfgs changes every model that pairs a flydsl_moe2_layout_* kernel with intermediate_pad > 0, but the downstream vLLM, SGLang and ATOM jobs were skipped. I suggest ci:all and 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.

Comment thread aiter/fused_moe.py
model_dim=model_dim,
inter_dim=inter_dim,
num_experts=expert,
inter_dim_pad=intermediate_pad,

@anhminhnguyenhoang anhminhnguyenhoang Dec 1, 2026 •

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.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread aiter/fused_moe.py
activation=activation,
inter_dim_pad=intermediate_pad,
model_dim_pad=hidden_pad,
stage2_skips_inter_pad=is_flydsl2_layout,

@anhminhnguyenhoang anhminhnguyenhoang Dec 1, 2026 •

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.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@anhminhnguyenhoang anhminhnguyenhoang Dec 1, 2026 •

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.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@fanxingran

Copy link
Copy Markdown
Contributor

@anhminhnguyenhoang @cpersson-amd Thanks for the ping. This is from reading the diff at 7625b76; I have not rerun it.

The only consumer of fused_moe_prepare_stage2 / fused_moe_run_stage2 I know of is vllm-project/vllm#64461 (Muse Spark, still open). This PR does not change it:

  • It never passes a padding. fused_moe_prepare_stage2 is called with the default intermediate_pad=0, and the staged path is turned off for any layer whose hidden or intermediate size is padded. So stage 1 never gets stage2_skips_inter_pad.
  • Every fused_moe_run_stage2 call there uses per_slot=True, and that branch returns before D_INTER_VALID is computed.

On the two findings:

  1. The stale pad is a real bug for an override without per_slot: the partial keeps inter_dim_pad while the override branch rescales only inter_dim. 256 - 64 = 192 valid columns matches the 0.499 you measured.
  2. The per-slot path does read the padding stage 1 skipped. Through the public API it is fp4 only, so I agree it is correct today and worth closing.

One request on the fix. vllm-project/vllm#64461 widens a2 with per_slot=True (it all-gathers I/tp to I across TP ranks). A valid width computed once in get_2stage_cfgs and then forwarded to the per-slot path would cut that call at I/tp, even with pad 0. Please derive it from the replaced a2 in the override branch, or drop the pad there. op_tests/test_moe_stage_api.py::test_split_intermediate_gather covers that call.

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.
@cpersson-amd

Copy link
Copy Markdown
Contributor Author

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

This branch has not been deployed

No deployments
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.

6 participants