Skip to content

fix(moe): mask cudagraph pad rows under pure TP again - #2893

Merged
JiaoliangYu merged 2 commits into
mainfrom
gbyu/k3-tp-mask-pad-rows
Nov 3, 2026
Merged

JiaoliangYu merged 2 commits into
mainfrom
gbyu/k3-tp-mask-pad-rows

Conversation

@gbyu-amd

@gbyu-amd gbyu-amd commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

#2776 limited pad-row masking to EP (mask_pad_rows=layer.use_ep) because "-1 route faults the TP a16w4 MoE". That fault was real: the MXFP4 BM16 inline-sort (K3 decode M <= 64) indexed its LDS histogram with the -1 id. ROCm/aiter#7020 (merged 2026-10-28) fixed it, so the sort now skips ids outside [0, E). This PR requires aiter at or past #7020; with older aiter, -1 pad rows on the TP path can fault intermittently. All results below use aiter bd4ff1c70, which contains #7020.

The TP regression from #2725 (K3 nightly 10/26 → 10/27, c128 TPOT ~96.6 → 99.8 ms) had a different cause: passing num_local_tokens together with a padded cudagraph M.

  • The opus moe_sorting pad sentinel is built from the real token count.
  • The a16w4 gemm2 (moe_2stage_a16wmix/gemm2.py, mxfp4_gemm2.py) bounds rows with token_id < M, where M is the padded count.
  • So every pad slot atomically added into a single row: gemm2_a16w4 went 5.45 → 24.9 ms/step at bs100.

Routing pad rows to -1 is safe because the sort skips them. This PR restores masking under TP and removes the mask_pad_rows switch.

Results (Kimi-K3, TP8, MI355X, image nightly_202610310756 / aiter bd4ff1c70)

main this PR
bs100 decode device time / step 44.56 ms 42.86 ms (−3.8%)
c128 Mean TPOT 102.46 ms 102.41 ms
GSM8K — 0.9666
DSpark GSM8K / MTP acceptance 0.9682 / 51.52% 0.9651 / 51.57%
  • The bs100 gain comes from topk (−1.31 ms) and gemm1/gemm2 (−0.79 ms), partly offset by sorting (+0.39 ms).
  • c128 is flat because a full batch has almost no pad rows. The gain shows up when the batch is padded up to a cudagraph bucket.
  • No faults in any run.

Follow-up for aiter (ROCm/aiter#7294): EP still passes num_local_tokens with a padded M; that PR makes the a16wmix and mxfp4 atomic gemm2 honour the valid token count (use_valid_token_count).

🤖 Generated with Claude Code

#2776 gated pad-row masking on EP (`mask_pad_rows=layer.use_ep`) after a
suspected "-1 route faults the TP a16w4 MoE". The fault does not reproduce:
the TP regression from #2725 was the sort's pad sentinel (real token count)
sitting inside the a16w4 gemm2's `token_id < M` bound (padded M), which piled
every pad slot into one row. Routing pad rows to -1 is skipped by the sort, so
restore it for TP and drop the switch.

Kimi-K3 TP8, aiter bd4ff1c70: bs100 decode device time 44.56 -> 42.86 ms/step
(-3.8%), c128 TPOT unchanged (102.46 -> 102.41 ms, full batches have no pad
rows), GSM8K 0.9666, DSpark GSM8K 0.9651 / acceptance 51.57%, no fault.

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

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every eligible PR before approval:

  • ✅ Pre Checkin: Black, Ruff, catalog schema validation, non-GPU unit tests

Heavy model tests:

  • ✅ Run after the PR is approved and Pre Checkin passes
  • ✅ Run immediately when an approval review is submitted
  • ✅ Can be requested before approval with labels
Label Tests
ci:full Run all heavy PR model tests: native ATOM, vLLM, and SGLang
ci:atom Run native ATOM model accuracy tests
ci:vllm Run ATOM vLLM OOT model accuracy tests
ci:sglang Run ATOM SGLang model accuracy tests

Heavy jobs are skipped when the PR is not approved and no matching ci:* label is present.
Add labels via the sidebar or gh pr edit 2893 --add-label <label>

@gbyu-amd
gbyu-amd marked this pull request as ready for review November 2, 2026 14:32

@valar-cop valar-cop left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

some changes needed..

smoke run on one non-Kimi-K3 TP MoE model? deepseek or qwen3-moe

ci green.

Comment thread atom/model_ops/moe.py Resolved
Comment thread atom/model_ops/moe.py Resolved
Comment thread atom/model_ops/moe.py Resolved
@JiaoliangYu
JiaoliangYu merged commit 7336e1d into main Nov 3, 2026
@JiaoliangYu
JiaoliangYu deleted the gbyu/k3-tp-mask-pad-rows branch November 3, 2026 03:06
charlieguo1106 added a commit that referenced this pull request Nov 11, 2026
…imi-K3

main (#2893) dropped FusedMoE.select_experts' mask_pad_rows, so the HERD
branch raised TypeError on its first decode step. Without it the Top-(K+1)
candidates route pad rows to -1 like _select_logical, and apply() passes the
same pass row count to fused_moe as num_local_tokens (#2905), which aiter's
herd_route_psum takes as num_valid_rows: the pad rows of a partly filled
graph bucket no longer vote in HERD's popularity.

The experts must run their pass's own rows. A DP-attention or PCP gather,
and simulated DP's repeat, interleave every rank's -1 pad rows, pass no count
and can exceed the 1024 rows fused_moe(herd_topk) takes, so use_flydsl_herd
stays off there.

HERD now serves DeepSeek-V4's sqrtsoftplus top-k and Kimi-K3's sigmoid
grouped top-k over one group only; both fold the routed scale in, which the
drop applies.

aiter's HERD constants import under a guard, and use_flydsl_herd needs
aiter v0.1.29 (ROCm/aiter#7494, probed by herd_route_psum): older aiter
aborts on Kimi-K3's Top-17 candidates with an FP32 bias.

test_flydsl_herd_routing binds the HERD candidate call to the real
FusedMoE.select_experts signature, and covers the routing set, the gather
guard, the aiter probe and the num_local_tokens hand-off.
test_flydsl_herd_graph_padding_gpu replays a bs=32 graph at 17, 1, 31 and 32
real rows against eager HERD over the real rows alone (gfx1250 only).
charlieguo1106 added a commit that referenced this pull request Nov 16, 2026
…2713)

* feat(moe): route FlyDSL MXFP4 decode through aiter HERD on gfx1250

With AITER_FLYDSL_USE_HERD=1, Mxfp4MoEMethod.apply() on gfx1250 picks the
Top-(K+1) sqrtsoftplus candidates with topk_gating and passes them to
fused_moe(herd_topk=K), which drops each token's least batch-popular
candidate, renormalizes and applies the routed scale (fused into the
route/prefix-sum kernel by default, AITER_HERD_FUSED_DROP=1).

Only decode steps whose batch is inside aiter's HERD window (16..128 tokens)
take this path, and only for TP (no EP or EPLB), GU-interleaved weights,
sqrtsoftplus routing with renormalization, no grouped top-k, no custom or
hash routing, no fused shared experts and no expert mask. Everything else
keeps select_experts.

* style(moe): sort the HERD import and use functools.cache

Fixes ruff I001 (aiter.ops.topk sorts after aiter.ops.shuffle) and UP033
(@cache instead of @lru_cache(maxsize=None)) on the FlyDSL HERD change.

* [MoE] Adapt FlyDSL HERD to aiter valid-row routing

* [MoE] Route FlyDSL HERD candidates through the layer's own top-k

The HERD branch of Mxfp4MoEMethod.select_experts forced a non-grouped
top-k, and _flydsl_herd_step admitted sqrtsoftplus only, so DeepSeek-V4
was the only model HERD could serve. Candidates now take the layer's own
routing at top_k + 1: topk_gating for sqrtsoftplus, fused_topk for
softmax (GPT-OSS) and biased_grouped_topk for a one-group grouped top-k
(Kimi-K3). Grouped top-k with more than one group keeps plain routes.

The drop applies the routed scale the layer's own routing folds in.
FusedMoE.routing_scale holds that rule and select_experts shares it:
grouped top-k and sqrtsoftplus fold the scale in, sigmoid only with a
fused shared column, softmax never.

Also sets use_flydsl_herd on the Mxfp4MoEMethod objects that
test_shared_expert_dispatch and test_mxfp4_triton_moe_decode build by
hand; the HERD hooks broke three of those tests.

* [MoE] Trim the FlyDSL HERD routing hooks

Cuts the HERD additions to atom/model_ops/moe.py from 134 to 71 lines
without changing behaviour:

- Drop the one-shot HERD batch log and FusedMoE.routing_scale; apply()
  takes the routed scale select_experts folds in (grouped top-k and
  sqrtsoftplus) inline, and select_experts is back to main.
- The HERD branch of select_experts leaves default arguments out.
- _flydsl_herd_step folds its checks into one condition and drops the
  expert_mask test, which use_flydsl_herd's not-EP check already covers.
- Shorter comments.

* [MoE] Route FlyDSL HERD pad rows like the plain routing

Since #2776, pure TP routes pad rows like real rows (mask_pad_rows=
layer.use_ep) and passes fused_moe no scheduled_tokens_gpu. The HERD
branch of select_experts kept the default mask, so its Top-(K+1)
candidates still carried -1 pad rows while fused_moe got no
num_local_tokens. aiter's HERD drop leaves -1 rows out only with that
count, so pure-TP HERD decode broke.

The HERD branch now takes the same mask_pad_rows as _select_logical:
pure TP routes its pad rows as real candidates, and a later change to
that policy applies to HERD as well.

* [MoE] Keep graph pad rows out of FlyDSL HERD; serve DeepSeek-V4 and Kimi-K3

main (#2893) dropped FusedMoE.select_experts' mask_pad_rows, so the HERD
branch raised TypeError on its first decode step. Without it the Top-(K+1)
candidates route pad rows to -1 like _select_logical, and apply() passes the
same pass row count to fused_moe as num_local_tokens (#2905), which aiter's
herd_route_psum takes as num_valid_rows: the pad rows of a partly filled
graph bucket no longer vote in HERD's popularity.

The experts must run their pass's own rows. A DP-attention or PCP gather,
and simulated DP's repeat, interleave every rank's -1 pad rows, pass no count
and can exceed the 1024 rows fused_moe(herd_topk) takes, so use_flydsl_herd
stays off there.

HERD now serves DeepSeek-V4's sqrtsoftplus top-k and Kimi-K3's sigmoid
grouped top-k over one group only; both fold the routed scale in, which the
drop applies.

aiter's HERD constants import under a guard, and use_flydsl_herd needs
aiter v0.1.29 (ROCm/aiter#7494, probed by herd_route_psum): older aiter
aborts on Kimi-K3's Top-17 candidates with an FP32 bias.

test_flydsl_herd_routing binds the HERD candidate call to the real
FusedMoE.select_experts signature, and covers the routing set, the gather
guard, the aiter probe and the num_local_tokens hand-off.
test_flydsl_herd_graph_padding_gpu replays a bs=32 graph at 17, 1, 31 and 32
real rows against eager HERD over the real rows alone (gfx1250 only).

* [MoE] Decide FlyDSL HERD in apply() as select_experts did, not by width

apply() told HERD's raw Top-(K+1) candidates from final routes by the ids'
width, which also had to rule out a fused shared column. It now asks the same
_flydsl_herd_step select_experts asked, over the same rows: HERD never runs on
gathered rows, so their count is the one routing saw. Nothing per step is kept
on the method.

No try around aiter: the HERD constants import directly, and the v0.1.29
check looks herd_route_psum up on aiter.ops.flydsl.herd_topk, which aiter has
had since those constants (v0.1.26). test_dp_gather_routed_rows stubs
aiter.ops.topk with the other aiter modules moe.py imports.
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.

3 participants