Skip to content

fix(EPv2): map the gfx942 HF and VF device IDs to their models - #795

Merged
jhchouuu merged 3 commits into
ROCm:mainfrom
jeffdaily:tune/mi300x-flydsl-ep8
Nov 25, 2026
Merged

jhchouuu merged 3 commits into
ROCm:mainfrom
jeffdaily:tune/mi300x-flydsl-ep8

Conversation

@jeffdaily

@jeffdaily jeffdaily commented Oct 17, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

_DID_TO_MODEL maps only the base gfx942 device IDs. MI300X HF (0x74a9, 0x74bd), MI300X VF (0x74b5), MI325X VF (0x74b9) and MI308X HF (0x74a8) resolve to no model, so the flydsl EP v2 lookup skips the per-device schedules, including #1001's MI300X EP8 schedules, and runs the CU-scaled fallback.

Technical Details

utils.py: map those five IDs (from libdrm's amdgpu.ids) to mi300x, mi325x and mi308x. No table changes: the MI300X table this PR originally added is dropped in favor of #1001, and the >256-CU assert from #794 is fixed by #876.

Test Plan

4x MI300X (DID 0x74a1), with topology() reporting 0x74a9 through a wrapper outside the source tree: main resolves no model and uses the fallback (dispatch 304x16, combine 256x4); this branch resolves mi300x and #1001's schedule. test_tuning_configs.py, test_op.py (default, TUNED=1, fp8 TUNED=1), test_ep_backend_parity.py and test_graph_capture.py at 2 and 4 ranks, and the intranode test matrix at 4 ranks pass, the same as on main.

bench_ep.py, graph mode, bf16, hidden 7168, topk 8, 4 ranks, median of 6 rounds, dispatch+combine us:

tok/rank main (fallback) this PR (#1001 schedule) change
128 108.5 105.5 -2.8 %
1024 760.0 663.1 -12.8 %
4096 2915.7 2660.1 -8.8 %

Not run on real HF or VF hardware.

Authored with Claude.

On MI300X (gfx942, 304 CU) the flydsl EP v2 backend had no tuned geometry. lookup() fell back to _cu_scaled_default(), which sets combine_block_num to the CU count (304), and op construction asserted `combine block_num 304 exceeds xdb_flag_slots 256`. MI300X HF parts (PCI DID 0x74a9) were not in _DID_TO_MODEL either, so they never reached the MI300X entry at all.

Part of ROCm#794 (MI300X only; other untuned parts above 256 CUs still need the clamp). This adds 0x74a9 to _DID_TO_MODEL and a measured MI300X schedule: dispatch 128x8 (128x16 above 1024 tokens), combine 64x8 up to 128 tokens and 32x8 above. Every block_num is <= 128, well under both the CU count and xdb_flag_slots. The MI325X schedule was not aliased, because its combine geometry is 13-19 % slower here at >= 256 tokens.

How it was measured: 8x MI300X HF over xGMI, bench_ep.py with BACKENDS=flydsl COMB_MODE=pull MODES=graph, hidden 7168, topk 8, EPR 32, tokens 8..8192, bf16 and fp8 dispatch. Dispatch block {64..304} x warp {4,8,16} and combine block {32..256} x warp {2,4,8,16} were swept per leg. The minimum mean latency was picked per bucket, and the winners and runners-up were re-run twice (spread < 1 % from 128 tokens up). bf16 and fp8 pick the same geometry, so one schedule serves both.

EP8 bf16 dispatch+combine pair, us, graph mode, mean of 3 rounds:

| tok/rank | this table | MI325X schedule | CU fallback, combine capped 256 |
|---|---|---|---|
| 128 | 94 | 101 | 108 |
| 1024 | 556 | 584 | 788 |
| 4096 | 2048 | 2354 | 2975 |
| 8192 | 4039 | 4642 | 6020 |

Test plan (8x MI300X, gfx942, flydsl default backend, no geometry overrides):

```
torchrun --standalone --nproc_per_node={2,4,8} test_op.py                      # also TUNED=1, DTYPE=fp8 TUNED=1, HIDDEN=7168
torchrun --standalone --nproc_per_node={2,4,8} test_ep_backend_parity.py       # also HIDDEN=7168 TOPK=8 EPR=32
torchrun --standalone --nproc_per_node=8 test_graph_capture.py
pytest tests/python/ops/dispatch_combine_v2/test_dispatch_combine_v2_intranode.py   # 17 passed, 1 skipped (fp4, gfx950-only)
```

Authored with Claude.
@kawhil-amd
kawhil-amd requested review from jhchouuu and kawhil-amd and removed request for jhchouuu October 18, 2026 10:34
Comment thread python/mori/ops/utils.py
# MI350X/MI355X (both 256 CU) are only separable here, not by CU count.
_DID_TO_MODEL = {
0x74A1: "mi300x", # MI300X (gfx942, 304 CU)
0x74A9: "mi300x", # MI300X HF (gfx942, 304 CU)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the diff in the two MI300X version?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the slow reply. They are effectively the same part (gfx942, 304 CU), so they can share the MI300X table. I also mapped the other gfx942 IDs from libdrm's amdgpu.ids that had no model (the VFs and the second MI300X HF ID); the PR description has the list.

detect_model() only knew the base device IDs, so an MI300X VF (0x74b5), the second MI300X HF ID (0x74bd) and an MI325X VF (0x74b9) found no model and, with no gfx942 arch fallback, fell back to the CU-scaled flydsl default that asserts on 304-CU parts. MI308X HF (0x74a8) fell back too and got an untuned geometry. The IDs and names are from libdrm's amdgpu.ids. gfx950 VFs already reach the MI355X table through the arch fallback, so they are left alone.

Test: detect_model() with topology() stubbed returns mi300x for 0x74a1/0x74a9/0x74b5/0x74bd, mi325x for 0x74a5/0x74b9, mi308x for 0x74a2/0x74a8. Not run on VF hardware.

Authored with Claude.
@kawhil-amd

kawhil-amd commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

@jeffdaily Thanks for the contribution, and for including the sweep method and results in the table comment. Having a measured MI300X schedule instead of the CU-scaled fallback is useful. I read the diff only (I have no MI300X to run it on), and I have two comments.

1. The default no longer follows the CU count (tuning_configs.py, _MI300X_DEFAULT / _MI300X_SCHEDULE)

The module docstring says block_num must stay <= CU count. Before this PR, an MI300X went through _cu_scaled_default(), which sizes blocks from cu_count(). Now _MI300X_DEFAULT and every bucket of _MI300X_SCHEDULE use a fixed dispatch_block_num of 128, so the rule is no longer enforced.

The model is picked by PCI DID, and utils.py notes that the CU count shifts with compute partitioning (SPX/DPX/CPX) and CU masking. A partitioned MI300X keeps the same DID but exposes fewer CUs (for example 38 per partition in CPX), so 128 blocks would exceed the CU count there. I have not run a partitioned part, so I do not know whether it hangs or only slows down. Could you check, or add a clamp such as min(block, cu_count()) when the CU count is below the tuned one? If a fixed 128 is intended, a short note in the comment would help.

2. The comment and the first bucket disagree (tuning_configs.py, _MI300X_SCHEDULE)

The header comment says the gather combine wants block 32 / warp 8 "at every count but 65..128 tokens (64x8)". The first bucket is (128, 128, 8, 64, 8), which covers 8..128 tokens, so 8..64 tokens also get combine 64x8, which the comment says is not the best there. Either split the bucket at 64, for example (64, 128, 8, 32, 8) then (128, 128, 8, 64, 8), or fix the comment if 64x8 is also fine below 65 tokens.

laithsakka pushed a commit to laithsakka/pytorch that referenced this pull request Nov 23, 2026
…h#201225)

## Linked issue or supporting maintainer

Part of pytorch#184147

## BC-breaking?

No

## Summary (human written only)

There will certainly be follow-up work, but this is a verified initial implementation of TokenSwitch + MoRI.

## Agent details (if applicable)

My agent came up with this analysis, but I've read and approved it.

**What.** `TokenSwitchMoRI` implements the `TokenSwitch` contract from pytorch#198963 on MoRI EP v2 (https://github.com/ROCm/mori) with its flydsl kernel backend, intranode only. Both layouts are supported: flat directly, and expert_major built on MoRI's flat dispatch (slots ordered by local expert, then receive row; `max_expert_major_slots_per_rank` bounds the slot count). The first `dispatch` on a `Routing` computes the layout and later dispatches replay it.

**Backward.** Since pytorch#201642, backends implement `_combine_backward` and `_dispatch_backward` themselves, and dispatch backward also returns `topk_weights` gradients. `TokenSwitchMoRI`'s combine backward replays the MoRI dispatch of the gradients. Its dispatch backward combines the token gradients, and gives each source weight `(t, k)` the gradient at its receive slot on expert k's rank: every rank's slot gradients are all_gathered in fp32 and indexed with MoRI's dest map (a MoRI combine would round them to the token dtype).

**Changes to shared code.**

- `_CombineAutograd.backward` sizes its gradient buffer as `max(max_recv_tokens_per_rank, rows of the combine input)`; MoRI's expert_major input can have up to `max_expert_major_slots_per_rank` rows. NCCL inputs never exceed `max_recv_tokens_per_rank`, so NCCL is unchanged.
- In `test_token_switch.py`, the shared flat-layout mixin now holds pytorch#201642's weight-gradient tests (they get the backend from the test class instead of constructing `TokenSwitchNCCL`), so they run for MoRI too. Parametrized tests are expanded on the mixins that define them, which moves `TokenSwitchNCCLTest`'s two `explicit_rendezvous` tests into an NCCL-only mixin. `test_create_routing` checks the NCCL EP handle's receive count only for `TokenSwitchNCCL`, and `TokenSwitchMoRI.create_routing` raises the same `topk_idx` dtype `TypeError` as NCCL.

**MoRI workarounds, in the code with comments.**

- MoRI's routing handle aliases op-owned buffers that the next routing dispatch overwrites (ROCm/mori#715), so the adapter copies the dest map and receive count. ROCm/mori#733 adds `snapshot=True` for this; a TODO marks the copy for removal once a MoRI release includes it.
- MoRI's kernels synchronize ranks on entry only, so two dispatches (or two combines) in a row can race a fast peer's writes into the receive buffers. The adapter fences with an `all_reduce` only when the same op repeats; alternating dispatch and combine is already ordered.

**Known limitation.** On MI300X, MoRI's flydsl backend fails at op construction with default geometry (ROCm/mori#794, a MoRI tuning gap), so `TokenSwitchMoRI` currently works on MI355X but not MI300X. ROCm/mori#795 adds a tuned MI300X table.

**Tests.** `TokenSwitchMoRITest` runs the flat, expert_major and contract test suites plus MoRI-only tests (38 tests) on 2+ GPUs. It skips when `mori` with flydsl is not importable, which includes current CI images, the same way the NCCL EP tests skip without `USE_NCCL_EP`.

**Test Plan.** MI355X, 4 ranks and 2 ranks, torch nightly 2.16.0.dev20261117+rocm10.1 with this PR's `_token_switch.py` in place of the installed module, MoRI main e73ccc539 built from source, flydsl 0.5.0:

```
python test/distributed/test_token_switch.py -v
```

Each run: `TokenSwitchMoRITest` 38/38 passed, `TokenSwitchReferenceTest` 13/13 passed, NCCL EP classes skipped (no `USE_NCCL_EP` build). Lint (default linters and `lintrunner --take PYREFLY --all-files`) is clean. The NCCL side of the shared changes is covered only by NCCL CI.

Pull Request resolved: pytorch#201225
Approved by: https://github.com/d4l3k
ROCm#1001 added per-shape MI300X EP8 schedules and ROCm#876 clamps the untuned fallback, so this branch's MI300X table is superseded; tuning_configs.py now matches main. What remains is the _DID_TO_MODEL entries for the gfx942 HF and VF device IDs, without which those parts never reach the MI300X, MI325X or MI308X schedules.

Authored with Claude.
@jeffdaily jeffdaily changed the title fix(EPv2): add a tuned flydsl EP8 table for MI300X fix(EPv2): map the gfx942 HF and VF device IDs to their models Nov 24, 2026
@jeffdaily

Copy link
Copy Markdown
Collaborator Author

@kawhil-amd thanks for the review, and sorry for the slow reply. Since then #1001 added MI300X EP8 schedules covering more shapes and #876 clamps the fallback, so I dropped this PR's table in favor of #1001 (merged main in 16f3e4d). Both of your comments were on that table, so they no longer apply. For the record on the first bucket: in my sweep, combine 32x8 and 64x8 were within 0.1-0.7 us of each other from 8 to 64 tokens, and 64x8 was 7% faster at 128.

What's left is the device-ID map: HF and VF gfx942 parts still miss #1001's schedules on main. @jhchouuu could you take a look too, since this routes those parts to your tables?

@github-actions

Copy link
Copy Markdown
Contributor

GPU CI for 16f3e4d1 (run): changed areas: ops_shared

lane runs jobs
mori yes MI355X intranode, MI355X ROCm 7.14, MI355X JAX, MI355X internode
mi300x yes MI300X intranode, MI300X internode
umbp yes UMBP unit test
cco yes CCO unit test, CCO EPv2 single-node test, CCO internode
fused yes CCO fused GEMM test
build yes MoRI build (compile check)

Test groups (the steps that run in those jobs): ep, kiwi, umbp, epv2, fused.

Lanes follow the changed files. Adding ci:full re-runs CI with every lane, the full internode sweeps and every fused GEMM case; adding ci:mi300x re-runs it with the MI300X lanes added. /ci-rerun re-runs CI against the current main.

@jeffdaily

Copy link
Copy Markdown
Collaborator Author

@kawhil-amd @jhchouuu friendly ping: this is now just the five gfx942 device-ID map entries (MI300X HF/VF, MI325X VF, MI308X HF), so MI300X HF and VF hosts pick up the tuned EP8 schedules from #1001 instead of the untuned fallback. Could one of you take a look?

@jhchouuu jhchouuu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

sorry for the delay

@jhchouuu

Copy link
Copy Markdown
Collaborator

/ci-rerun

@github-actions

Copy link
Copy Markdown
Contributor

Re-running MoRI CI test against the current main.

@jhchouuu
jhchouuu merged commit c99c7d7 into ROCm:main Nov 25, 2026
29 of 35 checks passed
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