Repository navigation
[FlyDSL] Fused online Hadamard rotation + MXFP4 quantization for dense and MoE stage-1 inputs (flydsl_rot_quant) - #4549
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
fbafd80 to
0f6e54c
Compare
|
No activity for 15 days, so this is now labelled |
|
Closing due to inactivity: no pushes, reviews, or comments for 65 days. This is housekeeping, not a judgement on the change — if you want to continue this work, comment here and we'll reopen, or open a fresh PR rebased on the latest main. |
0f6e54c to
39df83c
Compare
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
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 |
|
Reopened and updated after the inactivity close. The branch is rebased onto current |
39df83c to
0acafd3
Compare
c26dcd6 to
816eb03
Compare
|
@coderfeli @yadaish this is ready for review. Since the reopen on 11-06 it gained the MoE stage-1 variant ( |
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]>
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]>
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_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 ROCm#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]>
- 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 ROCm#7646 padding goes away with ROCm#7653. - Test: extra block sizes and counted sorts only for M <= 1024 (48 -> 34 s). Co-Authored-By: Claude Opus 5.5 <[email protected]>
When K // 32 is not a multiple of 4 the two-pass path also pads the scales for ROCm#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]>
…in main mxfp4_moe_sort_fwd keeps the last row's tail scales since ROCm#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]>
0ffc56c to
9c7ae54
Compare
Summary
Adds two gfx950 FlyDSL ops that rotate a bf16 activation by a block-diagonal normalized Sylvester Hadamard matrix (
rotation_size∈ {32, 64, 128}) and quantize it to MXFP4 in one pass. Scales use aiter's default RoundUp e8m0. The output equalsper_1x32_f4_quantapplied to the bf16-rounded rotated tensor.aiter.ops.flydsl.flydsl_rot_quant(x, *, rotation_size=64, shuffle=False)covers the dense case. Its returns matchper_1x32_f4_quant_hip(fp4x2, e8m0). Withshuffle=Truethe scales are written straight into the CK layout thatgemm_a4w4consumes.aiter.ops.flydsl.flydsl_rot_quant_moe_sort(x, sorted_ids, num_valid_ids, token_num, topk, block_size, *, rotation_size=64)produces the MoE stage-1 input. Its scales are already sorted bymoe_sorting'ssorted_ids. The leading arguments and the contract are those offused_dynamic_mxfp4_quant_moe_sorton the rotated tensor, so the output can be passed asprequantized_a1/prequantized_a1_scaletofused_moe_2stages.Motivation
Rotated MXFP4 checkpoints, such as Quark's online R1 rotation (QuaRot/SpinQuant style), fold a Hadamard rotation into the weights of the attention qkv and MLP gate/up projections, including MoE experts. The activation of every rotated projection must therefore be rotated online before it is quantized. Without fusion that is a bf16 Hadamard matmul over HBM followed by a quant pass, which costs about +8% TPOT in serving (see End-to-end below). Which rotation size recovers the most accuracy depends on the model: in our runs Qwen3-8B does best with 64 and Qwen3-30B-A3B with 32. All three sizes come from one kernel template, so all three are kept.
Relation to the existing ops:
per_1x32_f4_quant_hip(hadamard_rotate=True)(#6734)rotate_activation(out_fp4, x.view(-1, 128), scale)(DSv4, #3035)gemm_a4w4it needs a separateshuffle_scale.fused_dynamic_mxfp4_quant_moe_sortflydsl_rot_quant_moe_sortkeeps its contract and adds the rotation.fused_quant_transpose_hadamard_mxfp4(Triton, #6476)Changes
aiter/ops/flydsl/kernels/rot_quant.py(new): the dense op.quant_utils.emit_mx_e8m0_scalein RoundUp mode. At RS=64 the 1/8 normalization is applied to each group's amax and to the conversion exponent instead of to every element. This is exact for a power of two, and a NaN group still gets scale 255.silu_and_mul_fq.py. Its padding columns get 0x7F, asshuffle_scalewrites them.flydsl_rot_quant_moe_sort):fused_dynamic_mxfp4_quant_moe_sort, only the firstK // 32columns of valid-route rows are defined.token_numand its slot belowtopk. This is the same rule as the HIP kernels: a sort withnum_local_tokenspads withtopk << 24 | count, which names a real token.M * topk * K = 2^21the op instead runsflydsl_rot_quantonce followed bymxfp4_moe_sort_fwd. Over 600 measured points (K 2048-7168, every rotation size, topk 4 and 8) this switch picks a path within 5% of the faster one on 567, and is 0.9% slower than the faster path on average.aiter/ops/flydsl/__init__.py: lazy exports.op_tests/test_flydsl_rot_quant.py(new): see Changed op tests.Performance (if applicable)
Kernel timings are on MI350X (gfx950) with CUDA graphs: 50 launches per replay, median of 30 replays.
Dense. K=4096, CK-layout scales. Before is the fastest existing aiter path for that size. For RS=64/128 that is a bf16 blockwise Hadamard matmul followed by
per_1x32_f4_quant_hip(shuffle=True). For RS=32 it is the fusedper_1x32_f4_quant_hip(hadamard_rotate=True, shuffle=True)from #6734.MoE stage 1. E=128, topk=8, block 32. Before is a Hadamard matmul followed by
fused_dynamic_mxfp4_quant_moe_sort. The unrotated quant-and-sort is listed for reference.Across K ∈ {2048, 4096}, RS ∈ {32, 64, 128} and M from 1 to 4096, the MoE ratio is 1.29–2.86×. The weakest region is K=4096 around M=64.
End-to-end (vLLM serving, MI350X, TP=1)
These end-to-end numbers come from a local integration that is not part of this PR:
flydsl_rot_quantand then the existing AITERgemm_a4w4.fused_moe's quant-sort call withflydsl_rot_quant_moe_sort.Protocol:
Measured on the head before commit 4. Commit 4 does not change kernel timings: the MoE geomean is 1.00× and dense is within ±1%.
Each cell is the extra TPOT over RTN, in ms.
Notes on the MoE table:
Accuracy: the fused and separated paths are statistically indistinguishable at every RS on both models. On wikitext-2 token-level PPL (145 windows of 2048 tokens, paired per-window NLL), every difference is under 1.1σ and the largest |ΔPPL| is 0.014.
Testing
v_cvt_scalef32_pk_fp4_f32); the test skips other cards inmain()Documentation
These are new ops, and there is no user-guide page for FlyDSL quant ops. Their contracts (math, layouts, padding, which rows are defined) are in the docstrings.
Dependencies
Breaking Changes
None.
PR Self-Review
Reviewed revision
9c7ae54
Behavior and intent
This adds two new ops and changes no existing behavior. Today a rotated MXFP4 linear or MoE stage 1 needs a separate Hadamard matmul over HBM before the quant, unless the rotation size is 32 and the layer is dense (#6734).
flydsl_rot_quantdoes the rotation, MXFP4 quantization and the CK scale layout in one pass for rotation sizes 32/64/128, with the rounding contract ofper_1x32_f4_quant.flydsl_rot_quant_moe_sortdoes the same for the MoE stage-1 input with the scale layout offused_dynamic_mxfp4_quant_moe_sort. Rotated weights and the existinggemm_a4w4/fused_moekernels therefore work unchanged.Validation and unverified scope
On MI350X with
flydsl==0.5.0at this head:python op_tests/test_flydsl_rot_quant.py(defaults): 752 checks passed, 0 failed, exit 0, 34 s. fp4 bytes and e8m0 scales are compared withrtol=atol=0againstper_1x32_f4_quant, over:per_1x32_f4_quant_hip(hadamard_rotate=True);gemm_a4w4end to end at N ∈ {256, 4096};num_local_tokensfor M ≤ 1024. This exercises both the fused and the two-pass dispatch, including K=2880, whereK // 32is not a multiple of 4. A separate run at M=32768, K=2880 covers the scatter store ofmxfp4_moe_sort_fwd.blackandruff==0.16.0are clean.Unverified or not covered:
main(); the ops raise there by design.gemm_a4w4is not used as a reference below N=256 (it is wrong there for any quantizer, rel. err 0.68 at N=128) or for K not a multiple of 256 (it reads past K).expert_mask(expert parallelism), and it does not zero the rows of zero-weight top-1 routes as the HIP kernel does.PR-specific question
Before this PR, the test fails at import because neither op exists. Afterwards it fails on:
per_1x32_f4_quantof the rotated input;hadamard_rotatepath outside its amax floor;gemm_a4w4reads differently;sorted_ids, for any block size, with or withoutnum_local_tokens, on either dispatch path;op_tests/test_quant_mxfp4_hadamard.py(#6734) tests the HIP path at RS=32 only.test_quant.pyandtest_moe_sorting_mxfp4.pydo not rotate. None of them covers these ops or rotation sizes 64 and 128.Changed op tests
op_tests/test_flydsl_rot_quant.pyis new. It follows thetest_flydsl_<op>.pyconvention for FlyDSL ops rather than extendingop_tests/test_quant_mxfp4_hadamard.py, which tests a different (HIP) op at a single size.test_rot_quantis the exact-byte check against the shared torch reference. It also checks RS=32 parity with the HIP path, so the two independent implementations check each other.test_rot_quant_gemm_a4w4is the model call: CK scale layout intogemm_a4w4, the one consumer that layout exists for.test_rot_quant_moe_sortcovers the MoE op. Nothing else exercises its sorted-scale scatter, its route validity undernum_local_tokens, or its fused/two-pass dispatch.test_rot_quant_rejects_bad_inputcovers the input guards. An fp16 or strided input would otherwise be read as contiguous bf16 and give plausible garbage, and a stage-2 input would quantize the wrong rows.🤖 Generated with Claude Code