Repository navigation
[FlyDSL] [Bugfix] gfx1250 TDM C store: read C back from LDS in the grouped MXFP4 MoE GEMMs - #8466
XiaobingSuper wants to merge 5 commits into
Conversation
…n the same pass A shared expert run beside the routed MXFP4 MoE (MiniMax-M3.1) quantizes the same bf16 rows to MXFP8 in its own launch. The routed A1 quant already reads each row and reduces every 32-block's amax, so it can write that copy too: fused_moe / flydsl_moe_fused_quant_preshuffle take `mxfp8_copy_out=(payload, scale)`, row-major (M, K) E4M3 + (M, K/32) E8M0, the bytes of per_1x32_mx_quant_hip(quant_dtype=fp8, scale_type=fp8_e8m0) (amax floored at kAmaxFloor, RoundUp exponent against 448, v_cvt_scalef32_pk8_fp8_bf16). The three quant builds the M3.1 grouped MoE dispatches take a `mxfp8_copy` build flag (`_mx8cp` builds with two more pointers): token-multidest (plain and K-split), the compact-scale quant, and the route-indexed quant (each source row's first route writes it, mapped or not). Any other A1 path, or a non-grouped MoE / FHMoE, fills the copy with the HIP quant instead. Without the flag every build is unchanged: the kernel bodies moved into a @flyc.jit body inlined into either signature. fused_moe's plugin contract moves to version 3 (two more outputs a v2 provider would leave unwritten, so v2 is not superseded); tests/plugin_contracts.json records it. op_tests/test_moe_quant_shared_mxfp8.py: routed outputs bitwise unchanged and the copy bitwise the split quant (and a torch model of the rule) for every variant at 8..32768 tokens, the public fused_moe path, and timing of the fused launch against routed quant + split quant. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…shared-expert add Add an optional output epilogue to the grouped MoE gather-reduce: out = bf16(bf16(bf16(sum_k w * rows) * scale) + addend), each step rounded like the separate torch ops it replaces (the multiply is skipped at scale 1.0), so the bytes are unchanged. moe_output_epilogue(addend, scale, before=None) is a context manager that hands the pending epilogue to the next _grouped_a8w4_tdm_moe call in the block; `before` runs right before the gather-reduce launch (e.g. a wait for the stream that produced addend) and `consumed` tells the caller whether a gather-reduce took it. EP calls and other MoE paths leave it unconsumed and the caller applies the scale and add itself. Python-side state: for eager callers, not torch.compile tracing. MiniMax-M3.1 TP4 shape (hidden 6144, E128 top-4, bf16), 4x MI455X, us: T 64 128 192 256 gather-reduce+mul+add 5.41 5.61 6.09 6.50 fused gather-reduce 1.84 1.91 2.19 2.21 Output bitwise identical to the separate ops (eager and Inductor). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ed MXFP4 GEMM launches moe_shared_expert_fold(weights, scale) runs a dense shared expert inside the next grouped MoE call (_grouped_a8w4_tdm_moe), as one more expert every token routes to with weight 1: - GEMM1: launch_gemm_a8w4_tdm_shared_fold puts the shared gate/up tiles in the routed GEMM1 launch (leading workgroups, FP8 x FP8 on the MXFP8 copy of the input the A1 quant writes, SwiGLU and MXFP8 quant in the same epilogue), the routed tiles unchanged behind them; - GEMM2: the shared down tiles in the routed GEMM2 launch (BF16 rows); - the shared rows are added after the routed scale in the gather-reduce (moe_output_epilogue). The MXFP4 TDM GEMM gains b_fp8 (row-major FP8 B, staged like the FP8 activation), inline_task, dense_rows and c_stage_apart knobs for the dense tiles; the routed tiles compile as before. prepare_shared_fold_weights() lays the weights out once (gate/up interleaved, n32k4 scales) into storage it owns, so a caller may shuffle its own parameters in place afterwards. The fold is taken only for unclustered decode tiles without EP, compact or prequantized A1; elsewhere `consumed` stays False and the caller runs the shared expert itself. MiniMax-M3.1 TP4 MoE layer (E128 top-4 MXFP4 routed + MXFP8 shared, hidden 6144), 4x MI455X, us per layer: T 64 128 192 256 routed + shared (2 streams) 69.1 78.4 81.7 82.6 fold 65.6 72.7 78.0 81.0 Shared-expert rel-L2 vs fp32: 2.0e-3 (separate MLP: 1.7e-2, which rounds the gate/up and SwiGLU outputs to BF16 before its quant). Not bitwise the separate MLP for that reason; MoE output rel-L2 vs the current path 1.4e-5. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…stores, not a TDM store On gfx1250 a TDM store (tensor_store_from_lds) can read LDS bytes that predate ds_stores the workgroup issued before `s_wait_dscnt 0` and `s_barrier`. In the shared-fold GEMM2 (routed 16x128 with the 64x64 / 32x64 dense tiles), the C pieces of each wave's last ds_store, in the first rows of its 8-row TDM box, sometimes came out as the input stage's older A/B bytes. That happened in 0.2-10% of launches at T 48-80 under hot-expert routing on MI455X. A ds_load after the same barrier always sees the new data. New build option c_lds_store (bf16/f16 C without EP scatter): after the barrier that follows the C restage, the waves read the tile back from LDS in whole-row 16-byte chunks and write it with buffer_store_b128. Rows past mn_oob are dropped, as the TDM store's row extent drops them. launch_gemm_a8w4_tdm_shared_fold turns it on for GEMM2. The kernel name gains `_cldsst`. MI455X, T 48-256, hot-expert routing: 0 bad launches in 84,000 (the TDM store: up to 10%), bitwise the clean output, +0.0-0.6 us per GEMM2. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…m LDS instead of TDM-storing it On gfx1250, a TDM store's (tensor_store_from_lds) read of LDS is not ordered behind the other waves' ds_stores, even after `s_wait_dscnt 0` and a workgroup barrier. A 16-byte piece of a wave's TDM box can then leave as the bytes LDS held before the C tile: the input stage's FP4 / FP8 operands. ROCm#8430 fixed this for the shared-fold GEMM2. The same store pattern runs in the plain routed MoE GEMMs: - GEMM2's bf16 tile, where stale operand bytes read as bf16 are 1e25-1e38; - a stage-1 GEMM's quantized FP4 / FP8 tile (with its E8M0 scales), which GEMM2 then dequantizes. Both now leave LDS through a ds_load read-back behind the existing barrier, then buffer stores: - launch_gemm_a8w4_tdm takes `c_lds_store`. For GEMM2 it is the bf16 read-back ROCm#8430 added for the fold (`c_lds_store`). For a quantized stage-1 GEMM it is a new `cq_lds_store` epilogue that writes the payload rows the same way (kernel name `_cqldsst`), kept on the TDM store where its 16-byte chunking does not divide the block. - flydsl_grouped_gemm_a8w4_masked turns it on by default (`_c_lds_store`). EP scatter keeps its own store, and AITER_TDM_C_LDS_STORE=0 restores the TDM store. The race fires when another queue's kernels share the CUs, e.g. MiniMax-M3's shared MLP running beside the routed MoE on the other stream of a captured graph. On MiniMax-M3.1 TP4 (4x MI455X), together with the same change in the MXFP8 GEMM, all-zero logit rows (decoded as token 0) went from 8 of 40 greedy passes to 0 of 40, 0 of 12 high-concurrency passes and 0 over full GSM8K. The decode step is unchanged within noise; GSM8K 0.9545 / 0.9538. Co-Authored-By: Claude Opus 5.5 <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
…e2e9) Scope decision for the stray token-0 fix: only the read-back in gemm_a8w8_gfx1250 ships (ROCm#8273); the grouped MoE PR (ROCm#8466) is closed. This reverts both commits that brought it here: - f3c9c30, the `cq_lds_store` wiring for launch_gemm_a8w4_tdm_tpg; - the merge 77be2e9 of the grouped GEMM1 / GEMM2 read-back (`_c_lds_store`, `cq_lds_store`). The grouped MoE read-back cost about 4% of prefill time (uncached prefill TTFT +3.7-4.5%, C64 P90 274.7 vs 281-283), and no failure was ever observed in these kernels. They keep the TDM store, a latent risk recorded in ROCm#8273's hardware / compiler note. This branch's tree is again that of 9baf599. Co-Authored-By: Claude Opus 5.5 <[email protected]>
What
The gfx1250 grouped MXFP4 MoE GEMMs (
launch_gemm_a8w4_tdm) no longer write their C tile with a per-wave TDM store (tensor_store_from_lds). The tile is read back from LDS withds_loadbehind the existing barrier and written withbuffer_store_b128.c_lds_storeread-back that [FlyDSL] [Perf] gfx1250 MoE: fold a shared MXFP8 expert into the routed MXFP4 GEMMs #8430 added for the shared-fold GEMM2, now also on the plain routed launcher.cq_lds_storeepilogue writes the payload rows the same way (kernel name_cqldsst). Where its 16-byte chunking does not divide the block (tile_m * payload_row_bytes / 16not a multiple of the thread count, e.g. 16x128 tiles with 2 waves), the TDM store is kept. This is a known remaining exposure, the same as in the validated stack.flydsl_grouped_gemm_a8w4_maskedturns this on by default (_c_lds_store). EP scatter keeps its own store, andAITER_TDM_C_LDS_STORE=0restores the TDM store.The same change for the native MXFP8 GEMM (
gemm_a8w8_gfx1250,gemm_a8w8_256x256_gfx1250) is in #8273. Thetp_gatherGEMM1 of #8461 gets it there as one more commit.Why
This fixes the stray token-0 bug we hit in MiniMax-M3.1 TP4 decode on 4x MI455X. With dual-stream MoE (the shared MLP beside the routed MoE on the other HIP queue of a captured graph), now and then a decode row's logits were all zero and argmax picked token 0.
Root cause, proven byte for byte. At a bad step (rank 0, layer 45):
Why the MoE GEMMs are covered too. The routed MoE GEMMs store their C tiles the same way and run in the same concurrent window:
The shared-fold GEMM2 showed the same race before (#8430).
Hardware / compiler note
On gfx1250, the LDS read of a TDM store (
tensor_store_from_lds) is not ordered behindds_stores issued by the other waves of the workgroup, even after every wave has dones_wait_dscnt 0and passeds_barrier_signal/s_barrier_wait.ds_stores: the last k-tile's operands that the C tile overlays.ds_loadissued after the same barrier always sees the new data (0 bad in 84,000 launches in the [FlyDSL] [Perf] gfx1250 MoE: fold a shared MXFP8 expert into the routed MXFP4 GEMMs #8430 stress test).s_wait/ cache-control bit), the TDM store could come back. Until then this read-back is the workaround.The first occurrence (the shared-fold GEMM2) and its rates are summarized in #8430; a write-up with the ISA sequence and bad-byte provenance has been written up separately for the compiler / hardware team.
Correctness and performance
MiniMax-M3.1 TP4 on 4x MI455X, with this change and the MXFP8 GEMM one (#8273) together:
Dependencies
c_lds_store). Please review only the top commit.tp_gatherwiring.Validation status
tp_gathercode, which is not in [FlyDSL] [Perf] gfx1250 MoE: fold a shared MXFP8 expert into the routed MXFP4 GEMMs #8430. Thetp_gatherhunks were moved to [FlyDSL] [Perf] gfx1250 MoE: router writes MXFP4 token rows, GEMM1 gathers them (no decode A1 route/quant launch) #8461.launch_gemm_a8w4_tdmGEMM1 (quantized FP4 out) and GEMM2 (bf16 out) at 10 MiniMax-M3.1 tile configs, with the read-back off and on: 20 of 20 compile;py_compile, black and ruff (E4/E7/E9/F: no new findings) are clean.🤖 Generated with Claude Code