Skip to content

[MoE] Add fused_moe(stage2_fp8=True): per-call FP8 stage-2 route output - #8174

Draft
eppaneamd wants to merge 1 commit into
mainfrom
eppaneamd/moe-s2-fp8-per-call
Draft

eppaneamd wants to merge 1 commit into
mainfrom
eppaneamd/moe-s2-fp8-per-call

Conversation

@eppaneamd

@eppaneamd eppaneamd commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds fused_moe(..., stage2_fp8=False). With True, one call gets the FP8 route output that AITER_FLYDSL_STAGE2_FP8=1 gives every call in the process: a FlyDSL layout reduce stage 2 stores each route's output as FP8 (e4m3, an e8m0 scale per 32 columns) and sums the routes in fp32. After an MXMOE (flydsl_mxmoe_g1) stage 1, an MXFP4 scatter stage 2 runs as the reduce stage 2 of the same tile and gets the FP8 route output too. On MI355X, the Qwen3.8-Flash-Next MoE call at 16,384 tokens takes 918.4 µs instead of 1,137.6 µs (−19.3 %).

Limits:

  • Not bitwise equal to the default: 2.7 % relative L2 at that row (Accuracy below).
  • No gain at decode sizes: the 52 benchmarked reduce rows at ≤ 64 tokens change by −5.0 % to +4.5 % (median −0.1 %; 12 more than 1 % slower), the same kernels the env var runs there.
  • No gain on the 6 MiniMax-M3 _ldspa scatter rows (−1.3 % to +2.1 %), which the flag maps to reduce like the other MXFP4 scatter rows.
  • Unlike the env var, the flag leaves FlyDSL v1 stage 2s unchanged (the v1 heuristic fallback for untuned shapes too), and FHMoE rejects it. Also unchanged: atomic stage 2s, stage 2s with a bias, FP8-activation scatter, and scatter after a flydsl_moe1 stage 1 (2 kimik3_a4w4 rows; there the stage 1 sorts for the scatter).
  • An AOT build (aiter/aot/flydsl/mxfp4_moe.py) contains the FP8 reduce kernels only when built with AITER_FLYDSL_STAGE2_FP8=1, and never the reduce kernel a scatter row maps to. With the flag, those compile at their first call, or raise under FLYDSL_RUNTIME_RUN_ONLY=1.

Motivation

  • The env var is read per process. A server whose model has a speculative draft cannot give the target model's MoE layers the FP8 route output and keep the draft's MoE at default numerics when both have the same shape: Qwen3.8-Flash-Next's MTP layer has the target's MoE shape (E 513 with the fused shared expert, top-k 11, hidden 2560, inter 640), so both use the same tuned rows. A per-call argument lets the caller choose per layer.
  • [CK] [FlyDSL] MoE stage 2: pick FP8 route output per tuned row; DSv4.1 TP4 rows #8032 (open) selects the FP8 route output per tuned row through the kernel name. A row serves every call with its key, so it cannot separate a target and a draft of the same shape either. The two compose (row name or flag); both edit _flydsl_v2_stage2_wrapper and fused_moe_2stages, so the second to land needs a rebase.
  • Qwen3.8-Flash-Next's 16,384-token row runs a scatter stage 2 (bf16 route rows, then mxfp4_moe_scatter_reduce), which the env var leaves as it is.
  • [FlyDSL] [MoE] Use streaming loads for large FP8 stage-2 reductions #7940 (open) streams the FP8 route-output reduction for route buffers above 192 MiB, which includes this row's; it does not change which calls use FP8 route output.
  • [MoE] Public two-stage fused_moe API: fused_moe_prepare_stage2 / fused_moe_run_stage2 (per-slot output, replaceable stage-2 operands) #7135 (merged) added fused_moe_prepare_stage2 / fused_moe_run_stage2 (per-route outputs, replaced stage-2 operands); FP8 route output is not one of their options. The flag can change the stage-2 kernel, which also decides whether the sort zero-fills the output, so it is chosen with the config, in fused_moe; fused_moe_prepare_stage2 does not take it.

Changes

  • fused_moe, fused_moe_fake, fused_moe_, _fused_moe_impl: stage2_fp8 (default False), passed to get_2stage_cfgs, whose cache key includes it, so both variants coexist in one process.
  • get_2stage_cfgs: after an MXMOE stage 1 (tuned row, inline-sort probe, MXMOE heuristic fallback), _make_mxfp4_metadata runs the stage 2 _stage2_fp8_kernel_name returns, with FP8 route output; after a flydsl_moe1 / opus_moe1 stage 1, a layout reduce stage 2 gets FP8 route output. Each changed config logs one info line: [fused_moe] stage2_fp8: <kernel> writes FP8 route rows.
  • _stage2_fp8_kernel_name: reduce keeps its name; an MXFP4-activation bf16 scatter becomes the plain reduce of the same tile and sort block (no _persist, _nt, _ldspa); anything else returns None (unchanged).
  • _flydsl_v2_stage2_wrapper: FP8 route output when the flag or the env var is set (its error text names both); fused_moe_2stages passes topk_weights for it.
  • Rejected: with FHMoE (listed with its other unsupported arguments) and with deterministic=True (NotImplementedError).
  • op_tests/test_moe_2stage.py: two gfx950 tests and --stage2-fp8.

Scope (gfx950, tables under aiter/configs): the flag changes 324 layout reduce rows (the ones the env var changes; 26 model tables) and 47 MXFP4 scatter rows after an MXMOE stage 1 (DeepSeek-V3 14, Kimi-K2 12, MiniMax-M3 11, GLM-5.3-Flash 6, GLM-5 3, Qwen3.8-Flash-Next 1). These rows span 1 to 131,072 tokens, so decode-size calls change too. Measured: the Qwen3.8-Flash-Next 16,384-token call, and 252 of these rows in the in-tree benchmark below.

Performance (if applicable)

Hardware Workload/configuration Before After Change
MI355X Qwen3.8-Flash-Next MoE, 16,384 tokens, random MXFP4 weights 1,137.6 µs 918.4 µs −19.3 %
MI355X same, layer-20 checkpoint weights and router 1,115.2 µs 905.2 µs −18.8 %
MI355X in-tree benchmark, 200 changed tuned rows above 64 tokens – – median −13.4 % (−30.0 % to +2.3 %)
MI355X in-tree benchmark, 52 changed tuned rows at ≤ 64 tokens – – median −0.1 % (−5.0 % to +4.5 %)
Method, in-tree benchmark and stage-2 split
  • Per call: fused_moe as SGLang calls it (E 513, top-k 11, hidden 2560, inter 640, per_1x32, separated gate/up, shuffled weights, output=, sorting policy 2); 25 timed calls per round, each after a 512 MB write and a GPU sleep, CUDA events; the median of 3 rounds that alternate the arms in one process. main without this PR (separate process): 1,121.7 / 1,110.5 µs. A second job: 1,114.2 → 902.4 µs (−19.0 %) / 1,109.1 → 901.1 µs (−18.8 %). Layer 20: the routed and shared experts and the router of layer 20 of amd/Qwen3.8-Flash-Next-Quark-MXFP4-AttnFP8-PLEFP8, on N(0, 1) activations.
  • Per 16,384-token prefill chunk, the model's 48 MoE layers save 10.0 to 10.5 ms. Stage 2 (random weights): GEMM 494.0 → 346.6 µs; combine 181.2 µs (moe_scatter_reduce) → 109.4 µs (moe_reduction, FP8 rows).
  • In-tree: python3 op_tests/test_moe_2stage.py --no-legacy --csv-filter _scatter_sbm128 _reduce [--stage2-fp8], run off, on, off, on; minimum of the two runs per arm.
Tuned rows the flag changes Rows Change, median (range) calc_diff off → on
reduce, > 64 tokens 153 −14.0 % (−30.0 % to +2.3 %) ≤ 1.13e-3 → ≤ 1.48e-3 (all 205 reduce rows)
reduce, ≤ 64 tokens 52 −0.1 % (−5.0 % to +4.5 %)
MXFP4 scatter → reduce 41 −13.1 % (−22.6 % to −2.7 %) ≤ 6.1e-4 → ≤ 9.7e-4
MXFP4 scatter _ldspa → reduce 6 +1.1 % (−1.3 % to +2.1 %) 3.7e-6 → 3.6e-4
Unchanged (control): FP8-activation scatter, FlyDSL v1 reduce 9 + 9 −0.3 %, −0.8 % equal (3 v1 rows vary between runs of each arm)
  • Qwen3.8-Flash-Next's row in this harness: 1,030.1 → 882.3 µs (−14.3 %).

  • The harness runs the untagged rows for the GPU's CU count, so 252 of the 371 rows the flag changes ran; the other 119 did not (82 with a _tag value, 61 of them DeepSeek-V4.1; 28 for 128-CU GPUs; 9 the harness skips for its runtime mode).

  • Setup: image lmsysorg/sglang-rocm:v0.5.23-rocm10-mi35x-20261117 with this branch copied over its AITER (JIT modules rebuilt) and FlyDSL 0.5.0; one MI355X.

Accuracy

  • Default output: bitwise equal to main's in the per-call benchmark.
  • Relative L2 to an fp32 reference (exact FP4 weights, unquantized activations, 256 tokens), default → flag: random weights 0.2126 → 0.2142, layer 20 0.2156 → 0.2171. Flag against default, all tokens: 2.67 % / 2.65 %.
  • In-tree benchmark calc_diff (it warns above 1e-3 and fails above 0.01): reduce rows ≤ 1.13e-3 → ≤ 1.48e-3 (above 1e-3: 13 → 22 of 205 rows), MXFP4 scatter rows ≤ 6.1e-4 → ≤ 9.7e-4; every row passed the harness's assertion.
  • GSM8K, 1,319 questions, Qwen3.8-Flash-Next with the flag on its target MoE layers ([Perf] [AMD] quark: Add opt-in FP8 stage-2 route output for MXFP4 MoE sgl-project/sglang#51198), every prompt long enough (9,000–9,435 tokens) to reach the 16,384-token row: strict 97.12 % → 97.65 %, flexible the same (2 questions right only without the flag, 9 only with it; McNemar p = 0.07). This run used the image's own AITER (581579ab5) with this change applied to its fused_moe.py (the same added lines, without the deterministic hunk and [FlyDSL] MoE stage2: MXFP4 route-output reduction next to the FP8 one #7706's condition, which 581579ab5 predates).

Testing

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

MI355X (gfx950) only: the changed paths are gfx950 FlyDSL, and the new tests run on gfx950 only.

Documentation

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

Dependencies

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

Breaking Changes

None.

PR Self-Review

Reviewed revision

286cda3

Behavior and intent

Without the argument, behavior is unchanged (main's output bit for bit in the benchmark above). With it, a call whose stage 2 is a FlyDSL layout reduce, or an MXFP4-activation bf16 scatter after an MXMOE stage 1, writes FP8 route rows and sums them in fp32, as the env var does for every call; the cache key keeps both variants in one process, so a caller can choose per layer.

Validation and unverified scope

python3 op_tests/test_moe_2stage.py --no-legacy --csv-filter _scatter_sbm128 _reduce [--stage2-fp8]
  • The script runs both new tests (gfx950 only) before its cases, and CI runs the script: both pass. With main's fused_moe.py the file stops at the import of _stage2_fp8_kernel_name, and fused_moe has no stage2_fp8. Each of 9 mutations fails one of them on an assertion (flag ignored by the stage-2 wrapper, topk_weights not passed, no scatter mapping, flag dropped at the tuned-row, _resolve_metadata or flydsl_moe1 site, partial without the flag, FP8-activation scatter mapped, reduce not kept).
  • Also on this branch: op_tests/test_moe_stage_api.py ([MoE] Public two-stage fused_moe API: fused_moe_prepare_stage2 / fused_moe_run_stage2 (per-slot output, replaceable stage-2 operands) #7135's staged API) 16 passed, op_tests/flydsl_tests/test_flydsl_moe.py 59 passed.
  • Unverified: the 119 changed rows the harness does not run, expert parallelism with the flag, CUDA-graph capture outside the SGLang run, gfx1250.

PR-specific question

Both new tests fail before this PR: fused_moe has no stage2_fp8 argument and _stage2_fp8_kernel_name does not exist. test_stage2_fp8_per_call is the regression test: no existing test calls fused_moe with FP8 route output for one call only, and test_flydsl_stage2_fp8_ep_reduction covers the env var's reduction, not the per-call choice or the scatter mapping. test_stage2_fp8_kernel_name guards which kernels the flag maps.

Changed op tests

op_tests/test_moe_2stage.py:

  • test_stage2_fp8_per_call: at two tuned rows (Qwen3.8-Flash-Next 16,384 tokens, MXMOE stage 1 and scatter; DeepSeek-V3 MXFP4 2 tokens, flydsl_moe1 stage 1 and reduce), the flag's output is bitwise the env var's output with the row's reduce kernel, differs from the default by under 5 %, and a call without the flag keeps the default output.
  • test_stage2_fp8_kernel_name: MXFP4 scatter (with _nt / _ldspa) → reduce, reduce_persist kept, FP8-activation scatter and atomic → None.
  • --stage2-fp8: runs the tuned-row cases with the flag (the benchmark above).

This change was developed with AI assistance (Claude); I reviewed every line and ran the tests listed.

fused_moe(stage2_fp8=True) makes a FlyDSL layout reduce stage 2 of one
call write each route's output as FP8 (e4m3, an e8m0 scale per 32
columns) and sum the routes in fp32, which AITER_FLYDSL_STAGE2_FP8=1
does for every call. With a flydsl_mxmoe_g1 stage 1, a scatter stage 2
with MXFP4 activations and bf16 output runs as the reduce stage 2 of the
same tile. Other stage 2s (FlyDSL v1 included) and stage 2s with a bias
are unchanged. Default False; with True the output is not bitwise equal
to the default.

The flag is part of the get_2stage_cfgs cache key, so a caller can set
it for some layers of a model (for example its target layers) and not
for others with the same shapes. The change is logged once per config.

op_tests/test_moe_2stage.py: a per-call test at two tuned rows (an
MXMOE scatter row and a flydsl_moe1 reduce row), a kernel-name test,
and --stage2-fp8 for the tuned-row cases.

Co-authored-by: Claude <[email protected]>
Signed-off-by: Rony Leppänen <[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 8174 --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.

eppaneamd added a commit to eppaneamd/InferenceX that referenced this pull request Nov 20, 2026
…ithin the admission

Patches 22, 23, 26 and 27; c12, c24, c48 and c80 set their two variables:
- 22-23 FP8 stage-2 route output for the target's MXFP4 MoE (ROCm/aiter#8174 in the image's
  AITER checkout, sgl-project/sglang#51198 wiring), SGLANG_AITER_MOE_STAGE2_FP8
- 26 brings patch 15 to the head of sgl-project/sglang#50863; SGLANG_USE_HIP_HC_PREFILL_TRITON
  replaces SGLANG_AMD_HC_PREFILL, which 26 no longer reads
- 27 decode FP8 quant fused into the GDN gated norm and the attention output gate
  (sgl-project/sglang#51023), no variable

cuda-graph-bs-decode at c12-c80: every size to the admission at c12 and c24, every size to 64
and every 8 to 128 at c48 and c80. c48 also sets strip-thinking-cache and
mamba-max-states-per-path 2, as c80 does. Patch 12's header names ROCm/aiter#7556's head
1c8725c7f3 and how the patch differs from it. c1 and c2 are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Rony Leppänen <[email protected]>

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant