Skip to content

[HIP] [Bugfix] topk_gating: distinct expert ids for rows with too few valid experts - #7838

Draft
rjrock wants to merge 1 commit into
ROCm:mainfrom
rjrock:fix-topk-gating-all-nan-row-distinct-ids
Draft

rjrock wants to merge 1 commit into
ROCm:mainfrom
rjrock:fix-topk-gating-all-nan-row-distinct-ids

Conversation

@rjrock

@rjrock rjrock commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

topk_gating repeated expert ids for a row with fewer than topk valid experts (e.g. a
whole NaN row). It now completes such a row with distinct experts, each taken once after
the valid ones, with weight 0.

Motivation

The kernels pushed a NaN expert down to -INFINITY, which is also what the top-k merge
writes for an entry it has already taken or run out of. With no candidate above
-INFINITY, the argmax kept re-electing the first such entry, so a whole NaN row at
E=256, topk=8 (softmax) returned ids [0, 0, 0, 0, 0, 0, 0, 0].

A repeated id is not harmless at weight 0: the fused MoE sorter keeps one slot per
(token, expert), so a zero-weight repeat after a valid pick of the same expert overwrites
that pick.

This is the AITER-side part of getting vLLM's
tests/kernels/moe/test_fused_topk.py::test_fused_topk_bias_nan_inf_clamp to pass with
VLLM_ROCM_USE_AITER=1.

Changes

  • csrc/include/topk_gating_kernels.cuh: add INVALID_SCORE = -FLT_MAX and use it as the
    selection score of an invalid (NaN) expert in all six kernels (opt, opt_multiwave,
    opt_n, prefill, smem, smem_n). -INFINITY stays reserved for entries the merge
    has taken or run out of, so the two no longer tie. An invalid pick is weighted 0
    (invalid || !isfinite(max_val)); the flag is decided before the bias is subtracted.
    Valid experts still always win, and rows without NaN are unchanged.
  • op_tests/test_moe_topk_gating.py: test_topk_gating_all_nan_row_picks_k_distinct_experts
    (softmax, E=256, topk=8, bf16, row 0 all NaN) asserts the row's ids are in range and
    distinct. It is also run from the existing nan section.

Performance (if applicable)

Measured on a branch carrying the same kernel changes, gfx942, softmax, E=256, topk 8,
bf16 gating, fp32 bias:

Workload Before After Change
T=4096 9.72 us 9.73 us +0.1%
T=1 4.16 us 4.29 us +3.1%

Testing

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X

Documentation

  • No documentation change needed: no user-facing option changes

Dependencies

  • No new third-party dependencies added

Breaking Changes

None. A row without NaN is unchanged. A row with fewer than topk valid experts used to
repeat ids and now returns distinct ones, so no caller can rely on the old output.

PR Self-Review

Validation and unverified scope

MI300X (gfx942), rjrock2/vllm:rocm-ci_base-nov11, JIT module rebuilt from this tree:

  • New test on the unmodified kernel: fails with
    all-NaN row chose repeated experts: [0, 0, 0, 0, 0, 0, 0, 0].
  • New test with the kernel change: passes.
  • python3 op_tests/test_moe_topk_gating.py --section nan --num-experts 64,256 --topk 4,8 --num-tokens 4 --dtype bf16: passes, exit 0.
  • Not run: the full op_test sweep for this exact patch, gfx950/gfx1250, and the opt-in
    FlyDSL HERD route, which is a separate algorithm and is not changed.

Existing test comment

_starved_rows_ok says the sentinel ids on all-NaN rows "are allowed to repeat". That is
no longer true; the comment is left as is in this PR.

🤖 Generated with Claude Code

… valid experts

A row with fewer than topk valid experts (e.g. a whole NaN row) repeated
expert ids. The kernels pushed a NaN expert down to -INFINITY, which is also
what the top-k merge writes for an entry it has already taken or run out of,
so the argmax kept re-electing the first such entry: a whole NaN row at
E=256, topk=8 (softmax) came back as ids [0, 0, 0, 0, 0, 0, 0, 0].

A repeated id is not harmless at weight 0: aiter's fused MoE sorter keeps
one slot per (token, expert), so a zero-weight repeat after a valid pick of
the same expert overwrites that pick.

Floor the selection score of an invalid expert (a NaN logit) at -FLT_MAX
(INVALID_SCORE) in every topk_gating kernel (opt, opt_multiwave, opt_n,
prefill, smem, smem_n), keep -INFINITY for entries the merge has taken or
run out of, and weight an invalid pick 0. Valid experts still always win;
invalid ones fill the remaining slots once each, after the valid ones. Rows
without NaN are unchanged.

op_tests/test_moe_topk_gating.py: add
test_topk_gating_all_nan_row_picks_k_distinct_experts (softmax, E=256,
topk=8, bf16, row 0 all NaN), also run from the nan section. On gfx942 it
fails on the old kernel with the ids above and passes with this change.

Signed-off-by: Ryan Rock <[email protected]>
Co-Authored-By: Claude <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

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

Extended tests (opt-in via labels):

Label Tests
ci:mi300x Run Aiter standard tests on MI300X in PRs; main branch always runs them
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
ci:windows Windows JIT op smoke test on the Radeon R9700 ROCm runner
ci:multi-gpu 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: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
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 7838 --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.

Reviews:
A reviewer is usually assigned within one working day after the PR is ready for review (see Code Review Process). If nobody has responded for about a week, mention the area contact listed in CONTACTS.md. Want to help review? See REVIEWERS.md.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant