Skip to content

[Test] Regression test for topk_ids tail reads in multi-phase MoE sorting - #7062

Closed
ZLkanyo009 wants to merge 2 commits into
mainfrom
lingzha/moe-sort-tail-oob
Closed

ZLkanyo009 wants to merge 2 commits into
mainfrom
lingzha/moe-sort-tail-oob

Conversation

@ZLkanyo009

@ZLkanyo009 ZLkanyo009 commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

With num_local_tokens (the CUDA-graph path), the multi-phase MoE sorting kernels (MoeSortingMultiPhaseKernel_P0_v1, _P0_v2, _P01) used to load topk_ids past the end of the [tokens, topk] tensor when tokens is not a multiple of the id vector width. The extra ids were never stored, so results were correct, but the load faulted when topk_ids ended 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 main via #7085 (impl::moe_sorting_load_topk_vec, bounded by the topk_ids tensor height), which fixes the same three kernels. This PR originally carried an equivalent fix; after merging main it keeps main's csrc/include/moe_sorting_opus.h unchanged and only adds a regression test, since main has no test that fails if this tail read comes back.

Technical Details

The bug only faults when topk_ids ends right before an unmapped page. Under the PyTorch caching allocator another allocation almost always follows topk_ids, so the existing counted / uncounted sorting tests in test_moe_sorting.py read the extra ids without faulting and still pass on the unfixed kernels. The test that came with #7085 in test_moe_sorting_mxfp4.py (_check_padding_sentinels) covers the other half of #7085, the quant-sort sentinel row write past out, not this read.

Test Plan

test_moe_sorting_local_tokens_tail_read in op_tests/test_moe_sorting.py (also called from main()):

  • A child process runs with the caching allocator off (PYTORCH_NO_{CUDA,HIP}_MEMORY_CACHING=1). It finds a device allocation directly followed by an unmapped page (checked in /proc/self/maps) and places topk_ids at its very end, so a read past the tensor faults.
  • It sorts with num_local_tokens for 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}, and dispatch_policy 2 (forced multi-phase) and 0 (auto).
  • Each result must equal sorting the same ids followed by padded -1 rows.
  • It runs out of process because a GPU memory fault aborts the interpreter. If no allocation followed by an unmapped page is found, the test logs a warning and returns.

Test Result

MI355X (gfx950), image rocm/atom-dev:nightly_202610221638:

before #7085 (main 004245be3) this branch 58a7b141c (main 269675dea merged)
test_moe_sorting_local_tokens_tail_read 3/3 fail, hipErrorIllegalAddress 5/5 pass (no skip)
test_moe_sorting.py (whole file, default arguments) — 4253 checks passed, 0 failed

The diff against main is only op_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.h is main's version (the #7085 fix) and the diff against main is only op_tests/test_moe_sorting.py. The PR adds a regression test because nothing in main fails if the multi-phase sorting kernels start loading topk_ids past 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 at 58a7b141c:

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_read fails with a GPU memory fault (hipErrorIllegalAddress / Memory access fault) in MoeSortingMultiPhaseKernel_P0_v1 / _P0_v2 / _P01, because topk_ids sits 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 in test_moe_sorting.py run under the caching allocator, where the stray read does not fault and the extra ids are dropped. The test added with #7085 (_check_padding_sentinels in test_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 past out (the other bug #7085 fixed), and its topk_ids also live under the caching allocator, so a read past them would not fault there either.

Changed op tests

op_tests/test_moe_sorting.py: adds test_moe_sorting_local_tokens_tail_read, which places topk_ids at the very end of a device allocation followed by an unmapped page and sorts it with num_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 -1 rows. It is not redundant for the reasons above: it is the only test where a read past topk_ids faults instead of passing silently.

Submission Checklist

… 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]>
@ZLkanyo009 ZLkanyo009 added the HIP label Oct 28, 2026
@ZLkanyo009
ZLkanyo009 requested a review from a team October 28, 2026 02:46
@ZLkanyo009 ZLkanyo009 added the HIP label Oct 28, 2026
@github-actions github-actions Bot changed the title [HIP] [OPUS] [Bugfix] Bound topk_ids loads in multi-phase MoE sorting with num_local_tokens [HIP] [Bugfix] Bound topk_ids loads in multi-phase MoE sorting with num_local_tokens Oct 28, 2026
@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 (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 gfx1201 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 7062 --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.

@ZLkanyo009

Copy link
Copy Markdown
Contributor Author

@valarLip Update: this is now covered by main's #7085 (c90842add), which keeps the multi-phase MoE sort in bounds under num_local_tokens. Its bound is at least as strict as this PR's: reads never go past the end of topk_ids, even when the count exceeds the tensor height.

On aiter main 4d1980603 (without this PR), MI355X: this PR's repro test (topk_ids ending right before an unmapped page) passes 5/5, while it fails 3/3 on main before #7085; test_moe_sorting.py passes 4253.

The only part main does not have is that repro test. I can trim this PR down to it if it is useful; otherwise it can be closed.

@yzhou103

Copy link
Copy Markdown
Contributor

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.
@ZLkanyo009 ZLkanyo009 changed the title [HIP] [Bugfix] Bound topk_ids loads in multi-phase MoE sorting with num_local_tokens [Test] Regression test for topk_ids tail reads in multi-phase MoE sorting Oct 29, 2026
@github-actions github-actions Bot changed the title [Test] Regression test for topk_ids tail reads in multi-phase MoE sorting [Bugfix] Bound topk_ids loads in multi-phase MoE sorting with num_local_tokens Oct 29, 2026
@github-actions github-actions Bot removed the HIP label Oct 29, 2026
@aiter-bot

aiter-bot commented Oct 29, 2026 •

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

Revision 58a7b141c405 · mode shadow

Author self-review accepted. Readiness report:

Author answers

  • PASS — current-SHA answers are present and structurally specific
Show the author's self-review answers

Behavior and intent

No kernel or library behavior changes: after merging `main`, `csrc/include/moe_sorting_opus.h` is `main`'s version (the #7085 fix) and the diff against `main` is only `op_tests/test_moe_sorting.py`. The PR adds a regression test because nothing in `main` fails if the multi-phase sorting kernels start loading `topk_ids` past 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 at `58a7b141c`:
- `python3 -c "import test_moe_sorting as t; t.test_moe_sorting_local_tokens_tail_read()"` in `op_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.
- Negative control on `main` `004245be3` (before #7085): the reproducer failed 3/3 with `hipErrorIllegalAddress`.
- black and ruff 0.16.0 pass.

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_read` fails with a GPU memory fault (`hipErrorIllegalAddress` / `Memory access fault`) in `MoeSortingMultiPhaseKernel_P0_v1` / `_P0_v2` / `_P01`, because `topk_ids` sits 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 in `test_moe_sorting.py` run under the caching allocator, where the stray read does not fault and the extra ids are dropped. The test added with #7085 (`_check_padding_sentinels` in `test_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 past `out` (the other bug #7085 fixed), and its `topk_ids` also live under the caching allocator, so a read past them would not fault there either.

Changed op tests

`op_tests/test_moe_sorting.py`: adds `test_moe_sorting_local_tokens_tail_read`, which places `topk_ids` at the very end of a device allocation followed by an unmapped page and sorts it with `num_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 `-1` rows. It is not redundant for the reasons above: it is the only test where a read past `topk_ids` faults instead of passing silently.

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply
  • ADVISORY — Copilot did not review the current head SHA

Code and op-test hygiene

  • PASS — no committed artifact or conflict marker detected

Required CI

  • PASS — Black: passed
  • PASS — Ruff: passed
  • PASS — Repository dependency: passed
  • PASS — Aiter Test Gate: passed

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

@ZLkanyo009 ZLkanyo009 changed the title [Bugfix] Bound topk_ids loads in multi-phase MoE sorting with num_local_tokens [Test] Regression test for topk_ids tail reads in multi-phase MoE sorting Oct 29, 2026
@ZLkanyo009

Copy link
Copy Markdown
Contributor Author

@yzhou103 Conflicts resolved: merged main into the branch (merge commit 58a7b141c, a plain merge, no force push).

  • Kernel side: csrc/include/moe_sorting_opus.h is now exactly main's version. [HIP] [Bugfix] Keep MX quant-sort and multi-phase MoE sort in bounds under num_local_tokens #7085 fixed the same three multi-phase kernels (impl::moe_sorting_load_topk_vec), bounded by the topk_ids tensor height. git diff origin/main -- csrc/ is empty.
  • What the PR adds now: only the reproducer test_moe_sorting_local_tokens_tail_read in op_tests/test_moe_sorting.py (+103 lines). main has no test that fails if this tail read comes back. Under the caching allocator the stray read hits a neighbouring allocation, so the existing counted / uncounted tests pass even on the unfixed kernels. The test that came with [HIP] [Bugfix] Keep MX quant-sort and multi-phase MoE sort in bounds under num_local_tokens #7085 in test_moe_sorting_mxfp4.py checks the quant-sort sentinel write, not this read. This test places topk_ids right before an unmapped page and runs the sort in a subprocess, so the stray read faults (hipErrorIllegalAddress) instead of passing silently.
  • Validation (MI355X / gfx950, rocm/atom-dev:nightly_202610221638, aiter JIT-built from 58a7b141c):
  • Title and description are updated to match. The title is now [Test] Regression test for topk_ids tail reads in multi-phase MoE sorting, and the description has the PR Self-Review section.

cc @valarLip

@ZLkanyo009

Copy link
Copy Markdown
Contributor Author

@yzhou103 Thanks for asking to keep this. On our side we are closing it, since the fix itself is fully in main through #7085.

We confirmed that end to end on MI355X. With aiter main (with #7085) and without this PR, Qwen3.8-Flash-Next ran c32 / c48 / c64 with LMCache offload for 1 hour each, with 0 memory access faults and 0 request errors. That load used to fault within an hour before the fix.

All that is left here is the regression test (topk_ids ending right before an unmapped page). It passes 5/5 on main and fails 3/3 before #7085. If you would like to keep that test in main, feel free to reopen this PR, or I can open a small test-only PR. Thanks @valarLip as well.

@ZLkanyo009 ZLkanyo009 closed this Oct 30, 2026
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.

3 participants