Repository navigation
[FlyDSL] MoE stage2: MXFP4 route-output reduction next to the FP8 one - #7706
Conversation
With AITER_MXFP4_INTERMEDIATE=1, a FlyDSL v2 stage2 whose tuned kernel uses the reduce epilogue writes unweighted MXFP4 route rows instead of BF16 ones: [model_dim/2 packed e2m1 | model_dim/32 e8m0], pitch aligned to 64 bytes. The rows come from the existing FP8 route-out epilogue (same BF16 LDS staging, per-32-column amax and deferred routing weight) with the e2m1 convert and an e8m0 exponent offset of 2; moe_reduce decodes them as dtype "fp4", applies the topk weights and accumulates in FP32. Atomic and scatter configs are unchanged, and AITER_FLYDSL_STAGE2_FP8=1 keeps precedence. Also switch the GLM-5 FP4 8- and 16-token rows to the atomic_nt stage-2 kernel. GLM-5.2 TP4 shape (model_dim 6144, inter_dim 512, 257 experts, topk 9, a4w4) on gfx950, whole MoE layer, cold caches: 2048 tokens 408 -> 359 us, 4096 tokens 604 -> 483 us. The intermediate quantization is lossy (output SQNR about 18 dB versus 29 dB with BF16 partials), so it stays opt-in.
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
33674b7 to
dd8207c
Compare
WAITING ON AUTHORPR Self-ReviewRevision Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision. Questions to answer in the PR description
Edit the Missing or invalid answers
Shadow mode reports what would block but keeps the status successful. Set |
|
AOT failed to keep up: When |
774670b to
dd8207c
Compare
fixed |
5aefaef to
b27ad56
Compare
Keep the fp4 route-out scale layout and epilogue lanes while taking main's dropped-route reduction and LDS-pair scatter.
b27ad56 to
6201b3e
Compare
Rejoin the g2_epi_lanes call that the conflict resolution had split.
Resolve conflicts with the fused shared expert (ROCm#7773), the MXFP4 route-output intermediate (ROCm#7706) and the scatter LDS pairs option: keep the per-kernel-name FP8 route-out selection, with the MXFP4 intermediate as the fallback when FP8 is not selected. Co-authored-by: Claude Opus 5.5 <[email protected]> Signed-off-by: Carl Persson <[email protected]>
Summary
Adds an opt-in MXFP4 route-output path for FlyDSL v2 MoE stage 2, enabled by
AITER_MXFP4_INTERMEDIATE=1. It sits next to the existing FP8 route-output path (AITER_FLYDSL_STAGE2_FP8=1) and shares its code:moe_reducedecodes them, applies the topk weights and sums in FP32.Separately, the GLM-5 FP4 8- and 16-token tuned rows switch to the
atomic_ntstage-2 kernel.Motivation
In large-M reduce configs, stage 2 writes one BF16 row per (token, slot), and
moe_reducereads them all back. For hidden 6144 and topk 9 at 4096 tokens, that is 432 MiB written and read again; the reduction alone takes about 101 µs.The quantization is lossy, so the path stays opt-in.
Changes
mxmoe_gemm_v2.atomic_bf16_epilog: newroute_out_fp4, which reuses the FP8 route-out branch (BF16 LDS staging, per-32-column DPP amax, deferred routing weight). It differs only in:cvt_scalef32_pk_fp4_{bf16,f32}, eight values per dword;col / 2;max(exp(amax) - 2, 1), since e2m1's largest value is 6;N / 2.mxmoe_dispatcher:out_dtype="fp4"withepilog="reduce"maps toroute_out_fp4. It shares the FP8 64 B row pitch, 32-column scale block, epilogue-lane choice and name tags (_dw_pa64_sblk32_f4out).moe_reduce: newdtype_str="fp4"on the FP8 path. Each thread does one 32-bit load of 8 packed values, uses the same E8M0 lookup, and decodes withcvt_scalef32_pk_f32_fp4. It honoursstreaming_loads, the EP expert mask and deferred weights.moe_kernels._run_moe_reduction: newis_fp4argument.mxfp4_gemm_common: newfp4out_row_bytes.fused_moe:_flydsl_v2_stage2_wrapperpicks FP4 rows when all of these hold:AITER_MXFP4_INTERMEDIATE=1;model_dim % 32 == 0.The FP8 flag wins if both flags are set. The FP4 target buffer comes from
_stage2_scratch, like the FP8 one.fused_moe_2stagespassestopk_weightswhen either flag is set.glm5_fp4_tuned_fmoe.csv: the 8- and 16-token rows now usekernelName2=flydsl_moe2_layout_afp4_wfp4_bf16_t16x128x256_atomic_nt_sbm16. G1 is unchanged.Atomic and scatter configs are untouched, as are the default flags.
Performance (if applicable)
AITER_MXFP4_INTERMEDIATE0 → 1Workload. model_dim 6144, inter_dim 512, 257 experts, topk 9, a4w4 MXFP4, SiLU, BF16 input and output, preshuffled weights.
Method.
fused_moecall: the median over calls of the per-call sum of GPU kernel times.Stage-2 breakdown before the rebase (µs):
Accuracy (FP4 on).
test_moe_2stagelogits_diff is about 0.0075.Testing
No op test was added; see "Validation and unverified scope". Only gfx950 was tested; the a4w4 MXFP4 FlyDSL path is gfx950-only.
Documentation
The change reuses the existing
AITER_MXFP4_INTERMEDIATEflag and adds no new user-facing option.Dependencies
Breaking Changes
None with default flags.
Behaviour change for users who already set
AITER_MXFP4_INTERMEDIATE=1: it used to affect only scatter (BM=128) configs and the legacy MXFP4 GEMM2 path. Now FlyDSL v2 stage-2 configs with the reduce epilogue also write FP4 route rows.The 8- and 16-token GLM-5 FP4 CSV rows change the default stage-2 kernel for that shape.
PR Self-Review
Reviewed revision
dd8207c
Behavior and intent
With
AITER_MXFP4_INTERMEDIATE=1, reduce-epilogue FlyDSL v2 stage-2 configs store MXFP4 route rows instead of BF16. This trades accuracy (about 18 dB instead of 29 dB output SQNR) for 3.8× less stage-2 intermediate traffic. The design deliberately reuses the FP8 route-output epilogue and reduction rather than adding a separate kernel.Validation and unverified scope
FP4 reduction vs a torch decode reference: correct, with max relative error about 2e-3 (BF16 output rounding). Covered model_dim 6144/7168/4096 and topk 9/8, with and without an EP expert mask.
End to end: GLM-5.2 TP4 shape at 2048 and 4096 tokens, FP4 on and off, on this branch with FlyDSL 0.5.0. Timing and accuracy are as above.
Formatting:
black --checkandruff checkare clean on the changed files.Reproduction: the stock
op_tests/test_moe_2stage.pyexercises the same path:The cold-cache, fixed-routing timing above came from a local harness that is not part of this PR. The stock
run_perftesttiming reads about 5–6% higher at 4096 tokens.Not verified:
bias2with FP4 route rows;PR-specific question
Changed op tests
None.