[OUTDATED] perf: optimize fused AllReduce + RMSNorm (custom_all_reduce) - #3462
Closed
ftyghome wants to merge 2 commits into
Closed
[OUTDATED] perf: optimize fused AllReduce + RMSNorm (custom_all_reduce)#3462ftyghome wants to merge 2 commits into
ftyghome wants to merge 2 commits into
Conversation
Contributor
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR optimizes the AMD GPU shfl_xor implementation for 4-byte types by adding DPP-based fast paths for common XOR masks and reusing a single bit-cast input value for the fallback path.
Changes:
- Add DPP fast paths for
maskvalues 1, 2, 4, and 8 inshfl_xor. - Keep a ds_bpermute-based fallback for other masks.
- Avoid repeated
__builtin_bit_cast(int, var)by caching it asv.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+912
to
+929
| switch(mask) | ||
| { | ||
| case 1: | ||
| return __builtin_bit_cast(T, __builtin_amdgcn_mov_dpp(v, 0xb1, 0xf, 0xf, true)); | ||
| case 2: | ||
| return __builtin_bit_cast(T, __builtin_amdgcn_mov_dpp(v, 0x4e, 0xf, 0xf, true)); | ||
| case 4: | ||
| { | ||
| int r = __builtin_amdgcn_update_dpp(v, v, 0x104, 0xf, 0x5, true); | ||
| r = __builtin_amdgcn_update_dpp(r, v, 0x114, 0xf, 0xa, true); | ||
| return __builtin_bit_cast(T, r); | ||
| } | ||
| case 8: | ||
| { | ||
| int r = __builtin_amdgcn_update_dpp(v, v, 0x108, 0xf, 0x3, true); | ||
| r = __builtin_amdgcn_update_dpp(r, v, 0x118, 0xf, 0xc, true); | ||
| return __builtin_bit_cast(T, r); | ||
| } |
Comment on lines
+915
to
+921
| return __builtin_bit_cast(T, __builtin_amdgcn_mov_dpp(v, 0xb1, 0xf, 0xf, true)); | ||
| case 2: | ||
| return __builtin_bit_cast(T, __builtin_amdgcn_mov_dpp(v, 0x4e, 0xf, 0xf, true)); | ||
| case 4: | ||
| { | ||
| int r = __builtin_amdgcn_update_dpp(v, v, 0x104, 0xf, 0x5, true); | ||
| r = __builtin_amdgcn_update_dpp(r, v, 0x114, 0xf, 0xa, true); |
Comment on lines
+926
to
+927
| int r = __builtin_amdgcn_update_dpp(v, v, 0x108, 0xf, 0x3, true); | ||
| r = __builtin_amdgcn_update_dpp(r, v, 0x118, 0xf, 0xc, true); |
… for fused AR+RMSNorm reduce_scatter_cross_device_store: warp-per-rank+LDS -> FLAT. Each thread reduces 1 pack across all ngpus inputs (fp32) and broadcasts the bf16 sum to every rank's tmp. Bit-identical output (same canonical reduce order, same sum); faster at every m (0.91x@m64 -> 0.86x@m512 vs warp-per-rank) because N stores/thread pipeline the broadcast far better than warp-per-rank's 1 store/thread. No LDS, no intra-block sync. dispatchFusedAllReduceRMSNorm: launch stage1 at block 256 (rs_block/rs_grid) so small-m decode engages ~56 active CUs instead of 28; win-gate one-stage vs two-stage by data volume (use_1stage && bytes < 0.5MiB) so two-stage auto-engages at >=0.5MiB (m~37 @hidden7168, incl. m=64 decode) while small m keeps one-stage where its lower overhead wins. E2E Kimi-K2.5-MXFP4 TP4 CONC=64: median TPOT -1.3%, output throughput +1%, bit-correct 256/256. Kernel-level two-stage 1.21x vs one-stage at m=64.
This comment was marked as outdated.
This comment was marked as outdated.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
This PR makes three small updates to the custom fused AllReduce + RMSNorm kernels:
Technical Details
1stage
ar_fusion_epilogue_block_reducenow usesmultithread_reducefromhip_reduce.hinstead of the genericwarpReduceimplementation. This switches the intra-wave reduction from theds_bpermuteXOR butterfly to the existing DPP tree reduce.2stage
reduce-scatter.
reduce_scatter_cross_device_storeis rewritten as a flat reduce-scatter. Each thread reduces one pack across all input ranks in fp32, downcasts the result to bf16, and writes it to every rank's temporary buffer directly. This removes the previous LDS staging and__syncthreads()from the warp-per-rank implementation. The reduce order overptrs[0..ngpus-1]is preserved, so the result remains bit-identical to the previous path.stage-1 launch config. The stage-1 launch now uses block size 256 instead of 512. This gives small-
mdecode shapes more parallelism across CUs.stage-2 RMSNorm reduce.
local_device_load_rmsnormnow useswave_reducefromhip_reduce.hfor the RMSNorm square-sum reduction. This keeps the reduction implementation consistent with the shared DPP primitive. In testing, this change alone is performance-neutral.This PR does not change the one-stage/two-stage selection heuristic. The thresholds discussed in #3458 can therefore still be used as-is.
Test Plan
Test environment:
Benchmark:
The benchmark script (test_fused_ar_rmsnorm_perf.py is inspired by the script posted by @TennyWang1223 in #3458, with a few changes to improve stability.
To reduce host launch overhead and cross-rank rendezvous jitter, 2000 op calls are captured into one CUDA graph, and a single replay is timed with CUDA events. The reported latency is the minimum over 80 replays, taking the max latency across ranks. GPU clocks were pinned with:
Reproduce commands:
TP_LIST={4,8} AITER_AR_1STAGE={0,1} PYTHONPATH=$PWD \ python <script_path>Restore GPU clocks after benchmarking:
Test Results
For the CDF plots below, curves closer to the upper-left corner indicate better performance.
One-stage path
Two-stage path
Under the selection thresholds from #3458 (one-stage for m <= 32 at TP4 and m <= 16 at TP8), the latency-critical small-m decode shapes are served by the one-stage path, which this PR speeds up. The shapes where the two-stage path regresses largely fall in that one-stage range, so the practical impact is minimal; on the larger shapes that do use the two-stage path the change is a 2.5% net gain.
This PR does not change the 1stage/2stage selection heuristic, so the threshold from #3458 remains applicable.