Repository navigation
[HIP] [Bugfix] mxfp4_moe_sort: keep the last row's tail scales when model_dim / 32 is not a multiple of 4 - #7653
Conversation
mxfp4_moe_sort_kernel reads each scale row in 4- to 32-byte loads from a buffer bounded at the end of the scales. The buffer range check is per dword, so when a row is not a whole number of dwords (model_dim / 32 not a multiple of 4, e.g. 2880) the last row's final load read the dword holding its last in-range bytes as 0, and the last route lost its final scales. fused_dynamic_mxfp4_quant_moe_sort hits it through its split path. Such rows now read that one load byte by byte. The choice is a template parameter, so the other dims compile to the same code as before. Fixes ROCm#7646. Co-Authored-By: Claude Opus 5.5 <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
- 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]>
- 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]>
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 |
… load Review follow-up. Select byte_tail when the last load of a row crosses the row end (ceil(cols / 32) % load bytes), not only when the row is not whole dwords: on gfx950 the range check is per dword, but a per-access check would also drop the in-range part of an 8- or 16-byte load (e.g. model_dim 1152). The load width now comes from one helper shared by the kernel and the launcher. The test adds model_dim 96 and 224 (the <= 256 bucket) and 1152. Co-Authored-By: Claude Opus 5.5 <[email protected]>
2219f5b to
c478dfb
Compare
|
@zufayu @junhaha666 @rbrugaro-amd CI is green on c478dfb, ready for review. This fixes #7646: |
…odel_dim / 32 is not a multiple of 4 (#7653) * [HIP] [Bugfix] mxfp4_moe_sort: keep the last row's tail scales mxfp4_moe_sort_kernel reads each scale row in 4- to 32-byte loads from a buffer bounded at the end of the scales. The buffer range check is per dword, so when a row is not a whole number of dwords (model_dim / 32 not a multiple of 4, e.g. 2880) the last row's final load read the dword holding its last in-range bytes as 0, and the last route lost its final scales. fused_dynamic_mxfp4_quant_moe_sort hits it through its split path. Such rows now read that one load byte by byte. The choice is a template parameter, so the other dims compile to the same code as before. Fixes #7646. Co-Authored-By: Claude Opus 5.5 <[email protected]> * [HIP] mxfp4_moe_sort: take the byte tail whenever a row ends inside a load Review follow-up. Select byte_tail when the last load of a row crosses the row end (ceil(cols / 32) % load bytes), not only when the row is not whole dwords: on gfx950 the range check is per dword, but a per-access check would also drop the in-range part of an 8- or 16-byte load (e.g. model_dim 1152). The load width now comes from one helper shared by the kernel and the launcher. The test adds model_dim 96 and 224 (the <= 256 bucket) and 1152. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- 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]>
…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]>
…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]>
Summary
Fixes #7646. When
model_dim / 32is not a multiple of 4,mxfp4_moe_sort_hip/mxfp4_moe_sort_fwdreturn 0 for the final scale bytes of the last scale row. K=2880 (gpt-oss hidden size) is one such case.fused_dynamic_mxfp4_quant_moe_sortis affected through its split path. This change reads that one load byte by byte. Every other shape compiles to the same code as before.Motivation
mxfp4_moe_sort_kernelreads each scale row in 4- to 32-byte loads from a buffer that ends exactly at the end of the scales. The last row's final load runs past that end, and the buffer range check is per dword. When the row is not a whole number of dwords, the dword holding the row's last in-range bytes therefore comes back as 0. Earlier rows read into the next row instead, which is in range, and the extra bytes are masked bycol < scaleN_valid.The visible result: the last token gets e8m0 0 (2^-127) for its last 64 input channels in every route. With the repro in #7646 at K=2880, 16 bytes are wrong for any M (2 columns × 8 routes), and all of them are 0. In a prefill batch that token is the prompt's last one, which produces the first output token.
Changes
csrc/kernels/quant_kernels.cu:mxfp4_moe_sort_kernelgets abyte_tailtemplate parameter. With it set, a load that would cross the end of the scales is issued as byte loads.byte_tailonly when a row ends inside one of the kernel's loads:ceil(cols / 32) % load_bytes != 0, whereload_bytesis the 2 to 16 bytes per load of that dispatch bucket. On gfx950 only rows that are not a whole number of dwords lose bytes. A whole-dword row that ends inside an 8- or 16-byte load (e.g. model_dim 1152) would also lose bytes if the range check were per access, so it takes the byte tail too. Every other shape keeps the old code unchanged.op_tests/test_moe_sorting_mxfp4.py: a new asserting section, "Row tails of the scale sort". See Changed op tests.Performance (if applicable)
mxfp4_moe_sort_hipalone on MI350X (gfx950), CUDA graph (50 launches per replay, median of 20), E=128, topk=8, block_m=32. Before/after runs were alternated twice on one GPU, and each time is the mean of the two runs.Across model_dim ∈ {256, 2048, 4096, 7168} and M ∈ {1, 64, 4096, 16384} at both stages, every ratio is within 0.97–1.02×. Dims that take the byte tail cost 4–9% where they were wrong (2880), and 2–5% for whole-dword rows that take it only as a guard against a per-access range check (1152). A variant that always issues the wide load and then re-reads the tail cost the same, so the simpler form is kept.
Testing
Documentation
This is a bug fix inside a kernel with no API change.
Dependencies
Breaking Changes
None.
PR Self-Review
Reviewed revision
c478dfb
Behavior and intent
When
model_dim / 32is not a multiple of 4, the last route's final scale bytes are now the input's bytes instead of 0. Every other shape runs the same code as before. The existing behavior is wrong because the per-dword buffer range check zeroes a dword that straddles the end of the scales, taking its in-range bytes with it.Validation and unverified scope
On MI350X (gfx950), ROCm 7.2.4, this branch:
python3 op_tests/test_moe_sorting_mxfp4.pywith default arguments exits 0. All 61 new row-tail checks pass, and no existing check or comparison reports a mismatch. Onmainthe new section fails on its first case:model_dim=320 tokens=1 stage1: 16 scale bytes differ.mxfp4_moe_sort_fwdandfused_dynamic_mxfp4_quant_moe_sortat M = 5, 300 and 4096. Before this change both were 16 wrong bytes at M = 300 and 4096, andmxfp4_moe_sort_fwdalso at M = 5.blackandruff==0.16.0are clean.Unverified:
PR-specific question
The changed load is in
mxfp4_moe_sort_kernel, which sorts e8m0 scale bytes for both MXFP4 and MXFP8. It is reached throughmxfp4_moe_sort_hip,mxfp4_moe_sort_fwdand the split path offused_dynamic_mx{fp4,fp8}_quant_moe_sort, on any arch that buildsmodule_quant.The byte-tail variant is selected on the host from the shape alone:
ceil(cols / 32) % mxfp4_moe_sort_load_bytes(THREAD_DATA) != 0, i.e. a row ends inside a load. Within that variant, a load takes the byte path only whenoffset + vec_size > scale_per_row * num_tokens * topk, i.e. the final load of the last row. Rows that end on a load boundary cannot have a load crossing the end of the buffer, so they are launched as the unchanged template instance (byte_tail = false). Unsupported widths (cols > 16384) are still rejected by the existing dispatch check before any launch.Changed op tests
op_tests/test_moe_sorting_mxfp4.py: the new section "Row tails of the scale sort" callsmxfp4_moe_sort_hipinto a zeroed buffer with nonzero input scale bytes and asserts the whole output equalsrun_torch.mainon gfx950, since its range check is per dword; it guards the byte-tail selection rather than reproducing mxfp4_moe_sort_fwd zeroes the last token's final scales when K // 32 is not a multiple of 4 (e.g. K=2880) #7646.On
mainit fails with the last row's tail bytes read as 0. The existing sections do not catch this for two reasons: their default dims (4096, 7168, 256, 2048) all havemodel_dim / 32 % 4 == 0, andtest_moe_mxfp4_sort/test_moe_mx_quant_sortcompare withcheckAllclose, which only logs.🤖 Generated with Claude Code