Skip to content

Conversation

@mruberry
Copy link
Collaborator

@mruberry mruberry commented Apr 12, 2020

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:

  • 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.

@mruberry mruberry added module: complex Related to complex number support in PyTorch module: numpy Related to numpy support, and also numpy compatibility of our operators labels Apr 12, 2020
@dr-ci
Copy link

dr-ci bot commented Apr 12, 2020

💊 Build failures summary and remediations

As 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.

See how this bot performed.

This comment has been revised 11 times.

@mruberry mruberry requested a review from ngimel April 12, 2020 20:06
@mruberry
Copy link
Collaborator Author

mruberry commented Apr 12, 2020

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.

@mruberry mruberry changed the title Implements torch.isclose for complex tensors [WIP] Implements torch.isclose for complex tensors Apr 12, 2020
@anjali411 anjali411 self-requested a review April 16, 2020 16:41
@mruberry mruberry changed the title [WIP] Implements torch.isclose for complex tensors Implements torch.isclose for complex tensors Apr 17, 2020
@mruberry mruberry requested a review from ngimel April 17, 2020 04:14
Copy link
Collaborator

@ngimel ngimel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mruberry has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@mruberry merged this pull request in 4a2372b.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged module: complex Related to complex number support in PyTorch module: numpy Related to numpy support, and also numpy compatibility of our operators

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants