Skip to content

[MPS] Migrate sigmoid_backward from MPSGraph to Metal - #187151

Closed
malfet wants to merge 2 commits into
gh/malfet/951/basefrom
gh/malfet/951/head
Closed

malfet wants to merge 2 commits into
gh/malfet/951/basefrom
gh/malfet/951/head

Conversation

@malfet

@malfet malfet commented Jun 12, 2026 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

Replace the MPSGraph-based sigmoid_backward_out_mps with a native Metal kernel registered through the shared sigmoid_backward_stub.

Complex types are handled via the conjugated derivative (grad * conj((1 - output) * output)), matching CUDA's behavior.

Microbenchmark (us per call, A/B from the same script with only the impl
swapped):

shape|dtype                      mpsgraph     metal   speedup
1024|float32                        12.81      7.43     1.72x
1024|float16                        14.82      7.10     2.09x
1024|bfloat16                       12.11      7.15     1.69x
1024x1024|float16                   18.56     14.67     1.26x
1024x1024|float32                   45.02     40.06     1.12x
8x1024x1024|float32                382.21    376.67     1.01x
32x1024x1024|float32              1487.84   1493.75     1.00x

Metal wins on dispatch overhead at small sizes; parity at large sizes where
the op is memory-bandwidth bound, as expected for ~3 FLOPs/element.

Co-Authored-By: Claude Opus 4.7 [email protected]

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jun 12, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/187151

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

⏳ No Failures, 16 Pending

As of commit e2b8ebb with merge base eb80dd5 (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 added ciflow/mps Run MPS tests (subset of trunk) release notes: mps Release notes category labels Jun 12, 2026
@pytorch-bot

pytorch-bot Bot commented Jun 12, 2026

Copy link
Copy Markdown

Attention! native_functions.yaml was changed

If you are adding a new function or defaulted argument to native_functions.yaml, you cannot use it from pre-existing Python frontend code until our FC window passes (two weeks). Split your PR into two PRs, one which adds the new C++ functionality, and one that makes use of it from Python, and land them two weeks apart. See https://github.com/pytorch/pytorch/wiki/PyTorch's-Python-Frontend-Backward-and-Forward-Compatibility-Policy#forwards-compatibility-fc for more info.

[ghstack-poisoned]
malfet added a commit that referenced this pull request Jun 12, 2026
Replace the MPSGraph-based `sigmoid_backward_out_mps` with a native Metal kernel registered through the shared `sigmoid_backward_stub`.

Complex types are handled via the conjugated derivative (`grad * conj((1 - output) * output)`), matching CUDA's behavior.

Microbenchmark (us per call, A/B from the same script with only the impl
swapped):

```
shape|dtype                      mpsgraph     metal   speedup
1024|float32                        12.81      7.43     1.72x
1024|float16                        14.82      7.10     2.09x
1024|bfloat16                       12.11      7.15     1.69x
1024x1024|float16                   18.56     14.67     1.26x
1024x1024|float32                   45.02     40.06     1.12x
8x1024x1024|float32                382.21    376.67     1.01x
32x1024x1024|float32              1487.84   1493.75     1.00x
```

Metal wins on dispatch overhead at small sizes; parity at large sizes where
the op is memory-bandwidth bound, as expected for ~3 FLOPs/element.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
ghstack-source-id: fe495d5
Pull-Request: #187151
@malfet

malfet commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge -f "Lint + MPS are 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

pytorchmergebot pushed a commit that referenced this pull request Jun 14, 2026
…87228)

Replace the MPSGraph-based `log_sigmoid_forward_*_mps` / `log_sigmoid_backward_mps*` with native Metal kernels in `ActivationKernel.metal` using unary iterator.
Forward  numerically stable formula is $min(0, x) - \log1p(e^{-|x|})$. Backward uses the
observation that $\frac{d}{dx} log(sigmoid(x)) = sigmoid(-x)$, which lets us
write the gradient as `(in<0) ? (1-t) : t` with $t = \frac{z}{1+z}$, $z = e~{-|in|}$.

Replace fail in LogSigmoid float16 OpInfo `test_forward` with xfail to a `toleranceOverride(atol=5e-3, rtol=1e-3)`.

Microbenchmark (us per call, A/B from the same script with only the impl swapped):

```
shape|dtype                      fwd mpsg   fwd metal  fwd_x   bwd mpsg  bwd metal  bwd_x
1024|float32                        10.68       6.61   1.62x      30.00      15.94   1.88x
1024|float16                        10.85       6.77   1.60x      27.46      17.95   1.53x
1024|bfloat16                       11.83       6.70   1.77x      26.64      14.22   1.87x
1024x1024|float32                   36.35      20.64   1.76x      30.05      41.84   0.72x
1024x1024|float16                   14.68      11.92   1.23x      29.53      27.60   1.07x
8x1024x1024|float32                267.45     252.40   1.06x     378.48     380.34   1.00x
32x1024x1024|float32              1012.71     987.62   1.03x    1499.97    1481.34   1.01x
```

Co-Authored-By: Claude Opus 4.7 <[email protected]>
Pull Request resolved: #187228
Approved by: https://github.com/Skylion007
ghstack dependencies: #187151
@github-actions
github-actions Bot deleted the gh/malfet/951/head branch July 13, 2026 02:26
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
Replace the MPSGraph-based `sigmoid_backward_out_mps` with a native Metal kernel registered through the shared `sigmoid_backward_stub`.

Complex types are handled via the conjugated derivative (`grad * conj((1 - output) * output)`), matching CUDA's behavior.

Microbenchmark (us per call, A/B from the same script with only the impl
swapped):

```
shape|dtype                      mpsgraph     metal   speedup
1024|float32                        12.81      7.43     1.72x
1024|float16                        14.82      7.10     2.09x
1024|bfloat16                       12.11      7.15     1.69x
1024x1024|float16                   18.56     14.67     1.26x
1024x1024|float32                   45.02     40.06     1.12x
8x1024x1024|float32                382.21    376.67     1.01x
32x1024x1024|float32              1487.84   1493.75     1.00x
```

Metal wins on dispatch overhead at small sizes; parity at large sizes where
the op is memory-bandwidth bound, as expected for ~3 FLOPs/element.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
Pull Request resolved: pytorch#187151
Approved by: https://github.com/Skylion007
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…torch#187228)

Replace the MPSGraph-based `log_sigmoid_forward_*_mps` / `log_sigmoid_backward_mps*` with native Metal kernels in `ActivationKernel.metal` using unary iterator.
Forward  numerically stable formula is $min(0, x) - \log1p(e^{-|x|})$. Backward uses the
observation that $\frac{d}{dx} log(sigmoid(x)) = sigmoid(-x)$, which lets us
write the gradient as `(in<0) ? (1-t) : t` with $t = \frac{z}{1+z}$, $z = e~{-|in|}$.

Replace fail in LogSigmoid float16 OpInfo `test_forward` with xfail to a `toleranceOverride(atol=5e-3, rtol=1e-3)`.

Microbenchmark (us per call, A/B from the same script with only the impl swapped):

```
shape|dtype                      fwd mpsg   fwd metal  fwd_x   bwd mpsg  bwd metal  bwd_x
1024|float32                        10.68       6.61   1.62x      30.00      15.94   1.88x
1024|float16                        10.85       6.77   1.60x      27.46      17.95   1.53x
1024|bfloat16                       11.83       6.70   1.77x      26.64      14.22   1.87x
1024x1024|float32                   36.35      20.64   1.76x      30.05      41.84   0.72x
1024x1024|float16                   14.68      11.92   1.23x      29.53      27.60   1.07x
8x1024x1024|float32                267.45     252.40   1.06x     378.48     380.34   1.00x
32x1024x1024|float32              1012.71     987.62   1.03x    1499.97    1481.34   1.01x
```

Co-Authored-By: Claude Opus 4.7 <[email protected]>
Pull Request resolved: pytorch#187228
Approved by: https://github.com/Skylion007
ghstack dependencies: pytorch#187151
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Replace the MPSGraph-based `sigmoid_backward_out_mps` with a native Metal kernel registered through the shared `sigmoid_backward_stub`.

Complex types are handled via the conjugated derivative (`grad * conj((1 - output) * output)`), matching CUDA's behavior.

Microbenchmark (us per call, A/B from the same script with only the impl
swapped):

```
shape|dtype                      mpsgraph     metal   speedup
1024|float32                        12.81      7.43     1.72x
1024|float16                        14.82      7.10     2.09x
1024|bfloat16                       12.11      7.15     1.69x
1024x1024|float16                   18.56     14.67     1.26x
1024x1024|float32                   45.02     40.06     1.12x
8x1024x1024|float32                382.21    376.67     1.01x
32x1024x1024|float32              1487.84   1493.75     1.00x
```

Metal wins on dispatch overhead at small sizes; parity at large sizes where
the op is memory-bandwidth bound, as expected for ~3 FLOPs/element.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
Pull Request resolved: pytorch#187151
Approved by: https://github.com/Skylion007
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…torch#187228)

Replace the MPSGraph-based `log_sigmoid_forward_*_mps` / `log_sigmoid_backward_mps*` with native Metal kernels in `ActivationKernel.metal` using unary iterator.
Forward  numerically stable formula is $min(0, x) - \log1p(e^{-|x|})$. Backward uses the
observation that $\frac{d}{dx} log(sigmoid(x)) = sigmoid(-x)$, which lets us
write the gradient as `(in<0) ? (1-t) : t` with $t = \frac{z}{1+z}$, $z = e~{-|in|}$.

Replace fail in LogSigmoid float16 OpInfo `test_forward` with xfail to a `toleranceOverride(atol=5e-3, rtol=1e-3)`.

Microbenchmark (us per call, A/B from the same script with only the impl swapped):

```
shape|dtype                      fwd mpsg   fwd metal  fwd_x   bwd mpsg  bwd metal  bwd_x
1024|float32                        10.68       6.61   1.62x      30.00      15.94   1.88x
1024|float16                        10.85       6.77   1.60x      27.46      17.95   1.53x
1024|bfloat16                       11.83       6.70   1.77x      26.64      14.22   1.87x
1024x1024|float32                   36.35      20.64   1.76x      30.05      41.84   0.72x
1024x1024|float16                   14.68      11.92   1.23x      29.53      27.60   1.07x
8x1024x1024|float32                267.45     252.40   1.06x     378.48     380.34   1.00x
32x1024x1024|float32              1012.71     987.62   1.03x    1499.97    1481.34   1.01x
```

Co-Authored-By: Claude Opus 4.7 <[email protected]>
Pull Request resolved: pytorch#187228
Approved by: https://github.com/Skylion007
ghstack dependencies: pytorch#187151
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/mps Run MPS tests (subset of trunk) Merged release notes: mps Release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants