Repository navigation
Conversation
🔗 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 SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ⏳ No Failures, 16 PendingAs of commit e2b8ebb with merge base eb80dd5 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Attention! native_functions.yaml was changedIf 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. |
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
|
@pytorchbot merge -f "Lint + MPS are 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 |
…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
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
…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
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
…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
Stack from ghstack (oldest at bottom):
Replace the MPSGraph-based
sigmoid_backward_out_mpswith a native Metal kernel registered through the sharedsigmoid_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):
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]