Repository navigation
[FlyDSL] [Bugfix] MoE stage 1: wait for the A LDS-DMA before the half-step barrier - #8550
Conversation
…-step barrier The stage-1 main loop waited vmcnt(tile_m//32//fp4_ratio + B loads) before the barrier that hands the A LDS buffer to ds_read. On gfx950 that leaves the previous half's A LDS-DMA racing the other waves' ds_read: the DSv4.1 TP4 a8w4 kernel (t32x128x256 gui fp8) wrote garbage into 4-row x 16-column output blocks of rows 16-31 on every few calls, and two runs on the same input disagreed in 4-26 rows. Leave only the B loads in flight. Co-authored-by: Claude Opus 5.5 <[email protected]> Signed-off-by: Carl Persson <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
…DMA before the half-step barrier) Tested build: ROCm#8550's hunk on the builder of the fused-launch commit (result md5 68ee602a). Not our change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The LDS-DMA wait fix removed the last use of fp4_ratio (ruff F841), and the shortened vmcnt expression now fits on one line. Co-authored-by: Claude Opus 5.5 <[email protected]> Signed-off-by: Carl Persson <[email protected]>
READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
|
The wait-count change is correct, but the root cause in the comment and description doesn't hold. In the base ISA for t32x128x256_w2_gui_fp8, the second half of every K iteration issues DMA, scale, scale, DMA, B×4: LLVM schedules the scale buffer_load_dwords between the two A LDS-DMA instructions, because they share a scheduling region. So vmcnt(5) leaves the second DMA (rows 16–31 of the tile) in flight. On main every bad row is at offset 16–31, never 0–15. Adding only rocdl.sched_barrier(0) after prefetch_x_to_lds and keeping vmcnt(5) gives 0/60 for w2/w3/w4, versus 60/60 on main. #7158's reporter also retracted the cross-wave visibility microbenchmark. Please reword the comment to state the real invariant: B loads sit in later phases behind sched_barrier(0), so they are always younger than the DMA, while scale loads may be reordered with it. Also, the fix covers more than t32: a static ISA check of all 182 shipped fp8/fp4-A stage-1 configs finds 51 unsafe on main and 0 with this PR. t64x128x256_w3_gui_fp8 (K=7168, I=768) reproduces at 2/200 on main and 0/200 with this PR, so the tile_m=64 part is needed. |
The scale loads share the A LDS-DMA's scheduling region, and LLVM may place them between the two DMAs, so counting them in vmcnt can leave a DMA in flight. Only the B loads, issued behind sched_barrier(0) in later phases, are guaranteed to be younger. Reword the comment accordingly; no code change. Co-authored-by: Claude Opus 5.5 <[email protected]> Signed-off-by: Carl Persson <[email protected]>
|
Thanks for the review @Bernard-Liu! That's the right root cause and I've updated the comment and the description to match. I confirmed the order in the ISA of I also reproduced the tile_m=64 case: I kept the vmcnt change rather than the |
Summary
Fixes #7158. In the FlyDSL MoE stage-1 main loop, the wait before the
half-step barrier could leave the previous half's second A LDS-DMA in flight,
so
ds_readpicked up stale A rows. This corrupted rows 16–31 of a 32-rowsorted block and made stage 1 nondeterministic. The wait now leaves only the
previous half's B loads in flight.
Motivation
Before the barrier that hands the A LDS buffer to
ds_read, stage 1 waitedvmcnt(tile_m // 32 // fp4_ratio + B loads). That count assumes the scaleloads are issued after both A DMAs, which is not guaranteed: the DMAs and the
phase-0 scale
buffer_load_dwords share a scheduling region, and LLVM mayplace the scale loads between the two DMAs. In the main-branch ISA of
t32x128x256_w2_gui_fp8(hidden 5120), the last two half-steps before thetail issue
DMA, scale, scale, DMA, B x4, sovmcnt(5)leaves the second DMA(A rows 16–31) in flight. The result is near-full-scale garbage in 4-row x
16-column patches, always at offsets 16–31 of the block, at different rows on
every run.
Only the B loads are guaranteed to be younger than the DMA: they are issued
in phases >= 1, each behind
sched_barrier(0). Thanks to @Bernard-Liu for theISA analysis.
This hits the shipped DeepSeek V4.1 a8w4 stage-1 kernels
(
flydsl_moe1_afp8_wfp4_bf16_t32x*_gui_fp8, tuned rows at 1–16 and256–1024+ tokens) and more: @Bernard-Liu's static ISA check of the 182 shipped
FP8/FP4-activation stage-1 configs finds 51 unsafe on main and 0 with this PR,
including tile_m=64 kernels.
Changes
aiter/ops/flydsl/kernels/mixed_moe_gemm_2stage_common.py: the wait beforethe half-step barrier drops the
tile_m // 32 // fp4_ratioterm, so it nolonger counts the scale loads (vmcnt 5 -> 4 for
t32x128x256_w{2,3,4}_gui_fp8).This holds whichever order LLVM picks for the DMAs and scale loads. vmcnt 4,
3, 2 and 0 are all clean; 5 races. The comment states the invariant.
kernels compile unchanged. FP8/BF16-activation kernels with tile_m >= 32, and
FP4 with tile_m >= 64, wait for one or two more loads.
fp4_ratioand reformats the line for Black.A
sched_barrier(0)afterprefetch_x_to_ldswith vmcnt(5) also fixes it(0/60 per @Bernard-Liu). This PR keeps the vmcnt change because it does not
depend on the scheduler's order, and its cost is within noise (below).
Performance (if applicable)
MI355X, DeepSeek V4.1 Flash TP4 a8w4 MoE (5120, 640, 384 experts, top-6) with
the shipped
dsv41_fp8_tuned_fmoe.csvrows. Before: aiter main 2be1b79;after: this PR on the same base. 40 calls on routing captured from vLLM, 4
rotating weight copies. Stage 1 is the profiler kernel time per call; the
fused_moecall is timed under CUDA-graph replay. Median of 3 alternatingrounds; the rounds agree within 1% for stage 1.
flydsl_moe1_afp8_wfp4_bf16_prefix)fused_moebefore (µs)t32x64x256_w3_gui_kw2_fp8t32x128x256_w2_gui_fp8t16x256x256_w4_gui(unchanged kernel)t32x128x256_w3_gui_fp8t32x128x256_w4_gui_fp8t64x128x256_w3_bnt0_guit64x128x256_w4_bnt0_gui_xcd4_fp8The tile_m=32 kernels wait for one more load and the tile_m=64 kernels for two;
the 60-token row (tile_m=16) compiles to the same kernel and is the control.
Testing
No unit test is added: the race is timing-dependent, and a test that reruns a
kernel until two outputs differ would be slow and flaky. The repro from #7158
is the regression check (results below).
Documentation
Kernel-internal synchronization fix; no interface change.
Dependencies
Breaking Changes
None.
PR Self-Review
Reviewed revision
c865a40
Behavior and intent
Stage 1 now waits until everything but the previous half's B loads has
retired before the barrier that lets other waves read that A buffer, so both
A DMAs have landed whatever order LLVM issues them in relative to the scale
loads. Outputs become deterministic and match the reference; kernels where the
removed term is 0 are unchanged.
Validation and unverified scope
All on MI355X, FlyDSL 0.5.3.
Determinism on captured DSv4.1 TP4 routing (
t32x128x256_w4_gui_fp8,T = 234 / 384 / 768, 9 calls, 8 runs): every rerun on main disagrees with the
first in 4–20 rows; with this PR all reruns are bit-identical.
The FlyDSL a8w4 MoE stage 1
t32x128x256_w{2,3,4}_gui_fp8is nondeterministic: rows 16-31 of a 32-row sorted block get corrupted 4x16 patches (shipped DeepSeek-V4 tuned rows 256/512/1024) #7158 repro, unchanged (E=384, top-6, hidden 5120, inter 1152, M=1024,Zipf routing; 60
fused_moecalls and 60 stage-1 reruns per row):flydsl_moe1_afp8_wfp4_bf16_prefix)t32x128x256_w3_gui_fp8(shipped, 1024 tokens)t32x128x256_w2_gui_fp8With this PR, error against the fp32 reference stays at or below 0.042 on
every row.
Performance: the table above. Both builds load the shipped table through
AITER_CONFIG_FMOE, run withAITER_USE_FLYDSL_MOE_SORTING=1(as vLLM does)and alternate on one GPU. Stage-1 time is unchanged within ±1.1%, and
fused_moewithin ±0.7%.vLLM serving accuracy, DeepSeek-V4.1-Flash TP4 on 4x MI355X (vLLM main
9b38c833ee, this PR on aiter main 2be1b79): full gsm8k (5-shot, strict)
0.9249 without speculative decoding and 0.9242 with DSpark + monokernel;
niah_single_2 1.0 at 32k and 64k in both.
tile_m=64:
t64x128x256_w3_gui_fp8on the DeepSeek-V4 row (E=385, top-7,hidden 7168, inter 768, M=2048), the same repro with 200 calls and 200
stage-1 reruns: on main 1/200 reruns differ (a 4-row x 16-column patch at
block offsets 40–43); with this PR 0/200. @Bernard-Liu measured 2/200 and
0/200, and his static ISA check finds 51 of the 182 shipped FP8/FP4-activation
stage-1 configs unsafe on main and 0 with this PR.
Unverified:
t32x128x256_w{2,3,4}_gui_fp8is nondeterministic: rows 16-31 of a 32-row sorted block get corrupted 4x16 patches (shipped DeepSeek-V4 tuned rows 256/512/1024) #7158 (tile_m 64 + XCD remap) did not reproduce inmy runs on main, with or without this PR.
those paths less correct, but their performance was not measured.
PR-specific question
What is the most important boundary or failure mode introduced by this
change, and what evidence covers it?
The boundary is the wait count itself:
body_vmcnt_before_barrier = tile_n // 32 * gui_ratio * body_b_load_mult.vmcnt(N)is safe only if at least N loads are issued after the last ALDS-DMA. The B loads satisfy this by construction: they go only into phases
other waves read stale A rows. Evidence:
t32x128x256_w2_gui_fp8: on main, two half-steps issueDMA, scale, scale, DMA, B x4and waitvmcnt(5), which leaves the secondDMA in flight; with this PR they wait
vmcnt(4).t32x128x256_w4_gui_fp8: 5 (the old value) races,while 4 (the new value), 3, 2 and 0 are clean.
8 runs x 9 calls).
t32x128x256_w{2,3,4}_gui_fp8is nondeterministic: rows 16-31 of a 32-row sorted block get corrupted 4x16 patches (shipped DeepSeek-V4 tuned rows 256/512/1024) #7158 repro goes from 14/60 and 53/60 differing reruns to 0/60, andt64x128x256_w3_gui_fp8from 1/200 to 0/200.0 with this PR.
Evidence: stage 1 stays within ±1.1% and
fused_moewithin ±0.7% atT = 4–8192, including the tile_m=64 kernels, which now wait for two more
loads.
The remaining risk is a future main-loop change that moves B loads into
phase 0 (the DMA's scheduling region) or changes how many B loads a half
issues without updating the formula. Either would make the count wrong again
without any compile error; the comment above the line states the invariant.
The heterogeneous-B async-copy path already waits on vmcnt(0) and is
unchanged.
Changed op tests
None.
AI disclosure: developed with AI assistance (Claude Opus 5.5, Cursor); the
analysis and measurements were reviewed and run by the author.