Skip to content

[HIP] [Bugfix] mxfp4 moe sort skip invalid ids (-1s) - #6119

Open
cagrikymk wants to merge 2 commits into
mainfrom
mekaymak/mxfp4_moe_sort_skip_invalid_ids
Open

cagrikymk wants to merge 2 commits into
mainfrom
mekaymak/mxfp4_moe_sort_skip_invalid_ids

Conversation

@cagrikymk

Copy link
Copy Markdown
Contributor

Motivation

VLLM_MOE_SKIP_PADDING set from vllm (on by default), vllm uses -1 expert id for the filler rows. However, moe_mxfp4_aux path crashes as it tries to read -1 as valid row.

This PR adds a skip mechanism, so all modules involved in this process skips -1s safely.

Routes with an id outside [0, E) are dropped, as opus does. reverse_sorted is -1 for them, and the scatter-reduce adds 0 for them.

Similar idea as #5295

Technical Details

After the fix, VLLM starts the server succesfully for dsv41-flash on MI355 with a4w4 MoE enabled:

flexible strict
skip-padding 1 (fixed sort), no spec 95.98 81.35
skip-padding 0, no spec 95.91 82.49
skip-padding 1 (fixed sort), spec 95.68 81.43

Performace change:

kernel experts / top-k BM M old us fixed us change old run-to-run
BM16 one-CTA sort 384 / 6 16 1 2.65 2.77 +4.6% 0.5%
BM16 one-CTA sort 384 / 6 16 8 2.96 2.96 +0.2% 0.3%
BM16 one-CTA sort 384 / 6 16 32 3.62 3.67 +1.5% 0.3%
BM16 one-CTA sort 384 / 6 16 64 4.67 4.78 +2.4% 0.1%
BM16 one-CTA sort 384 / 6 16 128 6.09 6.14 +0.8% 0.0%
BM16 one-CTA sort 384 / 6 16 256 7.18 7.21 +0.4% 0.2%
BM16 one-CTA sort 384 / 6 16 512 9.00 9.07 +0.8% 0.1%
BM16 one-CTA sort 128 / 3 16 1 2.55 2.64 +3.6% 0.0%
BM16 one-CTA sort 128 / 3 16 8 2.54 2.65 +4.2% 0.0%
BM16 one-CTA sort 128 / 3 16 32 2.73 2.84 +3.7% 0.1%
BM16 one-CTA sort 128 / 3 16 64 3.07 3.12 +1.6% 0.2%
BM16 one-CTA sort 128 / 3 16 128 3.36 3.45 +2.8% 0.1%
BM16 one-CTA sort 128 / 3 16 256 3.71 3.70 -0.2% 0.2%
BM16 one-CTA sort 128 / 3 16 512 4.37 4.42 +1.1% 0.2%
BM16 sort + zero-init 384 / 6 16 1 2.84 2.91 +2.2% 0.6%
BM16 sort + zero-init 384 / 6 16 8 3.10 3.17 +2.3% 0.2%
BM16 sort + zero-init 384 / 6 16 32 3.76 3.83 +1.8% 0.5%
BM16 sort + zero-init 384 / 6 16 64 4.85 4.87 +0.6% 0.1%
BM16 sort + zero-init 384 / 6 16 128 6.31 6.34 +0.5% 0.5%
BM16 sort + zero-init 384 / 6 16 256 7.65 7.68 +0.4% 0.2%
BM16 sort + zero-init 384 / 6 16 512 9.85 9.88 +0.4% 0.3%
BM16 sort + zero-init 128 / 3 16 1 2.66 2.72 +2.1% 1.2%
BM16 sort + zero-init 128 / 3 16 8 2.63 2.76 +5.3% 1.3%
BM16 sort + zero-init 128 / 3 16 32 2.83 2.94 +3.9% 0.2%
BM16 sort + zero-init 128 / 3 16 64 3.16 3.22 +2.0% 0.4%
BM16 sort + zero-init 128 / 3 16 128 3.54 3.59 +1.4% 0.4%
BM16 sort + zero-init 128 / 3 16 256 4.14 4.10 -0.9% 0.1%
BM16 sort + zero-init 128 / 3 16 512 5.23 5.25 +0.4% 0.1%
three-stage sort 384 / 6 32 512 14.16 14.31 +1.1% 0.1%
three-stage sort 384 / 6 32 2,048 15.24 15.27 +0.2% 0.2%
three-stage sort 384 / 6 32 8,192 19.64 19.91 +1.4% 0.1%
three-stage sort 384 / 6 32 16,384 25.52 25.89 +1.4% 0.0%
three-stage sort 384 / 6 64 2,048 15.27 15.25 -0.1% 0.2%
three-stage sort 384 / 6 64 8,192 19.64 19.94 +1.5% 0.1%
three-stage sort 384 / 6 64 16,384 25.35 25.71 +1.4% 0.0%
three-stage sort 384 / 6 128 4,096 16.61 16.61 -0.0% 0.1%
three-stage sort 384 / 6 128 16,384 26.69 26.97 +1.1% 0.1%
three-stage sort 128 / 3 32 512 8.53 8.45 -1.0% 0.5%
three-stage sort 128 / 3 32 2,048 8.67 8.68 +0.2% 0.1%
three-stage sort 128 / 3 32 8,192 10.82 10.61 -1.9% 0.0%
three-stage sort 128 / 3 32 16,384 13.27 13.34 +0.5% 0.2%
three-stage sort 128 / 3 64 2,048 8.77 8.77 -0.1% 0.4%
three-stage sort 128 / 3 64 8,192 10.64 10.42 -2.1% 0.1%
three-stage sort 128 / 3 64 16,384 13.28 13.42 +1.0% 0.1%
three-stage sort 128 / 3 128 4,096 9.37 9.12 -2.6% 0.0%
three-stage sort 128 / 3 128 16,384 13.56 13.68 +0.9% 0.3%
one-CTA sort + quant (MB32) 384 / 6 32 32 5.56 5.61 +0.7% 0.7%
one-CTA sort + quant (MB32) 384 / 6 32 128 10.47 10.67 +1.9% 1.0%
one-CTA sort + quant (MB32) 384 / 6 32 512 14.35 14.45 +0.7% 0.8%
one-CTA sort + quant (MB32) 384 / 6 32 2,048 30.41 28.95 -4.8% 12.5%
one-CTA sort + quant (MB32) 128 / 3 32 32 3.57 3.69 +3.4% 0.5%
one-CTA sort + quant (MB32) 128 / 3 32 128 4.93 5.03 +2.0% 1.9%
one-CTA sort + quant (MB32) 128 / 3 32 512 5.91 5.98 +1.2% 0.2%
one-CTA sort + quant (MB32) 128 / 3 32 2,048 13.75 14.18 +3.1% 3.2%
scatter-reduce bf16 384 / 6 32 64 2.04 2.20 +7.9% 0.0%
scatter-reduce bf16 384 / 6 32 512 4.79 5.12 +7.0% 12.5%
scatter-reduce bf16 384 / 6 32 4,096 42.66 43.13 +1.1% 0.2%
scatter-reduce bf16 384 / 6 32 16,384 229.26 224.21 -2.2% 0.7%
scatter-reduce bf16 128 / 3 32 64 1.91 2.03 +6.3% 0.6%
scatter-reduce bf16 128 / 3 32 512 3.28 3.42 +4.3% 1.0%
scatter-reduce bf16 128 / 3 32 4,096 25.30 24.97 -1.3% 2.7%
scatter-reduce bf16 128 / 3 32 16,384 122.68 125.74 +2.5% 5.3%
scatter-reduce mxfp4 384 / 6 32 64 2.08 2.22 +6.8% 0.4%
scatter-reduce mxfp4 384 / 6 32 512 3.57 3.81 +6.7% 4.2%
scatter-reduce mxfp4 384 / 6 32 4,096 19.03 19.33 +1.6% 0.0%
scatter-reduce mxfp4 384 / 6 32 16,384 83.78 83.97 +0.2% 0.0%
scatter-reduce mxfp4 128 / 3 32 64 1.91 2.04 +6.7% 0.1%
scatter-reduce mxfp4 128 / 3 32 512 3.28 3.40 +3.8% 0.5%
scatter-reduce mxfp4 128 / 3 32 4,096 13.93 14.03 +0.7% 0.2%
scatter-reduce mxfp4 128 / 3 32 16,384 60.45 63.69 +5.4% 6.7%

Test Plan

New unit tests are added while matching the existing style.

op_tests/test_moe_sorting_mxfp4.py is extended.

Test Result

All tests pass.

@cagrikymk
cagrikymk requested a review from a team October 3, 2026 03:19
@github-actions

github-actions Bot commented Oct 3, 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 6119 --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 [HIP][Bugfix] mxfp4 moe sort skip invalid ids (-1s) [HIP] [Bugfix] mxfp4 moe sort skip invalid ids (-1s) Oct 3, 2026
@github-actions github-actions Bot added the HIP label Oct 3, 2026
@zufayu
zufayu requested a review from yzhou103 October 4, 2026 00:33
@zufayu

zufayu commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

@aiter-bot review


Trying the review bot on a few representative PRs across different areas, so the team can see what it actually produces on real diffs.

How to use it — just comment on any PR:

  • @aiter-bot review — first pass
  • @aiter-bot re-review — after you push fixes

What it is: advisory only. It surfaces hints for a human reviewer — perf regressions, silent correctness bugs, dispatch-gate holes, AI-generated code patterns. Its judgement is stochastic, it never blocks a merge, and every verdict (✅ / ⚠️ / 🔴) is posted so the reviewer who asked for it sees the result.

It runs on a self-hosted box against a local model, so a pass takes roughly 25–30 min.

Who can trigger it: the logins in authorized.txt — the aiter review roster. Details in SKILL.md.

@aiter-bot

Copy link
Copy Markdown

[ROCm/aiter] PR #6119 — [HIP] [Bugfix] mxfp4 moe sort skip invalid ids (-1s)

Teaches the gfx950 MXFP4 MoE aux kernels (one-CTA and three-stage sorts, the fused sort+quant, and both scatter-reduces) to drop routes whose expert id falls outside [0, E) — vLLM's -1 CUDA-graph padding rows — instead of indexing arrays with -1 and crashing, marking dropped routes reverse_sorted = -1 and adding exactly 0 for them in the scatter-reduce.

Review (advisory): 🔴 HIGH RISK
Validation (deterministic): NOT RUN — REVIEW_AUTO_VALIDATE=0 and no torch/built-aiter environment on this box; the target op_tests/test_moe_sorting_mxfp4.py exists but could not be executed (environment gap, not a PR defect)
Perf (advisory): NOT RUN — no runnable environment on this box (no torch, no built head kernels); the PR's own old-vs-fixed table, all-valid input only, is the only timing evidence

🔴 [verified] The three changed kernels (moe_sort_quant.cuh, moe_3stage_sort.cuh, moe_scatter_reduce.cuh) are the default a4w4 fused-MoE path on gfx950 that vLLM and SGLang consume — this PR exists to fix a vLLM crash — yet the PR carries only the HIP label, so every downstream job (ci:sglang, ci:vllm) skips and a post-merge breakage in GPT-OSS-120B or DeepSeek-R1-MXFP4 would be invisible while aiter CI stays green. Reviewer should ask for ci:sglang + ci:vllm (or ci:all) and require them green before merge.
⚠️ [inferred] The perf table is necessarily all-valid input (the old kernel crashes on -1), so the motivating workload is unmeasured: on a mostly-padding CUDA-graph batch every dropped route in the one-CTA sort issues atomicAdd(&count[0], 0) then atomicAdd(&counter[0], 0) (moe_sort_quant.cuh:79,167), serializing on expert 0's slots instead of spreading across E addresses — could this cost more than the +4.6% measured at M=1 with all-valid routing? Author must time a padding-heavy batch (e.g. M=512, mostly -1 routes) and confirm the branchless add-0 strategy holds there.
⚠️ [verified] The invalid-id tests added to op_tests/test_moe_sorting_mxfp4.py cover only (E, topk) = (384, 6) and (128, 3), but the same changed template code instantiates for Kimi-K2 (384/8), Kimi-K3 (896/16), GPT-OSS (128/4), MiniMax (129/5, 256/8) and qwen3.5 (512/10) per csrc/kernels/mxfp4_moe/moe_aux/codegen/gen_instances.py, none of which run the new cases. Author must run one non-DSv4.1 shape (e.g. GPT-OSS 128/4) through the new aux tests, or state why the -1 handling cannot be shape-dependent.
📝 [verified, numbers as reported in the PR] The fix costs +6.3–7.9% on scatter-reduce at small M (moe_scatter_reduce.cuh; bf16 E=384 M=64: 2.04 → 2.20 µs, run-to-run 0.0%), paid on every a4w4 MoE call including batches with no padding at all. Reviewer should ask whether an always-paid ~7% on the scatter stage is acceptable, or whether a no-invalid-ids fast path is worth it.

@yzhou103

yzhou103 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

please resolve conflicts

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

@github-actions github-actions Bot added the stale The PR hasn't been updated in 2 weeks label Oct 25, 2026
# MXFP4 aux sorts and scatter-reduce with invalid expert ids: correctness only,
# each case asserts. gfx950 only, like the MXFP4 fused-MoE paths that use
# `module_moe_mxfp4_aux`.
if get_gfx() == "gfx950":

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please use the runtime architecture for the new gfx950-only aux regression gate. get_gfx() follows the last entry in GPU_ARCHS: with GPU_ARCHS="gfx950;gfx942" these cases are silently skipped on gfx950, and with the reverse ordering they can run on gfx942, which this aux module does not support. get_gfx_runtime() (or the actual device properties) makes the gate independent of wheel build targets. The kernel checks themselves passed in my gfx950 run: 96 sort cases, 16 fused sort/quant cases, 32 scatter cases, plus 4 diagnostics with NaN/invalid data in row 0.

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

HIP stale The PR hasn't been updated in 2 weeks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants