Skip to content

[Bugfix][fused_moe] Do not use the inline-sort MXFP4 path when inter_dim % 256 != 0 - #6184

Closed
Fangzhou-Ai wants to merge 1 commit into
ROCm:mainfrom
Fangzhou-Ai:fix/mxfp4-inline-sort-inter-dim-256-align
Closed

Fangzhou-Ai wants to merge 1 commit into
ROCm:mainfrom
Fangzhou-Ai:fix/mxfp4-inline-sort-inter-dim-256-align

Conversation

@Fangzhou-Ai

@Fangzhou-Ai Fangzhou-Ai commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

#5232 kept CK-Tile stage2 away from non-256-aligned MXFP4 shapes, and #5233 records why. The inline-sort two-stage path still accepts them, and on that path the same misalignment is a hard memory fault rather than silently wrong results.

The intermediate scales are e8m0_shuffle'd in groups of 8 columns, so stage1 writes and stage2 reads them on a stride that rounds scale-N up to a multiple of 8 — kas_c_k1_for in mxfp4_gemm_common.py carries that comment explicitly:

def kas_c_k1_for(k):
    # A scales are e8m0_shuffle'd in groups of 8 columns, so the stride rounds
    # up: k=384 has 12 valid scale columns padded to 16, not truncated to 8.
    return ((k // 32) + 7) // 8

The buffers sized on the exact inter_dim are short of that stride whenever inter_dim % 256 != 0. On top of that, is_mxfp4_moe_shape_supported pads inter_dim up to 256 before its allowlist lookup:

padded_inter = ((int(inter_dim) + 255) // 256) * 256
return (expert, model_dim, padded_inter, topk) in MXFP4_MOE_SUPPORTED_SHAPES

MXFP4_MOE_SUPPORTED_SHAPES has no 1152 entry, so a 1152-wide call reports supported by matching (384, 5120, 1280, 6) and then runs the aux instance code-generated for 1280.

Neither existing gate catches it. _mxfp4_inline_sort_unsupported rejects on padding (hidden_pad or intermediate_pad) but never on alignment, and _mxfp4_moe_sort_internal_is_supported(NE, TOPK, D_HIDDEN, MB, zero_init) does not take inter_dim at all.

How it shows up

DeepSeek-V4.1-Flash shards moe_intermediate_size 2304 to 1152 at TP2. Every TP2 server dies during CUDA graph capture:

Warning: Queue error - HSA_STATUS_ERROR_MEMORY_FAULT
Memory access fault by GPU node-4 (Agent handle: 0x31a3dcc0) on address 0x7f84c9d33000. Reason: Unknown.
[aiter] Error in moe_sorting: CUDA error: an illegal memory access was encountered

With AMD_SERIALIZE_KERNEL=3 the fault lands immediately after the inline-sort pair is selected at inter_dim=1152:

[aiter] [fused_moe] using 2stage (kernelName1='flydsl_mxmoe_g1_a4w4_16x128x256_f16in_hpf_nt_xcd4',
  kernelName2='flydsl_moe2_layout_afp4_wfp4_bf16_t16x128x128_atomic_nt_sbm16')
  for ('gfx950', 256, 16, 5120, 1152, 384, 6, <ActivationType.Silu: 0>, 'torch.bfloat16',
       'torch.float4_e2m1fn_x2', 'torch.float4_e2m1fn_x2', 'QuantType.per_1x32', True, False)

This is not a shape the tuner avoids: tuned_fmoe.csv ships 16 tuned rows at inter_dim=1152 for (expert=384, topk=6), so the shape is actively steered onto the faulting path. TP4 escapes only by accident — its caller pads 576 up to 640, and the resulting nonzero intermediate_pad already trips the padding check, so TP4 logs inline-sort config is unsupported (hidden/intermediate padding) 34440 times per boot where TP2 logs it zero times.

Technical Details

Reject the shape at both existing gates, matching how expert_mask is already handled in both places:

  • get_2stage_cfgs: a new branch beside the is_ep one, so the tuned inline-sort row is never selected. Because the lookup is cached, this also collapses the warning to once per distinct shape key (64 lines per boot instead of 25284).
  • _mxfp4_inline_sort_unsupported: the same condition, so an invocation that still reaches it re-resolves through _resolve_metadata(disable_inline_sort=True).

Nothing else changes. The heuristic fallback already runs these shapes on a4w4 FlyDSL kernels correctly — that is what TP4 does today — so no shape loses a4w4 and no weights grow. The alternative of making the caller pad inter_dim up to 256 costs +11 % MoE weights at TP2 and +33 % at TP4 and moves both rows off every tuned row, which is strictly worse.

Test Plan

Hardware: MI355X (gfx950, 256 CU), ROCm 10.0, torch 2.12.0+rocm10.0.0.

Unit, extending the suite #5232 added for exactly this shape class:

python -m pytest op_tests/test_moe_mxfp4_inter_dim_dispatch.py -q
python -m pytest op_tests/flydsl_tests/test_moe_mxfp4_inter_dim_align.py -q

One new parametrized case, test_tuned_inline_sort_config_honours_inter_dim_alignment, asserts a tuned inline-sort row is dropped at 384, 640 and 1152 and kept at 512 and 1280. The existing cfg_2stages/warning-capture boilerplate is factored into a _tuned_cfg context manager that test_unsafe_tuned_cktile_stage2_is_discarded now shares, so the file gets shorter while covering one more behaviour.

The invocation-time guard in _mxfp4_inline_sort_unsupported is covered end to end rather than by a unit test; faking the 18 arguments it takes was more brittle than it was worth. It is still needed because the heuristic path can select an inline-sort pair without going through a tuned row.

End to end: deepseek-ai/DeepSeek-V4.1-Flash at TP2 (inter_dim=1152), 1M context, DSpark num_speculative_tokens=5, --moe-backend aiter, MXFP4 indexer KV, VLLM_ROCM_USE_AITER_MOE_A4W4_DSV4=1, --max-cudagraph-capture-size 1024. Accuracy via lm_eval local-chat-completions with --apply_chat_template, all 1319 GSM8K questions, 5-shot, max_tokens=16384, temperature=0.

Test Result

op_tests/test_moe_mxfp4_inter_dim_dispatch.py: 30 passed. Reverting the fused_moe.py hunk fails exactly the three unaligned cases and leaves the two aligned controls passing.

op_tests/flydsl_tests/test_moe_mxfp4_inter_dim_align.py: 3 failed, identically with and without this patch — module_moe_asm does not JIT-build in my worktree, so those are environmental.

End to end at TP2, inter_dim=1152:

graph capture memory faults /health GSM8K flexible-extract strict-match
before dies at 78/83 4 never ready n/a n/a
after completes 0 200 0.9674 ± 0.0049 0.9484 ± 0.0061

The server logs 64 discarding tuned inline-sort config at inter_dim=1152, which is not a multiple of 256 lines and then runs the experts on the a4w4 FlyDSL kernels (flydsl_mxmoe_g1_a4w4_* + flydsl_moe2_layout_afp4_wfp4_bf16_*), confirming the fallback keeps a4w4 rather than dropping to a16w4.

Submission Checklist

AI assistance (Claude Opus 5 via Cursor) was used to root-cause the fault and draft this change. I reviewed every changed line and ran every command above myself on the MI355X node.

@Fangzhou-Ai
Fangzhou-Ai requested a review from a team October 6, 2026 19:09
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
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
multigpu 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: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
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 6184 --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.

@github-actions github-actions Bot changed the title [FlyDSL] [fused_moe] Do not use the inline-sort MXFP4 path when inter_dim % 256 != 0 [fused_moe] Do not use the inline-sort MXFP4 path when inter_dim % 256 != 0 Oct 6, 2026
…_dim % 256 != 0

ROCm#5232 kept CK-Tile stage2 away from non-256-aligned MXFP4 shapes, but the
inline-sort two-stage path still accepts them. It must not: the intermediate
scales are e8m0_shuffle'd in groups of 8 columns, so stage1 writes and stage2
read them on a stride that rounds scale-N up to a multiple of 8
(kas_c_k1_for), while the buffers sized on the exact inter_dim are short of
that stride whenever inter_dim % 256 != 0. is_mxfp4_moe_shape_supported also
pads inter_dim up to 256 before its allowlist lookup, so a 1152-wide call
reports supported by matching the 1280 instance and then runs the kernel
generated for 1280.

DeepSeek-V4.1-Flash at TP2 shards moe_intermediate 2304 to 1152 and faults
during CUDA graph capture:

  Memory access fault by GPU node-4 on address 0x7f84c9d33000.
  [aiter] Error in moe_sorting: CUDA error: an illegal memory access

tuned_fmoe.csv carries 16 tuned rows at inter_dim=1152 for (expert=384,
topk=6), so the shape is actively steered onto the faulting path. TP4 (640)
only escapes because vLLM pads it to 640 from 576 and the resulting
intermediate_pad already trips the padding check.

Reject the shape at both existing gates, matching how expert_mask is handled:
in get_2stage_cfgs so the tuned row is never selected, and in
_mxfp4_inline_sort_unsupported so an invocation that reaches it falls back
through the heuristic path. The heuristic path already runs these shapes on
a4w4 FlyDSL kernels correctly, so no shape loses a4w4 and no weights grow.

Co-authored-by: Claude Opus 5
Signed-off-by: Fangzhou Ai <[email protected]>
Co-authored-by: Cursor <[email protected]>
Signed-off-by: Fangzhou Ai <[email protected]>
Co-authored-by: Cursor <[email protected]>
@Fangzhou-Ai
Fangzhou-Ai force-pushed the fix/mxfp4-inline-sort-inter-dim-256-align branch from 15f6ab9 to 4ecf522 Compare October 6, 2026 19:25
@Fangzhou-Ai Fangzhou-Ai changed the title [fused_moe] Do not use the inline-sort MXFP4 path when inter_dim % 256 != 0 [Bugfix][fused_moe] Do not use the inline-sort MXFP4 path when inter_dim % 256 != 0 Oct 6, 2026
@zufayu
zufayu requested a review from yifehuan October 7, 2026 01:47
@azaidy
azaidy requested a balanced review from Copilot October 8, 2026 20:37

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.

🟡 Changes recommended

The aligned test controls use unsupported shapes and therefore do not verify that valid inline-sort rows remain selected.

1 open finding
What changed in this PR

Prevents non-256-aligned MXFP4 MoE shapes from using the faulting inline-sort path.

Changes:

  • Adds tuned-config and invocation-time alignment guards.
  • Adds dispatch tests and refactors tuned-config setup.
File Description
aiter/​fused_moe.py Rejects unsafe inline-sort configurations.
op_tests/​test_moe_mxfp4_inter_dim_dispatch.py Adds alignment coverage and shared test setup.

🧠 Review effort: Balanced


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@vgokhale

vgokhale commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

@aiter-bot review

@ChuanLi1101

Copy link
Copy Markdown
Contributor

LGTM on the gate. The 512/1280 cases only check that the discard warning is absent. Not a merge blocker. yifehuan / Ali still need to approve.

@aiter-bot

Copy link
Copy Markdown

ROCm/aiter PR #6184 — [Bugfix][fused_moe] Do not use the inline-sort MXFP4 path when inter_dim % 256 != 0

Gates the tuned inline-sort MXFP4 MoE path in fused_moe.py on inter_dim % 256 == 0 at both the tuned-row lookup and the invocation-time check, so non-256-aligned shapes (e.g. DeepSeek-V4.1-Flash TP2 inter_dim=1152, which faults in moe_sorting today) fall back to the heuristic a4w4 FlyDSL path.

Review (advisory): 🔴 HIGH RISK
Validation (deterministic): NOT RUN — auto-validation disabled (REVIEW_AUTO_VALIDATE=0) and no report supplied, though triage required it (runtime surface aiter/fused_moe.py changed; target op_tests/test_moe_mxfp4_inter_dim_dispatch.py) — an environment gap, not a PR defect.
Perf (advisory): NOT RUN — the triaged target op_tests/test_moe_mxfp4_inter_dim_dispatch.py exposes no benchmark entry point (no --scenario bench / perftest harness) and auto-validation was disabled, so no base-vs-head timing exists for the shapes the gate redirects off the inline-sort path.

🔴 [verified] aiter/fused_moe.py is a stable downstream-facing MoE contract and this PR changes its default dispatch — every non-256-aligned inter_dim is rerouted off the tuned inline-sort path at fused_moe.py:3152 — yet the PR carries no maintainer review (only a bot review), so a one-approval merge lands a dispatch change for every MoE consumer unowned. Reviewer must @-mention the de-facto owner of fused_moe.py (git top committer — Felix Li/yifehuan/yadaish recently, @valarLip per the ownership snapshot) in a PR comment requesting explicit sign-off before merge.

🔴 [verified] The changed aiter/fused_moe.py dispatch is the default MoE path downstream consumes (the PR's own repro is vLLM --moe-backend aiter on DeepSeek-V4.1-Flash inter_dim=1152), but the PR carries no ci: label so every downstream job — .github/workflows/atom-test.yaml, kimi-downstream.yaml, sglang_downstream.yaml, vllm_benchmark.yaml are all label-gated — is skipped, and the PR can merge green while breaking ATOM/SGLang/vLLM invisibly. Author must add ci:all (DeepSeek is exercised by atom+sglang+vllm) and require it green before merge.

⚠️ [inferred] The gate at aiter/fused_moe.py:3152 redirects every non-256-aligned inter_dim, but the demonstrated fault covers only 1152 — tuned inline-sort rows also exist at inter_dim=384 (kimik3_a4w4_tuned_fmoe.csv E=896 topk=16, i.e. Kimi-K3 TP8 whose unpadded 384 is a generated aux shape, and minimax_m3_fp4_tuned_fmoe.csv E=129 topk=5), and this same path was made to work at the non-256-aligned D_INTER=1408 by padding the scale buffer (fused_moe.py:2206-2214) — so if 384 currently runs inline-sort correctly, this PR silently swaps that fast path for the heuristic fallback with no timing to show the cost. Author must confirm the other tuned non-aligned shapes fault today (or are already rejected by the padding/scale-size checks), or measure the fallback's cost on them.

⚠️ [verified] The aligned controls in test_tuned_inline_sort_config_honours_inter_dim_alignment (op_tests/test_moe_mxfp4_inter_dim_dispatch.py:210) pass vacuously — at inter_dim=512/1280 the tuned row is discarded anyway, because the kname 'flydsl_mxmoe_g1_a4w4_16x128x256_f16in_hpf_nt_xcd4' carries no SWIGLU token and parses to act='silu' against the test's Swiglu dispatch (fused_moe.py:3190) and (32, 6144, 512, 4)/(32, 6144, 1280, 4) are not in the aux allowlist (fused_moe.py:3200) — so the PR body's "kept at 512 and 1280" claim is not what the test asserts, and a regression that silently drops valid aligned inline-sort rows stays green. Author must re-point the controls at a kname/activation pair and (expert, model_dim, inter_dim, topk) that survive every gate, or assert the resolved metadata is inline-sort.

⚠️ [verified] The new comment's root cause (fused_moe.py:386-393 — buffers sized on the exact inter_dim are short of the padded stride; the aux instance code-generated for the 256-aligned width "faults in moe_sorting") does not match the code: the stage1 output scale buffer is already stride-padded by kas_per_chunk_dw_for (fused_moe.py:2209-2214, the fix that made the non-aligned D_INTER=1408 work on this very path), and the sortzi aux instance is keyed (NE, TOPK, MB, H) with D_INTER unused (csrc/kernels/mxfp4_moe/moe_mxfp4_aux.cu:158, gen_instances.py:307-315); the invocation-time guard is likewise unreachable (no heuristic path can produce an inline-sort pair — fused_moe.py:3323, 3735), so the PR's "heuristic path can select an inline-sort pair" justification doesn't hold — the fault is real (HSA trace, before/after e2e) but its stated mechanism is not. Author must name the buffer that actually overruns at inter_dim=1152 (or show the existing padding is insufficient there), so the 256 boundary is anchored in code rather than a narrative later maintainers will disprove.

Comment thread aiter/fused_moe.py
# width (is_mxfp4_moe_shape_supported pads before its lookup), so the call
# faults in moe_sorting instead of running slow. The heuristic fallback
# handles these shapes.
if inter % 256 != 0:

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.

Thanks a lot for tracking down the TP2 crash, the write-up is really thorough! One thing I'm not sure about yet: I don't think this guard is needed in aiter, and it may end up hurting other callers.
I ran DeepSeek-V4.1-Flash at TP2 (inter_dim=1152) in ATOM on gfx950, using the same aiter without this guard. Graph capture from 512 down to 1 used the inline-sort pairs at every size, including the token-16 case from your log. There were no memory faults, and decode answered GSM8K samples correctly. The kernels also run cleanly in the op test, both eagerly and under CUDA graph.
So the fault seems specific to the vLLM DSV4 a4w4 integration. Because this guard sits in aiter's fused_moe, it would also turn off the faster inline-sort path for every other caller, including Kimi-K3 and MiniMax-M3 at I=384, which work today. It already trips CI here: MiniMax-M3 falls back to a kernel that isn't AOT-compiled.
Would you mind dumping shape, stride, dtype and is_shuffled for w1/w2/w1_scale/w2_scale at vLLM's fused_moe call site? I'm happy to share what ATOM passes so we can diff the two. Then the fix can target the exact mismatch.

@github-actions

Copy link
Copy Markdown
Contributor

No activity for 15 days, so this is now labelled stale. A push or a comment clears it; keep-open exempts it. Nothing is closed automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale The PR hasn't been updated in 2 weeks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants