Skip to content

[FlyDSL] [MoE] Fix a race in the async stage-1 LDS pipeline; add AITER_MOE_SKIP_INTER_PAD - #6170

Open
jiaweche wants to merge 2 commits into
ROCm:mainfrom
jiaweche:moe-stage1-race-fix-and-pad-skip
Open

jiaweche wants to merge 2 commits into
ROCm:mainfrom
jiaweche:moe-stage1-race-fix-and-pad-skip

Conversation

@jiaweche

@jiaweche jiaweche commented Oct 6, 2026 •

Copy link
Copy Markdown

Summary

Two changes to the FlyDSL 2-stage fused MoE (fp8 activations × MXFP4 weights, gate/up interleaved), found while optimizing DeepSeek-V4.1-Flash (E=384, topk=6, model_dim 5120, intermediate 576 per rank padded to 640 at TP4) on gfx950:

  • fix: an intermittent race in the async stage-1 main loop. Each half of interleaved_half DMAs the next X tile into LDS (buffer_load ... lds). Then it issues the scale and B loads. The next half waits with s_waitcnt vmcnt(body_vmcnt_before_barrier) and s_barrier before other waves read that tile. The threshold (tile_m//32//fp4_ratio + tile_n//32*gui_ratio*mult, 5 for a 32×128×256 tile) covers the DMA only if that many loads are issued after it. The compiler does not guarantee that. In one half of the compiled loop only 4 loads follow the last DMA, so vmcnt(5) lets it still be in flight at the barrier, and rows of the next tile are read stale.
    • Symptom: a few rows of a call's output (runs of neighbouring tokens), off by 2–13% relative L2, in a small fraction of repeated runs on identical inputs. They are gone with use_async_copy=False or with vmcnt(0).
    • The fix, for ordinary async kernels only: the heterogeneous path already waits vmcnt(0).
      • A sched_barrier right after each half's X DMA, so no later load is scheduled between or above the DMAs.
      • The wait counts only the B loads, which always follow the DMAs.
      • The module name gains _dmaw, so cached kernels are recompiled.
    • Cost: none measurable (below). vmcnt(0) instead cost up to 10.8% per call.
  • AITER_MOE_SKIP_INTER_PAD=1 (opt-in, default off; off leaves the IR unchanged): stop loading the intermediate pad when intermediate_pad > 0. Today stage 1 computes the pad's N tile (w1 and its scales), and stage 2 reads the pad's K cells of w2, all zeros.
    • stage 1 (gate/up interleaved): an N tile wholly in the pad reads w1 and its scales through zero-sized buffer resources. The loads return 0 without touching memory, and the tile still writes the zeros stage 2 reads. Tag _padskip.
    • stage 2, cshuffle (fp4 B): B cells at K ≥ inter_dim − inter_dim_pad load through an out-of-range offset. This is enabled only while w2 is under 2 GiB, so the offset stays a valid 32-bit buffer offset. Tag _padskip<K>.
    • stage 2, layout v2 (gemm2_a4w4_port, fp4 B): the B view's buffer is bounded at K_REAL/32 cells per 16-row group, so K ≥ K_REAL reads zero. inter_dim_pad reaches it through _flydsl_v2_stage2_wrapper and mxfp4_moe_gemm2(D_INTER_PAD=...). Tag _kr<K>.
    • The pad's MFMAs still run on zero operands; only its loads go. On this model that is 64 of 640 intermediate columns per expert (10% of each stage's weight bytes). The measured fall in fetched bytes equals the pad's own bytes in each stage.

The two commits are independent, and the fix can be taken alone.

Test plan

All on MI350 (gfx950, 256 CUs). The data are fused_moe calls captured from DeepSeek-V4.1-Flash serving (one MoE layer, rank 0 of TP4), replayed with the call's own routing. The table is the DSV4.1-Flash FlyDSL rows (e.g. flydsl_moe1_afp8_wfp4_bf16_t32x128x256_w4_gui_fp8 with flydsl_moe2_afp8_wfp4_bf16_t32x128x128_atomic_bnt2 or flydsl_moe2_layout_afp8_wfp4_bf16_t32x256x128_atomic_nt_sbm32).

On this branch (main 5b2f7d1 + the two commits; flydsl==0.3.4.1, PREBUILD_KERNELS=0):

  • ISA (FlyDSL's final ISA; for every s_barrier after an X DMA, is there a vmcnt(N) with N ≤ the loads issued after the DMA):

    • main's stage-1 kernel: 1 of 19 barriers uncovered;
    • this branch, with and without the pad skip: 0 of 19.
  • Repeated runs on identical inputs: 24 runs × 2 calls per tier; rows off the elementwise median by more than 2% of its maximum.

    tier 64 tier 128 tier 256
    main 0 5 rows 1 row
    this branch 0 0 0
    this branch + pad skip 0 0 0
  • Accuracy against an fp32 reference built from the checkpoint (MXFP4 dequant, MXFP8 activations; five runs; tiers 8–256, 2,048, 8,192): this branch, with and without the pad skip, has main's max-abs and relative-L2 error at every tier: identical at 128, 2,048 and 8,192, within the run-to-run variation of stage 2's atomic accumulation elsewhere. Max-abs differences from main's outputs are 1–2 bf16 ulps, as between main's own runs.

  • µs per fused_moe call (median over calls and three launches):

    tier main this branch + pad skip branch / main pad skip
    8 45.4 45.6 44.4 1.004 −2.6%
    16 59.8 60.0 58.2 1.004 −3.1%
    32 74.6 74.6 72.0 1.001 −3.5%
    64 98.7 98.7 96.8 1.000 −2.0%
    128 118.6 118.8 115.2 1.002 −3.1%
    256 167.1 167.3 157.5 1.001 −5.8%
    2048 583.9 582.8 568.1 0.998 −2.5%
    8192 1269.4 1271.0 1236.8 1.001 −2.7%
  • black (26.10) clean on the touched files; ruff 0.16.0 reports nothing beyond main's own findings on these files.

The same changes, as a patch on v0.1.21.post2 (flydsl==0.3.2, the vLLM DeepSeek-V4.1-Flash image), in more depth:

  • ISA: every stage-1 variant the model's tables compile is covered, 64- and 128-row tiles and the K-split (kw4) kernel included.
  • Repeated runs (48 runs × 2 calls per tier, tiers 64–2,048): the unpatched kernel deviates (up to 53 rows in 21 of 96 runs at tier 128 with the pad skip's timing). Patched: 0.
  • Fetched bytes (rocprofv3 FETCH_SIZE, cold, per distinct expert):
    • stage 1: 3.545 → 3.197 MB;
    • stage 2: 1.785 → 1.621 MB;
    • each fall equals the pad's own bytes.
  • Per stage, cold, decode tiers 8–256: stage 1 −5.1% to −7.4%, stage 2 −6.5% to −10.9%.
  • Serving, vLLM, DeepSeek-V4.1-Flash TP4 (AgentX replays, 3 points per arm, alternating on one node):
    • GSM8K (1,319, real speculative acceptance) and first-token logprobs: within the control's own run-to-run movement.
    • C8: median ITL better in every input-length bucket; end to end −0.6% summed.
    • C32: Pareto.
    • Combined with a smaller MoE sort fast path and two tuned tiers: −2.0% end to end at C8, −1.2% at C32, nothing worse.
  • MI355X serving A/B (vLLM DeepSeek-V4.1-Flash TP4 on a current best-results branch; this patch on v0.1.21.post2 with the race fix in both arms; 3 points per arm): GSM8K pass; C8 median ITL better or flat in every bucket, end to end −0.5% summed; C32 median ITL 0.975–0.991 per bucket, end to end −0.75% summed, TTFT worse in the longest buckets after the first 300 s. Details in the comment below.

Not changed here, found on the way: in the K-split (k_wave > 1) stage 1, the loop's last half issues one X DMA past the wave's K range. The check is next_k_dma_py < int(k_dim), the full K, not the wave's share. That DMA lands in the buffer the K-wave reduction later reuses. In the compiled kernels I checked, a wait the compiler inserts for a register dependency covers it.

🤖 Generated with Claude Code

…ch half's X DMA before the barrier

In compile_mixed_moe_gemm1_common's interleaved main loop, each half issues
the next X tile's DMA into LDS (buffer_load ... lds), then the scale and B
loads, and the next half waits with s_waitcnt vmcnt(body_vmcnt_before_barrier)
and s_barrier before other waves read that tile. The threshold counts the X
tile and the B loads, which covers the DMA only if that many loads are issued
after it. In the compiled first half the scale loads land between the two X
DMAs (32-row tile: DMA, scale, scale, DMA, then four B loads), so vmcnt(5)
lets the second DMA (rows 16-31) still be in flight at the barrier.

Seen as intermittent output deviations in fused_moe with FlyDSL stage 1
(t32x128x256 / t64x128x256, fp8 X, fp4 interleaved W, use_async_copy=True as
_flydsl_stage1_wrapper passes): runs of neighbouring tokens off by 2-13%
relative L2, gone with use_async_copy=False or with vmcnt(0).

Fix (ordinary async kernels; the heterogeneous path already waits vmcnt(0)):
a sched_barrier right after each half's X DMA, so no later load is scheduled
between or above the DMAs, and the wait counts only the B loads, which always
follow them. Module name gains '_dmaw' so cached kernels are recompiled.
…e pad in both stages

When the intermediate dim is padded (intermediate_pad > 0; e.g. 576 real
columns per rank padded to 640 for DeepSeek-V4.1-Flash at TP4), both FlyDSL
stages load the pad: stage 1 computes the pad's N tile (w1 and its scales),
and stage 2 reads the pad's K cells of w2. Their contribution is zero.

With AITER_MOE_SKIP_INTER_PAD=1 (default off; off leaves the IR unchanged):
- stage 1 (gate/up interleaved, fp4/fp8 W): an N tile wholly in the pad
  reads w1 and its scales through zero-sized buffer resources. Every load
  returns 0 without touching memory; the tile still writes the zeros stage 2
  reads. Module name gains '_padskip'.
- stage 2, cshuffle (fp4 B): B cells at K >= inter_dim - inter_dim_pad are
  read through an out-of-range offset. Tagged '_padskip<K>'.
- stage 2, layout v2 (gemm2_a4w4_port, fp4 B): the B view's buffer is
  bounded at K_REAL/32 cells per 16-row group, so K >= K_REAL reads zero.
  Tagged '_kr<K>'; inter_dim_pad reaches it through _flydsl_v2_stage2_wrapper.
The pad's MFMAs still run (their operands are zero); only its loads go.
@jiaweche
jiaweche requested a review from a team October 6, 2026 10:44
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

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

Extended tests (opt-in via labels):

Label Tests
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
multigpu 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: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
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 6170 --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.

@github-actions github-actions Bot added the FlyDSL label Oct 6, 2026
@jiaweche

jiaweche commented Oct 6, 2026

Copy link
Copy Markdown
Author

MI355X results (the open item in the test plan).

Setup. vLLM serving DeepSeek-V4.1-Flash at TP4 on MI355X (gfx950):

  • Images and launch: a current best-results branch with its own launch: DSpark with 4 speculative tokens, --numa-bind, and FlyDSL MoE rows that select the async stage 1.
  • The patch: this PR's two commits, applied as a patch on the image's AITER v0.1.21.post2.
  • Arms: both carry the race fix. The control has the pad skip off; the treatment sets AITER_MOE_SKIP_INTER_PAD=1.
  • Runs: AgentX replays at seed 42, three points per arm, alternating on one node per concurrency.
  • Proof of path: every point's server log shows the stage-1 fix on all four ranks, and the pad-skip kernels on all four ranks in treatment points only.

Correctness. GSM8K (1,319, real speculative acceptance, 3 runs each), counts correct:

arm strict flexible
control 1282, 1285, 1285 1281, 1284, 1284
pad skip 1278, 1284, 1281 (mean 1281.0) 1277, 1283, 1280 (mean 1280.0)
control, second 1278, 1283, 1287 1277, 1282, 1286

C8, per-request median ratios against the control, by input length. "Better" and "worse" mean outside the control's own point-to-point spread.

input length n median ITL ITL p99 end to end
<32K 16 0.992 1.011 1.000
32–64K 66 0.992 (better) 0.993 (better) 0.991
64–128K 94 0.994 (better) 0.992 (better) 0.995
128–256K 48 0.995 0.994 (better) 0.995
256–512K 18 0.995 0.992 (better) 0.992

TTFT is flat in every bucket, and no cell is worse. End-to-end latency summed over requests: −0.5%.

C32:

input length n median ITL ITL p99 TTFT end to end
<32K 96 0.975 0.955 (better) 0.983 (better) 0.991
32–64K 323 0.991 (better) 1.002 1.002 0.991
64–128K 460 0.982 (better) 0.983 (better) 0.993 0.982
128–256K 212 0.976 (better) 0.989 (better) 1.003 0.976
256–512K 35 0.990 0.986 1.018 (worse) 1.002
≥512K 16 0.984 0.997 1.027 (worse) 0.991
  • End to end: latency summed −0.75%: −1.7% in the first 300 s of the replay, −0.5% after.
  • Worse, pooled: TTFT in the two longest buckets, by 1.8% and 2.7%.
  • Worse after 300 s, beyond those: TTFT at 32–64K (+2.9%) and <32K (+0.9%), and ITL p99 at 32–64K (+2.1%).

The verdicts are the same as on MI350: a keep at C8 and a Pareto point at C32.

@github-actions

Copy link
Copy Markdown
Contributor

No activity for 15 days, so this is now labelled stale. A push or a comment clears it; keep-open exempts it. Nothing is closed automatically.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

FlyDSL stale The PR hasn't been updated in 2 weeks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants