Skip to content

[Perf] act_mul_and_mxfp4_quant: Round to E2M1 with the gfx950 instruction - #7279

Closed
vorapolsiloai wants to merge 3 commits into
ROCm:mainfrom
vorapolsiloai:prwork/lp-act-mul-mxfp4-cvt-pr
Closed

vorapolsiloai wants to merge 3 commits into
ROCm:mainfrom
vorapolsiloai:prwork/lp-act-mul-mxfp4-cvt-pr

Conversation

@vorapolsiloai

Copy link
Copy Markdown
Contributor

(The title automation adds the [Triton/Gluon] tag.)

Summary

_act_mul_and_dynamic_mxfp4_quant_kernel now rounds to E2M1 with v_cvt_scalef32_pk_fp4_f32 on gfx950, as
_mxfp4_quant_op(USE_ASM=True) already does for the RoPE and KV-cache MXFP4 kernels. The output bytes are unchanged;
the kernel is 24-30% faster at prefill sizes on MI355X.

Stacked on #7116 (same call site): the last commit of the branch is this PR; it is a three-line change once #7116 merges.

Motivation

The fused activation-and-mul + MXFP4 quantization of the down-projection input rounds to E2M1 with the integer bit
arithmetic of _mxfp4_pack_bits. On MI355X this keeps the kernel ALU-bound, well below HBM bandwidth: for the
Qwen3.8-27B dense MLP (gate_up [16384, 2 x 17408] bf16, 1.29 GB moved per call) it takes 325 us, 4.0 TB/s. In a
long-prompt prefill that is 20 ms of every 16384-token chunk (64 layers), about 5% of the GPU time of a 131k-token prompt.

_mxfp4_quant_op already has USE_ASM: on gfx950 it rounds with v_cvt_scalef32_pk_fp4_f32, one instruction per pair
of values, and gives the same byte as the bit arithmetic for every non-NaN input (test_mxfp4_quant_op_asm_matches_bits).

Changes

  • _act_mul_and_dynamic_mxfp4_quant_kernel passes USE_ASM=True to _mxfp4_quant_op.
  • Other GPUs keep the bit arithmetic: _mxfp4_pack_op only takes the instruction path when is_hip_cdna4().
  • No API change; FP4 bytes and E8M0 scales are identical on every GPU.

Performance (if applicable)

Kernel time from 10 back-to-back calls (so the Python wrapper's CPU time is hidden), median of 15; silu, "roundup"
scales, shuffled scales, bf16 input [M, 2 x N]:

Hardware Workload/configuration Before After Change
MI355X M 16384, N 17408 325 us (4.0 TB/s) 248 us (5.2 TB/s) -24%
MI355X M 8192, N 17408 173 us 127 us -27%
MI355X M 4096, N 17408 92 us 64 us -30%
MI355X M 16384, N 8704 175 us 125 us -29%
MI355X M 8192, N 8704 91 us 65 us -29%
MI355X M <= 1024 ~25 us ~25 us launch-bound

Single calls including the wrapper: 395 -> 272 us at M 16384 (the number in the commit message). Compiled kernel (M 16384
x N 17408 launch): 1228 -> 857 VALU instructions, 94 -> 76 VGPRs, 89 -> 47 SGPRs, no spills.

End to end: Qwen3.8-27B MXFP4 (amd/Qwen3.8-27B-Quark-AWQ-MXFP4) on one MI355X, TP1, SGLang (this kernel is its MLP
activation + down_proj input quant), chunked prefill 16384, FP8 KV cache; python3 -m sglang.bench_serving --backend sglang-oai-chat --dataset-name random --random-range-ratio 1.0 --random-output-len 1 --max-concurrency 1 --request-rate inf --num-prompts 32 --warmup-requests 1 --flush-cache; same GPU, before / after / after / before, mean of the two median
TTFTs per side:

input tokens median TTFT before (ms) after (ms) change input tok/s change
100000 2731 2703 -28 ms (-1.0%) +1.1%
120000 3428 3400 -27 ms (-0.8%) +1.0%
140000 4182 4140 -42 ms (-1.0%) +1.0%
160000 5023 4982 -40 ms (-0.8%) +0.8%
180000 5854 5803 -51 ms (-0.9%) +0.8%
200000 6827 6774 -52 ms (-0.8%) +0.8%

The kernel saving is ~4.9 ms per 16384-token chunk (64 layers), so -30 ms at 100k and -60 ms at 200k expected.

Testing

  • Unit tests added or updated (the existing tests cover it: the byte-exact comparisons below)
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X

Documentation

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

Internal rounding path only; inputs, outputs and options are unchanged.

Dependencies

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

Breaking Changes

None.

PR Self-Review

Reviewed revision

0c96ad3

Behavior and intent

Same output bytes, fewer instructions: the kernel's E2M1 rounding uses the gfx950 convert instruction instead of integer
bit arithmetic. Before, the kernel was ALU-bound at 4.0 TB/s on MI355X; after, it streams at 5.2 TB/s, close to what
other streaming kernels reach on this GPU.

Validation and unverified scope

  • Bit-exact: FP4 bytes and E8M0 scales equal to the current kernel's (torch.equal) for M = 1 ... 16384 rows, N 17408, on
    normal, wide-range (magnitudes 1e-30 ... 1e4: saturating and subnormal values) and all-zero inputs.
  • MI355X (gfx950), ROCm 7.2, this branch: op_tests/triton_tests/fusions/test_activation.py 519 passed, 228 skipped
    (shuffled scales with N not a multiple of 512, by design); test_fused_silu_mul.py 23 passed;
    op_tests/triton_tests/quant/test_quant_mxfp4.py 77 passed, 48 skipped, including
    test_mxfp4_quant_op_asm_matches_bits. The three test_dynamic_mxfp4_quant_gluon_matches_triton cases fail the same
    way with and without this change: the test image's Triton has no gl.amd.cdna4.scaled_downcast.
  • End to end (Qwen3.8-27B MXFP4, TP1, SGLang): greedy output ids identical with and without the change on four reference
    prompts (100k-200k tokens and a short one); needle-in-a-haystack retrieval 30/30 at 100k-200k tokens; GSM8K-200
    (sglang.test.few_shot_gsm8k) 0.950 batched and 0.960 one question at a time (0.935 / 0.955 without the change;
    run-to-run spread is about 2 points).
  • Not run: MI250X / MI300X. There the kernel keeps the bit-arithmetic path (is_hip_cdna4() is false), so the compiled
    code is unchanged.

PR-specific question

(Answered once the automated PR Self-Review comment posts its question.)

Changed op tests

None.

…ndup" and round_to_input_dtype

act_mul_and_mxfp4_quant took scaling_mode but always launched the kernel
with SCALING_MODE=0, and the kernel did not pass SCALING_MODE on to
_mxfp4_quant_op, so every mode gave the "even" scale. Map "even" -> 0 (the
default), "ceil" -> 1 and the new "roundup" -> 2, and raise ValueError for
anything else.

"roundup" is the scale per_1x32_f4_quant_hip computes: the block amax
floored at 1e-10 (aiter::kAmaxFloor), then
fp_f32_to_e8m0_scale<MxScaleRoundMode::RoundUp>, the exponent of
amax * (1/6) plus one if its mantissa is nonzero. It is "ceil" with that
floor in place of 6 * 2^-126: every block whose amax is below 1e-10, an
all-zero block included, gets scale byte 92 as from the HIP quant, where
"ceil" gives an all-zero block 1.

round_to_input_dtype=True rounds act(x1) to x.dtype, and act(x1) * x2 to
x.dtype again, before quantizing: the values an unfused act-and-mul in
x.dtype writes (F.silu(a) * b on bf16 tensors). With "roundup" the output
is then that act-and-mul followed by per_1x32_f4_quant_hip, byte for byte.
The name and default follow round_to_input_dtype of
fused_reduce_act_mul_and_mxfp4_quant in ROCm#6024, which rounds only the
product.

The defaults ("even", no rounding) keep the existing output.

Tests: fp4 bytes and the scale bytes the valid rows own against a bf16
silu-and-mul followed by per_1x32_f4_quant_hip, for M in {1, 2, 8, 9, 32,
33, 255, 256, 257, 2048}, N in {256, 8704, 17408}, two magnitudes, plain
and shuffled scales, with an all-zero and a tiny block; the scale byte of
each mode; the rounding; the ValueError; and "roundup" in the
_mxfp4_quant_op ASM-vs-bits test.
- One E8M0 ceil-scale helper: _mxfp4_ceil_scale_from_amax takes the amax
  floor as AMAX_FLOOR; "roundup" passes 1e-10. Same bytes as before for
  every finite or Inf ratio; a NaN ratio now gives 0xFF, not a wrapped 0.
- Move silu_exp2_ref to op_tests/triton_tests/utils/activation_ref.py.
- Tests: explicit float32 dtype for randn, device="cuda" for the
  scaling_mode rejection input.
… instruction

The fused activation-and-mul MXFP4 kernel rounds to E2M1 with the bit
arithmetic of _mxfp4_pack_bits. Pass USE_ASM=True so that on gfx950 it uses
v_cvt_scalef32_pk_fp4_f32 (one instruction per pair of values), which gives
the same byte for every non-NaN input (test_mxfp4_quant_op_asm_matches_bits);
other GPUs keep the bit arithmetic.

MI355X, silu, "roundup" scales, shuffled scales, gate_up [16384, 2 x 17408]
bf16: 395 -> 272 us per call, fp4 bytes and scales identical.
@github-actions

github-actions Bot commented Nov 1, 2026

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 (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 gfx1201 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 7279 --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.

@aiter-bot

Copy link
Copy Markdown

NOT READY FOR HUMAN REVIEW

PR Self-Review

Revision 0c96ad36ef5e · 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. What behavior would each changed op test fail on before this PR, and why is that coverage not already provided by an existing canonical test?
  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 — PR-specific question is too short to show concrete reasoning (10 units; need 14)
  • FAIL — changed op test not explained in Changed op tests: op_tests/triton_tests/fusions/test_activation.py
  • FAIL — changed op test not explained in Changed op tests: op_tests/triton_tests/fusions/test_fused_silu_mul.py
  • FAIL — changed op test not explained in Changed op tests: op_tests/triton_tests/quant/test_quant_mxfp4.py
  • FAIL — changed op test not explained in Changed op tests: op_tests/triton_tests/utils/activation_ref.py
  • FAIL — Changed op tests must explain why each changed op test is necessary and non-redundant

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

vorapolsiloai added a commit to vorapolsiloai/aiter that referenced this pull request Nov 2, 2026
… instruction

The fused activation-and-mul MXFP4 kernel rounds to E2M1 with the bit
arithmetic of _mxfp4_pack_bits. Pass USE_ASM=True so that on gfx950 it uses
v_cvt_scalef32_pk_fp4_f32 (one instruction per pair of values), which gives
the same byte for every non-NaN input (test_mxfp4_quant_op_asm_matches_bits);
other GPUs keep the bit arithmetic.

MI355X, silu, "roundup" scales, shuffled scales, gate_up [16384, 2 x 17408]
bf16: 395 -> 272 us per call, fp4 bytes and scales identical.

(cherry picked from commit 0c96ad3)
Folded in from ROCm#7279 (0c96ad3)
@vorapolsiloai

Copy link
Copy Markdown
Contributor Author

Closing in favour of #7116, which now carries this PR's commit (same call site, same author; this PR was stacked on it):

commit here now in #7116 change
0c96ad36ef a93eb33224 act_mul_and_mxfp4_quant: Round to E2M1 with the gfx950 instruction

Reason: consolidation of stacked PRs by the same author, so the two changes to act_mul_and_mxfp4_quant are reviewed in one place. The code is unchanged: the commit was cherry-picked as it is (git cherry-pick -x, original message kept, plus a Folded in from line), and #7116's new head has a tree byte-identical to this PR's head. The description, kernel and end-to-end numbers and the validation moved into #7116's body.

Please continue the review on #7116.

vorapolsiloai added a commit to vorapolsiloai/aiter that referenced this pull request Nov 17, 2026
… instruction

The fused activation-and-mul MXFP4 kernel rounds to E2M1 with the bit
arithmetic of _mxfp4_pack_bits. Pass USE_ASM=True so that on gfx950 it uses
v_cvt_scalef32_pk_fp4_f32 (one instruction per pair of values), which gives
the same byte for every non-NaN input (test_mxfp4_quant_op_asm_matches_bits);
other GPUs keep the bit arithmetic.

MI355X, silu, "roundup" scales, shuffled scales, gate_up [16384, 2 x 17408]
bf16: 395 -> 272 us per call, fp4 bytes and scales identical.

(cherry picked from commit 0c96ad3)
Folded in from ROCm#7279 (0c96ad3)
azaidy pushed a commit that referenced this pull request Nov 24, 2026
…ndup" and round_to_input_dtype; round to E2M1 with the gfx950 instruction (#7116)

* [Triton/Gluon] act_mul_and_mxfp4_quant: honour scaling_mode, add "roundup" and round_to_input_dtype

act_mul_and_mxfp4_quant took scaling_mode but always launched the kernel
with SCALING_MODE=0, and the kernel did not pass SCALING_MODE on to
_mxfp4_quant_op, so every mode gave the "even" scale. Map "even" -> 0 (the
default), "ceil" -> 1 and the new "roundup" -> 2, and raise ValueError for
anything else.

"roundup" is the scale per_1x32_f4_quant_hip computes: the block amax
floored at 1e-10 (aiter::kAmaxFloor), then
fp_f32_to_e8m0_scale<MxScaleRoundMode::RoundUp>, the exponent of
amax * (1/6) plus one if its mantissa is nonzero. It is "ceil" with that
floor in place of 6 * 2^-126: every block whose amax is below 1e-10, an
all-zero block included, gets scale byte 92 as from the HIP quant, where
"ceil" gives an all-zero block 1.

round_to_input_dtype=True rounds act(x1) to x.dtype, and act(x1) * x2 to
x.dtype again, before quantizing: the values an unfused act-and-mul in
x.dtype writes (F.silu(a) * b on bf16 tensors). With "roundup" the output
is then that act-and-mul followed by per_1x32_f4_quant_hip, byte for byte.
The name and default follow round_to_input_dtype of
fused_reduce_act_mul_and_mxfp4_quant in #6024, which rounds only the
product.

Both ceil modes share _mxfp4_ceil_scale_from_amax, which takes the floor
as AMAX_FLOOR after the MAX_SCALE_E8M0 cap; "roundup" passes 1e-10. The
"ceil" bytes do not change. silu_exp2_ref moves to
op_tests/triton_tests/utils/activation_ref.py, shared by both tests.

The defaults ("even", no rounding) keep the existing output.

Tests: fp4 bytes and the scale bytes the valid rows own against a bf16
silu-and-mul followed by per_1x32_f4_quant_hip, for M in {1, 2, 8, 9, 32,
33, 255, 256, 257, 2048}, N in {256, 8704, 17408}, two magnitudes, plain
and shuffled scales, with an all-zero and a tiny block; the scale byte of
each mode; the rounding; the ValueError; and "roundup" in the
_mxfp4_quant_op ASM-vs-bits test.

Signed-off-by: Vorapol Assavasangthong <[email protected]>

* [Triton/Gluon] act_mul_and_mxfp4_quant: Round to E2M1 with the gfx950 instruction

The fused activation-and-mul MXFP4 kernel rounds to E2M1 with the bit
arithmetic of _mxfp4_pack_bits. Pass USE_ASM=True so that on gfx950 it uses
v_cvt_scalef32_pk_fp4_f32 (one instruction per pair of values), which gives
the same byte for every non-NaN input (test_mxfp4_quant_op_asm_matches_bits);
other GPUs keep the bit arithmetic.

MI355X, silu, "roundup" scales, shuffled scales, gate_up [16384, 2 x 17408]
bf16: 395 -> 272 us per call, fp4 bytes and scales identical.

(cherry picked from commit 0c96ad3)
Folded in from #7279 (0c96ad3)

---------

Signed-off-by: Vorapol Assavasangthong <[email protected]>
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.

2 participants