Repository navigation
Fuse the final reduction in matmul_reduce_scatter into one Triton kernel - #191803
anijain2305 wants to merge 17 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/191803
Note: Links to docs will display an error until the docs builds have been completed. ❌ 2 New Failures, 1 Unrelated FailureAs of commit 1bab0b7 with merge base 80a802e ( NEW FAILURES - The following jobs have failed:
BROKEN TRUNK - The following job failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Update the 2D `scatter_dim=0` `fused_matmul_reduce_scatter` path to use a ring schedule that accumulates partial sums through `torch.addmm` instead of materializing all partial outputs and reducing them at the end.
The existing implementation computes per-destination GEMM partials, exchanges them with the all-to-all pipeline, then performs the reduction with a final `torch.sum`/`torch.mean` over `stacked_partials`. That final reduction is a separate memory-bandwidth-bound kernel after the communication and GEMM work has already completed, so its memory traffic is exposed on the critical path.
We also tried splitting the final reduction into smaller add operations and overlapping them with the existing pipeline, but that still left exposed memory bandwidth from separate reduction reads/writes.
This change rotates each partial output shard around the ring. For the first iteration a rank writes its local partial with `mm.out`; for later iterations it waits for the left peer's partial and passes that remote partial as the C operand to `torch.addmm`, writing the next partial or the final output. This folds the reduction into GEMM's C operand instead of launching a final `torch.sum` reduction over `stacked_partials`.
The performance-critical form depends on the preceding addmm change so `addmm` can pass distinct C and D pointers to cuBLASLt without first copying C into D. Without that addmm change, a remote symmetric-memory C operand can trigger an extra D2D copy before GEMM, which defeats much of the ring schedule benefit.
Scope is intentionally narrow for now: unscaled 2D inputs with `scatter_dim=0` and `reduce_op in {"sum", "avg"}`. Other shapes and scaled matmul continue through the existing implementation.
Test plan:
- `git diff --check`
- `python -m py_compile torch/distributed/_symmetric_memory/__init__.py test/distributed/test_symmetric_memory.py`
- Standalone 2-rank NCCL check comparing `fused_matmul_reduce_scatter` against `_fused_matmul_reduce_scatter_fallback` for 2D `scatter_dim=0`, `sum` and `avg`
Co-authored-by: Codex <[email protected]>
ghstack-source-id: b00c5f4
Pull-Request: #191803
Rework the reduction in `fused_matmul_reduce_scatter` so it is no longer an
exposed memory-bandwidth-bound kernel at the end of the op.
The existing implementation computes per-destination GEMM partials, exchanges
them with the all-to-all pipeline, then reduces with a final `torch.sum` /
`torch.mean` over `stacked_partials`. That reduction is a separate kernel that
only starts after the communication and GEMM work has completed, so all of its
memory traffic sits on the critical path. Splitting it into smaller adds
overlapped with the pipeline was also tried, and still left the reduction
reads/writes exposed.
Two paths replace it, covering different parts of the dtype space:
1. Ring addmm schedule, used for 2D `scatter_dim=0` with
`reduce_op in {"sum", "avg"}` when the output dtype is not fp16/bf16, or
when `group_size == 2` and `reduce_op == "sum"`. Each partial output shard
rotates around the ring: the first iteration writes the local partial with
`mm.out`, and later iterations wait on the left peer's partial and pass it
as the C operand of `torch.addmm`. The reduction is folded into GEMM's C
operand, so there is no final reduction kernel at all.
2. A fused reducer for the remaining low-precision cases, which keeps the
existing all-to-all pipeline but replaces the final `torch.sum` with a
single Triton kernel that reads the bf16/fp16 partials, accumulates in fp32
registers, and stores the low-precision output once.
The reason low precision does not simply use the ring everywhere is that the
ring transports a running partial sum, which would be rounded back to bf16 at
every hop, so the accumulation error would grow with the TP degree. At
`group_size == 2` the transported value is a single partial rather than a
running sum, and `addmm` accumulates `A @ B` in fp32 before adding the peer's
bf16 partial, so there is exactly one rounding. That keeps the intended
contract in both paths: bf16 on the wire, fp32 accumulation, one rounding on
store.
The ring form depends on the preceding addmm change so that `addmm` can pass
distinct C and D pointers to cuBLASLt without first copying C into D. Without
it, a remote symmetric-memory C operand triggers an extra D2D copy before the
GEMM that defeats most of the benefit.
Scope stays narrow: unscaled 2D inputs with `scatter_dim=0` and
`reduce_op in {"sum", "avg"}`. Other shapes and scaled matmul keep the existing
implementation.
Performance
-----------
4x GB300, bf16, `M = 16384`, `A` is `[M, K_per_rank]` and `B` is
`[K_per_rank, N]`. Numbers are per-rank GEMM throughput
(`2 * M * N * K_per_rank / time`) under CUDA graph replay, median of 20
iterations, slowest rank. "before" is the previous schedule reproduced with the
same helpers, so both variants run against the same binary.
TP=2 (ring path):
| N | K_per_rank | before | after | speedup |
| --- | --- | --- | --- | --- |
| 7168 | 3584 | 1121.2 | 1719.5 | 1.53 |
| 4096 | 2048 | 787.2 | 1304.9 | 1.66 |
| 7168 | 8192 | 1511.9 | 1728.3 | 1.14 |
| 7168 | 9216 | 1538.2 | 1745.8 | 1.13 |
| 4096 | 9216 | 1548.4 | 1662.4 | 1.07 |
| 1536 | 24576 | 1688.3 | 1688.3 | 1.00 |
| 512 | 32768 | 1518.5 | 1561.4 | 1.03 |
TP=4 (fused reducer path):
| N | K_per_rank | before | after | speedup |
| --- | --- | --- | --- | --- |
| 7168 | 1792 | 902.3 | 1002.9 | 1.11 |
| 4096 | 1024 | 514.5 | 584.5 | 1.14 |
| 7168 | 4096 | 1370.8 | 1479.2 | 1.08 |
| 7168 | 9216 | 1664.2 | 1698.4 | 1.02 |
| 4096 | 9216 | 1581.5 | 1640.2 | 1.04 |
| 1536 | 24576 | 1690.6 | 1708.9 | 1.01 |
| 512 | 32768 | 1387.7 | 1418.4 | 1.02 |
The gains are in CUDA graph replay, which is how this op is used in practice.
In eager mode the result is mixed: TP=2 still improves on the wider shapes
(971.9 -> 1294.7 TFLOP/s for `N=7168, K_per_rank=3584`), but several TP=4 rows
regress (1033.3 -> 863.4 for `N=512, K_per_rank=32768`) because the extra
kernel launches are exposed without graph capture.
Accuracy did not regress. Relative L2 error against an fp32 reduce-scatter
reference is 2.45e-3 -> 2.03e-3 at TP=2 (the ring keeps the local product
unrounded before adding the peer partial) and 2.37e-3 -> 2.37e-3 at TP=4, where
the reduction math is unchanged.
Test plan:
- `lintrunner -a -- torch/distributed/_symmetric_memory/__init__.py test/distributed/test_symmetric_memory.py`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter_bfloat16_custom_reduce`
- `python test/test_reductions.py -k cuda`
- Before/after benchmark above via `torchrun --nproc_per_node={2,4}`, which also
checks both schedules against an fp32 reduce-scatter reference
This PR was authored with the help of an AI assistant.
Co-authored-by: Codex <[email protected]>
Co-authored-by: Claude <[email protected]>
ghstack-source-id: b299971
Pull-Request: #191803
Rework the reduction in `fused_matmul_reduce_scatter` so it is no longer an
exposed memory-bandwidth-bound kernel at the end of the op.
The existing implementation computes per-destination GEMM partials, exchanges
them with the all-to-all pipeline, then reduces with a final `torch.sum` /
`torch.mean` over `stacked_partials`. That reduction is a separate kernel that
only starts after the communication and GEMM work has completed, so all of its
memory traffic sits on the critical path. Splitting it into smaller adds
overlapped with the pipeline was also tried, and still left the reduction
reads/writes exposed.
Two paths replace it, covering different parts of the dtype space:
1. Ring addmm schedule, used for 2D `scatter_dim=0` with
`reduce_op in {"sum", "avg"}` when the output dtype is not fp16/bf16, or
when `group_size == 2` and `reduce_op == "sum"`. Each partial output shard
rotates around the ring: the first iteration writes the local partial with
`mm.out`, and later iterations wait on the left peer's partial and pass it
as the C operand of `torch.addmm`. The reduction is folded into GEMM's C
operand, so there is no final reduction kernel at all.
2. A fused reducer for the remaining low-precision cases, which keeps the
existing all-to-all pipeline but replaces the final `torch.sum` with a
single Triton kernel that reads the bf16/fp16 partials, accumulates in fp32
registers, and stores the low-precision output once.
The reason low precision does not simply use the ring everywhere is that the
ring transports a running partial sum, which would be rounded back to bf16 at
every hop, so the accumulation error would grow with the TP degree. At
`group_size == 2` the transported value is a single partial rather than a
running sum, and `addmm` accumulates `A @ B` in fp32 before adding the peer's
bf16 partial, so there is exactly one rounding. That keeps the intended
contract in both paths: bf16 on the wire, fp32 accumulation, one rounding on
store.
The ring form depends on the preceding addmm change so that `addmm` can pass
distinct C and D pointers to cuBLASLt without first copying C into D. Without
it, a remote symmetric-memory C operand triggers an extra D2D copy before the
GEMM that defeats most of the benefit.
Scope stays narrow: unscaled 2D inputs with `scatter_dim=0` and
`reduce_op in {"sum", "avg"}`. Other shapes and scaled matmul keep the existing
implementation.
Performance
-----------
4x GB300, bf16, `M = 16384`, `A` is `[M, K_per_rank]` and `B` is
`[K_per_rank, N]`. Numbers are per-rank GEMM throughput
(`2 * M * N * K_per_rank / time`) under CUDA graph replay, median of 20
iterations, slowest rank. "before" is the previous schedule reproduced with the
same helpers, so both variants run against the same binary.
TP=2 (ring path):
| N | K_per_rank | before | after | speedup |
| --- | --- | --- | --- | --- |
| 7168 | 3584 | 1121.2 | 1719.5 | 1.53 |
| 4096 | 2048 | 787.2 | 1304.9 | 1.66 |
| 7168 | 8192 | 1511.9 | 1728.3 | 1.14 |
| 7168 | 9216 | 1538.2 | 1745.8 | 1.13 |
| 4096 | 9216 | 1548.4 | 1662.4 | 1.07 |
| 1536 | 24576 | 1688.3 | 1688.3 | 1.00 |
| 512 | 32768 | 1518.5 | 1561.4 | 1.03 |
TP=4 (fused reducer path):
| N | K_per_rank | before | after | speedup |
| --- | --- | --- | --- | --- |
| 7168 | 1792 | 902.3 | 1002.9 | 1.11 |
| 4096 | 1024 | 514.5 | 584.5 | 1.14 |
| 7168 | 4096 | 1370.8 | 1479.2 | 1.08 |
| 7168 | 9216 | 1664.2 | 1698.4 | 1.02 |
| 4096 | 9216 | 1581.5 | 1640.2 | 1.04 |
| 1536 | 24576 | 1690.6 | 1708.9 | 1.01 |
| 512 | 32768 | 1387.7 | 1418.4 | 1.02 |
The gains are in CUDA graph replay, which is how this op is used in practice.
In eager mode the result is mixed: TP=2 still improves on the wider shapes
(971.9 -> 1294.7 TFLOP/s for `N=7168, K_per_rank=3584`), but several TP=4 rows
regress (1033.3 -> 863.4 for `N=512, K_per_rank=32768`) because the extra
kernel launches are exposed without graph capture.
Accuracy did not regress. Relative L2 error against an fp32 reduce-scatter
reference is 2.45e-3 -> 2.03e-3 at TP=2 (the ring keeps the local product
unrounded before adding the peer partial) and 2.37e-3 -> 2.37e-3 at TP=4, where
the reduction math is unchanged.
Test plan:
- `lintrunner -a -- torch/distributed/_symmetric_memory/__init__.py test/distributed/test_symmetric_memory.py`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter_bfloat16_custom_reduce`
- `python test/test_reductions.py -k cuda`
- Before/after benchmark above via `torchrun --nproc_per_node={2,4}`, which also
checks both schedules against an fp32 reduce-scatter reference
This PR was authored with the help of an AI assistant.
Co-authored-by: Codex <[email protected]>
Co-authored-by: Claude <[email protected]>
ghstack-source-id: b299971
Pull-Request: #191803
Rework the reduction in `fused_matmul_reduce_scatter` so it is no longer an
exposed memory-bandwidth-bound kernel at the end of the op.
The existing implementation computes per-destination GEMM partials, exchanges
them with the all-to-all pipeline, then reduces with a final `torch.sum` /
`torch.mean` over `stacked_partials`. That reduction is a separate kernel that
only starts after the communication and GEMM work has completed, so all of its
memory traffic sits on the critical path. Splitting it into smaller adds
overlapped with the pipeline was also tried, and still left the reduction
reads/writes exposed.
Two paths replace it, covering different parts of the dtype space:
1. Ring addmm schedule, used for 2D `scatter_dim=0` with
`reduce_op in {"sum", "avg"}` when the output dtype is not fp16/bf16, or
when `group_size == 2` and `reduce_op == "sum"`. Each partial output shard
rotates around the ring: the first iteration writes the local partial with
`mm.out`, and later iterations wait on the left peer's partial and pass it
as the C operand of `torch.addmm`. The reduction is folded into GEMM's C
operand, so there is no final reduction kernel at all.
2. A fused reducer for the remaining low-precision cases, which keeps the
existing all-to-all pipeline but replaces the final `torch.sum` with a
single Triton kernel that reads the bf16/fp16 partials, accumulates in fp32
registers, and stores the low-precision output once.
The reason low precision does not simply use the ring everywhere is that the
ring transports a running partial sum, which would be rounded back to bf16 at
every hop, so the accumulation error would grow with the TP degree. At
`group_size == 2` the transported value is a single partial rather than a
running sum, and `addmm` accumulates `A @ B` in fp32 before adding the peer's
bf16 partial, so there is exactly one rounding. That keeps the intended
contract in both paths: bf16 on the wire, fp32 accumulation, one rounding on
store.
The ring form depends on the preceding addmm change so that `addmm` can pass
distinct C and D pointers to cuBLASLt without first copying C into D. Without
it, a remote symmetric-memory C operand triggers an extra D2D copy before the
GEMM that defeats most of the benefit.
Scope stays narrow: unscaled 2D inputs with `scatter_dim=0` and
`reduce_op in {"sum", "avg"}`. Other shapes and scaled matmul keep the existing
implementation.
Performance
-----------
4x GB300, bf16, `M = 16384`, `A` is `[M, K_per_rank]` and `B` is
`[K_per_rank, N]`. Numbers are per-rank GEMM throughput
(`2 * M * N * K_per_rank / time`) under CUDA graph replay, median of 20
iterations, slowest rank. "before" is the previous schedule reproduced with the
same helpers, so both variants run against the same binary.
TP=2 (ring path):
| N | K_per_rank | before | after | speedup |
| --- | --- | --- | --- | --- |
| 7168 | 3584 | 1121.2 | 1719.5 | 1.53 |
| 4096 | 2048 | 787.2 | 1304.9 | 1.66 |
| 7168 | 8192 | 1511.9 | 1728.3 | 1.14 |
| 7168 | 9216 | 1538.2 | 1745.8 | 1.13 |
| 4096 | 9216 | 1548.4 | 1662.4 | 1.07 |
| 1536 | 24576 | 1688.3 | 1688.3 | 1.00 |
| 512 | 32768 | 1518.5 | 1561.4 | 1.03 |
TP=4 (fused reducer path):
| N | K_per_rank | before | after | speedup |
| --- | --- | --- | --- | --- |
| 7168 | 1792 | 902.3 | 1002.9 | 1.11 |
| 4096 | 1024 | 514.5 | 584.5 | 1.14 |
| 7168 | 4096 | 1370.8 | 1479.2 | 1.08 |
| 7168 | 9216 | 1664.2 | 1698.4 | 1.02 |
| 4096 | 9216 | 1581.5 | 1640.2 | 1.04 |
| 1536 | 24576 | 1690.6 | 1708.9 | 1.01 |
| 512 | 32768 | 1387.7 | 1418.4 | 1.02 |
The gains are in CUDA graph replay, which is how this op is used in practice.
In eager mode the result is mixed: TP=2 still improves on the wider shapes
(971.9 -> 1294.7 TFLOP/s for `N=7168, K_per_rank=3584`), but several TP=4 rows
regress (1033.3 -> 863.4 for `N=512, K_per_rank=32768`) because the extra
kernel launches are exposed without graph capture.
Accuracy did not regress. Relative L2 error against an fp32 reduce-scatter
reference is 2.45e-3 -> 2.03e-3 at TP=2 (the ring keeps the local product
unrounded before adding the peer partial) and 2.37e-3 -> 2.37e-3 at TP=4, where
the reduction math is unchanged.
Test plan:
- `lintrunner -a -- torch/distributed/_symmetric_memory/__init__.py test/distributed/test_symmetric_memory.py`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter_bfloat16_custom_reduce`
- `python test/test_reductions.py -k cuda`
- Before/after benchmark above via `torchrun --nproc_per_node={2,4}`, which also
checks both schedules against an fp32 reduce-scatter reference
This PR was authored with the help of an AI assistant.
Co-authored-by: Codex <[email protected]>
Co-authored-by: Claude <[email protected]>
ghstack-source-id: b299971
Pull-Request: #191803
Rework the reduction in `fused_matmul_reduce_scatter` so it is no longer an
exposed memory-bandwidth-bound kernel at the end of the op.
The existing implementation computes per-destination GEMM partials, exchanges
them with the all-to-all pipeline, then reduces with a final `torch.sum` /
`torch.mean` over `stacked_partials`. That reduction is a separate kernel that
only starts after the communication and GEMM work has completed, so all of its
memory traffic sits on the critical path. Splitting it into smaller adds
overlapped with the pipeline was also tried, and still left the reduction
reads/writes exposed.
Two paths replace it, covering different parts of the dtype space:
1. Ring addmm schedule, used for 2D `scatter_dim=0` with
`reduce_op in {"sum", "avg"}` when the output dtype is not fp16/bf16, or
when `group_size == 2` and `reduce_op == "sum"`. Each partial output shard
rotates around the ring: the first iteration writes the local partial with
`mm.out`, and later iterations wait on the left peer's partial and pass it
as the C operand of `torch.addmm`. The reduction is folded into GEMM's C
operand, so there is no final reduction kernel at all.
2. A fused reducer for the remaining low-precision cases, which keeps the
existing all-to-all pipeline but replaces the final `torch.sum` with a
single Triton kernel that reads the bf16/fp16 partials, accumulates in fp32
registers, and stores the low-precision output once.
The reason low precision does not simply use the ring everywhere is that the
ring transports a running partial sum, which would be rounded back to bf16 at
every hop, so the accumulation error would grow with the TP degree. At
`group_size == 2` the transported value is a single partial rather than a
running sum, and `addmm` accumulates `A @ B` in fp32 before adding the peer's
bf16 partial, so there is exactly one rounding. That keeps the intended
contract in both paths: bf16 on the wire, fp32 accumulation, one rounding on
store.
The ring form depends on the preceding addmm change so that `addmm` can pass
distinct C and D pointers to cuBLASLt without first copying C into D. Without
it, a remote symmetric-memory C operand triggers an extra D2D copy before the
GEMM that defeats most of the benefit.
Scope stays narrow: unscaled 2D inputs with `scatter_dim=0` and
`reduce_op in {"sum", "avg"}`. Other shapes and scaled matmul keep the existing
implementation.
Performance
-----------
4x GB300, bf16, `M = 16384`, `A` is `[M, K_per_rank]` and `B` is
`[K_per_rank, N]`. Numbers are per-rank GEMM throughput
(`2 * M * N * K_per_rank / time`), median of 60 iterations after 25 warmup
iterations, slowest rank. "before" is the previous schedule reproduced with the
same helpers, so both variants run against the same binary.
TP=2 (ring path):
| N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 7168 | 3584 | 972.9 | 1271.1 | 1.31 | 1122.9 | 1606.9 | 1.43 |
| 4096 | 2048 | 643.5 | 760.9 | 1.18 | 788.9 | 1325.0 | 1.68 |
| 7168 | 8192 | 1396.2 | 1492.3 | 1.07 | 1457.6 | 1626.1 | 1.12 |
| 7168 | 9216 | 1410.8 | 1474.7 | 1.05 | 1451.7 | 1613.8 | 1.11 |
| 4096 | 9216 | 1354.0 | 1327.8 | 0.98 | 1388.3 | 1621.3 | 1.17 |
| 1536 | 24576 | 1444.2 | 1361.3 | 0.94 | 1565.6 | 1665.4 | 1.06 |
| 512 | 32768 | 1165.1 | 1047.9 | 0.90 | 1515.2 | 1519.3 | 1.00 |
TP=4 (fused reducer path):
| N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 7168 | 1792 | 756.3 | 724.1 | 0.96 | 898.2 | 1003.0 | 1.12 |
| 4096 | 1024 | 294.8 | 240.9 | 0.82 | 516.0 | 583.5 | 1.13 |
| 7168 | 4096 | 1188.1 | 1179.6 | 0.99 | 1337.9 | 1465.3 | 1.10 |
| 7168 | 9216 | 1500.3 | 1499.7 | 1.00 | 1545.8 | 1600.7 | 1.04 |
| 4096 | 9216 | 1368.7 | 1300.6 | 0.95 | 1359.8 | 1575.2 | 1.16 |
| 1536 | 24576 | 1447.9 | 1308.4 | 0.90 | 1613.1 | 1663.8 | 1.03 |
| 512 | 32768 | 1059.2 | 938.2 | 0.89 | 1385.2 | 1414.4 | 1.02 |
Under CUDA graph replay, which is how this op is used in practice, every shape
improves or holds: 1.00x to 1.68x at TP=2 and 1.02x to 1.16x at TP=4.
Eager mode is a different story and is worth calling out. TP=2 gains on the
wider shapes (up to 1.31x) but loses on the three narrowest ones, and TP=4 is
neutral-to-worse everywhere, down to 0.82x. The cause is launch overhead that
graph capture removes: the ring issues more, smaller kernels than a single
final reduction, and the fused reducer is a Triton kernel whose dispatch cost
is exposed in eager. On the TP=4 rows the reducer's bandwidth win does not pay
for that overhead until the op is captured.
Accuracy did not regress. Relative L2 error against an fp32 reduce-scatter
reference is 2.45e-3 -> 2.03e-3 at TP=2 (the ring keeps the local product
unrounded before adding the peer partial) and 2.36e-3 -> 2.36e-3 at TP=4, where
the reduction math is unchanged.
Test plan:
- `lintrunner -a -- torch/distributed/_symmetric_memory/__init__.py test/distributed/test_symmetric_memory.py`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter_bfloat16_custom_reduce`
- `python test/test_reductions.py -k cuda`
- Before/after benchmark above via `torchrun --nproc_per_node={2,4}`, which also
checks both schedules against an fp32 reduce-scatter reference
This PR was authored with the help of an AI assistant.
Co-authored-by: Codex <[email protected]>
Co-authored-by: Claude <[email protected]>
ghstack-source-id: b299971
Pull-Request: #191803
Rework the reduction in `fused_matmul_reduce_scatter` so it is no longer an
exposed memory-bandwidth-bound kernel at the end of the op.
The existing implementation computes per-destination GEMM partials, exchanges
them with the all-to-all pipeline, then reduces with a final `torch.sum` /
`torch.mean` over `stacked_partials`. That reduction is a separate kernel that
only starts after the communication and GEMM work has completed, so all of its
memory traffic sits on the critical path. Splitting it into smaller adds
overlapped with the pipeline was also tried, and still left the reduction
reads/writes exposed.
Two paths replace it, covering different parts of the dtype space:
1. Ring addmm schedule, used for 2D `scatter_dim=0` with
`reduce_op in {"sum", "avg"}` when the output dtype is not fp16/bf16, or
when `group_size == 2` and `reduce_op == "sum"`. Each partial output shard
rotates around the ring: the first iteration writes the local partial with
`mm.out`, and later iterations wait on the left peer's partial and pass it
as the C operand of `torch.addmm`. The reduction is folded into GEMM's C
operand, so there is no final reduction kernel at all.
2. A fused reducer for the remaining low-precision cases, which keeps the
existing all-to-all pipeline but replaces the final `torch.sum` with a
single Triton kernel that reads the bf16/fp16 partials, accumulates in fp32
registers, and stores the low-precision output once.
The reason low precision does not simply use the ring everywhere is that the
ring transports a running partial sum, which would be rounded back to bf16 at
every hop, so the accumulation error would grow with the TP degree. At
`group_size == 2` the transported value is a single partial rather than a
running sum, and `addmm` accumulates `A @ B` in fp32 before adding the peer's
bf16 partial, so there is exactly one rounding. That keeps the intended
contract in both paths: bf16 on the wire, fp32 accumulation, one rounding on
store.
The ring form depends on the preceding addmm change so that `addmm` can pass
distinct C and D pointers to cuBLASLt without first copying C into D. Without
it, a remote symmetric-memory C operand triggers an extra D2D copy before the
GEMM that defeats most of the benefit.
Scope stays narrow: unscaled 2D inputs with `scatter_dim=0` and
`reduce_op in {"sum", "avg"}`. Other shapes and scaled matmul keep the existing
implementation.
Performance
-----------
4x GB300, bf16, `M = 16384`, `A` is `[M, K_per_rank]` and `B` is
`[K_per_rank, N]`. Numbers are per-rank GEMM throughput
(`2 * M * N * K_per_rank / time`), median of 60 iterations after 25 warmup
iterations, slowest rank. "before" is the previous schedule reproduced with the
same helpers, so both variants run against the same binary.
TP=2 (ring path):
| N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 7168 | 3584 | 972.9 | 1271.1 | 1.31 | 1122.9 | 1606.9 | 1.43 |
| 4096 | 2048 | 643.5 | 760.9 | 1.18 | 788.9 | 1325.0 | 1.68 |
| 7168 | 8192 | 1396.2 | 1492.3 | 1.07 | 1457.6 | 1626.1 | 1.12 |
| 7168 | 9216 | 1410.8 | 1474.7 | 1.05 | 1451.7 | 1613.8 | 1.11 |
| 4096 | 9216 | 1354.0 | 1327.8 | 0.98 | 1388.3 | 1621.3 | 1.17 |
| 1536 | 24576 | 1444.2 | 1361.3 | 0.94 | 1565.6 | 1665.4 | 1.06 |
| 512 | 32768 | 1165.1 | 1047.9 | 0.90 | 1515.2 | 1519.3 | 1.00 |
TP=4 (fused reducer path):
| N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 7168 | 1792 | 756.3 | 724.1 | 0.96 | 898.2 | 1003.0 | 1.12 |
| 4096 | 1024 | 294.8 | 240.9 | 0.82 | 516.0 | 583.5 | 1.13 |
| 7168 | 4096 | 1188.1 | 1179.6 | 0.99 | 1337.9 | 1465.3 | 1.10 |
| 7168 | 9216 | 1500.3 | 1499.7 | 1.00 | 1545.8 | 1600.7 | 1.04 |
| 4096 | 9216 | 1368.7 | 1300.6 | 0.95 | 1359.8 | 1575.2 | 1.16 |
| 1536 | 24576 | 1447.9 | 1308.4 | 0.90 | 1613.1 | 1663.8 | 1.03 |
| 512 | 32768 | 1059.2 | 938.2 | 0.89 | 1385.2 | 1414.4 | 1.02 |
Under CUDA graph replay, which is how this op is used in practice, every shape
improves or holds: 1.00x to 1.68x at TP=2 and 1.02x to 1.16x at TP=4.
Eager mode is a different story and is worth calling out. TP=2 gains on the
wider shapes (up to 1.31x) but loses on the three narrowest ones, and TP=4 is
neutral-to-worse everywhere, down to 0.82x.
This is not caused by issuing more kernels. Counted per call, the ring issues
fewer kernels than the previous schedule (6 vs 9 at TP=2), because writing
partials straight into symmetric memory and consuming the peer buffer as the C
operand drops both the peer copy and the final reduction kernel. At TP=4 the
launch count is identical (17 vs 17); only the reduction kernel differs.
Two measured effects contribute. The ring serializes its GEMMs behind the peer
signal, whereas the previous schedule could overlap per-destination GEMMs
across two streams, which costs more when each GEMM is small. And the Triton
reducer costs about 10 us more host-side launch time than `torch.sum`
(21.4 us vs 11.4 us), which CUDA graph capture removes. Those do not fully
account for the eager deltas, so the remaining gap is not yet explained;
CUDA graph replay is the regime this change targets.
Accuracy did not regress. Relative L2 error against an fp32 reduce-scatter
reference is 2.45e-3 -> 2.03e-3 at TP=2 (the ring keeps the local product
unrounded before adding the peer partial) and 2.36e-3 -> 2.36e-3 at TP=4, where
the reduction math is unchanged.
Test plan:
- `lintrunner -a -- torch/distributed/_symmetric_memory/__init__.py test/distributed/test_symmetric_memory.py`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter_bfloat16_custom_reduce`
- `python test/test_reductions.py -k cuda`
- Before/after benchmark above via `torchrun --nproc_per_node={2,4}`, which also
checks both schedules against an fp32 reduce-scatter reference
This PR was authored with the help of an AI assistant.
Co-authored-by: Codex <[email protected]>
Co-authored-by: Claude <[email protected]>
ghstack-source-id: b299971
Pull-Request: #191803
Rework the reduction in `fused_matmul_reduce_scatter` so it is no longer an
exposed memory-bandwidth-bound kernel at the end of the op.
The existing implementation computes per-destination GEMM partials, exchanges
them with the all-to-all pipeline, then reduces with a final `torch.sum` /
`torch.mean` over `stacked_partials`. That reduction is a separate kernel that
only starts after the communication and GEMM work has completed, so all of its
memory traffic sits on the critical path. Splitting it into smaller adds
overlapped with the pipeline was also tried, and still left the reduction
reads/writes exposed.
Two paths replace it, covering different parts of the dtype space:
1. Ring addmm schedule, used for 2D `scatter_dim=0` with
`reduce_op in {"sum", "avg"}` when the output dtype is not fp16/bf16, or
when `group_size == 2` and `reduce_op == "sum"`. Each partial output shard
rotates around the ring: the first iteration writes the local partial with
`mm.out`, and later iterations wait on the left peer's partial and pass it
as the C operand of `torch.addmm`. The reduction is folded into GEMM's C
operand, so there is no final reduction kernel at all.
2. A fused reducer for the remaining low-precision cases, which keeps the
existing all-to-all pipeline but replaces the final `torch.sum` with a
single Triton kernel that reads the bf16/fp16 partials, accumulates in fp32
registers, and stores the low-precision output once.
The reason low precision does not simply use the ring everywhere is that the
ring transports a running partial sum, which would be rounded back to bf16 at
every hop, so the accumulation error would grow with the TP degree. At
`group_size == 2` the transported value is a single partial rather than a
running sum, and `addmm` accumulates `A @ B` in fp32 before adding the peer's
bf16 partial, so there is exactly one rounding. That keeps the intended
contract in both paths: bf16 on the wire, fp32 accumulation, one rounding on
store.
The ring form depends on the preceding addmm change so that `addmm` can pass
distinct C and D pointers to cuBLASLt without first copying C into D. Without
it, a remote symmetric-memory C operand triggers an extra D2D copy before the
GEMM that defeats most of the benefit.
Scope stays narrow: unscaled 2D inputs with `scatter_dim=0` and
`reduce_op in {"sum", "avg"}`. Other shapes and scaled matmul keep the existing
implementation.
Performance
-----------
4x GB300, bf16, `M = 16384`, `A` is `[M, K_per_rank]` and `B` is
`[K_per_rank, N]`. Numbers are per-rank GEMM throughput
(`2 * M * N * K_per_rank / time`), median of 60 iterations after 25 warmup
iterations, slowest rank. "before" is the previous schedule reproduced with the
same helpers, so both variants run against the same binary.
TP=2 (ring path):
| N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 7168 | 3584 | 972.9 | 1271.1 | 1.31 | 1122.9 | 1606.9 | 1.43 |
| 4096 | 2048 | 643.5 | 760.9 | 1.18 | 788.9 | 1325.0 | 1.68 |
| 7168 | 8192 | 1396.2 | 1492.3 | 1.07 | 1457.6 | 1626.1 | 1.12 |
| 7168 | 9216 | 1410.8 | 1474.7 | 1.05 | 1451.7 | 1613.8 | 1.11 |
| 4096 | 9216 | 1354.0 | 1327.8 | 0.98 | 1388.3 | 1621.3 | 1.17 |
| 1536 | 24576 | 1444.2 | 1361.3 | 0.94 | 1565.6 | 1665.4 | 1.06 |
| 512 | 32768 | 1165.1 | 1047.9 | 0.90 | 1515.2 | 1519.3 | 1.00 |
TP=4 (fused reducer path):
| N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 7168 | 1792 | 756.3 | 724.1 | 0.96 | 898.2 | 1003.0 | 1.12 |
| 4096 | 1024 | 294.8 | 240.9 | 0.82 | 516.0 | 583.5 | 1.13 |
| 7168 | 4096 | 1188.1 | 1179.6 | 0.99 | 1337.9 | 1465.3 | 1.10 |
| 7168 | 9216 | 1500.3 | 1499.7 | 1.00 | 1545.8 | 1600.7 | 1.04 |
| 4096 | 9216 | 1368.7 | 1300.6 | 0.95 | 1359.8 | 1575.2 | 1.16 |
| 1536 | 24576 | 1447.9 | 1308.4 | 0.90 | 1613.1 | 1663.8 | 1.03 |
| 512 | 32768 | 1059.2 | 938.2 | 0.89 | 1385.2 | 1414.4 | 1.02 |
Under CUDA graph replay, which is how this op is used in practice, every shape
improves or holds: 1.00x to 1.68x at TP=2 and 1.02x to 1.16x at TP=4.
Eager mode is a different story and is worth calling out. TP=2 gains on the
wider shapes (up to 1.31x) but loses on the three narrowest ones, and TP=4 is
neutral-to-worse everywhere, down to 0.82x.
This is not caused by issuing more kernels. Counted per call, the ring issues
fewer kernels than the previous schedule (6 vs 9 at TP=2), because writing
partials straight into symmetric memory and consuming the peer buffer as the C
operand drops both the peer copy and the final reduction kernel. At TP=4 the
launch count is identical (17 vs 17); only the reduction kernel differs.
Two measured effects contribute. The ring serializes its GEMMs behind the peer
signal, whereas the previous schedule could overlap per-destination GEMMs
across two streams, which costs more when each GEMM is small. And the Triton
reducer costs about 10 us more host-side launch time than `torch.sum`
(21.4 us vs 11.4 us), which CUDA graph capture removes. Those do not fully
account for the eager deltas, so the remaining gap is not yet explained;
CUDA graph replay is the regime this change targets.
Accuracy did not regress. Relative L2 error against an fp32 reduce-scatter
reference is 2.45e-3 -> 2.03e-3 at TP=2 (the ring keeps the local product
unrounded before adding the peer partial) and 2.36e-3 -> 2.36e-3 at TP=4, where
the reduction math is unchanged.
Test plan:
- `lintrunner -a -- torch/distributed/_symmetric_memory/__init__.py test/distributed/test_symmetric_memory.py`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter_bfloat16_custom_reduce`
- `python test/test_reductions.py -k cuda`
- Before/after benchmark above via `torchrun --nproc_per_node={2,4}`, which also
checks both schedules against an fp32 reduce-scatter reference
This PR was authored with the help of an AI assistant.
Co-authored-by: Codex <[email protected]>
Co-authored-by: Claude <[email protected]>
ghstack-source-id: 5f7657b
Pull-Request: #191803
Rework the reduction in `fused_matmul_reduce_scatter` so it is no longer an
exposed memory-bandwidth-bound kernel at the end of the op.
The existing implementation computes per-destination GEMM partials, exchanges
them with the all-to-all pipeline, then reduces with a final `torch.sum` /
`torch.mean` over `stacked_partials`. That reduction is a separate kernel that
only starts after the communication and GEMM work has completed, so all of its
memory traffic sits on the critical path. Splitting it into smaller adds
overlapped with the pipeline was also tried, and still left the reduction
reads/writes exposed.
Two paths replace it, covering different parts of the dtype space:
1. Ring addmm schedule, used for 2D `scatter_dim=0` with
`reduce_op in {"sum", "avg"}` when the output dtype is not fp16/bf16, or
when `group_size == 2` and `reduce_op == "sum"`. Each partial output shard
rotates around the ring: the first iteration writes the local partial with
`mm.out`, and later iterations wait on the left peer's partial and pass it
as the C operand of `torch.addmm`. The reduction is folded into GEMM's C
operand, so there is no final reduction kernel at all.
2. A fused reducer for the remaining low-precision cases, which keeps the
existing all-to-all pipeline but replaces the final `torch.sum` with a
single Triton kernel that reads the bf16/fp16 partials, accumulates in fp32
registers, and stores the low-precision output once.
The reason low precision does not simply use the ring everywhere is that the
ring transports a running partial sum, which would be rounded back to bf16 at
every hop, so the accumulation error would grow with the TP degree. At
`group_size == 2` the transported value is a single partial rather than a
running sum, and `addmm` accumulates `A @ B` in fp32 before adding the peer's
bf16 partial, so there is exactly one rounding. That keeps the intended
contract in both paths: bf16 on the wire, fp32 accumulation, one rounding on
store.
The ring form depends on the preceding addmm change so that `addmm` can pass
distinct C and D pointers to cuBLASLt without first copying C into D. Without
it, a remote symmetric-memory C operand triggers an extra D2D copy before the
GEMM that defeats most of the benefit.
Scope stays narrow: unscaled 2D inputs with `scatter_dim=0` and
`reduce_op in {"sum", "avg"}`. Other shapes and scaled matmul keep the existing
implementation.
Performance
-----------
4x GB300, bf16, `M = 16384`, `A` is `[M, K_per_rank]` and `B` is
`[K_per_rank, N]`. Numbers are per-rank GEMM throughput
(`2 * M * N * K_per_rank / time`), median of 60 iterations after 25 warmup
iterations, slowest rank. "before" is the previous schedule reproduced with the
same helpers, so both variants run against the same binary.
TP=2 (ring path):
| N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 7168 | 3584 | 972.9 | 1271.1 | 1.31 | 1122.9 | 1606.9 | 1.43 |
| 4096 | 2048 | 643.5 | 760.9 | 1.18 | 788.9 | 1325.0 | 1.68 |
| 7168 | 8192 | 1396.2 | 1492.3 | 1.07 | 1457.6 | 1626.1 | 1.12 |
| 7168 | 9216 | 1410.8 | 1474.7 | 1.05 | 1451.7 | 1613.8 | 1.11 |
| 4096 | 9216 | 1354.0 | 1327.8 | 0.98 | 1388.3 | 1621.3 | 1.17 |
| 1536 | 24576 | 1444.2 | 1361.3 | 0.94 | 1565.6 | 1665.4 | 1.06 |
| 512 | 32768 | 1165.1 | 1047.9 | 0.90 | 1515.2 | 1519.3 | 1.00 |
TP=4 (fused reducer path):
| N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 7168 | 1792 | 756.3 | 724.1 | 0.96 | 898.2 | 1003.0 | 1.12 |
| 4096 | 1024 | 294.8 | 240.9 | 0.82 | 516.0 | 583.5 | 1.13 |
| 7168 | 4096 | 1188.1 | 1179.6 | 0.99 | 1337.9 | 1465.3 | 1.10 |
| 7168 | 9216 | 1500.3 | 1499.7 | 1.00 | 1545.8 | 1600.7 | 1.04 |
| 4096 | 9216 | 1368.7 | 1300.6 | 0.95 | 1359.8 | 1575.2 | 1.16 |
| 1536 | 24576 | 1447.9 | 1308.4 | 0.90 | 1613.1 | 1663.8 | 1.03 |
| 512 | 32768 | 1059.2 | 938.2 | 0.89 | 1385.2 | 1414.4 | 1.02 |
Under CUDA graph replay, which is how this op is used in practice, every shape
improves or holds: 1.00x to 1.68x at TP=2 and 1.02x to 1.16x at TP=4.
Eager mode is a different story and is worth calling out. TP=2 gains on the
wider shapes (up to 1.31x) but loses on the three narrowest ones, and TP=4 is
neutral-to-worse everywhere, down to 0.82x.
This is not caused by issuing more kernels. Counted per call, the ring issues
fewer kernels than the previous schedule (6 vs 9 at TP=2), because writing
partials straight into symmetric memory and consuming the peer buffer as the C
operand drops both the peer copy and the final reduction kernel. At TP=4 the
launch count is identical (17 vs 17); only the reduction kernel differs.
Two measured effects contribute. The ring serializes its GEMMs behind the peer
signal, whereas the previous schedule could overlap per-destination GEMMs
across two streams, which costs more when each GEMM is small. And the Triton
reducer costs about 10 us more host-side launch time than `torch.sum`
(21.4 us vs 11.4 us), which CUDA graph capture removes. Those do not fully
account for the eager deltas, so the remaining gap is not yet explained;
CUDA graph replay is the regime this change targets.
Accuracy did not regress. Relative L2 error against an fp32 reduce-scatter
reference is 2.45e-3 -> 2.03e-3 at TP=2 (the ring keeps the local product
unrounded before adding the peer partial) and 2.36e-3 -> 2.36e-3 at TP=4, where
the reduction math is unchanged.
Test plan:
- `lintrunner -a -- torch/distributed/_symmetric_memory/__init__.py test/distributed/test_symmetric_memory.py`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter_bfloat16_custom_reduce`
- `python test/test_reductions.py -k cuda`
- Before/after benchmark above via `torchrun --nproc_per_node={2,4}`, which also
checks both schedules against an fp32 reduce-scatter reference
This PR was authored with the help of an AI assistant.
Co-authored-by: Codex <[email protected]>
Co-authored-by: Claude <[email protected]>
ghstack-source-id: 5f7657b
Pull-Request: #191803
Rework the reduction in `fused_matmul_reduce_scatter` so it is no longer an
exposed memory-bandwidth-bound kernel at the end of the op.
The existing implementation computes per-destination GEMM partials, exchanges
them with the all-to-all pipeline, then reduces with a final `torch.sum` /
`torch.mean` over `stacked_partials`. That reduction is a separate kernel that
only starts after the communication and GEMM work has completed, so all of its
memory traffic sits on the critical path. Splitting it into smaller adds
overlapped with the pipeline was also tried, and still left the reduction
reads/writes exposed.
Two paths replace it, covering different parts of the dtype space:
1. Ring addmm schedule, used for 2D `scatter_dim=0` with
`reduce_op in {"sum", "avg"}` when the output dtype is not fp16/bf16, or
when `group_size == 2` and `reduce_op == "sum"`. Each partial output shard
rotates around the ring: the first iteration writes the local partial with
`mm.out`, and later iterations wait on the left peer's partial and pass it
as the C operand of `torch.addmm`. The reduction is folded into GEMM's C
operand, so there is no final reduction kernel at all.
2. A fused reducer for the remaining low-precision cases, which keeps the
existing all-to-all pipeline but replaces the final `torch.sum` with a
single Triton kernel that reads the bf16/fp16 partials, accumulates in fp32
registers, and stores the low-precision output once.
The reason low precision does not simply use the ring everywhere is that the
ring transports a running partial sum, which would be rounded back to bf16 at
every hop, so the accumulation error would grow with the TP degree. At
`group_size == 2` the transported value is a single partial rather than a
running sum, and `addmm` accumulates `A @ B` in fp32 before adding the peer's
bf16 partial, so there is exactly one rounding. That keeps the intended
contract in both paths: bf16 on the wire, fp32 accumulation, one rounding on
store.
The ring form depends on the preceding addmm change so that `addmm` can pass
distinct C and D pointers to cuBLASLt without first copying C into D. Without
it, a remote symmetric-memory C operand triggers an extra D2D copy before the
GEMM that defeats most of the benefit.
Scope stays narrow: unscaled 2D inputs with `scatter_dim=0` and
`reduce_op in {"sum", "avg"}`. Other shapes and scaled matmul keep the existing
implementation.
Performance
-----------
4x GB300, bf16, `M = 16384`, `A` is `[M, K_per_rank]` and `B` is
`[K_per_rank, N]`. Numbers are per-rank GEMM throughput
(`2 * M * N * K_per_rank / time`), median of 60 iterations after 25 warmup
iterations, slowest rank. "before" is the previous schedule reproduced with the
same helpers, so both variants run against the same binary.
TP=2 (ring path):
| N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 7168 | 3584 | 972.9 | 1271.1 | 1.31 | 1122.9 | 1606.9 | 1.43 |
| 4096 | 2048 | 643.5 | 760.9 | 1.18 | 788.9 | 1325.0 | 1.68 |
| 7168 | 8192 | 1396.2 | 1492.3 | 1.07 | 1457.6 | 1626.1 | 1.12 |
| 7168 | 9216 | 1410.8 | 1474.7 | 1.05 | 1451.7 | 1613.8 | 1.11 |
| 4096 | 9216 | 1354.0 | 1327.8 | 0.98 | 1388.3 | 1621.3 | 1.17 |
| 1536 | 24576 | 1444.2 | 1361.3 | 0.94 | 1565.6 | 1665.4 | 1.06 |
| 512 | 32768 | 1165.1 | 1047.9 | 0.90 | 1515.2 | 1519.3 | 1.00 |
TP=4 (fused reducer path):
| N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 7168 | 1792 | 756.3 | 724.1 | 0.96 | 898.2 | 1003.0 | 1.12 |
| 4096 | 1024 | 294.8 | 240.9 | 0.82 | 516.0 | 583.5 | 1.13 |
| 7168 | 4096 | 1188.1 | 1179.6 | 0.99 | 1337.9 | 1465.3 | 1.10 |
| 7168 | 9216 | 1500.3 | 1499.7 | 1.00 | 1545.8 | 1600.7 | 1.04 |
| 4096 | 9216 | 1368.7 | 1300.6 | 0.95 | 1359.8 | 1575.2 | 1.16 |
| 1536 | 24576 | 1447.9 | 1308.4 | 0.90 | 1613.1 | 1663.8 | 1.03 |
| 512 | 32768 | 1059.2 | 938.2 | 0.89 | 1385.2 | 1414.4 | 1.02 |
Under CUDA graph replay, which is how this op is used in practice, every shape
improves or holds: 1.00x to 1.68x at TP=2 and 1.02x to 1.16x at TP=4.
Eager mode is a different story and is worth calling out. TP=2 gains on the
wider shapes (up to 1.31x) but loses on the three narrowest ones, and TP=4 is
neutral-to-worse everywhere, down to 0.82x.
This is not caused by issuing more kernels. Counted per call, the ring issues
fewer kernels than the previous schedule (6 vs 9 at TP=2), because writing
partials straight into symmetric memory and consuming the peer buffer as the C
operand drops both the peer copy and the final reduction kernel. At TP=4 the
launch count is identical (17 vs 17); only the reduction kernel differs.
Two measured effects contribute. The ring serializes its GEMMs behind the peer
signal, whereas the previous schedule could overlap per-destination GEMMs
across two streams, which costs more when each GEMM is small. And the Triton
reducer costs about 10 us more host-side launch time than `torch.sum`
(21.4 us vs 11.4 us), which CUDA graph capture removes. Those do not fully
account for the eager deltas, so the remaining gap is not yet explained;
CUDA graph replay is the regime this change targets.
Accuracy did not regress. Relative L2 error against an fp32 reduce-scatter
reference is 2.45e-3 -> 2.03e-3 at TP=2 (the ring keeps the local product
unrounded before adding the peer partial) and 2.36e-3 -> 2.36e-3 at TP=4, where
the reduction math is unchanged.
Test plan:
- `lintrunner -a -- torch/distributed/_symmetric_memory/__init__.py test/distributed/test_symmetric_memory.py`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter_bfloat16_custom_reduce`
- `python test/test_reductions.py -k cuda`
- Before/after benchmark above via `torchrun --nproc_per_node={2,4}`, which also
checks both schedules against an fp32 reduce-scatter reference
This PR was authored with the help of an AI assistant.
Co-authored-by: Codex <[email protected]>
Co-authored-by: Claude <[email protected]>
ghstack-source-id: 376f1c9
Pull-Request: #191803
Rework the reduction in `fused_matmul_reduce_scatter` so it is no longer an
exposed memory-bandwidth-bound kernel at the end of the op.
The existing implementation computes per-destination GEMM partials, exchanges
them with the all-to-all pipeline, then reduces with a final `torch.sum` /
`torch.mean` over `stacked_partials`. That reduction is a separate kernel that
only starts after the communication and GEMM work has completed, so all of its
memory traffic sits on the critical path. Splitting it into smaller adds
overlapped with the pipeline was also tried, and still left the reduction
reads/writes exposed.
Two paths replace it, covering different parts of the dtype space:
1. Ring addmm schedule, used for 2D `scatter_dim=0` with
`reduce_op in {"sum", "avg"}` when the output dtype is not fp16/bf16, or
when `group_size == 2` and `reduce_op == "sum"`. Each partial output shard
rotates around the ring: the first iteration writes the local partial with
`mm.out`, and later iterations wait on the left peer's partial and pass it
as the C operand of `torch.addmm`. The reduction is folded into GEMM's C
operand, so there is no final reduction kernel at all.
2. A fused reducer for the remaining low-precision cases, which keeps the
existing all-to-all pipeline but replaces the final `torch.sum` with a
single Triton kernel that reads the bf16/fp16 partials, accumulates in fp32
registers, and stores the low-precision output once.
The reason low precision does not simply use the ring everywhere is that the
ring transports a running partial sum, which would be rounded back to bf16 at
every hop, so the accumulation error would grow with the TP degree. At
`group_size == 2` the transported value is a single partial rather than a
running sum, and `addmm` accumulates `A @ B` in fp32 before adding the peer's
bf16 partial, so there is exactly one rounding. That keeps the intended
contract in both paths: bf16 on the wire, fp32 accumulation, one rounding on
store.
The ring form depends on the preceding addmm change so that `addmm` can pass
distinct C and D pointers to cuBLASLt without first copying C into D. Without
it, a remote symmetric-memory C operand triggers an extra D2D copy before the
GEMM that defeats most of the benefit.
Scope stays narrow: unscaled 2D inputs with `scatter_dim=0` and
`reduce_op in {"sum", "avg"}`. Other shapes and scaled matmul keep the existing
implementation.
Performance
-----------
4x GB300, bf16, `M = 16384`, `A` is `[M, K_per_rank]` and `B` is
`[K_per_rank, N]`. Numbers are per-rank GEMM throughput
(`2 * M * N * K_per_rank / time`), median of 60 iterations after 25 warmup
iterations, slowest rank. "before" is the previous schedule reproduced with the
same helpers, so both variants run against the same binary.
TP=2 (ring path):
| N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 7168 | 3584 | 972.9 | 1271.1 | 1.31 | 1122.9 | 1606.9 | 1.43 |
| 4096 | 2048 | 643.5 | 760.9 | 1.18 | 788.9 | 1325.0 | 1.68 |
| 7168 | 8192 | 1396.2 | 1492.3 | 1.07 | 1457.6 | 1626.1 | 1.12 |
| 7168 | 9216 | 1410.8 | 1474.7 | 1.05 | 1451.7 | 1613.8 | 1.11 |
| 4096 | 9216 | 1354.0 | 1327.8 | 0.98 | 1388.3 | 1621.3 | 1.17 |
| 1536 | 24576 | 1444.2 | 1361.3 | 0.94 | 1565.6 | 1665.4 | 1.06 |
| 512 | 32768 | 1165.1 | 1047.9 | 0.90 | 1515.2 | 1519.3 | 1.00 |
TP=4 (fused reducer path):
| N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 7168 | 1792 | 756.3 | 724.1 | 0.96 | 898.2 | 1003.0 | 1.12 |
| 4096 | 1024 | 294.8 | 240.9 | 0.82 | 516.0 | 583.5 | 1.13 |
| 7168 | 4096 | 1188.1 | 1179.6 | 0.99 | 1337.9 | 1465.3 | 1.10 |
| 7168 | 9216 | 1500.3 | 1499.7 | 1.00 | 1545.8 | 1600.7 | 1.04 |
| 4096 | 9216 | 1368.7 | 1300.6 | 0.95 | 1359.8 | 1575.2 | 1.16 |
| 1536 | 24576 | 1447.9 | 1308.4 | 0.90 | 1613.1 | 1663.8 | 1.03 |
| 512 | 32768 | 1059.2 | 938.2 | 0.89 | 1385.2 | 1414.4 | 1.02 |
Under CUDA graph replay, which is how this op is used in practice, every shape
improves or holds: 1.00x to 1.68x at TP=2 and 1.02x to 1.16x at TP=4.
Eager mode is a different story and is worth calling out. TP=2 gains on the
wider shapes (up to 1.31x) but loses on the three narrowest ones, and TP=4 is
neutral-to-worse everywhere, down to 0.82x.
This is not caused by issuing more kernels. Counted per call, the ring issues
fewer kernels than the previous schedule (6 vs 9 at TP=2), because writing
partials straight into symmetric memory and consuming the peer buffer as the C
operand drops both the peer copy and the final reduction kernel. At TP=4 the
launch count is identical (17 vs 17); only the reduction kernel differs.
Two measured effects contribute. The ring serializes its GEMMs behind the peer
signal, whereas the previous schedule could overlap per-destination GEMMs
across two streams, which costs more when each GEMM is small. And the Triton
reducer costs about 10 us more host-side launch time than `torch.sum`
(21.4 us vs 11.4 us), which CUDA graph capture removes. Those do not fully
account for the eager deltas, so the remaining gap is not yet explained;
CUDA graph replay is the regime this change targets.
Accuracy did not regress. Relative L2 error against an fp32 reduce-scatter
reference is 2.45e-3 -> 2.03e-3 at TP=2 (the ring keeps the local product
unrounded before adding the peer partial) and 2.36e-3 -> 2.36e-3 at TP=4, where
the reduction math is unchanged.
Test plan:
- `lintrunner -a -- torch/distributed/_symmetric_memory/__init__.py test/distributed/test_symmetric_memory.py`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter_bfloat16_custom_reduce`
- `python test/test_reductions.py -k cuda`
- Before/after benchmark above via `torchrun --nproc_per_node={2,4}`, which also
checks both schedules against an fp32 reduce-scatter reference
This PR was authored with the help of an AI assistant.
Co-authored-by: Codex <[email protected]>
Co-authored-by: Claude <[email protected]>
ghstack-source-id: ffcda3a
Pull-Request: #191803
Rework the reduction in `fused_matmul_reduce_scatter` so it is no longer an
exposed memory-bandwidth-bound kernel at the end of the op.
The existing implementation computes per-destination GEMM partials, exchanges
them with the all-to-all pipeline, then reduces with a final `torch.sum` /
`torch.mean` over `stacked_partials`. That reduction is a separate kernel that
only starts after the communication and GEMM work has completed, so all of its
memory traffic sits on the critical path. Splitting it into smaller adds
overlapped with the pipeline was also tried, and still left the reduction
reads/writes exposed.
Two paths replace it, covering different parts of the dtype space:
1. Ring addmm schedule, used for 2D `scatter_dim=0` with
`reduce_op in {"sum", "avg"}` when the output dtype is not fp16/bf16, or
when `group_size == 2` and `reduce_op == "sum"`. Each partial output shard
rotates around the ring: the first iteration writes the local partial with
`mm.out`, and later iterations wait on the left peer's partial and pass it
as the C operand of `torch.addmm`. The reduction is folded into GEMM's C
operand, so there is no final reduction kernel at all.
2. A fused reducer for the remaining low-precision cases, which keeps the
existing all-to-all pipeline but replaces the final `torch.sum` with a
single Triton kernel that reads the bf16/fp16 partials, accumulates in fp32
registers, and stores the low-precision output once.
The reason low precision does not simply use the ring everywhere is that the
ring transports a running partial sum, which would be rounded back to bf16 at
every hop, so the accumulation error would grow with the TP degree. At
`group_size == 2` the transported value is a single partial rather than a
running sum, and `addmm` accumulates `A @ B` in fp32 before adding the peer's
bf16 partial, so there is exactly one rounding. That keeps the intended
contract in both paths: bf16 on the wire, fp32 accumulation, one rounding on
store.
The ring form depends on the preceding addmm change so that `addmm` can pass
distinct C and D pointers to cuBLASLt without first copying C into D. Without
it, a remote symmetric-memory C operand triggers an extra D2D copy before the
GEMM that defeats most of the benefit.
Scope stays narrow: unscaled 2D inputs with `scatter_dim=0` and
`reduce_op in {"sum", "avg"}`. Other shapes and scaled matmul keep the existing
implementation.
Performance
-----------
4x GB300, bf16, `M = 16384`, `A` is `[M, K_per_rank]` and `B` is
`[K_per_rank, N]`. Numbers are per-rank GEMM throughput
(`2 * M * N * K_per_rank / time`), median of 60 iterations after 25 warmup
iterations, slowest rank. "before" is the previous schedule reproduced with the
same helpers, so both variants run against the same binary.
TP=2 (ring path):
| N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 7168 | 3584 | 972.9 | 1271.1 | 1.31 | 1122.9 | 1606.9 | 1.43 |
| 4096 | 2048 | 643.5 | 760.9 | 1.18 | 788.9 | 1325.0 | 1.68 |
| 7168 | 8192 | 1396.2 | 1492.3 | 1.07 | 1457.6 | 1626.1 | 1.12 |
| 7168 | 9216 | 1410.8 | 1474.7 | 1.05 | 1451.7 | 1613.8 | 1.11 |
| 4096 | 9216 | 1354.0 | 1327.8 | 0.98 | 1388.3 | 1621.3 | 1.17 |
| 1536 | 24576 | 1444.2 | 1361.3 | 0.94 | 1565.6 | 1665.4 | 1.06 |
| 512 | 32768 | 1165.1 | 1047.9 | 0.90 | 1515.2 | 1519.3 | 1.00 |
TP=4 (fused reducer path):
| N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 7168 | 1792 | 756.3 | 724.1 | 0.96 | 898.2 | 1003.0 | 1.12 |
| 4096 | 1024 | 294.8 | 240.9 | 0.82 | 516.0 | 583.5 | 1.13 |
| 7168 | 4096 | 1188.1 | 1179.6 | 0.99 | 1337.9 | 1465.3 | 1.10 |
| 7168 | 9216 | 1500.3 | 1499.7 | 1.00 | 1545.8 | 1600.7 | 1.04 |
| 4096 | 9216 | 1368.7 | 1300.6 | 0.95 | 1359.8 | 1575.2 | 1.16 |
| 1536 | 24576 | 1447.9 | 1308.4 | 0.90 | 1613.1 | 1663.8 | 1.03 |
| 512 | 32768 | 1059.2 | 938.2 | 0.89 | 1385.2 | 1414.4 | 1.02 |
Under CUDA graph replay, which is how this op is used in practice, every shape
improves or holds: 1.00x to 1.68x at TP=2 and 1.02x to 1.16x at TP=4.
Eager mode is a different story and is worth calling out. TP=2 gains on the
wider shapes (up to 1.31x) but loses on the three narrowest ones, and TP=4 is
neutral-to-worse everywhere, down to 0.82x.
This is not caused by issuing more kernels. Counted per call, the ring issues
fewer kernels than the previous schedule (6 vs 9 at TP=2), because writing
partials straight into symmetric memory and consuming the peer buffer as the C
operand drops both the peer copy and the final reduction kernel. At TP=4 the
launch count is identical (17 vs 17); only the reduction kernel differs.
Two measured effects contribute. The ring serializes its GEMMs behind the peer
signal, whereas the previous schedule could overlap per-destination GEMMs
across two streams, which costs more when each GEMM is small. And the Triton
reducer costs about 10 us more host-side launch time than `torch.sum`
(21.4 us vs 11.4 us), which CUDA graph capture removes. Those do not fully
account for the eager deltas, so the remaining gap is not yet explained;
CUDA graph replay is the regime this change targets.
Accuracy did not regress. Relative L2 error against an fp32 reduce-scatter
reference is 2.45e-3 -> 2.03e-3 at TP=2 (the ring keeps the local product
unrounded before adding the peer partial) and 2.36e-3 -> 2.36e-3 at TP=4, where
the reduction math is unchanged.
Test plan:
- `lintrunner -a -- torch/distributed/_symmetric_memory/__init__.py test/distributed/test_symmetric_memory.py`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter_bfloat16_custom_reduce`
- `python test/test_reductions.py -k cuda`
- Before/after benchmark above via `torchrun --nproc_per_node={2,4}`, which also
checks both schedules against an fp32 reduce-scatter reference
This PR was authored with the help of an AI assistant.
Co-authored-by: Codex <[email protected]>
Co-authored-by: Claude <[email protected]>
ghstack-source-id: ffcda3a
Pull-Request: #191803
Rework the reduction in `fused_matmul_reduce_scatter` so it is no longer an
exposed memory-bandwidth-bound kernel at the end of the op.
The existing implementation computes per-destination GEMM partials, exchanges
them with the all-to-all pipeline, then reduces with a final `torch.sum` /
`torch.mean` over `stacked_partials`. That reduction is a separate kernel that
only starts after the communication and GEMM work has completed, so all of its
memory traffic sits on the critical path. Splitting it into smaller adds
overlapped with the pipeline was also tried, and still left the reduction
reads/writes exposed.
Two paths replace it, covering different parts of the dtype space:
1. Ring addmm schedule, used for 2D `scatter_dim=0` with
`reduce_op in {"sum", "avg"}` when the output dtype is not fp16/bf16, or
when `group_size == 2` and `reduce_op == "sum"`. Each partial output shard
rotates around the ring: the first iteration writes the local partial with
`mm.out`, and later iterations wait on the left peer's partial and pass it
as the C operand of `torch.addmm`. The reduction is folded into GEMM's C
operand, so there is no final reduction kernel at all.
2. A fused reducer for the remaining low-precision cases, which keeps the
existing all-to-all pipeline but replaces the final `torch.sum` with a
single Triton kernel that reads the bf16/fp16 partials, accumulates in fp32
registers, and stores the low-precision output once.
The reason low precision does not simply use the ring everywhere is that the
ring transports a running partial sum, which would be rounded back to bf16 at
every hop, so the accumulation error would grow with the TP degree. At
`group_size == 2` the transported value is a single partial rather than a
running sum, and `addmm` accumulates `A @ B` in fp32 before adding the peer's
bf16 partial, so there is exactly one rounding. That keeps the intended
contract in both paths: bf16 on the wire, fp32 accumulation, one rounding on
store.
The ring form depends on the preceding addmm change so that `addmm` can pass
distinct C and D pointers to cuBLASLt without first copying C into D. Without
it, a remote symmetric-memory C operand triggers an extra D2D copy before the
GEMM that defeats most of the benefit.
Scope stays narrow: unscaled 2D inputs with `scatter_dim=0` and
`reduce_op in {"sum", "avg"}`. Other shapes and scaled matmul keep the existing
implementation.
Performance
-----------
4x GB300, bf16, `M = 16384`, `A` is `[M, K_per_rank]` and `B` is
`[K_per_rank, N]`. Numbers are per-rank GEMM throughput
(`2 * M * N * K_per_rank / time`), median of 60 iterations after 25 warmup
iterations, slowest rank. "before" is the previous schedule reproduced with the
same helpers, so both variants run against the same binary.
TP=2 (ring path):
| N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 7168 | 3584 | 972.9 | 1271.1 | 1.31 | 1122.9 | 1606.9 | 1.43 |
| 4096 | 2048 | 643.5 | 760.9 | 1.18 | 788.9 | 1325.0 | 1.68 |
| 7168 | 8192 | 1396.2 | 1492.3 | 1.07 | 1457.6 | 1626.1 | 1.12 |
| 7168 | 9216 | 1410.8 | 1474.7 | 1.05 | 1451.7 | 1613.8 | 1.11 |
| 4096 | 9216 | 1354.0 | 1327.8 | 0.98 | 1388.3 | 1621.3 | 1.17 |
| 1536 | 24576 | 1444.2 | 1361.3 | 0.94 | 1565.6 | 1665.4 | 1.06 |
| 512 | 32768 | 1165.1 | 1047.9 | 0.90 | 1515.2 | 1519.3 | 1.00 |
TP=4 (fused reducer path):
| N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 7168 | 1792 | 756.3 | 724.1 | 0.96 | 898.2 | 1003.0 | 1.12 |
| 4096 | 1024 | 294.8 | 240.9 | 0.82 | 516.0 | 583.5 | 1.13 |
| 7168 | 4096 | 1188.1 | 1179.6 | 0.99 | 1337.9 | 1465.3 | 1.10 |
| 7168 | 9216 | 1500.3 | 1499.7 | 1.00 | 1545.8 | 1600.7 | 1.04 |
| 4096 | 9216 | 1368.7 | 1300.6 | 0.95 | 1359.8 | 1575.2 | 1.16 |
| 1536 | 24576 | 1447.9 | 1308.4 | 0.90 | 1613.1 | 1663.8 | 1.03 |
| 512 | 32768 | 1059.2 | 938.2 | 0.89 | 1385.2 | 1414.4 | 1.02 |
Under CUDA graph replay, which is how this op is used in practice, every shape
improves or holds: 1.00x to 1.68x at TP=2 and 1.02x to 1.16x at TP=4.
Eager mode is a different story and is worth calling out. TP=2 gains on the
wider shapes (up to 1.31x) but loses on the three narrowest ones, and TP=4 is
neutral-to-worse everywhere, down to 0.82x.
This is not caused by issuing more kernels. Counted per call, the ring issues
fewer kernels than the previous schedule (6 vs 9 at TP=2), because writing
partials straight into symmetric memory and consuming the peer buffer as the C
operand drops both the peer copy and the final reduction kernel. At TP=4 the
launch count is identical (17 vs 17); only the reduction kernel differs.
Two measured effects contribute. The ring serializes its GEMMs behind the peer
signal, whereas the previous schedule could overlap per-destination GEMMs
across two streams, which costs more when each GEMM is small. And the Triton
reducer costs about 10 us more host-side launch time than `torch.sum`
(21.4 us vs 11.4 us), which CUDA graph capture removes. Those do not fully
account for the eager deltas, so the remaining gap is not yet explained;
CUDA graph replay is the regime this change targets.
Accuracy did not regress. Relative L2 error against an fp32 reduce-scatter
reference is 2.45e-3 -> 2.03e-3 at TP=2 (the ring keeps the local product
unrounded before adding the peer partial) and 2.36e-3 -> 2.36e-3 at TP=4, where
the reduction math is unchanged.
Test plan:
- `lintrunner -a -- torch/distributed/_symmetric_memory/__init__.py test/distributed/test_symmetric_memory.py`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter_bfloat16_custom_reduce`
- `python test/test_reductions.py -k cuda`
- Before/after benchmark above via `torchrun --nproc_per_node={2,4}`, which also
checks both schedules against an fp32 reduce-scatter reference
This PR was authored with the help of an AI assistant.
Co-authored-by: Codex <[email protected]>
Co-authored-by: Claude <[email protected]>
ghstack-source-id: 1f7efed
Pull-Request: #191803
Rework the reduction in `fused_matmul_reduce_scatter` so it is no longer an
exposed memory-bandwidth-bound kernel at the end of the op.
The existing implementation computes per-destination GEMM partials, exchanges
them with the all-to-all pipeline, then reduces with a final `torch.sum` /
`torch.mean` over `stacked_partials`. That reduction is a separate kernel that
only starts after the communication and GEMM work has completed, so all of its
memory traffic sits on the critical path. Splitting it into smaller adds
overlapped with the pipeline was also tried, and still left the reduction
reads/writes exposed.
Two paths replace it, covering different parts of the dtype space:
1. Ring addmm schedule, used for 2D `scatter_dim=0` with
`reduce_op in {"sum", "avg"}` when the output dtype is not fp16/bf16, or
when `group_size == 2` and `reduce_op == "sum"`. Each partial output shard
rotates around the ring: the first iteration writes the local partial with
`mm.out`, and later iterations wait on the left peer's partial and pass it
as the C operand of `torch.addmm`. The reduction is folded into GEMM's C
operand, so there is no final reduction kernel at all.
2. A fused reducer for the remaining low-precision cases, which keeps the
existing all-to-all pipeline but replaces the final `torch.sum` with a
single Triton kernel that reads the bf16/fp16 partials, accumulates in fp32
registers, and stores the low-precision output once.
The reason low precision does not simply use the ring everywhere is that the
ring transports a running partial sum, which would be rounded back to bf16 at
every hop, so the accumulation error would grow with the TP degree. At
`group_size == 2` the transported value is a single partial rather than a
running sum, and `addmm` accumulates `A @ B` in fp32 before adding the peer's
bf16 partial, so there is exactly one rounding. That keeps the intended
contract in both paths: bf16 on the wire, fp32 accumulation, one rounding on
store.
The ring form depends on the preceding addmm change so that `addmm` can pass
distinct C and D pointers to cuBLASLt without first copying C into D. Without
it, a remote symmetric-memory C operand triggers an extra D2D copy before the
GEMM that defeats most of the benefit.
Scope stays narrow: unscaled 2D inputs with `scatter_dim=0` and
`reduce_op in {"sum", "avg"}`. Other shapes and scaled matmul keep the existing
implementation.
Performance
-----------
GB300, bf16. `A` is `[M, K_per_rank]`, `B` is `[K_per_rank, N]`, output is
`[M/TP, N]`. Per-rank GEMM throughput (`2 * M * N * K_per_rank / time`) in
TFLOP/s, median of 60 iterations after 25 warmup, slowest rank, averaged over
two runs per arm. "before" is a checkout of this commit's parent, so both arms
are the real operator. The last three rows of each table are the shapes recorded
in an internal fused reduce-scatter kernel; the rest are attention output
projections and all-gather backward dgrads.
TP=2 (ring path):
| M | N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| 16384 | 7168 | 3584 | 925.5 | 1315.7 | 1.42 | 1119.8 | 1661.0 | 1.48 |
| 16384 | 4096 | 2048 | 583.0 | 746.1 | 1.28 | 784.4 | 1323.0 | 1.69 |
| 16384 | 7168 | 8192 | 1337.1 | 1516.5 | 1.13 | 1453.2 | 1639.2 | 1.13 |
| 16384 | 7168 | 9216 | 1386.3 | 1528.4 | 1.10 | 1482.8 | 1623.8 | 1.10 |
| 16384 | 4096 | 9216 | 1282.2 | 1424.4 | 1.11 | 1408.2 | 1630.4 | 1.16 |
| 16384 | 1536 | 24576 | 1366.7 | 1412.8 | 1.03 | 1594.5 | 1669.3 | 1.05 |
| 16384 | 512 | 32768 | 1064.4 | 1091.8 | 1.03 | 1545.7 | 1546.7 | 1.00 |
| 4096 | 6144 | 8192 | 907.0 | 958.8 | 1.06 | 1382.3 | 1492.0 | 1.08 |
| 4096 | 8192 | 8192 | 986.0 | 1082.4 | 1.10 | 1391.2 | 1524.8 | 1.10 |
| 4096 | 11264 | 11264 | 1257.8 | 1362.6 | 1.08 | 1540.9 | 1686.4 | 1.09 |
TP=4 (fused reducer path):
| M | N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| 16384 | 7168 | 1792 | 683.6 | 726.5 | 1.06 | 900.6 | 1003.8 | 1.11 |
| 16384 | 4096 | 1024 | 260.4 | 245.6 | 0.94 | 518.6 | 585.1 | 1.13 |
| 16384 | 7168 | 4096 | 1125.1 | 1192.0 | 1.06 | 1340.8 | 1450.7 | 1.08 |
| 16384 | 7168 | 9216 | 1467.3 | 1517.9 | 1.03 | 1551.0 | 1603.2 | 1.03 |
| 16384 | 4096 | 9216 | 1320.1 | 1374.2 | 1.04 | 1480.9 | 1562.1 | 1.05 |
| 16384 | 1536 | 24576 | 1380.0 | 1409.2 | 1.02 | 1632.6 | 1649.4 | 1.01 |
| 16384 | 512 | 32768 | 957.5 | 920.6 | 0.96 | 1388.1 | 1424.7 | 1.03 |
| 4096 | 6144 | 8192 | 770.2 | 735.9 | 0.96 | 1404.2 | 1500.5 | 1.07 |
| 4096 | 8192 | 8192 | 1018.5 | 968.1 | 0.95 | 1469.7 | 1539.2 | 1.05 |
| 4096 | 11264 | 11264 | 1222.3 | 1250.0 | 1.02 | 1479.0 | 1520.8 | 1.03 |
Under CUDA graph replay every shape improves: 1.00x to 1.69x at TP=2 and 1.01x
to 1.13x at TP=4.
Eager differs by path. TP=2 gains everywhere (1.03x to 1.42x) because the ring
removes a whole kernel plus a peer copy. TP=4 is a wash (0.94x to 1.06x): the
fused reducer has the better GPU time but costs about 10 us more to launch than
`torch.sum`, which is not recovered until capture removes launch cost. Measuring
the two reducers directly in eager through the operator gives 0.95x to 1.05x at
TP=4 and 1.04x to 1.19x at TP=2, so gating the reducer on eager-vs-captured
would lose more than it saves and is deliberately not done.
Accuracy did not regress. Relative L2 error against an fp32 reduce-scatter
reference is 2.45e-3 -> 2.03e-3 at TP=2 (the ring keeps the local product
unrounded before adding the peer partial) and 2.36e-3 -> 2.36e-3 at TP=4, where
the reduction math is unchanged.
Test plan:
- `lintrunner -a -- torch/distributed/_symmetric_memory/__init__.py test/distributed/test_symmetric_memory.py`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter_bfloat16_custom_reduce`
(checks the eager result against the fallback and then captures the op in a
CUDA graph and rechecks, since graph replay is the regime this targets)
- `python test/test_reductions.py -k cuda`
- Before/after benchmark above via `torchrun --nproc_per_node={2,4}`, which also
checks both schedules against an fp32 reduce-scatter reference
This PR was authored with the help of an AI assistant.
Co-authored-by: Codex <[email protected]>
Co-authored-by: Claude <[email protected]>
ghstack-source-id: 117b1e4
Pull-Request: #191803
| mask=mask, | ||
| other=0.0, | ||
| ) | ||
| acc += vals.to(tl.float32) |
There was a problem hiding this comment.
Should we do tree reduction here? or does that not matter for fp32?
Rework the reduction in `fused_matmul_reduce_scatter` so it is no longer an
exposed memory-bandwidth-bound kernel at the end of the op.
The existing implementation computes per-destination GEMM partials, exchanges
them with the all-to-all pipeline, then reduces with a final `torch.sum` /
`torch.mean` over `stacked_partials`. That reduction is a separate kernel that
only starts after the communication and GEMM work has completed, so all of its
memory traffic sits on the critical path. Splitting it into smaller adds
overlapped with the pipeline was also tried, and still left the reduction
reads/writes exposed.
Two paths replace it, covering different parts of the dtype space:
1. Ring addmm schedule, used for 2D `scatter_dim=0` with
`reduce_op in {"sum", "avg"}` when the output dtype is not fp16/bf16, or
when `group_size == 2` and `reduce_op == "sum"`. Each partial output shard
rotates around the ring: the first iteration writes the local partial with
`mm.out`, and later iterations wait on the left peer's partial and pass it
as the C operand of `torch.addmm`. The reduction is folded into GEMM's C
operand, so there is no final reduction kernel at all.
2. A fused reducer for the remaining low-precision cases, which keeps the
existing all-to-all pipeline but replaces the final `torch.sum` with a
single Triton kernel that reads the bf16/fp16 partials, accumulates in fp32
registers, and stores the low-precision output once.
The reason low precision does not simply use the ring everywhere is that the
ring transports a running partial sum, which would be rounded back to bf16 at
every hop, so the accumulation error would grow with the TP degree. At
`group_size == 2` the transported value is a single partial rather than a
running sum, and `addmm` accumulates `A @ B` in fp32 before adding the peer's
bf16 partial, so there is exactly one rounding. That keeps the intended
contract in both paths: bf16 on the wire, fp32 accumulation, one rounding on
store.
The ring form depends on the preceding addmm change so that `addmm` can pass
distinct C and D pointers to cuBLASLt without first copying C into D. Without
it, a remote symmetric-memory C operand triggers an extra D2D copy before the
GEMM that defeats most of the benefit.
Scope stays narrow: unscaled 2D inputs with `scatter_dim=0` and
`reduce_op in {"sum", "avg"}`. Other shapes and scaled matmul keep the existing
implementation.
Performance
-----------
GB300, bf16. `A` is `[M, K_per_rank]`, `B` is `[K_per_rank, N]`, output is
`[M/TP, N]`. Per-rank GEMM throughput (`2 * M * N * K_per_rank / time`) in
TFLOP/s, median of 60 iterations after 25 warmup, slowest rank, averaged over
two runs per arm. "before" is a checkout of this commit's parent, so both arms
are the real operator. The last three rows of each table are the shapes recorded
in an internal fused reduce-scatter kernel; the rest are attention output
projections and all-gather backward dgrads.
TP=2 (ring path):
| M | N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| 16384 | 7168 | 3584 | 925.5 | 1315.7 | 1.42 | 1119.8 | 1661.0 | 1.48 |
| 16384 | 4096 | 2048 | 583.0 | 746.1 | 1.28 | 784.4 | 1323.0 | 1.69 |
| 16384 | 7168 | 8192 | 1337.1 | 1516.5 | 1.13 | 1453.2 | 1639.2 | 1.13 |
| 16384 | 7168 | 9216 | 1386.3 | 1528.4 | 1.10 | 1482.8 | 1623.8 | 1.10 |
| 16384 | 4096 | 9216 | 1282.2 | 1424.4 | 1.11 | 1408.2 | 1630.4 | 1.16 |
| 16384 | 1536 | 24576 | 1366.7 | 1412.8 | 1.03 | 1594.5 | 1669.3 | 1.05 |
| 16384 | 512 | 32768 | 1064.4 | 1091.8 | 1.03 | 1545.7 | 1546.7 | 1.00 |
| 4096 | 6144 | 8192 | 907.0 | 958.8 | 1.06 | 1382.3 | 1492.0 | 1.08 |
| 4096 | 8192 | 8192 | 986.0 | 1082.4 | 1.10 | 1391.2 | 1524.8 | 1.10 |
| 4096 | 11264 | 11264 | 1257.8 | 1362.6 | 1.08 | 1540.9 | 1686.4 | 1.09 |
TP=4 (fused reducer path):
| M | N | K_per_rank | eager before | eager after | eager x | graph before | graph after | graph x |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| 16384 | 7168 | 1792 | 683.6 | 726.5 | 1.06 | 900.6 | 1003.8 | 1.11 |
| 16384 | 4096 | 1024 | 260.4 | 245.6 | 0.94 | 518.6 | 585.1 | 1.13 |
| 16384 | 7168 | 4096 | 1125.1 | 1192.0 | 1.06 | 1340.8 | 1450.7 | 1.08 |
| 16384 | 7168 | 9216 | 1467.3 | 1517.9 | 1.03 | 1551.0 | 1603.2 | 1.03 |
| 16384 | 4096 | 9216 | 1320.1 | 1374.2 | 1.04 | 1480.9 | 1562.1 | 1.05 |
| 16384 | 1536 | 24576 | 1380.0 | 1409.2 | 1.02 | 1632.6 | 1649.4 | 1.01 |
| 16384 | 512 | 32768 | 957.5 | 920.6 | 0.96 | 1388.1 | 1424.7 | 1.03 |
| 4096 | 6144 | 8192 | 770.2 | 735.9 | 0.96 | 1404.2 | 1500.5 | 1.07 |
| 4096 | 8192 | 8192 | 1018.5 | 968.1 | 0.95 | 1469.7 | 1539.2 | 1.05 |
| 4096 | 11264 | 11264 | 1222.3 | 1250.0 | 1.02 | 1479.0 | 1520.8 | 1.03 |
Under CUDA graph replay every shape improves: 1.00x to 1.69x at TP=2 and 1.01x
to 1.13x at TP=4.
Eager differs by path. TP=2 gains everywhere (1.03x to 1.42x) because the ring
removes a whole kernel plus a peer copy. TP=4 is a wash (0.94x to 1.06x): the
fused reducer has the better GPU time but costs about 10 us more to launch than
`torch.sum`, which is not recovered until capture removes launch cost. Measuring
the two reducers directly in eager through the operator gives 0.95x to 1.05x at
TP=4 and 1.04x to 1.19x at TP=2, so gating the reducer on eager-vs-captured
would lose more than it saves and is deliberately not done.
Accuracy did not regress. Relative L2 error against an fp32 reduce-scatter
reference is 2.45e-3 -> 2.03e-3 at TP=2 (the ring keeps the local product
unrounded before adding the peer partial) and 2.36e-3 -> 2.36e-3 at TP=4, where
the reduction math is unchanged.
Test plan:
- `lintrunner -a -- torch/distributed/_symmetric_memory/__init__.py test/distributed/test_symmetric_memory.py`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter_bfloat16_custom_reduce`
(checks the eager result against the fallback and then captures the op in a
CUDA graph and rechecks, since graph replay is the regime this targets)
- `python test/test_reductions.py -k cuda`
- Before/after benchmark above via `torchrun --nproc_per_node={2,4}`, which also
checks both schedules against an fp32 reduce-scatter reference
This PR was authored with the help of an AI assistant.
Co-authored-by: Codex <[email protected]>
Co-authored-by: Claude <[email protected]>
ghstack-source-id: 81e50c4
Pull-Request: #191803
|
cc @slayton58, there's no reason why torch.sum shouldn't be reasonable to have outer dimension reduction over 4-8 values, hopefully we can get it with cuteDSL to not need triton |
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Merge failedReason: 2 jobs have failed, first few of them are: B200 Smoke Tests / linux-jammy-cuda13.0-py3.12-gcc11-sm100 / test (smoke_b200, 1, 1, linux.dgx.b200), B200 Smoke Tests / linux-jammy-cuda13.2-py3.12-gcc11-sm100 / test (smoke_b200, 1, 1, linux.dgx.b200) Details for Dev Infra teamRaised by workflow job |
Failing jobs: - linux-jammy-cuda13.0-py3.10-gcc11 / test (default, 11, 14, mt-l-x86aavx2-29-113-l4) - linux-jammy-cuda13.2-py3.10-gcc11 / test (default, 1, 5, mt-l-x86aavx2-29-113-l4) - linux-jammy-cuda13.2-py3.10-gcc11 / test (default, 4, 5, mt-l-x86aavx2-29-113-l4) - linux-jammy-cuda13.2-py3.10-gcc11 / test (default, 5, 5, mt-l-x86aavx2-29-113-l4) Failing targets: - distributed/test_pg_wrapper 1/1 - distributed/test_c10d_nccl 1/1 - distributed/test_store 1/1 Errors: AssertionError: test_pg_wrapper must not have initialized CUDA/XPU context on main process AssertionError: test_distributed must not have initialized CUDA context on main process AssertionError: test_distributed must not have initialized cuda context on main process The module-level has_triton() call queried CUDA device capabilities while the symmetric memory module was imported, initializing CUDA before these test entry points checked their main process. Move the Triton kernel to a lazily imported module and check the input device before Triton availability so the kernel is loaded only while reducing an actual CUDA tensor. Test Plan: Not run per the mergedog restriction against local PyTorch tests, builds, or imports. Lint checks passed: ``` lintrunner -a spin quicklint ``` Authored with an AI assistant. [ghstack-poisoned]
Rework the reduction in `fused_matmul_reduce_scatter` so it is no longer an
exposed memory-bandwidth-bound kernel at the end of the op.
The existing implementation computes per-destination GEMM partials, exchanges
them with the all-to-all pipeline, then reduces with a final `torch.sum` /
`torch.mean` over `stacked_partials`. That reduction is a separate kernel that
only starts after the communication and GEMM work has completed, so all of its
memory traffic sits on the critical path. Splitting it into smaller adds
overlapped with the pipeline was also tried, and still left the reduction
reads/writes exposed.
This keeps the all-to-all pipeline and replaces that final `torch.sum` with a
single Triton kernel that reads the bf16/fp16 partials, accumulates in fp32
registers, and stores the low-precision output once. The contract is unchanged:
bf16 on the wire, fp32 accumulation, one rounding on store.
`torch.sum` is a poor fit here rather than merely unoptimized. On these shapes it
reaches 23-43% of HBM peak against the Triton kernel's 82-88%, at identical DRAM
traffic, because ATen's `reduce_kernel` is instruction-bound: for a reduction of
extent 2-8 it executes roughly ten times the instructions per output element,
most of them index arithmetic and configuration branches rather than the handful
of adds the reduction needs. That cost is structural to a kernel whose reduction
strategy lives in runtime data, so it is not reachable by tuning.
Scope stays narrow: low-precision output with the reduction over the leading
dim, which is the layout the all-to-all pipeline produces. Everything else,
including scaled matmul, keeps `torch.sum` via the existing fallback.
A separate schedule for the two-rank case, which accumulates through `addmm`'s C
operand and removes the reduction kernel entirely, will follow in its own PR.
Performance
-----------
GB300, bf16. `A` is `[M, K_per_rank]`, `B` is `[K_per_rank, N]`, output is
`[M/TP, N]`. Per-rank GEMM throughput (`2 * M * N * K_per_rank / time`) in
TFLOP/s, median of 15 timed rounds of 10 back-to-back calls after warmup,
slowest rank. All figures are CUDA graph replay; the eager picture is discussed
below. `unfused` is `torch.mm` followed by `dist.reduce_scatter_tensor`, and is
included only as a reference point -- the change itself is `torch.sum` vs
`Triton`.
TP=4:
| N | K_per_rank | M | unfused | torch.sum | Triton | Triton/sum |
| --- | --- | --- | --- | --- | --- | --- |
| 7168 | 2048 | 8192 | 770 | 880 | 987 | 1.12 |
| 7168 | 2048 | 32768 | 850 | 1038 | 1150 | 1.11 |
| 12288 | 4096 | 8192 | 1136 | 1251 | 1337 | 1.07 |
| 12288 | 4096 | 32768 | 1238 | 1400 | 1496 | 1.07 |
| 5120 | 1280 | 8192 | 557 | 539 | 594 | 1.10 |
| 5120 | 1280 | 32768 | 622 | 747 | 848 | 1.13 |
| 5120 | 6400 | 8192 | 1270 | 1425 | 1531 | 1.07 |
| 5120 | 6400 | 32768 | 1332 | 1454 | 1516 | 1.04 |
| 4096 | 2048 | 8192 | 728 | 726 | 825 | 1.14 |
| 4096 | 2048 | 32768 | 816 | 1013 | 1115 | 1.10 |
| 2048 | 1024 | 8192 | 334 | 298 | 304 | 1.02 |
| 2048 | 1024 | 32768 | 490 | 521 | 588 | 1.13 |
Over the full grid (6 shapes x M in {4096, 8192, 16384, 32768}) the Triton
reducer is 1.02x to 1.14x, median 1.09x.
TP=2, where the reduction is over only two partials and `torch.sum` is at its
worst:
| N | K_per_rank | M | unfused | torch.sum | Triton | Triton/sum |
| --- | --- | --- | --- | --- | --- | --- |
| 7168 | 4096 | 8192 | 1175 | 1219 | 1441 | 1.18 |
| 7168 | 4096 | 32768 | 1231 | 1322 | 1555 | 1.18 |
| 12288 | 8192 | 8192 | 1429 | 1424 | 1521 | 1.07 |
| 12288 | 8192 | 32768 | 1455 | 1460 | 1560 | 1.07 |
| 5120 | 2560 | 8192 | 903 | 973 | 1271 | 1.31 |
| 5120 | 2560 | 32768 | 1015 | 931 | 1181 | 1.27 |
| 5120 | 12800 | 8192 | 1488 | 1517 | 1538 | 1.01 |
| 5120 | 12800 | 32768 | 1559 | 1567 | 1641 | 1.05 |
| 4096 | 4096 | 8192 | 1106 | 1240 | 1510 | 1.22 |
| 4096 | 4096 | 32768 | 1224 | 1178 | 1362 | 1.16 |
| 2048 | 2048 | 8192 | 682 | 612 | 758 | 1.24 |
| 2048 | 2048 | 32768 | 826 | 787 | 1010 | 1.28 |
Same grid: 1.01x to 1.32x, median 1.19x. The gain tracks how much of the op the
reduction is, so it is largest at small `K_per_rank` (thin GEMM, unchanged
reduction) and smallest at large `K_per_rank`, where the GEMM dominates.
For sizing the Triton dependency: measured in isolation the reducer is 2.0x to
3.2x faster than `torch.sum`, but the reduction is only 7-15% of the operator at
these shapes, which is why the end-to-end numbers above are 1.0x-1.3x rather
than 2x.
Eager is a wash and can be slightly negative: 0.86x to 1.14x, median 0.97x at
TP=4 and 1.05x at TP=2. The Triton kernel has the better GPU time but costs
about 10 us more to launch than `torch.sum`, which is not recovered until
capture removes launch cost. Gating the reducer on eager-vs-captured would lose
more than it saves and is deliberately not done.
Accuracy did not regress: relative L2 error against an fp32 reduce-scatter
reference is 2.36e-3 before and after, since the reduction math is unchanged.
Test plan:
- `lintrunner -a -- torch/distributed/_symmetric_memory/__init__.py test/distributed/test_symmetric_memory.py`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter_bfloat16_custom_reduce`
(checks the eager result against the fallback and then captures the op in a
CUDA graph and rechecks, since graph replay is the regime this targets)
- `python test/test_reductions.py -k cuda`
- Benchmark above via `torchrun --nproc_per_node={2,4}`, which also checks the
reducer against an fp32 reduce-scatter reference
This PR was authored with the help of an AI assistant.
Co-authored-by: Codex <[email protected]>
Co-authored-by: Claude <[email protected]>
ghstack-source-id: d610bb8
Pull-Request: #191803
| from torch.utils._triton import has_triton | ||
|
|
||
|
|
||
| _group_name_to_store: dict[str, c10d.Store] = {} |
There was a problem hiding this comment.
This hunk was LLM-generated by mergedog in commit ebb7ae1ef2a3. Review that commit for context and justification.
Note
This PR is being shepherded by mergedog, an automation tool run by a maintainer. It watches CI and may push fixes; the PR author does not need to act unless a maintainer asks.
| group_size: int, | ||
| ) -> torch.Tensor | None: | ||
| if ( | ||
| partials.device.type != "cuda" |
There was a problem hiding this comment.
This hunk was LLM-generated by mergedog in commit ebb7ae1ef2a3. Review that commit for context and justification.
Note
This PR is being shepherded by mergedog, an automation tool run by a maintainer. It watches CI and may push fixes; the PR author does not need to act unless a maintainer asks.
| out = partials.new_empty(partials.shape[1:], dtype=output_dtype) | ||
| shard_elems = out.numel() | ||
| block = 1024 | ||
| import triton |
There was a problem hiding this comment.
This hunk was LLM-generated by mergedog in commit ebb7ae1ef2a3. Review that commit for context and justification.
Note
This PR is being shepherded by mergedog, an automation tool run by a maintainer. It watches CI and may push fixes; the PR author does not need to act unless a maintainer asks.
| @@ -0,0 +1,27 @@ | |||
| import triton | |||
There was a problem hiding this comment.
This hunk was LLM-generated by mergedog in commit ebb7ae1ef2a3. Review that commit for context and justification.
Note
This PR is being shepherded by mergedog, an automation tool run by a maintainer. It watches CI and may push fixes; the PR author does not need to act unless a maintainer asks.
Failing jobs: linux-jammy-aarch64-py3.10 / test (default, 3, 7, lf-l-arm64g4-16-62) and linux-jammy-py3.14t-clang21 / test (default, 6, 11, lf-l-x86iavx512-16-128) Failing command: python test/test_public_bindings.py TestPublicBindings.test_modules_can_be_imported Error: torch.distributed._symmetric_memory._triton_kernels failed to import with error ModuleNotFoundError: No module named 'triton' The new private kernel module imported the optional Triton dependency unconditionally when the public-bindings test discovered every torch package. Guard the Triton imports and kernel definition with has_triton(), matching the existing runtime gate, so non-Triton configurations can import the module while Triton-capable configurations still define the kernel. Test Plan: ``` lintrunner -a ``` This PR was authored with an AI assistant. [ghstack-poisoned]
Rework the reduction in `fused_matmul_reduce_scatter` so it is no longer an
exposed memory-bandwidth-bound kernel at the end of the op.
The existing implementation computes per-destination GEMM partials, exchanges
them with the all-to-all pipeline, then reduces with a final `torch.sum` /
`torch.mean` over `stacked_partials`. That reduction is a separate kernel that
only starts after the communication and GEMM work has completed, so all of its
memory traffic sits on the critical path. Splitting it into smaller adds
overlapped with the pipeline was also tried, and still left the reduction
reads/writes exposed.
This keeps the all-to-all pipeline and replaces that final `torch.sum` with a
single Triton kernel that reads the bf16/fp16 partials, accumulates in fp32
registers, and stores the low-precision output once. The contract is unchanged:
bf16 on the wire, fp32 accumulation, one rounding on store.
`torch.sum` is a poor fit here rather than merely unoptimized. On these shapes it
reaches 23-43% of HBM peak against the Triton kernel's 82-88%, at identical DRAM
traffic, because ATen's `reduce_kernel` is instruction-bound: for a reduction of
extent 2-8 it executes roughly ten times the instructions per output element,
most of them index arithmetic and configuration branches rather than the handful
of adds the reduction needs. That cost is structural to a kernel whose reduction
strategy lives in runtime data, so it is not reachable by tuning.
Scope stays narrow: low-precision output with the reduction over the leading
dim, which is the layout the all-to-all pipeline produces. Everything else,
including scaled matmul, keeps `torch.sum` via the existing fallback.
A separate schedule for the two-rank case, which accumulates through `addmm`'s C
operand and removes the reduction kernel entirely, will follow in its own PR.
Performance
-----------
GB300, bf16. `A` is `[M, K_per_rank]`, `B` is `[K_per_rank, N]`, output is
`[M/TP, N]`. Per-rank GEMM throughput (`2 * M * N * K_per_rank / time`) in
TFLOP/s, median of 15 timed rounds of 10 back-to-back calls after warmup,
slowest rank. All figures are CUDA graph replay; the eager picture is discussed
below. `unfused` is `torch.mm` followed by `dist.reduce_scatter_tensor`, and is
included only as a reference point -- the change itself is `torch.sum` vs
`Triton`.
TP=4:
| N | K_per_rank | M | unfused | torch.sum | Triton | Triton/sum |
| --- | --- | --- | --- | --- | --- | --- |
| 7168 | 2048 | 8192 | 770 | 880 | 987 | 1.12 |
| 7168 | 2048 | 32768 | 850 | 1038 | 1150 | 1.11 |
| 12288 | 4096 | 8192 | 1136 | 1251 | 1337 | 1.07 |
| 12288 | 4096 | 32768 | 1238 | 1400 | 1496 | 1.07 |
| 5120 | 1280 | 8192 | 557 | 539 | 594 | 1.10 |
| 5120 | 1280 | 32768 | 622 | 747 | 848 | 1.13 |
| 5120 | 6400 | 8192 | 1270 | 1425 | 1531 | 1.07 |
| 5120 | 6400 | 32768 | 1332 | 1454 | 1516 | 1.04 |
| 4096 | 2048 | 8192 | 728 | 726 | 825 | 1.14 |
| 4096 | 2048 | 32768 | 816 | 1013 | 1115 | 1.10 |
| 2048 | 1024 | 8192 | 334 | 298 | 304 | 1.02 |
| 2048 | 1024 | 32768 | 490 | 521 | 588 | 1.13 |
Over the full grid (6 shapes x M in {4096, 8192, 16384, 32768}) the Triton
reducer is 1.02x to 1.14x, median 1.09x.
TP=2, where the reduction is over only two partials and `torch.sum` is at its
worst:
| N | K_per_rank | M | unfused | torch.sum | Triton | Triton/sum |
| --- | --- | --- | --- | --- | --- | --- |
| 7168 | 4096 | 8192 | 1175 | 1219 | 1441 | 1.18 |
| 7168 | 4096 | 32768 | 1231 | 1322 | 1555 | 1.18 |
| 12288 | 8192 | 8192 | 1429 | 1424 | 1521 | 1.07 |
| 12288 | 8192 | 32768 | 1455 | 1460 | 1560 | 1.07 |
| 5120 | 2560 | 8192 | 903 | 973 | 1271 | 1.31 |
| 5120 | 2560 | 32768 | 1015 | 931 | 1181 | 1.27 |
| 5120 | 12800 | 8192 | 1488 | 1517 | 1538 | 1.01 |
| 5120 | 12800 | 32768 | 1559 | 1567 | 1641 | 1.05 |
| 4096 | 4096 | 8192 | 1106 | 1240 | 1510 | 1.22 |
| 4096 | 4096 | 32768 | 1224 | 1178 | 1362 | 1.16 |
| 2048 | 2048 | 8192 | 682 | 612 | 758 | 1.24 |
| 2048 | 2048 | 32768 | 826 | 787 | 1010 | 1.28 |
Same grid: 1.01x to 1.32x, median 1.19x. The gain tracks how much of the op the
reduction is, so it is largest at small `K_per_rank` (thin GEMM, unchanged
reduction) and smallest at large `K_per_rank`, where the GEMM dominates.
For sizing the Triton dependency: measured in isolation the reducer is 2.0x to
3.2x faster than `torch.sum`, but the reduction is only 7-15% of the operator at
these shapes, which is why the end-to-end numbers above are 1.0x-1.3x rather
than 2x.
Eager is a wash and can be slightly negative: 0.86x to 1.14x, median 0.97x at
TP=4 and 1.05x at TP=2. The Triton kernel has the better GPU time but costs
about 10 us more to launch than `torch.sum`, which is not recovered until
capture removes launch cost. Gating the reducer on eager-vs-captured would lose
more than it saves and is deliberately not done.
Accuracy did not regress: relative L2 error against an fp32 reduce-scatter
reference is 2.36e-3 before and after, since the reduction math is unchanged.
Test plan:
- `lintrunner -a -- torch/distributed/_symmetric_memory/__init__.py test/distributed/test_symmetric_memory.py`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter_bfloat16_custom_reduce`
(checks the eager result against the fallback and then captures the op in a
CUDA graph and rechecks, since graph replay is the regime this targets)
- `python test/test_reductions.py -k cuda`
- Benchmark above via `torchrun --nproc_per_node={2,4}`, which also checks the
reducer against an fp32 reduce-scatter reference
This PR was authored with the help of an AI assistant.
Co-authored-by: Codex <[email protected]>
Co-authored-by: Claude <[email protected]>
ghstack-source-id: fceadd8
Pull-Request: #191803
| @@ -0,0 +1,30 @@ | |||
| from torch.utils._triton import has_triton | |||
There was a problem hiding this comment.
This hunk was LLM-generated by mergedog in commit 5625ea44c141. Review that commit for context and justification.
Note
This PR is being shepherded by mergedog, an automation tool run by a maintainer. It watches CI and may push fixes; the PR author does not need to act unless a maintainer asks.
| from torch.utils._triton import has_triton | ||
|
|
||
|
|
||
| if has_triton(): |
There was a problem hiding this comment.
This hunk was LLM-generated by mergedog in commit 5625ea44c141. Review that commit for context and justification.
Note
This PR is being shepherded by mergedog, an automation tool run by a maintainer. It watches CI and may push fixes; the PR author does not need to act unless a maintainer asks.
Rework the reduction in `fused_matmul_reduce_scatter` so it is no longer an
exposed memory-bandwidth-bound kernel at the end of the op.
The existing implementation computes per-destination GEMM partials, exchanges
them with the all-to-all pipeline, then reduces with a final `torch.sum` /
`torch.mean` over `stacked_partials`. That reduction is a separate kernel that
only starts after the communication and GEMM work has completed, so all of its
memory traffic sits on the critical path. Splitting it into smaller adds
overlapped with the pipeline was also tried, and still left the reduction
reads/writes exposed.
This keeps the all-to-all pipeline and replaces that final `torch.sum` with a
single Triton kernel that reads the bf16/fp16 partials, accumulates in fp32
registers, and stores the low-precision output once. The contract is unchanged:
bf16 on the wire, fp32 accumulation, one rounding on store.
`torch.sum` is a poor fit here rather than merely unoptimized. On these shapes it
reaches 23-43% of HBM peak against the Triton kernel's 82-88%, at identical DRAM
traffic, because ATen's `reduce_kernel` is instruction-bound: for a reduction of
extent 2-8 it executes roughly ten times the instructions per output element,
most of them index arithmetic and configuration branches rather than the handful
of adds the reduction needs. That cost is structural to a kernel whose reduction
strategy lives in runtime data, so it is not reachable by tuning.
Scope stays narrow: low-precision output with the reduction over the leading
dim, which is the layout the all-to-all pipeline produces. Everything else,
including scaled matmul, keeps `torch.sum` via the existing fallback.
A separate schedule for the two-rank case, which accumulates through `addmm`'s C
operand and removes the reduction kernel entirely, will follow in its own PR.
Performance
-----------
GB300, bf16. `A` is `[M, K_per_rank]`, `B` is `[K_per_rank, N]`, output is
`[M/TP, N]`. Per-rank GEMM throughput (`2 * M * N * K_per_rank / time`) in
TFLOP/s, median of 15 timed rounds of 10 back-to-back calls after warmup,
slowest rank. All figures are CUDA graph replay; the eager picture is discussed
below. `unfused` is `torch.mm` followed by `dist.reduce_scatter_tensor`, and is
included only as a reference point -- the change itself is `torch.sum` vs
`Triton`.
TP=4:
| N | K_per_rank | M | unfused | torch.sum | Triton | Triton/sum |
| --- | --- | --- | --- | --- | --- | --- |
| 7168 | 2048 | 8192 | 770 | 880 | 987 | 1.12 |
| 7168 | 2048 | 32768 | 850 | 1038 | 1150 | 1.11 |
| 12288 | 4096 | 8192 | 1136 | 1251 | 1337 | 1.07 |
| 12288 | 4096 | 32768 | 1238 | 1400 | 1496 | 1.07 |
| 5120 | 1280 | 8192 | 557 | 539 | 594 | 1.10 |
| 5120 | 1280 | 32768 | 622 | 747 | 848 | 1.13 |
| 5120 | 6400 | 8192 | 1270 | 1425 | 1531 | 1.07 |
| 5120 | 6400 | 32768 | 1332 | 1454 | 1516 | 1.04 |
| 4096 | 2048 | 8192 | 728 | 726 | 825 | 1.14 |
| 4096 | 2048 | 32768 | 816 | 1013 | 1115 | 1.10 |
| 2048 | 1024 | 8192 | 334 | 298 | 304 | 1.02 |
| 2048 | 1024 | 32768 | 490 | 521 | 588 | 1.13 |
Over the full grid (6 shapes x M in {4096, 8192, 16384, 32768}) the Triton
reducer is 1.02x to 1.14x, median 1.09x.
TP=2, where the reduction is over only two partials and `torch.sum` is at its
worst:
| N | K_per_rank | M | unfused | torch.sum | Triton | Triton/sum |
| --- | --- | --- | --- | --- | --- | --- |
| 7168 | 4096 | 8192 | 1175 | 1219 | 1441 | 1.18 |
| 7168 | 4096 | 32768 | 1231 | 1322 | 1555 | 1.18 |
| 12288 | 8192 | 8192 | 1429 | 1424 | 1521 | 1.07 |
| 12288 | 8192 | 32768 | 1455 | 1460 | 1560 | 1.07 |
| 5120 | 2560 | 8192 | 903 | 973 | 1271 | 1.31 |
| 5120 | 2560 | 32768 | 1015 | 931 | 1181 | 1.27 |
| 5120 | 12800 | 8192 | 1488 | 1517 | 1538 | 1.01 |
| 5120 | 12800 | 32768 | 1559 | 1567 | 1641 | 1.05 |
| 4096 | 4096 | 8192 | 1106 | 1240 | 1510 | 1.22 |
| 4096 | 4096 | 32768 | 1224 | 1178 | 1362 | 1.16 |
| 2048 | 2048 | 8192 | 682 | 612 | 758 | 1.24 |
| 2048 | 2048 | 32768 | 826 | 787 | 1010 | 1.28 |
Same grid: 1.01x to 1.32x, median 1.19x. The gain tracks how much of the op the
reduction is, so it is largest at small `K_per_rank` (thin GEMM, unchanged
reduction) and smallest at large `K_per_rank`, where the GEMM dominates.
For sizing the Triton dependency: measured in isolation the reducer is 2.0x to
3.2x faster than `torch.sum`, but the reduction is only 7-15% of the operator at
these shapes, which is why the end-to-end numbers above are 1.0x-1.3x rather
than 2x.
Eager is a wash and can be slightly negative: 0.86x to 1.14x, median 0.97x at
TP=4 and 1.05x at TP=2. The Triton kernel has the better GPU time but costs
about 10 us more to launch than `torch.sum`, which is not recovered until
capture removes launch cost. Gating the reducer on eager-vs-captured would lose
more than it saves and is deliberately not done.
Accuracy did not regress: relative L2 error against an fp32 reduce-scatter
reference is 2.36e-3 before and after, since the reduction math is unchanged.
Test plan:
- `lintrunner -a -- torch/distributed/_symmetric_memory/__init__.py test/distributed/test_symmetric_memory.py`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter`
- `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter_bfloat16_custom_reduce`
(checks the eager result against the fallback and then captures the op in a
CUDA graph and rechecks, since graph replay is the regime this targets)
- `python test/test_reductions.py -k cuda`
- Benchmark above via `torchrun --nproc_per_node={2,4}`, which also checks the
reducer against an fp32 reduce-scatter reference
This PR was authored with the help of an AI assistant.
Co-authored-by: Codex <[email protected]>
Co-authored-by: Claude <[email protected]>
ghstack-source-id: 7726656
Pull-Request: #191803
mergedog handoffNote This PR is being shepherded by mergedog, an automation tool run by a maintainer. It watches CI and may push fixes; the PR author does not need to act unless a maintainer asks. All CI is green (or skipped). Ready for human review and Current PR head: Autonomous changes pushed
CI notes at handoffmergedog is treating these still-failing checks as unrelated/spurious. Please verify before merging:
Warning: mergedog's suppressed check list differs from the latest Dr. CI summary. Suppressed by mergedog but not marked unrelated by Dr. CI:
Latest Dr. CI summary## :link: Helpful Links
### :test_tube: See artifacts and rendered test results at [hud.pytorch.org/pr/191803](https://hud.pytorch.org/pr/191803)
* :page_facing_up: Preview [Python docs built from this PR](https://docs-preview.pytorch.org/pytorch/pytorch/191803/index.html)
* :page_facing_up: Preview [C++ docs built from this PR](https://docs-preview.pytorch.org/pytorch/pytorch/191803/cppdocs/index.html)
* :question: Need help or want to give feedback on the CI? Visit the [bot commands wiki](https://github.com/pytorch/pytorch/wiki/Bot-commands)
Note: Links to docs will display an error until the docs builds have been completed.
## :x: 2 New Failures, 7 Pending, 1 Unrelated Failure
As of commit 1bab0b7a9943e3d0f7e8f0942b02ec0031f4f7d1 with merge base 80a802e49932d20c6b2539dc6cab6cc8d0dd04cc (<sub><sub><img alt="image" width=70 src="https://img.shields.io/date/1786196505?label=&color=FFFFFF&style=flat-square"></sub></sub>):
<details open><summary><b>NEW FAILURES</b> - The following jobs have failed:</summary><p>
* [B200 Smoke Tests / linux-jammy-cuda13.0-py3.12-gcc11-sm100 / test (smoke_b200, 1, 1, linux.dgx.b200)](https://hud.pytorch.org/pr/pytorch/pytorch/191803#93123192367) ([gh](https://github.com/pytorch/pytorch/actions/runs/31265196768/job/93123192367))
`test/inductor/test_flex_gemm.py::TestFlexGemmEpilogueHOP::test_addmm_compiled_matches_reference`
<details><summary>AI verdict: <a href="https://hud.pytorch.org/pr/pytorch/pytorch/191803#93123192367"><img alt="AI verdict: probably not related" src="https://hud.pytorch.org/api/drci/advisorBadge?owner=pytorch&repo=pytorch&sha=1bab0b7a9943e3d0f7e8f0942b02ec0031f4f7d1&job=B200+Smoke+Tests+%2F+linux-jammy-cuda13.0-py3.12-gcc11-sm100+%2F+test+%28smoke_b200%2C+1%2C+1%2C+linux.dgx.b200%29"></a></summary><blockquote>
The PR only modifies distributed symmetric memory code (3 files in torch/distributed/_symmetric_memory/), which has no causal connection to the failing inductor flex_gemm addmm test. The same B200 job was already failing on trunk with different tests from the same test_flex_gemm.py file, indicating pre-existing instability.
<a href="https://hud.pytorch.org/pr/pytorch/pytorch/191803#93123192367">Full reasoning on HUD →</a>
</blockquote></details>
* [B200 Smoke Tests / linux-jammy-cuda13.2-py3.12-gcc11-sm100 / test (smoke_b200, 1, 1, linux.dgx.b200)](https://hud.pytorch.org/pr/pytorch/pytorch/191803#93123116501) ([gh](https://github.com/pytorch/pytorch/actions/runs/31265196768/job/93123116501))
`test/inductor/test_flex_gemm.py::TestFlexGemmEpilogueHOP::test_addmm_compiled_matches_reference`
<details><summary>AI verdict: <a href="https://hud.pytorch.org/pr/pytorch/pytorch/191803#93123116501"><img alt="AI verdict: not related" src="https://hud.pytorch.org/api/drci/advisorBadge?owner=pytorch&repo=pytorch&sha=1bab0b7a9943e3d0f7e8f0942b02ec0031f4f7d1&job=B200+Smoke+Tests+%2F+linux-jammy-cuda13.2-py3.12-gcc11-sm100+%2F+test+%28smoke_b200%2C+1%2C+1%2C+linux.dgx.b200%29"></a></summary><blockquote>
The PR only modifies symmetric memory/distributed code (3 files). The failing test is from test_flex_gemm.py, a file introduced by a completely different concurrent commit (#191870). The B200 job was already consistently failing with other flex_gemm tests on trunk.
<a href="https://hud.pytorch.org/pr/pytorch/pytorch/191803#93123116501">Full reasoning on HUD →</a>
</blockquote></details>
</p></details>
<details ><summary><b>FLAKY</b> - The following job failed but was likely due to flakiness present on trunk:</summary><p>
* [trunk / linux-jammy-rocm-py3.10-mi350 / test (distributed, 1, 3, linux.rocm.gpu.gfx950.2)](https://hud.pytorch.org/pr/pytorch/pytorch/191803#93124919276) ([gh](https://github.com/pytorch/pytorch/actions/runs/31265196277/job/93124919276)) ([similar failure](https://hud.pytorch.org/pytorch/pytorch/commit/3e36cdbf38d7e56d78e618ec9dd7f4171905599a#93070314770))
`test/distributed/test_symmetric_memory.py::SymmetricMemoryTest::test_alloc_rendezvous_on_current_stream`
</p></details>
This comment was automatically generated by Dr. CI and updates every 15 minutes.During shepherding, the configured LLM was invoked 1 time. If any CI shows red, it judged that failure unrelated to this PR's changes — please verify below before merging. Session 1 — fix-CI (2026-08-08T17:58:38+00:00)
LLM transcript |
|
@pytorchbot merge -i |
Merge startedYour change will be merged while ignoring the following 3 checks: trunk / linux-jammy-rocm-py3.10-mi350 / test (distributed, 1, 3, linux.rocm.gpu.gfx950.2), B200 Smoke Tests / linux-jammy-cuda13.2-py3.12-gcc11-sm100 / test (smoke_b200, 1, 1, linux.dgx.b200), B200 Smoke Tests / linux-jammy-cuda13.0-py3.12-gcc11-sm100 / test (smoke_b200, 1, 1, linux.dgx.b200) Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
…nel (pytorch#191803) Rework the reduction in `fused_matmul_reduce_scatter` so it is no longer an exposed memory-bandwidth-bound kernel at the end of the op. The existing implementation computes per-destination GEMM partials, exchanges them with the all-to-all pipeline, then reduces with a final `torch.sum` / `torch.mean` over `stacked_partials`. That reduction is a separate kernel that only starts after the communication and GEMM work has completed, so all of its memory traffic sits on the critical path. Splitting it into smaller adds overlapped with the pipeline was also tried, and still left the reduction reads/writes exposed. This keeps the all-to-all pipeline and replaces that final `torch.sum` with a single Triton kernel that reads the bf16/fp16 partials, accumulates in fp32 registers, and stores the low-precision output once. The contract is unchanged: bf16 on the wire, fp32 accumulation, one rounding on store. `torch.sum` is a poor fit here rather than merely unoptimized. On these shapes it reaches 23-43% of HBM peak against the Triton kernel's 82-88%, at identical DRAM traffic, because ATen's `reduce_kernel` is instruction-bound: for a reduction of extent 2-8 it executes roughly ten times the instructions per output element, most of them index arithmetic and configuration branches rather than the handful of adds the reduction needs. That cost is structural to a kernel whose reduction strategy lives in runtime data, so it is not reachable by tuning. Scope stays narrow: low-precision output with the reduction over the leading dim, which is the layout the all-to-all pipeline produces. Everything else, including scaled matmul, keeps `torch.sum` via the existing fallback. A separate schedule for the two-rank case, which accumulates through `addmm`'s C operand and removes the reduction kernel entirely, will follow in its own PR. Performance ----------- <img width="1853" height="810" alt="image" src="https://github.com/user-attachments/assets/981462f6-5403-43c0-8131-3fb849016ae4" /> GB300, bf16. `A` is `[M, K_per_rank]`, `B` is `[K_per_rank, N]`, output is `[M/TP, N]`. Per-rank GEMM throughput (`2 * M * N * K_per_rank / time`) in TFLOP/s, median of 15 timed rounds of 10 back-to-back calls after warmup, slowest rank. All figures are CUDA graph replay; the eager picture is discussed below. `unfused` is `torch.mm` followed by `dist.reduce_scatter_tensor`, and is included only as a reference point -- the change itself is `torch.sum` vs `Triton`. TP=4: | N | K_per_rank | M | unfused | torch.sum | Triton | Triton/sum | | --- | --- | --- | --- | --- | --- | --- | | 7168 | 2048 | 8192 | 770 | 880 | 987 | 1.12 | | 7168 | 2048 | 32768 | 850 | 1038 | 1150 | 1.11 | | 12288 | 4096 | 8192 | 1136 | 1251 | 1337 | 1.07 | | 12288 | 4096 | 32768 | 1238 | 1400 | 1496 | 1.07 | | 5120 | 1280 | 8192 | 557 | 539 | 594 | 1.10 | | 5120 | 1280 | 32768 | 622 | 747 | 848 | 1.13 | | 5120 | 6400 | 8192 | 1270 | 1425 | 1531 | 1.07 | | 5120 | 6400 | 32768 | 1332 | 1454 | 1516 | 1.04 | | 4096 | 2048 | 8192 | 728 | 726 | 825 | 1.14 | | 4096 | 2048 | 32768 | 816 | 1013 | 1115 | 1.10 | | 2048 | 1024 | 8192 | 334 | 298 | 304 | 1.02 | | 2048 | 1024 | 32768 | 490 | 521 | 588 | 1.13 | Over the full grid (6 shapes x M in {4096, 8192, 16384, 32768}) the Triton reducer is 1.02x to 1.14x, median 1.09x. TP=2, where the reduction is over only two partials and `torch.sum` is at its worst: | N | K_per_rank | M | unfused | torch.sum | Triton | Triton/sum | | --- | --- | --- | --- | --- | --- | --- | | 7168 | 4096 | 8192 | 1175 | 1219 | 1441 | 1.18 | | 7168 | 4096 | 32768 | 1231 | 1322 | 1555 | 1.18 | | 12288 | 8192 | 8192 | 1429 | 1424 | 1521 | 1.07 | | 12288 | 8192 | 32768 | 1455 | 1460 | 1560 | 1.07 | | 5120 | 2560 | 8192 | 903 | 973 | 1271 | 1.31 | | 5120 | 2560 | 32768 | 1015 | 931 | 1181 | 1.27 | | 5120 | 12800 | 8192 | 1488 | 1517 | 1538 | 1.01 | | 5120 | 12800 | 32768 | 1559 | 1567 | 1641 | 1.05 | | 4096 | 4096 | 8192 | 1106 | 1240 | 1510 | 1.22 | | 4096 | 4096 | 32768 | 1224 | 1178 | 1362 | 1.16 | | 2048 | 2048 | 8192 | 682 | 612 | 758 | 1.24 | | 2048 | 2048 | 32768 | 826 | 787 | 1010 | 1.28 | Same grid: 1.01x to 1.32x, median 1.19x. The gain tracks how much of the op the reduction is, so it is largest at small `K_per_rank` (thin GEMM, unchanged reduction) and smallest at large `K_per_rank`, where the GEMM dominates. For sizing the Triton dependency: measured in isolation the reducer is 2.0x to 3.2x faster than `torch.sum`, but the reduction is only 7-15% of the operator at these shapes, which is why the end-to-end numbers above are 1.0x-1.3x rather than 2x. Eager is a wash and can be slightly negative: 0.86x to 1.14x, median 0.97x at TP=4 and 1.05x at TP=2. The Triton kernel has the better GPU time but costs about 10 us more to launch than `torch.sum`, which is not recovered until capture removes launch cost. Gating the reducer on eager-vs-captured would lose more than it saves and is deliberately not done. Accuracy did not regress: relative L2 error against an fp32 reduce-scatter reference is 2.36e-3 before and after, since the reduction math is unchanged. Test plan: - `lintrunner -a -- torch/distributed/_symmetric_memory/__init__.py test/distributed/test_symmetric_memory.py` - `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter` - `python test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter_bfloat16_custom_reduce` (checks the eager result against the fallback and then captures the op in a CUDA graph and rechecks, since graph replay is the regime this targets) - `python test/test_reductions.py -k cuda` - Benchmark above via `torchrun --nproc_per_node={2,4}`, which also checks the reducer against an fp32 reduce-scatter reference This PR was authored with the help of an AI assistant. Co-authored-by: Codex <[email protected]> Co-authored-by: Claude <[email protected]> Pull Request resolved: pytorch#191803 Approved by: https://github.com/ngimel
Stack from ghstack (oldest at bottom):
Rework the reduction in
fused_matmul_reduce_scatterso it is no longer anexposed memory-bandwidth-bound kernel at the end of the op.
The existing implementation computes per-destination GEMM partials, exchanges
them with the all-to-all pipeline, then reduces with a final
torch.sum/torch.meanoverstacked_partials. That reduction is a separate kernel thatonly starts after the communication and GEMM work has completed, so all of its
memory traffic sits on the critical path. Splitting it into smaller adds
overlapped with the pipeline was also tried, and still left the reduction
reads/writes exposed.
This keeps the all-to-all pipeline and replaces that final
torch.sumwith asingle Triton kernel that reads the bf16/fp16 partials, accumulates in fp32
registers, and stores the low-precision output once. The contract is unchanged:
bf16 on the wire, fp32 accumulation, one rounding on store.
torch.sumis a poor fit here rather than merely unoptimized. On these shapes itreaches 23-43% of HBM peak against the Triton kernel's 82-88%, at identical DRAM
traffic, because ATen's
reduce_kernelis instruction-bound: for a reduction ofextent 2-8 it executes roughly ten times the instructions per output element,
most of them index arithmetic and configuration branches rather than the handful
of adds the reduction needs. That cost is structural to a kernel whose reduction
strategy lives in runtime data, so it is not reachable by tuning.
Scope stays narrow: low-precision output with the reduction over the leading
dim, which is the layout the all-to-all pipeline produces. Everything else,
including scaled matmul, keeps
torch.sumvia the existing fallback.A separate schedule for the two-rank case, which accumulates through
addmm's Coperand and removes the reduction kernel entirely, will follow in its own PR.
Performance
GB300, bf16.
Ais[M, K_per_rank],Bis[K_per_rank, N], output is[M/TP, N]. Per-rank GEMM throughput (2 * M * N * K_per_rank / time) inTFLOP/s, median of 15 timed rounds of 10 back-to-back calls after warmup,
slowest rank. All figures are CUDA graph replay; the eager picture is discussed
below.
unfusedistorch.mmfollowed bydist.reduce_scatter_tensor, and isincluded only as a reference point -- the change itself is
torch.sumvsTriton.TP=4:
Over the full grid (6 shapes x M in {4096, 8192, 16384, 32768}) the Triton
reducer is 1.02x to 1.14x, median 1.09x.
TP=2, where the reduction is over only two partials and
torch.sumis at itsworst:
Same grid: 1.01x to 1.32x, median 1.19x. The gain tracks how much of the op the
reduction is, so it is largest at small
K_per_rank(thin GEMM, unchangedreduction) and smallest at large
K_per_rank, where the GEMM dominates.For sizing the Triton dependency: measured in isolation the reducer is 2.0x to
3.2x faster than
torch.sum, but the reduction is only 7-15% of the operator atthese shapes, which is why the end-to-end numbers above are 1.0x-1.3x rather
than 2x.
Eager is a wash and can be slightly negative: 0.86x to 1.14x, median 0.97x at
TP=4 and 1.05x at TP=2. The Triton kernel has the better GPU time but costs
about 10 us more to launch than
torch.sum, which is not recovered untilcapture removes launch cost. Gating the reducer on eager-vs-captured would lose
more than it saves and is deliberately not done.
Accuracy did not regress: relative L2 error against an fp32 reduce-scatter
reference is 2.36e-3 before and after, since the reduction math is unchanged.
Test plan:
lintrunner -a -- torch/distributed/_symmetric_memory/__init__.py test/distributed/test_symmetric_memory.pypython test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatterpython test/distributed/test_symmetric_memory.py -k test_fused_matmul_reduce_scatter_bfloat16_custom_reduce(checks the eager result against the fallback and then captures the op in a
CUDA graph and rechecks, since graph replay is the regime this targets)
python test/test_reductions.py -k cudatorchrun --nproc_per_node={2,4}, which also checks thereducer against an fp32 reduce-scatter reference
This PR was authored with the help of an AI assistant.
Co-authored-by: Codex [email protected]
Co-authored-by: Claude [email protected]