[Fix][FlyDSL] Handle remainder workgroups in MoE XCD swizzle - #4315
Open
Fangzhou-Ai wants to merge 1 commit into
Open
[Fix][FlyDSL] Handle remainder workgroups in MoE XCD swizzle#4315Fangzhou-Ai wants to merge 1 commit into
Fangzhou-Ai wants to merge 1 commit into
Conversation
Contributor
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
Fangzhou-Ai
force-pushed
the
afz/moe-xcd-remainder-fix
branch
from
July 21, 2026 06:33
f1acf6c to
141c6dc
Compare
Fangzhou-Ai
marked this pull request as ready for review
July 21, 2026 08:54
Fangzhou-Ai
force-pushed
the
afz/moe-xcd-remainder-fix
branch
from
July 28, 2026 21:06
4e0bde4 to
c6d6abd
Compare
Distribute remainder workgroups across XCD ranges so non-divisible grids remain a complete permutation. The previous floor-division mapping duplicated workgroups and omitted the tail of affected grids. Co-authored-by: OpenAI Codex <codex@openai.com> Signed-off-by: Fangzhou Ai <fangzhouai@gmail.com>
Fangzhou-Ai
force-pushed
the
afz/moe-xcd-remainder-fix
branch
from
July 28, 2026 21:21
c6d6abd to
f8fba02
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
28x399and28x400.How the mapping works
For a launch with
Wworkgroups andX = 8XCDs, define:q = W // Xandr = W % X;x = linear_id % X, the XCD lane used by the swizzle;j = linear_id // X, that workgroup's local ordinal on XCDx.Linear IDs are distributed round-robin, so XCDs
0..r-1receiveq + 1workgroups and the remaining XCDs receiveq. The previous mapping was:That formula implicitly reserves only
qlogical IDs for every XCD. Whenr != 0, the extra workgroup on each leading XCD overlaps the next XCD's range, producing duplicate logical tiles and leaving the finalrtiles unassigned.The corrected mapping is:
The term
x * q + min(x, r)is the prefix sum of the workgroup counts assigned to XCDs beforex. It gives each XCD a non-overlapping contiguous range:q + 1IDs whenx < r, otherwiseqIDs.For a compact example, let
W = 10andX = 4. Thenq = 2andr = 2:0, 4, 80, 1, 20, 1, 21, 5, 92, 3, 43, 4, 52, 64, 56, 73, 76, 78, 9The previous mapping duplicates IDs
2and4and never assigns8or9. The corrected mapping is a permutation of0..9.The affected kernel A/B below uses
X = 8and hasW = 19 * 28 = 532, soq = 66andr = 4. The previous XCD ranges overlap at66, 132, 198, 264and stop at527, omitting528..531. The corrected ranges start at0, 67, 134, 201, 268, 334, 400, 466, have sizes67, 67, 67, 67, 66, 66, 66, 66, and cover0..531exactly once.This remapped ID then enters the existing WGM tiling logic; the fix changes tile assignment, not GEMM arithmetic. It is bypassed when XCD swizzling is disabled. For divisible grids,
r = 0, so the corrected formula reduces exactly to the previous one. The correction is therefore relevant only to swizzled grids with a remainder.Scope
This is a standalone fix targeting
main. It has no prerequisites and nothing depends on it.Earlier revisions of this PR made it the base of a review chain with #4269 (FHMoE) and #4314 (fused-MoE tuning). That coupling has been removed: every route selected by #4314 uses XCD0, and the initial FHMoE enablement in #4269 is XCD-free, so neither PR needs this mapping correction to be reviewed or landed. #4269 and #4314 now form their own two-layer stack on
main, independent of this PR, and this PR can be reviewed and merged on its own in either order.Empirical kernel A/B
A controlled same-host A/B used identical deterministic A4W4 inputs, the DSV4 model dimension of 7168, 28 N tiles, 19 sorted M blocks, and an affected
19x28 = 532workgroup XCD4 grid (532 % 8 = 4). The expert count was reduced to fit alongside the running server; the XCD and WGM mapping is unchanged.Output versus the torch reference:
The corrected and release outputs differ by 9.235% relative L2 and are not bitwise equal. This demonstrates a kernel-output correction on a real routed workload, although robust end-to-end release evaluations may not frequently exercise affected grids or may tolerate their localized error.
Performance used 50 warmups followed by 10 repeats of 200 HIP-event-timed iterations:
The correction does not provide a performance gain in this replay: it is approximately 1.0% slower by mean and 1.3% slower by median. It is proposed as a correctness prerequisite, not as a performance optimization by itself.
The A/B used the same patch, now at
f8fba0211after a rebase ontomainat4a1cc773f. The rebase changed only the parent and the import position of the newmoe_commonhelper; intervening upstream changes do not touch the swizzle arithmetic.Unit validation
Result on
141c6dcda, whose swizzle logic is unchanged at the current headf8fba0211: 2 passed, 258 subtests passed.Static checks passed:
Duplicate-work check
Open-PR searches found no separate equivalent fix.
AI assistance
OpenAI Codex was used to audit the mapping, build and run the kernel A/B, run tests, organize the stacked history, and draft this description. The human submitter reviewed the change and remains responsible for understanding and defending it end-to-end.