Repository navigation
[Triton/Gluon] [Perf] MXFP8 MoE: keep the MFMA in FP8 when MX_SCALE_BLOCK_K == 1 - #5832
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
PR title tags & labels: |
There was a problem hiding this comment.
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.
6296202 to
98cfd57
Compare
|
Thanks for the reviews! Summary of the feedback and what changed:
Verified on MI325X (gfx942):
@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. |
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]>
2c612ea to
4c0645d
Compare
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]>
4c0645d to
a82f6af
Compare
Boss2002n
left a comment
There was a problem hiding this comment.
Approving on behalf of Team_AITER
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]>
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.
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.
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.
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.
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]>
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]>
Motivation
The
USE_FNUZpath added by #5658 upcasts both operands to fp32 and broadcasts both scale tiles across the full K tile, plus atl.trans, beforetl.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
and
MX_SCALE_BLOCK_K = BLOCK_K // MX_PACK_DIVISOR, soMX_SCALE_BLOCK_Kis 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: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_Ktail. TheMX_SCALE_BLOCK_K > 1branches 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
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: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=96andK=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-FP8with 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,2048and64,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.