Skip to content

[HIP] [Bugfix] Guard against negative expert ids in MoE sorting P0 - #4839

Merged
yifehuan merged 2 commits into
ROCm:mainfrom
xudonlyu:fix/moe-sorting-negative-expert-id
Sep 4, 2026
Merged

yifehuan merged 2 commits into
ROCm:mainfrom
xudonlyu:fix/moe-sorting-negative-expert-id

Conversation

@xudonlyu

@xudonlyu xudonlyu commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4827.

MoeSortingMultiPhaseKernel_P0_v1 reads the expert id out of topk_ids into a signed IndexType and only bounds-checks it from above:

IndexType eid = x[j.value];        // IndexType = int (signed)
if(eid < kargs.num_experts)        // no lower bound
    p_expert_mesh[eid * mesh_stride + curr_token_id] = (curr_topk_id + 1) & 0xffff;

A negative eid still satisfies eid < num_experts, so eid * mesh_stride becomes a negative offset and the store writes before p_expert_mesh, faulting the GPU.

Where the negative id comes from

vLLM writes it deliberately. Its topk kernels mark padded rows with -1 so
downstream consumers can skip them — csrc/libtorch_stable/moe/topk_softmax_kernels.cu:

indices[idx] = is_pad_row ? static_cast<IndType>(-1)
                          : (should_process_row ? (expert - start_expert) : num_experts);

This is the VLLM_MOE_SKIP_PADDING path and it is on by default. Both
sentinels on that line are out-of-range on purpose: num_experts for a row this
rank does not own, -1 for a padded row. The existing eid < num_experts check
is already filtering the first one, so this change is the other half of the same
contract rather than a new policy.

Instrumented a DP8 + EP run to confirm it at the source. The first hit is during
profile_run, where every row is padding:

NEG topk_ids: min=-1 count=24576 uniq=[-1] shape=(4096, 6)
  profile_run -> _dummy_run -> ... -> rocm_aiter_fused_experts

uniq=[-1] and a count equal to the whole tensor — the entire batch is padded,
not a stray value. That also explains the timing: the fault only ever appeared
during cuda graph capture and profiling, which is exactly when every row is
padding.

Note this corrects the earlier description of this PR, which attributed the
negative ids to expert parallelism. It is not EP: a row owned by another rank is
written as num_experts, a positive value the upper bound already rejects. EP
only mattered because that configuration is what reaches the multi-phase path.

Repro

8x MI355X (gfx950), vLLM serving DeepSeek-V4-Pro (384 routed experts, top-6), DP8 + EP + DSpark speculative decoding, --max-num-seqs 512. Every worker aborted during CUDA graph capture:

Memory access fault by GPU node-3 (Agent handle: 0x...) on address 0x7f836a527000. Reason: Unknown.

ROCr Debug Agent with --precise-memory attributed it to this kernel:

void aiter::opus_moe_sorting_entry<
    aiter::MoeSortingMultiPhaseKernel_P0_v1<
        aiter::MoeSortingProblemMp<int, float, unsigned char, 4, true, false, true>>, ...>

Verification

Same run, same flags, only this change:

before after
memory access faults 8 (every worker) 0
server startup dies in CUDA graph capture READY
GSM8K (200 samples, 5-shot) n/a 0.955
decode TPOT (batch=1) n/a 13.4 ms (30.6 ms without spec decode)
DSpark acceptance length n/a 2.66

The multi-phase path is exercised unchanged — no dispatch_policy override and no reduction in max_num_seqs. Confirmed moe_sorting_opus_get_workspace_size(M, 385, 6, 0) != 0 for the M values in play, i.e. moe_sorting_opus_mp() is the path taken.

Scope

Three other sites in moe_sorting_opus.h index the mesh or the token map by a
raw topk_ids value with no lower bound — around lines 794, 1021 and 2160. They
take the same input under the same contract, so they have the same hole; I only
touched the site I could attribute the fault to. Happy to extend this PR to
cover them if that is preferred.

Note I could not build with clang-format-18 in my environment — the change is a single condition plus a comment, so I kept the surrounding formatting untouched. Happy to reformat if CI flags it.

@xudonlyu
xudonlyu requested review from a team and a lite review from Copilot August 18, 2026 22:01
@github-actions

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; main branch always runs both MI35X and MI300X
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 4839 --add-label <label>

PR title tags:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf] and op tags like [MLA] are left untouched. Add the no-auto-title label to opt this PR out of title tagging.

@github-actions github-actions Bot changed the title Guard against negative expert ids in MoE sorting P0 [HIP] Guard against negative expert ids in MoE sorting P0 Aug 18, 2026

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.

Pull request overview

Fixes a GPU memory access fault in the Opus MoE multi-phase sorting P0 kernel when expert-parallelism can produce negative expert IDs, by adding a missing lower-bound check before indexing into the expert mesh.

Changes:

  • Add eid >= 0 to the existing eid < num_experts guard in MoeSortingMultiPhaseKernel_P0_v1.
  • Document why negative expert IDs can occur under expert parallelism and how that leads to negative pointer offsets without the check.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@xudonlyu xudonlyu changed the title [HIP] Guard against negative expert ids in MoE sorting P0 [Bugfix] Guard against negative expert ids in MoE sorting P0 Aug 18, 2026
@zufayu
zufayu requested a review from yifehuan August 19, 2026 01:24
@github-actions github-actions Bot changed the title [Bugfix] Guard against negative expert ids in MoE sorting P0 [HIP] [Bugfix] Guard against negative expert ids in MoE sorting P0 Aug 19, 2026
Copilot AI review requested due to automatic review settings August 25, 2026 12:28
@xudonlyu

Copy link
Copy Markdown
Contributor Author

Chased the negative id to its source rather than clamping it blind — description updated with the details.

Short version: vLLM writes it on purpose. topk_softmax_kernels.cu marks padded rows with -1 so downstream consumers skip them, and that path (VLLM_MOE_SKIP_PADDING) is on by default. Both out-of-range values on that line are sentinels: num_experts for a row this rank does not own, -1 for a padded row. This kernel already filters the first one with eid < num_experts, so the lower bound is the other half of a contract it is following, not a new guard.

Instrumented a DP8 run to confirm: the first hit is in profile_run, uniq=[-1] with a count equal to the whole tensor — every row is padding there, which is also why the fault only ever showed up during profiling and graph capture.

This also corrects my original description, which blamed expert parallelism. That was wrong: a row owned by another rank is written as num_experts, a positive value.

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.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread csrc/include/moe_sorting_opus.h Resolved
Copilot AI review requested due to automatic review settings August 25, 2026 13:33
@xudonlyu
xudonlyu force-pushed the fix/moe-sorting-negative-expert-id branch from 6406887 to 4e5866c Compare August 25, 2026 13:33

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.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

yifehuan
yifehuan previously approved these changes Aug 26, 2026

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

LGTM, or maybe you can condense comment (1671~1676) to a single line

eid is read from topk_ids into a signed IndexType and only bounds-checked
from above. Callers mark a padded row with a negative id, the counterpart
of the num_experts sentinel the upper bound already rejects; without a
lower bound it makes eid * mesh_stride a negative offset, storing before
p_expert_mesh and faulting the GPU.
Copilot AI review requested due to automatic review settings August 27, 2026 02:03
@xudonlyu
xudonlyu force-pushed the fix/moe-sorting-negative-expert-id branch from 4e5866c to e08e23a Compare August 27, 2026 02:03

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.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

@tuukkjs

tuukkjs commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Can this be merged? I have a follow-up PR coming up

@xudonlyu

xudonlyu commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

@yifehuan Could we get this PR merged?

@yifehuan
yifehuan merged commit b297d2d into ROCm:main Sep 4, 2026
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] MoeSortingMultiPhaseKernel_P0_v1: missing lower bound on signed expert id causes GPU memory fault under EP (one-line fix)

4 participants