Repository navigation
Conversation
…stency The backward of combinations is IndexBackward0, i.e. index_put(accumulate=True), which sums the fp16 gradients of repeated elements and rounds to fp16 after every add. CPU adds them serially and MPS with parallel atomic adds, so the two round differently. Both are equally close to a float64 reference, but they can differ from each other by up to 4e-3 relative, more than the default fp16 tolerance (rtol=1e-3). As a result, test_output_grad_match_combinations_mps_float16 fails on every run on an M4 Pro and has been disabled as flaky in CI since July (pytorch#188985). See pytorch#202798 for the numbers. This adds combinations to FP16_LOW_PRECISION_LIST, which already has _unsafe_masked_index_put_accumulate (the same accumulating index_put) for the same reason. Making MPS follow CPU's order instead would need a slower kernel that is no more accurate: even the exact sum, rounded once to fp16, fails the default tolerance against CPU in 37% of random test runs. The new tolerance still catches real accumulation bugs: a simulated lost atomic add is caught in every test run. The forward is exact indexing, so the looser tolerance hides nothing there. Fixes pytorch#202798 Fixes pytorch#188985 Test Plan: ``` python test/test_mps.py -k combinations ``` test_output_grad_match_combinations_mps_float16 fails on main and passes with this change. All 12 combinations tests pass. This change was authored with the help of an AI assistant (Claude Code). Co-Authored-By: Claude Opus 5.5 <[email protected]>
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/202803
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 5853ea2 with merge base b0e48e7 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
|
|
@pytorchbot merge -f "All is green" |
Merge startedYour change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
…stency (pytorch#202803) Fixes pytorch#202798 Fixes pytorch#188985 ## Agent details (if applicable) Written with Claude Code (AI assistant). I reviewed it and ran the commands on my M4 Pro (macOS 27.0.1). The backward of `combinations` is `IndexBackward0`, i.e. `index_put(accumulate=True)`, which sums the fp16 gradients of repeated elements in a different order on MPS than on CPU. Both are equally close to a float64 reference, but they can differ from each other by up to 4e-3 relative, more than the default fp16 tolerance (rtol=1e-3). See pytorch#202798 for the numbers. This started with pytorch#186595 (landed 2026-07-05 as 5b84db3, reverted, then relanded 2026-07-15 as pytorch#189305 / 41d3568), which is when pytorch#188985 was filed. That PR replaced `meshgrid` + `masked_select` + `stack` with `self.index(nonzero_static(mask))`. Before it, the gradient for each repeated element came from expand's backward, a `sum` reduction that accumulates in fp32 on both CPU and MPS and rounds to fp16 once, so the two backends agreed exactly. After it, the backward goes through `index_put_(accumulate=True)`, which rounds to fp16 after every add. CPU does this serially in a fixed order (`cpu/IndexKernel.cpp`), and MPS does it with compare-and-swap fp16 atomics in whatever order the threads land (`AtomicType<half>` in `c10/metal/atomic.h`). Over 2000 seeds of the failing sample (`n=2, r=4, with_replacement=True`), the old formulation gives identical CPU and MPS grads every time, while the current one fails rtol=1e-3 in 6.7% of seeds (worst relative difference 2.2e-3). The same change also made fp16/bf16 `combinations` grads somewhat less accurate on every backend, which is out of scope here. This adds `combinations` to `FP16_LOW_PRECISION_LIST`, which already has `_unsafe_masked_index_put_accumulate` (the same accumulating `index_put`) for the same reason. Making MPS follow CPU's order instead would need a slower kernel that is no more accurate: even the exact sum, rounded once to fp16, fails the default tolerance against CPU in 37% of random test runs. The new tolerance still catches real accumulation bugs: a simulated lost atomic add is caught in every test run. The forward is exact indexing, so the looser tolerance hides nothing there. Closing pytorch#188985 re-enables the test in CI. Test plan: ``` python test/test_mps.py -k combinations ``` `test_output_grad_match_combinations_mps_float16` fails on main and passes with this change. All 12 `combinations` tests pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Pull Request resolved: pytorch#202803 Approved by: https://github.com/malfet Co-authored-by: Claude Opus 5.5 <[email protected]>
…stency (pytorch#202803) Fixes pytorch#202798 Fixes pytorch#188985 ## Agent details (if applicable) Written with Claude Code (AI assistant). I reviewed it and ran the commands on my M4 Pro (macOS 27.0.1). The backward of `combinations` is `IndexBackward0`, i.e. `index_put(accumulate=True)`, which sums the fp16 gradients of repeated elements in a different order on MPS than on CPU. Both are equally close to a float64 reference, but they can differ from each other by up to 4e-3 relative, more than the default fp16 tolerance (rtol=1e-3). See pytorch#202798 for the numbers. This started with pytorch#186595 (landed 2026-07-05 as 5b84db3, reverted, then relanded 2026-07-15 as pytorch#189305 / 41d3568), which is when pytorch#188985 was filed. That PR replaced `meshgrid` + `masked_select` + `stack` with `self.index(nonzero_static(mask))`. Before it, the gradient for each repeated element came from expand's backward, a `sum` reduction that accumulates in fp32 on both CPU and MPS and rounds to fp16 once, so the two backends agreed exactly. After it, the backward goes through `index_put_(accumulate=True)`, which rounds to fp16 after every add. CPU does this serially in a fixed order (`cpu/IndexKernel.cpp`), and MPS does it with compare-and-swap fp16 atomics in whatever order the threads land (`AtomicType<half>` in `c10/metal/atomic.h`). Over 2000 seeds of the failing sample (`n=2, r=4, with_replacement=True`), the old formulation gives identical CPU and MPS grads every time, while the current one fails rtol=1e-3 in 6.7% of seeds (worst relative difference 2.2e-3). The same change also made fp16/bf16 `combinations` grads somewhat less accurate on every backend, which is out of scope here. This adds `combinations` to `FP16_LOW_PRECISION_LIST`, which already has `_unsafe_masked_index_put_accumulate` (the same accumulating `index_put`) for the same reason. Making MPS follow CPU's order instead would need a slower kernel that is no more accurate: even the exact sum, rounded once to fp16, fails the default tolerance against CPU in 37% of random test runs. The new tolerance still catches real accumulation bugs: a simulated lost atomic add is caught in every test run. The forward is exact indexing, so the looser tolerance hides nothing there. Closing pytorch#188985 re-enables the test in CI. Test plan: ``` python test/test_mps.py -k combinations ``` `test_output_grad_match_combinations_mps_float16` fails on main and passes with this change. All 12 `combinations` tests pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Pull Request resolved: pytorch#202803 Approved by: https://github.com/malfet Co-authored-by: Claude Opus 5.5 <[email protected]>
Fixes #202798
Fixes #188985
Agent details (if applicable)
Written with Claude Code (AI assistant). I reviewed it and ran the commands on my M4 Pro (macOS 27.0.1).
The backward of
combinationsisIndexBackward0, i.e.index_put(accumulate=True), which sums the fp16 gradients of repeated elements in a different order on MPS than on CPU. Both are equally close to a float64 reference, but they can differ from each other by up to 4e-3 relative, more than the default fp16 tolerance (rtol=1e-3). See #202798 for the numbers.This started with #186595 (landed 2026-07-05 as 5b84db3, reverted, then relanded 2026-07-15 as #189305 / 41d3568), which is when #188985 was filed. That PR replaced
meshgrid+masked_select+stackwithself.index(nonzero_static(mask)). Before it, the gradient for each repeated element came from expand's backward, asumreduction that accumulates in fp32 on both CPU and MPS and rounds to fp16 once, so the two backends agreed exactly. After it, the backward goes throughindex_put_(accumulate=True), which rounds to fp16 after every add. CPU does this serially in a fixed order (cpu/IndexKernel.cpp), and MPS does it with compare-and-swap fp16 atomics in whatever order the threads land (AtomicType<half>inc10/metal/atomic.h). Over 2000 seeds of the failing sample (n=2, r=4, with_replacement=True), the old formulation gives identical CPU and MPS grads every time, while the current one fails rtol=1e-3 in 6.7% of seeds (worst relative difference 2.2e-3). The same change also made fp16/bf16combinationsgrads somewhat less accurate on every backend, which is out of scope here.This adds
combinationstoFP16_LOW_PRECISION_LIST, which already has_unsafe_masked_index_put_accumulate(the same accumulatingindex_put) for the same reason. Making MPS follow CPU's order instead would need a slower kernel that is no more accurate: even the exact sum, rounded once to fp16, fails the default tolerance against CPU in 37% of random test runs. The new tolerance still catches real accumulation bugs: a simulated lost atomic add is caught in every test run. The forward is exact indexing, so the looser tolerance hides nothing there. Closing #188985 re-enables the test in CI.Test plan:
test_output_grad_match_combinations_mps_float16fails on main and passes with this change. All 12combinationstests pass.🤖 Generated with Claude Code