Skip to content

[HIP] Add fused all_reduce_add; retune gfx950 all-reduce dispatch - #5918

Merged
TennyWang1223 merged 23 commits into
ROCm:mainfrom
cpersson-amd:ar_fused_add
Nov 24, 2026
Merged

TennyWang1223 merged 23 commits into
ROCm:mainfrom
cpersson-amd:ar_fused_add

Conversation

@cpersson-amd

@cpersson-amd cpersson-amd commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

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 * 16 bytes ("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=...) and custom_all_reduce_add(...) return all_reduce(inp + addend). The op is ops.all_reduce_add; supports_all_reduce_add is false on gfx1250. inp, addend and out must be contiguous with the same shape and dtype: should_custom_ar_add returns false otherwise, and all_reduce_add raises ValueError (the C++ op also checks dtype and numel).

  • LL (ar_ll_gfx9) and LL128 (ar_ll128_unroll2) take an optional addend, selected through a HAS_ADDEND template parameter, so the existing instantiations are unchanged. Each rank adds inp + addend on load and rounds to T before publishing, so results are bitwise identical to a separate add followed by the all-reduce.
  • Other paths (one-stage, two-stage, the fp16 fp8-quant path, and fp32): a small add_inputs kernel 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 gcnArchName in the CustomAllreduce constructor. 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 uses all_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:

TP2 TP4 TP8
Plain LL, up to 64 / 72 KB 40 / 56 KB 32 / 48 KB
Plain LL128, up to – / 96 KB – / – – / –
Fused LL, up to 80 / 80 KB 64 / 112 KB 56 / 96 KB
Fused LL128, up to 208 / 384 KB – / – – / –
One-stage, below never / never never / 104 KB never / 256 KB

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 in select_ar_stage(), which all_reduce_residual also 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 new ops.all_reduce_one_shot instead of copying the old thresholds, so it reports what custom_all_reduce runs, LL and LL128 included. It takes an optional registered_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 --bench was 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

TP, sizes kernel changed faster / same / slower median Δ Δ range noise (unchanged sizes)
TP8 aligned 10 of 48 10 / 0 / 0 -0.63 µs (-8.8%) -1.98 … -0.43 µs ±0.2 µs
TP8 unaligned 16 of 48 16 / 0 / 0 -1.64 µs (-14.5%) -2.73 … -0.24 µs ±4.2 µs
TP4 aligned 22 of 48 21 / 1 / 0 -1.14 µs (-16.2%) -2.06 … -0.10 µs ±0.8 µs
TP4 unaligned 6 of 48 3 / 3 / 0 -0.16 µs (-2.0%) -0.33 … -0.06 µs ±1.2 µs
TP2 aligned 44 of 48 42 / 2 / 0 -1.31 µs (-10.9%) -5.04 … -0.17 µs ±0.1 µs
TP2 unaligned 44 of 48 38 / 6 / 0 -0.92 µs (-9.4%) -4.28 … -0.01 µs ±0.1 µs

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:

  • 200 points faster (−0.2 to −3.3 µs), 159 the same.
  • One point is slower: TP4 bf16 112 KB + 16 B, by +0.26 µs (3%). Between 104 and 128 KB unaligned at TP4, one-stage and naive two-stage are within 0.3 µs of each other.

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_BYTES stays 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.

TP, sizes points faster / same / slower median Δ Δ range
TP8 aligned 6 6 / 0 / 0 -1.70 µs (-21.2%) -3.54 … -1.54 µs
TP8 unaligned 10 10 / 0 / 0 -1.74 µs (-20.8%) -3.57 … -0.88 µs
TP4 aligned 8 8 / 0 / 0 -2.21 µs (-26.4%) -3.60 … -1.53 µs
TP4 unaligned 12 12 / 0 / 0 -1.60 µs (-21.7%) -3.60 … -0.92 µs
TP2 aligned 18 18 / 0 / 0 -2.10 µs (-28.2%) -4.03 … -1.29 µs
TP2 unaligned 20 20 / 0 / 0 -2.28 µs (-27.4%) -4.05 … -1.29 µs

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.

TP, sizes points faster / same / slower median Δ Δ range
TP8 aligned 42 0 / 42 / 0 -0.00 µs -0.13 … +0.13 µs
TP8 unaligned 38 3 / 34 / 1 -0.02 µs -4.87 … +0.25 µs
TP4 aligned 40 0 / 39 / 1 -0.03 µs -1.20 … +0.51 µs
TP4 unaligned 36 0 / 36 / 0 -0.02 µs -0.53 … +0.19 µs
TP2 aligned 30 0 / 30 / 0 -0.01 µs -0.08 … +0.59 µs
TP2 unaligned 28 0 / 28 / 0 -0.08 µs -0.68 … +0.05 µs

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.add into 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_add on 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:

input / output concurrency separate add all_reduce_add Δ
8192 / 1024 4 7.36 7.24 -1.7%
8192 / 1024 8 8.95 8.81 -1.5%
8192 / 1024 16 10.86 10.87 +0.1%
8192 / 1024 32 14.92 14.93 +0.1%
8192 / 1024 64 21.12 21.14 +0.1%
8192 / 1024 128 32.44 32.45 +0.0%
8192 / 1024 256 54.34 54.58 +0.4%
60000 / 600 4 12.57 12.47 -0.8%
60000 / 600 16 35.65 35.57 -0.2%
60000 / 600 64 126.34 126.74 +0.3%
128000 / 1024 4 14.71 14.60 -0.8%
128000 / 1024 16 46.91 47.23 +0.7%

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:

Filter Default Tuned Δ
flexible-extract 0.9227 ± 0.0074 0.9189 ± 0.0075 -0.0038
strict-match 0.9219 ± 0.0074 0.9189 ± 0.0075 -0.0030

Testing

  • New test op_tests/multigpu_tests/test_custom_allreduce_add.py, for bf16/fp16/fp32 at TP2/4/8:

    • 49 sizes: 16, 48 and 112 B (fewer packs than ranks), and sizes at and just past every aligned and unaligned cutover, up to 4 MB, including the naive two-stage path;
    • exact match against an fp32 reference for both all_reduce and all_reduce_add, in eager and graph mode, and with out aliasing inp or addend;
    • all_reduce_add bitwise equal to all_reduce(x + y) on random data;
    • fp16 open_fp8_quant=True (numel 128 × 2048 and 512 × 2048): bitwise equal to a separate add followed by the quantized all-reduce;
    • transposed inp, addend or out, and a wrong-dtype out, must raise ValueError;
    • one graph interleaving plain and fused calls across LL, LL128, one-stage and two-stage, including back-to-back plain and fused LL128 calls of different sizes ([HIP] Fix ar_ll128_unroll2 hang on consecutive calls of different sizes #6446);
    • --bench prints the CUDA-graph time per call of all_reduce, add + all_reduce and all_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 --regression passes for bf16/fp16/fp32 at TP2/4/8. test_custom_allreduce_residual.py -t 8 6 4 2 -d bf16 fp16 fp32 and test_custom_allreduce_rank_order.py pass.

  • 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_buffers returns early when a rank has nothing new to register. If allocator reuse gives ranks different registration counts, the IPC handle exchange desyncs and fails with hipIpcOpenMemHandle errors. That problem already exists before this PR, and the PR doesn't change it.

Submission Checklist

@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
multigpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 5918 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

@cpersson-amd
cpersson-amd marked this pull request as ready for review September 30, 2026 18:11
@cpersson-amd
cpersson-amd requested a review from a team September 30, 2026 18:11
@github-actions github-actions Bot changed the title Add fused all_reduce_add; retune gfx950 all-reduce dispatch [HIP] Add fused all_reduce_add; retune gfx950 all-reduce dispatch Sep 30, 2026
@github-actions github-actions Bot added the HIP label Sep 30, 2026
@zufayu
zufayu requested a review from junhaha666 October 1, 2026 00:36
@github-actions

Copy link
Copy Markdown
Contributor

No activity for 15 days, so this is now labelled stale. A push or a comment clears it; keep-open exempts it. Nothing is closed automatically.

@github-actions github-actions Bot added the stale The PR hasn't been updated in 2 weeks label Oct 16, 2026
@junhaha666

Copy link
Copy Markdown
Contributor

@aiter-bot review

@aiter-bot

Copy link
Copy Markdown

aiter-bot — queued behind 1 review(s); expect to start in about 50 min. The box reviews one PR at a time.

@aiter-bot

Copy link
Copy Markdown

ROCm/aiter PR #5918 — [HIP] Add fused all_reduce_add; retune gfx950 all-reduce dispatch

Adds 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): ⚠️ NEEDS WORK
Validation (deterministic): NOT RUN — auto-validation disabled (REVIEW_AUTO_VALIDATE=0) and this box has no built aiter environment (system python has no torch), so the target op_tests/multigpu_tests/test_custom_allreduce_add.py existed but could not be run — an environment gap, not a PR defect
Perf (advisory): NOT RUN — the target exposes no benchmark entry point (no perftest/bench mode, unlike the sibling test_custom_allreduce.py); the only timing evidence is the author's 1458-point paired sweep (MI355X, TP2/4/8, bf16/fp16/fp32, 7 alternating rounds), whose harness and results file are not in the repo

⚠️ [verified] The patch no longer applies to main (git apply fails at csrc/include/custom_all_reduce.cuh:4436): #6446 landed after this PR's base and replaced the LL128 per-message-size slot stride with a fixed kLL128SlotStride to fix a hang on consecutive different-size LL128 calls, but this PR's HAS_ADDEND changes still sit on the old stride (head lines 3885/3898/3923) and its new gfx950 caps widen the LL128 window (fused up to 768 KB at TP2) into exactly that scenario, which the PR's interleave test — one LL128 size only — cannot catch. Author must rebase onto #6446's fixed-stride kernel and re-run the multigpu suite, which now carries main's LL128 mixed-size regression.
⚠️ [verified] The new fp8-quant + addend branch (csrc/kernels/custom_all_reduce.cu:96-104) is reached by no test — test_custom_allreduce_add.py never passes open_fp8_quant=True and the existing test_custom_allreduce_fp8.py drives only the plain path — so a wrong staged sum or quant read at fp16 numel ≥ 128*2048 would ship silently to any caller passing open_fp8_quant=True. Author must add an open_fp8_quant=True case to the new test.
⚠️ [verified] The only test covering the new op and the retuned gfx950 dispatch, op_tests/multigpu_tests/test_custom_allreduce_add.py, runs solely under the opt-in multigpu CI label, so a merge without the label ships a dispatch change to every MI355X TP deployment's plain all_reduce with no CI coverage at all. Reviewer should ask for the multigpu label on this PR and for sign-off from the custom-AR path's de-facto owner (TennyWang1223, top committer of both changed core files).
⚠️ [verified] The retune's 12 cap cells and its 21 slower staged points (768 KB–16 MB, +1.0–2.8%, cause unknown per the PR) are not re-derivable in-tree: op_tests/multigpu_tests/test_custom_allreduce_add.py ships no timing mode, the sweep harness and the linked results file are not in the repo, and the sibling test_custom_allreduce.py shows the perftest pattern that was not used. Author must ship the sweep script or add a perftest mode so the cutovers can be re-measured.
📝 [verified] No consumer is wired: custom_all_reduce_add in aiter/dist/device_communicators/custom_all_reduce.py has no in-tree caller, aiter/dist/communication_op.py gained no tensor_model_parallel_all_reduce_add wrapper, and no vLLM PR is linked, so the fused path stays dead until vLLM's MoE shared-expert add calls it. Reviewer should ask where the vLLM-side change is and who will pass the addend.

@junhaha666

Copy link
Copy Markdown
Contributor

@cpersson-amd I tested head 0957559102a77dd6cb668dc62c541c052abffcb5 on gfx950 and reproduced two correctness issues.

[P1] The fused API accepts incompatible input layouts and silently produces incorrect results

Location: should_custom_ar_add.

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 is_weak_contiguous, while the fused kernels and the staging-add kernel read both inputs using flat physical offsets without handling strides.

Reproducer, executed on both ranks of a TP2 communicator (ca is initialized on the current rank's device):

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)     # Fails

Here x.stride() == (16, 1) and y.stride() == (1, 16). The fused result has 240/256 incorrect elements on each rank. This reproduces with BF16, FP16 and FP32. A larger BF16 case, (1024, 1024), also fails through the staged-add path.

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 all_reduce_add entry point should enforce the same contract.

[P2] The gfx950 dispatch change breaks valid 16-byte TP2 FP32 all-reduce inputs

Location: new gfx950 dispatch.

FP32 bypasses the LL fast path, and the new gfx950 TP2 dispatch always selects two-stage. For a 16-byte input, size becomes one 16-byte pack. The subsequent integer division size / world_size_ is zero, producing a zero-block launch.

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:

Version / operation Result
Base af3514ad6be7d1a2ec1ae25dc9cabdaa6834f0ac, plain all-reduce Correct: [3, 3, 3, 3] on both ranks
PR head, plain all-reduce HIP error 9, hipErrorInvalidConfiguration; output remains NaN
PR head, all_reduce_add with the same input size Same failure

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 context

The PR's unmodified test_custom_allreduce_add.py passed at TP2/4/8, with BF16/FP16/FP32, all 32 sizes, four changing-input iterations, eager/graph checks and the interleaved graph. The two additional cases above are not covered by that suite.

@github-actions github-actions Bot removed the stale The PR hasn't been updated in 2 weeks label Oct 24, 2026
@zufayu
zufayu requested review from TennyWang1223 and removed request for junhaha666 October 27, 2026 07:13
@zufayu zufayu assigned TennyWang1223 and unassigned junhaha666 Oct 27, 2026
@TennyWang1223

Copy link
Copy Markdown
Contributor

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

  1. [HIGH] [verified] Mismatched strides give silently wrong results. should_custom_ar_add (custom_all_reduce.py:1276) does not check that inp, addend and out have the same strides, but all three kernels read by flat offset. With a contiguous x and a transposed y of shape (16,16), the check passes and 240 of 256 output elements are wrong. This reproduces on bf16, fp16 and fp32. Fix: require matching strides or contiguous inputs in should_custom_ar_add and all_reduce_add, and add a strided test case.
  2. [WARNING] [verified] Plain all_reduce on gfx950 TP2 fails for 16-byte messages. The new gfx950 dispatch (custom_all_reduce.cuh:4763) always picks 2-stage at TP2. For fp32 numel=4 (or fp16 numel=8 on the registered graph path), the launch gets blocks = 0 (:4780), which is an invalid configuration. The base code used 1-stage here and handled it. Fix: keep 1-stage, or set blocks to at least 1, when there are fewer packs than ranks; add a 16-byte test case.
  3. [WARNING] [verified] The fp8-quant + addend branch has no test. custom_all_reduce.cu:98 (fp16, numel ≥ 262144, open_fp8_quant=True) is never exercised. Fix: add a test case for it.
  4. [WARNING] [verified] CI has not tested this change. No check failed, but Multi-GPU Tests was skipped because the PR has no multi-GPU label. All green runs used the old base af3514a, which predates [HIP] Fix ar_ll128_unroll2 hang on consecutive calls of different sizes #6446, [HIP] [CustomAR] Keep CUDA-graph buffer registration in step across ranks #6819 and [CustomAllreduce] Re-take the expandable_segments decision at capture time #5799. Ask: rebase and run with the multi-GPU label on MI355X.
  5. [NOTE] [inferred] The gfx950 cutover table can't be reproduced. The sweep script and the results file are not in the repo. Ask: commit the sweep harness or add a perftest mode.

@cpersson-amd

Copy link
Copy Markdown
Contributor Author

Thanks @junhaha666 and @TennyWang1223 for the reviews! Rebased on current main (including #6446) and pushed.

Fixes

  • Layouts (P1, Tenny 1): should_custom_ar_add now requires contiguous inp and addend with the same shape and dtype. all_reduce_add raises ValueError if inp, addend or out is non-contiguous or mismatched, and the C++ op also checks out. The test runs your transposed case for each tensor, plus a wrong-dtype out.
  • 16-byte inputs (P2, Tenny 2): gfx950 now uses 1-stage when there are fewer 16-byte packs than ranks. The test adds 16 / 48 / 112 B cases; with the fix reverted, they fail with hipErrorInvalidConfiguration.
  • [HIP] Fix ar_ll128_unroll2 hang on consecutive calls of different sizes #6446 (bot): the LL128 add path uses the fixed kLL128SlotStride. The interleaved graph now runs plain and fused LL128 calls of different sizes back to back.
  • fp8 quant (Tenny 3): new fp16 open_fp8_quant=True case, which must be bitwise equal to custom_all_reduce(x + y, open_fp8_quant=True).
  • Also tested now: out aliasing inp or addend.

Perf mode: test_custom_allreduce_add.py --bench prints CUDA-graph time per call for all_reduce, add + all_reduce, and all_reduce_add (median of alternating rounds, max over ranks). It also runs on main, so the two dispatches can be compared directly.

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:

TP2 TP4 TP8
Plain LL 64 / 72 (64 / 80) 40 / 56 (40 / 80) 40 / 48 (40 / 64)
Plain LL128 – / 96 (– / –) – / – – / –
Fused LL 80 / 80 (96 / 96) 64 / 96 (80 / 112) 64 / 96 (64 / 112)
Fused LL128 208 / 384 (384 / 768) – / 128 (– / 160) – / –
One-stage, below never / never never / 104 (never / 96) never / 256

I reran --bench on main and on this branch (registered inputs, bf16/fp16/fp32, TP2/4/8, 32 sizes from 16 KB to 16 MB) and updated the results in the description:

  • Plain all_reduce: of the 142 points where the kernel changes, 133 are faster and 9 the same.
  • all_reduce_add up to the fused caps: all 78 points are faster than today's separate add (median −1.6 to −2.2 µs). Against a separate add with the new dispatch, 75 are faster and 3 the same.
  • Above the caps (staged): within noise of a separate add, except 5 of 210 points at 1–4 MB + 16 B, which are up to +0.7 µs (≤1.7%) slower; which points varies between runs. That cost is in the hand-off from the ar_add_inputs staging kernel to the all-reduce. A write-mode two-stage kernel that does the add itself was 0.5–1.7 µs slower, so I kept the staging.

Validation (MI355X): test_custom_allreduce_add.py and test_custom_allreduce.py --regression pass at TP2/4/8 for bf16/fp16/fp32.

@TennyWang1223 TennyWang1223 added the ci:multi-gpu Trigger multi-GPU op tests on PR label Oct 29, 2026
@TennyWang1223

Copy link
Copy Markdown
Contributor

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.
@cpersson-amd

Copy link
Copy Markdown
Contributor Author

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 all_reduce_add would have silently dropped the addend once lamport was on (the default on gfx950). Fixed in the merge commit:

  • Addend + lamport: lamport now reduces the staged sum, unless the fused LL / LL128 add is faster. A fine MI355X sweep of fused vs torch.add + lamport put the crossover at 24 KB on TP8 and 48 KB on TP4; on TP2 fused wins up to its existing caps. Off gfx950 (unmeasured) it's always staged + lamport, i.e. what a separate add gets today.
  • uses_1stage_ar(): asks the lamport dispatch first; lamport one-shot counts as one-shot, two-shot doesn't. Main's version also missed this.
  • Tests: test_custom_allreduce_add.py now covers the 24 / 48 KB cutovers and the lamport one-shot / two-shot boundaries (59 sizes).

--bench on MI355X against current main, lamport on in both:

  • Plain: 80 of 87 changed points faster, 7 same; lamport sizes unchanged.
  • Fused: 52 of 62 points faster than torch.add + all-reduce, 10 same.
  • Staged: 221 of 226 points same or faster; 5 are slower by 0.2–0.6 µs (≤2.5%), three of them at TP8 768 KB–1 MB, where add_inputs is ~0.25 µs slower than torch.add (was already the case before this merge).

Add, regression, residual, rank-order and lamport tests pass at TP2/4/8 (add also with lamport off). The one failure is TP6 fp16 open_fp8_quant, which fails the same way on main.

TennyWang1223
TennyWang1223 previously approved these changes Nov 10, 2026

@TennyWang1223 TennyWang1223 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cpersson-amd

Copy link
Copy Markdown
Contributor Author

@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.

valarLip
valarLip previously approved these changes Nov 20, 2026
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.
@cpersson-amd
cpersson-amd dismissed stale reviews from valarLip and TennyWang1223 via 0101f2e November 23, 2026 07:33
@cpersson-amd

Copy link
Copy Markdown
Contributor Author

The last multi-GPU run failed in test_custom_allreduce_add.py's interleaved graph (TP8 bf16, numels[i]=131080, fused=1). It's a real race, fixed in bd02605 and merged with main again.

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 start_sync covers that), but the LL kernels write into peers' scratch without one. So an LL call right after a 2-stage call can overwrite a peer's reduce-scatter partial while other ranks still read it in stage 2. The failing graph runs fused 256 KB + 16 B (naive 2-stage) directly followed by fused 88 KB + 16 B (LL); it's timing-dependent, which is why it passed before.

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 (AITER_CUSTOM_AR_DISABLE_LAMPORT=1); with lamport on, lamport takes every size plain LL / LL128 would serve after a 2-stage call.

Fix: the meta buffer is now [Signal][LL / LL128 scratch][tmp]. get_tmp_buf / local_tmp_reduced_ptr skip kLLScratchBytes and meta_size() includes it, so the Python allocation and the init API are unchanged. kLL128PocCapBytes drops from 4 MB to 1 MB (the dispatch sends at most 896 KB, gfx942 TP2), keeping the new region at 21 MiB per GPU. No kernel or dispatch changes.

Validation (MI355X):

  • Stress test, 2-stage → LL pairs in one graph with GEMM load on the other ranks:

    Pair Before After
    TP8 fused (the CI case) 81 / 51,200 wrong 0
    TP8 plain, lamport off 213 / 51,200 wrong 0
    TP4 fused 1 / 25,600 wrong 0
  • LL128 forced for every size up to the new 1 MB cap at TP2/4/8: exact, and 1 MB + 16 B is rejected.

  • --bench before / after: geomean 0.997–1.003x at TP2/4/8.

  • Add (TP2/4/8), capture registration, test_custom_allreduce.py --regression (TP4/8), fused interleave, reduce-scatter and allgather pass.

@TennyWang1223
TennyWang1223 merged commit 6d51ef8 into ROCm:main Nov 24, 2026
64 checks passed
TennyWang1223 pushed a commit that referenced this pull request Nov 24, 2026
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]>
zejunchen-zejun added a commit that referenced this pull request Nov 24, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:multi-gpu Trigger multi-GPU op tests on PR HIP multigpu

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants