Repository navigation
[HIP] Fuse a 32-point Hadamard rotation into the MXFP4 per-1x32 quant and AR + RMSNorm epilogue - #6734
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
…ojections SGLANG_ROCM_GDN_ONLINE_MXFP4_HADAMARD=1 rotates each 32-wide input block of the online MXFP4 GDN projections by the orthonormal Hadamard matrix. The weight is rotated once at load. The activation is rotated inside per_1x32_f4_quant_hip (ROCm/aiter#6734). The product does not change, and the MXFP4 error is lower. Thinking-on GSM8K, 8 reps at temperature 1.0: plain MXFP4 makes the answers 10.7% longer than BF16 (p=0.0017 on accuracy). With the rotation, the answers are 2.1% longer, and the accuracy is the same as BF16 (p=0.62). Co-authored-by: Cursor <[email protected]>
…R's MXFP4 quant Add LinearMethodBase.fuse_input_hadamard(layer, size), default False. InputHadamardLinearMethod calls it after the wrapped method's process_weights_after_loading and skips its own input rotation when the method takes it over. Online MXFP4 and Quark OCP MX forward the call to their MxFp4LinearKernel; AiterMxfp4LinearKernel accepts on the ASM GEMM path at size 32 and passes hadamard_rotate=True to the HIP activation quant (ROCm/aiter#6734). hadamard_rotate is only passed to AITER when set, so older AITER keeps working as long as nothing fuses. Also fix the fake implementation of gemm_with_dynamic_quant, whose signature had drifted from the op. Co-authored-by: Cursor <[email protected]> Signed-off-by: Mikko Tukiainen <[email protected]>
bc2aec9 to
baab824
Compare
…mard-32 Plain MXFP4 on the Qwen3.5 / Qwen3.8 GDN in_proj_qkvz and out_proj makes the thinking longer. On thinking-on GSM8K (8 reps at temperature 1.0), the answers are 10.7% longer than BF16, and the accuracy drops from 0.9641 to 0.9597 (McNemar p = 0.0017). SGLANG_ROCM_GDN_ONLINE_MXFP4_HADAMARD (default true) rotates each 32-wide input block of these projections by the orthonormal Hadamard matrix. The weight is rotated once at load. The activation is rotated inside per_1x32_f4_quant_hip (ROCm/aiter#6734), on the Triton and the ASM path. The product does not change, and the MXFP4 error is lower. With the rotation, the answers are 2.1% longer than BF16, and the accuracy is the same (p = 0.62). TPOT changes by 0.2% or less. Without hadamard_rotate in AITER, the layer raises RuntimeError. Set the variable to 0 to run plain MXFP4. Co-authored-by: Cursor <[email protected]>
The review asks to not check AITER for hadamard_rotate. The SGLang AITER pin must have ROCm/aiter#6734 before this PR merges. An AITER without it raises TypeError at the first rotated quant. With SGLANG_ROCM_GDN_ONLINE_MXFP4_HADAMARD=0, apply() does not give hadamard_rotate to AITER, so plain MXFP4 runs on each AITER. The fallback test goes away. The gfx95x test always runs both rotations. Co-authored-by: Cursor <[email protected]>
baab824 to
19af255
Compare
READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
…R's MXFP4 quant Add LinearMethodBase.fuse_input_hadamard(layer, size), default False. InputHadamardLinearMethod calls it after the wrapped method's process_weights_after_loading and skips its own input rotation when the method takes it over. Online MXFP4 and Quark OCP MX forward the call to their MxFp4LinearKernel; AiterMxfp4LinearKernel accepts on the ASM GEMM path at size 32 and passes hadamard_rotate=True to the HIP activation quant (ROCm/aiter#6734). hadamard_rotate is only passed to AITER when set, so older AITER keeps working as long as nothing fuses. Also fix the fake implementation of gemm_with_dynamic_quant, whose signature had drifted from the op. Co-authored-by: Cursor <[email protected]> Signed-off-by: Mikko Tukiainen <[email protected]>
|
@aiter-bot review |
ROCm/aiter PR #6734 — [HIP] Fuse a 32-point Hadamard rotation into the MXFP4 per-1x32 quant and AR + RMSNorm epilogueFuses a 32-point normalized Hadamard rotation into the gfx950 MXFP4 per-1x32 quant kernel and the AR+RMSNorm+MXFP4 epilogue behind a new opt-in Review (advisory):
|
Add `hadamard_rotate` to per_1x32_mx_quant_hip, per_1x32_f4_quant_hip and dynamic_per_group_scaled_quant. When set (fp4x2 output, group size 32), each 32-element group is rotated by the normalized Sylvester Hadamard matrix in registers before its amax is taken, so the output equals quantizing an explicitly rotated input. Existing instantiations are unchanged (new `hadamard32` template parameter defaults to false). Adds op_tests/test_quant_mxfp4_hadamard.py. Co-authored-by: Cursor <[email protected]> Signed-off-by: Mikko Tukiainen <[email protected]>
per_1x32_f4_quant_hip allocates its shuffled scale buffer with torch.empty, so the padding the kernel does not write differs from the test's zero-filled buffer. The packed output is fully written and stays compared. Co-authored-by: Cursor <[email protected]> Signed-off-by: Mikko Tukiainen <[email protected]>
Fold the butterfly-vs-matrix check into the fused test and test three shapes instead of six. Drop the GEMM error and rejection tests: the first restates the math the exact match already pins down, the second needs a subprocess to observe an AITER_CHECK abort. Co-authored-by: Cursor <[email protected]> Signed-off-by: Mikko Tukiainen <[email protected]>
hadamard_rotate=True on fused_allreduce_rmsnorm_quant (MXFP4) quantizes the normed output rotated per 32-element group, bit-exact with per_1x32_f4_quant_hip(normed, hadamard_rotate=True). The bf16 side output and the residual stay unrotated. Covers the 1-stage, 2-stage and fallback paths. Signed-off-by: Mikko Tukiainen <[email protected]> Co-authored-by: Cursor <[email protected]>
The rotated quant and AR epilogue use the gfx950 FP4 convert, which opus stubs to zeros on other targets. Check the arch at both entry points and skip the rotated test cases elsewhere. Signed-off-by: Mikko Tukiainen <[email protected]> Co-authored-by: Cursor <[email protected]>
…ut Gemma The scale_layout_m32k4 branch of per_1x32_mx_quant_hip did not pass hadamard_rotate on, so the combination returned unrotated bytes. The rotated AR cases now also run with plain RMSNorm weights. Signed-off-by: Mikko Tukiainen <[email protected]> Co-authored-by: Cursor <[email protected]>
19af255 to
f06b94f
Compare
Thanks, addressed in f06b94f:
|
|
@TennyWang1223 aiter-bot clean we can move to human review |
Add aiter.ops.flydsl.flydsl_rot_quant(x, RS, shuffle_scales=): one gfx950
kernel that rotates each RS-wide block of a bf16 activation by a normalized
Sylvester Hadamard matrix (RS in {32, 64, 128}), quantizes the result to
MXFP4 with the default RoundUp e8m0 scale, and optionally writes the scales
in the CK layout that gemm_a4w4 consumes. The output equals
per_1x32_f4_quant applied to the bf16-rounded rotated tensor.
Rotated MXFP4 checkpoints fold the rotation into the weights, so the
activation of every rotated projection needs it online. Unfused, that is a
Hadamard matmul followed by a quant pass. The HIP per-1x32 quant fuses a
32-point rotation since ROCm#6734; this op adds 64 and 128 in the same single
pass and agrees with the HIP path byte for byte at 32, apart from groups
under its amax floor.
One thread owns one RS-wide block, so the FWHT runs in registers without
cross-lane traffic. At RS=64 the 1/8 normalization is applied to each
group's amax and to the conversion exponent instead of to every element.
MI350X, K=4096, RS=64, CK-layout scales, CUDA graph: 1.8x (M=32) to 2.9x
(M>=1024) over a bf16 Hadamard matmul plus per_1x32_f4_quant_hip.
op_tests/test_flydsl_rot_quant.py checks the fp4 bytes and e8m0 scales
exactly against per_1x32_f4_quant (RS 32/64/128, both scale layouts, M
1..16384, K 2880/4096/7168, zero, tiny and NaN rows), parity with the HIP
path at RS=32, and the CK scale layout end to end through gemm_a4w4.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Add aiter.ops.flydsl.flydsl_rot_quant(x, RS, shuffle_scales=): one gfx950
kernel that rotates each RS-wide block of a bf16 activation by a normalized
Sylvester Hadamard matrix (RS in {32, 64, 128}), quantizes the result to
MXFP4 with the default RoundUp e8m0 scale, and optionally writes the scales
in the CK layout that gemm_a4w4 consumes. The output equals
per_1x32_f4_quant applied to the bf16-rounded rotated tensor.
Rotated MXFP4 checkpoints fold the rotation into the weights, so the
activation of every rotated projection needs it online. Unfused, that is a
Hadamard matmul followed by a quant pass. The HIP per-1x32 quant fuses a
32-point rotation since ROCm#6734; this op adds 64 and 128 in the same single
pass and agrees with the HIP path byte for byte at 32, apart from groups
under its amax floor.
One thread owns one RS-wide block, so the FWHT runs in registers without
cross-lane traffic. At RS=64 the 1/8 normalization is applied to each
group's amax and to the conversion exponent instead of to every element.
MI350X, K=4096, RS=64, CK-layout scales, CUDA graph: 1.8x (M=32) to 2.9x
(M>=1024) over a bf16 Hadamard matmul plus per_1x32_f4_quant_hip.
op_tests/test_flydsl_rot_quant.py checks the fp4 bytes and e8m0 scales
exactly against per_1x32_f4_quant (RS 32/64/128, both scale layouts, M
1..16384, K 2880/4096/7168, zero, tiny and NaN rows), parity with the HIP
path at RS=32, and the CK scale layout end to end through gemm_a4w4.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…R's MXFP4 quant Add LinearMethodBase.fuse_input_hadamard(layer, size), default False. InputHadamardLinearMethod calls it after the wrapped method's process_weights_after_loading and skips its own input rotation when the method takes it over. Online MXFP4 and Quark OCP MX forward the call to their MxFp4LinearKernel; AiterMxfp4LinearKernel accepts on the ASM GEMM path at size 32 and passes hadamard_rotate=True to the HIP activation quant (ROCm/aiter#6734). hadamard_rotate is only passed to AITER when set, so older AITER keeps working as long as nothing fuses. Also fix the fake implementation of gemm_with_dynamic_quant, whose signature had drifted from the op. Co-authored-by: Cursor <[email protected]> Signed-off-by: Mikko Tukiainen <[email protected]>
Add aiter.ops.flydsl.flydsl_rot_quant(x, RS, shuffle_scales=): one gfx950
kernel that rotates each RS-wide block of a bf16 activation by a normalized
Sylvester Hadamard matrix (RS in {32, 64, 128}), quantizes the result to
MXFP4 with the default RoundUp e8m0 scale, and optionally writes the scales
in the CK layout that gemm_a4w4 consumes. The output equals
per_1x32_f4_quant applied to the bf16-rounded rotated tensor.
Rotated MXFP4 checkpoints fold the rotation into the weights, so the
activation of every rotated projection needs it online. Unfused, that is a
Hadamard matmul followed by a quant pass. The HIP per-1x32 quant fuses a
32-point rotation since ROCm#6734; this op adds 64 and 128 in the same single
pass and agrees with the HIP path byte for byte at 32, apart from groups
under its amax floor.
One thread owns one RS-wide block, so the FWHT runs in registers without
cross-lane traffic. At RS=64 the 1/8 normalization is applied to each
group's amax and to the conversion exponent instead of to every element.
MI350X, K=4096, RS=64, CK-layout scales, CUDA graph: 1.8x (M=32) to 2.9x
(M>=1024) over a bf16 Hadamard matmul plus per_1x32_f4_quant_hip.
op_tests/test_flydsl_rot_quant.py checks the fp4 bytes and e8m0 scales
exactly against per_1x32_f4_quant (RS 32/64/128, both scale layouts, M
1..16384, K 2880/4096/7168, zero, tiny and NaN rows), parity with the HIP
path at RS=32, and the CK scale layout end to end through gemm_a4w4.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e and MoE stage-1 inputs (flydsl_rot_quant) (#4549) * [FlyDSL] Fused online Hadamard rotation + MXFP4 quantization Add aiter.ops.flydsl.flydsl_rot_quant(x, RS, shuffle_scales=): one gfx950 kernel that rotates each RS-wide block of a bf16 activation by a normalized Sylvester Hadamard matrix (RS in {32, 64, 128}), quantizes the result to MXFP4 with the default RoundUp e8m0 scale, and optionally writes the scales in the CK layout that gemm_a4w4 consumes. The output equals per_1x32_f4_quant applied to the bf16-rounded rotated tensor. Rotated MXFP4 checkpoints fold the rotation into the weights, so the activation of every rotated projection needs it online. Unfused, that is a Hadamard matmul followed by a quant pass. The HIP per-1x32 quant fuses a 32-point rotation since #6734; this op adds 64 and 128 in the same single pass and agrees with the HIP path byte for byte at 32, apart from groups under its amax floor. One thread owns one RS-wide block, so the FWHT runs in registers without cross-lane traffic. At RS=64 the 1/8 normalization is applied to each group's amax and to the conversion exponent instead of to every element. MI350X, K=4096, RS=64, CK-layout scales, CUDA graph: 1.8x (M=32) to 2.9x (M>=1024) over a bf16 Hadamard matmul plus per_1x32_f4_quant_hip. op_tests/test_flydsl_rot_quant.py checks the fp4 bytes and e8m0 scales exactly against per_1x32_f4_quant (RS 32/64/128, both scale layouts, M 1..16384, K 2880/4096/7168, zero, tiny and NaN rows), parity with the HIP path at RS=32, and the CK scale layout end to end through gemm_a4w4. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] rot_quant: one MXFP4 group per lane Split each RS-wide block across RS/32 adjacent lanes. Every lane runs the first five FWHT stages on its 32 values in registers, and the lanes finish the remaining log2(RS/32) stages with xor shuffles in the same stage order, so the output is unchanged byte for byte. A lane now owns exactly one MXFP4 group: its scale stays thread-local, VGPRs drop from 101/228 to 60 at RS=64/128, and small M spreads over 2-4x more lanes. MI350X, K=4096, CK-layout scales, CUDA graph: RS=64 is 1.13-1.26x and RS=128 1.15-1.77x faster than the previous revision; RS=32 is unchanged. CTA size and the SLP setting were re-swept for the new mapping and kept. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] rot_quant: MoE stage-1 input with sorted scales Add flydsl_rot_quant_moe_sort(x, sorted_ids, num_valid_ids, topk, block_size, RS): the rotated MXFP4 quantization of the MoE stage-1 input with its scales already sorted for the MoE GEMMs, i.e. fused_dynamic_mxfp4_quant_moe_sort applied to the rotated tensor (without that op's amax floor). Without it a rotated MoE layer runs a rotation pass before the quant-and-sort kernel. For small batches one kernel takes a thread per (sorted row, group): valid routes rotate and quantize their token's group and scatter the scale to the sorted row in the mxfp4_moe_sort_fwd layout, padding rows exit. 1024-thread CTAs keep the cost of the exiting threads down. Because a token is quantized once per route, past M * topk * K = 2^21 the op instead runs flydsl_rot_quant once and mxfp4_moe_sort_fwd. mxfp4_moe_sort_fwd loses the last token's final scales when K // 32 is not a multiple of 4, so those K always take the fused kernel. MI350X, E=128, topk=8, CUDA graph: 1.3-2.8x faster than a Hadamard matmul followed by fused_dynamic_mxfp4_quant_moe_sort; at K=2048 within ~0.5 us of the unrotated quant-and-sort. The op test gains test_rot_quant_moe_sort: fp4 bytes and sorted scales compared exactly with the torch reference for K 2880/4096/7168, RS 32/64/128 and M 1..16384. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] rot_quant: pre-review fixes - flydsl_rot_quant_moe_sort takes token_num in the position fused_dynamic_mxfp4_quant_moe_sort has it and rejects a stage-2 input; rotation_size and shuffle (were RS and shuffle_scales) are keyword-only. - Skip the padding slots of a num_local_tokens sort (topk << 24 | count, a real token): M=64 with 40 local tokens 8.6 -> 5.8 us. - Write 0 to the MoE scale padding columns, as mxfp4_moe_sort_fwd does. - K // 32 not a multiple of 4 now takes the two-pass path as well, with zero-padded scale columns to avoid #7646: K=2880 M=16384 141.6 -> 42.4 us. - Launch under the input's device. - Set the no-SLP compile hint directly; one helper for the CK padded scale columns. - Test: block_m 16/32/64 and counted sorts in the MoE check, padding columns compared, per-candidate column names. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] rot_quant: review follow-ups - MoE: stop writing the scale padding columns. As for fused_dynamic_mxfp4_quant_moe_sort, only the first K // 32 columns of valid-route rows are defined; mxfp4_moe_sort_fwd's scatter store leaves the padding columns unwritten, so the two-pass path never matched a "0" claim. - Merge the no-SLP hint into existing llvm_options instead of replacing them. - Module docstring shows the keyword API; note that the #7646 padding goes away with #7653. - Test: extra block sizes and counted sorts only for M <= 1024 (48 -> 34 s). Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] rot_quant: keep padded K on the fused MoE kernel longer When K // 32 is not a multiple of 4 the two-pass path also pads the scales for #7646, which moves its crossover with the fused kernel about 8x higher. Switch at M * topk * K > 2^24 for those K and keep 2^21 otherwise. K=2880, rotation size 64, MI350X: 1.27-1.68x faster for M * topk from 768 to 4096; other K are unchanged. Over 544 measured points (K 2048-7168, every rotation size, topk 4 and 8) the dispatch is within 0.7% of the faster path on average, against 2.4% before. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [FlyDSL] rot_quant: drop the #7646 padding now that #7653 is in main mxfp4_moe_sort_fwd keeps the last row's tail scales since #7653, so the MoE two-pass path no longer pads the scale rows, and the 2^24 threshold that compensated for the padding goes too. With the fixed sort the padded-K dims cross over where the others do: one 2^21 switch for every K is within 5% of the faster path on 567 of 600 measured points (MI350X, K 2048-7168, every rotation size, topk 4 and 8), 0.9% off on average. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
…n the README - Section 2 tells how the qh and oh builds rotate the activation of the MXFP4 GDN projections of sgl-project/sglang#46059, in the order of ROCm/aiter#6734. - Section 5 refuses only an unknown quant method or state now. _build makes a launch for each format of the projections. - Section 6 adds the quant check, and the e2e tests run the default rotation of SGLang. Co-authored-by: Cursor <[email protected]>
The final review of the branch estimates that the rotation adds about 100 VALU instructions to each quant round. Two changes keep the same bits with fewer instructions: - The cross-lane stages compute -b + a in the upper lane, not a - b. The partner value then has one use, so its DPP move can fold into the add. - _quad_scale packs the rotated values to bf16 one time, then unpacks them for the amax. It does not round each value and then pack again. A CPU model of the new order gives the same bf16 values as the order of ROCm/aiter#6734 for 2^20 values of each input kind, and for blocks of signed zeros. Co-authored-by: Cursor <[email protected]>
…nt_x by Hadamard-32 sgl-project/sglang#46059 rotates the weights of the MXFP4 GDN projections by Hadamard-32. The activation must get the same rotation before its MXFP4 quant. - Add hadamard32_quad to mono/ops.py. It rotates a 1 x 32 block that lies on 4 adjacent lanes. It adds in the order of hadamard32_rotate of ROCm/aiter#6734, so the bf16 values are the same bit for bit. - Add the h32 flag to quant_x and _quad_scale in layer.py and in mono/stages/moe.py. The flag is off by default, so the plain builds do not change. - Add check_quant.py and TestQwen38MoeQuant. The check compares the rotation and the LDS rows of quant_x with per_1x32_f4_quant_hip, plain and rotated, bit for bit. The rotation inputs have an exponent for each element, so the f32 sums round and a changed add order shows. Co-authored-by: Cursor <[email protected]>
…n the README - Section 2 tells how the qh and oh builds rotate the activation of the MXFP4 GDN projections of sgl-project/sglang#46059, in the order of ROCm/aiter#6734. - Section 5 refuses only an unknown quant method or state now. _build makes a launch for each format of the projections. - Section 6 adds the quant check, and the e2e tests run the default rotation of SGLang. Co-authored-by: Cursor <[email protected]>
…nt_x by Hadamard-32 sgl-project/sglang#46059 rotates the weights of the MXFP4 GDN projections by Hadamard-32. The activation must get the same rotation before its MXFP4 quant. - Add hadamard32_quad to mono/ops.py. It rotates a 1 x 32 block that lies on 4 adjacent lanes. It adds in the order of hadamard32_rotate of ROCm/aiter#6734, so the bf16 values are the same bit for bit. - Add the h32 flag to quant_x and _quad_scale in layer.py and in mono/stages/moe.py. The flag is off by default, so the plain builds do not change. - Add check_quant.py and TestQwen38MoeQuant. The check compares the rotation and the LDS rows of quant_x with per_1x32_f4_quant_hip, plain and rotated, bit for bit. The rotation inputs have an exponent for each element, so the f32 sums round and a changed add order shows. Co-authored-by: Cursor <[email protected]>
…n the README - Section 2 tells how the qh and oh builds rotate the activation of the MXFP4 GDN projections of sgl-project/sglang#46059, in the order of ROCm/aiter#6734. - Section 5 refuses only an unknown quant method or state now. _build makes a launch for each format of the projections. - Section 6 adds the quant check, and the e2e tests run the default rotation of SGLang. Co-authored-by: Cursor <[email protected]>
…nt_x by Hadamard-32 sgl-project/sglang#46059 rotates the weights of the MXFP4 GDN projections by Hadamard-32. The activation must get the same rotation before its MXFP4 quant. - Add hadamard32_quad to mono/ops.py. It rotates a 1 x 32 block that lies on 4 adjacent lanes. It adds in the order of hadamard32_rotate of ROCm/aiter#6734, so the bf16 values are the same bit for bit. - Add the h32 flag to quant_x and _quad_scale in layer.py and in mono/stages/moe.py. The flag is off by default, so the plain builds do not change. - Add check_quant.py and TestQwen38MoeQuant. The check compares the rotation and the LDS rows of quant_x with per_1x32_f4_quant_hip, plain and rotated, bit for bit. The rotation inputs have an exponent for each element, so the f32 sums round and a changed add order shows. Co-authored-by: Cursor <[email protected]>
…n the README - Section 2 tells how the qh and oh builds rotate the activation of the MXFP4 GDN projections of sgl-project/sglang#46059, in the order of ROCm/aiter#6734. - Section 5 refuses only an unknown quant method or state now. _build makes a launch for each format of the projections. - Section 6 adds the quant check, and the e2e tests run the default rotation of SGLang. Co-authored-by: Cursor <[email protected]>
…nt_x by Hadamard-32 sgl-project/sglang#46059 rotates the weights of the MXFP4 GDN projections by Hadamard-32. The activation must get the same rotation before its MXFP4 quant. - Add hadamard32_quad to mono/ops.py. It rotates a 1 x 32 block that lies on 4 adjacent lanes. It adds in the order of hadamard32_rotate of ROCm/aiter#6734, so the bf16 values are the same bit for bit. - Add the h32 flag to quant_x and _quad_scale in layer.py and in mono/stages/moe.py. The flag is off by default, so the plain builds do not change. - Add check_quant.py and TestQwen38MoeQuant. The check compares the rotation and the LDS rows of quant_x with per_1x32_f4_quant_hip, plain and rotated, bit for bit. The rotation inputs have an exponent for each element, so the f32 sums round and a changed add order shows. Co-authored-by: Cursor <[email protected]>
…n the README - Section 2 tells how the qh and oh builds rotate the activation of the MXFP4 GDN projections of sgl-project/sglang#46059, in the order of ROCm/aiter#6734. - Section 5 refuses only an unknown quant method or state now. _build makes a launch for each format of the projections. - Section 6 adds the quant check, and the e2e tests run the default rotation of SGLang. Co-authored-by: Cursor <[email protected]>
…ontainers A rocm720 image built before docker/rocm.Dockerfile set SGLANG_ROCM_GDN_ONLINE_MXFP4_HADAMARD=0 does not have the flag. Its AITER has no hadamard_rotate (ROCm/aiter#6734), so the online MXFP4 GDN tests fail there with a TypeError. amd_ci_start_container.sh now sets the flag for --rocm-version rocm720. Remove it with the pin bump of the rocm720 stage. Co-authored-by: Cursor <[email protected]>
Summary
Adds
hadamard_rotate=Trueto the HIP per-1x32 MXFP4 quant and to the fused AR + RMSNorm + MXFP4 epilogue: each 32-element group is rotated by a normalized Hadamard matrix in registers before it is quantized. gfx950 only.Motivation
MXFP4 shares one e8m0 scale per 32 values, so a single outlier channel flushes the rest of its block toward zero. Rotating each block by a normalized Hadamard matrix spreads the outlier across the block, and the same rotation applied to the weight leaves the GEMM unchanged: ((xH)(WH)^T = xW^T). Quark exports MXFP4 checkpoints with this online rotation (
input_rotation).Today the rotation needs a separate pass over HBM before the quant, and rotated layers lose the AR + RMSNorm + MXFP4 fusion. The consumer is vLLM's MXFP4 ASM GEMM path (vllm-project/vllm#63272), which applies this quant only for shapes tuned in AITER's a4w4 configs; the configs for Qwen3.8-2.4T's GDN
in_proj_qkvzare in #7441.Changes
per_1x32_mx_quant_hip,per_1x32_f4_quant_hipanddynamic_per_group_scaled_quantgainhadamard_rotate=False. WithTrue, the output is the MXFP4 quantization ofx.view(m, n // 32, 32) @ (scipy.linalg.hadamard(32) / sqrt(32)), computed in fp32 and rounded tox.dtypebefore the scale is taken. Scales, packing,shuffle,num_rowsandnum_rows_factorbehave as without rotation. The caller rotates the weight the same way.tensor_model_parallel_fused_allreduce_rmsnorm_quant(..., quant_type="mxfp4", hadamard_rotate=True)returns MXFP4 bytes and scales bit-identical toper_1x32_f4_quant_hip(normed, hadamard_rotate=True), wherenormedis the kernel's own RMSNorm output. The residual and theemit_bf16side output stay unrotated. This covers the 1-stage, 2-stage and fallback paths; other quant types raiseValueError.group_size == 32(thread_data_size == 32). A newhadamard32template parameter ofdynamic_per_group_scaled_quant_kernelruns a 5-stage fp32 butterfly on that thread's 32 values before the abs-max, so loads and stores are unchanged.PACK_SIZElanes. Butterfly stages belowPACK_SIZErun in registers and the rest exchange values with__shfl_xor. The rotated values take the quant kernel's RoundUp e8m0 scale and hardware FP4 convert.falseunderif constexpr, so existing paths compile to the same kernels.Performance (if applicable)
Before: AR + RMSNorm, then a standalone rotated quant. After: the rotation fused into the AR epilogue. bf16, Gemma RMSNorm with
emit_bf16, us per call (graph replay of 20 calls × 50).The fused rotated epilogue is also 0.5–0.7 us faster than the unrotated one at these shapes (8.34 / 9.49 / 10.29 / 10.40 / 11.77 / 12.72 us).
Standalone quant on one MI355X, bf16 input (
run_perftest, us), plain vs Hadamard-32:Testing
Documentation
Dependencies
Breaking Changes
None.
hadamard_rotatedefaults toFalse, and the unrotated kernels are unchanged.PR Self-Review
Reviewed revision
f06b94f
Behavior and intent
With
hadamard_rotate=True, the HIP per-1x32 MXFP4 quant and the AR + RMSNorm + MXFP4 epilogue quantize each 32-element group after a normalized Hadamard-32 rotation, instead of the caller rotating in a separate HBM pass and giving up the AR fusion. With the defaultFalse, behavior and generated kernels are unchanged.Validation and unverified scope
MI355X (gfx950), branch installed editable with
PREBUILD_KERNELS=0 GPU_ARCHS=gfx950so the modules are JIT-built from it.python3 op_tests/test_quant_mxfp4_hadamard.py --perf python3 op_tests/multigpu_tests/test_fused_ar_rms_mxfp4_quant.py python3 op_tests/multigpu_tests/test_fused_ar_rms_mxfp4_quant.py -t 8 --stage both --emit-bf16 \ -s 1,8192 4,8192 8,8192 16,8192 32,8192 8,7168 16,4096On the tree rebased onto 0f61f2b, all 12 quant cases pass bit-exactly. Every rotated AR case at TP 2 and 4 (1-stage, 2-stage and fallback; with and without
gemma_norm) is byte-identical to the standalone rotated quant, also with--stage both; the unrotated cases pass as before. TP8 passed before the rebase; on the rebased tree it runs in theci:multi-gpujob.per_1x32_mx_quant_hip(..., scale_layout_m32k4=True, hadamard_rotate=True)now fails its assert. Timings are in Performance; they were taken before the rebase, which does not touch the kernels.Unverified: MI250X and MI300X were not run. The rotated path is rejected there by design and its test cases are skipped; the unrotated kernels compile to the same code. End-to-end model accuracy with rotated checkpoints is validated on the vLLM side, not here.
PR-specific question
dynamic_per_group_scaled_quantandfused_allreduce_rmsnorm_mxfp4_quantrejecthadamard_rotateon any other arch withAITER_CHECK(get_gpu_arch() == "gfx950").quant_dtype == dtypes.fp4x2; the AR wrappers raiseValueErrorfor anyquant_typeother than"mxfp4".group_size == 32withoutscale_layout_m32k4, checked byAITER_CHECKin the C++ entry;per_1x32_mx_quant_hipalso asserts againstscale_layout_m32k4, whose branch does not reach that check. Inside the kernel,static_asserts require fp4 output,group_size == 32andthread_data_size == 32, so the rotation is only instantiated where one thread owns a whole group.mandnfollow the unrotated quant's existing limits.hadamard_rotate=Falseeverywhere and the template parameters default tofalseunderif constexpr, so no existing caller reaches the new code.Changed op tests
op_tests/test_quant_mxfp4_hadamard.py(new): checks that packed bytes and scales fromper_1x32_f4_quant_hip(hadamard_rotate=True)are bit-identical to rotating in torch and then quantizing, for ragged and alignedm, bf16 and fp16, and shuffled and unshuffled scales. It also checks that the torch butterfly reference equals the matrix definition.--perfadds rotated vs plain timings. No existing test covers the rotated quant.op_tests/multigpu_tests/test_fused_ar_rms_mxfp4_quant.py: adds rotated cases on gfx950 for every shape, with and withoutgemma_norm, checking the fused epilogue's output byte for byte against the rotated standalone quant of its own bf16 side output. This is the only test of the shuffle-based rotation in the AR epilogue, which is separate code from the quant kernel's in-register rotation.Co-authored-by: Cursor [email protected]