Repository navigation
[Perf] act_mul_and_mxfp4_quant: Round to E2M1 with the gfx950 instruction - #7279
vorapolsiloai wants to merge 3 commits into
Conversation
…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.
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
NOT READY FOR HUMAN REVIEWPR Self-ReviewRevision 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
Edit the Missing or invalid answers
Shadow mode reports what would block but keeps the status successful. Set |
… 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)
|
Closing in favour of #7116, which now carries this PR's commit (same call site, same author; this PR was stacked on it):
Reason: consolidation of stacked PRs by the same author, so the two changes to Please continue the review on #7116. |
… 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)
…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]>
(The title automation adds the
[Triton/Gluon]tag.)Summary
_act_mul_and_dynamic_mxfp4_quant_kernelnow rounds to E2M1 withv_cvt_scalef32_pk_fp4_f32on 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 theQwen3.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_opalready hasUSE_ASM: on gfx950 it rounds withv_cvt_scalef32_pk_fp4_f32, one instruction per pairof 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_kernelpassesUSE_ASM=Trueto_mxfp4_quant_op._mxfp4_pack_oponly takes the instruction path whenis_hip_cdna4().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]:
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 MLPactivation + 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 medianTTFTs per side:
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
Documentation
Internal rounding path only; inputs, outputs and options are unchanged.
Dependencies
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
torch.equal) for M = 1 ... 16384 rows, N 17408, onnormal, wide-range (magnitudes 1e-30 ... 1e4: saturating and subnormal values) and all-zero inputs.
op_tests/triton_tests/fusions/test_activation.py519 passed, 228 skipped(shuffled scales with N not a multiple of 512, by design);
test_fused_silu_mul.py23 passed;op_tests/triton_tests/quant/test_quant_mxfp4.py77 passed, 48 skipped, includingtest_mxfp4_quant_op_asm_matches_bits. The threetest_dynamic_mxfp4_quant_gluon_matches_tritoncases fail the sameway with and without this change: the test image's Triton has no
gl.amd.cdna4.scaled_downcast.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).
is_hip_cdna4()is false), so the compiledcode is unchanged.
PR-specific question
(Answered once the automated PR Self-Review comment posts its question.)
Changed op tests
None.