Skip to content

[MPS] Relax the fp16 tolerance for combinations backward in TestConsistency - #202803

Closed
lsyurea wants to merge 1 commit into
pytorch:mainfrom
lsyurea:mps/combinations-fp16-grad-tolerance
Closed

lsyurea wants to merge 1 commit into
pytorch:mainfrom
lsyurea:mps/combinations-fp16-grad-tolerance

Conversation

@lsyurea

@lsyurea lsyurea commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

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 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 #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 + 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 #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

…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]>
@pytorch-bot pytorch-bot Bot added ciflow/mps Run MPS tests (subset of trunk) topic: not user facing topic category labels Oct 30, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

🔗 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 Failures

As of commit 5853ea2 with merge base b0e48e7 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot

pytorch-bot Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@malfet
malfet marked this pull request as ready for review October 31, 2026 04:46
@malfet
malfet requested a review from Isalia20 as a code owner October 31, 2026 04:46
@github-actions github-actions Bot added bot-triaged This is a label only to be used by the auto triage bot triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Oct 31, 2026
@malfet

malfet commented Oct 31, 2026

Copy link
Copy Markdown
Collaborator

@pytorchbot merge -f "All is green"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…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]>
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…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]>

This branch was previously deployed

1 inactive deployment
bedrock — 5853ea2f Deployed Oct 31, 2026 by malfet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot-triaged This is a label only to be used by the auto triage bot ciflow/mps Run MPS tests (subset of trunk) Merged open source topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

4 participants