Repository navigation
[HIP] Add fused all_reduce_add; retune gfx950 all-reduce dispatch - #5918
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
|
No activity for 15 days, so this is now labelled |
|
@aiter-bot review |
|
aiter-bot — queued behind 1 review(s); expect to start in about 50 min. The box reviews one PR at a time. |
ROCm/aiter PR #5918 — [HIP] Add fused all_reduce_add; retune gfx950 all-reduce dispatchAdds a fused all-reduce returning all_reduce(inp + addend) — the LL/LL128 kernels add the addend on load, every other path stages the sum into the registered input buffer first — and retunes the gfx950 LL/LL128/1-stage/2-stage dispatch cutovers from sweep data. Review (advisory):
|
|
@cpersson-amd I tested head [P1] The fused API accepts incompatible input layouts and silently produces incorrect resultsLocation: The predicate checks shape, dtype, alignment and weak contiguity, but it does not ensure compatible physical layouts. A transposed tensor occupying its full storage passes Reproducer, executed on both ranks of a TP2 communicator ( device = torch.device(f"cuda:{rank}")
x = torch.full((16, 16), rank + 1, device=device, dtype=torch.bfloat16)
y = (
(torch.arange(256, device=device) % 17)
.to(torch.bfloat16)
.reshape(16, 16)
.t()
)
assert ca.should_custom_ar_add(x, y) # Returns True
expected = 3 + 2 * y
separate = ca.custom_all_reduce(x + y)
fused = ca.custom_all_reduce_add(x, y)
torch.cuda.synchronize()
assert torch.equal(separate, expected) # Passes
assert torch.equal(fused, expected) # FailsHere Please restrict the API to layouts supported by the kernels, or materialize unsupported layouts before dispatch. A conservative fix is to require contiguous inputs and validate the output layout as well. The direct [P2] The gfx950 dispatch change breaks valid 16-byte TP2 FP32 all-reduce inputsLocation: new gfx950 dispatch. FP32 bypasses the LL fast path, and the new gfx950 TP2 dispatch always selects two-stage. For a 16-byte input, Reproducer on both TP2 ranks: device = torch.device(f"cuda:{rank}")
x = torch.full((4,), rank + 1, device=device, dtype=torch.float32)
out = torch.full_like(x, float("nan"))
assert ca.should_custom_ar(x) # Returns True
ca.all_reduce(x, out=out)
torch.cuda.synchronize()Observed results:
Adjacent sizes of 32, 48, 64, 128, 256 and 1024 bytes passed. This is a regression in the existing plain all-reduce API, independently of adoption of the fused API. Please preserve a valid tiny-message dispatch or correctly handle fewer packs than ranks in the two-stage launch, and add a 16-byte regression case. Validation contextThe PR's unmodified |
|
Review Summary Scope: Adds CustomAllreduce.all_reduce_add. LL/LL128 do the add on load; the other paths stage the sum in the registered pool first. The PR also retunes the gfx950 all-reduce dispatch. Verdict: HIGH RISK (advisory) Validation: The auto-validator could not be used: git apply fails against current main, and the report could not be read because jsonschema is not installed. A 3-way merge with main is clean. I ran the new test by hand on the merged tree on gfx942 (MI300X), and it passes at TP2 and TP8 for bf16, fp16 and fp32. The gfx950 dispatch was not exercised because this machine has no gfx950. Perf: Not run. The test has no benchmark mode, and the retune only applies on gfx950. Findings
|
0957559 to
9bdc0ec
Compare
|
Thanks @junhaha666 and @TennyWang1223 for the reviews! Rebased on current main (including #6446) and pushed. Fixes
Perf mode: Retuned caps: on current main (#6446, #6640), LL and LL128 are slower than when I tuned, so some caps were too high. I re-derived them with forced-kernel sweeps for bf16/fp16/fp32 at TP2/4/8. New caps in KB, aligned / unaligned, with the old value in parentheses where it changed:
I reran
Validation (MI355X): |
|
Thanks, all four review items are fixed as asked. Before approval: (1) rebase onto main — #6270 all_reduce_residual and #7149 select_ar_stage conflict with this PR; please decide whether the gfx950 1/2-stage retune and the size < world_size_ guard move into the shared select_ar_stage (note allreduce_residual currently assumes TP2 is always 1-stage) or stay in allreduce only, and say which; (2) re-run --bench on MI355X after the rebase, since #6954 changed the reduce order on every path; (3) the multigpu CI run failed before it reached the custom-AR tests (test_mori_smoke.py is missing on this branch), so please get a green multi-GPU run after the rebase. |
# Conflicts: # aiter/dist/device_communicators/custom_all_reduce.py # aiter/ops/custom_all_reduce.py # csrc/include/custom_all_reduce.cuh # csrc/include/custom_all_reduce.h # csrc/include/rocm_ops.hpp
Move the gfx950 1-/2-stage rule and the packs < world_size guard into select_ar_stage() and the LL/LL128 caps into select_ll_path(), so allreduce and allreduce_residual pick stages the same way (TP2 residual is no longer 1-stage only). Add ops.all_reduce_one_shot and make uses_1stage_ar() query the dispatch, LL included. ROCm#6954 made LL/LL128 slower near their caps; re-swept on the merged tree: TP8 plain LL aligned 40 -> 32 KB, TP8 fused LL aligned 64 -> 56 KB, TP4 fused unaligned LL 96 -> 112 KB with LL128 dropped.
|
Merged main again, which brought in the lamport all-reduce (#7452). It runs ahead of every other path for bf16/fp16 and has no addend input, so as merged
Add, regression, residual, rank-order and lamport tests pass at TP2/4/8 (add also with lamport off). The one failure is TP6 fp16 |
|
@TennyWang1223 the CI is green except for an unrelated failure: Failed test files (shard 4): op_tests/test_flydsl_hgemm.py. The multigpu test has been a bit flaky, but is passing now as well. Please let me know if I need to do anything before a potential merge. |
The LL and LL128 kernels staged into the meta buffer's post-Signal region, the same memory the 2-stage kernels use as tmp. The 2-stage kernels end without a barrier and the LL kernels write into peers' scratch without one, so an LL call right after a 2-stage call could overwrite a peer's reduce-scatter partial while other ranks still read it in stage 2. This failed CI in test_custom_allreduce_add.py's interleaved graph (TP8 bf16, fused 256 KB + 16 B naive 2-stage followed by fused 88 KB + 16 B LL). Plain all-reduce hits it too when lamport is off (AITER_CUSTOM_AR_DISABLE_LAMPORT=1); with lamport on, lamport takes every size plain LL / LL128 would serve after a 2-stage call. The meta buffer is now [Signal][LL / LL128 scratch][tmp]: get_tmp_buf and local_tmp_reduced_ptr skip kLLScratchBytes and meta_size() includes it, so the Python allocation (meta_size() + 2 * max_size) is unchanged. kLL128PocCapBytes drops from 4 MB to 1 MB (the dispatch sends at most 896 KB), which keeps the new region at 21 MiB.
0101f2e
|
The last multi-GPU run failed in Cause: the LL / LL128 kernels staged into the meta buffer's post-Signal region, the same memory the 2-stage kernels use as tmp. The 2-stage kernels end without a barrier (the next kernel's Scope: the fused path's caps make 2-stage → LL common. Plain all-reduce has the same exposure, on main too, when lamport is off ( Fix: the meta buffer is now Validation (MI355X):
|
Resolve the conflict in csrc/include/custom_all_reduce.cuh with #5918: keep main's fused_add_beats_lamport() and this branch's runLamport() comment and other-operand signature. Co-Authored-By: Claude Opus 5.5 <[email protected]>
One conflict: the _init_ipc meta-size comment in aiter/dist/device_communicators/custom_all_reduce.py. The old-arch line is main's (#5918 put the LL scratch in meta_size()); the gfx1250 lines are this PR's.
Motivation
In vLLM MoE layers (DeepSeek V4.1 Flash, TP4 + EP on MI355X), the shared-expert output is added to the routed output right before the TP all-reduce. Both are per-rank partial sums, so the layer computes
AR(shared + routed). The add is a separate elementwise kernel: about 4.4 µs per MoE layer in a decode profile, plus one extra graph node per layer. This PR adds an all-reduce that sums its two inputs, so the add costs nothing on the small-message path.Measuring the new path showed that the gfx950 kernel dispatch was off for plain all-reduce too. On aligned sizes one-stage never won, and the LL / LL128 cutovers were too high. On sizes that aren't a multiple of
world_size * 16bytes ("unaligned"), the cutovers were also wrong in the other direction. This PR retunes both from sweep data.Changes
New API.
CustomAllreduce.all_reduce_add(inp, addend, out=...)andcustom_all_reduce_add(...)returnall_reduce(inp + addend). The op isops.all_reduce_add;supports_all_reduce_addis false on gfx1250.inp,addendandoutmust be contiguous with the same shape and dtype:should_custom_ar_addreturns false otherwise, andall_reduce_addraisesValueError(the C++ op also checks dtype and numel).ar_ll_gfx9) and LL128 (ar_ll128_unroll2) take an optionaladdend, selected through aHAS_ADDENDtemplate parameter, so the existing instantiations are unchanged. Each rank addsinp + addendon load and rounds toTbefore publishing, so results are bitwise identical to a separate add followed by the all-reduce.add_inputskernel writes the sum into the registered input buffer, then the normal all-reduce runs. Neither input needs to be IPC-registered.gfx950 dispatch. gfx950 is detected once from
gcnArchNamein theCustomAllreduceconstructor. Other architectures keep the existing tables. There is no environment flag: the dispatch applies automatically on gfx950, and the fused path only runs when a caller usesall_reduce_add.Aligned sizes can use the optimized two-stage kernel. Unaligned sizes can only use one-stage or the naive two-stage kernel, which is much slower at TP4/TP8. So LL/LL128 and one-stage stay faster to larger sizes there. The caps were re-derived on current main with forced-kernel sweeps: #6446 (fixed LL128 slot stride), #6640 (RNE bf16 stores) and #6954 (fixed reduce order) made LL / LL128 slower than when they were first tuned. Caps are listed as aligned / unaligned:
Everything else goes to two-stage (naive for unaligned sizes). One-stage is also used when there are fewer 16-byte packs than ranks, where two-stage would launch zero blocks.
Shared with
all_reduce_residual(#6270). The one-/two-stage rule lives inselect_ar_stage(), whichall_reduce_residualalso uses, so the residual path gets the gfx950 rule too. At TP2 it was one-stage only and now takes two-stage or naive like the other TP sizes.uses_1stage_ar()now asks the C++ dispatch through a newops.all_reduce_one_shotinstead of copying the old thresholds, so it reports whatcustom_all_reduceruns, LL and LL128 included. It takes an optionalregistered_input, because registered fp16/fp32 skip LL.Results
MI355X, CUDA graph, registered inputs, TP2/4/8, bf16/fp16/fp32. The sizes are 16 from 16 KB to 16 MB ("aligned") and the same sizes + 16 B ("unaligned"), so 96 points per TP.
test_custom_allreduce_add.py --benchwas run on main (f1965cd) and on this branch merged with it, in the order main, branch, branch, main. Each run times the variants in 7 alternating rounds, takes the median and the max over ranks; the tables use the mean of the two runs per build. "Same" means within max(0.2 µs, 1%). "Kernel changed" models both dispatches (registered fp16/fp32 use the broadcast output, which skips LL).Plain all-reduce, new vs original dispatch
No size with a changed kernel is slower. The TP8 unaligned noise comes from 16 MB + 16 B, which runs the same naive two-stage kernel on both builds. There the branch is 1.5 µs faster for bf16, 3.0 µs slower for fp16 and 4.2 µs faster for fp32 (up to 3.5%).
Below 16 KB, plain bf16 runs LL with both dispatches. Registered fp16/fp32 go to two-stage instead of one-stage there, which is 0.2–1.1 µs faster at every aligned size from 2 KB (measured with the residual bench below).
all_reduce_residual
Same setup, sizes from 2 KB to 4 MB (+0 / +16 B), bf16/fp16/fp32, TP2/4/8, 360 points. The comparison is this branch against the same tree with main's one-/two-stage rule for the residual kernel, run in the order main rule, branch, branch, main rule:
The fused residual is faster than all-reduce followed by a separate add at 346 of the 360 points and the same at 14, so
_AR_RESIDUAL_MAX_BYTESstays unlimited on gfx950.all_reduce_add, add inside LL / LL128
This is bf16/fp16 up to the fused caps, which covers the MoE decode sizes. The comparison is against today: a separate add followed by all-reduce with the original dispatch.
Most of this comes from fusing. Against a separate add with the new dispatch, all 74 points are faster too (median −1.21 to −1.60 µs, at least −0.51 µs).
all_reduce_add, staged add
These are sizes above the fused caps, plus all fp32 sizes. Here the sum is written to the registered buffer and then all-reduced, so fusing can't save anything. The comparison is against a separate add with the new dispatch, which shows the cost of the fused API itself. The gain over today at these sizes is the dispatch gain from the plain table.
2 of 214 points are slower: TP8 fp32 1 MB + 16 B by +0.25 µs (1.2%) and TP4 fp16 4 MB by +0.51 µs (1.1%). Which points cross the threshold varies between runs. Earlier runs had 4–5 slower points each, between 512 KB and 4 MB and at most +1.0 µs (2.2%). The cost is in the hand-off from the staging kernel to the all-reduce: a
torch.addinto the same registered buffer is 0.2–0.35 µs faster before the same all-reduce. A two-stage kernel that does the add itself while writing to the IPC buffer was 0.5–1.7 µs slower, so the staging stays. A caller that wants to avoid it can use a separate add above the fused caps. (The −4.87 µs at TP8 unaligned is at 16 MB + 16 B, where timings vary by several µs between runs.)End-to-end, DeepSeek V4.1 Flash decode
The fused path saves a few µs per MoE layer, so it was also measured on a full model: vLLM on MI355X, TP4 + EP, FP8, 12 sweep points,
all_reduce_addon the shared-expert add against a separate add with the original dispatch. This was measured before the rebase onto #6446 / #6640, with the earlier caps (TP4 fused LL up to 80 / 112 KB, LL128 up to 160 KB unaligned), and was not rerun. Median TPOT, in ms:The mean Δ is -0.27%, faster at 5 of 12 points. The sign tracks concurrency: below 16 the fused path is 0.8–1.7% faster, at 16 and above it is 0.0–0.7% slower. Repeat-to-repeat noise on median TPOT is about 0.3%, so the low-concurrency gains are real and everything above cc16 is flat. Median ITL behaves the same way (-1.6 to -1.9% below cc16, +0.2 to +0.6% above), and output throughput moves +0.3% on average.
Accuracy stays unchanged:
Testing
New test
op_tests/multigpu_tests/test_custom_allreduce_add.py, for bf16/fp16/fp32 at TP2/4/8:all_reduceandall_reduce_add, in eager and graph mode, and withoutaliasinginporaddend;all_reduce_addbitwise equal toall_reduce(x + y)on random data;open_fp8_quant=True(numel 128 × 2048 and 512 × 2048): bitwise equal to a separate add followed by the quantized all-reduce;inp,addendorout, and a wrong-dtypeout, must raiseValueError;--benchprints the CUDA-graph time per call ofall_reduce, add +all_reduceandall_reduce_add(fused only where supported, so it also runs on main).It passes at TP2/4/8 for bf16/fp16/fp32.
Existing tests
test_custom_allreduce.py --regressionpasses for bf16/fp16/fp32 at TP2/4/8.test_custom_allreduce_residual.py -t 8 6 4 2 -d bf16 fp16 fp32andtest_custom_allreduce_rank_order.pypass.uses_1stage_ar()was checked against the cap table at 2,718 sizes and modes per TP (bf16/fp16/fp32, registered and eager, up to 512 KB), with no mismatches.Notes
The new test preallocates its CUDA-graph outputs. Graph output buffers are registered through an address-deduplicated map, and
flush_graph_buffersreturns early when a rank has nothing new to register. If allocator reuse gives ranks different registration counts, the IPC handle exchange desyncs and fails withhipIpcOpenMemHandleerrors. That problem already exists before this PR, and the PR doesn't change it.Submission Checklist