Repository navigation
[FlyDSL] Fix LDS race in MoE sorting p23 kernel - #7349
Conversation
In p23_kernel every wave reads my_start/my_end = cumsum[my_expert(+1)] from LDS and then Step 3 overwrites cumsum[tid] = local_idx. Without an expert mask there is no barrier between the two, so a lagging wave can read its already-overwritten boundaries (my_start == my_expert), write sorted_expert_ids/sorted_token_ids at the wrong offsets and drop entries. With the mask, the mask prefix sum's barriers happened to hide this. Add a workgroup barrier before the Step 3 store. 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: |
WAITING ON AUTHORPR Self-ReviewRevision Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision. Questions to answer in the PR description
Edit the Missing or invalid answers
Shadow mode reports what would block but keeps the status successful. Set |
Review: LGTM, tested on MI300X (gfx942)I tested this PR on 8× MI300X. The fix is correct. I could reproduce the bug without the barrier, and the barrier removed every failure. What I checked
Test resultsI poisoned the output buffers, compared against a reference, and ran a GEMM and a VALU kernel on a second stream to create contention.
Performance (CUDA-graph replay of 100 sorts, interleaved A/B, 9 rounds):
Tested on MI300X only (MI250X not tested). Two things I noticed
Suggestions (not blocking)
|
Regression test for the p23_kernel LDS race: 200 FlyDSL sorts at token=9000, E=384, topk=6 with a GEMM and VALU kernels on a side stream, outputs poisoned before every sort and checked against the torch reference. On MI355X the kernel without the barrier fails 25-41 of 200 sorts in every run; with it, 0. Co-authored-by: Claude Opus 5.5 <[email protected]> Signed-off-by: Carl Persson <[email protected]>
|
Thanks for the review and for testing on MI300X @msaffari-amd!
|
…on test Also run every requested iteration: the last batch may be partial, so iters below or not a multiple of the batch size are no longer dropped. Co-authored-by: Claude Opus 5.5 <[email protected]> Signed-off-by: Carl Persson <[email protected]>
|
@valar-cop review |
|
conflict with main, pls solve first |
# Conflicts: # op_tests/test_moe_sorting.py
|
Merged main and resolved the test_moe_sorting.py conflict with #8316. The contention test now runs after the FlyDSL/Opus CUDA-graph loop, and the full test passes on MI355X. |
…call flydsl_moe_sorting_fwd kept the multiphase sort workspace in a module-level _workspace_cache keyed only by device and passed the same buffer to every call. Token counts above the oneshot limit (e.g. M >= 17 for E=384, topk=6) write their expert mesh and per-expert counts there. Graphs captured while the same cached allocation is reused record the same scratch address. Two such graphs replayed at the same time on different streams overwrite each other's scratch, and sorted_ids, sorted_weights, sorted_expert_ids and num_valid_ids come back wrong although each graph's inputs are valid. Allocate the workspace with torch.empty on every call, as _moe_sorting_impl already does for the Opus workspace (ROCm#3047). Each call writes every workspace word it uses before reading it, so nothing depends on an earlier call. Under capture the buffer comes from the capturing graph's memory pool; in eager mode the caching allocator keeps reuse stream-ordered. Kernels, launch arguments, outputs and the public signature are unchanged. The p23 LDS barrier that the concurrent regression also exercises is on main via ROCm#7349 (4fb090d). This commit is rebased onto that main and carries only the workspace change; its companion commit is dropped. Tests: - tests/test_flydsl_moe_sorting_workspace.py (CPU): two multiphase calls must receive distinct workspaces, oneshot calls none. Fails before this change, passes after. - op_tests/test_moe_sorting.py: test_moe_sorting_flydsl_concurrent_graph_replay captures eight FlyDSL sorts in each of two graphs and replays both on two streams. Each attempt queues both graphs behind one gate event that a spin kernel keeps pending; an attempt counts only if the gate is still pending after both launches, i.e. neither graph could start before the other was queued. Ten such attempts are required within twenty (the spin doubles after an unconfirmed attempt, up to 2^24 cycles); otherwise the test raises RuntimeError (precondition not confirmed) instead of passing. Every sort must match the torch reference exactly (checkAllclose with rtol=0, atol=0), including weight 0 in padding slots. Oneshot shapes (no workspace) are skipped. An earlier version of this test without the gate passed on the unpatched wrapper on gfx950 while a standalone reproducer showed failures on the same GPU. Signed-off-by: Yaakoub El Khamra <[email protected]>
Summary
In
p23_kernel, every wave readsmy_start/my_end = cumsum[my_expert(+1)]from LDS. Step 3 then overwritescumsum[tid] = local_idx. Without an expert mask there is no barrier between the two. A lagging wave can then read already-overwritten boundaries (my_start == my_expert) and writesorted_expert_ids/sorted_token_idsat the wrong offsets. That drops entries, which leaves MoE output rows uninitialized (NaN), and can fill whole ranges ofsorted_expert_idswith the wrong expert id. With a mask, the barriers in the mask prefix sum hide the race.p23_kernelis the last kernel of both multiphase paths, so every FlyDSL sort above the oneshot limit (16 tokens for E=384) is affected: the 2-kernel path up to 2048 tokens and the 4-kernel path above it.It shows up when other kernels run on the same CUs as the sort. In vLLM, DeepSeek-V4.1-Flash TP4 with speculative decoding and the mHC/shared-expert overlap gave thousands of NaN decode steps in 5 minutes at c16 (
AITER_USE_FLYDSL_MOE_SORTING=1, M=96, E=384). With this barrier: 0 NaN steps and 0 sort mismatches.Changes
aiter/ops/flydsl/kernels/moe_sorting_kernel.py: add agpu.barrier()before the Step 3cumsum[tid]store inp23_kernel. The output is unchanged except that the race is gone.op_tests/test_moe_sorting.py:test_moe_sorting_flydsl_under_contention, a regression test (see below).Performance
FlyDSL sort, CUDA-graph replay of 100 sorts, same GPU, mean of 2 runs:
@msaffari-amd measured the same on MI300X (within ±0.25%).
Testing
Documentation
Dependencies
Breaking Changes
None
PR Self-Review
Reviewed revision
9f8a834
Behavior and intent
The FlyDSL multiphase sort (
p23_kernel, no expert mask, any M above the oneshot limit) no longer reads its expert's LDS boundaries after another wave has overwritten them. Before this change, the output was only correct if every wave in the workgroup finished readingcumsum[my_expert(+1)]before any wave did the Step 3 store. Under concurrent load a lagging wave could seemy_start == my_expertand writesorted_expert_ids/sorted_token_idsat the wrong offsets. That dropped entries, which left MoE output rows uninitialized (NaN in vLLM), and could give blocks the wrong expert id.Validation and unverified scope
test_moe_sorting_flydsl_under_contentionon MI355X: without the barrier, 25–41 of 200 sorts are wrong in every one of 18 runs on 8 GPUs (some with wrong expert ids). With it, 0 in every run.python3 op_tests/test_moe_sorting.py -e 32 256 40 385 384 -t 5 8 6 7 6on MI355X (gfx950): all 482 cases pass with FlyDSL error 0. This covers with and without the expert mask, padding,valid/all-empty/mixedrouting, CUDA-graph capture/replay, and E up to 385.s_barrier.PR-specific question
The main risk of the new barrier is a hang, which would happen if some threads in the workgroup didn't reach it. It sits inside
if is_sort_block:, which depends only onbidand so is uniform across the workgroup. It's at the same nesting level as the existing barriers before it (after the prefix sum) and after it (beforemy_local_idx), and the only divergent branch before it (bid == 0 & tid == 0, thenum_valid_idsstore) has already closed. The zero-fill blocks never enter this branch. Evidence: the fulltest_moe_sorting.pymatrix above, including graph replay, and the vLLM runs complete with no hangs. The other risk is extra latency, which the benchmark table covers.Changed op tests
op_tests/test_moe_sorting.py::test_moe_sorting_flydsl_under_contentionruns 200 FlyDSL sorts (token=9000, E=384, topk=6) while a GEMM and VALU kernels run on a side stream. Outputs are poisoned before every sort.sorted_ids,sorted_weights,sorted_expert_idsandnum_tokens_post_paddedare compared against the torch reference, andmoe_bufmust come back zeroed. The other sorting tests run the sort alone, and they pass with and without the barrier. This one fails on the old kernel in every run on MI355X. It takes about 2 s.AI assistance
Developed with AI assistance (Claude). I reviewed every change and ran the tests and benchmarks on MI355X.