Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace assert torch.allclose with torch.testing.assert_allclose #6895

Merged
merged 3 commits into from
Nov 3, 2022

Conversation

pmeier
Copy link
Collaborator

@pmeier pmeier commented Nov 3, 2022

Saw this while going through the test suite. We shouldn't use the assert torch.allclose idiom for a couple of reasons:

  1. It broadcasts the inputs:
>>> a = torch.zeros(3, 2)
>>> b = torch.zeros(4, 3, 2)
>>> assert torch.allclose(a, b)
  1. It only gives a boolean output without any diagnostic information:
>>> a[0, 0] = 1
>>> assert torch.allclose(a, b)
AssertionError

Both of these things are undesirable inside the test suite. Instead we should be using torch.testing.assert_close that has neither of the issues mentioned above:

>>> a = torch.zeros(3, 2)
>>> b = torch.zeros(4, 3, 2)
>>> torch.testing.assert_close(a, b)
AssertionError: The values for attribute 'shape' do not match: torch.Size([3, 2]) != torch.Size([4, 3, 2]).
>>> b = a.clone()
>>> a[0, 0] = 1
>>> torch.testing.assert_close(a, b)
AssertionError: Tensor-likes are not close!
Mismatched elements: 1 / 6 (16.7%)
Greatest absolute difference: 1.0 at index (0, 0) (up to 1e-05 allowed)
Greatest relative difference: inf at index (0, 0) (up to 1.3e-06 allowed)

@@ -630,7 +630,7 @@ def test_nms_ref(self, iou, seed):
boxes, scores = self._create_tensors_with_iou(1000, iou)
keep_ref = self._reference_nms(boxes, scores, iou)
keep = ops.nms(boxes, scores, iou)
assert torch.allclose(keep, keep_ref), err_msg.format(iou)
torch.testing.assert_close(keep, keep_ref, msg=err_msg.format(iou))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Point 2. from above does not apply here, since supplying a msg: str overwrites the default diagnostic message.

Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

Thanks Philip, LGTM

@pmeier pmeier merged commit a2151b9 into pytorch:main Nov 3, 2022
@pmeier pmeier deleted the assert-close branch November 3, 2022 11:23
facebook-github-bot pushed a commit that referenced this pull request Nov 4, 2022
…lose (#6895)

Reviewed By: datumbox

Differential Revision: D41020557

fbshipit-source-id: a4fd1add453cb0c5f29e27ea8befec376c503df7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants