Skip to content

[HIP] [Perf] Route gfx950 Opus MoE sort auto dispatch to multi-phase for E=384 decode - #7359

Merged
zufayu merged 1 commit into
ROCm:mainfrom
yelkhamra:amd-automation/opus-moe-sort-e384-multiphase
Nov 4, 2026
Merged

zufayu merged 1 commit into
ROCm:mainfrom
yelkhamra:amd-automation/opus-moe-sort-e384-multiphase

Conversation

@yelkhamra

@yelkhamra yelkhamra commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #7358

Base: main at 016a3efb695ebf15ba132422080e7dd4f1fc2831, one commit, now rebased onto main at 6a4fa17deb060989f12ec10c366775034defe5f2 (after #7134 merged; the prefer_mp and boundary-test conflict was resolved by keeping #7134's E=128..255, M>=17 term and its test expectations and applying this change on top). GPU results below come from the earlier base and were not re-run on the new base.

Summary

Lower the gfx950 Opus MoE-sort auto-dispatch expert bound from >= 385 to >= 384. This changes E=384 at M<8, including M=0, from one-shot to multi-phase; forced policies and other architectures retain their existing dispatch.

The comparison below, on gfx950 with the base sources, found the multi-phase sort faster than the one-shot sort that auto dispatch currently selects for E=384 at M=1, 2, 4 and 7, for both plain auto dispatch and auxiliary outputs: E=384 sits on the multi-phase side of the crossover that #5987 established for E=385. Correctness checks passed on both builds, including dirty-memory and captured-graph checks at M=0. Our GPU validation covers gfx950 only; gfx942/gfx1250 coverage and the mixed-architecture subtest are left to upstream CI and are not validated by us. A command to measure the crossover yourself is under Reproducer.

Motivation

Problem

#5987 routes gfx950 auto dispatch to multi-phase at E>=385, or at E>=256 with M>=8. Its review added E=384 to the boundary test with the E=256/257 token expectations, so the base test requires one-shot at E=384, M=1 and M=7. The reported crossover timings in #5987 cover E=256 and E=385, not E=384.

Root cause

At E=384 and M<8, the LDS rule permits one-shot and no branch of num_experts_ >= 385 || (num_experts_ >= 256 && tokens_ >= 8) || (num_experts_ >= 128 && tokens_ >= 17) selects multi-phase. The measured E=384 crossover therefore falls immediately below the expert bound.

Evidence

Build provenance. BASE uses the MoE-sort sources at 016a3efb695ebf15ba132422080e7dd4f1fc2831; PATCH applies the change below. The diagnostic runtime checkout was 78f416704fa989cc9db3765bcdccc10f89e8afb8: the six pinned source files match the earlier base 016a3efb apart from the patched header and test, and the four commits between that checkout and the base change only Triton code, tests and configurations. Correctness and timing runs used the same image and reproduced identical built-library bytes for each arm.

Gfx950 correctness. On PATCH, the affected-shape op-test command exited 0, including the added E=384 auxiliary-output and local-ID cases; its only skip was the architecture-switch subtest requiring gfx950 and non-gfx950 devices in one process. The patched test on BASE failed at the expected E=384, M=1 assertion; BASE with its own test exited 0.

The separate exact-reference check returned GPU_CHECK PASS for BASE and PATCH, 26 rows each: plain and aux paths at M=0..8, and plain with num_local_tokens at static capacities 1..8. Every row completed 20 dirty-memory eager trials and 100 graph replays without a mismatch; captured outputs were poisoned before every replay. Both E=384, M=0 modes passed on both builds, with BASE one-shot and PATCH multi-phase.

The comparator checks both valid-count entries, sorted IDs including padding, real-route weights, used expert blocks, capacities, real moe_buf rows and auxiliary mappings. With num_local_tokens, capacities follow the static shape while the padding sentinel follows the real count loaded by the kernels. Allocation tails are excluded.

The gfx950 default regression matrix exited 0 on BASE and PATCH. Real gfx942 correctness/regression and the mixed-architecture subtest were not run.

Changes

Fix

-    // gfx950 crossover measured with E=128..255 at M>=17, E=256/257 at M>=8 and E=385 at all M.
-    bool prefer_mp = num_experts_ >= 385 || (num_experts_ >= 256 && tokens_ >= 8) ||
+    // gfx950 crossover measured with E=128..255 at M>=17, E=256/257 at M>=8,
+    // E=384 at M<8 and E=385 at all M.
+    bool prefer_mp = num_experts_ >= 384 || (num_experts_ >= 256 && tokens_ >= 8) ||
                      (num_experts_ >= 128 && tokens_ >= 17);

The host test now expects multi-phase at E=384 for every listed M, adds E=383 as the lower boundary case, and asserts E=384 remains one-shot off gfx950. The auxiliary-output and local-ID tests each add (1, 384, 6). The bound is not generalized to lower expert counts.

Why it works

Changing the expert bound routes the measured E=384 small-token cases through the existing multi-phase implementation. Python sizes workspace using topk_ids.get_device(), and launch dispatch uses the same workspace predicate under HipDeviceGuard, preserving allocation/launch agreement.

Reproducer

The crossover can be measured with the same op test that #5987 used as its reproducer. On a BASE build and on a PATCH build (gfx950):

python3 op_tests/test_moe_sorting.py -d bf16 -m 1 2 4 7 8 -e 384 -t 6 -md 7168 -dp 0 1 2 -p 0 -em f -accum t -rc valid

-dp 0 is auto dispatch, -dp 1 forces the one-shot kernel and -dp 2 forces multi-phase (csrc/include/moe_sorting_opus.h, comment above the workspace predicate). Compare the opus us column of the summary table: on BASE, -dp 0 should track -dp 1 at M<8; on PATCH it should track -dp 2, which is the faster of the two forced policies at these M. At M=8 all builds already pick multi-phase. The op test's default profile budget is short (run_perftest(..., num_warmup=1, num_iters=2)), so repeat the command several times or raise that budget locally before reading small differences.

Performance (if applicable)

Kernel timing on gfx950. One gfx950 GPU, HIP 7.2.53211, PyTorch 2.10.0+rocm7.2.4.git3d3aa833; E=384, top-6, block_m=32, model_dim=7168, bf16 moe_buf, Opus backend. BASE and PATCH repeats alternated within one job with fresh routes per repeat; each measurement replays a CUDA graph of repeated sort calls.

  • At M=1, 2, 4 and 7, PATCH plain auto (dispatch_policy=0) and PATCH auxiliary-output auto were each faster than the matching BASE arm. PATCH plain auto selects the same multi-phase implementation as forced policy dispatch_policy=2.
  • Controls: at M=8, both builds select multi-phase. Forced policies 1 and 2 select the same implementations before and after the change.
  • Workspace queries report one-shot for BASE auto at M<8 and multi-phase for PATCH auto; at M=8 both select multi-phase.
  • Limits: one GPU, kernel time only. Continuous exclusive use of the GPU during the run was not established, and no locked-clock claim is made. This is not a throughput or end-to-end claim.

In our experiments on the same single GPU the multi-phase sort was faster per call at these token counts, well outside the repeat-to-repeat spread of either build.
This is kernel time only and not a throughput or end-to-end result.

Testing

Tests

CPU rerun: the real BASE/PATCH headers were compiled with hipcc -x hip --cuda-host-only -std=c++20; fake gfx950/gfx942 device queries let the extracted upstream host-test bodies run without a GPU. The expected negative controls below demonstrate that the boundary test distinguishes the old and new selectors.

patched-test x patched-header gfx950 test_moe_sorting_opus_host_dispatch_boundaries: PASS
patched-test x patched-header gfx942 test_moe_sorting_opus_host_dispatch_boundaries: PASS
patched-test x base-header gfx950 test_moe_sorting_opus_host_dispatch_boundaries: FAIL (unexpected auto dispatch for E=384, M=1)
base-test x base-header gfx950 test_moe_sorting_opus_host_dispatch_boundaries: PASS
base-test x patched-header gfx950 test_moe_sorting_opus_host_dispatch_boundaries: FAIL (unexpected auto dispatch for E=384, M=1)
auto-dispatch changes base->patched (E=1..1024, M=0..64, gfx950+gfx942): 8 cases: [(384, 0)] M = [0, 1, 2, 3, 4, 5, 6, 7]
E=384 lds_oneshot M=0..8: [1, 1, 1, 1, 1, 1, 1, 1, 1] | forced policy 1/2 unchanged: True

The explicit-device and architecture-switch host tests pass in all four test/header combinations on both fake devices. Those CPU simulations do not validate real mixed-architecture GPU execution.

The comparator self-test, source/oracle audit and dispatch probe were rerun on CPU:

CPU_SELFTEST PASS
AUDIT_ROUND3 PASS
probe_round2: PASS
REVIEW_CHECKS PASS

Python compilation and git diff --check pass. The timing summarizer was also checked on synthetic CPU input. GPU results above come from the recorded diagnostics; this documentation revision ran CPU checks only.

Black/Ruff re-check done. Black 26.10.1 and Ruff 0.16.0 passed on the changed Python file, op_tests/test_moe_sorting.py, in a fresh clone at 016a3efb with this patch applied. Exact recorded output:

changed py: op_tests/test_moe_sorting.py
All done! ✨ 🍰 ✨
1 file would be left unchanged.
All checks passed!

On the branch rebased onto 6a4fa17d, Black and Ruff 0.16.0 are clean on op_tests/test_moe_sorting.py, and clang-format (repository .clang-format) reports no change on the changed lines of csrc/include/moe_sorting_opus.h.

GPU validation plan

Done on gfx950: affected-shape tests except the architecture-switch subtest, negative/positive controls, exact-reference dirty-memory and graph-replay checks including M=0 and local-token counts, the default regression matrix, and the kernel timing comparison above.

Not validated by us: the diagnostic records leave these items open:

  • Exact-reference correctness on gfx942.
  • Default op-test regression on gfx942 for BASE and PATCH.
  • The architecture-switch subtest with gfx950 and non-gfx950 devices in one process.
  • Optional end-to-end validation; no study or throughput result is claimed.

We have no gfx942/gfx1250 hardware. Coverage on those architectures and execution of the mixed-architecture subtest are left to upstream CI; they are not prerequisites to filing this PR. A skipped subtest does not establish coverage.

Documentation

No documentation change.

Dependencies

None.

Breaking Changes

Risk / compatibility

#7134 (merged) edits the same prefer_mp predicate, adding an E=128..255, M>=17 term, and op_tests/test_moe_sorting.py; this branch is rebased on it. The dispatch changes affect different cases: this PR changes only E=384 at M<8 (E=384 at M>=8 was already multi-phase).

Affected auto-dispatch calls now need multi-phase workspace. Multi-phase launches more kernels than one-shot, so kernel-time savings alone do not establish an application-level benefit. The measured correctness coverage is gfx950 only; gfx942/gfx1250 and mixed-device correctness remain unvalidated by us and are left to upstream CI.

PR Self-Review

Reviewed revision

b28370021910

Behavior and intent

On gfx950, auto dispatch (dispatch_policy=0) of the Opus MoE sort now selects the existing multi-phase sort instead of one-shot at E=384 for M<8, including M=0, by lowering the expert bound from >= 385 to >= 384. Forced policies, other architectures and every other (E, M) keep their current kernel.

Validation and unverified scope

Validated on CPU: the real base and patched headers compiled host-only with hipcc and run with fake gfx950/gfx942 device queries pass the host tests, the expected negative controls fail at E=384, M=1, and the dispatch scan finds changes only at E=384, M=0..7. Validated on one gfx950 GPU at the earlier base: the affected-shape op test exited 0, the exact-reference check returned GPU_CHECK PASS for 26 rows on both builds, the default regression matrix exited 0, and the kernel timing comparison under Performance was run. On the rebased branch Black, Ruff 0.16.0 and clang-format are clean. Unverified: exact-reference correctness and default regression on gfx942, gfx1250, the mixed-architecture subtest, end-to-end throughput, and a GPU re-run on the rebased branch.

PR-specific question

The changed predicate in moe_sorting_auto_is_oneshot takes only the token count, expert count and device; dtype does not enter it. It is evaluated only for auto dispatch, only after the existing LDS rule already permits one-shot, and only on gfx950 (moe_sorting_device_is_gfx950); other architectures and forced policies 1 and 2 are unchanged. The only newly selected inputs are E=384 at M<8 (M=0..7): E>=385 at every M and E>=256 at M>=8 were already multi-phase, and the CPU scan of E=1..1024, M=0..64 on gfx950 and gfx942 found no other change. The bound is not generalized to lower expert counts. Allocation and launch agree because Python sizes the workspace with topk_ids.get_device() and launch dispatch uses the same workspace predicate under HipDeviceGuard; this PR does not change the multi-phase implementation, which is the one forced dispatch_policy=2 already selects. The GPU checks used a bf16 moe_buf, top-6, block_m 32 and model_dim 7168.

Changed op tests

  • op_tests/test_moe_sorting.py: the host dispatch-boundary test now expects multi-phase at E=384 for every listed M on gfx950, adds E=383 as the lower boundary case, and asserts E=384 remains one-shot off gfx950; the auxiliary-output and local-ID tests each add (1, 384, 6) to cover the newly multi-phase auto route. The base test required one-shot at E=384, so the patched test fails on the base header at E=384, M=1.

Credit

The gfx950 crossover policy comes from #5987.

@github-actions

github-actions Bot commented Nov 2, 2026

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 7359 --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.

@aiter-bot

aiter-bot commented Nov 2, 2026 •

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

Revision b28370021910 · 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

On gfx950, auto dispatch (`dispatch_policy=0`) of the Opus MoE sort now selects the existing multi-phase sort instead of one-shot at E=384 for M<8, including M=0, by lowering the expert bound from `>= 385` to `>= 384`. Forced policies, other architectures and every other (E, M) keep their current kernel.

Validation and unverified scope

Validated on CPU: the real base and patched headers compiled host-only with `hipcc` and run with fake gfx950/gfx942 device queries pass the host tests, the expected negative controls fail at E=384, M=1, and the dispatch scan finds changes only at E=384, M=0..7. Validated on one gfx950 GPU at the earlier base: the affected-shape op test exited 0, the exact-reference check returned `GPU_CHECK PASS` for 26 rows on both builds, the default regression matrix exited 0, and the kernel timing comparison under Performance was run. On the rebased branch Black, Ruff `0.16.0` and clang-format are clean. Unverified: exact-reference correctness and default regression on gfx942, gfx1250, the mixed-architecture subtest, end-to-end throughput, and a GPU re-run on the rebased branch.

PR-specific question

The changed predicate in `moe_sorting_auto_is_oneshot` takes only the token count, expert count and device; dtype does not enter it. It is evaluated only for auto dispatch, only after the existing LDS rule already permits one-shot, and only on gfx950 (`moe_sorting_device_is_gfx950`); other architectures and forced policies 1 and 2 are unchanged. The only newly selected inputs are E=384 at M<8 (M=0..7): E>=385 at every M and E>=256 at M>=8 were already multi-phase, and the CPU scan of E=1..1024, M=0..64 on gfx950 and gfx942 found no other change. The bound is not generalized to lower expert counts. Allocation and launch agree because Python sizes the workspace with `topk_ids.get_device()` and launch dispatch uses the same workspace predicate under `HipDeviceGuard`; this PR does not change the multi-phase implementation, which is the one forced `dispatch_policy=2` already selects. The GPU checks used a bf16 `moe_buf`, top-6, block_m 32 and model_dim 7168.

Changed op tests

- `op_tests/test_moe_sorting.py`: the host dispatch-boundary test now expects multi-phase at E=384 for every listed M on gfx950, adds E=383 as the lower boundary case, and asserts E=384 remains one-shot off gfx950; the auxiliary-output and local-ID tests each add `(1, 384, 6)` to cover the newly multi-phase auto route. The base test required one-shot at E=384, so the patched test fails on the base header at E=384, M=1.

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.

@yelkhamra
yelkhamra marked this pull request as ready for review November 2, 2026 20:24
@yelkhamra
yelkhamra requested a review from a team November 2, 2026 20:24
@github-actions github-actions Bot added the HIP label Nov 2, 2026
@zufayu
zufayu self-requested a review November 3, 2026 01:13
@zufayu zufayu self-assigned this Nov 3, 2026
@zufayu

zufayu commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Logic LGTM. The gfx950 auto-dispatch change is narrowly scoped to E=384 with M<8, the boundary/correctness coverage is appropriate, and the supplied data supports the multi-phase selection without showing a regression outside the target range. Please rebase to resolve the current conflict and rerun the required Aiter Test Gate; approval/merge should wait for the updated head and green required CI.

…for E=384 decode

ROCm#5987 made gfx950 dispatch_policy=0 (auto) pick the multi-phase Opus MoE
sort for E>=385 at every token count, and for E>=256 from 8 tokens;
ROCm#7134 added E>=128 from 17 tokens. E=384 falls one expert below the
all-M bound, so it stays on the one-shot sort at M<8.
E=384 is DeepSeek-V4-Pro's routed-expert count (top-6) when the shared
expert is not fused into the routed MoE.

In a gfx950 diagnostic with E=384, top-6 and block_m 32 under CUDA-graph
timing, the forced multi-phase sort (dispatch_policy=2) is faster than
the one-shot sort that auto selects at M=1, 2, 4 and 7; at M=8 auto is
already multi-phase. Valid-region outputs were identical. To measure it,
run op_tests/test_moe_sorting.py -e 384 -t 6 -dp 0 1 2 at those token
counts.

Lower the expert bound from 385 to 384. Only gfx950 auto dispatch at
E=384 with M<8 changes; forced policies, other architectures and every
other (E, M) keep their current kernel.

Tests: E=384 now expects multi-phase at every listed token count in the
host boundary test, a new E=383 row keeps the E=256/257 expectations to
pin the new lower side, the non-gfx950 branch asserts E=384 stays
one-shot, and (1, 384, 6) is added to the auxiliary-output and local-ID
tests that check newly multi-phase auto routes.

Signed-off-by: Yaakoub El Khamra <[email protected]>
@yelkhamra
yelkhamra force-pushed the amd-automation/opus-moe-sort-e384-multiphase branch from a174710 to b283700 Compare November 3, 2026 12:53
@yelkhamra

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Rebased onto main at 6a4fa17d (after #7134 merged). The conflict in prefer_mp and the boundary test was resolved by keeping #7134's E=128..255, M>=17 term and its test expectations and applying the E=384 change on top; the only auto-dispatch change remains E=384 at M<8. New head b2837002; Black, Ruff and clang-format are clean, and the required checks are re-running on it.

@yelkhamra

yelkhamra commented Nov 3, 2026 via email

Copy link
Copy Markdown
Contributor Author

@zufayu
zufayu merged commit 4e485bb into ROCm:main Nov 4, 2026
shantipriya-amd pushed a commit to shantipriya-amd/aiter that referenced this pull request Nov 18, 2026
…for E=384 decode (ROCm#7359)

ROCm#5987 made gfx950 dispatch_policy=0 (auto) pick the multi-phase Opus MoE
sort for E>=385 at every token count, and for E>=256 from 8 tokens;
ROCm#7134 added E>=128 from 17 tokens. E=384 falls one expert below the
all-M bound, so it stays on the one-shot sort at M<8.
E=384 is DeepSeek-V4-Pro's routed-expert count (top-6) when the shared
expert is not fused into the routed MoE.

In a gfx950 diagnostic with E=384, top-6 and block_m 32 under CUDA-graph
timing, the forced multi-phase sort (dispatch_policy=2) is faster than
the one-shot sort that auto selects at M=1, 2, 4 and 7; at M=8 auto is
already multi-phase. Valid-region outputs were identical. To measure it,
run op_tests/test_moe_sorting.py -e 384 -t 6 -dp 0 1 2 at those token
counts.

Lower the expert bound from 385 to 384. Only gfx950 auto dispatch at
E=384 with M<8 changes; forced policies, other architectures and every
other (E, M) keep their current kernel.

Tests: E=384 now expects multi-phase at every listed token count in the
host boundary test, a new E=383 row keeps the E=256/257 expectations to
pin the new lower side, the non-gfx950 branch asserts E=384 stays
one-shot, and (1, 384, 6) is added to the auxiliary-output and local-ID
tests that check newly multi-phase auto routes.

Signed-off-by: Yaakoub El Khamra <[email protected]>
Co-authored-by: Yaakoub El Khamra <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Issue]: gfx950 Opus MoE sort auto dispatch keeps E=384 small-token decode on the slower one-shot kernel

3 participants