Implements torch.isclose for complex tensors#36456
Closed
Conversation
💊 Build failures summary and remediationsAs of commit f2a1779 (more details on the Dr. CI page): 💚 💚 Looks good so far! There are no failures yet. 💚 💚 This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.Please report bugs/suggestions on the GitHub issue tracker. This comment has been revised 11 times. |
mruberry
commented
Apr 12, 2020
mruberry
commented
Apr 12, 2020
mruberry
commented
Apr 12, 2020
mruberry
commented
Apr 12, 2020
mruberry
commented
Apr 12, 2020
mruberry
commented
Apr 12, 2020
mruberry
commented
Apr 12, 2020
ngimel
reviewed
Apr 12, 2020
Collaborator
Author
|
Record from offline conversation: @ngimel and I are considering whether to implement this behavior which is more like cmath/NumPy or perform a component-wise comparison of complex values. The latter is probably more pragmatically useful. We'll extend that discussion with an issue. Update: the issue is #36462. |
e08ecb1 to
43aeaf9
Compare
ngimel
approved these changes
Apr 20, 2020
added 2 commits
April 19, 2020 17:19
anjali411
approved these changes
Apr 21, 2020
Contributor
facebook-github-bot
left a comment
There was a problem hiding this comment.
@mruberry has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
Contributor
This was referenced Apr 26, 2020
laurentdupin
pushed a commit
to laurentdupin/pytorch
that referenced
this pull request
Apr 24, 2026
Summary: Previously torch.isclose would RuntimeError when called on complex tensors. This update updates torch.isclose to run on complex tensors and be consistent with [NumPy](https://numpy.org/doc/1.18/reference/generated/numpy.isclose.html). However, NumPy's handling of NaN, -inf, and inf values is odd, so I adopted Python's [cmath.isclose](https://docs.python.org/3/library/cmath.html) behavior when dealing with them. See numpy/numpy#15959 for more on NumPy's behavior. While implementing complex isclose I also simplified the isclose algorithm to: - A is close to B if A and B are equal, if equal_nan is true then NaN is equal to NaN - If A and B are finite, then A is close to B if `abs(a - b) <= (atol + abs(rtol * b))` This PR also documents torch.isclose, since it was undocumented, and adds multiple tests for its behavior to test_torch.py since it had no dedicated tests. The PR leaves equal_nan=True with complex inputs an error for now, pending the outcome of numpy/numpy#15959. Pull Request resolved: pytorch#36456 Differential Revision: D21159853 Pulled By: mruberry fbshipit-source-id: fb18fa7048e6104cc24f5ce308fdfb0ba5e4bb30
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.
Previously torch.isclose would RuntimeError when called on complex tensors. This update updates torch.isclose to run on complex tensors and be consistent with NumPy. However, NumPy's handling of NaN, -inf, and inf values is odd, so I adopted Python's cmath.isclose behavior when dealing with them. See numpy/numpy#15959 for more on NumPy's behavior.
While implementing complex isclose I also simplified the isclose algorithm to:
abs(a - b) <= (atol + abs(rtol * b))This PR also documents torch.isclose, since it was undocumented, and adds multiple tests for its behavior to test_torch.py since it had no dedicated tests.
The PR leaves equal_nan=True with complex inputs an error for now, pending the outcome of numpy/numpy#15959.