Skip to content

[Triton/Gluon] [Perf] MXFP8 MoE: keep the MFMA in FP8 when MX_SCALE_BLOCK_K == 1 - #5832

Merged
Boss2002n merged 4 commits into
ROCm:mainfrom
akii96:perf/mxfp8-moe-fp8-mfma
Oct 3, 2026
Merged

Boss2002n merged 4 commits into
ROCm:mainfrom
akii96:perf/mxfp8-moe-fp8-mfma

Conversation

@akii96

@akii96 akii96 commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Motivation

The USE_FNUZ path added by #5658 upcasts both operands to fp32 and broadcasts both scale tiles across the full K tile, plus a tl.trans, before tl.dot. That promotes an FP8 MFMA to an FP32 one at 4x the operand footprint.

It is never a win. #5658 itself closed off the other configurations: in response to review comment r4045560377 the wrapper now asserts

assert quant_block_size == 32, "_moe_gemm_a8w8 hardcodes MX_PACK_DIVISOR=32"

and MX_SCALE_BLOCK_K = BLOCK_K // MX_PACK_DIVISOR, so MX_SCALE_BLOCK_K is always 1 on every MXFP8 MoE call. That means one scale per operand per K-step, constant across the dot.

When the scale is constant across the dot, scaling the [BLOCK_M, BLOCK_N] result is equivalent to scaling the [*, BLOCK_K] operands, and the operands can stay in FP8:

acc += (
    tl.dot(x, w, input_precision="ieee")
    * tl.sum(a_sc, axis=1)[:, None]
    * tl.sum(b_sc, axis=1)[None, :]
)

This keeps the merged kernel and fixes that path. It does not revert #5658.

Technical Details

Applied to both the main loop and the not EVEN_K tail. The MX_SCALE_BLOCK_K > 1 branches are preserved byte for byte.

Those branches are unreachable today, since the assert pins BLOCK_K = 32. They also disagree with each other: the main loop broadcasts the weight scale on axis 2 and then transposes, while the tail broadcasts on axis 1 and does not. At most one can be correct. I have left both alone, since fixing an unreachable path is a separate concern, but reviewers should know those configurations are untested and that this PR does not try to measure them.

Test Plan

pytest op_tests/triton_tests/moe/test_moe_gemm_mxfp8.py -v
python op_tests/op_benchmarks/triton/bench_moe_gemm_mxfp8.py

Test Result

MI325X (gfx942), ROCm 7.2.3, idle node, Triton cache cleared between arms, best of 5 timing runs.

E=32, N=2048, K=6144, weights already stored K-major:

tokens main this PR
8 0.99 ms 0.30 ms
64 0.99 ms 0.31 ms
512 0.99 ms 0.33 ms
2048 1.00 ms 0.41 ms

About 3.2x at decode-sized batches.

The same measurement with NK weights, where both arms pay the wrapper's per-call transpose, shows the hoist alone is worth about 0.7 ms (3.43 ms down to 2.75 ms). The remaining gap between 2.75 ms and 0.31 ms is that transpose, which the companion wrapper PR removes for callers that already store K-major.

Correctness across 13 shape and layout configurations with random E8M0 scales, including K=96 and K=32, plus ragged group sizes [0,7,64,1,100,0,84,0] with zero-token experts: cosine >= 0.999999 against an fp32 reference in every case.

End to end on vLLM 0.30.1rc1, 8x MI325X, TP=8, tencent/Hy4-preview-FP8 with real weights, with both this PR and the wrapper PR applied: decode throughput 1.68 to 10.01 tok/s, TPOT 97.65 ms, GSM8K unchanged within noise (84.00% vs 85.50% last-number, n=200, 0 errors in both arms).

Notes for reviewers

Please add ci:triton-300x. The default Triton CI targets gfx950, and this path is exercised on gfx942.

The companion wrapper fix is akii96:fix/mxfp8-moe-wrapper. The two branches are cut from the same base and touch disjoint files, so either can land first. This one alone is numerically correct but not CUDA graph capturable, since that fix lives in the wrapper PR.

The benchmark's in-tree shapes are DSv4 oriented (8,7168,2048 and 64,2048,7168), so the Hy4 MoE shape above is not among them.


AI assistance was used to investigate and prepare this change. The submitter has reviewed every changed line, ran the tests and measurements reported above, and is responsible for the result.

@akii96
akii96 requested a review from a team September 25, 2026 01:19
@github-actions

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; main branch always runs both MI35X and MI300X
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 5832 --add-label <label>

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 opt this PR out.

@github-actions github-actions Bot changed the title [Perf] MXFP8 MoE: keep the MFMA in FP8 when MX_SCALE_BLOCK_K == 1 [Triton/Gluon] [Perf] MXFP8 MoE: keep the MFMA in FP8 when MX_SCALE_BLOCK_K == 1 Sep 25, 2026
@Boss2002n
Boss2002n requested a lite review from Copilot September 25, 2026 02:07

Copilot AI 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.

Copilot review overview

🔵 Needs a closer look

The changes require final human review because they are too complex or risky for automated approval.

Review effort: Lite
Findings: None

What changed in this PR

Optimizes the FNUZ MXFP8 MoE GEMM path by keeping FP8 operands when one scale covers each K tile.

Changes:

  • Adds accumulator-side scaling for MX_SCALE_BLOCK_K == 1.
  • Preserves the existing FP32 fallback branches.
  • Adds targeted correctness tests with non-uniform activation and weight scales.
File Description
op_tests/​triton_tests/​moe/​test_moe_gemm_mxfp8.py Updated as part of this pull request.
aiter/​ops/​triton/​_triton_kernels/​moe/​moe_op_gemm_a8w8.py Updated as part of this pull request.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@akii96
akii96 force-pushed the perf/mxfp8-moe-fp8-mfma branch 2 times, most recently from 6296202 to 98cfd57 Compare September 25, 2026 07:00
@azaidy
azaidy requested review from lburzawa and nsusanto September 25, 2026 14:01
nsusanto
nsusanto previously approved these changes Sep 29, 2026

@nsusanto nsusanto 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

@akii96

akii96 commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews! Summary of the feedback and what changed:

Reviewer Ask Action
@nsusanto FYI: the non even K + USE_FNUZ path is unreachable and untested Acknowledged. The wrapper still pins BLOCK_K = 32 with K % 32 == 0, so that path stays unreachable.
@lburzawa Use tl.reshape / tl.ravel instead of tl.sum to read the scale Switched to tl.reshape in 8bee39b. Output is bitwise identical to the previous head and perf is unchanged
@lburzawa Remove the extra UT Removed in 8bee39b.

Verified on MI325X (gfx942):

  • test_moe_gemm_mxfp8.py passes
  • Output is bitwise identical to the previous head across 7 shapes
  • Perf is unchanged within noise

@lburzawa @nsusanto could you take another look? The push dismissed the earlier approval.

Also, could someone review the companion wrapper PR #5831? It removes the per call transpose and makes the path CUDA graph capturable. The full speedup needs both PRs.

nsusanto pushed a commit to akii96/aiter that referenced this pull request Oct 1, 2026
Address review feedback on ROCm#5832:

- Read the single per-K-step E8M0 scale with tl.reshape instead of
  tl.sum over the length-1 axis, in both the main loop and the tail.
- Drop test_moe_gemm_mxfp8_matches_per_operand_scaling; the existing
  correctness test already checks the kernel against an operand-scaled
  fp32 reference with non-uniform scales.

Signed-off-by: Aakif Nawaz <[email protected]>
@nsusanto
nsusanto force-pushed the perf/mxfp8-moe-fp8-mfma branch from 2c612ea to 4c0645d Compare October 1, 2026 15:28
akii96 and others added 3 commits October 1, 2026 13:17
The USE_FNUZ path upcasts both operands to fp32 and broadcasts both scale tiles
across the K tile (plus a tl.trans) before tl.dot. That promotes an FP8 MFMA to
an FP32 one at 4x operand footprint.

It is never a win. ROCm#5658 asserts quant_block_size == 32 and
MX_SCALE_BLOCK_K = BLOCK_K // MX_PACK_DIVISOR, so MX_SCALE_BLOCK_K is always 1:
one scale per operand per K-step, constant across the dot. Scaling the
[BLOCK_M, BLOCK_N] result is then equivalent to scaling the [*, BLOCK_K]
operands, and keeps the inputs in FP8.

Applied to the main loop and the not-EVEN_K tail. The MX_SCALE_BLOCK_K > 1
branches are preserved byte-identically; they are unreachable while the wrapper
pins BLOCK_K, and they disagree on the weight-scale broadcast axis, so at most
one of them is correct.

gfx942, E=32/N=2048/K=6144, weights already K-major:

    T=8     0.99 -> 0.30 ms
    T=64    0.99 -> 0.31 ms
    T=512   0.99 -> 0.33 ms
    T=2048  1.00 -> 0.41 ms

Correctness across 13 shape/layout configs with random E8M0 scales, including
K=96, K=32 and ragged group sizes with zero-token experts: cosine >= 0.999999
vs an fp32 reference.

AI assistance was used. The submitter reviewed every line, ran the tests and
measurements above, and is responsible for the result.

Signed-off-by: akii96 <[email protected]>
Two follow-ups to the previous commit.

Test. The existing correctness tests already fail if the hoist is wrong
(verified by mutation: dropping b_sc makes test_moe_gemm_mxfp8_correctness
fail). What they do not pin down is the identity the optimization rests on,
that with MX_SCALE_BLOCK_K == 1 scaling the [BLOCK_M, BLOCK_N] accumulator
equals scaling the [*, BLOCK_K] operands. This asserts that directly, with
scales that vary along every axis on both operands and N != M, so a transposed
or wrongly broadcast scale cannot pass. Two mutants fail it: swapping the scale
axes, and dropping the weight scale.

The MX_SCALE_BLOCK_K > 1 branches stay uncovered because no in-tree caller can
reach them. moe_gemm_mxfp8 is the only caller passing USE_FNUZ=True and it pins
BLOCK_K = quant_block_size = 32 while asserting K % 32 == 0, so EVEN_K is always
True and MX_SCALE_BLOCK_K is always 1; the other _moe_gemm_a8w8 caller never
sets USE_FNUZ.

Cleanup. a_f32/b_f32 were computed before the MX_SCALE_BLOCK_K branch and left
unused on the == 1 path, which reads as if the change still promotes the MFMA to
FP32. Triton was eliminating them -- timings are unchanged either way -- but
moving them into the else makes the code say what it does. The broadcast comment
moves with them, and the length-1 tl.sum gets a note: a_sc[:, 0] is rejected as
an unsupported tensor index.

Output is bitwise identical before and after the cleanup across 5 shapes
(including the K=96 tail and E=32/N=2048/K=6144); no arithmetic changed.

Signed-off-by: akii96 <[email protected]>
Address review feedback on ROCm#5832:

- Read the single per-K-step E8M0 scale with tl.reshape instead of
  tl.sum over the length-1 axis, in both the main loop and the tail.
- Drop test_moe_gemm_mxfp8_matches_per_operand_scaling; the existing
  correctness test already checks the kernel against an operand-scaled
  fp32 reference with non-uniform scales.

Signed-off-by: Aakif Nawaz <[email protected]>
@nsusanto
nsusanto force-pushed the perf/mxfp8-moe-fp8-mfma branch from 4c0645d to a82f6af Compare October 1, 2026 17:17

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

Approving on behalf of Team_AITER

@Boss2002n
Boss2002n merged commit c99c361 into ROCm:main Oct 3, 2026
akii96 added a commit to akii96/aiter that referenced this pull request Oct 5, 2026
Review feedback on ROCm#5832 asked for fewer, reference-based tests. Apply the
same here:

  kn_weights_match_nk, ragged_groups  folded into test_moe_gemm_mxfp8_correctness
      as parameters, so the K-major view and the ragged/zero-token groups are
      checked against the torch reference instead of bespoke equality tests.
      _make_mxfp8_inputs takes optional group_sizes, removing the
      hand-rolled input setup.
  unwritten_rows_are_zero  one shape instead of two; poisons a single
      output-sized block instead of 32 MB, and compares exactly.
  cuda_graph_capture  one warm-up call instead of three.

Tests in the file: 11 -> 11, test lines vs main: +101 -> +68 -9.

Verified on MI325X (gfx942): 11 passed. Negative controls still bite:
reverting torch.zeros -> torch.empty fails unwritten_rows_are_zero, and
dropping total_tokens (restoring the .item() sync) fails cuda_graph_capture.
black and ruff 0.16.0 (CI pin) clean.

Signed-off-by: Aakif Nawaz <[email protected]>
akii96 added a commit to akii96/aiter that referenced this pull request Oct 7, 2026
Replaces ROCm#5831. Per review, the fixes land on the long-standing
moe_gemm_a8w8 wrapper instead of moe_gemm_mxfp8.py, which ROCm#6035 removes.

- Select USE_FNUZ from the input dtype. float8_e4m3fnuz inputs previously
  failed to compile because tl.dot_scaled rejects fp8e4b8.
- Plain FP8 (static, per-token or per-expert scales) runs tl.dot on the FP8
  operands; scales stay in the fp32 epilogue.
- MXFP8 uses BLOCK_K=32 so each K step carries one E8M0 scale per operand and
  takes the FP8 MFMA path from ROCm#5832 instead of upcasting to fp32. Tiles come
  from the new gfx942 A8W8_MXFP8_FNUZ dispatch table, keyed by block_m so
  decode and prefill get separate configs; long-K decode shapes use split-K.
- Split-K partials accumulate in fp32 and are reduced to the output dtype.
- The matmul output is zero-initialised only when a scatter can leave rows
  unwritten.
- dynamic_mxfp8_quant uses the fnuz range (max 240) for float8_e4m3fnuz.

Weights stored as [E, N, K] are consumed through the [E, K, N] transposed
view, so no per-call copy is made.

Tests (gfx942): test_op_fnuz and test_op_mxfp8_fnuz, 111 passed.
akii96 added a commit to akii96/aiter that referenced this pull request Oct 7, 2026
Replaces ROCm#5831. Per review, the gfx942 support lands on the existing
moe_gemm_a8w8 wrapper instead of moe_gemm_mxfp8.py, which ROCm#6035 removes.

- Pass USE_FNUZ from the input dtype. float8_e4m3fnuz inputs previously
  failed to compile because tl.dot_scaled rejects fp8e4b8.
- Plain FP8 on gfx942 runs tl.dot on the FP8 operands; scales stay in the
  fp32 epilogue.
- MXFP8 on gfx942 uses BLOCK_K=32 so each K step carries one E8M0 scale per
  operand and takes the FP8 MFMA path from ROCm#5832.
- Add the tuned dispatch that a8w4 and a16w4 already use, keyed by
  bm<block_m>_n<N>_k<K>, with a gfx942 table for Hy4-preview-FP8 at TP8.
  NUM_KSPLIT is honoured for the long-K gate/up shapes. Untuned shapes and
  other archs keep the existing heuristic.
- dynamic_mxfp8_quant uses the fnuz range (max 240) for float8_e4m3fnuz.

The test adds one gfx942 case per path (plain FP8, MXFP8 decode, MXFP8
prefill) to test_moe_gemm_a8w8.py.
akii96 added a commit to akii96/aiter that referenced this pull request Oct 7, 2026
Replaces ROCm#5831. Per review, the gfx942 support lands on the existing
moe_gemm_a8w8 wrapper instead of moe_gemm_mxfp8.py, which ROCm#6035 removes.

- Pass USE_FNUZ from the input dtype. float8_e4m3fnuz inputs previously
  failed to compile because tl.dot_scaled rejects fp8e4b8.
- Plain FP8 on gfx942 runs tl.dot on the FP8 operands; scales stay in the
  fp32 epilogue.
- MXFP8 on gfx942 uses BLOCK_K=32 so each K step carries one E8M0 scale per
  operand and takes the FP8 MFMA path from ROCm#5832.
- Add the tuned dispatch that a8w4 and a16w4 already use, keyed by
  bm<block_m>_n<N>_k<K>, with a gfx942 table for Hy4-preview-FP8 at TP8.
  NUM_KSPLIT is honoured for the long-K gate/up shapes. Untuned shapes and
  other archs keep the existing heuristic.
- dynamic_mxfp8_quant uses the fnuz range (max 240) for float8_e4m3fnuz.

The test adds one gfx942 case per path (plain FP8, MXFP8 decode, MXFP8
prefill) to test_moe_gemm_a8w8.py.
akii96 added a commit to akii96/aiter that referenced this pull request Oct 7, 2026
Replaces ROCm#5831. Per review, the gfx942 support lands on the existing
moe_gemm_a8w8 wrapper instead of moe_gemm_mxfp8.py, which ROCm#6035 removes.

- Pass USE_FNUZ from the input dtype. float8_e4m3fnuz inputs previously
  failed to compile because tl.dot_scaled rejects fp8e4b8.
- Plain FP8 on gfx942 runs tl.dot on the FP8 operands; scales stay in the
  fp32 epilogue.
- MXFP8 on gfx942 uses BLOCK_K=32 so each K step carries one E8M0 scale per
  operand and takes the FP8 MFMA path from ROCm#5832.
- Add the tuned dispatch that a8w4 and a16w4 already use, keyed by
  bm<block_m>_n<N>_k<K>, with a gfx942 table for Hy4-preview-FP8 at TP8.
  NUM_KSPLIT is honoured for the long-K gate/up shapes. Untuned shapes and
  other archs keep the existing heuristic.
- dynamic_mxfp8_quant uses the fnuz range (max 240) for float8_e4m3fnuz.

The test adds one gfx942 case per path (plain FP8, MXFP8 decode, MXFP8
prefill) to test_moe_gemm_a8w8.py.
akii96 added a commit to akii96/aiter that referenced this pull request Oct 8, 2026
gfx942 uses the fnuz FP8 encoding, which tl.dot_scaled does not accept, so
moe_gemm_a8w8 could not run there and MXFP8 MoE models fell back to
dequantizing expert weights to BF16 at load.

USE_FNUZ now follows the input dtype. Plain FP8 runs tl.dot on the FP8
operands. MXFP8 uses BLOCK_K=32 so each K step carries one E8M0 scale per
operand and stays on the FP8 MFMA path from ROCm#5832; the tile is fixed before
num_stages is chosen so pipelining matches what launches.

MXFP8 shapes on gfx942 read a tuned table through the same dispatch as a8w4
and a16w4, keyed bm<block_m>_n<N>_k<K>, with NUM_KSPLIT for long K. Plain FP8
keeps the existing heuristic, since the 32-wide K tile is only right for MX.

dynamic_mxfp8_quant scales fnuz to its own range (max 240) instead of the
OCP 448, which otherwise overflows and drops roundtrip cosine to 0.989.

Replaces ROCm#5831.

Signed-off-by: Aakif Nawaz <[email protected]>
akii96 added a commit to akii96/aiter that referenced this pull request Oct 8, 2026
gfx942 uses the fnuz FP8 encoding, which tl.dot_scaled does not accept, so
moe_gemm_a8w8 could not run there and MXFP8 MoE models fell back to
dequantizing expert weights to BF16 at load.

USE_FNUZ now follows the input dtype. Plain FP8 runs tl.dot on the FP8
operands. MXFP8 uses BLOCK_K=32 so each K step carries one E8M0 scale per
operand and stays on the FP8 MFMA path from ROCm#5832; the tile is fixed before
num_stages is chosen so pipelining matches what launches.

MXFP8 shapes on gfx942 read a tuned table through the same dispatch as a8w4
and a16w4, keyed bm<block_m>_n<N>_k<K>, with NUM_KSPLIT for long K. Plain FP8
keeps the existing heuristic, since the 32-wide K tile is only right for MX.

dynamic_mxfp8_quant scales fnuz to its own range (max 240) instead of the
OCP 448, which otherwise overflows and drops roundtrip cosine to 0.989.

Replaces ROCm#5831.

Signed-off-by: Aakif Nawaz <[email protected]>
@akii96
akii96 deleted the perf/mxfp8-moe-fp8-mfma branch October 12, 2026 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants