Skip to content

[FlyDSL] MoE stage2: MXFP4 route-output reduction next to the FP8 one - #7706

Merged
coderfeli merged 5 commits into
mainfrom
cguo/gemm2fp4
Nov 16, 2026
Merged

coderfeli merged 5 commits into
mainfrom
cguo/gemm2fp4

Conversation

@charlieguo1106

Copy link
Copy Markdown
Contributor

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:

  • GEMM2 writes unweighted per-route MXFP4 rows.
  • moe_reduce decodes them, applies the topk weights and sums in FP32.

Separately, the GLM-5 FP4 8- and 16-token tuned rows switch to the atomic_nt stage-2 kernel.

Motivation

In large-M reduce configs, stage 2 writes one BF16 row per (token, slot), and moe_reduce reads 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.

  • FP8 route rows halve this traffic.
  • MXFP4 rows cut it 3.8×: 3264 B instead of 12288 B per route at hidden 6144.

The quantization is lossy, so the path stays opt-in.

Changes

  • mxmoe_gemm_v2.atomic_bf16_epilog: new route_out_fp4, which reuses the FP8 route-out branch (BF16 LDS staging, per-32-column DPP amax, deferred routing weight). It differs only in:

    • the convert: cvt_scalef32_pk_fp4_{bf16,f32}, eight values per dword;
    • the value offset: col / 2;
    • the E8M0 rule: max(exp(amax) - 2, 1), since e2m1's largest value is 6;
    • the scale bytes, which start at N / 2.
  • mxmoe_dispatcher: out_dtype="fp4" with epilog="reduce" maps to route_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: new dtype_str="fp4" on the FP8 path. Each thread does one 32-bit load of 8 packed values, uses the same E8M0 lookup, and decodes with cvt_scalef32_pk_f32_fp4. It honours streaming_loads, the EP expert mask and deferred weights.

  • moe_kernels._run_moe_reduction: new is_fp4 argument.

  • mxfp4_gemm_common: new fp4out_row_bytes.

  • fused_moe: _flydsl_v2_stage2_wrapper picks FP4 rows when all of these hold:

    • AITER_MXFP4_INTERMEDIATE=1;
    • the tuned stage-2 kernel uses the reduce epilogue;
    • routing weights are available;
    • 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_2stages passes topk_weights when either flag is set.

  • glm5_fp4_tuned_fmoe.csv: the 8- and 16-token rows now use kernelName2 = 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)

Hardware Workload/configuration Before After Change
gfx950, 256 CUs GLM-5.2 TP4 MoE, 2048 tokens; AITER_MXFP4_INTERMEDIATE 0 → 1 408.2 µs 359.0 µs −12.1%
gfx950, 256 CUs Same, 4096 tokens 604.0 µs 483.5 µs −20.0%
gfx950, 256 CUs Same, 8 tokens, CSV row (default flags) 68.24 µs 67.00 µs −1.8%
gfx950, 256 CUs Same, 16 tokens, CSV row (default flags) 95.05 µs 92.36 µs −2.8%

Workload. model_dim 6144, inter_dim 512, 257 experts, topk 9, a4w4 MXFP4, SiLU, BF16 input and output, preshuffled weights.

Method.

  • Time is the whole fused_moe call: the median over calls of the per-call sum of GPU kernel times.
  • Before each call, a 1 GiB buffer is read (not timed) so every call starts with cold caches.
  • Routing is a fixed uniform-random draw, with 52–99 rows per expert at 2048 tokens and 111–176 at 4096.
  • The 2048/4096 rows were measured on this branch (rebased on cb6929b, FlyDSL 0.5.0).
  • The 8/16 rows were measured on the previous main with the same protocol, 16 rounds.

Stage-2 breakdown before the rebase (µs):

Tokens G2 before → after Reduction before → after
2048 144.3 → 117.3 39.9 → 15.5
4096 209.7 → 159.9 101.3 → 27.7

Accuracy (FP4 on).

  • Whole-layer output SQNR against an FP32 reference is about 18.1 dB, versus about 29 dB with BF16 route rows.
  • test_moe_2stage logits_diff is about 0.0075.

Testing

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

No op test was added; see "Validation and unverified scope". Only gfx950 was tested; the a4w4 MXFP4 FlyDSL path is gfx950-only.

Documentation

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

The change reuses the existing AITER_MXFP4_INTERMEDIATE flag and adds no new user-facing option.

Dependencies

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

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 --check and ruff check are clean on the changed files.

  • Reproduction: the stock op_tests/test_moe_2stage.py exercises the same path:

    AITER_MXFP4_INTERMEDIATE=1 python3 op_tests/test_moe_2stage.py --no-flydsl-csv \
      -d bf16 -dim 6144,512 -q 4 -e 257 -k 9 -t 2048 4096
    

    The cold-cache, fixed-routing timing above came from a local harness that is not part of this PR. The stock run_perftest timing reads about 5–6% higher at 4096 tokens.

Not verified:

  • bias2 with FP4 route rows;
  • FP16 output;
  • BN=512 stage-2 tiles;
  • a8w4 (FP8 A) stage 2 with FP4 rows;
  • EP end to end (only the reduction's mask path was checked);
  • CUDA-graph capture;
  • model-level accuracy evals;
  • 1024 tokens and other atomic-epilogue rows, which are unaffected because FP4 applies only to the reduce epilogue.

PR-specific question

Changed op tests

None.

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.
@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 7706 --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.

@charlieguo1106
charlieguo1106 deleted the cguo/gemm2fp4 branch November 12, 2026 03:07
@charlieguo1106
charlieguo1106 restored the cguo/gemm2fp4 branch November 12, 2026 03:10
@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 7706 --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.

@charlieguo1106
charlieguo1106 marked this pull request as ready for review November 13, 2026 03:26
@charlieguo1106
charlieguo1106 requested a review from a team November 13, 2026 03:26
@aiter-bot

aiter-bot commented Nov 13, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision b6a1541a92f6 · mode shadow

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

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. Which exact workload keys consume these configuration changes, and what evidence shows that unrelated shapes keep their previous selection?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA
  • FAIL — PR-specific question has no substantive answer
  • FAIL — validation answer must explicitly state what remains unverified (or say none with a reason)

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

@binding7012

binding7012 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

AOT failed to keep up: When AITER_MXFP4_INTERMEDIATE=1, the kernels precompiled for reduce operations are still bf16 kernels. When using AOT pre-compilation and deploying with FLYDSL_RUNTIME_RUN_ONLY=1 enabled, the kernel cannot be found, resulting in an error during the first forward pass.

@charlieguo1106

Copy link
Copy Markdown
Contributor Author

AOT failed to keep up: When AITER_MXFP4_INTERMEDIATE=1, the kernels precompiled for reduce operations are still bf16 kernels. When using AOT pre-compilation and deploying with FLYDSL_RUNTIME_RUN_ONLY=1 enabled, the kernel cannot be found, resulting in an error during the first forward pass.

fixed

Keep the fp4 route-out scale layout and epilogue lanes while taking main's dropped-route reduction and LDS-pair scatter.
Rejoin the g2_epi_lanes call that the conflict resolution had split.

@binding7012 binding7012 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@coderfeli
coderfeli merged commit 77ea506 into main Nov 16, 2026
@coderfeli
coderfeli deleted the cguo/gemm2fp4 branch November 16, 2026 13:50
cpersson-amd added a commit to cpersson-amd/aiter that referenced this pull request Nov 18, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants