Repository navigation
Fix Metal expm1 argument reduction accuracy - #4780
Merged
Merged
Conversation
zcbenz
force-pushed
the
fix/expm1-ln2-correction
branch
from
October 31, 2026 02:13
3d2ddc7 to
fb71a24
Compare
zcbenz
approved these changes
Oct 31, 2026
malfet
added a commit
to pytorch/pytorch
that referenced
this pull request
Oct 31, 2026
Fixes #202520 Adds the missing low-part FMA of the `a - j*ln2` reduction to `c10::metal::expm1f` (vendored from MLX in #155611, same fix as ml-explore/mlx#4780), which makes it <1 ulp on [-88, 88] instead of up to ~2900 ulp for |x| > ln2/2, and drops the `|x| < 0.1` cutoff in `expm1_functor` that worked around it (and left ~10 ulp just above 0.1). ELU/CELU/SELU now use `c10::metal::expm1f` instead of `precise::exp(x) - 1`, which returned 0 for small negative inputs Extends `test_expm1_small` to ~79 and the ln2/2 boundary with rtol=3e-7 (fails on MPS before this change) and removes the now-passing complex64 `test_reference_numerics_small` xfail for expm1. Adds a similar `test_elu_small` for ELU/CELU/SELU (also fails on MPS before this change). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> ghstack-source-id: 47552eb Pull-Request: #202897
pytorchmergebot
pushed a commit
to pytorch/pytorch
that referenced
this pull request
Nov 1, 2026
Fixes #202520 Adds the missing low-part FMA of the `a - j*ln2` reduction to `c10::metal::expm1f` (vendored from MLX in #155611, same fix as ml-explore/mlx#4780), which makes it <1 ulp on [-88, 88] instead of up to ~2900 ulp for |x| > ln2/2, and drops the `|x| < 0.1` cutoff in `expm1_functor` that worked around it (and left ~10 ulp just above 0.1). ELU/CELU/SELU now use `c10::metal::expm1f` instead of `precise::exp(x) - 1`, which returned 0 for small negative inputs Extends `test_expm1_small` to ~79 and the ln2/2 boundary with rtol=3e-7 (fails on MPS before this change) and removes the now-passing complex64 `test_reference_numerics_small` xfail for expm1. Adds a similar `test_elu_small` for ELU/CELU/SELU (also fails on MPS before this change). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Pull Request resolved: #202897 Approved by: https://github.com/Skylion007 ghstack dependencies: #202890
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Restore the low-part ln(2) correction in the Metal
expm1fargument reduction. Omitting it biases the reduced argument and propagates accuracy errors toexpm1,erf, and exact GELU.Add a small GPU regression test comparing eight float32 inputs against NumPy float64 results. The test fails without the correction and passes with it.
Validation:
MLX_METAL_JIT=ONsucceeded.expm1,erf, and GELU tests passed.git diff --checkpassed.Fixes #4765