forked from facebookresearch/ClassyVision
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add better support for testing torch.allclose (facebookresearch#652)
Summary: Pull Request resolved: facebookresearch#652 Implemented a new `ClassyTestCase` with support for `assertTorchAllClose`. This prints in the assertion what the failure inputs are which makes writing tests easier. Reviewed By: kazhang Differential Revision: D24911128 fbshipit-source-id: b462b722a8d4e429f726924007d4c4855aabece1
- Loading branch information
1 parent
932aabc
commit ad95b93
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright (c) Facebook, Inc. and its affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import unittest | ||
from test.generic.utils import ClassyTestCase | ||
|
||
import torch | ||
|
||
|
||
class TestClassyTestCase(unittest.TestCase): | ||
def test_assert_torch_all_close(self): | ||
test_fixture = ClassyTestCase() | ||
|
||
data = [1.1, 2.2] | ||
tensor_1 = torch.Tensor(data) | ||
|
||
# shouldn't raise an exception | ||
tensor_2 = tensor_1 | ||
test_fixture.assertTorchAllClose(tensor_1, tensor_2) | ||
|
||
# should fail because tensors are not close | ||
tensor_2 = tensor_1 / 2 | ||
with self.assertRaises(AssertionError): | ||
test_fixture.assertTorchAllClose(tensor_1, tensor_2) | ||
|
||
# should fail because tensor_2 is not a tensor | ||
tensor_2 = data | ||
with self.assertRaises(AssertionError): | ||
test_fixture.assertTorchAllClose(tensor_1, tensor_2) | ||
|
||
# should fail because tensor_1 is not a tensor | ||
tensor_1 = data | ||
tensor_2 = torch.Tensor(data) | ||
with self.assertRaises(AssertionError): | ||
test_fixture.assertTorchAllClose(tensor_1, tensor_2) |