Repository navigation
Conversation
… 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]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
Summary
topk_gatingrepeated expert ids for a row with fewer thantopkvalid experts (e.g. awhole 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 mergewrites 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 atE=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_clampto pass withVLLM_ROCM_USE_AITER=1.Changes
csrc/include/topk_gating_kernels.cuh: addINVALID_SCORE = -FLT_MAXand use it as theselection score of an invalid (NaN) expert in all six kernels (
opt,opt_multiwave,opt_n,prefill,smem,smem_n).-INFINITYstays reserved for entries the mergehas 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
nansection.Performance (if applicable)
Measured on a branch carrying the same kernel changes, gfx942, softmax, E=256, topk 8,
bf16 gating, fp32 bias:
Testing
Documentation
Dependencies
Breaking Changes
None. A row without NaN is unchanged. A row with fewer than
topkvalid experts used torepeat 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:all-NaN row chose repeated experts: [0, 0, 0, 0, 0, 0, 0, 0].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.FlyDSL HERD route, which is a separate algorithm and is not changed.
Existing test comment
_starved_rows_oksays the sentinel ids on all-NaN rows "are allowed to repeat". That isno longer true; the comment is left as is in this PR.
🤖 Generated with Claude Code