Repository navigation
[Test] Regression test for topk_ids tail reads in multi-phase MoE sorting - #7062
ZLkanyo009 wants to merge 2 commits into
Conversation
… with num_local_tokens With num_local_tokens, MoeSortingMultiPhaseKernel_P0_v1, _P0_v2 and _P01 round the token count up to the id vector width (SubTokenTile / index_pack) and load topk_ids as whole vectors. When tokens is not a multiple of the width, the last vector reads up to (width - 1) * topk ids past the end of the [tokens, topk] tensor. The stray ids are never stored, but the load faults when topk_ids ends right before an unmapped page. Load the last, partial vector element by element and fill the missing lanes with -1, which the kernels already skip. Add op test test_moe_sorting_local_tokens_tail_read: it places topk_ids at the end of a device allocation followed by an unmapped page and checks that sorting neither faults nor changes its result. Co-authored-by: Cursor <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
|
@valarLip Update: this is now covered by On aiter The only part |
|
Hi @ZLkanyo009 please resolve conflicts |
Take main's csrc/include/moe_sorting_opus.h as-is: #7085 fixed the same three multi-phase kernels (impl::moe_sorting_load_topk_vec, bounded by the topk_ids tensor height), so this branch's kernel change is dropped. The branch now only adds the reproducer test_moe_sorting_local_tokens_tail_read to op_tests/test_moe_sorting.py.
READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
|
@yzhou103 Conflicts resolved: merged
cc @valarLip |
|
@yzhou103 Thanks for asking to keep this. On our side we are closing it, since the fix itself is fully in We confirmed that end to end on MI355X. With aiter All that is left here is the regression test ( |
Motivation
With
num_local_tokens(the CUDA-graph path), the multi-phase MoE sorting kernels (MoeSortingMultiPhaseKernel_P0_v1,_P0_v2,_P01) used to loadtopk_idspast the end of the[tokens, topk]tensor whentokensis not a multiple of the id vector width. The extra ids were never stored, so results were correct, but the load faulted whentopk_idsended right before an unmapped page (Memory access fault ... on address 0x...400000, 2 MiB-aligned, intermittent). We hit it serving Qwen3.8-Flash-Next (E=512, topk=10) in ATOM on MI355X (ROCm/ATOM#2771).The kernel fix is now in
mainvia #7085 (impl::moe_sorting_load_topk_vec, bounded by thetopk_idstensor height), which fixes the same three kernels. This PR originally carried an equivalent fix; after mergingmainit keepsmain'scsrc/include/moe_sorting_opus.hunchanged and only adds a regression test, sincemainhas no test that fails if this tail read comes back.Technical Details
The bug only faults when
topk_idsends right before an unmapped page. Under the PyTorch caching allocator another allocation almost always followstopk_ids, so the existing counted / uncounted sorting tests intest_moe_sorting.pyread the extra ids without faulting and still pass on the unfixed kernels. The test that came with #7085 intest_moe_sorting_mxfp4.py(_check_padding_sentinels) covers the other half of #7085, the quant-sort sentinel row write pastout, not this read.Test Plan
test_moe_sorting_local_tokens_tail_readinop_tests/test_moe_sorting.py(also called frommain()):PYTORCH_NO_{CUDA,HIP}_MEMORY_CACHING=1). It finds a device allocation directly followed by an unmapped page (checked in/proc/self/maps) and placestopk_idsat its very end, so a read past the tensor faults.num_local_tokensfor E/topk 512/10, 256/8 and 128/6, M in {1, 2, 3, 5, 7, 13, 31, 63, 127, 255, 257, 511, 513, 514, 1023, 2047, 4097}, anddispatch_policy2 (forced multi-phase) and 0 (auto).-1rows.Test Result
MI355X (gfx950), image
rocm/atom-dev:nightly_202610221638:main004245be3)58a7b141c(main269675deamerged)test_moe_sorting_local_tokens_tail_readhipErrorIllegalAddresstest_moe_sorting.py(whole file, default arguments)The diff against
mainis onlyop_tests/test_moe_sorting.py(+103 lines). black and ruff 0.16.0 pass.cc @valarLip @yzhou103
PR Self-Review
Reviewed revision
58a7b14
Behavior and intent
No kernel or library behavior changes: after merging
main,csrc/include/moe_sorting_opus.hismain's version (the #7085 fix) and the diff againstmainis onlyop_tests/test_moe_sorting.py. The PR adds a regression test because nothing inmainfails if the multi-phase sorting kernels start loadingtopk_idspast the tensor again: under the caching allocator that read hits a neighbouring allocation and the results stay correct.Validation and unverified scope
MI355X (gfx950),
rocm/atom-dev:nightly_202610221638, aiter JIT-built from this branch at58a7b141c:python3 -c "import test_moe_sorting as t; t.test_moe_sorting_local_tokens_tail_read()"inop_tests/, 5 runs: 5/5 pass, none took the skip path.python3 op_tests/test_moe_sorting.py: rc 0, 4253 checks passed, 0 failed.main004245be3(before [HIP] [Bugfix] Keep MX quant-sort and multi-phase MoE sort in bounds under num_local_tokens #7085): the reproducer failed 3/3 withhipErrorIllegalAddress.Unverified: only gfx950 was tested. On a machine where no device allocation is directly followed by an unmapped page, the test logs a warning and returns without checking anything.
PR-specific question
Before #7085,
test_moe_sorting_local_tokens_tail_readfails with a GPU memory fault (hipErrorIllegalAddress/Memory access fault) inMoeSortingMultiPhaseKernel_P0_v1/_P0_v2/_P01, becausetopk_idssits right before an unmapped page and the last id vector is loaded past the end of the tensor. No existing canonical test covers this: the counted / uncounted sorting tests intest_moe_sorting.pyrun under the caching allocator, where the stray read does not fault and the extra ids are dropped. The test added with #7085 (_check_padding_sentinelsintest_moe_sorting_mxfp4.py) compares counted and uncounted sorts, but only through the MX quant-sort output: it checks that the sentinel rows are not written pastout(the other bug #7085 fixed), and itstopk_idsalso live under the caching allocator, so a read past them would not fault there either.Changed op tests
op_tests/test_moe_sorting.py: addstest_moe_sorting_local_tokens_tail_read, which placestopk_idsat the very end of a device allocation followed by an unmapped page and sorts it withnum_local_tokens(multi-phase and auto dispatch, E/topk 512/10, 256/8, 128/6, 17 values of M that are not multiples of 4). It must not fault and must match sorting the same ids with padded-1rows. It is not redundant for the reasons above: it is the only test where a read pasttopk_idsfaults instead of passing silently.Submission Checklist