Repository navigation
[HIP] [Bugfix] Guard against negative expert ids in MoE sorting P0 - #4839
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
PR title tags: |
There was a problem hiding this comment.
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 >= 0to the existingeid < num_expertsguard inMoeSortingMultiPhaseKernel_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.
|
Chased the negative id to its source rather than clamping it blind — description updated with the details. Short version: vLLM writes it on purpose. Instrumented a DP8 run to confirm: the first hit is in This also corrects my original description, which blamed expert parallelism. That was wrong: a row owned by another rank is written as |
6406887 to
4e5866c
Compare
yifehuan
left a comment
There was a problem hiding this comment.
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.
4e5866c to
e08e23a
Compare
|
Can this be merged? I have a follow-up PR coming up |
|
@yifehuan Could we get this PR merged? |
Fixes #4827.
MoeSortingMultiPhaseKernel_P0_v1reads the expert id out oftopk_idsinto a signedIndexTypeand only bounds-checks it from above:A negative
eidstill satisfieseid < num_experts, soeid * mesh_stridebecomes a negative offset and the store writes beforep_expert_mesh, faulting the GPU.Where the negative id comes from
vLLM writes it deliberately. Its topk kernels mark padded rows with
-1sodownstream consumers can skip them —
csrc/libtorch_stable/moe/topk_softmax_kernels.cu:This is the
VLLM_MOE_SKIP_PADDINGpath and it is on by default. Bothsentinels on that line are out-of-range on purpose:
num_expertsfor a row thisrank does not own,
-1for a padded row. The existingeid < num_expertscheckis 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: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. EPonly 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:ROCr Debug Agentwith--precise-memoryattributed it to this kernel:Verification
Same run, same flags, only this change:
The multi-phase path is exercised unchanged — no
dispatch_policyoverride and no reduction inmax_num_seqs. Confirmedmoe_sorting_opus_get_workspace_size(M, 385, 6, 0) != 0for the M values in play, i.e.moe_sorting_opus_mp()is the path taken.Scope
Three other sites in
moe_sorting_opus.hindex the mesh or the token map by araw
topk_idsvalue with no lower bound — around lines 794, 1021 and 2160. Theytake 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-18in 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.