Repository navigation
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/180430
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ❌ 3 New Failures, 6 Unrelated FailuresAs of commit fb6d054 with merge base 9ed7708 ( NEW FAILURES - The following jobs have failed:
UNSTABLE - The following jobs are marked as unstable, possibly due to flakiness on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
|
@haoyuz has exported this pull request. If you are a Meta employee, you can view the originating Diff in D100635529. |
…ensors (pytorch#180430) Summary: CONTEXT: On AMD MI350X, index_add_ with large source tensors and Zipf-distributed indices (common in MPZCH embedding tables) suffers from severe bf16 atomicAdd contention. The indexFuncLargeIndex kernel serializes on CAS-loop atomics when many threads target the same destination rows. WHAT: When running on HIP with alpha=1, dim=0, 2D tensors, and source.size(0) > 100K, redirect index_add_ to scatter_add_ which uses a different kernel path that avoids the atomicAdd contention bottleneck. Also adds a benchmark (bench_index_add_powerlaw.py) that reproduces the issue with Zipf-distributed indices at various alpha values and verifies correctness of the redirect. Test Plan: Benchmark on AMD MI350X (gfx950), dst=[634364, 128] bf16, src=[9039274, 128] bf16: | Distribution | index_add_ (before) | scatter_add_ (after) | Speedup | |------------------|---------------------|----------------------|---------| | Uniform | 7,673 us | 3,926 us | 2.0x | | Zipf alpha=0.5 | 8,034 us | 3,912 us | 2.1x | | Zipf alpha=1.0 | 68,996 us | 16,695 us | 4.1x | | Zipf alpha=1.5 | 397,525 us | 83,434 us | 4.8x | | Zipf alpha=2.0 | 689,997 us | 132,012 us | 5.2x | | Zipf alpha=2.5 | 889,804 us | 161,805 us | 5.5x | | Zipf alpha=3.0 | 1,021,968 us | 180,512 us | 5.7x | Zipf alpha=1.5 matches the ~420,000 us observed in the IG Threads LSR Slimper training trace. The redirect achieves 4.8x speedup for this case. buck run mode/opt-amd-gpu -m rocm70 //scripts/haoyuz/amd/triton/jfa:bench_index_add_powerlaw Differential Revision: D100635529
1e5c796 to
5b4504e
Compare
5b4504e to
7f248a0
Compare
…ensors (pytorch#180430) Summary: CONTEXT: On AMD MI350X, index_add_ with large source tensors and Zipf-distributed indices (common in MPZCH embedding tables) suffers from severe bf16 atomicAdd contention. The indexFuncLargeIndex kernel serializes on CAS-loop atomics when many threads target the same destination rows. WHAT: When running on HIP with alpha=1, dim=0, and 2D tensors, redirect index_add_ to scatter_add_, which uses a different kernel path that avoids the atomicAdd contention bottleneck. The redirect now fires unconditionally for the matching shape; an earlier revision gated it on `source.size(0) > 100000`, but a size sweep showed scatter_add_ is at parity with index_add_ at n=1K and strictly faster from n=5K onward, so the threshold was unnecessarily conservative. The redirect mirrors `index_add_cuda_impl`'s structured-kernel pre-copy: `if (!result.is_same(self)) result.copy_(self);` — required so that the out-of-place `torch.index_add(self, ...)` and `torch.index_add(self, ..., out=...)` paths (which share the same TORCH_IMPL_FUNC via structured_delegate) seed `result` before scatter_add_ accumulates into it. Also adds: - bench_index_add_powerlaw.py: reproduces the issue with Zipf-distributed indices at various alpha values, plus a size sweep that justifies removing the threshold. - test_index_add_redirect_kernel_selection_hip in test_indexing.py: verifies via torch.profiler that the redirect actually fires for matching cases and is bypassed for alpha != 1, dim != 0, 1D, and 3D inputs (addressing reviewer feedback that prior tests only covered numerical correctness, not selection). - test_index_add_correctness_2d_out_of_place: regression test for the structured-kernel pre-copy path (functional and explicit-out forms of torch.index_add). Test Plan: **Existing benchmark on AMD MI350X (gfx950), dst=[634364, 128] bf16, src=[9039274, 128] bf16:** | Distribution | index_add_ (before) | scatter_add_ (after) | Speedup | |------------------|---------------------|----------------------|---------| | Uniform | 7,673 us | 3,926 us | 2.0x | | Zipf alpha=0.5 | 8,034 us | 3,912 us | 2.1x | | Zipf alpha=1.0 | 68,996 us | 16,695 us | 4.1x | | Zipf alpha=1.5 | 397,525 us | 83,434 us | 4.8x | | Zipf alpha=2.0 | 689,997 us | 132,012 us | 5.2x | | Zipf alpha=2.5 | 889,804 us | 161,805 us | 5.5x | | Zipf alpha=3.0 | 1,021,968 us | 180,512 us | 5.7x | Zipf alpha=1.5 matches the ~420,000 us observed in the IG Threads LSR Slimper training trace. The redirect achieves 4.8x speedup for this case. **Threshold sweep on AMD MI350X (justifies removing the >100K gate). All bf16, dst=[634364, 128]:** Uniform indices: | src_size | index_add_ | scatter_add_ | Speedup | |---------:|-----------:|-------------:|--------:| | 1,000 | 21.6us | 21.5us | 1.00x | | 5,000 | 24.5us | 21.5us | 1.14x | | 10,000 | 28.4us | 22.6us | 1.26x | | 50,000 | 62.0us | 36.8us | 1.69x | | 100,000 | 105.0us | 55.5us | 1.89x | | 200,000 | 193.7us | 93.3us | 2.08x | |1,000,000 | 889.4us | 451.7us | 1.97x | |9,039,274 | 7,698.5us | 3,935.3us | 1.96x | Zipf alpha=1.5 (the contention regime that motivated this change): | src_size | index_add_ | scatter_add_ | Speedup | |---------:|-----------:|-------------:|--------:| | 1,000 | 61.5us | 25.1us | 2.45x | | 5,000 | 230.1us | 60.9us | 3.78x | | 10,000 | 457.0us | 109.4us | 4.18x | | 50,000 | 2,224.4us | 480.9us | 4.63x | | 100,000 | 4,397.2us | 937.2us | 4.69x | |9,039,274 |397,906.9us | 83,519.0us | 4.76x | Conclusion: scatter_add_ is at parity at the smallest measured size and strictly faster from 5K onward, on every distribution tested. Removing the size gate is a clear win. Bench command: ``` buck run mode/dev-nosan-amd-gpu -m rocm70 -m fbcode//triton/cc:force_noop //scripts/haoyuz/amd/index_add:bench_index_add_powerlaw ``` **Numerical correctness on AMD MI350X (//scripts/haoyuz/amd/index_add:test_index_add_redirect, all 7 PASS):** ``` test_unique_indices_fp32 ... ok (bit-exact) test_unique_indices_bf16 ... ok (no contention -> close to f32 ref) test_small_exact_bf16 ... ok (small int inputs -> both paths exact in bf16) test_duplicates_fp32_exact ... ok (deterministic atomicAdd in fp32) test_contention_bf16_relative_error ... ok (L2 rel err < 0.01) test_high_contention_zipf_bf16 ... ok (L2 rel err < 0.5 even with 76K dupes) test_redirect_kernel_selection_via_profiler ... ok (profiler confirms scatter_add_ fires only for matching cases) ``` **test_indexing.py changes:** - `test_index_add_correctness_2d_large` (fp32 + bf16 + fp16): Compares the redirected path to a CPU f32 reference for a 200K x 64 case with duplicate indices. Uses L2 relative error for low-precision dtypes (worst-case per-element error from ~200 atomic accumulations is too large for max-abs). - `test_index_add_correctness_2d_out_of_place`: Regression test for the structured-kernel pre-copy. Exercises both `torch.index_add(self, ...)` (functional) and `torch.index_add(self, ..., out=...)` paths, which would silently produce garbage if the redirect skipped the `result.copy_(self)` seeding step. - `test_index_add_correctness_2d_alpha`: Validates alpha != 1 path correctness (which always bypasses the redirect). - `test_index_add_redirect_kernel_selection_hip`: HIP-only profiler-based test that asserts (a) redirect fires when alpha=1, dim=0, 2D, and (b) is bypassed for alpha != 1, dim != 0, 1D, and 3D inputs. This addresses reviewer feedback that previous tests only checked numerical correctness, not which kernel was selected. **AI review fixes incorporated in this revision:** - BUG (critical): Out-of-place `torch.index_add(self, ...)` and `torch.index_add(..., out=)` reach the same TORCH_IMPL_FUNC via `structured_delegate` and start with an uninitialized `result`. The redirect was missing the `result.copy_(self)` seeding that the legacy `index_add_cuda_impl` does at the top of its body — fixed by mirroring that pre-copy. - Test gap: added `test_index_add_correctness_2d_out_of_place` regression for the above. - Test fix: `test_small_exact_bf16` previously computed `dst_before` (bypass) but never asserted on it; added assertion. - Test stability: `test_index_add_correctness_2d_large` used `atol=1e-1` per-element max for bf16, which is borderline tight for 200 dupes/row; switched to L2 relative error (< 1e-2). - Test guard: standalone `test_index_add_redirect.py` is now class-level HIP-only (numerical comparisons on NVIDIA exercise the same kernel for both paths, providing no real coverage). Co-authored-by: Claude Reviewed By: echen4096 Differential Revision: D100635529
7f248a0 to
25d4a5b
Compare
…ensors (pytorch#180430) Summary: CONTEXT: On AMD MI350X, index_add_ with large source tensors and Zipf-distributed indices (common in MPZCH embedding tables) suffers from severe bf16 atomicAdd contention. The indexFuncLargeIndex kernel serializes on CAS-loop atomics when many threads target the same destination rows. WHAT: When running on HIP with alpha=1, dim=0, and 2D tensors, redirect index_add_ to scatter_add_, which uses a different kernel path that avoids the atomicAdd contention bottleneck. The redirect now fires unconditionally for the matching shape; an earlier revision gated it on `source.size(0) > 100000`, but a size sweep showed scatter_add_ is at parity with index_add_ at n=1K and strictly faster from n=5K onward, so the threshold was unnecessarily conservative. The redirect mirrors `index_add_cuda_impl`'s structured-kernel pre-copy: `if (!result.is_same(self)) result.copy_(self);` — required so that the out-of-place `torch.index_add(self, ...)` and `torch.index_add(self, ..., out=...)` paths (which share the same TORCH_IMPL_FUNC via structured_delegate) seed `result` before scatter_add_ accumulates into it. Also adds: - bench_index_add_powerlaw.py: reproduces the issue with Zipf-distributed indices at various alpha values, plus a size sweep that justifies removing the threshold. - test_index_add_redirect_kernel_selection_hip in test_indexing.py: verifies via torch.profiler that the redirect actually fires for matching cases and is bypassed for alpha != 1, dim != 0, 1D, and 3D inputs (addressing reviewer feedback that prior tests only covered numerical correctness, not selection). - test_index_add_correctness_2d_out_of_place: regression test for the structured-kernel pre-copy path (functional and explicit-out forms of torch.index_add). Test Plan: **Existing benchmark on AMD MI350X (gfx950), dst=[634364, 128] bf16, src=[9039274, 128] bf16:** | Distribution | index_add_ (before) | scatter_add_ (after) | Speedup | |------------------|---------------------|----------------------|---------| | Uniform | 7,673 us | 3,926 us | 2.0x | | Zipf alpha=0.5 | 8,034 us | 3,912 us | 2.1x | | Zipf alpha=1.0 | 68,996 us | 16,695 us | 4.1x | | Zipf alpha=1.5 | 397,525 us | 83,434 us | 4.8x | | Zipf alpha=2.0 | 689,997 us | 132,012 us | 5.2x | | Zipf alpha=2.5 | 889,804 us | 161,805 us | 5.5x | | Zipf alpha=3.0 | 1,021,968 us | 180,512 us | 5.7x | Zipf alpha=1.5 matches the ~420,000 us observed in the IG Threads LSR Slimper training trace. The redirect achieves 4.8x speedup for this case. **Threshold sweep on AMD MI350X (justifies removing the >100K gate). All bf16, dst=[634364, 128]:** Uniform indices: | src_size | index_add_ | scatter_add_ | Speedup | |---------:|-----------:|-------------:|--------:| | 1,000 | 21.6us | 21.5us | 1.00x | | 5,000 | 24.5us | 21.5us | 1.14x | | 10,000 | 28.4us | 22.6us | 1.26x | | 50,000 | 62.0us | 36.8us | 1.69x | | 100,000 | 105.0us | 55.5us | 1.89x | | 200,000 | 193.7us | 93.3us | 2.08x | |1,000,000 | 889.4us | 451.7us | 1.97x | |9,039,274 | 7,698.5us | 3,935.3us | 1.96x | Zipf alpha=1.5 (the contention regime that motivated this change): | src_size | index_add_ | scatter_add_ | Speedup | |---------:|-----------:|-------------:|--------:| | 1,000 | 61.5us | 25.1us | 2.45x | | 5,000 | 230.1us | 60.9us | 3.78x | | 10,000 | 457.0us | 109.4us | 4.18x | | 50,000 | 2,224.4us | 480.9us | 4.63x | | 100,000 | 4,397.2us | 937.2us | 4.69x | |9,039,274 |397,906.9us | 83,519.0us | 4.76x | Conclusion: scatter_add_ is at parity at the smallest measured size and strictly faster from 5K onward, on every distribution tested. Removing the size gate is a clear win. Bench command: ``` buck run mode/dev-nosan-amd-gpu -m rocm70 -m fbcode//triton/cc:force_noop //scripts/haoyuz/amd/index_add:bench_index_add_powerlaw ``` **Numerical correctness on AMD MI350X (//scripts/haoyuz/amd/index_add:test_index_add_redirect, all 7 PASS):** ``` test_unique_indices_fp32 ... ok (bit-exact) test_unique_indices_bf16 ... ok (no contention -> close to f32 ref) test_small_exact_bf16 ... ok (small int inputs -> both paths exact in bf16) test_duplicates_fp32_exact ... ok (deterministic atomicAdd in fp32) test_contention_bf16_relative_error ... ok (L2 rel err < 0.01) test_high_contention_zipf_bf16 ... ok (L2 rel err < 0.5 even with 76K dupes) test_redirect_kernel_selection_via_profiler ... ok (profiler confirms scatter_add_ fires only for matching cases) ``` **test_indexing.py changes:** - `test_index_add_correctness_2d_large` (fp32 + bf16 + fp16): Compares the redirected path to a CPU f32 reference for a 200K x 64 case with duplicate indices. Uses L2 relative error for low-precision dtypes (worst-case per-element error from ~200 atomic accumulations is too large for max-abs). - `test_index_add_correctness_2d_out_of_place`: Regression test for the structured-kernel pre-copy. Exercises both `torch.index_add(self, ...)` (functional) and `torch.index_add(self, ..., out=...)` paths, which would silently produce garbage if the redirect skipped the `result.copy_(self)` seeding step. - `test_index_add_correctness_2d_alpha`: Validates alpha != 1 path correctness (which always bypasses the redirect). - `test_index_add_redirect_kernel_selection_hip`: HIP-only profiler-based test that asserts (a) redirect fires when alpha=1, dim=0, 2D, and (b) is bypassed for alpha != 1, dim != 0, 1D, and 3D inputs. This addresses reviewer feedback that previous tests only checked numerical correctness, not which kernel was selected. **AI review fixes incorporated in this revision:** - BUG (critical): Out-of-place `torch.index_add(self, ...)` and `torch.index_add(..., out=)` reach the same TORCH_IMPL_FUNC via `structured_delegate` and start with an uninitialized `result`. The redirect was missing the `result.copy_(self)` seeding that the legacy `index_add_cuda_impl` does at the top of its body — fixed by mirroring that pre-copy. - Test gap: added `test_index_add_correctness_2d_out_of_place` regression for the above. - Test fix: `test_small_exact_bf16` previously computed `dst_before` (bypass) but never asserted on it; added assertion. - Test stability: `test_index_add_correctness_2d_large` used `atol=1e-1` per-element max for bf16, which is borderline tight for 200 dupes/row; switched to L2 relative error (< 1e-2). - Test guard: standalone `test_index_add_redirect.py` is now class-level HIP-only (numerical comparisons on NVIDIA exercise the same kernel for both paths, providing no real coverage). Co-authored-by: Claude Reviewed By: echen4096 Differential Revision: D100635529
…ensors (pytorch#180430) Summary: CONTEXT: On AMD MI350X, index_add_ with large source tensors and Zipf-distributed indices (common in MPZCH embedding tables) suffers from severe bf16 atomicAdd contention. The indexFuncLargeIndex kernel serializes on CAS-loop atomics when many threads target the same destination rows. WHAT: When running on HIP with alpha=1, dim=0, and 2D tensors, redirect index_add_ to scatter_add_, which uses a different kernel path that avoids the atomicAdd contention bottleneck. The redirect now fires unconditionally for the matching shape; an earlier revision gated it on `source.size(0) > 100000`, but a size sweep showed scatter_add_ is at parity with index_add_ at n=1K and strictly faster from n=5K onward, so the threshold was unnecessarily conservative. The redirect mirrors `index_add_cuda_impl`'s structured-kernel pre-copy: `if (!result.is_same(self)) result.copy_(self);` — required so that the out-of-place `torch.index_add(self, ...)` and `torch.index_add(self, ..., out=...)` paths (which share the same TORCH_IMPL_FUNC via structured_delegate) seed `result` before scatter_add_ accumulates into it. Also adds: - bench_index_add_powerlaw.py: reproduces the issue with Zipf-distributed indices at various alpha values, plus a size sweep that justifies removing the threshold. - test_index_add_redirect_kernel_selection_hip in test_indexing.py: verifies via torch.profiler that the redirect actually fires for matching cases and is bypassed for alpha != 1, dim != 0, 1D, and 3D inputs (addressing reviewer feedback that prior tests only covered numerical correctness, not selection). - test_index_add_correctness_2d_out_of_place: regression test for the structured-kernel pre-copy path (functional and explicit-out forms of torch.index_add). Test Plan: **Existing benchmark on AMD MI350X (gfx950), dst=[634364, 128] bf16, src=[9039274, 128] bf16:** | Distribution | index_add_ (before) | scatter_add_ (after) | Speedup | |------------------|---------------------|----------------------|---------| | Uniform | 7,673 us | 3,926 us | 2.0x | | Zipf alpha=0.5 | 8,034 us | 3,912 us | 2.1x | | Zipf alpha=1.0 | 68,996 us | 16,695 us | 4.1x | | Zipf alpha=1.5 | 397,525 us | 83,434 us | 4.8x | | Zipf alpha=2.0 | 689,997 us | 132,012 us | 5.2x | | Zipf alpha=2.5 | 889,804 us | 161,805 us | 5.5x | | Zipf alpha=3.0 | 1,021,968 us | 180,512 us | 5.7x | Zipf alpha=1.5 matches the ~420,000 us observed in the IG Threads LSR Slimper training trace. The redirect achieves 4.8x speedup for this case. **Threshold sweep on AMD MI350X (justifies removing the >100K gate). All bf16, dst=[634364, 128]:** Uniform indices: | src_size | index_add_ | scatter_add_ | Speedup | |---------:|-----------:|-------------:|--------:| | 1,000 | 21.6us | 21.5us | 1.00x | | 5,000 | 24.5us | 21.5us | 1.14x | | 10,000 | 28.4us | 22.6us | 1.26x | | 50,000 | 62.0us | 36.8us | 1.69x | | 100,000 | 105.0us | 55.5us | 1.89x | | 200,000 | 193.7us | 93.3us | 2.08x | |1,000,000 | 889.4us | 451.7us | 1.97x | |9,039,274 | 7,698.5us | 3,935.3us | 1.96x | Zipf alpha=1.5 (the contention regime that motivated this change): | src_size | index_add_ | scatter_add_ | Speedup | |---------:|-----------:|-------------:|--------:| | 1,000 | 61.5us | 25.1us | 2.45x | | 5,000 | 230.1us | 60.9us | 3.78x | | 10,000 | 457.0us | 109.4us | 4.18x | | 50,000 | 2,224.4us | 480.9us | 4.63x | | 100,000 | 4,397.2us | 937.2us | 4.69x | |9,039,274 |397,906.9us | 83,519.0us | 4.76x | Conclusion: scatter_add_ is at parity at the smallest measured size and strictly faster from 5K onward, on every distribution tested. Removing the size gate is a clear win. Bench command: ``` buck run mode/dev-nosan-amd-gpu -m rocm70 -m fbcode//triton/cc:force_noop //scripts/haoyuz/amd/index_add:bench_index_add_powerlaw ``` **Numerical correctness on AMD MI350X (//scripts/haoyuz/amd/index_add:test_index_add_redirect, all 7 PASS):** ``` test_unique_indices_fp32 ... ok (bit-exact) test_unique_indices_bf16 ... ok (no contention -> close to f32 ref) test_small_exact_bf16 ... ok (small int inputs -> both paths exact in bf16) test_duplicates_fp32_exact ... ok (deterministic atomicAdd in fp32) test_contention_bf16_relative_error ... ok (L2 rel err < 0.01) test_high_contention_zipf_bf16 ... ok (L2 rel err < 0.5 even with 76K dupes) test_redirect_kernel_selection_via_profiler ... ok (profiler confirms scatter_add_ fires only for matching cases) ``` **test_indexing.py changes:** - `test_index_add_correctness_2d_large` (fp32 + bf16 + fp16): Compares the redirected path to a CPU f32 reference for a 200K x 64 case with duplicate indices. Uses L2 relative error for low-precision dtypes (worst-case per-element error from ~200 atomic accumulations is too large for max-abs). - `test_index_add_correctness_2d_out_of_place`: Regression test for the structured-kernel pre-copy. Exercises both `torch.index_add(self, ...)` (functional) and `torch.index_add(self, ..., out=...)` paths, which would silently produce garbage if the redirect skipped the `result.copy_(self)` seeding step. - `test_index_add_correctness_2d_alpha`: Validates alpha != 1 path correctness (which always bypasses the redirect). - `test_index_add_redirect_kernel_selection_hip`: HIP-only profiler-based test that asserts (a) redirect fires when alpha=1, dim=0, 2D, and (b) is bypassed for alpha != 1, dim != 0, 1D, and 3D inputs. This addresses reviewer feedback that previous tests only checked numerical correctness, not which kernel was selected. **AI review fixes incorporated in this revision:** - BUG (critical): Out-of-place `torch.index_add(self, ...)` and `torch.index_add(..., out=)` reach the same TORCH_IMPL_FUNC via `structured_delegate` and start with an uninitialized `result`. The redirect was missing the `result.copy_(self)` seeding that the legacy `index_add_cuda_impl` does at the top of its body — fixed by mirroring that pre-copy. - Test gap: added `test_index_add_correctness_2d_out_of_place` regression for the above. - Test fix: `test_small_exact_bf16` previously computed `dst_before` (bypass) but never asserted on it; added assertion. - Test stability: `test_index_add_correctness_2d_large` used `atol=1e-1` per-element max for bf16, which is borderline tight for 200 dupes/row; switched to L2 relative error (< 1e-2). - Test guard: standalone `test_index_add_redirect.py` is now class-level HIP-only (numerical comparisons on NVIDIA exercise the same kernel for both paths, providing no real coverage). Co-authored-by: Claude Reviewed By: echen4096 Differential Revision: D100635529
25d4a5b to
fb6d054
Compare
@jeffdaily The benchmarks (before or after) were done without the PR included, so it should be independent from that PR. |
|
@pytorchbot merge -i (Initiating merge automatically since Phabricator Diff has merged, merging with -i because oss signals were bypassed internally) |
Merge failedReason: This PR needs a If not, please add the To add a label, you can comment to pytorchbot, for example For more information, see Details for Dev Infra teamRaised by workflow job |
This PR needs a
|
|
@pytorchbot merge -i (Initiating merge automatically since Phabricator Diff has merged, merging with -i because oss signals were bypassed internally) |
Merge failedReason: This PR needs a If not, please add the To add a label, you can comment to pytorchbot, for example For more information, see Details for Dev Infra teamRaised by workflow job |
|
@pytorchbot merge -i (Initiating merge automatically since Phabricator Diff has merged, merging with -i because oss signals were bypassed internally) |
Merge failedReason: This PR has internal changes and must be landed via Phabricator! Please try reimporting/rexporting the PR! Details for Dev Infra teamRaised by workflow job |
|
@pytorchbot merge -i (Initiating merge automatically since Phabricator Diff has merged, merging with -i because oss signals were bypassed internally) |
Merge failedReason: This PR has internal changes and must be landed via Phabricator! Please try reimporting/rexporting the PR! Details for Dev Infra teamRaised by workflow job |
@ngimel PR #146264 did the original optimization for |
… large tensors Summary: CONTEXT: Reland of D100635529 (pytorch#180430), which was reverted in D103039749 because the new bf16 path of `test_index_add_correctness_2d_out_of_place` failed on NVIDIA OSS CI. ngimel pointed out that with `num_dest=200, num_src=5000` (~25 atomic adds per destination row), the per-element rounding from NVIDIA's `index_add_` kernel exceeds the original `atol=5e-2` max-abs tolerance — exactly the rounding behavior expected for bf16 atomics under contention. Local repro on H100 confirmed the same failure (max abs diff ~0.39 vs 0.05 allowed) and showed that two more nearby tests (`test_index_add_correctness_2d_large` and `test_index_add_correctness_2d_alpha`) had similarly tight tolerances that fail on NVIDIA when the high-contention atomic-add ordering diverges from the CPU sequential reference. WHAT: All AMD-side changes from D100635529 are restored verbatim — the HIP-only `index_add_ → scatter_add_` redirect in `Indexing.cu` and the new `scripts/haoyuz/amd/index_add/` benchmark + standalone test. The only difference vs the reverted version is in the three new tests in `caffe2/test/test_indexing.py` that compare a high-contention GPU result against a CPU fp32 sequential reference. Per-element max-abs/rel tolerances are replaced with bounds that reflect the actual atomic-add accumulation noise: bf16/fp16 use L2 relative error with a 5e-2 ceiling (measured 0.017 worst-case), and fp32 uses atol=rtol=1e-3 (measured 1.3e-5 to 1.8e-4 worst-case). The HIP redirect itself is unchanged and the AMD MI350X performance numbers (4.8x speedup on Zipf alpha=1.5) still hold. Co-authored-by: Claude Test Plan: On NVIDIA H100 (devvm2464), all six new index_add CUDA tests pass, and the broader `TestIndexing` suite is clean: ``` PYTORCH_TEST_REMOTE_GPU=1 .../test_others_cuda.par -r 'index_add_correctness' test_index_add_correctness_2d_alpha_cuda ... ok test_index_add_correctness_2d_large_cuda_bfloat16 ... ok test_index_add_correctness_2d_large_cuda_float16 ... ok test_index_add_correctness_2d_large_cuda_float32 ... ok test_index_add_correctness_2d_out_of_place_cuda_bfloat16 ... ok test_index_add_correctness_2d_out_of_place_cuda_float32 ... ok Ran 6 tests in 0.139s PYTORCH_TEST_REMOTE_GPU=1 .../test_others_cuda.par -r 'TestIndexing' Ran 176 tests in 8.290s OK (skipped=1) ``` Confirmed the fix actually addresses the original failure: with the prior tolerances re-applied, `test_index_add_correctness_2d_out_of_place_cuda_bfloat16` reproduces the exact OSS CI failure on H100 (Mismatched elements: 54 / 6400, greatest absolute difference: 0.3878 vs 0.05 allowed). The pre-existing AMD MI350X benchmarks and standalone tests from D100635529 are unchanged. Reverted-by: D103039749 Differential Revision: D103100245
… large tensors Summary: CONTEXT: Reland of D100635529 (pytorch#180430), which was reverted in D103039749 because the new bf16 path of `test_index_add_correctness_2d_out_of_place` failed on NVIDIA OSS CI. ngimel pointed out that with `num_dest=200, num_src=5000` (~25 atomic adds per destination row), the per-element rounding from NVIDIA's `index_add_` kernel exceeds the original `atol=5e-2` max-abs tolerance — exactly the rounding behavior expected for bf16 atomics under contention. Local repro on H100 confirmed the same failure (max abs diff ~0.39 vs 0.05 allowed) and showed that two more nearby tests (`test_index_add_correctness_2d_large` and `test_index_add_correctness_2d_alpha`) had similarly tight tolerances that fail on NVIDIA when the high-contention atomic-add ordering diverges from the CPU sequential reference. WHAT: All AMD-side changes from D100635529 are restored verbatim — the HIP-only `index_add_ → scatter_add_` redirect in `Indexing.cu` and the new `scripts/haoyuz/amd/index_add/` benchmark + standalone test. The only difference vs the reverted version is in the three new tests in `caffe2/test/test_indexing.py` that compare a high-contention GPU result against a CPU fp32 sequential reference. Per-element max-abs/rel tolerances are replaced with bounds that reflect the actual atomic-add accumulation noise: bf16/fp16 use L2 relative error with a 5e-2 ceiling (measured 0.017 worst-case), and fp32 uses atol=rtol=1e-3 (measured 1.3e-5 to 1.8e-4 worst-case). The HIP redirect itself is unchanged and the AMD MI350X performance numbers (4.8x speedup on Zipf alpha=1.5) still hold. Co-authored-by: Claude Test Plan: On NVIDIA H100 (devvm2464), all six new index_add CUDA tests pass, and the broader `TestIndexing` suite is clean: ``` PYTORCH_TEST_REMOTE_GPU=1 .../test_others_cuda.par -r 'index_add_correctness' test_index_add_correctness_2d_alpha_cuda ... ok test_index_add_correctness_2d_large_cuda_bfloat16 ... ok test_index_add_correctness_2d_large_cuda_float16 ... ok test_index_add_correctness_2d_large_cuda_float32 ... ok test_index_add_correctness_2d_out_of_place_cuda_bfloat16 ... ok test_index_add_correctness_2d_out_of_place_cuda_float32 ... ok Ran 6 tests in 0.139s PYTORCH_TEST_REMOTE_GPU=1 .../test_others_cuda.par -r 'TestIndexing' Ran 176 tests in 8.290s OK (skipped=1) ``` Confirmed the fix actually addresses the original failure: with the prior tolerances re-applied, `test_index_add_correctness_2d_out_of_place_cuda_bfloat16` reproduces the exact OSS CI failure on H100 (Mismatched elements: 54 / 6400, greatest absolute difference: 0.3878 vs 0.05 allowed). The pre-existing AMD MI350X benchmarks and standalone tests from D100635529 are unchanged. Reverted-by: D103039749 Differential Revision: D103100245
… large tensors (pytorch#181955) Summary: Reland of D100635529 (pytorch#180430), which was reverted in D103039749 because the new bf16 path of `test_index_add_correctness_2d_out_of_place` failed on NVIDIA OSS CI. ngimel pointed out that with `num_dest=200, num_src=5000` (~25 atomic adds per destination row), the per-element rounding from NVIDIA's `index_add_` kernel exceeds the original `atol=5e-2` max-abs tolerance — exactly the rounding behavior expected for bf16 atomics under contention. Local repro on H100 confirmed the same failure (max abs diff ~0.39 vs 0.05 allowed) and showed that two more nearby tests (`test_index_add_correctness_2d_large` and `test_index_add_correctness_2d_alpha`) had similarly tight tolerances that fail on NVIDIA when the high-contention atomic-add ordering diverges from the CPU sequential reference. All AMD-side changes from D100635529 are restored — the HIP-only `index_add_ → scatter_add_` redirect in `Indexing.cu` and the new `scripts/haoyuz/amd/index_add/` benchmark + standalone test. The only difference vs the reverted version is in the three new tests in `caffe2/test/test_indexing.py` that compare a high-contention GPU result against a CPU fp32 sequential reference. Per-element max-abs/rel tolerances are replaced with bounds that reflect the actual atomic-add accumulation noise: bf16/fp16 use L2 relative error with a 5e-2 ceiling (measured 0.017 worst-case), and fp32 uses atol=rtol=1e-3 (measured 1.3e-5 to 1.8e-4 worst-case). The HIP redirect itself is unchanged and the AMD MI350X performance numbers (4.8x speedup on Zipf alpha=1.5) still hold. [PR pytorch#146264](pytorch#146264) has more details on `scatter_add` optimizations for AMD MI300 / MI350 GPUs. Co-authored-by: Claude Test Plan: On NVIDIA H100 (devvm2464), all six new index_add CUDA tests pass, and the broader `TestIndexing` suite is clean: ``` PYTORCH_TEST_REMOTE_GPU=1 .../test_others_cuda.par -r 'index_add_correctness' test_index_add_correctness_2d_alpha_cuda ... ok test_index_add_correctness_2d_large_cuda_bfloat16 ... ok test_index_add_correctness_2d_large_cuda_float16 ... ok test_index_add_correctness_2d_large_cuda_float32 ... ok test_index_add_correctness_2d_out_of_place_cuda_bfloat16 ... ok test_index_add_correctness_2d_out_of_place_cuda_float32 ... ok Ran 6 tests in 0.139s PYTORCH_TEST_REMOTE_GPU=1 .../test_others_cuda.par -r 'TestIndexing' Ran 176 tests in 8.290s OK (skipped=1) ``` Confirmed the fix actually addresses the original failure: with the prior tolerances re-applied, `test_index_add_correctness_2d_out_of_place_cuda_bfloat16` reproduces the exact OSS CI failure on H100 (Mismatched elements: 54 / 6400, greatest absolute difference: 0.3878 vs 0.05 allowed). The pre-existing AMD MI350X benchmarks and standalone tests from D100635529 are unchanged. Reverted-by: D103039749 Reviewed By: echen4096 Differential Revision: D103100245
… large tensors (#181955) (#181955) Summary: Reland of D100635529 (#180430), which was reverted in D103039749 because the new bf16 path of `test_index_add_correctness_2d_out_of_place` failed on NVIDIA OSS CI. ngimel pointed out that with `num_dest=200, num_src=5000` (~25 atomic adds per destination row), the per-element rounding from NVIDIA's `index_add_` kernel exceeds the original `atol=5e-2` max-abs tolerance — exactly the rounding behavior expected for bf16 atomics under contention. Local repro on H100 confirmed the same failure (max abs diff ~0.39 vs 0.05 allowed) and showed that two more nearby tests (`test_index_add_correctness_2d_large` and `test_index_add_correctness_2d_alpha`) had similarly tight tolerances that fail on NVIDIA when the high-contention atomic-add ordering diverges from the CPU sequential reference. All AMD-side changes from D100635529 are restored — the HIP-only `index_add_ → scatter_add_` redirect in `Indexing.cu` and the new `scripts/haoyuz/amd/index_add/` benchmark + standalone test. The only difference vs the reverted version is in the three new tests in `caffe2/test/test_indexing.py` that compare a high-contention GPU result against a CPU fp32 sequential reference. Per-element max-abs/rel tolerances are replaced with bounds that reflect the actual atomic-add accumulation noise: bf16/fp16 use L2 relative error with a 5e-2 ceiling (measured 0.017 worst-case), and fp32 uses atol=rtol=1e-3 (measured 1.3e-5 to 1.8e-4 worst-case). The HIP redirect itself is unchanged and the AMD MI350X performance numbers (4.8x speedup on Zipf alpha=1.5) still hold. [PR #146264](#146264) has more details on `scatter_add` optimizations for AMD MI300 / MI350 GPUs. Co-authored-by: Claude Test Plan: On NVIDIA H100 (devvm2464), all six new index_add CUDA tests pass, and the broader `TestIndexing` suite is clean: ``` PYTORCH_TEST_REMOTE_GPU=1 .../test_others_cuda.par -r 'index_add_correctness' test_index_add_correctness_2d_alpha_cuda ... ok test_index_add_correctness_2d_large_cuda_bfloat16 ... ok test_index_add_correctness_2d_large_cuda_float16 ... ok test_index_add_correctness_2d_large_cuda_float32 ... ok test_index_add_correctness_2d_out_of_place_cuda_bfloat16 ... ok test_index_add_correctness_2d_out_of_place_cuda_float32 ... ok Ran 6 tests in 0.139s PYTORCH_TEST_REMOTE_GPU=1 .../test_others_cuda.par -r 'TestIndexing' Ran 176 tests in 8.290s OK (skipped=1) ``` Confirmed the fix actually addresses the original failure: with the prior tolerances re-applied, `test_index_add_correctness_2d_out_of_place_cuda_bfloat16` reproduces the exact OSS CI failure on H100 (Mismatched elements: 54 / 6400, greatest absolute difference: 0.3878 vs 0.05 allowed). The pre-existing AMD MI350X benchmarks and standalone tests from D100635529 are unchanged. Reverted-by: D103039749 Reviewed By: echen4096 Differential Revision: D103100245 Pull Request resolved: #181955 Approved by: https://github.com/echen4096
… large tensors (pytorch#180430) (pytorch#180430)" This reverts commit 2deace1. Reverted pytorch#180430 on behalf of https://github.com/albanD due to The unstable job hid the failure. But you can see drci above for real cuda errors on index_add_ ([comment](pytorch#180430 (comment)))
… large tensors (pytorch#181955) (pytorch#181955) Summary: Reland of D100635529 (pytorch#180430), which was reverted in D103039749 because the new bf16 path of `test_index_add_correctness_2d_out_of_place` failed on NVIDIA OSS CI. ngimel pointed out that with `num_dest=200, num_src=5000` (~25 atomic adds per destination row), the per-element rounding from NVIDIA's `index_add_` kernel exceeds the original `atol=5e-2` max-abs tolerance — exactly the rounding behavior expected for bf16 atomics under contention. Local repro on H100 confirmed the same failure (max abs diff ~0.39 vs 0.05 allowed) and showed that two more nearby tests (`test_index_add_correctness_2d_large` and `test_index_add_correctness_2d_alpha`) had similarly tight tolerances that fail on NVIDIA when the high-contention atomic-add ordering diverges from the CPU sequential reference. All AMD-side changes from D100635529 are restored — the HIP-only `index_add_ → scatter_add_` redirect in `Indexing.cu` and the new `scripts/haoyuz/amd/index_add/` benchmark + standalone test. The only difference vs the reverted version is in the three new tests in `caffe2/test/test_indexing.py` that compare a high-contention GPU result against a CPU fp32 sequential reference. Per-element max-abs/rel tolerances are replaced with bounds that reflect the actual atomic-add accumulation noise: bf16/fp16 use L2 relative error with a 5e-2 ceiling (measured 0.017 worst-case), and fp32 uses atol=rtol=1e-3 (measured 1.3e-5 to 1.8e-4 worst-case). The HIP redirect itself is unchanged and the AMD MI350X performance numbers (4.8x speedup on Zipf alpha=1.5) still hold. [PR pytorch#146264](pytorch#146264) has more details on `scatter_add` optimizations for AMD MI300 / MI350 GPUs. Co-authored-by: Claude Test Plan: On NVIDIA H100 (devvm2464), all six new index_add CUDA tests pass, and the broader `TestIndexing` suite is clean: ``` PYTORCH_TEST_REMOTE_GPU=1 .../test_others_cuda.par -r 'index_add_correctness' test_index_add_correctness_2d_alpha_cuda ... ok test_index_add_correctness_2d_large_cuda_bfloat16 ... ok test_index_add_correctness_2d_large_cuda_float16 ... ok test_index_add_correctness_2d_large_cuda_float32 ... ok test_index_add_correctness_2d_out_of_place_cuda_bfloat16 ... ok test_index_add_correctness_2d_out_of_place_cuda_float32 ... ok Ran 6 tests in 0.139s PYTORCH_TEST_REMOTE_GPU=1 .../test_others_cuda.par -r 'TestIndexing' Ran 176 tests in 8.290s OK (skipped=1) ``` Confirmed the fix actually addresses the original failure: with the prior tolerances re-applied, `test_index_add_correctness_2d_out_of_place_cuda_bfloat16` reproduces the exact OSS CI failure on H100 (Mismatched elements: 54 / 6400, greatest absolute difference: 0.3878 vs 0.05 allowed). The pre-existing AMD MI350X benchmarks and standalone tests from D100635529 are unchanged. Reverted-by: D103039749 Reviewed By: echen4096 Differential Revision: D103100245 Pull Request resolved: pytorch#181955 Approved by: https://github.com/echen4096
…ensors (pytorch#180430) (pytorch#180430) Summary: CONTEXT: On AMD MI350X, index_add_ with large source tensors and Zipf-distributed indices (common in MPZCH embedding tables) suffers from severe bf16 atomicAdd contention. The indexFuncLargeIndex kernel serializes on CAS-loop atomics when many threads target the same destination rows. WHAT: When running on HIP with alpha=1, dim=0, and 2D tensors, redirect index_add_ to scatter_add_, which uses a different kernel path that avoids the atomicAdd contention bottleneck. The redirect now fires unconditionally for the matching shape; an earlier revision gated it on `source.size(0) > 100000`, but a size sweep showed scatter_add_ is at parity with index_add_ at n=1K and strictly faster from n=5K onward, so the threshold was unnecessarily conservative. The redirect mirrors `index_add_cuda_impl`'s structured-kernel pre-copy: `if (!result.is_same(self)) result.copy_(self);` — required so that the out-of-place `torch.index_add(self, ...)` and `torch.index_add(self, ..., out=...)` paths (which share the same TORCH_IMPL_FUNC via structured_delegate) seed `result` before scatter_add_ accumulates into it. Also adds: - bench_index_add_powerlaw.py: reproduces the issue with Zipf-distributed indices at various alpha values, plus a size sweep that justifies removing the threshold. - test_index_add_redirect_kernel_selection_hip in test_indexing.py: verifies via torch.profiler that the redirect actually fires for matching cases and is bypassed for alpha != 1, dim != 0, 1D, and 3D inputs (addressing reviewer feedback that prior tests only covered numerical correctness, not selection). - test_index_add_correctness_2d_out_of_place: regression test for the structured-kernel pre-copy path (functional and explicit-out forms of torch.index_add). Test Plan: **Existing benchmark on AMD MI350X (gfx950), dst=[634364, 128] bf16, src=[9039274, 128] bf16:** | Distribution | index_add_ (before) | scatter_add_ (after) | Speedup | |------------------|---------------------|----------------------|---------| | Uniform | 7,673 us | 3,926 us | 2.0x | | Zipf alpha=0.5 | 8,034 us | 3,912 us | 2.1x | | Zipf alpha=1.0 | 68,996 us | 16,695 us | 4.1x | | Zipf alpha=1.5 | 397,525 us | 83,434 us | 4.8x | | Zipf alpha=2.0 | 689,997 us | 132,012 us | 5.2x | | Zipf alpha=2.5 | 889,804 us | 161,805 us | 5.5x | | Zipf alpha=3.0 | 1,021,968 us | 180,512 us | 5.7x | Zipf alpha=1.5 matches the ~420,000 us observed in the IG Threads LSR Slimper training trace. The redirect achieves 4.8x speedup for this case. **Threshold sweep on AMD MI350X (justifies removing the >100K gate). All bf16, dst=[634364, 128]:** Uniform indices: | src_size | index_add_ | scatter_add_ | Speedup | |---------:|-----------:|-------------:|--------:| | 1,000 | 21.6us | 21.5us | 1.00x | | 5,000 | 24.5us | 21.5us | 1.14x | | 10,000 | 28.4us | 22.6us | 1.26x | | 50,000 | 62.0us | 36.8us | 1.69x | | 100,000 | 105.0us | 55.5us | 1.89x | | 200,000 | 193.7us | 93.3us | 2.08x | |1,000,000 | 889.4us | 451.7us | 1.97x | |9,039,274 | 7,698.5us | 3,935.3us | 1.96x | Zipf alpha=1.5 (the contention regime that motivated this change): | src_size | index_add_ | scatter_add_ | Speedup | |---------:|-----------:|-------------:|--------:| | 1,000 | 61.5us | 25.1us | 2.45x | | 5,000 | 230.1us | 60.9us | 3.78x | | 10,000 | 457.0us | 109.4us | 4.18x | | 50,000 | 2,224.4us | 480.9us | 4.63x | | 100,000 | 4,397.2us | 937.2us | 4.69x | |9,039,274 |397,906.9us | 83,519.0us | 4.76x | Conclusion: scatter_add_ is at parity at the smallest measured size and strictly faster from 5K onward, on every distribution tested. Removing the size gate is a clear win. Bench command: ``` buck run mode/dev-nosan-amd-gpu -m rocm70 -m fbcode//triton/cc:force_noop //scripts/haoyuz/amd/index_add:bench_index_add_powerlaw ``` **Numerical correctness on AMD MI350X (//scripts/haoyuz/amd/index_add:test_index_add_redirect, all 7 PASS):** ``` test_unique_indices_fp32 ... ok (bit-exact) test_unique_indices_bf16 ... ok (no contention -> close to f32 ref) test_small_exact_bf16 ... ok (small int inputs -> both paths exact in bf16) test_duplicates_fp32_exact ... ok (deterministic atomicAdd in fp32) test_contention_bf16_relative_error ... ok (L2 rel err < 0.01) test_high_contention_zipf_bf16 ... ok (L2 rel err < 0.5 even with 76K dupes) test_redirect_kernel_selection_via_profiler ... ok (profiler confirms scatter_add_ fires only for matching cases) ``` **test_indexing.py changes:** - `test_index_add_correctness_2d_large` (fp32 + bf16 + fp16): Compares the redirected path to a CPU f32 reference for a 200K x 64 case with duplicate indices. Uses L2 relative error for low-precision dtypes (worst-case per-element error from ~200 atomic accumulations is too large for max-abs). - `test_index_add_correctness_2d_out_of_place`: Regression test for the structured-kernel pre-copy. Exercises both `torch.index_add(self, ...)` (functional) and `torch.index_add(self, ..., out=...)` paths, which would silently produce garbage if the redirect skipped the `result.copy_(self)` seeding step. - `test_index_add_correctness_2d_alpha`: Validates alpha != 1 path correctness (which always bypasses the redirect). - `test_index_add_redirect_kernel_selection_hip`: HIP-only profiler-based test that asserts (a) redirect fires when alpha=1, dim=0, 2D, and (b) is bypassed for alpha != 1, dim != 0, 1D, and 3D inputs. This addresses reviewer feedback that previous tests only checked numerical correctness, not which kernel was selected. **AI review fixes incorporated in this revision:** - BUG (critical): Out-of-place `torch.index_add(self, ...)` and `torch.index_add(..., out=)` reach the same TORCH_IMPL_FUNC via `structured_delegate` and start with an uninitialized `result`. The redirect was missing the `result.copy_(self)` seeding that the legacy `index_add_cuda_impl` does at the top of its body — fixed by mirroring that pre-copy. - Test gap: added `test_index_add_correctness_2d_out_of_place` regression for the above. - Test fix: `test_small_exact_bf16` previously computed `dst_before` (bypass) but never asserted on it; added assertion. - Test stability: `test_index_add_correctness_2d_large` used `atol=1e-1` per-element max for bf16, which is borderline tight for 200 dupes/row; switched to L2 relative error (< 1e-2). - Test guard: standalone `test_index_add_redirect.py` is now class-level HIP-only (numerical comparisons on NVIDIA exercise the same kernel for both paths, providing no real coverage). Co-authored-by: Claude Reviewed By: echen4096 Differential Revision: D100635529 Pull Request resolved: pytorch#180430 Approved by: https://github.com/echen4096
… large tensors (pytorch#180430) (pytorch#180430)" This reverts commit 2deace1. Reverted pytorch#180430 on behalf of https://github.com/albanD due to The unstable job hid the failure. But you can see drci above for real cuda errors on index_add_ ([comment](pytorch#180430 (comment)))
… large tensors (pytorch#181955) (pytorch#181955) Summary: Reland of D100635529 (pytorch#180430), which was reverted in D103039749 because the new bf16 path of `test_index_add_correctness_2d_out_of_place` failed on NVIDIA OSS CI. ngimel pointed out that with `num_dest=200, num_src=5000` (~25 atomic adds per destination row), the per-element rounding from NVIDIA's `index_add_` kernel exceeds the original `atol=5e-2` max-abs tolerance — exactly the rounding behavior expected for bf16 atomics under contention. Local repro on H100 confirmed the same failure (max abs diff ~0.39 vs 0.05 allowed) and showed that two more nearby tests (`test_index_add_correctness_2d_large` and `test_index_add_correctness_2d_alpha`) had similarly tight tolerances that fail on NVIDIA when the high-contention atomic-add ordering diverges from the CPU sequential reference. All AMD-side changes from D100635529 are restored — the HIP-only `index_add_ → scatter_add_` redirect in `Indexing.cu` and the new `scripts/haoyuz/amd/index_add/` benchmark + standalone test. The only difference vs the reverted version is in the three new tests in `caffe2/test/test_indexing.py` that compare a high-contention GPU result against a CPU fp32 sequential reference. Per-element max-abs/rel tolerances are replaced with bounds that reflect the actual atomic-add accumulation noise: bf16/fp16 use L2 relative error with a 5e-2 ceiling (measured 0.017 worst-case), and fp32 uses atol=rtol=1e-3 (measured 1.3e-5 to 1.8e-4 worst-case). The HIP redirect itself is unchanged and the AMD MI350X performance numbers (4.8x speedup on Zipf alpha=1.5) still hold. [PR pytorch#146264](pytorch#146264) has more details on `scatter_add` optimizations for AMD MI300 / MI350 GPUs. Co-authored-by: Claude Test Plan: On NVIDIA H100 (devvm2464), all six new index_add CUDA tests pass, and the broader `TestIndexing` suite is clean: ``` PYTORCH_TEST_REMOTE_GPU=1 .../test_others_cuda.par -r 'index_add_correctness' test_index_add_correctness_2d_alpha_cuda ... ok test_index_add_correctness_2d_large_cuda_bfloat16 ... ok test_index_add_correctness_2d_large_cuda_float16 ... ok test_index_add_correctness_2d_large_cuda_float32 ... ok test_index_add_correctness_2d_out_of_place_cuda_bfloat16 ... ok test_index_add_correctness_2d_out_of_place_cuda_float32 ... ok Ran 6 tests in 0.139s PYTORCH_TEST_REMOTE_GPU=1 .../test_others_cuda.par -r 'TestIndexing' Ran 176 tests in 8.290s OK (skipped=1) ``` Confirmed the fix actually addresses the original failure: with the prior tolerances re-applied, `test_index_add_correctness_2d_out_of_place_cuda_bfloat16` reproduces the exact OSS CI failure on H100 (Mismatched elements: 54 / 6400, greatest absolute difference: 0.3878 vs 0.05 allowed). The pre-existing AMD MI350X benchmarks and standalone tests from D100635529 are unchanged. Reverted-by: D103039749 Reviewed By: echen4096 Differential Revision: D103100245 Pull Request resolved: pytorch#181955 Approved by: https://github.com/echen4096
…ensors (pytorch#180430) (pytorch#180430) Summary: CONTEXT: On AMD MI350X, index_add_ with large source tensors and Zipf-distributed indices (common in MPZCH embedding tables) suffers from severe bf16 atomicAdd contention. The indexFuncLargeIndex kernel serializes on CAS-loop atomics when many threads target the same destination rows. WHAT: When running on HIP with alpha=1, dim=0, and 2D tensors, redirect index_add_ to scatter_add_, which uses a different kernel path that avoids the atomicAdd contention bottleneck. The redirect now fires unconditionally for the matching shape; an earlier revision gated it on `source.size(0) > 100000`, but a size sweep showed scatter_add_ is at parity with index_add_ at n=1K and strictly faster from n=5K onward, so the threshold was unnecessarily conservative. The redirect mirrors `index_add_cuda_impl`'s structured-kernel pre-copy: `if (!result.is_same(self)) result.copy_(self);` — required so that the out-of-place `torch.index_add(self, ...)` and `torch.index_add(self, ..., out=...)` paths (which share the same TORCH_IMPL_FUNC via structured_delegate) seed `result` before scatter_add_ accumulates into it. Also adds: - bench_index_add_powerlaw.py: reproduces the issue with Zipf-distributed indices at various alpha values, plus a size sweep that justifies removing the threshold. - test_index_add_redirect_kernel_selection_hip in test_indexing.py: verifies via torch.profiler that the redirect actually fires for matching cases and is bypassed for alpha != 1, dim != 0, 1D, and 3D inputs (addressing reviewer feedback that prior tests only covered numerical correctness, not selection). - test_index_add_correctness_2d_out_of_place: regression test for the structured-kernel pre-copy path (functional and explicit-out forms of torch.index_add). Test Plan: **Existing benchmark on AMD MI350X (gfx950), dst=[634364, 128] bf16, src=[9039274, 128] bf16:** | Distribution | index_add_ (before) | scatter_add_ (after) | Speedup | |------------------|---------------------|----------------------|---------| | Uniform | 7,673 us | 3,926 us | 2.0x | | Zipf alpha=0.5 | 8,034 us | 3,912 us | 2.1x | | Zipf alpha=1.0 | 68,996 us | 16,695 us | 4.1x | | Zipf alpha=1.5 | 397,525 us | 83,434 us | 4.8x | | Zipf alpha=2.0 | 689,997 us | 132,012 us | 5.2x | | Zipf alpha=2.5 | 889,804 us | 161,805 us | 5.5x | | Zipf alpha=3.0 | 1,021,968 us | 180,512 us | 5.7x | Zipf alpha=1.5 matches the ~420,000 us observed in the IG Threads LSR Slimper training trace. The redirect achieves 4.8x speedup for this case. **Threshold sweep on AMD MI350X (justifies removing the >100K gate). All bf16, dst=[634364, 128]:** Uniform indices: | src_size | index_add_ | scatter_add_ | Speedup | |---------:|-----------:|-------------:|--------:| | 1,000 | 21.6us | 21.5us | 1.00x | | 5,000 | 24.5us | 21.5us | 1.14x | | 10,000 | 28.4us | 22.6us | 1.26x | | 50,000 | 62.0us | 36.8us | 1.69x | | 100,000 | 105.0us | 55.5us | 1.89x | | 200,000 | 193.7us | 93.3us | 2.08x | |1,000,000 | 889.4us | 451.7us | 1.97x | |9,039,274 | 7,698.5us | 3,935.3us | 1.96x | Zipf alpha=1.5 (the contention regime that motivated this change): | src_size | index_add_ | scatter_add_ | Speedup | |---------:|-----------:|-------------:|--------:| | 1,000 | 61.5us | 25.1us | 2.45x | | 5,000 | 230.1us | 60.9us | 3.78x | | 10,000 | 457.0us | 109.4us | 4.18x | | 50,000 | 2,224.4us | 480.9us | 4.63x | | 100,000 | 4,397.2us | 937.2us | 4.69x | |9,039,274 |397,906.9us | 83,519.0us | 4.76x | Conclusion: scatter_add_ is at parity at the smallest measured size and strictly faster from 5K onward, on every distribution tested. Removing the size gate is a clear win. Bench command: ``` buck run mode/dev-nosan-amd-gpu -m rocm70 -m fbcode//triton/cc:force_noop //scripts/haoyuz/amd/index_add:bench_index_add_powerlaw ``` **Numerical correctness on AMD MI350X (//scripts/haoyuz/amd/index_add:test_index_add_redirect, all 7 PASS):** ``` test_unique_indices_fp32 ... ok (bit-exact) test_unique_indices_bf16 ... ok (no contention -> close to f32 ref) test_small_exact_bf16 ... ok (small int inputs -> both paths exact in bf16) test_duplicates_fp32_exact ... ok (deterministic atomicAdd in fp32) test_contention_bf16_relative_error ... ok (L2 rel err < 0.01) test_high_contention_zipf_bf16 ... ok (L2 rel err < 0.5 even with 76K dupes) test_redirect_kernel_selection_via_profiler ... ok (profiler confirms scatter_add_ fires only for matching cases) ``` **test_indexing.py changes:** - `test_index_add_correctness_2d_large` (fp32 + bf16 + fp16): Compares the redirected path to a CPU f32 reference for a 200K x 64 case with duplicate indices. Uses L2 relative error for low-precision dtypes (worst-case per-element error from ~200 atomic accumulations is too large for max-abs). - `test_index_add_correctness_2d_out_of_place`: Regression test for the structured-kernel pre-copy. Exercises both `torch.index_add(self, ...)` (functional) and `torch.index_add(self, ..., out=...)` paths, which would silently produce garbage if the redirect skipped the `result.copy_(self)` seeding step. - `test_index_add_correctness_2d_alpha`: Validates alpha != 1 path correctness (which always bypasses the redirect). - `test_index_add_redirect_kernel_selection_hip`: HIP-only profiler-based test that asserts (a) redirect fires when alpha=1, dim=0, 2D, and (b) is bypassed for alpha != 1, dim != 0, 1D, and 3D inputs. This addresses reviewer feedback that previous tests only checked numerical correctness, not which kernel was selected. **AI review fixes incorporated in this revision:** - BUG (critical): Out-of-place `torch.index_add(self, ...)` and `torch.index_add(..., out=)` reach the same TORCH_IMPL_FUNC via `structured_delegate` and start with an uninitialized `result`. The redirect was missing the `result.copy_(self)` seeding that the legacy `index_add_cuda_impl` does at the top of its body — fixed by mirroring that pre-copy. - Test gap: added `test_index_add_correctness_2d_out_of_place` regression for the above. - Test fix: `test_small_exact_bf16` previously computed `dst_before` (bypass) but never asserted on it; added assertion. - Test stability: `test_index_add_correctness_2d_large` used `atol=1e-1` per-element max for bf16, which is borderline tight for 200 dupes/row; switched to L2 relative error (< 1e-2). - Test guard: standalone `test_index_add_redirect.py` is now class-level HIP-only (numerical comparisons on NVIDIA exercise the same kernel for both paths, providing no real coverage). Co-authored-by: Claude Reviewed By: echen4096 Differential Revision: D100635529 Pull Request resolved: pytorch#180430 Approved by: https://github.com/echen4096
… large tensors (pytorch#180430) (pytorch#180430)" This reverts commit 2deace1. Reverted pytorch#180430 on behalf of https://github.com/albanD due to The unstable job hid the failure. But you can see drci above for real cuda errors on index_add_ ([comment](pytorch#180430 (comment)))
… large tensors (pytorch#181955) (pytorch#181955) Summary: Reland of D100635529 (pytorch#180430), which was reverted in D103039749 because the new bf16 path of `test_index_add_correctness_2d_out_of_place` failed on NVIDIA OSS CI. ngimel pointed out that with `num_dest=200, num_src=5000` (~25 atomic adds per destination row), the per-element rounding from NVIDIA's `index_add_` kernel exceeds the original `atol=5e-2` max-abs tolerance — exactly the rounding behavior expected for bf16 atomics under contention. Local repro on H100 confirmed the same failure (max abs diff ~0.39 vs 0.05 allowed) and showed that two more nearby tests (`test_index_add_correctness_2d_large` and `test_index_add_correctness_2d_alpha`) had similarly tight tolerances that fail on NVIDIA when the high-contention atomic-add ordering diverges from the CPU sequential reference. All AMD-side changes from D100635529 are restored — the HIP-only `index_add_ → scatter_add_` redirect in `Indexing.cu` and the new `scripts/haoyuz/amd/index_add/` benchmark + standalone test. The only difference vs the reverted version is in the three new tests in `caffe2/test/test_indexing.py` that compare a high-contention GPU result against a CPU fp32 sequential reference. Per-element max-abs/rel tolerances are replaced with bounds that reflect the actual atomic-add accumulation noise: bf16/fp16 use L2 relative error with a 5e-2 ceiling (measured 0.017 worst-case), and fp32 uses atol=rtol=1e-3 (measured 1.3e-5 to 1.8e-4 worst-case). The HIP redirect itself is unchanged and the AMD MI350X performance numbers (4.8x speedup on Zipf alpha=1.5) still hold. [PR pytorch#146264](pytorch#146264) has more details on `scatter_add` optimizations for AMD MI300 / MI350 GPUs. Co-authored-by: Claude Test Plan: On NVIDIA H100 (devvm2464), all six new index_add CUDA tests pass, and the broader `TestIndexing` suite is clean: ``` PYTORCH_TEST_REMOTE_GPU=1 .../test_others_cuda.par -r 'index_add_correctness' test_index_add_correctness_2d_alpha_cuda ... ok test_index_add_correctness_2d_large_cuda_bfloat16 ... ok test_index_add_correctness_2d_large_cuda_float16 ... ok test_index_add_correctness_2d_large_cuda_float32 ... ok test_index_add_correctness_2d_out_of_place_cuda_bfloat16 ... ok test_index_add_correctness_2d_out_of_place_cuda_float32 ... ok Ran 6 tests in 0.139s PYTORCH_TEST_REMOTE_GPU=1 .../test_others_cuda.par -r 'TestIndexing' Ran 176 tests in 8.290s OK (skipped=1) ``` Confirmed the fix actually addresses the original failure: with the prior tolerances re-applied, `test_index_add_correctness_2d_out_of_place_cuda_bfloat16` reproduces the exact OSS CI failure on H100 (Mismatched elements: 54 / 6400, greatest absolute difference: 0.3878 vs 0.05 allowed). The pre-existing AMD MI350X benchmarks and standalone tests from D100635529 are unchanged. Reverted-by: D103039749 Reviewed By: echen4096 Differential Revision: D103100245 Pull Request resolved: pytorch#181955 Approved by: https://github.com/echen4096
…ensors (pytorch#180430) (pytorch#180430) Summary: CONTEXT: On AMD MI350X, index_add_ with large source tensors and Zipf-distributed indices (common in MPZCH embedding tables) suffers from severe bf16 atomicAdd contention. The indexFuncLargeIndex kernel serializes on CAS-loop atomics when many threads target the same destination rows. WHAT: When running on HIP with alpha=1, dim=0, and 2D tensors, redirect index_add_ to scatter_add_, which uses a different kernel path that avoids the atomicAdd contention bottleneck. The redirect now fires unconditionally for the matching shape; an earlier revision gated it on `source.size(0) > 100000`, but a size sweep showed scatter_add_ is at parity with index_add_ at n=1K and strictly faster from n=5K onward, so the threshold was unnecessarily conservative. The redirect mirrors `index_add_cuda_impl`'s structured-kernel pre-copy: `if (!result.is_same(self)) result.copy_(self);` — required so that the out-of-place `torch.index_add(self, ...)` and `torch.index_add(self, ..., out=...)` paths (which share the same TORCH_IMPL_FUNC via structured_delegate) seed `result` before scatter_add_ accumulates into it. Also adds: - bench_index_add_powerlaw.py: reproduces the issue with Zipf-distributed indices at various alpha values, plus a size sweep that justifies removing the threshold. - test_index_add_redirect_kernel_selection_hip in test_indexing.py: verifies via torch.profiler that the redirect actually fires for matching cases and is bypassed for alpha != 1, dim != 0, 1D, and 3D inputs (addressing reviewer feedback that prior tests only covered numerical correctness, not selection). - test_index_add_correctness_2d_out_of_place: regression test for the structured-kernel pre-copy path (functional and explicit-out forms of torch.index_add). Test Plan: **Existing benchmark on AMD MI350X (gfx950), dst=[634364, 128] bf16, src=[9039274, 128] bf16:** | Distribution | index_add_ (before) | scatter_add_ (after) | Speedup | |------------------|---------------------|----------------------|---------| | Uniform | 7,673 us | 3,926 us | 2.0x | | Zipf alpha=0.5 | 8,034 us | 3,912 us | 2.1x | | Zipf alpha=1.0 | 68,996 us | 16,695 us | 4.1x | | Zipf alpha=1.5 | 397,525 us | 83,434 us | 4.8x | | Zipf alpha=2.0 | 689,997 us | 132,012 us | 5.2x | | Zipf alpha=2.5 | 889,804 us | 161,805 us | 5.5x | | Zipf alpha=3.0 | 1,021,968 us | 180,512 us | 5.7x | Zipf alpha=1.5 matches the ~420,000 us observed in the IG Threads LSR Slimper training trace. The redirect achieves 4.8x speedup for this case. **Threshold sweep on AMD MI350X (justifies removing the >100K gate). All bf16, dst=[634364, 128]:** Uniform indices: | src_size | index_add_ | scatter_add_ | Speedup | |---------:|-----------:|-------------:|--------:| | 1,000 | 21.6us | 21.5us | 1.00x | | 5,000 | 24.5us | 21.5us | 1.14x | | 10,000 | 28.4us | 22.6us | 1.26x | | 50,000 | 62.0us | 36.8us | 1.69x | | 100,000 | 105.0us | 55.5us | 1.89x | | 200,000 | 193.7us | 93.3us | 2.08x | |1,000,000 | 889.4us | 451.7us | 1.97x | |9,039,274 | 7,698.5us | 3,935.3us | 1.96x | Zipf alpha=1.5 (the contention regime that motivated this change): | src_size | index_add_ | scatter_add_ | Speedup | |---------:|-----------:|-------------:|--------:| | 1,000 | 61.5us | 25.1us | 2.45x | | 5,000 | 230.1us | 60.9us | 3.78x | | 10,000 | 457.0us | 109.4us | 4.18x | | 50,000 | 2,224.4us | 480.9us | 4.63x | | 100,000 | 4,397.2us | 937.2us | 4.69x | |9,039,274 |397,906.9us | 83,519.0us | 4.76x | Conclusion: scatter_add_ is at parity at the smallest measured size and strictly faster from 5K onward, on every distribution tested. Removing the size gate is a clear win. Bench command: ``` buck run mode/dev-nosan-amd-gpu -m rocm70 -m fbcode//triton/cc:force_noop //scripts/haoyuz/amd/index_add:bench_index_add_powerlaw ``` **Numerical correctness on AMD MI350X (//scripts/haoyuz/amd/index_add:test_index_add_redirect, all 7 PASS):** ``` test_unique_indices_fp32 ... ok (bit-exact) test_unique_indices_bf16 ... ok (no contention -> close to f32 ref) test_small_exact_bf16 ... ok (small int inputs -> both paths exact in bf16) test_duplicates_fp32_exact ... ok (deterministic atomicAdd in fp32) test_contention_bf16_relative_error ... ok (L2 rel err < 0.01) test_high_contention_zipf_bf16 ... ok (L2 rel err < 0.5 even with 76K dupes) test_redirect_kernel_selection_via_profiler ... ok (profiler confirms scatter_add_ fires only for matching cases) ``` **test_indexing.py changes:** - `test_index_add_correctness_2d_large` (fp32 + bf16 + fp16): Compares the redirected path to a CPU f32 reference for a 200K x 64 case with duplicate indices. Uses L2 relative error for low-precision dtypes (worst-case per-element error from ~200 atomic accumulations is too large for max-abs). - `test_index_add_correctness_2d_out_of_place`: Regression test for the structured-kernel pre-copy. Exercises both `torch.index_add(self, ...)` (functional) and `torch.index_add(self, ..., out=...)` paths, which would silently produce garbage if the redirect skipped the `result.copy_(self)` seeding step. - `test_index_add_correctness_2d_alpha`: Validates alpha != 1 path correctness (which always bypasses the redirect). - `test_index_add_redirect_kernel_selection_hip`: HIP-only profiler-based test that asserts (a) redirect fires when alpha=1, dim=0, 2D, and (b) is bypassed for alpha != 1, dim != 0, 1D, and 3D inputs. This addresses reviewer feedback that previous tests only checked numerical correctness, not which kernel was selected. **AI review fixes incorporated in this revision:** - BUG (critical): Out-of-place `torch.index_add(self, ...)` and `torch.index_add(..., out=)` reach the same TORCH_IMPL_FUNC via `structured_delegate` and start with an uninitialized `result`. The redirect was missing the `result.copy_(self)` seeding that the legacy `index_add_cuda_impl` does at the top of its body — fixed by mirroring that pre-copy. - Test gap: added `test_index_add_correctness_2d_out_of_place` regression for the above. - Test fix: `test_small_exact_bf16` previously computed `dst_before` (bypass) but never asserted on it; added assertion. - Test stability: `test_index_add_correctness_2d_large` used `atol=1e-1` per-element max for bf16, which is borderline tight for 200 dupes/row; switched to L2 relative error (< 1e-2). - Test guard: standalone `test_index_add_redirect.py` is now class-level HIP-only (numerical comparisons on NVIDIA exercise the same kernel for both paths, providing no real coverage). Co-authored-by: Claude Reviewed By: echen4096 Differential Revision: D100635529 Pull Request resolved: pytorch#180430 Approved by: https://github.com/echen4096
… large tensors (pytorch#180430) (pytorch#180430)" This reverts commit 2deace1. Reverted pytorch#180430 on behalf of https://github.com/albanD due to The unstable job hid the failure. But you can see drci above for real cuda errors on index_add_ ([comment](pytorch#180430 (comment)))
Summary:
CONTEXT: On AMD MI350X, index_add_ with large source tensors and Zipf-distributed indices (common in MPZCH embedding tables) suffers from severe bf16 atomicAdd contention. The indexFuncLargeIndex kernel serializes on CAS-loop atomics when many threads target the same destination rows.
WHAT: When running on HIP with alpha=1, dim=0, and 2D tensors, redirect index_add_ to scatter_add_, which uses a different kernel path that avoids the atomicAdd contention bottleneck. The redirect now fires unconditionally for the matching shape; an earlier revision gated it on
source.size(0) > 100000, but a size sweep showed scatter_add_ is at parity with index_add_ at n=1K and strictly faster from n=5K onward, so the threshold was unnecessarily conservative.The redirect mirrors
index_add_cuda_impl's structured-kernel pre-copy:if (!result.is_same(self)) result.copy_(self);— required so that the out-of-placetorch.index_add(self, ...)andtorch.index_add(self, ..., out=...)paths (which share the same TORCH_IMPL_FUNC via structured_delegate) seedresultbefore scatter_add_ accumulates into it.Also adds:
Test Plan:
Existing benchmark on AMD MI350X (gfx950), dst=[634364, 128] bf16, src=[9039274, 128] bf16:
Zipf alpha=1.5 matches the ~420,000 us observed in the IG Threads LSR Slimper training trace. The redirect achieves 4.8x speedup for this case.
Threshold sweep on AMD MI350X (justifies removing the >100K gate). All bf16, dst=[634364, 128]:
Uniform indices:
Zipf alpha=1.5 (the contention regime that motivated this change):
Conclusion: scatter_add_ is at parity at the smallest measured size and strictly faster from 5K onward, on every distribution tested. Removing the size gate is a clear win. Bench command:
Numerical correctness on AMD MI350X (//scripts/haoyuz/amd/index_add:test_index_add_redirect, all 7 PASS):
test_indexing.py changes:
test_index_add_correctness_2d_large(fp32 + bf16 + fp16): Compares the redirected path to a CPU f32 reference for a 200K x 64 case with duplicate indices. Uses L2 relative error for low-precision dtypes (worst-case per-element error from ~200 atomic accumulations is too large for max-abs).test_index_add_correctness_2d_out_of_place: Regression test for the structured-kernel pre-copy. Exercises bothtorch.index_add(self, ...)(functional) andtorch.index_add(self, ..., out=...)paths, which would silently produce garbage if the redirect skipped theresult.copy_(self)seeding step.test_index_add_correctness_2d_alpha: Validates alpha != 1 path correctness (which always bypasses the redirect).test_index_add_redirect_kernel_selection_hip: HIP-only profiler-based test that asserts (a) redirect fires when alpha=1, dim=0, 2D, and (b) is bypassed for alpha != 1, dim != 0, 1D, and 3D inputs. This addresses reviewer feedback that previous tests only checked numerical correctness, not which kernel was selected.AI review fixes incorporated in this revision:
torch.index_add(self, ...)andtorch.index_add(..., out=)reach the same TORCH_IMPL_FUNC viastructured_delegateand start with an uninitializedresult. The redirect was missing theresult.copy_(self)seeding that the legacyindex_add_cuda_impldoes at the top of its body — fixed by mirroring that pre-copy.test_index_add_correctness_2d_out_of_placeregression for the above.test_small_exact_bf16previously computeddst_before(bypass) but never asserted on it; added assertion.test_index_add_correctness_2d_largeusedatol=1e-1per-element max for bf16, which is borderline tight for 200 dupes/row; switched to L2 relative error (< 1e-2).test_index_add_redirect.pyis now class-level HIP-only (numerical comparisons on NVIDIA exercise the same kernel for both paths, providing no real coverage).Co-authored-by: Claude
Reviewed By: echen4096
Differential Revision: D100635529