Skip to content

[FlyDSL] [Bugfix] MoE stage 1: wait for the A LDS-DMA before the half-step barrier - #8550

Merged
Bernard-Liu merged 3 commits into
ROCm:mainfrom
cpersson-amd:moe-s1-dma-vmcnt
Nov 30, 2026
Merged

Bernard-Liu merged 3 commits into
ROCm:mainfrom
cpersson-amd:moe-s1-dma-vmcnt

Conversation

@cpersson-amd

@cpersson-amd cpersson-amd commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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_read picked up stale A rows. This corrupted rows 16–31 of a 32-row
sorted 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 waited
vmcnt(tile_m // 32 // fp4_ratio + B loads). That count assumes the scale
loads 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 may
place 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 the
tail issue DMA, scale, scale, DMA, B x4, so vmcnt(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 the
ISA 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 and
256–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 before
    the half-step barrier drops the tile_m // 32 // fp4_ratio term, so it no
    longer 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.
  • The term is 0 for tile_m=16, and for FP4 activations at tile_m=32, so those
    kernels compile unchanged. FP8/BF16-activation kernels with tile_m >= 32, and
    FP4 with tile_m >= 64, wait for one or two more loads.
  • Drops the now-unused fp4_ratio and reformats the line for Black.

A sched_barrier(0) after prefetch_x_to_lds with 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.csv rows. 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_moe call is timed under CUDA-graph replay. Median of 3 alternating
rounds; the rounds agree within 1% for stage 1.

Tokens Stage 1 (tuned kernel, flydsl_moe1_afp8_wfp4_bf16_ prefix) Stage 1 before (µs) after (µs) Change fused_moe before (µs) after (µs) Change
4 t32x64x256_w3_gui_kw2_fp8 15.62 15.63 +0.1% 36.6 36.7 +0.3%
16 t32x128x256_w2_gui_fp8 29.59 29.27 -1.1% 53.7 54.1 +0.7%
60 t16x256x256_w4_gui (unchanged kernel) 55.54 55.44 -0.2% 105.3 105.4 +0.1%
252 t32x128x256_w3_gui_fp8 98.84 98.83 0.0% 165.2 165.1 -0.1%
1024 t32x128x256_w4_gui_fp8 208.42 208.56 +0.1% 333.2 333.3 0.0%
2048 t64x128x256_w3_bnt0_gui 255.20 256.14 +0.4% 454.3 452.9 -0.3%
8192 t64x128x256_w4_bnt0_gui_xcd4_fp8 510.29 514.93 +0.9% 947.7 950.8 +0.3%

The 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

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X

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

  • Docstrings updated
  • User guide updated
  • Performance guide updated
  • No documentation change is needed, with the reason stated below

Kernel-internal synchronization fix; no interface change.

Dependencies

  • No new third-party dependencies added
  • New dependencies are listed and justified below

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_fp8 is 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_moe calls and 60 stage-1 reruns per row):

    Stage 1 (flydsl_moe1_afp8_wfp4_bf16_ prefix) main: reruns differing main: calls deviating PR: reruns differing PR: calls deviating
    t32x128x256_w3_gui_fp8 (shipped, 1024 tokens) 14 / 60 8 / 60 0 / 60 0 / 60
    t32x128x256_w2_gui_fp8 53 / 60 46 / 60 0 / 60 0 / 60

    With 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 with AITER_USE_FLYDSL_MOE_SORTING=1 (as vLLM does)
    and alternate on one GPU. Stage-1 time is unchanged within ±1.1%, and
    fused_moe within ±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_fp8 on 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:

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 A
LDS-DMA. The B loads satisfy this by construction: they go only into phases

= 1, each behind sched_barrier(0), so LLVM cannot hoist them above the DMA.
The scale loads do not: they share phase 0's scheduling region with the DMAs,
and counting them was the bug. The count can fail in two directions:

  • Too high (the bug): an A DMA is still in flight at the barrier, so
    other waves read stale A rows. Evidence:
  • Too low (cost only): stage 1 waits for loads it does not need.
    Evidence: stage 1 stays within ±1.1% and fused_moe within ±0.7% at
    T = 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.

…-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]>
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter standard tests on MI35X
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:mi300x Run Aiter standard tests on MI300X in PRs; main branch always runs them
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 Radeon R9700 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 8550 --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.

Reviews:
A reviewer is usually assigned within one working day after the PR is ready for review (see Code Review Process). If nobody has responded for about a week, mention the area contact listed in CONTACTS.md. Want to help review? See REVIEWERS.md.

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]>
@cpersson-amd
cpersson-amd marked this pull request as ready for review November 25, 2026 10:39
@cpersson-amd
cpersson-amd requested a review from a team November 25, 2026 10:39
@aiter-bot

aiter-bot commented Nov 25, 2026 •

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

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

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 #7158 repro, unchanged (E=384, top-6, hidden 5120, inter 1152, M=1024,
  Zipf routing; 60 `fused_moe` calls and 60 stage-1 reruns per row):

  | Stage 1 (`flydsl_moe1_afp8_wfp4_bf16_` prefix) | main: reruns differing | main: calls deviating | PR: reruns differing | PR: calls deviating |
  |---|---|---|---|---|
  | `t32x128x256_w3_gui_fp8` (shipped, 1024 tokens) | 14 / 60 | 8 / 60 | 0 / 60 | 0 / 60 |
  | `t32x128x256_w2_gui_fp8` | 53 / 60 | 46 / 60 | 0 / 60 | 0 / 60 |

  With 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 with `AITER_USE_FLYDSL_MOE_SORTING=1` (as vLLM does)
  and alternate on one GPU. Stage-1 time is unchanged within ±1.1%, and
  `fused_moe` within ±0.7%.
- vLLM serving accuracy, DeepSeek-V4.1-Flash TP4 on 4x MI355X (vLLM main
  9b38c833ee, this PR on aiter main 2be1b7932): 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_fp8` on 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:
  - The second trigger in #7158 (tile_m 64 + XCD remap) did not reproduce in
    my runs on main, with or without this PR.
  - Stage 2 has its own wait logic, which this PR does not touch or check.
  - MI250X / MI300X: not run. The change only adds waits, so it cannot make
    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 A
LDS-DMA. The B loads satisfy this by construction: they go only into phases
>= 1, each behind `sched_barrier(0)`, so LLVM cannot hoist them above the DMA.
The scale loads do not: they share phase 0's scheduling region with the DMAs,
and counting them was the bug. The count can fail in two directions:

- **Too high** (the bug): an A DMA is still in flight at the barrier, so
  other waves read stale A rows. Evidence:
  - ISA of `t32x128x256_w2_gui_fp8`: on main, two half-steps issue
    `DMA, scale, scale, DMA, B x4` and wait `vmcnt(5)`, which leaves the second
    DMA in flight; with this PR they wait `vmcnt(4)`.
  - A vmcnt sweep on `t32x128x256_w4_gui_fp8`: 5 (the old value) races,
    while 4 (the new value), 3, 2 and 0 are clean.
  - Bit-identical reruns on captured DSv4.1 routing (T = 234 / 384 / 768,
    8 runs x 9 calls).
  - The #7158 repro goes from 14/60 and 53/60 differing reruns to 0/60, and
    `t64x128x256_w3_gui_fp8` from 1/200 to 0/200.
  - Static ISA check of 182 shipped configs (@Bernard-Liu): 51 unsafe on main,
    0 with this PR.
  - Serving gsm8k and NIAH are unchanged.
- **Too low** (cost only): stage 1 waits for loads it does not need.
  Evidence: stage 1 stays within ±1.1% and `fused_moe` within ±0.7% at
  T = 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.

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.

@Bernard-Liu

Copy link
Copy Markdown
Contributor

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]>
@cpersson-amd

Copy link
Copy Markdown
Contributor Author

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 t32x128x256_w2_gui_fp8 at hidden 5120: most half-steps issue DMA, DMA, scale, scale, B x4, which is safe at vmcnt(5), but the last two before the tail issue DMA, scale, scale, DMA, B x4, so vmcnt(5) leaves the second DMA in flight. With the PR both orders wait vmcnt(4).

I also reproduced the tile_m=64 case: t64x128x256_w3_gui_fp8 on the DSv4 row (E=385, top-7, K=7168, I=768, M=2048) gives 1/200 differing stage-1 reruns on main (a 4x16 patch at block offsets 40–43) and 0/200 with the PR. The description now credits your 2/200 and the 51/182 static check, and drops the tile_m=64 caveat and the Primus-Turbo reference.

I kept the vmcnt change rather than the sched_barrier(0) alternative: it holds whichever order LLVM picks for the DMAs and scale loads, and it costs within noise (stage 1 within ±1.1% at T = 4–8192).

@Bernard-Liu
Bernard-Liu merged commit 31bd852 into ROCm:main Nov 30, 2026
64 checks passed
@zufayu zufayu mentioned this pull request Dec 1, 2026
5 of 10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

4 participants